payment-kit 1.29.2 → 1.29.3

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.
Files changed (67) hide show
  1. package/api/src/bootstrap.ts +11 -0
  2. package/api/src/crons/index.ts +14 -13
  3. package/api/src/crons/tenant-fanout.ts +82 -0
  4. package/api/src/host-node/did-connect-runtime-node.ts +33 -0
  5. package/api/src/host-node/serve-static-arc.ts +68 -0
  6. package/api/src/host-node/serve-static.ts +41 -0
  7. package/api/src/libs/auth.ts +166 -27
  8. package/api/src/libs/context.ts +11 -0
  9. package/api/src/libs/did-connect/runtime-did-connect-js.ts +88 -0
  10. package/api/src/libs/did-connect/tenant-identity.ts +221 -0
  11. package/api/src/libs/drivers/identity.ts +61 -0
  12. package/api/src/libs/drivers/index.ts +1 -1
  13. package/api/src/libs/http-fetch-adapter.ts +11 -1
  14. package/api/src/libs/queue/index.ts +14 -2
  15. package/api/src/middlewares/hono/context.ts +7 -0
  16. package/api/src/middlewares/hono/csrf.ts +13 -2
  17. package/api/src/middlewares/hono/security.ts +6 -11
  18. package/api/src/queues/checkout-session.ts +21 -9
  19. package/api/src/queues/event.ts +29 -7
  20. package/api/src/queues/payment.ts +23 -9
  21. package/api/src/queues/payout.ts +28 -16
  22. package/api/src/queues/refund.ts +18 -6
  23. package/api/src/routes/hono/customers.ts +6 -1
  24. package/api/src/routes/hono/refunds.ts +2 -3
  25. package/api/src/service.ts +178 -31
  26. package/api/src/store/sequelize.ts +16 -1
  27. package/api/tests/bootstrap/bootstrap.spec.ts +162 -0
  28. package/api/tests/crons/tenant-fanout.spec.ts +158 -0
  29. package/api/tests/libs/did-connect-runtime-js.spec.ts +98 -0
  30. package/api/tests/libs/did-connect-tenant-identity.spec.ts +159 -0
  31. package/api/tests/libs/service-host.spec.ts +37 -0
  32. package/api/tests/queues/event-tenant.spec.ts +60 -4
  33. package/api/tests/service/didconnect-storage-slot.spec.ts +60 -0
  34. package/api/tests/service/fail-closed-http.spec.ts +79 -0
  35. package/api/tests/service/static-arc-handler.spec.ts +101 -0
  36. package/api/tests/service/static-externalized.spec.ts +48 -0
  37. package/blocklet.yml +1 -1
  38. package/cloudflare/MIGRATION-RUNBOOK.md +3 -8
  39. package/cloudflare/README.md +8 -21
  40. package/cloudflare/STAGING-MIGRATION-GUIDE.md +3 -15
  41. package/cloudflare/build.ts +10 -5
  42. package/cloudflare/cf-adapter.ts +419 -0
  43. package/cloudflare/did-connect-runtime.ts +96 -0
  44. package/cloudflare/did-connect-token-storage.ts +151 -0
  45. package/cloudflare/esbuild-cf-config.cjs +407 -0
  46. package/cloudflare/run-build.js +33 -357
  47. package/cloudflare/scripts/cf-package-import-probe.mjs +90 -0
  48. package/cloudflare/scripts/didconnect-mock-smoke.mjs +140 -0
  49. package/cloudflare/shims/blocklet-sdk/wallet-authenticator.ts +16 -1
  50. package/cloudflare/shims/blocklet-sdk/wallet-handler.ts +18 -3
  51. package/cloudflare/tests/cf-adapter.spec.ts +244 -0
  52. package/cloudflare/tests/did-connect-token-storage.spec.ts +105 -0
  53. package/cloudflare/tests/worker-handler-gate.spec.ts +35 -10
  54. package/cloudflare/vite.config.ts +53 -45
  55. package/cloudflare/worker.ts +98 -56
  56. package/cloudflare/wrangler.json +0 -6
  57. package/cloudflare/wrangler.jsonc +0 -6
  58. package/cloudflare/wrangler.local-e2e.jsonc +0 -1
  59. package/cloudflare/wrangler.staging.json +0 -6
  60. package/package.json +7 -7
  61. package/scripts/bootstrap-inject.ts +166 -0
  62. package/src/app.tsx +2 -1
  63. package/src/libs/service-host.ts +13 -0
  64. package/vite.arc.config.ts +159 -0
  65. package/cloudflare/did-connect-auth.ts +0 -310
  66. package/cloudflare/shims/blocklet-sdk/util-csrf.ts +0 -13
  67. package/cloudflare/shims/blocklet-sdk/util-wallet.ts +0 -8
