npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gatsby-core-utils

v4.13.1

Published

A collection of gatsby utils used in different gatsby packages

Downloads

2,457,637

Readme

gatsby-core-utils

Utilities used in multiple Gatsby packages.

Usage

npm install gatsby-core-utils

createContentDigest

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)
// ...

cpuCoreCount

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()
// ...

joinPath

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)
// ...

isCI

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
}
// ...

getCIName

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"}
// ...

createRequireFromPath

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")
// ...

Mutex

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()

Hashing

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`)