@bleedingdev/modern-js-plugin-bff 3.2.0-ultramodern.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.
Files changed (88) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +26 -0
  3. package/cli.js +1 -0
  4. package/dist/cjs/cli.js +294 -0
  5. package/dist/cjs/constants.js +48 -0
  6. package/dist/cjs/index.js +58 -0
  7. package/dist/cjs/loader.js +106 -0
  8. package/dist/cjs/runtime/create-request/index.js +48 -0
  9. package/dist/cjs/runtime/data-platform/index.js +693 -0
  10. package/dist/cjs/runtime/effect/adapter.js +311 -0
  11. package/dist/cjs/runtime/effect/context.js +48 -0
  12. package/dist/cjs/runtime/effect/index.js +608 -0
  13. package/dist/cjs/runtime/effect-client/index.js +178 -0
  14. package/dist/cjs/runtime/hono/adapter.js +168 -0
  15. package/dist/cjs/runtime/hono/index.js +65 -0
  16. package/dist/cjs/runtime/hono/operators.js +68 -0
  17. package/dist/cjs/server.js +179 -0
  18. package/dist/cjs/utils/clientGenerator.js +342 -0
  19. package/dist/cjs/utils/createHonoRoutes.js +138 -0
  20. package/dist/cjs/utils/crossProjectApiPlugin.js +118 -0
  21. package/dist/cjs/utils/effectClientGenerator.js +673 -0
  22. package/dist/cjs/utils/pluginGenerator.js +73 -0
  23. package/dist/cjs/utils/runtimeGenerator.js +133 -0
  24. package/dist/esm/cli.mjs +245 -0
  25. package/dist/esm/constants.mjs +11 -0
  26. package/dist/esm/index.mjs +1 -0
  27. package/dist/esm/loader.mjs +62 -0
  28. package/dist/esm/runtime/create-request/index.mjs +1 -0
  29. package/dist/esm/runtime/data-platform/index.mjs +599 -0
  30. package/dist/esm/runtime/effect/adapter.mjs +267 -0
  31. package/dist/esm/runtime/effect/context.mjs +11 -0
  32. package/dist/esm/runtime/effect/index.mjs +438 -0
  33. package/dist/esm/runtime/effect-client/index.mjs +90 -0
  34. package/dist/esm/runtime/hono/adapter.mjs +124 -0
  35. package/dist/esm/runtime/hono/index.mjs +2 -0
  36. package/dist/esm/runtime/hono/operators.mjs +31 -0
  37. package/dist/esm/server.mjs +135 -0
  38. package/dist/esm/utils/clientGenerator.mjs +293 -0
  39. package/dist/esm/utils/createHonoRoutes.mjs +92 -0
  40. package/dist/esm/utils/crossProjectApiPlugin.mjs +54 -0
  41. package/dist/esm/utils/effectClientGenerator.mjs +623 -0
  42. package/dist/esm/utils/pluginGenerator.mjs +29 -0
  43. package/dist/esm/utils/runtimeGenerator.mjs +89 -0
  44. package/dist/esm-node/cli.mjs +249 -0
  45. package/dist/esm-node/constants.mjs +12 -0
  46. package/dist/esm-node/index.mjs +2 -0
  47. package/dist/esm-node/loader.mjs +64 -0
  48. package/dist/esm-node/runtime/create-request/index.mjs +2 -0
  49. package/dist/esm-node/runtime/data-platform/index.mjs +600 -0
  50. package/dist/esm-node/runtime/effect/adapter.mjs +269 -0
  51. package/dist/esm-node/runtime/effect/context.mjs +12 -0
  52. package/dist/esm-node/runtime/effect/index.mjs +439 -0
  53. package/dist/esm-node/runtime/effect-client/index.mjs +91 -0
  54. package/dist/esm-node/runtime/hono/adapter.mjs +125 -0
  55. package/dist/esm-node/runtime/hono/index.mjs +3 -0
  56. package/dist/esm-node/runtime/hono/operators.mjs +32 -0
  57. package/dist/esm-node/server.mjs +136 -0
  58. package/dist/esm-node/utils/clientGenerator.mjs +294 -0
  59. package/dist/esm-node/utils/createHonoRoutes.mjs +93 -0
  60. package/dist/esm-node/utils/crossProjectApiPlugin.mjs +55 -0
  61. package/dist/esm-node/utils/effectClientGenerator.mjs +625 -0
  62. package/dist/esm-node/utils/pluginGenerator.mjs +33 -0
  63. package/dist/esm-node/utils/runtimeGenerator.mjs +91 -0
  64. package/dist/types/cli.d.ts +3 -0
  65. package/dist/types/constants.d.ts +2 -0
  66. package/dist/types/index.d.ts +1 -0
  67. package/dist/types/loader.d.ts +27 -0
  68. package/dist/types/runtime/create-request/index.d.ts +2 -0
  69. package/dist/types/runtime/data-platform/index.d.ts +187 -0
  70. package/dist/types/runtime/effect/adapter.d.ts +22 -0
  71. package/dist/types/runtime/effect/context.d.ts +8 -0
  72. package/dist/types/runtime/effect/index.d.ts +171 -0
  73. package/dist/types/runtime/effect-client/index.d.ts +47 -0
  74. package/dist/types/runtime/hono/adapter.d.ts +19 -0
  75. package/dist/types/runtime/hono/index.d.ts +2 -0
  76. package/dist/types/runtime/hono/operators.d.ts +10 -0
  77. package/dist/types/server.d.ts +3 -0
  78. package/dist/types/utils/clientGenerator.d.ts +37 -0
  79. package/dist/types/utils/createHonoRoutes.d.ts +10 -0
  80. package/dist/types/utils/crossProjectApiPlugin.d.ts +9 -0
  81. package/dist/types/utils/effectClientGenerator.d.ts +27 -0
  82. package/dist/types/utils/pluginGenerator.d.ts +9 -0
  83. package/dist/types/utils/runtimeGenerator.d.ts +7 -0
  84. package/docs/data-platform-architecture.md +61 -0
  85. package/package.json +172 -0
  86. package/rslib.config.mts +4 -0
  87. package/rstest.config.mts +10 -0
  88. package/server.js +1 -0
