@allbluecn/database 0.14.16 → 0.14.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@allbluecn/database",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.18",
|
|
4
4
|
"license": "AGPL-3.0-or-later",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.ts",
|
|
@@ -51,7 +51,8 @@
|
|
|
51
51
|
"pglite-seed.ts",
|
|
52
52
|
"index.ts",
|
|
53
53
|
"get-prisma.ts",
|
|
54
|
-
"pg-client.ts"
|
|
54
|
+
"pg-client.ts",
|
|
55
|
+
"scripts"
|
|
55
56
|
],
|
|
56
57
|
"publishConfig": {
|
|
57
58
|
"access": "public"
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Prisma 7 在 Cloudflare Workers 上的 WASM 变通(prisma/orm#28657 未修):
|
|
2
|
+
// workerd 禁止动态 WASM 编译(new WebAssembly.Module(bytes) 抛
|
|
3
|
+
// "Wasm code generation disallowed by embedder"),而生成的
|
|
4
|
+
// internal/class.ts 默认对 base64 内联 wasm 运行时解码。
|
|
5
|
+
//
|
|
6
|
+
// 本脚本在 `prisma generate` 之后运行(package.json generate 链):
|
|
7
|
+
// 1. 把 @prisma/client/runtime/*.wasm-base64.mjs 的内联 wasm 解码为
|
|
8
|
+
// generated/prisma-query-compiler.postgresql.wasm 真实文件;
|
|
9
|
+
// 2. 给 getQueryCompilerWasmModule 加双路径:workerd(navigator.userAgent
|
|
10
|
+
// === "Cloudflare-Workers",官方检测口径)走 wasm 模块导入(@cloudflare/
|
|
11
|
+
// vite-plugin 构建期处理为 WasmModule,无需动态编译);Node/Vercel/desktop
|
|
12
|
+
// 保持 base64 原路,行为不变。
|
|
13
|
+
//
|
|
14
|
+
// 幂等:patch 标记存在即跳过。generated/ 为生成产物,重新 generate 后须重跑
|
|
15
|
+
// (已挂进 generate 脚本链)。
|
|
16
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
17
|
+
import { createRequire } from "node:module";
|
|
18
|
+
import { dirname, join } from "node:path";
|
|
19
|
+
import { fileURLToPath } from "node:url";
|
|
20
|
+
|
|
21
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
22
|
+
const pkgDir = join(here, "..");
|
|
23
|
+
const classFile = join(pkgDir, "generated", "internal", "class.ts");
|
|
24
|
+
const wasmFile = join(pkgDir, "generated", "prisma-query-compiler.postgresql.wasm");
|
|
25
|
+
|
|
26
|
+
const MARKER = "PRISMA_WORKERD_WASM_PATCH_V1";
|
|
27
|
+
let code = readFileSync(classFile, "utf8");
|
|
28
|
+
if (code.includes(MARKER)) {
|
|
29
|
+
console.log("[patch-workerd-wasm] already patched, skip");
|
|
30
|
+
process.exit(0);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const base64Module = "@prisma/client/runtime/query_compiler_fast_bg.postgresql.wasm-base64.mjs";
|
|
34
|
+
const importRe = new RegExp(
|
|
35
|
+
String.raw`const \{ wasm \} = await import\("${base64Module.replace(/[./]/g, "\\$&")}"\)\s*;?\s*return await decodeBase64AsWasm\(wasm\)`,
|
|
36
|
+
);
|
|
37
|
+
if (!importRe.test(code)) {
|
|
38
|
+
console.error("[patch-workerd-wasm] 未找到预期的 base64 加载块——生成器输出结构可能已变更,请人工核对");
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 解码 wasm 文件(从内联模块读取 base64 常量)。@prisma/client 的物理位置随
|
|
43
|
+
// 包管理器布局而异(npm 嵌套在包内 node_modules,pnpm 在虚拟 store 的兄弟
|
|
44
|
+
// node_modules)——createRequire 从本包出发走标准解析,不硬编码层级。
|
|
45
|
+
// 解析不到时静默跳过:本 patch 只服务 Cloudflare Workers 构建(workerd 禁
|
|
46
|
+
// 动态 WASM 编译);Node/Vercel/桌面消费端保持 base64 原路即可,generate
|
|
47
|
+
// 的本职(产出 client)在跑进这里之前已经完成,不能让消费端构建死于可选 patch。
|
|
48
|
+
let base64ModulePath;
|
|
49
|
+
try {
|
|
50
|
+
base64ModulePath = createRequire(join(pkgDir, "index.ts")).resolve(base64Module);
|
|
51
|
+
} catch {
|
|
52
|
+
console.warn(
|
|
53
|
+
"[patch-workerd-wasm] 无法从本包解析 @prisma/client 的 wasm-base64 运行时——跳过 workerd patch(非 Cloudflare 构建无需它)",
|
|
54
|
+
);
|
|
55
|
+
process.exit(0);
|
|
56
|
+
}
|
|
57
|
+
const base64Src = readFileSync(base64ModulePath, "utf8").match(
|
|
58
|
+
/"([A-Za-z0-9+/=]{100,})"/,
|
|
59
|
+
);
|
|
60
|
+
if (!base64Src) {
|
|
61
|
+
throw new Error("未能从 wasm-base64 模块中提取 base64 常量");
|
|
62
|
+
}
|
|
63
|
+
writeFileSync(wasmFile, Buffer.from(base64Src[1], "base64"));
|
|
64
|
+
|
|
65
|
+
const patched = code.replace(
|
|
66
|
+
importRe,
|
|
67
|
+
[
|
|
68
|
+
`// ${MARKER}: workerd 禁动态 WASM 编译(prisma/orm#28657),`,
|
|
69
|
+
`// Cloudflare-Workers 环境走 vite-plugin 处理的 wasm 模块导入;`,
|
|
70
|
+
`// Node/Vercel/desktop 保持 base64 原路(decodeBase64AsWasm)。`,
|
|
71
|
+
`if (typeof navigator !== "undefined" && navigator.userAgent === "Cloudflare-Workers") {`,
|
|
72
|
+
` const wasmNs = (await import("../prisma-query-compiler.postgresql.wasm")) as unknown as { default: WebAssembly.Module };`,
|
|
73
|
+
` return wasmNs.default ?? (wasmNs as unknown as WebAssembly.Module);`,
|
|
74
|
+
`}`,
|
|
75
|
+
`const { wasm } = await import("${base64Module}");`,
|
|
76
|
+
`return await decodeBase64AsWasm(wasm)`,
|
|
77
|
+
].join("\n"),
|
|
78
|
+
);
|
|
79
|
+
writeFileSync(classFile, patched);
|
|
80
|
+
console.log("[patch-workerd-wasm] patched + wasm extracted:", wasmFile);
|