Skip to content

Commit

Permalink
fix(deps): update undici to 4.x (#8842)
Browse files Browse the repository at this point in the history
Co-authored-by: Joël Galeran <Jolg42@users.noreply.github.com>
Co-authored-by: Pierre-Antoine Mills <pierreantoine.urvoy@gmail.com>
Co-authored-by: Michal Kvasničák <michal.kvasnicak@gmail.com>
  • Loading branch information
4 people committed Mar 23, 2022
1 parent 9c71dde commit 6fc821f
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 60 deletions.
44 changes: 40 additions & 4 deletions helpers/compile/plugins/replaceWithPlugin.ts
@@ -1,9 +1,16 @@
import type * as esbuild from 'esbuild'
import fs from 'fs'
import { builtinModules } from 'module'

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,15 +21,44 @@ 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) }
})
},
}
}

/**
* Example
*/
// 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
// 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])
// const wasmContents = (await fs.promises.readFile(lhttpWasmPath)).toString('base64')
// const inlineWasm = `${match[1]}(Buffer.from("${wasmContents}", "base64")))`

// contents = contents.replace(match[0], inlineWasm)
// }
// }

// return contents
// },
// ],
// ])
8 changes: 4 additions & 4 deletions packages/cli/helpers/build.ts
@@ -1,6 +1,6 @@
import type * as esbuild from 'esbuild'
import fs from 'fs'
import { copySync } from 'fs-extra'
import { copy } from 'fs-extra'
import lineReplace from 'line-replace'
import path from 'path'
import { promisify } from 'util'
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'],
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 @@ -113,7 +114,7 @@
"build": "node -r esbuild-register helpers/build.ts",
"test": "jest --maxConcurrency=1 --verbose",
"install": "node scripts/install-entry.js",
"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"
},
Expand Down
20 changes: 7 additions & 13 deletions packages/client/helpers/build.ts
Expand Up @@ -5,22 +5,11 @@ import type { BuildOptions } from '../../../helpers/compile/build'
import { build } from '../../../helpers/compile/build'
import { fillPlugin } from '../../../helpers/compile/plugins/fill-plugin/fillPlugin'

const external = ['_http_common']

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

// 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
Expand All @@ -34,7 +23,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 @@ -44,7 +32,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 @@ -70,6 +57,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
1 change: 1 addition & 0 deletions packages/client/package.json
Expand Up @@ -100,6 +100,7 @@
"pkg-up": "3.1.0",
"pluralize": "8.0.0",
"replace-string": "3.1.0",
"resolve": "1.22.0",
"rimraf": "3.0.2",
"sort-keys": "4.2.0",
"source-map-support": "0.5.21",
Expand Down
4 changes: 2 additions & 2 deletions packages/engine-core/package.json
Expand Up @@ -22,7 +22,7 @@
"@swc/core": "1.2.141",
"@swc/jest": "0.2.17",
"@types/jest": "27.4.1",
"@types/node": "12.20.47",
"@types/node": "16.6.2",
"esbuild": "0.13.14",
"jest": "27.5.1",
"jest-junit": "13.0.0",
Expand All @@ -47,7 +47,7 @@
"p-retry": "4.6.1",
"strip-ansi": "6.0.1",
"terminal-link": "2.1.1",
"undici": "3.3.6"
"undici": "4.16.0"
},
"files": [
"README.md",
Expand Down
29 changes: 18 additions & 11 deletions packages/engine-core/src/binary/Connection.ts
@@ -1,14 +1,17 @@
import getStream from 'get-stream'
import type { Client } from 'undici'
import { Pool } from 'undici'
import type { Dispatcher, Pool } from 'undici'
import type { URL } from 'url'

export type Result<R> = {
statusCode: Client.ResponseData['statusCode']
headers: Client.ResponseData['headers']
statusCode: Dispatcher.ResponseData['statusCode']
headers: Dispatcher.ResponseData['headers']
data: R
}

// because undici lazily loads llhttp wasm which bloats the memory
// TODO: hopefully replace with `import` but that causes segfaults
const undici = () => require('undici')

/**
* Assertion function to make sure that we have a pool
* @param pool
Expand Down Expand Up @@ -53,10 +56,11 @@ export class Connection {
open(url: string | URL, options?: Pool.Options) {
if (this._pool) return

this._pool = new Pool(url, {
this._pool = new (undici().Pool)(url, {
connections: 1000,
keepAliveMaxTimeout: 600e3,
headersTimeout: 0,
bodyTimeout: 0,
...options,
})
}
Expand All @@ -72,8 +76,8 @@ export class Connection {
async raw<R>(
method: 'POST' | 'GET',
endpoint: string,
headers?: Client.DispatchOptions['headers'],
body?: Client.DispatchOptions['body'],
headers?: Dispatcher.DispatchOptions['headers'],
body?: Dispatcher.DispatchOptions['body'],
) {
assertHasPool(this._pool)

Expand All @@ -84,8 +88,7 @@ export class Connection {
'Content-Type': 'application/json',
...headers,
},
body,
bodyTimeout: 0,
body: body,
})

const result: Result<R> = {
Expand All @@ -104,7 +107,11 @@ export class Connection {
* @param headers
* @returns
*/
post<R>(endpoint: string, body?: Client.DispatchOptions['body'], headers?: Client.DispatchOptions['headers']) {
post<R>(
endpoint: string,
body?: Dispatcher.DispatchOptions['body'],
headers?: Dispatcher.DispatchOptions['headers'],
) {
return this.raw<R>('POST', endpoint, headers, body)
}

Expand All @@ -115,7 +122,7 @@ export class Connection {
* @param headers
* @returns
*/
get<R>(path: string, headers?: Client.DispatchOptions['headers']) {
get<R>(path: string, headers?: Dispatcher.DispatchOptions['headers']) {
return this.raw<R>('GET', path, headers)
}

Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/src/resolveBinary.ts
Expand Up @@ -52,7 +52,7 @@ export async function resolveBinary(name: BinaryType, proposedPath?: string): Pr
if (fs.existsSync(prismaPath)) {
return maybeCopyToTmp(prismaPath)
}

// for pkg (related: https://github.com/vercel/pkg#snapshot-filesystem)
const prismaPath2 = path.join(__dirname, '..', binaryName)
if (fs.existsSync(prismaPath2)) {
Expand Down Expand Up @@ -83,7 +83,7 @@ export async function resolveBinary(name: BinaryType, proposedPath?: string): Pr

export async function maybeCopyToTmp(file: string): Promise<string> {
const dir = eval('__dirname')

if (dir.startsWith('/snapshot/')) {
// in this case, we are in a "pkg" context with a virtual fs
// to make this work, we need to copy the binary to /tmp and execute it from there
Expand All @@ -93,13 +93,13 @@ export async function maybeCopyToTmp(file: string): Promise<string> {
const targetDir = path.join(tempDir, 'prisma-binaries')
await makeDir(targetDir)
const target = path.join(targetDir, path.basename(file))

// We have to read and write until https://github.com/zeit/pkg/issues/639 is resolved
const data = await readFile(file)
await writeFile(target, data)
// TODO Undo when https://github.com/vercel/pkg/pull/1484 is released
// await copyFile(file, target)

plusX(target)
return target
}
Expand Down

0 comments on commit 6fc821f

Please sign in to comment.