Skip to content

Commit

Permalink
refactor: use provided log utils (#1784)
Browse files Browse the repository at this point in the history
* refactor: use provided log utils

* refactor: use provided log utils

* chore: prettier fix

* fix: change log to log.debug

* chore: add .DS_Store to gitignore

---------

Co-authored-by: MazurDorian <[email protected]>
  • Loading branch information
czubocha and DorianMazur authored May 19, 2024
1 parent 68f8f53 commit 93fafdb
Show file tree
Hide file tree
Showing 36 changed files with 306 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ npm-debug.log*

# IntelliJ
.idea

# MacOs
.DS_Store
286 changes: 247 additions & 39 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"./lambda": "./src/lambda/index.js"
},
"type": "module",
"main": "src/index.js",
"scripts": {
"code-quality": "npm run prettier && npm run lint",
"lint": "eslint .",
Expand Down Expand Up @@ -80,7 +81,6 @@
"@hapi/boom": "^10.0.1",
"@hapi/h2o2": "^10.0.4",
"@hapi/hapi": "^21.3.3",
"@serverless/utils": "^6.15.0",
"array-unflat-js": "^0.1.3",
"boxen": "^7.1.1",
"chalk": "^5.3.0",
Expand Down
5 changes: 3 additions & 2 deletions src/ServerlessOffline.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import process, { exit } from "node:process"
import { log } from "@serverless/utils/log.js"
import { log, setLogUtils } from "./utils/log.js"
import {
commandOptions,
CUSTOM_OPTION,
Expand Down Expand Up @@ -54,9 +54,10 @@ export default class ServerlessOffline {
"offline:start:ready": this.#ready.bind(this),
}

constructor(serverless, cliOptions) {
constructor(serverless, cliOptions, utils) {
this.#cliOptions = cliOptions
this.#serverless = serverless
setLogUtils(utils)
}

// Entry point for the plugin (sls offline) when running 'sls offline start'
Expand Down
2 changes: 1 addition & 1 deletion src/events/alb/HttpServer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Buffer } from "node:buffer"
import { exit } from "node:process"
import { Server } from "@hapi/hapi"
import { log } from "@serverless/utils/log.js"
import { log } from "../../utils/log.js"
import {
detectEncoding,
generateAlbHapiPath,
Expand Down
2 changes: 1 addition & 1 deletion src/events/authFunctionNameExtractor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from "@serverless/utils/log.js"
import { log } from "../utils/log.js"

function buildFailureResult(warningMessage) {
log.warning(warningMessage)
Expand Down
2 changes: 1 addition & 1 deletion src/events/authValidateContext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Boom from "@hapi/boom"
import { log } from "@serverless/utils/log.js"
import { log } from "../utils/log.js"

const { entries, fromEntries, values } = Object

Expand Down
2 changes: 1 addition & 1 deletion src/events/http/Endpoint.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync, readFileSync } from "node:fs"
import { log } from "@serverless/utils/log.js"
import { join } from "desm"
import { log } from "../../utils/log.js"
import OfflineEndpoint from "./OfflineEndpoint.js"

const { entries } = Object
Expand Down
2 changes: 1 addition & 1 deletion src/events/http/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { join, resolve } from "node:path"
import { exit } from "node:process"
import h2o2 from "@hapi/h2o2"
import { Server } from "@hapi/hapi"
import { log } from "@serverless/utils/log.js"
import { log } from "../../utils/log.js"
import authFunctionNameExtractor from "../authFunctionNameExtractor.js"
import authJWTSettingsExtractor from "./authJWTSettingsExtractor.js"
import createAuthScheme from "./createAuthScheme.js"
Expand Down
2 changes: 1 addition & 1 deletion src/events/http/authJWTSettingsExtractor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from "@serverless/utils/log.js"
import { log } from "../../utils/log.js"

function buildFailureResult(warningMessage) {
log.warning(warningMessage)
Expand Down
2 changes: 1 addition & 1 deletion src/events/http/createAuthScheme.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Boom from "@hapi/boom"
import { log } from "@serverless/utils/log.js"
import { log } from "../../utils/log.js"
import authCanExecuteResource from "../authCanExecuteResource.js"
import authValidateContext from "../authValidateContext.js"
import {
Expand Down
2 changes: 1 addition & 1 deletion src/events/http/createJWTAuthScheme.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Boom from "@hapi/boom"
import { log } from "@serverless/utils/log.js"
import { decodeJwt } from "jose"
import { log } from "../../utils/log.js"

const { isArray } = Array
const { now } = Date
Expand Down
2 changes: 1 addition & 1 deletion src/events/http/lambda-events/LambdaIntegrationEvent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { env } from "node:process"
import { log } from "@serverless/utils/log.js"
import { log } from "../../../utils/log.js"
import renderVelocityTemplateObject from "./renderVelocityTemplateObject.js"
import VelocityContext from "./VelocityContext.js"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Buffer } from "node:buffer"
import crypto from "node:crypto"
import { env } from "node:process"
import { log } from "@serverless/utils/log.js"
import { decodeJwt } from "jose"
import { log } from "../../../utils/log.js"
import {
detectEncoding,
formatToClfTime,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Buffer } from "node:buffer"
import { env } from "node:process"
import { log } from "@serverless/utils/log.js"
import { decodeJwt } from "jose"
import { log } from "../../../utils/log.js"
import {
detectEncoding,
formatToClfTime,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { log } from "@serverless/utils/log.js"
import velocityjs from "velocityjs"
import { log } from "../../../utils/log.js"
import runInPollutedScope from "../javaHelpers.js"
import { isPlainObject } from "../../../utils/index.js"

Expand Down
2 changes: 1 addition & 1 deletion src/events/schedule/Schedule.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// based on:
// https://github.com/ajmath/serverless-offline-scheduler

import { log } from "@serverless/utils/log.js"
import nodeSchedule from "node-schedule"
import { log } from "../../utils/log.js"
import ScheduleEvent from "./ScheduleEvent.js"
import ScheduleEventDefinition from "./ScheduleEventDefinition.js"

Expand Down
2 changes: 1 addition & 1 deletion src/events/websocket/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFile } from "node:fs/promises"
import { resolve } from "node:path"
import { exit } from "node:process"
import { Server } from "@hapi/hapi"
import { log } from "@serverless/utils/log.js"
import { log } from "../../utils/log.js"
import { catchAllRoute, connectionsRoutes } from "./http-routes/index.js"

export default class HttpServer {
Expand Down
2 changes: 1 addition & 1 deletion src/events/websocket/WebSocketClients.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WebSocket } from "ws"
import { isBoom } from "@hapi/boom"
import { log } from "@serverless/utils/log.js"
import { log } from "../../utils/log.js"
import {
WebSocketAuthorizerEvent,
WebSocketConnectEvent,
Expand Down
2 changes: 1 addition & 1 deletion src/events/websocket/WebSocketServer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crypto from "node:crypto"
import { log } from "@serverless/utils/log.js"
import { WebSocketServer as WsWebSocketServer } from "ws"
import { log } from "../../utils/log.js"

export default class WebSocketServer {
#connectionIds = new Map()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from "@serverless/utils/log.js"
import { log } from "../../../../utils/log.js"

export default function catchAllRoute() {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { log } from "@serverless/utils/log.js"
import ConnectionsController from "./ConnectionsController.js"
import { log } from "../../../../utils/log.js"

export default function connectionsRoutes(webSocketClients) {
const connectionsController = new ConnectionsController(webSocketClients)
Expand Down
2 changes: 1 addition & 1 deletion src/lambda/HttpServer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { exit } from "node:process"
import { Server } from "@hapi/hapi"
import { log } from "@serverless/utils/log.js"
import { log } from "../utils/log.js"
import { invocationsRoute, invokeAsyncRoute } from "./routes/index.js"

export default class HttpServer {
Expand Down
2 changes: 1 addition & 1 deletion src/lambda/LambdaFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { dirname, join, resolve } from "node:path"
import process from "node:process"
import { performance } from "node:perf_hooks"
import { setTimeout } from "node:timers/promises"
import { log } from "@serverless/utils/log.js"
import { emptyDir, ensureDir, remove } from "fs-extra"
import jszip from "jszip"
import { log } from "../utils/log.js"
import HandlerRunner from "./handler-runner/index.js"
import LambdaContext from "./LambdaContext.js"
import {
Expand Down
2 changes: 1 addition & 1 deletion src/lambda/handler-runner/HandlerRunner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from "@serverless/utils/log.js"
import { log } from "../../utils/log.js"
import {
supportedGo,
supportedJava,
Expand Down
2 changes: 1 addition & 1 deletion src/lambda/handler-runner/docker-runner/DockerContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { readFile, unlink, writeFile } from "node:fs/promises"
import { platform } from "node:os"
import { dirname, join, sep } from "node:path"
import { LambdaClient, GetLayerVersionCommand } from "@aws-sdk/client-lambda"
import { log, progress } from "@serverless/utils/log.js"
import { execa } from "execa"
import { ensureDir, pathExists } from "fs-extra"
import isWsl from "is-wsl"
import jszip from "jszip"
import { log, progress } from "../../../utils/log.js"
import DockerImage from "./DockerImage.js"
import Runtime from "./DockerRuntime.js"

Expand Down
2 changes: 1 addition & 1 deletion src/lambda/handler-runner/docker-runner/DockerImage.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { log } from "@serverless/utils/log.js"
import { execa } from "execa"
// TODO FIXME eslint plugin import bug, or not supporting package.json "expprts" field?
// eslint-disable-next-line import/no-unresolved
import promiseMemoize from "p-memoize"
import { log } from "../../../utils/log.js"

export default class DockerImage {
#imageNameTag = null
Expand Down
4 changes: 2 additions & 2 deletions src/lambda/handler-runner/go-runner/GoRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { mkdir, readFile, rm, rmdir, writeFile } from "node:fs/promises"
import { EOL } from "node:os"
import process, { chdir, cwd } from "node:process"
import { parse as pathParse, resolve, sep } from "node:path"
import { log } from "@serverless/utils/log.js"
import { execa } from "execa"
import { log } from "../../../utils/log.js"
import { splitHandlerPathAndName } from "../../../utils/index.js"

const { parse, stringify } = JSON
Expand Down Expand Up @@ -89,7 +89,7 @@ export default class GoRunner {
}

// Log to console in case engineers want to see the rest of the info
log(logs.join(EOL))
log.debug(logs.join(EOL))

return payload
}
Expand Down
2 changes: 1 addition & 1 deletion src/lambda/handler-runner/java-runner/JavaRunner.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EOL } from "node:os"
import process from "node:process"
import { log } from "@serverless/utils/log.js"
import { invokeJavaLocal } from "java-invoke-local"
import { log } from "../../../utils/log.js"

const { parse, stringify } = JSON
const { hasOwn } = Object
Expand Down
2 changes: 1 addition & 1 deletion src/lambda/handler-runner/python-runner/PythonRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { EOL, platform } from "node:os"
import { delimiter, join as pathJoin, relative } from "node:path"
import process, { cwd, nextTick } from "node:process"
import { createInterface } from "node:readline"
import { log } from "@serverless/utils/log.js"
import { join } from "desm"
import { log } from "../../../utils/log.js"
import { splitHandlerPathAndName } from "../../../utils/index.js"

const { parse, stringify } = JSON
Expand Down
2 changes: 1 addition & 1 deletion src/lambda/handler-runner/ruby-runner/RubyRunner.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { EOL, platform } from "node:os"
import { relative } from "node:path"
import { cwd } from "node:process"
import { log } from "@serverless/utils/log.js"
import { join } from "desm"
import { execa } from "execa"
import { log } from "../../../utils/log.js"
import { splitHandlerPathAndName } from "../../../utils/index.js"

const { parse, stringify } = JSON
Expand Down
2 changes: 1 addition & 1 deletion src/lambda/routes/invocations/InvocationsController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from "@serverless/utils/log.js"
import { log } from "../../../utils/log.js"

export default class InvocationsController {
#lambda = null
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getHttpApiCorsConfig.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from "@serverless/utils/log.js"
import { log } from "./log.js"

export default function getHttpApiCorsConfig(httpApiCors) {
if (httpApiCors === true) {
Expand Down
20 changes: 20 additions & 0 deletions src/utils/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let utils

export function setLogUtils({ log, progress }) {
utils = { log, progress }
}

export const log = {
debug: (...args) => utils?.log?.debug(...args),
error: (...args) => utils?.log?.error(...args),
info: (...args) => utils?.log?.info(...args),
notice: (...args) => utils?.log?.notice(...args),
verbose: (...args) => utils?.log?.verbose(...args),
warning: (...args) => utils?.log?.warning(...args),
}

export const progress = {
get: (namespace) => utils?.progress?.get(namespace),
notice: (message) => utils?.progress?.notice(message),
remove: () => utils?.progress?.remove(),
}
2 changes: 1 addition & 1 deletion tests/_testHelpers/pullImage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { log } from "@serverless/utils/log.js"
import { execa } from "execa"
import { log } from "../../src/utils/log.js"

export default async function pullImage(image) {
try {
Expand Down
1 change: 1 addition & 0 deletions tests/old-unit/support/OfflineBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class OfflineBuilder {
this.#serverlessOffline = new ServerlessOffline(
this.#serverlessBuilder.toObject(),
this.#options,
{},
)

this.#serverlessOffline.internals().mergeOptions()
Expand Down

0 comments on commit 93fafdb

Please sign in to comment.