Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deps): update undici to 4.x #8842

Merged
merged 33 commits into from Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
fea217c
fix(deps): update unidici
michalkvasnicak Jun 29, 2021
e79239e
build: copy undici wasm files to dist
michalkvasnicak Jun 30, 2021
4cc8333
build: copy llhttp
michalkvasnicak Jun 30, 2021
b92f97d
fix: mark _http_common external
michalkvasnicak Jun 30, 2021
206c0ab
chore: remove fs-extra
michalkvasnicak Jun 30, 2021
3644418
Merge remote-tracking branch 'origin/main' into pr/michalkvasnicak/7931
Jolg42 Aug 19, 2021
c6b0286
Delete undici.ts
Jolg42 Aug 19, 2021
6ddf052
Delete build.js
Jolg42 Aug 19, 2021
487d6a8
add runtime/llhttp to package.json
Jolg42 Aug 19, 2021
22b805c
copy wasm to generator-build as well
Jolg42 Aug 19, 2021
3a2df2c
remove unused import
Jolg42 Aug 19, 2021
6079f14
fix(deps): update undici to 4.4.6 (#8831)
janpio Aug 20, 2021
50b79d6
Merge branch 'main' into integration/undici-4
millsp Aug 20, 2021
9659281
Apply suggestions from code review
janpio Aug 23, 2021
c0cebfd
fix(deps): update undici to 4.x (remove one of the two copy actions f…
janpio Aug 24, 2021
32ac3e2
Merge branch 'main' into integration/undici-4
janpio Aug 24, 2021
65e110c
Merge branch 'main' into integration/undici-4
millsp Aug 24, 2021
37f6c02
Merge branches 'integration/undici-4' and 'main' of github.com:prisma…
millsp Nov 15, 2021
db39f11
fix(engine-core): disable body timeout
millsp Nov 15, 2021
79460db
chore(build): inline wasm
millsp Nov 18, 2021
d885a7b
Merge branch 'main' of github.com:prisma/prisma into integration/undi…
millsp Dec 22, 2021
63c83bc
chore: update locks
millsp Dec 22, 2021
92a3334
fix(client): bundle wasm
millsp Dec 22, 2021
42aa830
fix(cli): remove wasm copy
millsp Dec 22, 2021
4dd5fb0
chore(engine-core): bump undici
millsp Dec 22, 2021
a811b01
chore: cleanup
millsp Dec 22, 2021
ac83530
Merge branch 'main' into integration/undici-4
janpio Jan 6, 2022
fb7d30d
Merge branch 'main' of github.com:prisma/prisma into integration/undi…
millsp Mar 22, 2022
c2a8ed8
fix(client): unlazify undici
millsp Mar 23, 2022
347dd06
fix(client): defer undici loading
millsp Mar 23, 2022
fcc6f06
fix(client): load unidici with require
millsp Mar 23, 2022
6b860b3
chore(client): cleanup
millsp Mar 23, 2022
6c82dc0
docs(client): add comment
millsp Mar 23, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 3 additions & 7 deletions helpers/compile/depcheck.ts
@@ -1,4 +1,5 @@
import * as esbuild from 'esbuild'
import { builtinModules } from 'module'
import glob from 'globby'
import path from 'path'

Expand Down Expand Up @@ -37,11 +38,6 @@ const unusedIgnore = [
// packages that aren't missing but are detected
const missingIgnore = ['.prisma', '@prisma/client']

// native nodejs imports so that we can filter out
const nativeDependencies = new Set(
Object.keys((process as any).binding('natives')),
)

/**
* Checks for unused and missing dependencies
*/
Expand Down Expand Up @@ -79,12 +75,12 @@ const unusedPlugin: esbuild.Plugin = {
build.onEnd(() => {
// we take all the dependencies that aren't collected and are native
const unusedDependencies = [...dependencies].filter((dep) => {
return !collectedDependencies.has(dep) || nativeDependencies.has(dep)
return !collectedDependencies.has(dep) || builtinModules.includes(dep)
})

// we take all the collected deps that aren't deps and aren't native
const missingDependencies = [...collectedDependencies].filter((dep) => {
return !dependencies.has(dep) && !nativeDependencies.has(dep)
return !dependencies.has(dep) && !builtinModules.includes(dep)
})

// we exclude the deps that match our unusedIgnore patterns
Expand Down
19 changes: 15 additions & 4 deletions helpers/compile/plugins/replaceWithPlugin.ts
@@ -1,9 +1,16 @@
import { builtinModules } from 'module'
import type * as esbuild from 'esbuild'
import fs from 'fs'

function applyReplacements(contents: string, replacements: [RegExp, string][]) {
type Replacement = [RegExp, string | ((regex: RegExp, contents: string) => string | Promise<string>)]

async function applyReplacements(contents: string, replacements: Replacement[]) {
for (const [regex, replacement] of replacements) {
contents = contents.replace(regex, replacement)
if (typeof replacement === 'string') {
contents = contents.replace(regex, replacement)
} else {
contents = await replacement(regex, contents)
}
}

return contents
Expand All @@ -14,14 +21,18 @@ function applyReplacements(contents: string, replacements: [RegExp, string][]) {
* @param replacements
* @returns
*/
export const replaceWithPlugin = (replacements: [RegExp, string][]): esbuild.Plugin => {
export const replaceWithPlugin = (replacements: Replacement[]): esbuild.Plugin => {
return {
name: 'replaceWithPlugin',
setup(build) {
build.onLoad({ filter: /.*/ }, async (args) => {
// we skip, don't attempt to edit files that aren't js
if (builtinModules.includes(args.path)) return {}
if (!/.*?(.js|.mjs)$/.exec(args.path)) return {}

const contents = await fs.promises.readFile(args.path, 'utf8')

return { contents: applyReplacements(contents, replacements) }
return { contents: await applyReplacements(contents, replacements) }
})
},
}
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/helpers/build.ts
@@ -1,7 +1,7 @@
import type { BuildOptions } from '../../../helpers/compile/build'
import { run } from '../../../helpers/compile/build'
import { build } from '../../../helpers/compile/build'
import { copySync } from 'fs-extra'
import { copy } from 'fs-extra'
millsp marked this conversation as resolved.
Show resolved Hide resolved
import path from 'path'
import type * as esbuild from 'esbuild'
import fs from 'fs'
Expand Down Expand Up @@ -37,7 +37,7 @@ const resolveHelperPlugin: esbuild.Plugin = {
const cliLifecyclePlugin: esbuild.Plugin = {
name: 'cliLifecyclePlugin',
setup(build) {
// we only do this for the first oen of the builds
// we only do this for the first one of the builds
if (build.initialOptions?.format === 'esm') return

build.onStart(async () => {
Expand All @@ -47,7 +47,7 @@ const cliLifecyclePlugin: esbuild.Plugin = {

build.onEnd(async () => {
// we copy the contents from @prisma/studio to build
copySync(path.join(require.resolve('@prisma/studio/package.json'), '../dist'), './build/public', {
await copy(path.join(require.resolve('@prisma/studio/package.json'), '../dist'), './build/public', {
recursive: true,
overwrite: true,
})
Expand All @@ -72,7 +72,7 @@ const cliLifecyclePlugin: esbuild.Plugin = {
const cliBuildConfig: BuildOptions = {
entryPoints: ['src/bin.ts'],
outfile: 'build/index',
external: ['@prisma/engines', '_http_common'],
external: ['@prisma/engines'],
millsp marked this conversation as resolved.
Show resolved Hide resolved
plugins: [resolveHelperPlugin, cliLifecyclePlugin],
bundle: true,
}
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/package.json
Expand Up @@ -46,6 +46,7 @@
"runtime/*.d.ts",
"runtime/utils",
"runtime/dist",
"runtime/llhttp",
"prisma-client",
"preinstall",
"scripts/preinstall-entry.js",
Expand Down Expand Up @@ -124,7 +125,7 @@
"format": "prettier --write .",
"lint": "eslint --cache --fix --ext .ts .",
"lint-ci": "eslint --ext .ts .",
"tsc": "tsc -d -p tsconfig.build.json && bash scripts/copy-runtime-dist.sh",
"tsc": "tsc -d -p tsconfig.build.json",
"prepublishOnly": "pnpm run build",
"preinstall": "node scripts/preinstall-entry.js",
"precommit": "lint-staged"
Expand Down
22 changes: 4 additions & 18 deletions packages/cli/src/__tests__/__helpers__/snapshotSerializer.ts
Expand Up @@ -18,16 +18,11 @@ function removePlatforms(str) {
}
millsp marked this conversation as resolved.
Show resolved Hide resolved

function normalizeGithubLinks(str) {
return str.replace(
/https:\/\/github.com\/prisma\/prisma-client-js\/issues\/\S+/,
'TEST_GITHUB_LINK',
)
return str.replace(/https:\/\/github.com\/prisma\/prisma-client-js\/issues\/\S+/, 'TEST_GITHUB_LINK')
}

function normalizeRustError(str) {
return str
.replace(/\/rustc\/(.+)\//g, '/rustc/hash/')
.replace(/(\[.*)(:\d*:\d*)(\])/g, '[/some/rust/path:0:0$3')
return str.replace(/\/rustc\/(.+)\//g, '/rustc/hash/').replace(/(\[.*)(:\d*:\d*)(\])/g, '[/some/rust/path:0:0$3')
}

function normalizeTmpDir(str) {
Expand Down Expand Up @@ -65,22 +60,13 @@ export function test(value) {
}

export function serialize(value) {
const message =
typeof value === 'string'
? value
: value instanceof Error
? value.message
: ''
const message = typeof value === 'string' ? value : value instanceof Error ? value.message : ''
return prepareSchemaForSnapshot(
normalizeGithubLinks(
normalizeRustError(
normalizeMs(
normalizeTmpDir(
normalizeGithubLinks(
normalizeToUnixPaths(
removePlatforms(trimErrorPaths(stripAnsi(message))),
),
),
normalizeGithubLinks(normalizeToUnixPaths(removePlatforms(trimErrorPaths(stripAnsi(message))))),
),
),
),
Expand Down
50 changes: 10 additions & 40 deletions packages/cli/src/__tests__/commands/Init.test.ts
Expand Up @@ -10,10 +10,7 @@ test('is schema and env written on disk replace', async () => {
const result = await ctx.cli('init')
millsp marked this conversation as resolved.
Show resolved Hide resolved
expect(stripAnsi(result.stdout)).toMatchSnapshot()

const schema = fs.readFileSync(
join(ctx.tmpDir, 'prisma', 'schema.prisma'),
'utf-8',
)
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema())

const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
Expand All @@ -25,10 +22,7 @@ test('works with url param', async () => {
const result = await ctx.cli('init', '--url', 'file:dev.db')
expect(stripAnsi(result.stdout)).toMatchSnapshot()

const schema = fs.readFileSync(
join(ctx.tmpDir, 'prisma', 'schema.prisma'),
'utf-8',
)
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema('sqlite'))

const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
Expand All @@ -48,10 +42,7 @@ test('works with provider param - postgresql', async () => {
const result = await ctx.cli('init', '--datasource-provider', 'postgresql')
expect(stripAnsi(result.stdout)).toMatchSnapshot()

const schema = fs.readFileSync(
join(ctx.tmpDir, 'prisma', 'schema.prisma'),
'utf-8',
)
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema('postgresql'))

const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
Expand All @@ -71,10 +62,7 @@ test('works with provider param - mysql', async () => {
const result = await ctx.cli('init', '--datasource-provider', 'mysql')
expect(stripAnsi(result.stdout)).toMatchSnapshot()

const schema = fs.readFileSync(
join(ctx.tmpDir, 'prisma', 'schema.prisma'),
'utf-8',
)
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema('mysql'))

const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
Expand All @@ -94,10 +82,7 @@ test('works with provider param - SQLITE', async () => {
const result = await ctx.cli('init', '--datasource-provider', 'SQLITE')
expect(stripAnsi(result.stdout)).toMatchSnapshot()

const schema = fs.readFileSync(
join(ctx.tmpDir, 'prisma', 'schema.prisma'),
'utf-8',
)
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema('sqlite'))

const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
Expand All @@ -117,10 +102,7 @@ test('works with provider param - SqlServer', async () => {
const result = await ctx.cli('init', '--datasource-provider', 'SqlServer')
expect(stripAnsi(result.stdout)).toMatchSnapshot()

const schema = fs.readFileSync(
join(ctx.tmpDir, 'prisma', 'schema.prisma'),
'utf-8',
)
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema('sqlserver'))

const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
Expand All @@ -140,10 +122,7 @@ test('works with provider param - MongoDB', async () => {
const result = await ctx.cli('init', '--datasource-provider', 'MongoDB')
expect(stripAnsi(result.stdout)).toMatchSnapshot()

const schema = fs.readFileSync(
join(ctx.tmpDir, 'prisma', 'schema.prisma'),
'utf-8',
)
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema('mongodb'))

const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
Expand All @@ -165,17 +144,11 @@ test('errors with invalid provider param', async () => {
})

test('warns when DATABASE_URL present in .env ', async () => {
fs.writeFileSync(
join(ctx.tmpDir, '.env'),
`DATABASE_URL="postgres://dont:overwrite@me:5432/tests"`,
)
fs.writeFileSync(join(ctx.tmpDir, '.env'), `DATABASE_URL="postgres://dont:overwrite@me:5432/tests"`)
const result = await ctx.cli('init')
expect(stripAnsi(result.all!)).toMatchSnapshot()

const schema = fs.readFileSync(
join(ctx.tmpDir, 'prisma', 'schema.prisma'),
'utf-8',
)
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema())

const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
Expand All @@ -187,10 +160,7 @@ test('appends when .env present', async () => {
const result = await ctx.cli('init')
expect(stripAnsi(result.stdout)).toMatchSnapshot()

const schema = fs.readFileSync(
join(ctx.tmpDir, 'prisma', 'schema.prisma'),
'utf-8',
)
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema())

const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
Expand Down
42 changes: 31 additions & 11 deletions packages/client/helpers/build.ts
@@ -1,30 +1,45 @@
import type { BuildOptions } from '../../../helpers/compile/build'
import { build } from '../../../helpers/compile/build'
import { fillPlugin } from '../../../helpers/compile/plugins/fill-plugin/fillPlugin'
import { replaceWithPlugin } from '../../../helpers/compile/plugins/replaceWithPlugin'
import { Extractor, ExtractorConfig } from '@microsoft/api-extractor'
import resolve from 'resolve'
import path from 'path'
import fs from 'fs'

const external = ['_http_common']
const inlineUndiciWasm = replaceWithPlugin([
[
/(await WebAssembly\.compile\().*?'(.*?)'\)\)\)/g,
async (regex, contents) => {
for (const match of contents.matchAll(regex)) {
if (match[2].includes('simd') === false) {
// we only bundle lhttp wasm files that are not simd compiled
millsp marked this conversation as resolved.
Show resolved Hide resolved
const engineCoreDir = resolve.sync('@prisma/engine-core')
const undiciPackage = resolve.sync('undici/package.json', { basedir: engineCoreDir })
const lhttpWasmPath = path.join(path.dirname(undiciPackage), 'lib', match[2])
millsp marked this conversation as resolved.
Show resolved Hide resolved
millsp marked this conversation as resolved.
Show resolved Hide resolved
const wasmContents = (await fs.promises.readFile(lhttpWasmPath)).toString('base64')
millsp marked this conversation as resolved.
Show resolved Hide resolved
const inlineWasm = `${match[1]}(Buffer.from("${wasmContents}", "base64")))`

// we define the config for generator
const generatorBuildConfig: BuildOptions = {
entryPoints: ['src/generation/generator.ts'],
outfile: 'generator-build/index',
bundle: true,
external: external,
}
contents = contents.replace(match[0], inlineWasm)
}
}

return contents
},
],
])

// we define the config for runtime
const runtimeBuildConfig: BuildOptions = {
entryPoints: ['src/runtime/index.ts'],
outfile: 'runtime/index',
bundle: true,
external: external,
define: {
'globalThis.NOT_PRISMA_DATA_PROXY': 'true',
// that fixes an issue with lz-string umd builds
'define.amd': 'false',
},
plugins: [inlineUndiciWasm],
}

// we define the config for browser
Expand All @@ -33,7 +48,6 @@ const browserBuildConfig: BuildOptions = {
outfile: 'runtime/index-browser',
target: ['chrome58', 'firefox57', 'safari11', 'edge16'],
bundle: true,
external: external,
}

// we define the config for proxy
Expand All @@ -43,7 +57,6 @@ const proxyBuildConfig: BuildOptions = {
bundle: true,
minify: true,
legalComments: 'none',
external: external,
define: {
// that helps us to tree-shake unused things out
'globalThis.NOT_PRISMA_DATA_PROXY': 'false',
Expand All @@ -69,6 +82,13 @@ const proxyBuildConfig: BuildOptions = {
logLevel: 'error',
}

// we define the config for generator
const generatorBuildConfig: BuildOptions = {
entryPoints: ['src/generation/generator.ts'],
outfile: 'generator-build/index',
bundle: true,
}

/**
* Bundle all type definitions by using the API Extractor from RushStack
* @param filename the source d.ts to bundle
Expand Down