Gathering detailed insights and metrics for gatsby-worker
Gathering detailed insights and metrics for gatsby-worker
The best React-based framework with performance, scalability and security built in.
npm install gatsby-worker
Typescript
Module System
Min. Node Version
Node Version
NPM Version
90.1
Supply Chain
98.1
Quality
84.6
Maintenance
100
Vulnerability
100
License
gatsby-source-shopify@9.0.0
Published on 07 Jan 2025
gatsby-link@5.14.1
Published on 07 Jan 2025
gatsby-source-contentful@8.15.0
Published on 07 Jan 2025
v5.14.0
Published on 06 Nov 2024
gatsby-source-shopify@8.13.2
Published on 28 Oct 2024
gatsby-source-wordpress@7.13.5 and 6 more...
Published on 28 Oct 2024
JavaScript (59%)
TypeScript (38.63%)
CSS (1.05%)
HTML (0.69%)
MDX (0.44%)
Shell (0.12%)
Dockerfile (0.03%)
PHP (0.02%)
EJS (0.01%)
Total Downloads
42,922,879
Last Day
38,189
Last Week
180,499
Last Month
803,389
Last Year
11,528,225
55,659 Stars
21,720 Commits
10,306 Forks
727 Watching
473 Branches
3,983 Contributors
Minified
Minified + Gzipped
Latest Version
2.14.0
Package Id
gatsby-worker@2.14.0
Unpacked Size
50.34 kB
Size
14.03 kB
File Count
14
NPM Version
lerna/3.22.1/node@v20.11.1+arm64 (darwin)
Node Version
20.11.1
Publised On
06 Nov 2024
Cumulative downloads
Total Downloads
Last day
-2.4%
38,189
Compared to previous day
Last week
-12.3%
180,499
Compared to previous week
Last month
3.7%
803,389
Compared to previous month
Last year
-15.4%
11,528,225
Compared to previous year
Utility to execute tasks in forked processes. Highly inspired by jest-worker
.
File worker.ts
:
1export async function heavyTask(param: string): Promise<string> {
2 // using workers is ideal for CPU intensive tasks
3 return await heavyProcessing(param)
4}
5
6export async function setupStep(param: string): Promise<void> {
7 await heavySetup(param)
8}
File parent.ts
:
1import { WorkerPool } from "gatsby-worker" 2 3const workerPool = new WorkerPool<typeof import("./worker")>( 4 require.resolve(`./worker`), 5 { 6 numWorkers: 5, 7 env: { 8 CUSTOM_ENV_VAR_TO_SET_IN_WORKER: `foo`, 9 }, 10 silent: false, 11 } 12) 13 14// queue a task on all workers 15const arrayOfPromises = workerPool.all.setupStep(`bar`) 16 17// queue a task on single worker 18const singlePromise = workerPool.single.heavyTask(`baz`)
1// TypeOfWorkerModule allows to type exposed functions ensuring type safety. 2// It will convert sync methods to async and discard/disallow usage of exports 3// that are not functions. Recommended to use with `<typeof import("path_to_worker_module")>`. 4const workerPool = new WorkerPool<TypeOfWorkerModule>( 5 // Absolute path to worker module. Recommended to use with `require.resolve` 6 workerPath: string, 7 // Not required options 8 options?: { 9 // Number of workers to spawn. Defaults to `1` if not defined. 10 numWorkers?: number 11 // Additional env vars to set in worker. Worker will inherit env vars of parent process 12 // as well as additional `GATSBY_WORKER_ID` env var (starting with "1" for first worker) 13 env?: Record<string, string>, 14 // Whether or not the output from forked process should ignored. Defaults to `false` if not defined. 15 silent?: boolean, 16 } 17)
.single
1// Exports of the worker module become available under `.single` property of `WorkerPool` instance. 2// Calling those will either start executing immediately if there are any idle workers or queue them 3// to be executed once a worker become idle. 4const singlePromise = workerPool.single.heavyTask(`baz`)
.all
1// Exports of the worker module become available under `.all` property of `WorkerPool` instance. 2// Calling those will ensure a function is executed on all workers. Best usage for this is performing 3// setup/bootstrap of workers. 4const arrayOfPromises = workerPool.all.setupStep(`baz`)
.end
1// Used to shutdown `WorkerPool`. If there are any in progress or queued tasks, promises for those will be rejected as they won't be able to complete. 2const arrayOfPromises = workerPool.end()
isWorker
1// Determine if current context is executed in worker context. Useful for conditional handling depending on context. 2import { isWorker } from "gatsby-worker" 3 4if (isWorker) { 5 // this is executed in worker context 6} else { 7 // this is NOT executed in worker context 8}
gatsby-worker
allows sending messages from worker to main and from main to worker at any time.
File message-types.ts
:
1// `gatsby-worker` supports message types. Creating common module that centralize possible messages 2// that is shared by worker and parent will ensure messages type safety. 3interface IPingMessage { 4 type: `PING` 5} 6 7interface IAnotherMessageFromChild { 8 type: `OTHER_MESSAGE_FROM_CHILD` 9 payload: { 10 foo: string 11 } 12} 13 14export type MessagesFromChild = IPingMessage | IAnotherMessageFromChild 15 16interface IPongMessage { 17 type: `PONG` 18} 19 20interface IAnotherMessageFromParent { 21 type: `OTHER_MESSAGE_FROM_PARENT` 22 payload: { 23 foo: string 24 } 25} 26 27export type MessagesFromParent = IPongMessage | IAnotherMessageFromParent
File worker.ts
:
1import { getMessenger } from "gatsby-worker" 2 3import { MessagesFromParent, MessagesFromChild } from "./message-types" 4 5const messenger = getMessenger<MessagesFromParent, MessagesFromChild>() 6// messenger might be `undefined` if `getMessenger` 7// is called NOT in worker context 8if (messenger) { 9 // send a message to a parent 10 messenger.send({ type: `PING` }) 11 messenger.send({ 12 type: `OTHER_MESSAGE_FROM_CHILD`, 13 payload: { 14 foo: `bar`, 15 }, 16 }) 17 18 // following would cause type error as message like that is 19 // not part of MessagesFromChild type union 20 // messenger.send({ type: `NOT_PART_OF_TYPES` }) 21 22 // start listening to messages from parent 23 messenger.onMessage(msg => { 24 switch (msg.type) { 25 case `PONG`: { 26 // handle PONG message 27 break 28 } 29 case `OTHER_MESSAGE_FROM_PARENT`: { 30 // msg.payload.foo will be typed as `string` here 31 // handle 32 break 33 } 34 35 // following would cause type error as there is no msg with 36 // given type as part of MessagesFromParent type union 37 // case `NOT_PART_OF_TYPES`: {} 38 } 39 }) 40}
File parent.ts
:
1import { getMessenger } from "gatsby-worker" 2 3import { MessagesFromParent, MessagesFromChild } from "./message-types" 4 5const workerPool = new WorkerPool< 6 typeof import("./worker"), 7 MessagesFromParent, 8 MessagesFromChild 9>( 10 workerPath: require.resolve(`./worker`) 11) 12 13// `sendMessage` on WorkerPool instance requires second parameter 14// `workerId` to specify to which worker to send message to 15// (`workerId` starts at 1 for first worker). 16workerPool.sendMessage( 17 { 18 type: `OTHER_MESSAGE_FROM_PARENT`, 19 payload: { 20 foo: `baz` 21 } 22 }, 23 1 24) 25 26// start listening to messages from child 27// `onMessage` callback will be called with message sent from worker 28// and `workerId` (to identify which worker send this message) 29workerPool.onMessage((msg: MessagesFromChild, workerId: number): void => { 30 switch(msg.type) { 31 case: `PING`: { 32 // send message back making sure we send it back to same worker 33 // that sent `PING` message 34 workerPool.sendMessage({ type: `PONG` }, workerId) 35 break 36 } 37 } 38})
If you are working with source files that need transpilation, you will need to make it possible to load untranspiled modules in child processes.
This can be done with @babel/register
(or similar depending on your build toolchain). Example setup:
1const testWorkerPool = new WorkerPool<WorkerModuleType>(workerModule, { 2 numWorkers, 3 env: { 4 NODE_OPTIONS: `--require ${require.resolve(`./ts-register`)}`, 5 }, 6})
This will execute additional module before allowing adding runtime support for new JavaScript syntax or support for TypeScript. Example ts-register.js
:
1// spawned process won't use jest config (or other testing framework equivalent) to support TS, so we need to add support ourselves 2require(`@babel/register`)({ 3 extensions: [`.js`, `.ts`], 4 configFile: require.resolve(relativePathToYourBabelConfig), 5 ignore: [/node_modules/], 6})
No vulnerabilities found.
Reason
security policy file detected
Details
Reason
no dangerous workflow patterns detected
Reason
22 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
license file detected
Details
Reason
no binaries found in the repo
Reason
SAST tool is not run on all commits -- score normalized to 8
Details
Reason
Found 12/16 approved changesets -- score normalized to 7
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
85 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More