Installations
npm install @ascorbic/gatsby-worker
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=14.15.0
Releases
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
Contributors
Languages
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%)
Developer
Download Statistics
Total Downloads
5,733
Last Day
1
Last Week
12
Last Month
24
Last Year
634
GitHub Statistics
55,654 Stars
21,720 Commits
10,304 Forks
727 Watching
473 Branches
3,984 Contributors
Bundle Size
6.86 kB
Minified
2.65 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.14.14
Package Id
@ascorbic/gatsby-worker@1.14.14
Unpacked Size
36.77 kB
Size
10.47 kB
File Count
15
Total Downloads
Cumulative downloads
Total Downloads
5,733
Last day
0%
1
Compared to previous day
Last week
140%
12
Compared to previous week
Last month
-50%
24
Compared to previous month
Last year
-69.7%
634
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
gatsby-worker
Utility to execute tasks in forked processes. Highly inspired by jest-worker
.
Example
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`)
API
Constructor
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}
Messaging
gatsby-worker
allows sending messages from worker to main and from main to worker at any time.
Sending messages from worker
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})
Usage with unit tests
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
- Info: security policy file detected: SECURITY.md:1
- Info: Found linked content: SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
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
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/schedule-stale.yml:10
- Info: topLevel 'contents' permission set to 'read': .github/workflows/schedule-stale.yml:6
- Info: no jobLevel write permissions found
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
no binaries found in the repo
Reason
SAST tool is not run on all commits -- score normalized to 8
Details
- Warn: 24 commits out of 27 are checked with a SAST tool
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
- Warn: no fuzzer integrations found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/schedule-stale.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/gatsbyjs/gatsby/schedule-stale.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/schedule-stale.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/gatsbyjs/gatsby/schedule-stale.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/schedule-stale.yml:51: update your workflow using https://app.stepsecurity.io/secureworkflow/gatsbyjs/gatsby/schedule-stale.yml/master?enable=pin
- Warn: containerImage not pinned by hash: .github/actions/gatsby-site-showcase-validator/Dockerfile:1: pin your Docker image by updating node:10-slim to node:10-slim@sha256:88932859e3d022d79161b99628c4c2c50e836437455e2d1b1a008d98367b10d6
- Warn: containerImage not pinned by hash: .github/actions/high-priority-prs/Dockerfile:1: pin your Docker image by updating node:10-slim to node:10-slim@sha256:88932859e3d022d79161b99628c4c2c50e836437455e2d1b1a008d98367b10d6
- Warn: containerImage not pinned by hash: .gitpod.Dockerfile:1: pin your Docker image by updating gitpod/workspace-full to gitpod/workspace-full@sha256:bec45ebdcc9b9c5ec28d5c61c16bf599200aa0d2dc1e69e2ed8ab0a424bae6db
- Warn: containerImage not pinned by hash: benchmarks/docker-runner/Dockerfile:1: pin your Docker image by updating node:14-buster to node:14-buster@sha256:a158d3b9b4e3fa813fa6c8c590b8f0a860e015ad4e59bbce5744d2f6fd8461aa
- Warn: containerImage not pinned by hash: integration-tests/gatsby-source-wordpress/docker/wordpress/Dockerfile:1: pin your Docker image by updating wordpress:5.9 to wordpress:5.9@sha256:f9d68493ee98ea8f39e6e0fc2327b48e0b555ef0ec3fcc06b8d42cbc539c49a4
- Warn: containerImage not pinned by hash: integration-tests/gatsby-source-wordpress/docker/wp-cli/Dockerfile:1: pin your Docker image by updating wordpress:cli-php7.4 to wordpress:cli-php7.4@sha256:946a8b7f237f6cf90d8f04aff952544a0332d43374d598925dcf0180e4441c6c
- Warn: npmCommand not pinned by hash: .gitpod.Dockerfile:8
- Warn: npmCommand not pinned by hash: benchmarks/docker-runner/Dockerfile:6
- Warn: npmCommand not pinned by hash: examples/build-all-examples.sh:34
- Warn: npmCommand not pinned by hash: scripts/e2e-test.sh:17
- Warn: npmCommand not pinned by hash: scripts/e2e-test.sh:17
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 0 out of 6 containerImage dependencies pinned
- Info: 1 out of 6 npmCommand dependencies pinned
Reason
85 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-9pv7-vfvm-6vr7
- Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-7hpj-7hhx-2fgx
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-rm97-x556-q36h
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-f9xv-q969-pqx4
- Warn: Project is vulnerable to: GHSA-c429-5p7v-vgjp
- Warn: Project is vulnerable to: GHSA-whgm-jr23-g3j9
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-wg6g-ppvx-927h
- Warn: Project is vulnerable to: GHSA-7gc6-qh9x-w6h8
- Warn: Project is vulnerable to: GHSA-8gh8-hqwg-xf34
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-pfq8-rq6v-vf5m
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-8cf7-32gw-wr33
- Warn: Project is vulnerable to: GHSA-hjrf-2m68-5959
- Warn: Project is vulnerable to: GHSA-qwph-4952-7xr6
- Warn: Project is vulnerable to: GHSA-3wc5-fcw2-2329
- Warn: Project is vulnerable to: GHSA-64fm-8hw2-v72w
- Warn: Project is vulnerable to: GHSA-f98w-7cxr-ff2h
- Warn: Project is vulnerable to: GHSA-cg87-wmx4-v546
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-w7rc-rwvf-8q5r
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-px4h-xg32-q955
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-3j8f-xvm3-ffx4
- Warn: Project is vulnerable to: GHSA-4p35-cfcx-8653
- Warn: Project is vulnerable to: GHSA-7f3x-x4pr-wqhj
- Warn: Project is vulnerable to: GHSA-jpp7-7chh-cf67
- Warn: Project is vulnerable to: GHSA-q6wq-5p59-983w
- Warn: Project is vulnerable to: GHSA-j9fq-vwqv-2fm2
- Warn: Project is vulnerable to: GHSA-pqw5-jmp5-px4v
- Warn: Project is vulnerable to: GHSA-566m-qj78-rww5
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-rxrc-rgv4-jpvx
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-vx3p-948g-6vhq
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-w5p7-h5w8-2hfq
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-38fc-wpqx-33j7
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
- Warn: Project is vulnerable to: GHSA-g3ch-rx76-35fx
- Warn: Project is vulnerable to: GHSA-g78m-2chm-r7qv
- Warn: Project is vulnerable to: GHSA-4r6h-8v6p-xvw6
- Warn: Project is vulnerable to: GHSA-5pgg-2g8v-p4x9
- Warn: Project is vulnerable to: GHSA-h6q6-9hqw-rwfv
- Warn: Project is vulnerable to: GHSA-5fg8-2547-mr8q
- Warn: Project is vulnerable to: GHSA-crh6-fp67-6883
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
6.8
/10
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