Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
gatsby-core-utils
Advanced tools
A collection of gatsby utils used in different gatsby packages
The gatsby-core-utils package provides a set of utility functions that are commonly used in Gatsby projects. These utilities help with various tasks such as file system operations, URL handling, and more.
createContentDigest
This utility function creates a content digest (hash) for a given input. It is useful for generating unique identifiers for content.
const { createContentDigest } = require('gatsby-core-utils');
const data = { key: 'value' };
const digest = createContentDigest(data);
console.log(digest);
cpuCoreCount
This function returns the number of CPU cores available on the machine. It can be useful for optimizing parallel tasks.
const { cpuCoreCount } = require('gatsby-core-utils');
const coreCount = cpuCoreCount();
console.log(coreCount);
isCI
This utility checks if the code is running in a Continuous Integration (CI) environment. It can be used to conditionally execute code based on the environment.
const { isCI } = require('gatsby-core-utils');
if (isCI()) {
console.log('Running in a CI environment');
} else {
console.log('Not running in a CI environment');
}
Lodash is a popular utility library that provides a wide range of functions for common programming tasks. While it is more general-purpose compared to gatsby-core-utils, it offers similar functionalities such as deep cloning, object manipulation, and more.
Node-fetch is a lightweight module that brings window.fetch to Node.js. It is similar to gatsby-core-utils in that it provides utility functions for making HTTP requests, but it is more focused on network operations.
Fs-extra is a module that extends the built-in Node.js file system module with additional features such as copying, moving, and removing files. It is similar to gatsby-core-utils in that it provides enhanced file system operations.
gatsby-core-utils
Utilities used in multiple Gatsby packages.
npm install gatsby-core-utils
Encrypts an input using md5 hash of hexadecimal digest.
const { createContentDigest } = require("gatsby-core-utils")
const options = {
key: "value",
foo: "bar",
}
const digest = createContentDigest(options)
// ...
Calculate the number of CPU cores on the current machine
This function can be controlled by an env variable GATSBY_CPU_COUNT
setting the first argument to true.
value | description |
---|---|
Counts amount of real cores by running a shell command | |
logical_cores | require("os").cpus() to count all virtual cores |
any number | Sets cpu count to that specific number |
const { cpuCoreCount } = require("gatsby-core-utils")
const coreCount = cpuCoreCount(false)
// ...
const { cpuCoreCount } = require("gatsby-core-utils")
process.env.GATSBY_CPU_COUNT = "logical_cores"
const coreCount = cpuCoreCount()
// ...
A utility that joins paths with a /
on windows and unix-type platforms. This can also be used for URL concatenation.
const { joinPath } = require("gatsby-core-utils")
const BASEPATH = "/mybase/"
const pathname = "./gatsby/is/awesome"
const url = joinPath(BASEPATH, pathname)
// ...
A utility that enhances isCI
from 'ci-info` with support for Vercel and Heroku detection
const { isCI } = require("gatsby-core-utils")
if (isCI()) {
// execute CI-specific code
}
// ...
A utility that returns the name of the current CI environment if available, null
otherwise
const { getCIName } = require("gatsby-core-utils")
const CI_NAME = getCIName()
console.log({ CI_NAME })
// {CI_NAME: null}, or
// {CI_NAME: "Vercel"}
// ...
A cross-version polyfill for Node's Module.createRequire
.
const { createRequireFromPath } = require("gatsby-core-utils")
const requireUtil = createRequireFromPath("../src/utils/")
// Require `../src/utils/some-tool`
requireUtil("./some-tool")
// ...
When working inside workers or async operations you want some kind of concurrency control that a specific work load can only concurrent one at a time. This is what a Mutex does.
By implementing the following code, the code is only executed one at a time and the other threads/async workloads are awaited until the current one is done. This is handy when writing to the same file to disk.
const { createMutex } = require("gatsby-core-utils/mutex")
const mutex = createMutex("my-custom-mutex-key")
await mutex.acquire()
await fs.writeFile("pathToFile", "my custom content")
await mutex.release()
Parts of hash-wasm
are re-exported from gatsby-core-utils
or used in custom functions. When working on hashing where you'd normally use crypto
from Node.js, you can use these functions instead. They especially show their advantage on large inputs so don't feel obliged to always use them. Refer to hash-wasm
's documentation for more details on usage for the re-exported functions.
const { md5File, md5, createMD5, sha256, sha1 } = require("gatsby-core-utils")
// For md5, createMD5, sha256, sha1 refer to hash-wasm
await md5(`some-string`)
// md5File gives you the MD5 hex hash for a given filepath
await md5File(`package.json`)
FAQs
A collection of gatsby utils used in different gatsby packages
The npm package gatsby-core-utils receives a total of 322,125 weekly downloads. As such, gatsby-core-utils popularity was classified as popular.
We found that gatsby-core-utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.