Skip to content

Commit

Permalink
feat: move away from old default uuid (#33275)
Browse files Browse the repository at this point in the history
* feat: move away from old default uuid

* fix test

* update uuid for v5

* fix import

* move to secure algo
  • Loading branch information
wardpeet authored Sep 27, 2021
1 parent bd59514 commit 325fdf4
Show file tree
Hide file tree
Showing 25 changed files with 132 additions and 48 deletions.
4 changes: 2 additions & 2 deletions packages/create-gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"devDependencies": {
"@ascorbic/worker-threads-shim": "^1.0.0",
"@babel/runtime": "^7.15.4",
"@lukeed/uuid": "^2.0.0",
"@types/configstore": "^4.0.0",
"@types/fs-extra": "^9.0.12",
"@types/node": "^14.17.14",
Expand All @@ -34,8 +35,7 @@
"prettier": "^2.3.2",
"string-length": "^4.0.2",
"terminal-link": "^2.1.1",
"tiny-spin": "^1.0.2",
"uuid": "3.4.0"
"tiny-spin": "^1.0.2"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-gatsby/src/tracking.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch from "node-fetch"
import uuidv4 from "uuid/v4"
import { v4 as uuidv4 } from "@lukeed/uuid"
import { getConfigStore } from "./get-config-store"

const store = getConfigStore()
Expand Down
1 change: 0 additions & 1 deletion packages/create-gatsby/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
declare module "stream-filter"
declare module "ansi-wordwrap"
declare module 'uuid/v4';
4 changes: 2 additions & 2 deletions packages/gatsby-cli/src/reporter/redux/internal-actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uuidv4 from "uuid"
import { uuid } from "gatsby-core-utils"
import { trackCli } from "gatsby-telemetry"
import signalExit from "signal-exit"
import { Dispatch } from "redux"
Expand Down Expand Up @@ -199,7 +199,7 @@ export const startActivity = ({
type: Actions.StartActivity,
payload: {
id,
uuid: uuidv4(),
uuid: uuid.v4(),
text,
type,
status,
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-core-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"babel-preset-gatsby-package": "^2.0.0-zz-next.1",
"cross-env": "^7.0.3",
"msw": "^0.35.0",
"is-uuid": "^1.0.2",
"typescript": "^4.4.2"
},
"engines": {
Expand Down
27 changes: 27 additions & 0 deletions packages/gatsby-core-utils/src/__tests__/uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copied from https://github.com/lukeed/uuid
* https://github.com/lukeed/uuid/blob/master/test/index.js
*/
import isUUID from "is-uuid"
import { v4 as uuid } from "../uuid"

describe(`uuid`, () => {
it(`returns`, () => {
const out = uuid()
expect(out).toHaveLength(36)
expect(typeof out).toBe(`string`)
})

it(`unique`, () => {
const length = 1e6
expect(uuid()).not.toBe(uuid())

const unique = new Set(Array.from({ length }, uuid))
expect(unique.size).toBe(length)
})

it(`validate`, () => {
const arr = Array.from({ length: 1e3 }, uuid)
expect(arr.map(item => isUUID.v4(item)).filter(Boolean)).toHaveLength(1e3)
})
})
1 change: 1 addition & 0 deletions packages/gatsby-core-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { getGatsbyVersion } from "./get-gatsby-version"
export { getTermProgram } from "./get-term-program"
export { fetchRemoteFile, IFetchRemoteFileOptions } from "./fetch-remote-file"
export { isTruthy } from "./is-truthy"
export * as uuid from "./uuid"
export { getMatchPath } from "./match-path"
export * from "./service-lock"
export * from "./site-metadata"
Expand Down
36 changes: 36 additions & 0 deletions packages/gatsby-core-utils/src/uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copied from https://github.com/lukeed/uuid
* https://github.com/lukeed/uuid/blob/master/src/secure.js
*/
import { randomBytes } from "crypto"

const SIZE = 4096
const HEX: Array<string> = []
let IDX = 0
let BUFFER: Buffer

for (; IDX < 256; IDX++) {
HEX[IDX] = (IDX + 256).toString(16).substring(1)
}

export function v4(): string {
if (!BUFFER || IDX + 16 > SIZE) {
BUFFER = randomBytes(SIZE)
IDX = 0
}

let i = 0
let tmp
let out = ``
for (; i < 16; i++) {
tmp = BUFFER[IDX + i]
if (i == 6) out += HEX[(tmp & 15) | 64]
else if (i == 8) out += HEX[(tmp & 63) | 128]
else out += HEX[tmp]

if (i & 1 && i > 1 && i < 11) out += `-`
}

IDX += 16
return out
}
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-benchmark-reporting/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"dependencies": {
"@babel/runtime": "^7.15.4",
"fast-glob": "^3.2.7",
"node-fetch": "^2.6.1",
"uuid": "3.4.0"
"gatsby-core-utils": "^3.0.0-zz-next.1",
"node-fetch": "^2.6.1"
},
"scripts": {
"build": "babel src --out-dir . --ignore \"**/__tests__\"",
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-benchmark-reporting/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { performance } = require(`perf_hooks`)

const { sync: glob } = require(`fast-glob`)
const nodeFetch = require(`node-fetch`)
const uuidv4 = require(`uuid/v4`)
const { uuid } = require(`gatsby-core-utils`)
const { execSync } = require(`child_process`)
const fs = require(`fs`)

Expand Down Expand Up @@ -167,7 +167,7 @@ class BenchMeta {

return {
time: this.localTime,
sessionId: process.gatsbyTelemetrySessionId || uuidv4(),
sessionId: process.gatsbyTelemetrySessionId || uuid.v4(),
cwd: process.cwd() ?? ``,
timestamps: this.timestamps,
gitHash,
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-source-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"apollo-link": "1.2.14",
"apollo-link-http": "^1.5.17",
"dataloader": "^2.0.0",
"gatsby-core-utils": "^3.0.0-zz-next.1",
"invariant": "^2.2.4",
"node-fetch": "^2.6.1",
"uuid": "3.4.0"
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@babel/cli": "^7.15.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-source-graphql/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const uuidv4 = require(`uuid/v4`)
const { uuid } = require(`gatsby-core-utils`)
const { buildSchema, printSchema } = require(`gatsby/graphql`)
const {
wrapSchema,
Expand Down Expand Up @@ -142,7 +142,7 @@ exports.sourceNodes = async (
}

function createSchemaNode({ id, typeName, fieldName, createContentDigest }) {
const nodeContent = uuidv4()
const nodeContent = uuid.v4()
const nodeContentDigest = createContentDigest(nodeContent)
return {
id,
Expand Down
3 changes: 1 addition & 2 deletions packages/gatsby-telemetry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"git-up": "^4.0.5",
"is-docker": "^2.2.1",
"lodash": "^4.17.21",
"node-fetch": "^2.6.1",
"uuid": "3.4.0"
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@babel/cli": "^7.15.4",
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby-telemetry/src/__tests__/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { AnalyticsTracker } from "../telemetry"
import * as fs from "fs-extra"
import * as os from "os"
import * as path from "path"
import uuidv4 from "uuid/v4"
import { uuid } from "gatsby-core-utils"

const uuidv4 = uuid.v4

jest.mock(`../event-storage`)

Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-telemetry/src/in-memory-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uuidv4 from "uuid/v4"
import { uuid } from "gatsby-core-utils"
import os from "os"
import { join } from "path"

Expand All @@ -13,7 +13,7 @@ export class InMemoryConfigStore {
createBaseConfig(): Record<string, unknown> {
return {
"telemetry.enabled": true,
"telemetry.machineId": `not-a-machine-id-${uuidv4()}`,
"telemetry.machineId": `not-a-machine-id-${uuid.v4()}`,
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby-telemetry/src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import uuidv4 from "uuid/v4"
import * as fs from "fs-extra"
import os from "os"
import {
isCI,
getCIName,
createContentDigest,
getTermProgram,
uuid,
} from "gatsby-core-utils"
import {
getRepositoryId as _getRepositoryId,
Expand All @@ -20,7 +20,7 @@ import { getDependencies } from "./get-dependencies"
import isDocker from "is-docker"
import lodash from "lodash"

const typedUUIDv4 = uuidv4 as () => string
const typedUUIDv4 = uuid.v4 as () => string

const finalEventRegex = /(END|STOP)$/
const dbEngine = `redux`
Expand Down Expand Up @@ -181,7 +181,7 @@ export class AnalyticsTracker {
if (inherited) {
p.gatsbyTelemetrySessionId = inherited
} else {
p.gatsbyTelemetrySessionId = uuidv4()
p.gatsbyTelemetrySessionId = uuid.v4()
process.env.INTERNAL_GATSBY_TELEMETRY_SESSION_ID =
p.gatsbyTelemetrySessionId
}
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
"true-case-path": "^2.2.1",
"type-of": "^2.0.1",
"url-loader": "^4.1.1",
"uuid": "3.4.0",
"uuid": "^8.3.2",
"v8-compile-cache": "^2.2.0",
"webpack": "^5.35.0",
"webpack-dev-middleware": "^4.1.0",
Expand Down
6 changes: 2 additions & 4 deletions packages/gatsby/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import report from "gatsby-cli/lib/reporter"
import signalExit from "signal-exit"
import fs from "fs-extra"
import telemetry from "gatsby-telemetry"
import { updateSiteMetadata, isTruthy } from "gatsby-core-utils"
import { updateSiteMetadata, isTruthy, uuid } from "gatsby-core-utils"
import {
buildRenderer,
buildHTMLPagesAndDeleteStaleArtifacts,
Expand Down Expand Up @@ -50,14 +50,12 @@ import {
import { createGraphqlEngineBundle } from "../schema/graphql-engine/bundle-webpack"
import { createPageSSRBundle } from "../utils/page-ssr-module/bundle-webpack"
import { shouldGenerateEngines } from "../utils/engines-helpers"
import uuidv4 from "uuid/v4"
import reporter from "gatsby-cli/lib/reporter"

module.exports = async function build(program: IBuildArgs): Promise<void> {
// global gatsby object to use without store
global.__GATSBY = {
buildId: uuidv4(),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
buildId: uuid.v4(),
root: program!.directory,
}

Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/commands/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import {
getService,
updateSiteMetadata,
UnlockFn,
uuid,
} from "gatsby-core-utils"
import reporter from "gatsby-cli/lib/reporter"
import { getSslCert } from "../utils/get-ssl-cert"
import { IProxyControls, startDevelopProxy } from "../utils/develop-proxy"
import { IProgram, IDebugInfo } from "./types"
import { flush as telemetryFlush } from "gatsby-telemetry"
import uuidv4 from "uuid/v4"

// Adapted from https://stackoverflow.com/a/16060619
const requireUncached = (file: string): any => {
Expand Down Expand Up @@ -200,7 +200,7 @@ const REGEX_IP =

module.exports = async (program: IProgram): Promise<void> => {
global.__GATSBY = {
buildId: uuidv4(),
buildId: uuid.v4(),
root: program.directory,
}

Expand Down
15 changes: 12 additions & 3 deletions packages/gatsby/src/redux/__tests__/jobsv2.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
const jobsManager = require(`../../utils/jobs/manager`)
import { jobsV2Reducer as jobsReducer } from "../reducers/jobsv2"
import * as jobsManager from "../../utils/jobs/manager"

jest.spyOn(jobsManager, `enqueueJob`)
jest.spyOn(jobsManager, `removeInProgressJob`)
jest.mock(`uuid/v4`, () => () => `1234`)

import { jobsV2Reducer as jobsReducer } from "../reducers/jobsv2"
jest.mock(`gatsby-core-utils`, () => {
return {
...jest.requireActual(`gatsby-core-utils`),
isCI: () => true,
uuid: {
v4: jest.fn(() => `1234`),
},
}
})

describe(`Job v2 actions/reducer`, () => {
const plugin = {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/create-node-id.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uuidv5 from "uuid/v5"
import { v5 as uuidv5 } from "uuid"
import report from "gatsby-cli/lib/reporter"

const seedConstant = `638f7a53-c567-4eca-8fc1-b23efb1cfb2b`
Expand Down
12 changes: 5 additions & 7 deletions packages/gatsby/src/utils/jobs/__tests__/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ jest.mock(
{ virtual: true }
)

jest.mock(`uuid/v4`, () =>
jest.fn().mockImplementation(jest.requireActual(`uuid/v4`))
)

const worker = require(`/node_modules/gatsby-plugin-test/gatsby-worker`)
const reporter = require(`gatsby-cli/lib/reporter`)
const hasha = require(`hasha`)
const fs = require(`fs-extra`)
const pDefer = require(`p-defer`)
const uuidv4 = require(`uuid/v4`)
const { uuid } = require(`gatsby-core-utils`)

jest.spyOn(uuid, `v4`)

fs.ensureDir = jest.fn().mockResolvedValue(true)

Expand Down Expand Up @@ -84,7 +82,7 @@ describe(`Jobs manager`, () => {
worker.TEST_JOB.mockReset()
endActivity.mockClear()
pDefer.mockClear()
uuidv4.mockClear()
uuid.v4.mockClear()
reporter.phantomActivity.mockImplementation(() => {
return {
start: jest.fn(),
Expand Down Expand Up @@ -153,7 +151,7 @@ describe(`Jobs manager`, () => {
const internalJob = createInternalJob(createMockJob(), plugin)
createInternalJob(internalJob, plugin)

expect(uuidv4).toHaveBeenCalledTimes(1)
expect(uuid.v4).toHaveBeenCalledTimes(1)
})
})

Expand Down
5 changes: 2 additions & 3 deletions packages/gatsby/src/utils/jobs/manager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import uuidv4 from "uuid/v4"
import path from "path"
import hasha from "hasha"
import fs from "fs-extra"
import pDefer from "p-defer"
import _ from "lodash"
import { createContentDigest, slash } from "gatsby-core-utils"
import { createContentDigest, slash, uuid } from "gatsby-core-utils"
import reporter from "gatsby-cli/lib/reporter"
import { IPhantomReporter } from "gatsby-cli"
import {
Expand Down Expand Up @@ -221,7 +220,7 @@ export function createInternalJob(
})

const internalJob: InternalJob = {
id: uuidv4(),
id: uuid.v4(),
name,
contentDigest: ``,
inputPaths: inputPathsWithContentDigest,
Expand Down
Loading

0 comments on commit 325fdf4

Please sign in to comment.