From a9f361c73aa1b103dc50a73a88e5f9c9d73faf7f Mon Sep 17 00:00:00 2001 From: DongYoung Kim Date: Wed, 24 Apr 2024 02:37:29 +0900 Subject: [PATCH 1/2] Add preLoadModules option to import modules on main thread --- src/ServerlessOffline.js | 18 ++++++++++++++++++ src/config/commandOptions.js | 4 ++++ src/config/defaultOptions.js | 1 + 3 files changed, 23 insertions(+) diff --git a/src/ServerlessOffline.js b/src/ServerlessOffline.js index 7d3f4ea32..6812dfb0d 100644 --- a/src/ServerlessOffline.js +++ b/src/ServerlessOffline.js @@ -63,6 +63,8 @@ export default class ServerlessOffline { async start() { this.#mergeOptions() + this.#preLoadModules() + const { albEvents, httpEvents, @@ -417,6 +419,18 @@ export default class ServerlessOffline { } } + #preLoadModules() { + const { preLoadModules } = this.#options + const modules = preLoadModules.split(",") + modules.forEach((module) => { + try { + import(module) + } catch (err) { + log.error(`Error importing module ${module}: ${err}`) + } + }) + } + // TODO FIXME // TEMP quick fix to expose for testing, look for better solution internals() { @@ -440,6 +454,10 @@ export default class ServerlessOffline { mergeOptions: () => { this.#mergeOptions() }, + + preLoadModules: () => { + this.#preLoadModules() + }, } } } diff --git a/src/config/commandOptions.js b/src/config/commandOptions.js index 9de897155..ec445f80f 100644 --- a/src/config/commandOptions.js +++ b/src/config/commandOptions.js @@ -135,4 +135,8 @@ export default { type: "string", usage: "Websocket port to listen on. Default: 3001.", }, + preLoadModules: { + type: "string", + usage: "A comma separated list of modules to preload on the main thread", + }, } diff --git a/src/config/defaultOptions.js b/src/config/defaultOptions.js index 9159204c6..5d6c31c6f 100644 --- a/src/config/defaultOptions.js +++ b/src/config/defaultOptions.js @@ -28,4 +28,5 @@ export default { webSocketHardTimeout: 7200, webSocketIdleTimeout: 600, websocketPort: 3001, + preLoadModules: '', } From 707fa6bc2dcae7fa1be5d31a4ab2b89f93ef91b8 Mon Sep 17 00:00:00 2001 From: DongYoung Kim Date: Mon, 19 Aug 2024 23:17:06 +0900 Subject: [PATCH 2/2] Add README document --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 78a202a59..1246d300e 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,11 @@ Default: 600 (10 minutes) WebSocket port to listen on.
Default: 3001 +#### preLoadModules + +Pre-load specified modules in the main thread to avoid crashes when importing in worker threads. Provide module names as a comma-separated list (e.g., "sharp,canvas").
+Default: '' + Any of the CLI options can be added to your `serverless.yml`. For example: ```yml