@tokenoftrust/storefront-runner 6.15.1 → 6.15.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.
|
@@ -45,13 +45,14 @@ export interface TenantCatalogFiles {
|
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
47
|
* The most catalog files one request may read from a per-request source that has no listing
|
|
48
|
-
* projection
|
|
49
|
-
*
|
|
50
|
-
* catalog (2,959 products, 357 collections, 27.2 MB of files): read file by
|
|
51
|
-
* calls and ~40 MB of parsed heap per request
|
|
52
|
-
*
|
|
48
|
+
* projection. Each artifact read is its own KV call, and the rest of the page needs its own.
|
|
49
|
+
* It sits at ADR 0033's ~7,000-product single-tenant line. Measured 2026-10-06 on
|
|
50
|
+
* giantvapes.com's catalog (2,959 products, 357 collections, 27.2 MB of files): read file by
|
|
51
|
+
* file it is 3,316 calls and ~40 MB of parsed heap per request, so the budget's ceiling is
|
|
52
|
+
* extrapolated, not measured, at ~95 MB of the Worker's 128 MB; its listing projection is one
|
|
53
|
+
* 11.4 MB read (1.55 MB gzipped), 18 MB of heap, parsed in ~20 ms once per isolate and version.
|
|
53
54
|
*/
|
|
54
|
-
export const CATALOG_REQUEST_READ_BUDGET =
|
|
55
|
+
export const CATALOG_REQUEST_READ_BUDGET = 7000;
|
|
55
56
|
|
|
56
57
|
/**
|
|
57
58
|
* The selected source and its catalog paths. A source that cannot enumerate (`null`:
|
|
@@ -67,6 +67,8 @@ const BATCH_MOVEMENT_KEYS = new Set(["sku", "locationId", "quantityDelta", "reas
|
|
|
67
67
|
const LEVEL_KEYS = new Set(["sku", "locationId", "onHand", "observedAt"]);
|
|
68
68
|
const LOCATION_KEYS = new Set(["name", "kind", "isFulfillable", "status"]);
|
|
69
69
|
const NAME_MAX = 200;
|
|
70
|
+
/** An unknown body key a refusal may name back: identifier-shaped and short. Any other is named `body`. */
|
|
71
|
+
const ECHOABLE_KEY = /^[A-Za-z][A-Za-z0-9_]{0,63}$/;
|
|
70
72
|
/** Fields the platform derives; a body naming any of them is refused, never trusted or ignored. */
|
|
71
73
|
export const SERVER_DERIVED_FIELDS = ["asOf", "source", "sourceEventId", "actorId"] as const;
|
|
72
74
|
|
|
@@ -102,6 +104,13 @@ export interface InventoryRefusal {
|
|
|
102
104
|
error: string;
|
|
103
105
|
/** The batch item the refusal names, when it names one. */
|
|
104
106
|
index?: number;
|
|
107
|
+
/**
|
|
108
|
+
* The request part a 400 names: a body field (`kind`, `quantityDelta`, ...), a server-derived
|
|
109
|
+
* key, an identifier-shaped unknown key as sent, `body` for any other unknown key and for an
|
|
110
|
+
* empty, oversized, non-JSON or non-object body, `items` for the batch array, the path
|
|
111
|
+
* parameter `locationId`, or the header `Idempotency-Key`.
|
|
112
|
+
*/
|
|
113
|
+
field?: string;
|
|
105
114
|
}
|
|
106
115
|
|
|
107
116
|
export interface InventoryReceipt {
|
|
@@ -166,9 +175,17 @@ export interface InventoryWriteActor {
|
|
|
166
175
|
appId: string;
|
|
167
176
|
}
|
|
168
177
|
|
|
169
|
-
export function inventoryRefusal(code: InventoryRefusalCode, index?: number): InventoryWriteResponse<never> {
|
|
178
|
+
export function inventoryRefusal(code: InventoryRefusalCode, index?: number, field?: string): InventoryWriteResponse<never> {
|
|
170
179
|
if (code === "invalid_token") return { status: 401, body: { code, error: "Unauthorized" } };
|
|
171
|
-
return {
|
|
180
|
+
return {
|
|
181
|
+
status: STATUS[code],
|
|
182
|
+
body: {
|
|
183
|
+
code,
|
|
184
|
+
error: field === undefined ? code : `${code}: ${field}`,
|
|
185
|
+
...(index === undefined ? {} : { index }),
|
|
186
|
+
...(field === undefined ? {} : { field }),
|
|
187
|
+
},
|
|
188
|
+
};
|
|
172
189
|
}
|
|
173
190
|
|
|
174
191
|
export function isInventoryRefusal(body: unknown): body is InventoryRefusal {
|
|
@@ -181,15 +198,28 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
|
|
181
198
|
|
|
182
199
|
type ParseRefusal = "malformed" | "server-derived-field";
|
|
183
200
|
|
|
184
|
-
|
|
185
|
-
|
|
201
|
+
/** Why a body is outside its closed schema: the code, the request part it names, and the batch item. */
|
|
202
|
+
export interface ParseFailure {
|
|
203
|
+
refused: ParseRefusal;
|
|
204
|
+
field?: string;
|
|
205
|
+
index?: number;
|
|
186
206
|
}
|
|
187
207
|
|
|
188
|
-
function
|
|
208
|
+
function fail(refused: ParseRefusal, field: string): ParseFailure {
|
|
209
|
+
return { refused, field };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function isParseFailure(value: unknown): value is ParseFailure {
|
|
213
|
+
return isRecord(value) && (value.refused === "malformed" || value.refused === "server-derived-field");
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function closedKeys(body: Record<string, unknown>, allowed: ReadonlySet<string>): ParseFailure | null {
|
|
189
217
|
const keys = Object.keys(body);
|
|
190
|
-
|
|
191
|
-
if (
|
|
192
|
-
|
|
218
|
+
const derived = keys.find((key) => (SERVER_DERIVED_FIELDS as readonly string[]).includes(key));
|
|
219
|
+
if (derived !== undefined) return fail("server-derived-field", derived);
|
|
220
|
+
const unknown = keys.find((key) => !allowed.has(key));
|
|
221
|
+
if (unknown === undefined) return null;
|
|
222
|
+
return fail("malformed", ECHOABLE_KEY.test(unknown) ? unknown : "body");
|
|
193
223
|
}
|
|
194
224
|
|
|
195
225
|
function parseLocation(value: unknown): string | null {
|
|
@@ -201,20 +231,24 @@ function parseReason(value: unknown): MovementReason | null {
|
|
|
201
231
|
return typeof value === "string" && (MOVEMENT_REASONS as readonly string[]).includes(value) ? (value as MovementReason) : null;
|
|
202
232
|
}
|
|
203
233
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
234
|
+
function isDelta(value: unknown): value is number {
|
|
235
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value !== 0;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** The body in the closed schema, or why it is not. */
|
|
239
|
+
export function parseInventoryDelta(body: unknown): AppInventoryDelta | ParseFailure {
|
|
240
|
+
if (!isRecord(body)) return fail("malformed", "body");
|
|
207
241
|
const refused = closedKeys(body, DELTA_KEYS);
|
|
208
242
|
if (refused) return refused;
|
|
209
243
|
const sku = parseSku(body.sku);
|
|
210
|
-
if (!sku.ok) return "malformed";
|
|
244
|
+
if (!sku.ok) return fail("malformed", "sku");
|
|
211
245
|
const delta = body.quantityDelta;
|
|
212
|
-
if (
|
|
246
|
+
if (!isDelta(delta)) return fail("malformed", "quantityDelta");
|
|
213
247
|
const location = parseLocation(body.locationId);
|
|
214
|
-
if (location === null) return "malformed";
|
|
248
|
+
if (location === null) return fail("malformed", "locationId");
|
|
215
249
|
if (body.reason === undefined) return { sku: sku.sku, locationId: location, quantityDelta: delta };
|
|
216
250
|
const reason = parseReason(body.reason);
|
|
217
|
-
if (reason === null) return "malformed";
|
|
251
|
+
if (reason === null) return fail("malformed", "reason");
|
|
218
252
|
return { sku: sku.sku, locationId: location, quantityDelta: delta, reason };
|
|
219
253
|
}
|
|
220
254
|
|
|
@@ -225,78 +259,83 @@ export function isInventoryBatchBody(body: unknown): boolean {
|
|
|
225
259
|
|
|
226
260
|
function parseItems<T>(
|
|
227
261
|
body: unknown,
|
|
228
|
-
parseItem: (item: Record<string, unknown>) => T |
|
|
229
|
-
): { items: T[] } |
|
|
230
|
-
if (!isRecord(body)) return
|
|
262
|
+
parseItem: (item: Record<string, unknown>) => T | ParseFailure,
|
|
263
|
+
): { items: T[] } | ParseFailure {
|
|
264
|
+
if (!isRecord(body)) return fail("malformed", "body");
|
|
231
265
|
const refused = closedKeys(body, new Set(["items"]));
|
|
232
|
-
if (refused) return
|
|
266
|
+
if (refused) return refused;
|
|
233
267
|
const raw = body.items;
|
|
234
|
-
if (!Array.isArray(raw) || raw.length < 1 || raw.length > INVENTORY_BATCH_MAX) return
|
|
268
|
+
if (!Array.isArray(raw) || raw.length < 1 || raw.length > INVENTORY_BATCH_MAX) return fail("malformed", "items");
|
|
235
269
|
const items: T[] = [];
|
|
236
270
|
for (const [index, entry] of raw.entries()) {
|
|
237
|
-
if (!isRecord(entry)) return { refused: "malformed", index };
|
|
271
|
+
if (!isRecord(entry)) return { refused: "malformed", field: "items", index };
|
|
238
272
|
const parsed = parseItem(entry);
|
|
239
|
-
if (
|
|
273
|
+
if (isParseFailure(parsed)) return { ...parsed, index };
|
|
240
274
|
items.push(parsed);
|
|
241
275
|
}
|
|
242
276
|
return { items };
|
|
243
277
|
}
|
|
244
278
|
|
|
245
279
|
/** A movements batch in the closed schema: 1..250 items, each with a WMS reason. */
|
|
246
|
-
export function parseInventoryMovements(body: unknown): { items: AppInventoryMovement[] } |
|
|
280
|
+
export function parseInventoryMovements(body: unknown): { items: AppInventoryMovement[] } | ParseFailure {
|
|
247
281
|
return parseItems(body, (entry) => {
|
|
248
282
|
const refused = closedKeys(entry, BATCH_MOVEMENT_KEYS);
|
|
249
283
|
if (refused) return refused;
|
|
250
284
|
const sku = parseSku(entry.sku);
|
|
285
|
+
if (!sku.ok) return fail("malformed", "sku");
|
|
251
286
|
const delta = entry.quantityDelta;
|
|
287
|
+
if (!isDelta(delta) || Math.abs(delta) > MIRROR_BALANCE_MAX) return fail("malformed", "quantityDelta");
|
|
252
288
|
const location = parseLocation(entry.locationId);
|
|
289
|
+
if (location === null) return fail("malformed", "locationId");
|
|
253
290
|
const reason = parseReason(entry.reason);
|
|
254
|
-
if (
|
|
255
|
-
if (Math.abs(delta) > MIRROR_BALANCE_MAX || location === null || reason === null) return "malformed";
|
|
291
|
+
if (reason === null) return fail("malformed", "reason");
|
|
256
292
|
return { sku: sku.sku, locationId: location, quantityDelta: delta, reason };
|
|
257
293
|
});
|
|
258
294
|
}
|
|
259
295
|
|
|
260
296
|
/** A levels batch in the closed schema: 1..250 absolute on-hand readings, one per (sku, location). */
|
|
261
|
-
export function parseInventoryLevels(
|
|
262
|
-
body: unknown,
|
|
263
|
-
nowMs: number,
|
|
264
|
-
): { items: AppInventoryLevel[] } | { refused: ParseRefusal; index?: number } {
|
|
297
|
+
export function parseInventoryLevels(body: unknown, nowMs: number): { items: AppInventoryLevel[] } | ParseFailure {
|
|
265
298
|
const parsed = parseItems<AppInventoryLevel>(body, (entry) => {
|
|
266
299
|
const refused = closedKeys(entry, LEVEL_KEYS);
|
|
267
300
|
if (refused) return refused;
|
|
268
301
|
const sku = parseSku(entry.sku);
|
|
269
|
-
|
|
302
|
+
if (!sku.ok) return fail("malformed", "sku");
|
|
270
303
|
const location = parseLocation(entry.locationId);
|
|
304
|
+
if (location === null) return fail("malformed", "locationId");
|
|
305
|
+
const onHand = entry.onHand;
|
|
306
|
+
if (typeof onHand !== "number" || !Number.isSafeInteger(onHand) || onHand < 0 || onHand > MIRROR_BALANCE_MAX) {
|
|
307
|
+
return fail("malformed", "onHand");
|
|
308
|
+
}
|
|
271
309
|
const observedAt = entry.observedAt;
|
|
272
|
-
if (
|
|
273
|
-
if (onHand < 0 || onHand > MIRROR_BALANCE_MAX || typeof observedAt !== "string" || observedAt.length > 64) return "malformed";
|
|
310
|
+
if (typeof observedAt !== "string" || observedAt.length > 64) return fail("malformed", "observedAt");
|
|
274
311
|
const observedMs = Date.parse(observedAt);
|
|
275
|
-
if (!Number.isFinite(observedMs) || observedMs > nowMs + OBSERVED_AT_SKEW_MS) return "malformed";
|
|
312
|
+
if (!Number.isFinite(observedMs) || observedMs > nowMs + OBSERVED_AT_SKEW_MS) return fail("malformed", "observedAt");
|
|
276
313
|
return { sku: sku.sku, locationId: location, onHand, observedAt };
|
|
277
314
|
});
|
|
278
315
|
if (!("items" in parsed)) return parsed;
|
|
279
316
|
const seen = new Set<string>();
|
|
280
317
|
for (const [index, item] of parsed.items.entries()) {
|
|
281
318
|
const key = `${skuKey(item.sku)}\n${item.locationId}`;
|
|
282
|
-
if (seen.has(key)) return { refused: "malformed", index };
|
|
319
|
+
if (seen.has(key)) return { refused: "malformed", field: "items", index };
|
|
283
320
|
seen.add(key);
|
|
284
321
|
}
|
|
285
322
|
return parsed;
|
|
286
323
|
}
|
|
287
324
|
|
|
288
|
-
/** A location declaration in the closed schema. */
|
|
289
|
-
export function parseInventoryLocation(body: unknown): AppInventoryLocationInput |
|
|
290
|
-
if (!isRecord(body)) return "malformed";
|
|
325
|
+
/** A location declaration in the closed schema, or why it is not. */
|
|
326
|
+
export function parseInventoryLocation(body: unknown): AppInventoryLocationInput | ParseFailure {
|
|
327
|
+
if (!isRecord(body)) return fail("malformed", "body");
|
|
291
328
|
const refused = closedKeys(body, LOCATION_KEYS);
|
|
292
329
|
if (refused) return refused;
|
|
293
330
|
const { name, kind } = body;
|
|
294
331
|
const isFulfillable = body.isFulfillable ?? true;
|
|
295
332
|
const status = body.status ?? "active";
|
|
296
|
-
if (typeof name !== "string" || name.trim() === "" || name.length > NAME_MAX || /[\u0000-\u001f]/.test(name))
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
if (typeof
|
|
333
|
+
if (typeof name !== "string" || name.trim() === "" || name.length > NAME_MAX || /[\u0000-\u001f]/.test(name)) {
|
|
334
|
+
return fail("malformed", "name");
|
|
335
|
+
}
|
|
336
|
+
if (typeof kind !== "string" || !(LOCATION_KINDS as readonly string[]).includes(kind)) return fail("malformed", "kind");
|
|
337
|
+
if (typeof isFulfillable !== "boolean") return fail("malformed", "isFulfillable");
|
|
338
|
+
if (typeof status !== "string" || !(LOCATION_STATUSES as readonly string[]).includes(status)) return fail("malformed", "status");
|
|
300
339
|
return { name, kind: kind as LocationKind, isFulfillable, status: status as LocationStatus };
|
|
301
340
|
}
|
|
302
341
|
|
|
@@ -408,7 +447,7 @@ export interface InventoryLevelsRequest extends LedgerWriteRequest {
|
|
|
408
447
|
}
|
|
409
448
|
|
|
410
449
|
function preflight(request: LedgerWriteRequest): InventoryWriteResponse<never> | null {
|
|
411
|
-
if (!IDEMPOTENCY_KEY.test(request.idempotencyKey)) return inventoryRefusal("malformed");
|
|
450
|
+
if (!IDEMPOTENCY_KEY.test(request.idempotencyKey)) return inventoryRefusal("malformed", undefined, "Idempotency-Key");
|
|
412
451
|
if (!ID.test(request.actor.installId) || !request.actor.tenantId) return inventoryRefusal("invalid_token");
|
|
413
452
|
return null;
|
|
414
453
|
}
|
|
@@ -732,7 +771,7 @@ export async function applyAppInventoryLevels(
|
|
|
732
771
|
const seen = new Set<string>();
|
|
733
772
|
for (const [index, item] of items.entries()) {
|
|
734
773
|
const pair = `${item.sku}\n${item.locationId}`;
|
|
735
|
-
if (seen.has(pair)) return inventoryRefusal("malformed", index);
|
|
774
|
+
if (seen.has(pair)) return inventoryRefusal("malformed", index, "items");
|
|
736
775
|
seen.add(pair);
|
|
737
776
|
}
|
|
738
777
|
|
|
@@ -802,7 +841,7 @@ export async function upsertAppInventoryLocation(
|
|
|
802
841
|
): Promise<InventoryWriteResponse<InventoryLocation>> {
|
|
803
842
|
const { actor, locationId, location } = request;
|
|
804
843
|
if (!ID.test(actor.installId) || !actor.tenantId) return inventoryRefusal("invalid_token");
|
|
805
|
-
if (!ID.test(locationId)) return inventoryRefusal("malformed");
|
|
844
|
+
if (!ID.test(locationId)) return inventoryRefusal("malformed", undefined, "locationId");
|
|
806
845
|
if (locationId === MIRROR_LOCATION_UNSPECIFIED && location.status !== "active") return inventoryRefusal("location-protected");
|
|
807
846
|
const now = request.now();
|
|
808
847
|
const admission = installAdmissionCondition(actor);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/storefront-runner",
|
|
3
|
-
"version": "6.15.
|
|
3
|
+
"version": "6.15.3",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE",
|
|
5
5
|
"description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
|
|
6
6
|
"packageManager": "pnpm@11.9.0",
|