sproutboat 0.50.0 → 0.51.0
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/CHANGELOG.md +9 -0
- package/SURFACE.md +1 -1
- package/package.json +8 -8
- package/src/bundle.ts +94 -4
- package/src/main.ts +3 -1
- package/src/node-shims.ts +82 -0
package/CHANGELOG.md
CHANGED
|
@@ -10,6 +10,15 @@ maintained going forward by the `release` skill.
|
|
|
10
10
|
|
|
11
11
|
## Unreleased
|
|
12
12
|
|
|
13
|
+
## [0.51.0] - 2026-11-03
|
|
14
|
+
|
|
15
|
+
Built on `@sproutboat/runtime` 0.42.0, `@sproutboat/toolchain` 0.23.0 and `@sproutboat/wire` 0.22.0. The change below starts at a new compatibility date; earlier dates answer as before.
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
|
|
19
|
+
- **`connect()` from `cloudflare:sockets` from `2026-11-17`**: TCP sockets with `readable`, `writable`, `opened`, `closed`, `close()` and `startTls()`, on a standalone binary, under `sproutboat dev` and on a platform deploy. The same unmodified Postgres (`pg`, `postgres`) and Redis driver code that runs on workerd runs here, with no differences from workerd in the driver suites. Outbound policy is the one `fetch()` uses. Before the date, importing `cloudflare:sockets` fails the build with `cloudflare:sockets needs compatibility_date 2026-11-17 or later`.
|
|
20
|
+
- **From the same date a driver bundles as it does for workerd**: the `workerd` export condition (`pg-cloudflare`, `postgres/cf`), a bundled `Buffer`, `process` and `global`, and shims for `net`, `tls`, `dns`, `fs` and `crypto` so the Node branch of a driver names `cloudflare:sockets` instead of failing the build. A named function expression that shadows a global builtin function is renamed, which sproutd admission needs.
|
|
21
|
+
|
|
13
22
|
## [0.50.0] - 2026-11-02
|
|
14
23
|
|
|
15
24
|
Built on `@sproutboat/runtime` 0.41.0, `@sproutboat/toolchain` 0.22.0 and `@sproutboat/wire` 0.21.4. Each change below that an existing app could observe starts at a new compatibility date; earlier dates answer as before.
|
package/SURFACE.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sproutboat",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.51.0",
|
|
4
4
|
"description": "Wrangler-shaped CLI for Sproutboat. Deploys workers to any control plane via --api-url / SPROUTBOAT_API_URL.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -64,9 +64,9 @@
|
|
|
64
64
|
"@sproutboat/artifact": "^0.3.0",
|
|
65
65
|
"@sproutboat/assets": "^0.3.0",
|
|
66
66
|
"@sproutboat/config": "^0.6.0",
|
|
67
|
-
"@sproutboat/runtime": "^0.
|
|
68
|
-
"@sproutboat/toolchain": "^0.
|
|
69
|
-
"@sproutboat/wire": "^0.
|
|
67
|
+
"@sproutboat/runtime": "^0.42.0",
|
|
68
|
+
"@sproutboat/toolchain": "^0.23.0",
|
|
69
|
+
"@sproutboat/wire": "^0.22.0"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@oxlint/plugins": "1.85.0",
|
|
@@ -78,10 +78,10 @@
|
|
|
78
78
|
"typescript": "7.0.2"
|
|
79
79
|
},
|
|
80
80
|
"optionalDependencies": {
|
|
81
|
-
"@sproutboat/cli-darwin-arm64": "0.
|
|
82
|
-
"@sproutboat/cli-darwin-x64": "0.
|
|
83
|
-
"@sproutboat/cli-linux-arm64": "0.
|
|
84
|
-
"@sproutboat/cli-linux-x64": "0.
|
|
81
|
+
"@sproutboat/cli-darwin-arm64": "0.51.0",
|
|
82
|
+
"@sproutboat/cli-darwin-x64": "0.51.0",
|
|
83
|
+
"@sproutboat/cli-linux-arm64": "0.51.0",
|
|
84
|
+
"@sproutboat/cli-linux-x64": "0.51.0"
|
|
85
85
|
},
|
|
86
86
|
"engines": {
|
|
87
87
|
"node": ">=18"
|
package/src/bundle.ts
CHANGED
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
* reaching for a Node API fails exactly as user code would.
|
|
13
13
|
*/
|
|
14
14
|
import { relative } from "node:path";
|
|
15
|
-
import {
|
|
15
|
+
import { SOCKETS_DATE } from "@sproutboat/runtime";
|
|
16
|
+
import { NODE_SHIMS, SOCKET_NODE_SHIMS } from "./node-shims";
|
|
16
17
|
|
|
17
18
|
export type BundleResult = {
|
|
18
19
|
/** One self-contained ESM module: what gets validated, hashed, and compiled. */
|
|
@@ -54,6 +55,86 @@ const nodeShimsPlugin: import("bun").BunPlugin = {
|
|
|
54
55
|
},
|
|
55
56
|
};
|
|
56
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Porffor lists the global builtin functions on the global object of every unit, and a function
|
|
60
|
+
* named like one of them (postgres: `export const escapeIdentifier = function escape(str) {}`)
|
|
61
|
+
* drops the entry from that unit alone, so a sproutd app that does it no longer matches the
|
|
62
|
+
* runtime's common code and is rejected at admission. A named function expression's name is only
|
|
63
|
+
* visible inside itself, so renaming it is safe unless the body calls itself by that name.
|
|
64
|
+
*/
|
|
65
|
+
const SHADOWED_BUILTIN_FUNCTIONS =
|
|
66
|
+
/([=(:,?&|]\s*|\breturn\s+)function\s+(escape|unescape|eval|isNaN|isFinite|parseInt|parseFloat|encodeURI|encodeURIComponent|decodeURI|decodeURIComponent)\s*\(/g;
|
|
67
|
+
|
|
68
|
+
/** What a driver reads off `process`: an empty environment and a microtask for nextTick. */
|
|
69
|
+
const PROCESS_STUB = `
|
|
70
|
+
const env = {};
|
|
71
|
+
function nextTick(fn, ...args) { Promise.resolve().then(() => fn(...args)); }
|
|
72
|
+
const process = { env, platform: "linux", arch: "wasm32", version: "v20.0.0", versions: {}, argv: [], browser: true, nextTick, emitWarning() {}, cwd() { return "/"; }, stderr: { write() {} }, stdout: { write() {} }, on() {}, off() {}, once() {}, emit() {} };
|
|
73
|
+
export default process;
|
|
74
|
+
`;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Node libraries use `Buffer` and `process` as free globals. Bun's browser target does not
|
|
78
|
+
* inject them for a CommonJS file, so a dependency that never imports Buffer would throw a
|
|
79
|
+
* ReferenceError at runtime. This gives every dependency file that uses one a local binding to the
|
|
80
|
+
* bundled implementation (the `buffer` package, which Porffor compiles; see the toolchain notes on
|
|
81
|
+
* typed array method dispatch). Applied only to node_modules, never to the handler's own code.
|
|
82
|
+
*/
|
|
83
|
+
const nodeGlobalsPlugin: import("bun").BunPlugin = {
|
|
84
|
+
name: "sproutboat-node-globals",
|
|
85
|
+
setup(build) {
|
|
86
|
+
build.onResolve({ filter: /^sb-node-process$/ }, () => ({ path: "sb-node-process", namespace: "sb-node-global" }));
|
|
87
|
+
build.onLoad({ filter: /.*/, namespace: "sb-node-global" }, () => ({ contents: PROCESS_STUB, loader: "js" }));
|
|
88
|
+
build.onLoad({ filter: /node_modules\/.*\.(?:c?js|mjs)$/ }, async (args) => {
|
|
89
|
+
const original = await Bun.file(args.path).text();
|
|
90
|
+
const text = original.replace(
|
|
91
|
+
SHADOWED_BUILTIN_FUNCTIONS,
|
|
92
|
+
(_m, lead: string, name: string) => `${lead}function ${name}_sb(`,
|
|
93
|
+
);
|
|
94
|
+
const usesBuffer =
|
|
95
|
+
/(?:^|[^.\w$])Buffer\s*[.(]/m.test(text) &&
|
|
96
|
+
!/\b(?:import|require)\b[^;\n]*\bBuffer\b/.test(text) &&
|
|
97
|
+
!/(?:const|let|var)\s+\{?\s*Buffer\b/.test(text);
|
|
98
|
+
const usesProcess =
|
|
99
|
+
/(?:^|[^.\w$])process\s*\./m.test(text) &&
|
|
100
|
+
!/\b(?:import|require)\b[^;\n]*\bprocess\b/.test(text) &&
|
|
101
|
+
!/(?:const|let|var|function)\s+\{?\s*process\b/.test(text);
|
|
102
|
+
const usesGlobal =
|
|
103
|
+
/(?:^|[^.\w$])global\s*\./m.test(text) && !/(?:const|let|var|function)\s+\{?\s*global\b/.test(text);
|
|
104
|
+
if (!usesBuffer && !usesProcess && !usesGlobal)
|
|
105
|
+
return text === original ? undefined : { contents: text, loader: "js" };
|
|
106
|
+
const esm = /^\s*(?:import|export)\s/m.test(text);
|
|
107
|
+
let prefix = "";
|
|
108
|
+
if (usesBuffer) prefix += esm ? `import { Buffer } from "buffer";\n` : `var { Buffer } = require("buffer");\n`;
|
|
109
|
+
if (usesProcess)
|
|
110
|
+
prefix += esm
|
|
111
|
+
? `import process from "sb-node-process";\n`
|
|
112
|
+
: `var process = require("sb-node-process").default;\n`;
|
|
113
|
+
if (usesGlobal) prefix += "var global = globalThis;\n";
|
|
114
|
+
return { contents: prefix + text, loader: "js" };
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const socketShimSpecifiers = new RegExp(
|
|
120
|
+
`^(${Object.keys(SOCKET_NODE_SHIMS)
|
|
121
|
+
.map((s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
|
|
122
|
+
.join("|")})$`,
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
/** The Node built-ins a driver names, replaced from the sockets date: see SOCKET_NODE_SHIMS. */
|
|
126
|
+
const socketShimsPlugin: import("bun").BunPlugin = {
|
|
127
|
+
name: "sproutboat-socket-shims",
|
|
128
|
+
setup(build) {
|
|
129
|
+
build.onResolve({ filter: socketShimSpecifiers }, (args) => ({ path: args.path, namespace: "sb-socket-shim" }));
|
|
130
|
+
build.onLoad({ filter: /.*/, namespace: "sb-socket-shim" }, (args) => ({
|
|
131
|
+
// SAFETY: onResolve above only routes specifiers built from SOCKET_NODE_SHIMS's own keys here.
|
|
132
|
+
contents: (SOCKET_NODE_SHIMS as Record<string, string>)[args.path],
|
|
133
|
+
loader: "js",
|
|
134
|
+
}));
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
|
|
57
138
|
/** Bun reports resolution failures on `AggregateError.errors`; its own message is just "Bundle failed". */
|
|
58
139
|
function bundleDetail(cause: unknown): string {
|
|
59
140
|
const errors = cause instanceof AggregateError ? cause.errors : [];
|
|
@@ -63,7 +144,15 @@ function bundleDetail(cause: unknown): string {
|
|
|
63
144
|
}
|
|
64
145
|
|
|
65
146
|
/** Bundle `entryPath` into a single module, resolving imports from `projectDir`. */
|
|
66
|
-
export async function bundleHandler(
|
|
147
|
+
export async function bundleHandler(
|
|
148
|
+
entryPath: string,
|
|
149
|
+
projectDir: string,
|
|
150
|
+
options: { compatibilityDate?: string } = {},
|
|
151
|
+
): Promise<BundleResult> {
|
|
152
|
+
// From the sockets date a driver bundles as it does for workerd: its "workerd" export condition,
|
|
153
|
+
// Node's free `Buffer`/`process`/`global` bound to bundled code, and net/tls/dns/fs/crypto shims
|
|
154
|
+
// that name cloudflare:sockets when the driver takes its Node path.
|
|
155
|
+
const sockets = options.compatibilityDate !== undefined && options.compatibilityDate >= SOCKETS_DATE;
|
|
67
156
|
let built: Awaited<ReturnType<typeof Bun.build>>;
|
|
68
157
|
try {
|
|
69
158
|
built = await Bun.build({
|
|
@@ -78,10 +167,11 @@ export async function bundleHandler(entryPath: string, projectDir: string): Prom
|
|
|
78
167
|
minify: false,
|
|
79
168
|
splitting: false,
|
|
80
169
|
sourcemap: "none",
|
|
81
|
-
|
|
170
|
+
conditions: sockets ? ["workerd", "worker"] : undefined,
|
|
171
|
+
plugins: sockets ? [nodeShimsPlugin, socketShimsPlugin, nodeGlobalsPlugin] : [nodeShimsPlugin],
|
|
82
172
|
// WorkerEntrypoint, DurableObject, RpcTarget and env are runtime globals; the
|
|
83
173
|
// compiler turns this import into them, so it stays in the bundle as written.
|
|
84
|
-
external: ["cloudflare:workers"],
|
|
174
|
+
external: ["cloudflare:workers", "cloudflare:sockets"],
|
|
85
175
|
});
|
|
86
176
|
} catch (cause) {
|
|
87
177
|
// An unresolvable specifier arrives as an AggregateError whose `errors`
|
package/src/main.ts
CHANGED
|
@@ -234,7 +234,9 @@ async function readProject(directory = process.cwd()) {
|
|
|
234
234
|
// smuggle in a Node API the handler is not allowed to touch.
|
|
235
235
|
let bundle: BundleResult;
|
|
236
236
|
try {
|
|
237
|
-
bundle = await bundleHandler(sourcePath, project.directory
|
|
237
|
+
bundle = await bundleHandler(sourcePath, project.directory, {
|
|
238
|
+
compatibilityDate: project.config.compatibility_date,
|
|
239
|
+
});
|
|
238
240
|
} catch (cause) {
|
|
239
241
|
fail(cause instanceof BundleError ? cause.message : String(cause));
|
|
240
242
|
}
|
package/src/node-shims.ts
CHANGED
|
@@ -386,3 +386,85 @@ export const NODE_SHIMS = {
|
|
|
386
386
|
"node:querystring": NODE_QUERYSTRING_SHIM,
|
|
387
387
|
"node:url": NODE_URL_SHIM,
|
|
388
388
|
} satisfies Record<string, string>;
|
|
389
|
+
|
|
390
|
+
// From the sockets date (2026-11-17), for drivers that carry a Node code path next to their Workers one.
|
|
391
|
+
/**
|
|
392
|
+
* node:net, node:tls, node:dns, node:fs and node:util/types for drivers that carry a Node code path
|
|
393
|
+
* next to their Workers one (pg, mysql2). The Workers path never calls them: every function throws
|
|
394
|
+
* a message that names cloudflare:sockets, so a driver that does take the Node path says why.
|
|
395
|
+
*/
|
|
396
|
+
const unavailable = (what: string) =>
|
|
397
|
+
`function unavailable() { throw new Error("${what} is not available in a compiled handler; use cloudflare:sockets (connect())"); }\n`;
|
|
398
|
+
const NODE_NET_SHIM =
|
|
399
|
+
unavailable("the net module") +
|
|
400
|
+
`class Socket { constructor() { unavailable(); } }
|
|
401
|
+
const ipv4 = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$/;
|
|
402
|
+
function isIP(x) { return ipv4.test(String(x)) ? 4 : String(x).indexOf(":") >= 0 && /^[0-9a-fA-F:.]+$/.test(String(x)) ? 6 : 0; }
|
|
403
|
+
function connect() { unavailable(); }
|
|
404
|
+
export { Socket, isIP, connect };
|
|
405
|
+
export default { Socket, isIP, connect, createConnection: connect };
|
|
406
|
+
`;
|
|
407
|
+
const NODE_TLS_SHIM =
|
|
408
|
+
unavailable("the tls module") +
|
|
409
|
+
`function connect() { unavailable(); }
|
|
410
|
+
export { connect };
|
|
411
|
+
export default { connect };
|
|
412
|
+
`;
|
|
413
|
+
const NODE_DNS_SHIM =
|
|
414
|
+
unavailable("the dns module") +
|
|
415
|
+
`function lookup() { unavailable(); }
|
|
416
|
+
export { lookup };
|
|
417
|
+
export default { lookup, promises: { lookup } };
|
|
418
|
+
`;
|
|
419
|
+
const NODE_FS_SHIM =
|
|
420
|
+
unavailable("the fs module") +
|
|
421
|
+
`function readFileSync() { unavailable(); }
|
|
422
|
+
function existsSync() { return false; }
|
|
423
|
+
function statSync() { unavailable(); }
|
|
424
|
+
function createReadStream() { unavailable(); }
|
|
425
|
+
export { readFileSync, existsSync, statSync, createReadStream };
|
|
426
|
+
export default { readFileSync, existsSync, statSync, createReadStream };
|
|
427
|
+
`;
|
|
428
|
+
const NODE_UTIL_TYPES_SHIM = `
|
|
429
|
+
const isDate = (v) => v instanceof Date;
|
|
430
|
+
const isTypedArray = (v) => ArrayBuffer.isView(v) && !(v instanceof DataView);
|
|
431
|
+
const isUint8Array = (v) => v instanceof Uint8Array;
|
|
432
|
+
const isArrayBuffer = (v) => v instanceof ArrayBuffer;
|
|
433
|
+
const isPromise = (v) => v instanceof Promise;
|
|
434
|
+
const isRegExp = (v) => v instanceof RegExp;
|
|
435
|
+
export { isDate, isTypedArray, isUint8Array, isArrayBuffer, isPromise, isRegExp };
|
|
436
|
+
export default { isDate, isTypedArray, isUint8Array, isArrayBuffer, isPromise, isRegExp };
|
|
437
|
+
`;
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* node:crypto as a Workers handler has it: the Web Crypto global, not the Node API. pg asks for
|
|
441
|
+
* `crypto.webcrypto || globalThis.crypto` and for createHash("md5") inside a try, so a createHash
|
|
442
|
+
* that throws sends it to crypto.subtle. Bun's default polyfill (crypto-browserify) is 20k lines,
|
|
443
|
+
* and its elliptic-curve code does not compile under Porffor.
|
|
444
|
+
*/
|
|
445
|
+
const NODE_CRYPTO_SHIM = `
|
|
446
|
+
function unavailable() { throw new Error("this crypto function is not available; use crypto.subtle"); }
|
|
447
|
+
const webcrypto = globalThis.crypto;
|
|
448
|
+
function randomBytes(n) { return webcrypto.getRandomValues(new Uint8Array(n)); }
|
|
449
|
+
function randomUUID() { return webcrypto.randomUUID(); }
|
|
450
|
+
function createHash() { unavailable(); }
|
|
451
|
+
function createHmac() { unavailable(); }
|
|
452
|
+
export { webcrypto, randomBytes, randomUUID, createHash, createHmac };
|
|
453
|
+
export default { webcrypto, randomBytes, randomUUID, createHash, createHmac, getRandomValues: (a) => webcrypto.getRandomValues(a), subtle: webcrypto.subtle };
|
|
454
|
+
`;
|
|
455
|
+
|
|
456
|
+
/** Replaces Bun's browser polyfills, in a bundle at the sockets date or later (see bundle.ts). */
|
|
457
|
+
export const SOCKET_NODE_SHIMS = {
|
|
458
|
+
"node:crypto": NODE_CRYPTO_SHIM,
|
|
459
|
+
crypto: NODE_CRYPTO_SHIM,
|
|
460
|
+
"node:net": NODE_NET_SHIM,
|
|
461
|
+
net: NODE_NET_SHIM,
|
|
462
|
+
"node:tls": NODE_TLS_SHIM,
|
|
463
|
+
tls: NODE_TLS_SHIM,
|
|
464
|
+
"node:dns": NODE_DNS_SHIM,
|
|
465
|
+
dns: NODE_DNS_SHIM,
|
|
466
|
+
"node:fs": NODE_FS_SHIM,
|
|
467
|
+
fs: NODE_FS_SHIM,
|
|
468
|
+
"node:util/types": NODE_UTIL_TYPES_SHIM,
|
|
469
|
+
"util/types": NODE_UTIL_TYPES_SHIM,
|
|
470
|
+
} satisfies Record<string, string>;
|