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