@@ -0,0 +1,693 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ DEFAULT_DATA_BATCH_ENDPOINT: ()=>DEFAULT_DATA_BATCH_ENDPOINT,
28
+ DEFAULT_DATA_BATCH_HEADER: ()=>DEFAULT_DATA_BATCH_HEADER,
29
+ DEFAULT_DATA_ENVELOPE_HEADER: ()=>DEFAULT_DATA_ENVELOPE_HEADER,
30
+ buildQueryKey: ()=>buildQueryKey,
31
+ buildScopeKey: ()=>buildScopeKey,
32
+ createDataBatchTransport: ()=>createDataBatchTransport,
33
+ createHydrationEnvelope: ()=>createHydrationEnvelope,
34
+ createInvalidationEvent: ()=>createInvalidationEvent,
35
+ createOperationId: ()=>createOperationId,
36
+ createRequestEnvelope: ()=>createRequestEnvelope,
37
+ decodeRequestEnvelopeHeader: ()=>decodeRequestEnvelopeHeader,
38
+ deriveChildTraceContext: ()=>deriveChildTraceContext,
39
+ encodeRequestEnvelopeHeader: ()=>encodeRequestEnvelopeHeader,
40
+ formatTraceparentHeader: ()=>formatTraceparentHeader,
41
+ normalizeOrigin: ()=>normalizeOrigin,
42
+ parseTraceparentHeader: ()=>parseTraceparentHeader,
43
+ shouldApplyInvalidation: ()=>shouldApplyInvalidation,
44
+ stableStringify: ()=>stableStringify,
45
+ validateHydrationEnvelope: ()=>validateHydrationEnvelope,
46
+ validateRequestEnvelope: ()=>validateRequestEnvelope,
47
+ validateSelectionPlan: ()=>validateSelectionPlan
48
+ });
49
+ const DEFAULT_DATA_ENVELOPE_HEADER = 'x-modernjs-data-envelope';
50
+ const DEFAULT_DATA_BATCH_ENDPOINT = '/_data/batch';
51
+ const DEFAULT_DATA_BATCH_HEADER = 'x-modernjs-data-batch';
52
+ const TRACEPARENT_REGEX = /^00-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$/i;
53
+ function isPlainObject(value) {
54
+ if ('object' != typeof value || null === value || Array.isArray(value)) return false;
55
+ const proto = Object.getPrototypeOf(value);
56
+ return proto === Object.prototype || null === proto;
57
+ }
58
+ function canonicalize(value) {
59
+ if (Array.isArray(value)) return value.map((item)=>canonicalize(item));
60
+ if (isPlainObject(value)) return Object.keys(value).sort().reduce((acc, key)=>{
61
+ acc[key] = canonicalize(value[key]);
62
+ return acc;
63
+ }, {});
64
+ return value;
65
+ }
66
+ function stableStringify(value) {
67
+ return JSON.stringify(canonicalize(value));
68
+ }
69
+ function encodeRequestEnvelopeHeader(envelope) {
70
+ return encodeURIComponent(stableStringify(envelope));
71
+ }
72
+ function isRequestEnvelopeShape(value) {
73
+ if (!isPlainObject(value)) return false;
74
+ return 1 === value.protocolVersion && 'string' == typeof value.operationId && 'string' == typeof value.appNamespace && 'string' == typeof value.origin && 'string' == typeof value.scopeKey && 'string' == typeof value.inputHash && 'number' == typeof value.timestamp;
75
+ }
76
+ function decodeRequestEnvelopeHeader(value) {
77
+ try {
78
+ const decoded = decodeURIComponent(value);
79
+ const parsed = JSON.parse(decoded);
80
+ if (!isRequestEnvelopeShape(parsed)) return null;
81
+ return parsed;
82
+ } catch {
83
+ return null;
84
+ }
85
+ }
86
+ function hashString(value) {
87
+ let hash = 0x811c9dc5;
88
+ for(let index = 0; index < value.length; index++){
89
+ hash ^= value.charCodeAt(index);
90
+ hash = Math.imul(hash, 0x01000193);
91
+ }
92
+ return (hash >>> 0).toString(16).padStart(8, '0');
93
+ }
94
+ function sanitizeSegment(segment) {
95
+ const normalized = segment.trim().replace(/[^a-zA-Z0-9_-]+/g, '_');
96
+ return normalized.length > 0 ? normalized : 'unknown';
97
+ }
98
+ function normalizeOrigin(origin) {
99
+ const normalized = origin.trim();
100
+ try {
101
+ const url = new URL(normalized);
102
+ return `${url.protocol}//${url.host}`.toLowerCase();
103
+ } catch {
104
+ return normalized.toLowerCase();
105
+ }
106
+ }
107
+ function normalizeOperationDescriptor(input) {
108
+ return {
109
+ appNamespace: sanitizeSegment(input.appNamespace),
110
+ apiId: sanitizeSegment(input.apiId),
111
+ group: sanitizeSegment(input.group),
112
+ endpoint: sanitizeSegment(input.endpoint),
113
+ schemaHash: input.schemaHash || null,
114
+ version: input.version ?? 1
115
+ };
116
+ }
117
+ function createOperationId(input) {
118
+ const descriptor = normalizeOperationDescriptor(input);
119
+ const readable = `${descriptor.appNamespace}.${descriptor.apiId}.${descriptor.group}.${descriptor.endpoint}.v${String(descriptor.version)}`;
120
+ return `${readable}:${hashString(stableStringify(descriptor))}`;
121
+ }
122
+ function buildScopeKey(scope) {
123
+ const canonical = {
124
+ appNamespace: sanitizeSegment(scope.appNamespace),
125
+ origin: normalizeOrigin(scope.origin),
126
+ tenantId: scope.tenantId ?? null,
127
+ userId: scope.userId ?? null,
128
+ sessionId: scope.sessionId ?? null
129
+ };
130
+ return `${canonical.appNamespace}:${hashString(stableStringify(canonical))}`;
131
+ }
132
+ function buildQueryKey(input) {
133
+ const canonical = {
134
+ operationId: input.operationId,
135
+ scopeKey: input.scopeKey,
136
+ requestMode: input.requestMode ?? 'cache-first',
137
+ requestInput: input.requestInput ?? null,
138
+ selectionPlan: input.selectionPlan ?? null
139
+ };
140
+ return `${input.operationId}:${hashString(stableStringify(canonical))}`;
141
+ }
142
+ function isAllZeroHex(value) {
143
+ return /^0+$/.test(value);
144
+ }
145
+ function isValidHex(value, length) {
146
+ return value.length === length && /^[0-9a-f]+$/.test(value);
147
+ }
148
+ function parseTraceparentHeader(header) {
149
+ const match = header.trim().match(TRACEPARENT_REGEX);
150
+ if (!match) return null;
151
+ const traceId = match[1].toLowerCase();
152
+ const spanId = match[2].toLowerCase();
153
+ const flags = match[3].toLowerCase();
154
+ if (isAllZeroHex(traceId) || isAllZeroHex(spanId)) return null;
155
+ const sampled = (0x1 & Number.parseInt(flags, 16)) === 1;
156
+ return {
157
+ traceId,
158
+ spanId,
159
+ sampled
160
+ };
161
+ }
162
+ function formatTraceparentHeader(trace) {
163
+ const traceId = trace.traceId.toLowerCase();
164
+ const spanId = trace.spanId.toLowerCase();
165
+ if (!isValidHex(traceId, 32) || !isValidHex(spanId, 16)) throw new Error('Invalid trace context: traceId/spanId format mismatch');
166
+ if (isAllZeroHex(traceId) || isAllZeroHex(spanId)) throw new Error('Invalid trace context: traceId/spanId cannot be zero');
167
+ const flags = trace.sampled ? '01' : '00';
168
+ return `00-${traceId}-${spanId}-${flags}`;
169
+ }
170
+ function deriveChildTraceContext(parent, childSpanId) {
171
+ const normalizedSpanId = childSpanId.toLowerCase();
172
+ if (!isValidHex(normalizedSpanId, 16) || isAllZeroHex(normalizedSpanId)) throw new Error('Invalid child span id');
173
+ return {
174
+ traceId: parent.traceId.toLowerCase(),
175
+ spanId: normalizedSpanId,
176
+ sampled: parent.sampled,
177
+ parentSpanId: parent.spanId.toLowerCase()
178
+ };
179
+ }
180
+ function validateSelectionPlan(plan, options = {}) {
181
+ const maxDepthLimit = options.maxDepth ?? 8;
182
+ const maxFieldsLimit = options.maxFields ?? 256;
183
+ const allowedLeafPaths = options.allowedLeafPaths ? new Set(options.allowedLeafPaths) : null;
184
+ const errors = [];
185
+ let maxDepth = 0;
186
+ let fieldCount = 0;
187
+ const walk = (node, path)=>{
188
+ if (!isPlainObject(node)) return void errors.push(`Selection node at "${path.join('.') || '<root>'}" must be an object`);
189
+ const keys = Object.keys(node);
190
+ if (0 === keys.length) return void errors.push(`Selection node at "${path.join('.') || '<root>'}" cannot be empty`);
191
+ for (const key of keys){
192
+ fieldCount += 1;
193
+ if (fieldCount > maxFieldsLimit) return void errors.push(`Selection has too many fields: ${String(fieldCount)} > ${String(maxFieldsLimit)}`);
194
+ const nextPath = [
195
+ ...path,
196
+ key
197
+ ];
198
+ const depth = nextPath.length;
199
+ if (depth > maxDepth) maxDepth = depth;
200
+ if (depth > maxDepthLimit) return void errors.push(`Selection exceeds maxDepth at "${nextPath.join('.')}" (${String(depth)} > ${String(maxDepthLimit)})`);
201
+ const value = node[key];
202
+ if (true === value) {
203
+ if (allowedLeafPaths && !allowedLeafPaths.has(nextPath.join('.'))) errors.push(`Unknown selected field "${nextPath.join('.')}"`);
204
+ continue;
205
+ }
206
+ if (!isPlainObject(value)) {
207
+ errors.push(`Invalid selection value at "${nextPath.join('.')}"; expected true or nested object`);
208
+ continue;
209
+ }
210
+ walk(value, nextPath);
211
+ }
212
+ };
213
+ walk(plan, []);
214
+ return {
215
+ ok: 0 === errors.length,
216
+ errors,
217
+ stats: {
218
+ maxDepth,
219
+ fieldCount
220
+ }
221
+ };
222
+ }
223
+ function createRequestEnvelope(input) {
224
+ if (input.requireTraceContext && !input.traceContext) throw new Error('Trace context is required for this request envelope');
225
+ const traceparent = input.traceContext ? formatTraceparentHeader(input.traceContext) : void 0;
226
+ const envelope = {
227
+ protocolVersion: input.protocolVersion ?? 1,
228
+ operationId: createOperationId(input.operation),
229
+ appNamespace: sanitizeSegment(input.scope.appNamespace),
230
+ origin: normalizeOrigin(input.scope.origin),
231
+ requestMode: input.requestMode ?? 'cache-first',
232
+ mutationMode: input.mutationMode,
233
+ scopeKey: buildScopeKey(input.scope),
234
+ input: input.requestInput,
235
+ inputHash: hashString(stableStringify(input.requestInput ?? null)),
236
+ selectionPlan: input.selectionPlan,
237
+ selectionHash: input.selectionPlan ? hashString(stableStringify(input.selectionPlan)) : void 0,
238
+ traceparent,
239
+ timestamp: input.timestamp ?? Date.now()
240
+ };
241
+ return envelope;
242
+ }
243
+ function validateRequestEnvelope(envelope, options = {}) {
244
+ const errors = [];
245
+ if (void 0 !== options.expectedProtocolVersion && envelope.protocolVersion !== options.expectedProtocolVersion) errors.push(`Protocol mismatch: expected ${String(options.expectedProtocolVersion)} but received ${String(envelope.protocolVersion)}`);
246
+ if (options.expectedNamespace && envelope.appNamespace !== options.expectedNamespace) errors.push(`Namespace mismatch: expected ${options.expectedNamespace} but received ${envelope.appNamespace}`);
247
+ if (options.expectedOrigin) {
248
+ const expectedOrigin = normalizeOrigin(options.expectedOrigin);
249
+ if (envelope.origin !== expectedOrigin) errors.push(`Origin mismatch: expected ${expectedOrigin} but received ${envelope.origin}`);
250
+ }
251
+ if (!envelope.operationId) errors.push('Missing operationId');
252
+ if (!envelope.scopeKey) errors.push('Missing scopeKey');
253
+ if (options.requireTraceContext && !envelope.traceparent) errors.push('Missing trace context');
254
+ if (envelope.traceparent && !parseTraceparentHeader(envelope.traceparent)) errors.push('Invalid traceparent header');
255
+ const computedInputHash = hashString(stableStringify(envelope.input ?? null));
256
+ if (computedInputHash !== envelope.inputHash) errors.push('Input hash mismatch');
257
+ if (envelope.selectionPlan) {
258
+ const computedSelectionHash = hashString(stableStringify(envelope.selectionPlan));
259
+ if (computedSelectionHash !== envelope.selectionHash) errors.push('Selection hash mismatch');
260
+ }
261
+ return {
262
+ ok: 0 === errors.length,
263
+ errors
264
+ };
265
+ }
266
+ function createHydrationEnvelope(input) {
267
+ const envelopeWithoutChecksum = {
268
+ protocolVersion: input.protocolVersion ?? 1,
269
+ runtimeVersion: input.runtimeVersion,
270
+ appNamespace: sanitizeSegment(input.scope.appNamespace),
271
+ origin: normalizeOrigin(input.scope.origin),
272
+ createdAt: input.createdAt ?? Date.now(),
273
+ payload: input.payload
274
+ };
275
+ const checksum = hashString(stableStringify(envelopeWithoutChecksum));
276
+ return {
277
+ ...envelopeWithoutChecksum,
278
+ checksum
279
+ };
280
+ }
281
+ function validateHydrationEnvelope(envelope, options = {}) {
282
+ const errors = [];
283
+ if (void 0 !== options.expectedProtocolVersion && envelope.protocolVersion !== options.expectedProtocolVersion) errors.push(`Protocol mismatch: expected ${String(options.expectedProtocolVersion)} but received ${String(envelope.protocolVersion)}`);
284
+ if (options.expectedNamespace && envelope.appNamespace !== options.expectedNamespace) errors.push(`Namespace mismatch: expected ${options.expectedNamespace} but received ${envelope.appNamespace}`);
285
+ if (options.expectedOrigin) {
286
+ const expectedOrigin = normalizeOrigin(options.expectedOrigin);
287
+ if (envelope.origin !== expectedOrigin) errors.push(`Origin mismatch: expected ${expectedOrigin} but received ${envelope.origin}`);
288
+ }
289
+ if (options.expectedRuntimeVersion && envelope.runtimeVersion !== options.expectedRuntimeVersion) errors.push(`Runtime version mismatch: expected ${options.expectedRuntimeVersion} but received ${envelope.runtimeVersion}`);
290
+ const checksumBase = {
291
+ protocolVersion: envelope.protocolVersion,
292
+ runtimeVersion: envelope.runtimeVersion,
293
+ appNamespace: envelope.appNamespace,
294
+ origin: envelope.origin,
295
+ createdAt: envelope.createdAt,
296
+ payload: envelope.payload
297
+ };
298
+ const checksum = hashString(stableStringify(checksumBase));
299
+ if (checksum !== envelope.checksum) errors.push('Hydration checksum mismatch');
300
+ return {
301
+ ok: 0 === errors.length,
302
+ errors
303
+ };
304
+ }
305
+ function createInvalidationEvent(input) {
306
+ return {
307
+ sourceNamespace: sanitizeSegment(input.sourceOperation.appNamespace),
308
+ sourceOperationId: createOperationId(input.sourceOperation),
309
+ scopeKey: buildScopeKey(input.sourceScope),
310
+ targetNamespaces: input.targetNamespaces?.map((namespace)=>sanitizeSegment(namespace)),
311
+ targetOperationIds: input.targetOperations?.map((operation)=>createOperationId(operation))
312
+ };
313
+ }
314
+ function shouldApplyInvalidation(event, subscriber) {
315
+ if (subscriber.scopeKey && subscriber.scopeKey !== event.scopeKey) return false;
316
+ const sameNamespace = subscriber.namespace === event.sourceNamespace;
317
+ const allowedCrossNamespace = true === subscriber.acceptCrossNamespace && event.targetNamespaces?.includes(subscriber.namespace) === true;
318
+ if (!sameNamespace && !allowedCrossNamespace) return false;
319
+ if (!subscriber.operationIds || 0 === subscriber.operationIds.length) return true;
320
+ if (!event.targetOperationIds || 0 === event.targetOperationIds.length) return true;
321
+ return subscriber.operationIds.some((operationId)=>event.targetOperationIds?.includes(operationId));
322
+ }
323
+ function resolveRuntimeOrigin() {
324
+ if ("u" > typeof window && window.location && 'string' == typeof window.location.origin && window.location.origin) return window.location.origin;
325
+ if ("u" > typeof globalThis && globalThis.location && 'string' == typeof globalThis.location?.origin) return globalThis.location.origin;
326
+ return 'http://localhost';
327
+ }
328
+ function toAbsoluteUrl(input) {
329
+ if (input instanceof URL) return input;
330
+ if ("u" > typeof Request && input instanceof Request) return new URL(input.url);
331
+ const value = String(input);
332
+ try {
333
+ return new URL(value);
334
+ } catch {
335
+ return new URL(value, resolveRuntimeOrigin());
336
+ }
337
+ }
338
+ function normalizeBatchEndpoint(requestUrl, endpoint) {
339
+ const value = endpoint || DEFAULT_DATA_BATCH_ENDPOINT;
340
+ try {
341
+ return new URL(value);
342
+ } catch {
343
+ return new URL(value, requestUrl.origin);
344
+ }
345
+ }
346
+ function toHeaderRecord(headers) {
347
+ if (!headers) return {};
348
+ if (headers instanceof Headers) {
349
+ const next = {};
350
+ headers.forEach((value, key)=>{
351
+ next[key.toLowerCase()] = value;
352
+ });
353
+ return next;
354
+ }
355
+ if (Array.isArray(headers)) return headers.reduce((acc, [key, value])=>{
356
+ acc[String(key).toLowerCase()] = String(value);
357
+ return acc;
358
+ }, {});
359
+ return Object.entries(headers).reduce((acc, [key, value])=>{
360
+ if (void 0 === value) return acc;
361
+ acc[String(key).toLowerCase()] = Array.isArray(value) ? value.join(', ') : String(value);
362
+ return acc;
363
+ }, {});
364
+ }
365
+ function isBatchResponseItem(value) {
366
+ return isPlainObject(value) && 'string' == typeof value.id && 'number' == typeof value.status;
367
+ }
368
+ function isBatchResponsePayload(value) {
369
+ return isPlainObject(value) && 1 === value.protocolVersion && 'string' == typeof value.batchId && 'number' == typeof value.receivedAt && Array.isArray(value.items) && value.items.every((item)=>isBatchResponseItem(item));
370
+ }
371
+ function measureTextBytes(value) {
372
+ if ("u" > typeof TextEncoder) return new TextEncoder().encode(value).length;
373
+ if ("u" > typeof Buffer) return Buffer.byteLength(value);
374
+ return value.length;
375
+ }
376
+ function createBatchId() {
377
+ const now = Date.now().toString(36);
378
+ const random = Math.random().toString(16).slice(2, 10);
379
+ return `batch_${now}_${random}`;
380
+ }
381
+ function normalizeMethod(method) {
382
+ return (method || 'GET').toUpperCase();
383
+ }
384
+ function toRequestBody(initBody) {
385
+ if ('string' == typeof initBody) return initBody;
386
+ if ("u" > typeof URLSearchParams && initBody instanceof URLSearchParams) return initBody.toString();
387
+ }
388
+ function shouldBatchRequest(input) {
389
+ if (input.requestUrl.href === input.batchEndpoint) return false;
390
+ if ('off' === input.headers[DEFAULT_DATA_BATCH_HEADER]) return false;
391
+ if (!input.allowedMethods.has(input.method)) return false;
392
+ if (void 0 !== input.body) return false;
393
+ return true;
394
+ }
395
+ async function parseResponseLikeCreateRequest(response) {
396
+ const contentType = response.headers.get('content-type') || '';
397
+ if (!response.ok) {
398
+ let data = null;
399
+ data = contentType.includes('application/json') ? await response.json() : await response.text();
400
+ response.data = data;
401
+ throw response;
402
+ }
403
+ if (contentType.includes('application/json') || contentType.includes('text/json')) return response.json();
404
+ if (contentType.includes('text/html') || contentType.includes('text/plain')) return response.text();
405
+ if (contentType.includes('application/x-www-form-urlencoded') || contentType.includes('multipart/form-data')) return response.formData();
406
+ if (contentType.includes('application/octet-stream')) return response.arrayBuffer();
407
+ if (contentType.includes('image/png')) return response;
408
+ return response.text();
409
+ }
410
+ function ensureBucket(buckets, endpoint) {
411
+ const existing = buckets.get(endpoint);
412
+ if (existing) return existing;
413
+ const next = {
414
+ items: [],
415
+ bytes: 0,
416
+ timer: null,
417
+ flushing: false
418
+ };
419
+ buckets.set(endpoint, next);
420
+ return next;
421
+ }
422
+ function createDataBatchTransport(options = {}) {
423
+ const fallbackFetch = 'function' == typeof fetch ? fetch.bind(globalThis) : void 0;
424
+ const baseFetch = options.fetch || fallbackFetch;
425
+ if (!baseFetch) throw new Error('createDataBatchTransport requires a fetch implementation');
426
+ const flushIntervalMs = Math.max(0, options.flushIntervalMs ?? 8);
427
+ const maxBatchSize = Math.max(1, options.maxBatchSize ?? 16);
428
+ const maxBatchBytes = Math.max(1024, options.maxBatchBytes ?? 65536);
429
+ const requestTimeoutMs = options.requestTimeoutMs;
430
+ const allowedMethods = new Set((options.allowedMethods && options.allowedMethods.length > 0 ? options.allowedMethods : [
431
+ 'GET'
432
+ ]).map((method)=>method.toUpperCase()));
433
+ const onEvent = options.onEvent;
434
+ const buckets = new Map();
435
+ const pendingByKey = new Map();
436
+ const disabledEndpoints = new Set();
437
+ const runSingle = async (request)=>{
438
+ const response = await baseFetch(request.requestUrl, request.requestInit);
439
+ return parseResponseLikeCreateRequest(response);
440
+ };
441
+ const settleRequests = async (items, runner)=>{
442
+ await Promise.all(items.map(async (item)=>{
443
+ try {
444
+ const value = await runner(item);
445
+ item.resolve(value);
446
+ } catch (error) {
447
+ item.reject(error);
448
+ } finally{
449
+ pendingByKey.delete(item.key);
450
+ }
451
+ }));
452
+ };
453
+ const flushBucket = async (endpoint)=>{
454
+ const bucket = buckets.get(endpoint);
455
+ if (!bucket || bucket.flushing) return;
456
+ if (bucket.timer) {
457
+ clearTimeout(bucket.timer);
458
+ bucket.timer = null;
459
+ }
460
+ if (0 === bucket.items.length) return;
461
+ bucket.flushing = true;
462
+ const items = bucket.items;
463
+ bucket.items = [];
464
+ bucket.bytes = 0;
465
+ if (1 === items.length || disabledEndpoints.has(endpoint)) {
466
+ onEvent?.({
467
+ type: disabledEndpoints.has(endpoint) ? 'fallback' : 'flush',
468
+ endpoint,
469
+ size: items.length,
470
+ reason: disabledEndpoints.has(endpoint) ? 'batch-disabled' : void 0
471
+ });
472
+ await settleRequests(items, runSingle);
473
+ bucket.flushing = false;
474
+ return;
475
+ }
476
+ const batchId = createBatchId();
477
+ const payload = {
478
+ protocolVersion: 1,
479
+ batchId,
480
+ sentAt: Date.now(),
481
+ items: items.map((item)=>item.item)
482
+ };
483
+ onEvent?.({
484
+ type: 'flush',
485
+ endpoint,
486
+ batchId,
487
+ size: items.length
488
+ });
489
+ const payloadJson = JSON.stringify(payload);
490
+ const traceparent = items.find((item)=>'string' == typeof item.item.headers?.traceparent)?.item.headers?.traceparent || void 0;
491
+ const requestInit = {
492
+ method: 'POST',
493
+ headers: {
494
+ accept: 'application/json, */*;q=0.8',
495
+ 'content-type': 'application/json; charset=utf-8',
496
+ [DEFAULT_DATA_BATCH_HEADER]: '1',
497
+ ...traceparent ? {
498
+ traceparent
499
+ } : {}
500
+ },
501
+ body: payloadJson
502
+ };
503
+ let timeoutHandle = null;
504
+ try {
505
+ const controller = requestTimeoutMs && requestTimeoutMs > 0 ? new AbortController() : void 0;
506
+ if (controller) {
507
+ requestInit.signal = controller.signal;
508
+ timeoutHandle = setTimeout(()=>{
509
+ controller.abort();
510
+ onEvent?.({
511
+ type: 'fallback',
512
+ endpoint,
513
+ batchId,
514
+ size: items.length,
515
+ reason: 'batch-timeout'
516
+ });
517
+ }, requestTimeoutMs);
518
+ }
519
+ const response = await baseFetch(endpoint, requestInit);
520
+ if (!response.ok) {
521
+ if (404 === response.status || 405 === response.status) {
522
+ disabledEndpoints.add(endpoint);
523
+ onEvent?.({
524
+ type: 'disable',
525
+ endpoint,
526
+ batchId,
527
+ reason: `batch-endpoint-unavailable-${String(response.status)}`
528
+ });
529
+ } else onEvent?.({
530
+ type: 'fallback',
531
+ endpoint,
532
+ batchId,
533
+ size: items.length,
534
+ reason: `batch-response-${String(response.status)}`
535
+ });
536
+ await settleRequests(items, runSingle);
537
+ bucket.flushing = false;
538
+ return;
539
+ }
540
+ const result = await response.json();
541
+ if (!isBatchResponsePayload(result)) {
542
+ onEvent?.({
543
+ type: 'fallback',
544
+ endpoint,
545
+ batchId,
546
+ size: items.length,
547
+ reason: 'invalid-batch-response'
548
+ });
549
+ await settleRequests(items, runSingle);
550
+ bucket.flushing = false;
551
+ return;
552
+ }
553
+ const itemMap = new Map();
554
+ for (const item of result.items)itemMap.set(item.id, item);
555
+ await settleRequests(items, async (request)=>{
556
+ const resultItem = itemMap.get(request.item.id);
557
+ if (!resultItem) return runSingle(request);
558
+ const reconstructedResponse = new Response(resultItem.body ?? '', {
559
+ status: resultItem.status,
560
+ headers: resultItem.headers
561
+ });
562
+ return parseResponseLikeCreateRequest(reconstructedResponse);
563
+ });
564
+ } catch (error) {
565
+ onEvent?.({
566
+ type: 'fallback',
567
+ endpoint,
568
+ batchId,
569
+ size: items.length,
570
+ reason: 'batch-transport-error'
571
+ });
572
+ await settleRequests(items, runSingle);
573
+ } finally{
574
+ if (timeoutHandle) clearTimeout(timeoutHandle);
575
+ bucket.flushing = false;
576
+ }
577
+ };
578
+ return (input, init)=>{
579
+ const requestUrl = toAbsoluteUrl(input);
580
+ const batchEndpointUrl = normalizeBatchEndpoint(requestUrl, options.endpoint);
581
+ const endpoint = batchEndpointUrl.toString();
582
+ const method = normalizeMethod(init?.method);
583
+ const body = toRequestBody(init?.body ?? null);
584
+ const headers = toHeaderRecord(init?.headers);
585
+ const normalizedInit = {
586
+ ...init,
587
+ method,
588
+ headers,
589
+ body
590
+ };
591
+ if (disabledEndpoints.has(endpoint) || !shouldBatchRequest({
592
+ method,
593
+ body,
594
+ headers,
595
+ allowedMethods,
596
+ batchEndpoint: endpoint,
597
+ requestUrl
598
+ })) return baseFetch(requestUrl.toString(), normalizedInit).then(parseResponseLikeCreateRequest);
599
+ const item = {
600
+ id: `${Date.now().toString(36)}_${Math.random().toString(16).slice(2, 8)}`,
601
+ path: `${requestUrl.pathname}${requestUrl.search}`,
602
+ method,
603
+ headers,
604
+ ...body ? {
605
+ body
606
+ } : {}
607
+ };
608
+ const key = stableStringify({
609
+ endpoint,
610
+ path: item.path,
611
+ method: item.method,
612
+ headers: item.headers,
613
+ body: item.body ?? null
614
+ });
615
+ const existing = pendingByKey.get(key);
616
+ if (existing) return existing;
617
+ const size = measureTextBytes(stableStringify(item));
618
+ const promise = new Promise((resolve, reject)=>{
619
+ const bucket = ensureBucket(buckets, endpoint);
620
+ const queued = {
621
+ key,
622
+ endpoint,
623
+ requestUrl: requestUrl.toString(),
624
+ requestInit: normalizedInit,
625
+ item,
626
+ size,
627
+ resolve,
628
+ reject
629
+ };
630
+ bucket.items.push(queued);
631
+ bucket.bytes += size;
632
+ onEvent?.({
633
+ type: 'enqueue',
634
+ endpoint,
635
+ size: bucket.items.length
636
+ });
637
+ if (bucket.items.length >= maxBatchSize || bucket.bytes >= maxBatchBytes) return void flushBucket(endpoint);
638
+ if (!bucket.timer) bucket.timer = setTimeout(()=>{
639
+ bucket.timer = null;
640
+ flushBucket(endpoint);
641
+ }, flushIntervalMs);
642
+ });
643
+ pendingByKey.set(key, promise);
644
+ return promise;
645
+ };
646
+ }
647
+ exports.DEFAULT_DATA_BATCH_ENDPOINT = __webpack_exports__.DEFAULT_DATA_BATCH_ENDPOINT;
648
+ exports.DEFAULT_DATA_BATCH_HEADER = __webpack_exports__.DEFAULT_DATA_BATCH_HEADER;
649
+ exports.DEFAULT_DATA_ENVELOPE_HEADER = __webpack_exports__.DEFAULT_DATA_ENVELOPE_HEADER;
650
+ exports.buildQueryKey = __webpack_exports__.buildQueryKey;
651
+ exports.buildScopeKey = __webpack_exports__.buildScopeKey;
652
+ exports.createDataBatchTransport = __webpack_exports__.createDataBatchTransport;
653
+ exports.createHydrationEnvelope = __webpack_exports__.createHydrationEnvelope;
654
+ exports.createInvalidationEvent = __webpack_exports__.createInvalidationEvent;
655
+ exports.createOperationId = __webpack_exports__.createOperationId;
656
+ exports.createRequestEnvelope = __webpack_exports__.createRequestEnvelope;
657
+ exports.decodeRequestEnvelopeHeader = __webpack_exports__.decodeRequestEnvelopeHeader;
658
+ exports.deriveChildTraceContext = __webpack_exports__.deriveChildTraceContext;
659
+ exports.encodeRequestEnvelopeHeader = __webpack_exports__.encodeRequestEnvelopeHeader;
660
+ exports.formatTraceparentHeader = __webpack_exports__.formatTraceparentHeader;
661
+ exports.normalizeOrigin = __webpack_exports__.normalizeOrigin;
662
+ exports.parseTraceparentHeader = __webpack_exports__.parseTraceparentHeader;
663
+ exports.shouldApplyInvalidation = __webpack_exports__.shouldApplyInvalidation;
664
+ exports.stableStringify = __webpack_exports__.stableStringify;
665
+ exports.validateHydrationEnvelope = __webpack_exports__.validateHydrationEnvelope;
666
+ exports.validateRequestEnvelope = __webpack_exports__.validateRequestEnvelope;
667
+ exports.validateSelectionPlan = __webpack_exports__.validateSelectionPlan;
668
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
669
+ "DEFAULT_DATA_BATCH_ENDPOINT",
670
+ "DEFAULT_DATA_BATCH_HEADER",
671
+ "DEFAULT_DATA_ENVELOPE_HEADER",
672
+ "buildQueryKey",
673
+ "buildScopeKey",
674
+ "createDataBatchTransport",
675
+ "createHydrationEnvelope",
676
+ "createInvalidationEvent",
677
+ "createOperationId",
678
+ "createRequestEnvelope",
679
+ "decodeRequestEnvelopeHeader",
680
+ "deriveChildTraceContext",
681
+ "encodeRequestEnvelopeHeader",
682
+ "formatTraceparentHeader",
683
+ "normalizeOrigin",
684
+ "parseTraceparentHeader",
685
+ "shouldApplyInvalidation",
686
+ "stableStringify",
687
+ "validateHydrationEnvelope",
688
+ "validateRequestEnvelope",
689
+ "validateSelectionPlan"
690
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
691
+ Object.defineProperty(exports, '__esModule', {
692
+ value: true
693
+ });