Skip to content

Commit

Permalink
Merge pull request #1777 from astron8t-voyagerx/preload-module-option
Browse files Browse the repository at this point in the history
Add preLoadModules option to import modules on main thread
  • Loading branch information
dherault authored Aug 30, 2024
2 parents 8512a10 + c8fedf9 commit f49f10f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ Default: 600 (10 minutes)
WebSocket port to listen on.<br />
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").<br />
Default: ''

Any of the CLI options can be added to your `serverless.yml`. For example:

```yml
Expand Down
18 changes: 18 additions & 0 deletions src/ServerlessOffline.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export default class ServerlessOffline {
async start() {
this.#mergeOptions()

this.#preLoadModules()

if (this.#cliOptions.noSponsor) {
log.notice()
} else {
Expand Down Expand Up @@ -423,6 +425,18 @@ export default class ServerlessOffline {
}
}

#preLoadModules() {
const modules = this.#options.preLoadModules.split(",")

modules.forEach((module) => {
try {
import(module)
} catch (error) {
log.error(`Error importing module ${module}: ${error}`)
}
})
}

// TODO FIXME
// TEMP quick fix to expose for testing, look for better solution
internals() {
Expand All @@ -446,6 +460,10 @@ export default class ServerlessOffline {
mergeOptions: () => {
this.#mergeOptions()
},

preLoadModules: () => {
this.#preLoadModules()
},
}
}
}
4 changes: 4 additions & 0 deletions src/config/commandOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,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",
},
}
1 change: 1 addition & 0 deletions src/config/defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export default {
webSocketHardTimeout: 7200,
webSocketIdleTimeout: 600,
websocketPort: 3001,
preLoadModules: '',
}

0 comments on commit f49f10f

Please sign in to comment.