@@ -0,0 +1,407 @@
1
+ // Shared CF Workers esbuild config — the ONE worker-safe alias/shim/plugin/banner
2
+ // table for the payment graph. Both consumers reuse it so the two build outputs
3
+ // can never drift (S3-CF plan "从源头消除 drift"):
4
+ //
5
+ // - run-build.js → bundles cloudflare/worker.ts into dist/worker.js
6
+ // (the standalone Payment Kit Worker; default export).
7
+ // - packages/payment-core/build-cf.mjs
8
+ // → bundles the runtime-neutral http.fetch adapter into
9
+ // @arcblock/payment-service/dist/cf.js (a LIBRARY: no
10
+ // default export — the consumer, arc, provides it).
11
+ //
12
+ // The whole point of the `./cf` artifact (S3-CF Phase 2 命门) is that it is a
13
+ // SELF-CONTAINED RE-BUNDLEABLE artifact: every npm/server dependency is inlined
14
+ // here behind these shims, so a downstream consumer (arc's worker build)
15
+ // re-bundles dist/cf.js with ZERO alias / ZERO define / ZERO external of its own.
16
+ // Only `node:*` builtins (CF nodejs_compat) + `cloudflare:*` (worker runtime)
17
+ // remain as imports, which every Workers host already provides.
18
+
19
+ const path = require('path');
20
+
21
+ const cfDir = __dirname;
22
+ const s = (f) => path.resolve(cfDir, f);
23
+
24
+ // Plugin to neutralize rolldown-generated `__require(import.meta.url)` helpers
25
+ // that ship inside npm packages built with rolldown (e.g. @ocap/message,
26
+ // @arcblock/did-connect-js). These helpers use `createRequire(import.meta.url)`
27
+ // at module init to pull in CJS deps like `google-protobuf`, `debug`, etc. —
28
+ // which neither resolve nor work in the CF Workers runtime.
29
+ //
30
+ // The downstream patches/shims that use these require calls are all either:
31
+ // - monkey-patches for edge cases Payment Kit doesn't exercise, or
32
+ // - debug()/logger() factories that Payment Kit doesn't need on CF.
33
+ //
34
+ // Replace every `_virtual/rolldown_runtime.mjs` with a stub whose `__require`
35
+ // returns a Proxy that swallows further property accesses + function calls,
36
+ // and whose `__commonJSMin` returns an empty-module factory. This lets the
37
+ // importing modules finish loading without crashing the worker at startup.
38
+ const rolldownRuntimeStub = `
39
+ // After the Phase 2 CBOR switch (payment-method.ts + did-connect-auth.ts
40
+ // moved off @ocap/client/legacy), no codepath in the worker calls
41
+ // __require('google-protobuf') at runtime. The CBOR-only @ocap/client
42
+ // default entry + @ocap/client/encode use plain ESM imports for their
43
+ // deps. Any remaining __require calls in Rolldown-compiled packages
44
+ // (debug, @arcblock/event-hub, etc.) are handled by the __noop Proxy
45
+ // below — they don't need real modules for payment-kit's flows.
46
+ const __requireMap = {};
47
+ const __noopTarget = function(){};
48
+ const __noopHandler = {
49
+ get(t, p) {
50
+ if (p === Symbol.toPrimitive || p === 'toString') return () => '';
51
+ if (p === 'default') return new Proxy(__noopTarget, __noopHandler);
52
+ return new Proxy(__noopTarget, __noopHandler);
53
+ },
54
+ apply() { return new Proxy(__noopTarget, __noopHandler); },
55
+ construct() { return new Proxy(__noopTarget, __noopHandler); },
56
+ };
57
+ const __noop = new Proxy(__noopTarget, __noopHandler);
58
+ export const __commonJSMin = function(cb){
59
+ return function(){
60
+ var m = { exports: {} };
61
+ try { cb(m.exports, m); } catch (_) {}
62
+ return m.exports;
63
+ };
64
+ };
65
+ export const __require = function(id){
66
+ if (__requireMap[id]) return __requireMap[id];
67
+ return __noop;
68
+ };
69
+ export const __exportAll = function(target, source){
70
+ if (source) {
71
+ try {
72
+ for (var key in source) {
73
+ if (key !== 'default' && !Object.prototype.hasOwnProperty.call(target, key)) {
74
+ Object.defineProperty(target, key, {
75
+ get: function() { return source[key]; },
76
+ enumerable: true,
77
+ configurable: true,
78
+ });
79
+ }
80
+ }
81
+ } catch (_) {}
82
+ }
83
+ return target;
84
+ };
85
+ export const __toESM = function(mod){ return mod && mod.__esModule ? mod : { default: mod }; };
86
+ export const __toCommonJS = function(mod){ return mod; };
87
+ export const __copyProps = function(target){ return target; };
88
+ export default { __commonJSMin, __require, __exportAll, __toESM, __toCommonJS, __copyProps };
89
+ `;
90
+ const rolldownRuntimeNoopPlugin = {
91
+ name: 'rolldown-runtime-noop',
92
+ setup(build) {
93
+ // Catch any import that resolves to a `_virtual/rolldown_runtime.mjs`
94
+ // file, regardless of which package it comes from.
95
+ build.onResolve({ filter: /_virtual\/rolldown_runtime\.mjs$/ }, (args) => {
96
+ return { path: args.path, namespace: 'rolldown-runtime-noop' };
97
+ });
98
+ build.onLoad({ filter: /.*/, namespace: 'rolldown-runtime-noop' }, () => ({
99
+ contents: rolldownRuntimeStub,
100
+ loader: 'js',
101
+ // Need a resolveDir so the virtual stub's `import "google-protobuf"` resolves
102
+ // against the workspace's node_modules. Pointing at cfDir is sufficient since
103
+ // pnpm hoists google-protobuf up to the repo root node_modules.
104
+ resolveDir: cfDir,
105
+ }));
106
+ },
107
+ };
108
+
109
+ // Plugin: fix lodash sub-path CJS/ESM interop
110
+ // The "lodash" → "lodash-es" alias also redirects require('lodash/camelCase')
111
+ // to lodash-es/camelCase (ESM). CJS consumers get { __esModule, default: fn }
112
+ // instead of the function itself, causing "_m is not a function" at runtime.
113
+ // Fix: intercept lodash-es sub-path imports from CJS contexts and generate a
114
+ // virtual wrapper that re-exports the default as module.exports.
115
+ const lodashSubpathPlugin = {
116
+ name: 'lodash-subpath-interop',
117
+ setup(build) {
118
+ // Intercept resolved lodash-es sub-path imports that come from require() calls
119
+ // Intercept CJS require('lodash/xxx') — the alias hasn't applied yet at this stage
120
+ build.onResolve({ filter: /^lodash\/.+/ }, (args) => {
121
+ if (args.kind === 'require-call') {
122
+ // Convert lodash/xxx → lodash-es/xxx and wrap for CJS compat
123
+ const esPath = args.path.replace(/^lodash\//, 'lodash-es/');
124
+ return {
125
+ path: esPath,
126
+ namespace: 'lodash-cjs-compat',
127
+ };
128
+ }
129
+ });
130
+ build.onLoad({ filter: /.*/, namespace: 'lodash-cjs-compat' }, (args) => {
131
+ // Resolve the actual file path, then read and re-export as CJS.
132
+ // By loading the original ESM source as 'js' with CJS-style wrapping,
133
+ // esbuild treats the result as CJS, avoiding the __toModule interop.
134
+ const subPath = args.path.replace('lodash-es/', '');
135
+ const filePath = require.resolve(`lodash-es/${subPath}`);
136
+ const source = require('fs').readFileSync(filePath, 'utf8');
137
+ // Transform: replace ESM export default with module.exports
138
+ // lodash-es modules all follow: import deps... ; function fn(...){...}; export default fn;
139
+ const transformed = source
140
+ .replace(/^export default /m, 'module.exports = ')
141
+ .replace(/^export\s*\{[^}]*\}\s*;?\s*$/m, '');
142
+ return {
143
+ contents: transformed,
144
+ loader: 'js',
145
+ resolveDir: require('path').dirname(filePath),
146
+ };
147
+ });
148
+ },
149
+ };
150
+
151
+ // Plugin: redirect bare `@arcblock/did-util` and `@ocap/message` imports to their
152
+ // CBOR-only subpath entries, and noop `@arcblock/did-util/protobuf`. This strips
153
+ // the protobuf runtime (`@ocap/proto/runtime`, `google-protobuf`, `*_pb.js`) out
154
+ // of the CF Workers bundle. Only matches EXACT bare specifiers — subpath imports
155
+ // (e.g. `@arcblock/did-util/cbor`) pass through untouched.
156
+ const cborOnlyPlugin = {
157
+ name: 'cbor-only-redirect',
158
+ setup(build) {
159
+ build.onResolve({ filter: /^@arcblock\/did-util$/ }, () => ({
160
+ path: path.resolve(cfDir, '../../..', 'node_modules/@arcblock/did-util/esm/cbor.mjs'),
161
+ }));
162
+ build.onResolve({ filter: /^@ocap\/message$/ }, () => ({
163
+ path: path.resolve(cfDir, '../../..', 'node_modules/@ocap/message/esm/cbor.mjs'),
164
+ }));
165
+ build.onResolve({ filter: /^@arcblock\/did-util\/protobuf$/ }, () => ({
166
+ path: s('shims/noop.ts'),
167
+ }));
168
+ },
169
+ };
170
+
171
+ // Plugin: noop entire package trees that have sub-path imports (alias alone
172
+ // doesn't work because esbuild treats "axon" alias as prefix, breaking
173
+ // "axon/lib/plugins/queue"). This plugin intercepts bare + sub-path imports.
174
+ const noopPackagesPlugin = {
175
+ name: 'noop-packages',
176
+ setup(build) {
177
+ const packages = ['axon', '@arcblock/ws', '@arcblock/event-hub', 'graphql', 'follow-redirects', 'proxy-from-env'];
178
+ const filter = new RegExp('^(' + packages.join('|') + ')(\\/|$)');
179
+ build.onResolve({ filter }, () => ({ path: s('shims/noop.ts') }));
180
+ },
181
+ };
182
+
183
+ // Plugin: drop ethers' non-English BIP39 wordlists (~70K dead weight). The payment
184
+ // worker never uses Mnemonic/HD wallets (0 source refs to Mnemonic/HDNode/wordlist),
185
+ // so the 8 non-English word tables never execute. ethers' own /dist build strips
186
+ // these too (~80kb). Keep LangEn (Mnemonic default). Stub the rest with a static
187
+ // wordlist() returning null so wordlists.js's top-level LangXx.wordlist() calls
188
+ // (run at module init) don't crash.
189
+ const dropEthersWordlistsPlugin = {
190
+ name: 'drop-ethers-wordlists',
191
+ setup(build) {
192
+ build.onLoad({ filter: /ethers\/lib\.esm\/wordlists\/lang-(cz|es|fr|ja|ko|it|pt|zh)\.js$/ }, (args) => {
193
+ const lang = /lang-(\w+)\.js$/.exec(args.path)[1];
194
+ const cls = 'Lang' + lang.charAt(0).toUpperCase() + lang.slice(1);
195
+ return { contents: `export class ${cls} { static wordlist() { return null; } }`, loader: 'js' };
196
+ });
197
+ },
198
+ };
199
+
200
+ // The worker-safe alias table: every Node/server dependency the payment graph
201
+ // reaches is redirected to a CF Workers-safe shim or a tree-shakeable ESM build.
202
+ const alias = {
203
+ // axios → lightweight fetch-based shim (115KB → ~2KB)
204
+ axios: s('shims/axios-lite.ts'),
205
+
206
+ // node-fetch → native fetch (drops encoding/tr46/whatwg-url polyfill ~754KB
207
+ // pulled in by @apple/app-store-server-library; dead weight on CF Workers)
208
+ 'node-fetch': s('shims/node-fetch.ts'),
209
+
210
+ // Stripe — wrap constructor to use fetch HTTP client in CF Workers
211
+ stripe: s('shims/stripe-cf.ts'),
212
+ __real_stripe__: require.resolve('stripe'),
213
+
214
+ // @ocap/message ships a patch.mjs that monkey-patches google-protobuf at
215
+ // module init. google-protobuf isn't available in CF Workers (no filesystem
216
+ // for createRequire to resolve), and Payment Kit doesn't use the features
217
+ // that patch touches. Replace with a noop so the import chain stays alive
218
+ // but the patch is skipped. Same for the rolldown_runtime helper that
219
+ // relies on createRequire(import.meta.url).
220
+ '@ocap/message/esm/patch.mjs': s('shims/noop.ts'),
221
+ '@ocap/message/esm/_virtual/rolldown_runtime.mjs': s('shims/noop.ts'),
222
+
223
+ sequelize: s('shims/sequelize-d1/index.ts'),
224
+ sqlite3: s('shims/noop.ts'),
225
+ 'cls-hooked': s('shims/noop.ts'),
226
+ 'express-async-errors': s('shims/noop.ts'),
227
+ cors: s('shims/cors.ts'),
228
+ 'cookie-parser': s('shims/cookie-parser.ts'),
229
+ '@blocklet/sdk/lib/middlewares/fallback': s('shims/blocklet-sdk/fallback.ts'),
230
+ '@blocklet/sdk/lib/middlewares/cdn': s('shims/blocklet-sdk/cdn.ts'),
231
+ '@blocklet/sdk/lib/middlewares/session': s('shims/blocklet-sdk/session.ts'),
232
+ '@blocklet/sdk/lib/middlewares': s('shims/blocklet-sdk/middlewares.ts'),
233
+ '@blocklet/sdk/lib/env': s('shims/blocklet-sdk/env.ts'),
234
+ '@blocklet/sdk/lib/config': s('shims/blocklet-sdk/config.ts'),
235
+ '@blocklet/sdk/lib/component': s('shims/blocklet-sdk/component.ts'),
236
+ '@blocklet/sdk/lib/wallet': s('shims/blocklet-sdk/wallet.ts'),
237
+ '@blocklet/sdk/lib/wallet-authenticator': s('shims/blocklet-sdk/wallet-authenticator.ts'),
238
+ '@blocklet/sdk/lib/wallet-handler': s('shims/blocklet-sdk/wallet-handler.ts'),
239
+ '@blocklet/sdk/lib/security': s('shims/blocklet-sdk/security.ts'),
240
+ '@blocklet/sdk/lib/util/verify-sign': s('shims/blocklet-sdk/verify-sign.ts'),
241
+ '@blocklet/sdk/lib/util/verify-session': s('shims/blocklet-sdk/verify-session.ts'),
242
+ '@blocklet/sdk/lib/util/component-api': s('shims/blocklet-sdk/component-api.ts'),
243
+ // S3-CF Phase 1 ②: the broad "@blocklet/sdk" alias above also captures these
244
+ // util/* sub-paths (esbuild alias matches sub-paths), mangling them to
245
+ // ".../index.ts/lib/util/*". The hono app-shell pipeline (csrf/cdn) + the
246
+ // sessionMiddleware reach them, so the worker bundle must resolve each one:
247
+ // - csrf + wallet: pass through to the REAL worker-safe modules (util/csrf is
248
+ // crypto-only; util/wallet is zero-dep). The converged single http.fetch
249
+ // runs the full pipeline on the worker, so csrf must REALLY protect write
250
+ // routes — a dead stub would silently disable csrf (plan ② "直通真模块").
251
+ '@blocklet/sdk/lib/util/csrf': require.resolve('@blocklet/sdk/lib/util/csrf'),
252
+ '@blocklet/sdk/lib/util/wallet': require.resolve('@blocklet/sdk/lib/util/wallet'),
253
+ // - asset-host-transformer (cdn, inert on JSON), login (verbatim string
254
+ // checks), service-api (blacklist-off stub): existing worker-safe shims.
255
+ '@blocklet/sdk/lib/util/asset-host-transformer': s('shims/blocklet-sdk/asset-host-transformer.ts'),
256
+ '@blocklet/sdk/lib/util/login': s('shims/blocklet-sdk/login.ts'),
257
+ '@blocklet/sdk/lib/util/service-api': s('shims/blocklet-sdk/service-api.ts'),
258
+ '@blocklet/sdk/lib/error-handler': s('shims/noop.ts'),
259
+ '@blocklet/sdk/lib/did': s('shims/blocklet-sdk/did.ts'),
260
+ '@blocklet/sdk/lib/types/notification': s('shims/noop.ts'),
261
+ '@blocklet/sdk/service/notification': s('shims/blocklet-sdk/notification.ts'),
262
+ '@blocklet/sdk/service/eventbus': s('shims/blocklet-sdk/eventbus.ts'),
263
+ '@blocklet/sdk/service/auth': s('shims/blocklet-sdk/auth-service.ts'),
264
+ '@blocklet/sdk/service/blocklet': s('shims/blocklet-sdk/auth-service.ts'),
265
+ '@blocklet/sdk': s('shims/blocklet-sdk/index.ts'),
266
+ '@blocklet/xss': s('shims/xss.ts'),
267
+ '@blocklet/error': s('shims/error.ts'),
268
+ '@blocklet/logger': s('shims/blocklet-sdk/logger.ts'),
269
+ '@blocklet/payment-vendor': s('shims/payment-vendor.ts'),
270
+ '@arcblock/did-connect-storage-nedb': s('shims/nedb-storage.ts'),
271
+ '@abtnode/cron': s('shims/cron.ts'),
272
+ 'dotenv-flow': s('shims/noop.ts'),
273
+ fastq: s('shims/fastq.ts'),
274
+
275
+ // Bundle size optimizations — lodash base import aliased to lodash-es for tree-shaking.
276
+ // Sub-path imports (lodash/camelCase etc.) handled by lodashSubpathPlugin below.
277
+ lodash: 'lodash-es', // 357KB CJS → tree-shakeable ESM
278
+ esprima: s('shims/noop.ts'), // 215KB — only used by @ocap/contract, not called in payment-kit
279
+ mustache: s('shims/noop.ts'), // 16KB — template engine, not used in payment-kit
280
+ 'mime-types': s('shims/mime-types.ts'), // 150KB — lightweight shim with common MIME types
281
+ 'mime-db': s('shims/noop.ts'),
282
+ ws: s('shims/ws-lite.ts'), // 66KB — native WebSocket wrapper for CF Workers
283
+ 'crypto-js': s('shims/crypto-js-warn.ts'), // 115KB — AES legacy not used, warns if called
284
+ 'crypto-js/aes': s('shims/crypto-js-warn.ts'),
285
+ 'crypto-js/enc-base64': s('shims/noop.ts'),
286
+ 'crypto-js/enc-hex': s('shims/noop.ts'),
287
+ 'crypto-js/enc-latin1': s('shims/noop.ts'),
288
+ 'crypto-js/enc-utf8': s('shims/noop.ts'),
289
+ 'crypto-js/enc-utf16': s('shims/noop.ts'),
290
+
291
+ // Force ESM-only to avoid CJS+ESM double-bundling
292
+ valibot: path.resolve(cfDir, '../../..', 'node_modules/valibot/dist/index.mjs'), // 396KB → ~200KB
293
+ // CF Workers: noop modules not useful in Workers environment
294
+ phoenix: s('shims/noop.ts'), // 36KB — Phoenix channels, only used by @arcblock/ws
295
+ numbro: s('shims/noop.ts'), // 49KB — number formatting, replaced inline in util.ts
296
+
297
+ // Node built-in shims (not fully supported in CF Workers nodejs_compat)
298
+ os: s('shims/node-os.ts'),
299
+ 'node:os': s('shims/node-os.ts'),
300
+ tty: s('shims/node-tty.ts'),
301
+ 'node:tty': s('shims/node-tty.ts'),
302
+ http: s('shims/node-http.ts'),
303
+ 'node:http': s('shims/node-http.ts'),
304
+ https: s('shims/node-https.ts'),
305
+ 'node:https': s('shims/node-https.ts'),
306
+ http2: s('shims/node-http.ts'),
307
+ 'node:http2': s('shims/node-http.ts'),
308
+ net: s('shims/node-net.ts'),
309
+ 'node:net': s('shims/node-net.ts'),
310
+ tls: s('shims/node-misc.ts'),
311
+ 'node:tls': s('shims/node-misc.ts'),
312
+ fs: s('shims/node-fs.ts'),
313
+ 'node:fs': s('shims/node-fs.ts'),
314
+ cluster: s('shims/node-misc.ts'),
315
+ 'node:cluster': s('shims/node-misc.ts'),
316
+ zlib: s('shims/node-zlib.ts'),
317
+ 'node:zlib': s('shims/node-zlib.ts'),
318
+ child_process: s('shims/node-child-process.ts'),
319
+ 'node:child_process': s('shims/node-child-process.ts'),
320
+ bufferutil: s('shims/noop.ts'),
321
+ 'utf-8-validate': s('shims/noop.ts'),
322
+ dns: s('shims/noop.ts'),
323
+ 'node:dns': s('shims/noop.ts'),
324
+ pg: s('shims/noop.ts'),
325
+ postgres: s('shims/noop.ts'),
326
+ };
327
+
328
+ // The require()-polyfill + timer-deferral banner. Every CF bundle (worker OR the
329
+ // re-bundleable ./cf library) needs these globals installed before the payment
330
+ // graph's module-init code runs, so the banner is shared. When the ./cf artifact
331
+ // is re-bundled by arc, this banner code is already inlined into dist/cf.js and
332
+ // runs at import — no consumer banner needed.
333
+ const banner = {
334
+ js: [
335
+ '// Polyfill require() for CJS modules that dynamically require Node builtins',
336
+ 'import __nodeCrypto from "node:crypto";',
337
+ 'import __nodeBuffer from "node:buffer";',
338
+ 'import __nodeEvents from "node:events";',
339
+ 'import __nodeStream from "node:stream";',
340
+ 'import __nodeUtil from "node:util";',
341
+ 'import __nodeAssert from "node:assert";',
342
+ 'import __nodePath from "node:path";',
343
+ 'import __nodeUrl from "node:url";',
344
+ 'import __nodeProcess from "node:process";',
345
+ 'const __qsShim = { stringify: function(o) { return new URLSearchParams(o).toString(); }, parse: function(s) { return Object.fromEntries(new URLSearchParams(s).entries()); } };',
346
+ 'const __nodeModules = { crypto: __nodeCrypto, buffer: __nodeBuffer, events: __nodeEvents, stream: __nodeStream, util: __nodeUtil, assert: __nodeAssert, path: __nodePath, url: __nodeUrl, process: __nodeProcess, querystring: __qsShim };',
347
+ '// Accept both "xxx" and "node:xxx" specifier forms; return empty stub for unknown modules',
348
+ '// (some deps tree-shake to nothing but still call require() at init — a hard throw crashes the worker).',
349
+ 'globalThis.require = globalThis.require || function(m) { var key = typeof m === "string" && m.indexOf("node:") === 0 ? m.slice(5) : m; if (__nodeModules[key]) return __nodeModules[key]; console.warn("[require shim] unknown module: " + m); return {}; };',
350
+ '// Ensure process.stderr.fd exists for debug/supports-color',
351
+ 'if (typeof process !== "undefined") { if (!process.stderr) process.stderr = { fd: 2, write: function(){} }; else if (process.stderr.fd === undefined) process.stderr.fd = 2; if (!process.stdout) process.stdout = { fd: 1, write: function(){} }; else if (process.stdout.fd === undefined) process.stdout.fd = 1; }',
352
+ '// Polyfill process.on/process.exit for modules that use them at init',
353
+ 'if (typeof process !== "undefined" && !process.on) { process.on = function(){}; process.once = function(){}; process.off = function(){}; process.removeListener = function(){}; process.emit = function(){}; process.exit = function(){}; }',
354
+ '// CF Workers: defer timers called during global scope init until first request',
355
+ 'var __deferredTimers = []; var __timersReady = false;',
356
+ 'globalThis.__flushDeferredTimers = function() { if (__timersReady) return; __timersReady = true; var q = __deferredTimers; __deferredTimers = []; q.forEach(function(entry) { try { entry.fn.apply(null, entry.args); } catch(e) { console.error("deferred timer error:", e); } }); };',
357
+ 'var _origSetTimeout = globalThis.setTimeout; globalThis.setTimeout = function() { if (!__timersReady) { var args = arguments; var fn = args[0]; __deferredTimers.push({fn: function(){ _origSetTimeout.apply(globalThis, args); }, args: []}); return { unref: function(){}, ref: function(){}, [Symbol.toPrimitive]: function(){ return 0; } }; } var id = _origSetTimeout.apply(this, arguments); if (typeof id === "number") { return { unref: function(){}, ref: function(){}, [Symbol.toPrimitive]: function(){ return id; } }; } if (id && !id.unref) id.unref = function(){}; return id; };',
358
+ 'var _origSetInterval = globalThis.setInterval; globalThis.setInterval = function() { if (!__timersReady) { var args = arguments; __deferredTimers.push({fn: function(){ _origSetInterval.apply(globalThis, args); }, args: []}); return { unref: function(){}, ref: function(){}, [Symbol.toPrimitive]: function(){ return 0; } }; } var id = _origSetInterval.apply(this, arguments); if (typeof id === "number") { return { unref: function(){}, ref: function(){}, [Symbol.toPrimitive]: function(){ return id; } }; } if (id && !id.unref) id.unref = function(){}; return id; };',
359
+ 'var _origSetImmediate = globalThis.setImmediate; if (_origSetImmediate) { globalThis.setImmediate = function() { if (!__timersReady) { var args = arguments; __deferredTimers.push({fn: function(){ _origSetImmediate.apply(globalThis, args); }, args: []}); return { unref: function(){}, ref: function(){} }; } return _origSetImmediate.apply(this, arguments); }; } else { globalThis.setImmediate = function(fn) { if (!__timersReady) { __deferredTimers.push({fn: function(){ _origSetTimeout(fn, 0); }, args: []}); return { unref: function(){}, ref: function(){} }; } return _origSetTimeout(fn, 0); }; }',
360
+ 'if (typeof process !== "undefined") { var _origNextTick = process.nextTick; if (_origNextTick) { process.nextTick = function() { if (!__timersReady) { var args = Array.prototype.slice.call(arguments); var fn = args.shift(); __deferredTimers.push({fn: function(){ _origNextTick.apply(process, [fn].concat(args)); }, args: []}); return; } return _origNextTick.apply(process, arguments); }; } else { process.nextTick = function(fn) { if (!__timersReady) { __deferredTimers.push({fn: function(){ _origSetTimeout(fn, 0); }, args: []}); return; } _origSetTimeout(fn, 0); }; } }',
361
+ ].join('\n'),
362
+ };
363
+
364
+ /**
365
+ * Build the shared esbuild options object for a CF payment bundle.
366
+ *
367
+ * @param {object} opts
368
+ * @param {string[]|Record<string,string>} opts.entryPoints
369
+ * @param {string} opts.outdir
370
+ * @param {string} [opts.outExtension] — e.g. { '.js': '.js' }
371
+ * @param {boolean} [opts.metafile=true]
372
+ * @returns esbuild build options
373
+ */
374
+ function buildCfEsbuildOptions({ entryPoints, outdir, metafile = true }) {
375
+ return {
376
+ entryPoints,
377
+ bundle: true,
378
+ format: 'esm',
379
+ platform: 'node',
380
+ target: 'esnext',
381
+ outdir,
382
+ minify: true,
383
+ sourcemap: true,
384
+ metafile,
385
+ mainFields: ['module', 'main'],
386
+ plugins: [noopPackagesPlugin, rolldownRuntimeNoopPlugin, lodashSubpathPlugin, dropEthersWordlistsPlugin],
387
+ external: ['cloudflare:*', '__STATIC_CONTENT_MANIFEST'],
388
+ // Give import.meta.url a stable fallback so bundled deps that call
389
+ // createRequire(import.meta.url) at module init don't throw at worker
390
+ // startup. The polyfill in banner handles any legitimate require() lookups.
391
+ define: {
392
+ 'import.meta.url': '"file:///worker.js"',
393
+ },
394
+ banner,
395
+ alias,
396
+ logLevel: 'warning',
397
+ };
398
+ }
399
+
400
+ module.exports = {
401
+ cfDir,
402
+ alias,
403
+ banner,
404
+ buildCfEsbuildOptions,
405
+ // exported for completeness / potential reuse; not all consumers need them
406
+ plugins: { rolldownRuntimeNoopPlugin, lodashSubpathPlugin, cborOnlyPlugin, noopPackagesPlugin, dropEthersWordlistsPlugin },
407
+ };