arcadible 1.0.30 → 1.0.32

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.
@@ -36,6 +36,10 @@ export const DROPPED = [
36
36
  // the code a game is given when its function failed, as on Arcadible: it
37
37
  // threw, or it isn't running.
38
38
  export const FAILED = "function_failed";
39
+ // what marks an answer as Arcadible's own, with its code, as the dispatcher
40
+ // marks its refusals: dropped from what a function's code answers, so a
41
+ // game can't be told a refusal that isn't one.
42
+ export const REFUSAL = "x-arcadible-refusal";
39
43
  // the runtime, fetched the first time a project needs it: workerd is about
40
44
  // 150 MB, and most games have no runner, so it's no dependency of the CLI
41
45
  // (the bundler is: `arc build` bundles a runner too, ../runner/bundle.ts).
@@ -46,15 +50,26 @@ const COMPATIBILITY_DATE = "2026-01-01";
46
50
  const OUTBOUND = join(import.meta.dirname, "..", "runner", "outbound.js");
47
51
  // what stands in front of the function's code in the runtime, as the
48
52
  // dispatcher does on Arcadible: code that throws is the game's
49
- // `function_failed`, and the error is the terminal's, where its developer is.
50
- const FRONT = `export default {
53
+ // `function_failed`, marked as Arcadible's, and the error is the
54
+ // terminal's, where its developer is; what the code answers goes back
55
+ // without the mark.
56
+ const FRONT = `const REFUSAL = ${JSON.stringify(REFUSAL)};
57
+ export default {
51
58
  async fetch(request, env) {
59
+ let answer;
52
60
  try {
53
- return await env.RUNNER.fetch(request);
61
+ answer = await env.RUNNER.fetch(request);
54
62
  } catch (err) {
55
63
  console.error("The function threw:", String(err));
56
- return Response.json({ code: ${JSON.stringify(FAILED)} }, { status: 502 });
64
+ return Response.json(
65
+ { code: ${JSON.stringify(FAILED)} },
66
+ { headers: { [REFUSAL]: ${JSON.stringify(FAILED)} }, status: 502 },
67
+ );
57
68
  }
69
+ if (!answer.headers.has(REFUSAL)) return answer;
70
+ const headers = new Headers(answer.headers);
71
+ headers.delete(REFUSAL);
72
+ return new Response(answer.body, { headers, status: answer.status, statusText: answer.statusText });
58
73
  },
59
74
  };
60
75
  `;
@@ -64,6 +79,8 @@ const READY_MS = 15000;
64
79
  // origin, and a dev server framed with --url is another origin.
65
80
  const CORS = {
66
81
  "access-control-allow-headers": "authorization, content-type",
82
+ // so a game on another origin (--url) reads Arcadible's refusal too
83
+ "access-control-expose-headers": REFUSAL,
67
84
  "access-control-allow-methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
68
85
  "access-control-allow-origin": "*",
69
86
  "access-control-max-age": "600",
@@ -254,7 +271,11 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
254
271
  ? await tickets.check(ticket, origin)
255
272
  : { data: { code: "invalid" }, status: 422 };
256
273
  if (checked.status !== 200) {
257
- res.writeHead(401, { ...CORS, "content-type": "application/json" });
274
+ res.writeHead(401, {
275
+ ...CORS,
276
+ "content-type": "application/json",
277
+ [REFUSAL]: checked.data.code,
278
+ });
258
279
  res.end(JSON.stringify({ code: checked.data.code }));
259
280
  return;
260
281
  }
@@ -262,7 +283,11 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
262
283
  // is the terminal's, said as it happened
263
284
  const fail = function () {
264
285
  if (!res.headersSent) {
265
- res.writeHead(502, { ...CORS, "content-type": "application/json" });
286
+ res.writeHead(502, {
287
+ ...CORS,
288
+ "content-type": "application/json",
289
+ [REFUSAL]: FAILED,
290
+ });
266
291
  }
267
292
  res.end(JSON.stringify({ code: FAILED }));
268
293
  };
@@ -56,17 +56,38 @@ export function sizeOf(value) {
56
56
  return Buffer.byteLength(text(value));
57
57
  }
58
58
  // [function]
59
- // a call's keys, or null: at least one, no more than a call takes, each a
60
- // key, none twice.
61
- function keysOf(keys) {
62
- if (!Array.isArray(keys) ||
63
- keys.length < 1 ||
64
- keys.length > LIMITS.callKeys ||
65
- !keys.every((key) => typeof key === "string" && KEY.test(key)) ||
66
- new Set(keys).size !== keys.length) {
59
+ // a whole number a revision may be: 0 or more.
60
+ function isRevision(value) {
61
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
62
+ }
63
+ // [function]
64
+ // a call's items, or null: 1 to a call's keys, each an object with a scope,
65
+ // an owner, and a key, none named twice, and each of the verb's own fields
66
+ // (`fields`) a revision where it's given; `value`, for a write, there.
67
+ function itemsOf(given, fields, needsValue) {
68
+ if (!Array.isArray(given) ||
69
+ given.length < 1 ||
70
+ given.length > LIMITS.callKeys) {
67
71
  return null;
68
72
  }
69
- return keys;
73
+ const seen = new Set();
74
+ for (const item of given) {
75
+ if (!isTable(item) ||
76
+ !SCOPES.includes(item["scope"]) ||
77
+ typeof item["owner"] !== "string" ||
78
+ typeof item["key"] !== "string" ||
79
+ !KEY.test(item["key"]) ||
80
+ (needsValue && !Object.hasOwn(item, "value")) ||
81
+ fields.some((field) => item[field] !== undefined && !isRevision(item[field]))) {
82
+ return null;
83
+ }
84
+ const name = `${String(item["scope"])}:${item["owner"]}:${item["key"]}`;
85
+ if (seen.has(name)) {
86
+ return null;
87
+ }
88
+ seen.add(name);
89
+ }
90
+ return given;
70
91
  }
71
92
  // [function]
72
93
  // the developer's values, from the project's folder: each `.json` file a
@@ -164,7 +185,137 @@ export function values({ isPlayer, now = () => Date.now(), root, }) {
164
185
  const refusal = function (scope, owner) {
165
186
  return scope === "player" && !isPlayer(owner) ? 403 : null;
166
187
  };
188
+ // the first refusal of the items' owners, as the database checks each
189
+ const refusalOf = function (items) {
190
+ return items.some((item) => refusal(item.scope, item.owner)) ? 403 : null;
191
+ };
192
+ const rowOf = function (item) {
193
+ return held(item.scope, item.owner).get(item.key);
194
+ };
195
+ const readItems = function (given) {
196
+ const items = itemsOf(given, ["since"], false);
197
+ if (!items) {
198
+ return { data: null, status: 400 };
199
+ }
200
+ const refused = refusalOf(items);
201
+ if (refused) {
202
+ return { data: null, status: refused };
203
+ }
204
+ const rows = items.map(rowOf);
205
+ // what's read together, the unchanged left out
206
+ if (rows.reduce((sum, row, at) => sum + (row && row.revision !== items[at]?.since ? row.size : 0), 0) > LIMITS.readSize) {
207
+ return { data: null, status: 413 };
208
+ }
209
+ return {
210
+ data: items.map((item, at) => {
211
+ const row = rows[at];
212
+ if (!row) {
213
+ return null;
214
+ }
215
+ const unchanged = row.revision === item.since;
216
+ return {
217
+ key: row.key,
218
+ owner: item.owner,
219
+ revision: row.revision,
220
+ scope: item.scope,
221
+ unchanged,
222
+ updated_at: row.updated_at,
223
+ ...(unchanged ? {} : { value: row.value }),
224
+ };
225
+ }),
226
+ status: 200,
227
+ };
228
+ };
229
+ const writeItems = function (given) {
230
+ const items = itemsOf(given, ["revision"], true);
231
+ if (!items) {
232
+ return { data: null, status: 400 };
233
+ }
234
+ const refused = refusalOf(items);
235
+ if (refused) {
236
+ return { data: null, status: refused };
237
+ }
238
+ const sizes = items.map((item) => sizeOf(item.value));
239
+ if (sizes.some((size) => size > LIMITS.size) ||
240
+ sizes.reduce((sum, size) => sum + size, 0) > LIMITS.callSize) {
241
+ return { data: null, status: 413 };
242
+ }
243
+ // every named revision first, so a call writes all or none
244
+ for (const item of items) {
245
+ if (item.revision !== undefined &&
246
+ item.revision !== (rowOf(item)?.revision ?? 0)) {
247
+ return { data: null, status: 409 };
248
+ }
249
+ }
250
+ const next = { ...kept.rows };
251
+ let revision = kept.revision;
252
+ const at = new Date(now()).toISOString();
253
+ const written = items.map((item, index) => {
254
+ const name = `${item.scope}:${item.owner}`;
255
+ const rows = { ...(next[name] ?? {}) };
256
+ const key = item.key;
257
+ revision += 1;
258
+ rows[key] = {
259
+ key,
260
+ revision,
261
+ size: sizes[index] ?? 0,
262
+ updated_at: at,
263
+ value: item.value,
264
+ };
265
+ next[name] = rows;
266
+ return {
267
+ key,
268
+ owner: item.owner,
269
+ revision,
270
+ scope: item.scope,
271
+ updated_at: at,
272
+ };
273
+ });
274
+ // each scope it wrote, held to its limits
275
+ for (const item of items) {
276
+ const all = Object.values(next[`${item.scope}:${item.owner}`] ?? {});
277
+ const limits = item.scope === "player" ? "player" : "other";
278
+ if (all.length > LIMITS.keys[limits] ||
279
+ all.reduce((sum, row) => sum + row.size, 0) > LIMITS.bytes[limits]) {
280
+ return { data: null, status: 507 };
281
+ }
282
+ }
283
+ kept = { revision, rows: next };
284
+ save();
285
+ return { data: written, status: 200 };
286
+ };
287
+ const removeItems = function (given) {
288
+ const items = itemsOf(given, ["revision"], false);
289
+ if (!items) {
290
+ return { data: null, status: 400 };
291
+ }
292
+ const refused = refusalOf(items);
293
+ if (refused) {
294
+ return { data: null, status: refused };
295
+ }
296
+ for (const item of items) {
297
+ if (item.revision !== undefined &&
298
+ item.revision !== (rowOf(item)?.revision ?? 0)) {
299
+ return { data: null, status: 409 };
300
+ }
301
+ }
302
+ let removed = 0;
303
+ for (const item of items) {
304
+ const rows = kept.rows[`${item.scope}:${item.owner}`] ?? {};
305
+ if (Object.hasOwn(rows, item.key)) {
306
+ Reflect.deleteProperty(rows, item.key);
307
+ removed += 1;
308
+ }
309
+ }
310
+ if (removed) {
311
+ save();
312
+ }
313
+ return { data: { removed }, status: 200 };
314
+ };
167
315
  return {
316
+ readItems,
317
+ removeItems,
318
+ writeItems,
168
319
  list(scope, owner, { after, limit, prefix }) {
169
320
  const refused = refusal(scope, owner);
170
321
  if (refused) {
@@ -207,111 +358,5 @@ export function values({ isPlayer, now = () => Date.now(), root, }) {
207
358
  status: 200,
208
359
  };
209
360
  },
210
- read(scope, owner, asked) {
211
- const refused = refusal(scope, owner);
212
- if (refused) {
213
- return { data: null, status: refused };
214
- }
215
- const keys = keysOf(asked);
216
- if (!keys) {
217
- return { data: null, status: 400 };
218
- }
219
- const rows = held(scope, owner);
220
- // more than a read may hold together is the caller's to ask for less
221
- if (keys.reduce((sum, key) => sum + (rows.get(key)?.size ?? 0), 0) >
222
- LIMITS.readSize) {
223
- return { data: null, status: 413 };
224
- }
225
- return {
226
- data: keys.map((key) => {
227
- const row = rows.get(key);
228
- return row
229
- ? {
230
- key,
231
- revision: row.revision,
232
- updated_at: row.updated_at,
233
- value: row.value,
234
- }
235
- : null;
236
- }),
237
- status: 200,
238
- };
239
- },
240
- remove(scope, owner, asked) {
241
- const refused = refusal(scope, owner);
242
- if (refused) {
243
- return { data: null, status: refused };
244
- }
245
- const keys = keysOf(asked);
246
- if (!keys) {
247
- return { data: null, status: 400 };
248
- }
249
- const rows = kept.rows[`${scope}:${owner}`] ?? {};
250
- const there = keys.filter((key) => Object.hasOwn(rows, key));
251
- for (const key of there) {
252
- Reflect.deleteProperty(rows, key);
253
- }
254
- if (there.length) {
255
- save();
256
- }
257
- return { data: { removed: there.length }, status: 200 };
258
- },
259
- write(scope, owner, given) {
260
- const refused = refusal(scope, owner);
261
- if (refused) {
262
- return { data: null, status: refused };
263
- }
264
- if (!Array.isArray(given) ||
265
- !given.every((item) => isTable(item) &&
266
- Object.hasOwn(item, "value") &&
267
- (item["revision"] === undefined ||
268
- (typeof item["revision"] === "number" &&
269
- Number.isSafeInteger(item["revision"]) &&
270
- item["revision"] >= 0)))) {
271
- return { data: null, status: 400 };
272
- }
273
- const items = given;
274
- const keys = keysOf(items.map((item) => item.key));
275
- if (!keys) {
276
- return { data: null, status: 400 };
277
- }
278
- const sizes = items.map((item) => sizeOf(item.value));
279
- if (sizes.some((size) => size > LIMITS.size) ||
280
- sizes.reduce((sum, size) => sum + size, 0) > LIMITS.callSize) {
281
- return { data: null, status: 413 };
282
- }
283
- const name = `${scope}:${owner}`;
284
- const rows = { ...(kept.rows[name] ?? {}) };
285
- // every named revision first, so a call writes all or none
286
- for (const [at, item] of items.entries()) {
287
- const revision = rows[keys[at] ?? ""]?.revision ?? 0;
288
- if (item.revision !== undefined && item.revision !== revision) {
289
- return { data: null, status: 409 };
290
- }
291
- }
292
- let revision = kept.revision;
293
- const at = new Date(now()).toISOString();
294
- const written = items.map((item, index) => {
295
- const key = keys[index] ?? "";
296
- revision += 1;
297
- rows[key] = {
298
- key,
299
- revision,
300
- size: sizes[index] ?? 0,
301
- updated_at: at,
302
- value: item.value,
303
- };
304
- return { key, revision, updated_at: at };
305
- });
306
- const limits = scope === "player" ? "player" : "other";
307
- const all = Object.values(rows);
308
- if (all.length > LIMITS.keys[limits] ||
309
- all.reduce((sum, row) => sum + row.size, 0) > LIMITS.bytes[limits]) {
310
- return { data: null, status: 507 };
311
- }
312
- kept = { revision, rows: { ...kept.rows, [name]: rows } };
313
- save();
314
- return { data: written, status: 200 };
315
- },
316
361
  };
317
362
  }