@northtalk/ts 0.0.0-stage → 0.2.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 (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +59 -2
  3. package/UNICODE-LICENSE.txt +39 -0
  4. package/base64-DORMd3Hv.js +6621 -0
  5. package/binary-value-CEmh60GC.d.ts +15 -0
  6. package/capabilities-CH0FkRMq.js +1301 -0
  7. package/checker-Dc1jl2WH.js +6657 -0
  8. package/core-DptnvQT9.d.ts +14 -0
  9. package/debug-CQBxKnJB.d.ts +3065 -0
  10. package/debug.d.ts +3 -0
  11. package/debug.js +2 -0
  12. package/driver.d.ts +41 -0
  13. package/driver.js +152 -0
  14. package/encoding-nlrpj-Pb.js +99 -0
  15. package/engine-BGaJ9dAk.js +326 -0
  16. package/engine-vbYInVYF.d.ts +86 -0
  17. package/errors-LYJZ_c8h.js +3735 -0
  18. package/group-B742gg4T.js +10170 -0
  19. package/http-capability-CSTkw1Yt.js +728 -0
  20. package/http.d.ts +59 -0
  21. package/http.js +465 -0
  22. package/index.d.ts +651 -0
  23. package/index.js +44 -0
  24. package/manifest-BscyH_rW.d.ts +70 -0
  25. package/manifest-DjsxDSuB.js +129 -0
  26. package/message-layer.d.ts +12 -0
  27. package/message-layer.js +4 -0
  28. package/notify.d.ts +3 -0
  29. package/notify.js +2 -0
  30. package/objects-DJ3QtCCs.d.ts +640 -0
  31. package/objects-Db7IB5vQ.js +91 -0
  32. package/operation-promises-EgZXG5N_.js +23 -0
  33. package/package.json +70 -4
  34. package/readers-BFPuosPk.d.ts +25 -0
  35. package/readers-CjJHKHvH.js +482 -0
  36. package/request-pump-binary-DdY3X-yl.js +2246 -0
  37. package/router--71qvxtY.js +343 -0
  38. package/router-Zhrw5l__.d.ts +55 -0
  39. package/session.d.ts +9 -0
  40. package/session.js +3510 -0
  41. package/setup.d.ts +3 -0
  42. package/setup.js +193 -0
  43. package/sha256-B_kyYw3Z.js +124 -0
  44. package/sqlite.d.ts +74 -0
  45. package/sqlite.js +529 -0
  46. package/store-capability-OZp4hXmj.js +390 -0
  47. package/store-hJrRULMx.js +60 -0
  48. package/store.d.ts +21 -0
  49. package/store.js +4 -0
  50. package/user-capability-BK4Asg7D.js +276 -0
  51. package/wasm.d.ts +243 -0
  52. package/wasm.js +1737 -0
@@ -0,0 +1,390 @@
1
+
2
+ import { K as text, M as map, P as nothing, i as ScriptError, t as HostError, u as bool, w as instant } from "./errors-LYJZ_c8h.js";
3
+ import { a as defineValueCapability, f as validComputedMessageName, u as shape } from "./capabilities-CH0FkRMq.js";
4
+ import { i as ScriptError$1, lt as errorMessages, o as arithmetic } from "./base64-DORMd3Hv.js";
5
+ import { r as registerStandardChecks } from "./encoding-nlrpj-Pb.js";
6
+ //#region src/standard-capabilities.ts
7
+ const costOf = (costs, name) => {
8
+ const cost = costs && Object.hasOwn(costs, name) ? costs[name] : void 0;
9
+ if (!cost || !Number.isSafeInteger(cost.fuel) || cost.fuel < 0 || cost.alloc !== void 0 && (!Number.isSafeInteger(cost.alloc) || cost.alloc < 0)) throw new HostError("invalid value", `Invalid or missing cost for ${name}`);
10
+ return Object.freeze({
11
+ fuel: cost.fuel,
12
+ ...cost.alloc === void 0 ? {} : { alloc: cost.alloc }
13
+ });
14
+ };
15
+ const fixed = (capability) => {
16
+ for (const op of capability.operations.values()) {
17
+ Object.freeze(op.args);
18
+ Object.freeze(op.errors);
19
+ Object.freeze(op);
20
+ }
21
+ return capability;
22
+ };
23
+ const instantShape = Object.freeze({
24
+ k: "kind",
25
+ kind: "instant"
26
+ });
27
+ const textShape$1 = Object.freeze({
28
+ k: "kind",
29
+ kind: "text"
30
+ });
31
+ const valueShape = Object.freeze({ k: "value" });
32
+ const dataListShape = Object.freeze({
33
+ k: "list",
34
+ of: Object.freeze({ k: "any" })
35
+ });
36
+ /** The Pump's Clock reading; there is no Host clock implementation. */
37
+ const clockCapability = (costs) => fixed(defineValueCapability("clock", { now: {
38
+ mode: "immediate",
39
+ args: [],
40
+ result: instantShape,
41
+ errors: [],
42
+ cost: costOf(costs, "now"),
43
+ do: (call) => instant(call.now)
44
+ } }));
45
+ /** The Host owns durable timers, scoped by Script and name. */
46
+ const timerCapability = (impl, costs) => {
47
+ if (!impl || typeof impl.schedule !== "function" || typeof impl.cancel !== "function") throw new HostError("invalid value", "Timer needs Schedule and Cancel");
48
+ return fixed(defineValueCapability("timer", {
49
+ schedule: {
50
+ mode: "fire-and-forget",
51
+ args: [
52
+ textShape$1,
53
+ instantShape,
54
+ textShape$1,
55
+ dataListShape
56
+ ],
57
+ errors: [],
58
+ cost: costOf(costs, "schedule"),
59
+ fire: (call, name, at, message, args) => impl.schedule(call, name.asText(), at, message.asText(), args)
60
+ },
61
+ cancel: {
62
+ mode: "fire-and-forget",
63
+ args: [textShape$1],
64
+ errors: [],
65
+ cost: costOf(costs, "cancel"),
66
+ fire: (call, name) => impl.cancel(call, name.asText())
67
+ }
68
+ }));
69
+ };
70
+ /** The Host shows Values in text form and answers read with a line of text. */
71
+ const consoleCapability = (impl, costs) => {
72
+ if (!impl || typeof impl.write !== "function" || typeof impl.read !== "function") throw new HostError("invalid value", "Console needs Write and Read");
73
+ return fixed(defineValueCapability("console", {
74
+ write: {
75
+ mode: "fire-and-forget",
76
+ args: [valueShape],
77
+ errors: [],
78
+ cost: costOf(costs, "write"),
79
+ fire: (call, value) => impl.write(call, value)
80
+ },
81
+ read: {
82
+ mode: "suspending",
83
+ args: [],
84
+ result: textShape$1,
85
+ errors: [],
86
+ maxPendingMs: 2147483647,
87
+ cost: costOf(costs, "read"),
88
+ start: (call) => impl.read(call)
89
+ }
90
+ }));
91
+ };
92
+ const civilShape = Object.freeze({
93
+ k: "kind",
94
+ kind: "civil date"
95
+ });
96
+ const optionalTextShape = Object.freeze({
97
+ k: "optional",
98
+ of: textShape$1
99
+ });
100
+ const secondsShape = Object.freeze({
101
+ k: "quantity",
102
+ unit: "s"
103
+ });
104
+ const disambiguations = /* @__PURE__ */ new Set([
105
+ "compatible",
106
+ "earlier",
107
+ "later",
108
+ "reject"
109
+ ]);
110
+ const zoneText = (value) => value?.asText();
111
+ const calendarError = (code, data) => data.get("zone").kind === "text" && (code === "unknown zone" || code === "ambiguous time" && data.get("civil").kind === "civil date");
112
+ const checkToInstant = (args) => {
113
+ const civil = args[0];
114
+ const disambiguation = zoneText(args[1]);
115
+ const bad = !("hour" in civil.asCivilDate()) ? civil : args.length === 3 && disambiguation !== void 0 && !disambiguations.has(disambiguation) ? args[1] : void 0;
116
+ if (bad) throw new ScriptError$1("out of domain", [["function", text("toInstant")], ["value", bad]], true);
117
+ };
118
+ /** The Host owns zone data; the Grant binding supplies its default zone. */
119
+ const calendarCapability = (impl, costs) => {
120
+ for (const name of [
121
+ "today",
122
+ "now",
123
+ "toCivil",
124
+ "toInstant",
125
+ "offset",
126
+ "zone"
127
+ ]) if (!impl || typeof impl[name] !== "function") throw new HostError("invalid value", `Calendar needs ${name}`);
128
+ const unknownZone = [{
129
+ code: "unknown zone",
130
+ fields: { zone: textShape$1 }
131
+ }];
132
+ const operations = {
133
+ today: {
134
+ mode: "immediate",
135
+ args: [optionalTextShape],
136
+ result: civilShape,
137
+ errors: unknownZone,
138
+ cost: costOf(costs, "today"),
139
+ do: (call, zone) => impl.today(call, zoneText(zone))
140
+ },
141
+ now: {
142
+ mode: "immediate",
143
+ args: [optionalTextShape],
144
+ result: civilShape,
145
+ errors: unknownZone,
146
+ cost: costOf(costs, "now"),
147
+ do: (call, zone) => impl.now(call, zoneText(zone))
148
+ },
149
+ toCivil: {
150
+ mode: "immediate",
151
+ args: [instantShape, optionalTextShape],
152
+ result: civilShape,
153
+ errors: unknownZone,
154
+ cost: costOf(costs, "toCivil"),
155
+ do: (call, value, zone) => impl.toCivil(call, value, zoneText(zone))
156
+ },
157
+ toInstant: {
158
+ mode: "immediate",
159
+ args: [
160
+ civilShape,
161
+ optionalTextShape,
162
+ optionalTextShape
163
+ ],
164
+ result: instantShape,
165
+ errors: [...unknownZone, {
166
+ code: "ambiguous time",
167
+ fields: {
168
+ civil: civilShape,
169
+ zone: textShape$1
170
+ }
171
+ }],
172
+ cost: costOf(costs, "toInstant"),
173
+ do: (call, ...args) => {
174
+ const second = zoneText(args[1]);
175
+ const isZone = args.length === 2 && second !== void 0 && !disambiguations.has(second);
176
+ return impl.toInstant(call, args[0], isZone ? "compatible" : second ?? "compatible", isZone ? second : zoneText(args[2]));
177
+ }
178
+ },
179
+ offset: {
180
+ mode: "immediate",
181
+ args: [instantShape, optionalTextShape],
182
+ result: secondsShape,
183
+ errors: unknownZone,
184
+ cost: costOf(costs, "offset"),
185
+ do: (call, value, zone) => impl.offset(call, value, zoneText(zone))
186
+ },
187
+ zone: {
188
+ mode: "immediate",
189
+ args: [optionalTextShape],
190
+ result: textShape$1,
191
+ errors: unknownZone,
192
+ cost: costOf(costs, "zone"),
193
+ do: (call, zone) => impl.zone(call, zoneText(zone))
194
+ }
195
+ };
196
+ for (const [name, op] of Object.entries(operations)) registerStandardChecks(op, {
197
+ error: calendarError,
198
+ ...name === "toInstant" ? { arguments: checkToInstant } : {},
199
+ ...[
200
+ "today",
201
+ "now",
202
+ "toCivil"
203
+ ].includes(name) ? { result: (value) => !("hour" in value.asCivilDate()) === (name === "today") } : {}
204
+ });
205
+ return fixed(defineValueCapability("calendar", operations));
206
+ };
207
+ //#endregion
208
+ //#region src/store-capability.ts
209
+ /** A Core-raised error as the Host sees it, with its catalogue message. */
210
+ const hostScriptError = (code, fields) => {
211
+ const message = (errorMessages[code] ?? code).replaceAll(/{(\w+)}/g, (_, name) => {
212
+ const field = fields.find(([k]) => k === name);
213
+ return field ? field[1].toString() : `{${name}}`;
214
+ });
215
+ return new ScriptError(code, message, map(fields.map(([k, v]) => [k, v])));
216
+ };
217
+ /** The `+` rules outside any Run, uncharged (chapter 9, Adding). */
218
+ const add = (a, b) => {
219
+ try {
220
+ return arithmetic("add", a, b);
221
+ } catch (error) {
222
+ throw error instanceof ScriptError$1 ? hostScriptError(error.code, error.fields) : error;
223
+ }
224
+ };
225
+ const textShape = shape.text;
226
+ const keyShape = textShape;
227
+ const amountShape = shape.oneOf(shape.number, {
228
+ k: "kind",
229
+ kind: "quantity"
230
+ });
231
+ const OPERATIONS = [
232
+ "get",
233
+ "set",
234
+ "delete",
235
+ "keys",
236
+ "increment",
237
+ "swap",
238
+ "watch",
239
+ "unwatch"
240
+ ];
241
+ const isText = (data, field) => data.get(field).kind === "text";
242
+ const errorFields = {
243
+ "can't store": (data) => isText(data, "kind"),
244
+ "store full": (data) => [
245
+ "size",
246
+ "keys",
247
+ "value"
248
+ ].includes(data.get("limit").asText() ?? ""),
249
+ "store busy": (data) => isText(data, "key"),
250
+ "watch limit": (data) => [
251
+ "watches",
252
+ "size",
253
+ "pending"
254
+ ].includes(data.get("limit").asText() ?? ""),
255
+ "wrong kind": (data) => isText(data, "expected") && isText(data, "got") && data.entries().some(([k]) => k === "value"),
256
+ "incompatible units": (data) => isText(data, "left") && isText(data, "right"),
257
+ overflow: (data) => isText(data, "operator")
258
+ };
259
+ const checkKey = (args) => {
260
+ if (args[0].asText() === "") throw new ScriptError$1("invalid key", [], true);
261
+ };
262
+ const effectiveMessage = (args) => args[2]?.asText() ?? "storeChanged";
263
+ const checkUnwatch = (args) => {
264
+ if (args[0].asText() === "") throw new ScriptError$1("invalid watch", [["field", text("name")]], true);
265
+ };
266
+ const checkWatch = (args) => {
267
+ checkUnwatch(args);
268
+ const message = effectiveMessage(args);
269
+ if (message.includes(":") || !validComputedMessageName(message, 1)) throw new ScriptError$1("invalid watch", [["field", text("message")]], true);
270
+ };
271
+ const argumentChecks = {
272
+ watch: checkWatch,
273
+ unwatch: checkUnwatch
274
+ };
275
+ const errors = (...codes) => codes.map((code) => ({ code }));
276
+ const writes = [
277
+ "can't store",
278
+ "store full",
279
+ "store busy"
280
+ ];
281
+ const key = (args) => args[0].asText();
282
+ /** The Store is the Host's; the Grant binding names it, and `impl` coordinates every binding. */
283
+ const storeCapability = (impl, costs) => {
284
+ for (const name of [
285
+ ...OPERATIONS,
286
+ "begin",
287
+ "commit",
288
+ "rollback",
289
+ "release"
290
+ ]) if (!impl || typeof impl[name] !== "function") throw new HostError("invalid value", `Store needs ${name}`);
291
+ const operations = {
292
+ watch: {
293
+ mode: "immediate",
294
+ args: [
295
+ textShape,
296
+ textShape,
297
+ shape.optional(textShape)
298
+ ],
299
+ result: shape.nothing,
300
+ errors: errors("watch limit"),
301
+ cost: costOf(costs, "watch"),
302
+ do: (call, ...args) => {
303
+ impl.watch(call, key(args), args[1].asText(), effectiveMessage(args));
304
+ return nothing;
305
+ }
306
+ },
307
+ unwatch: {
308
+ mode: "immediate",
309
+ args: [textShape],
310
+ result: shape.nothing,
311
+ errors: [],
312
+ cost: costOf(costs, "unwatch"),
313
+ do: (call, ...args) => {
314
+ impl.unwatch(call, key(args));
315
+ return nothing;
316
+ }
317
+ },
318
+ get: {
319
+ mode: "immediate",
320
+ args: [keyShape, shape.optional(shape.any)],
321
+ result: shape.any,
322
+ errors: [],
323
+ cost: costOf(costs, "get"),
324
+ do: (call, ...args) => impl.get(call, key(args), args[1])
325
+ },
326
+ set: {
327
+ mode: "immediate",
328
+ args: [keyShape, shape.any],
329
+ result: shape.nothing,
330
+ errors: errors(...writes),
331
+ cost: costOf(costs, "set"),
332
+ segmentBound: true,
333
+ do: (call, ...args) => {
334
+ impl.set(call, key(args), args[1]);
335
+ return nothing;
336
+ }
337
+ },
338
+ delete: {
339
+ mode: "immediate",
340
+ args: [keyShape],
341
+ result: shape.nothing,
342
+ errors: errors("store busy"),
343
+ cost: costOf(costs, "delete"),
344
+ segmentBound: true,
345
+ do: (call, ...args) => {
346
+ impl.delete(call, key(args));
347
+ return nothing;
348
+ }
349
+ },
350
+ keys: {
351
+ mode: "immediate",
352
+ args: [shape.optional(textShape)],
353
+ result: shape.listOf(textShape),
354
+ errors: [],
355
+ cost: costOf(costs, "keys"),
356
+ do: (call, prefix) => impl.keys(call, prefix?.asText())
357
+ },
358
+ increment: {
359
+ mode: "immediate",
360
+ args: [keyShape, shape.optional(amountShape)],
361
+ result: amountShape,
362
+ errors: errors("wrong kind", "incompatible units", "overflow", "store full", "store busy"),
363
+ cost: costOf(costs, "increment"),
364
+ segmentBound: true,
365
+ do: (call, ...args) => impl.increment(call, key(args), args[1])
366
+ },
367
+ swap: {
368
+ mode: "immediate",
369
+ args: [
370
+ keyShape,
371
+ shape.any,
372
+ shape.any
373
+ ],
374
+ result: shape.bool,
375
+ errors: errors(...writes),
376
+ cost: costOf(costs, "swap"),
377
+ segmentBound: true,
378
+ do: (call, ...args) => bool(impl.swap(call, key(args), args[1], args[2]))
379
+ }
380
+ };
381
+ const coordinator = impl;
382
+ const capability = defineValueCapability("store", operations, { coordinator: () => coordinator }, (context) => impl.release(context));
383
+ for (const [name, op] of capability.operations) registerStandardChecks(op, {
384
+ error: (code, data) => Boolean(op.errors?.some((e) => e.code === code) && errorFields[code]?.(data)),
385
+ ...name === "keys" ? {} : { arguments: argumentChecks[name] ?? checkKey }
386
+ });
387
+ return fixed(capability);
388
+ };
389
+ //#endregion
390
+ export { clockCapability as a, fixed as c, calendarCapability as i, timerCapability as l, hostScriptError as n, consoleCapability as o, storeCapability as r, costOf as s, add as t };
@@ -0,0 +1,60 @@
1
+
2
+ import { st as compareText } from "./base64-DORMd3Hv.js";
3
+ import { a as encodeContents, i as decodeContents, n as StoreStateUnknownError, r as Stores, s as sessionWatchLimits } from "./engine-BGaJ9dAk.js";
4
+ //#region src/store/memory.ts
5
+ /** A backend that keeps nothing outside the engine's own copy. */
6
+ const memoryBackend = () => ({
7
+ load: () => [],
8
+ save: () => {}
9
+ });
10
+ /**
11
+ * Stores kept only in memory, each starting empty. `admitted` is told after
12
+ * each Notification a Group admits.
13
+ */
14
+ const memoryStores = (quotas, admitted) => new Stores(memoryBackend(), quotas, sessionWatchLimits, admitted);
15
+ //#endregion
16
+ //#region src/store/web-storage.ts
17
+ /** Each Store under the item `prefix` + its name. */
18
+ const webStorageBackend = (storage, prefix = "northtalk.store.") => {
19
+ const contents = /* @__PURE__ */ new Map();
20
+ return {
21
+ load: (store) => {
22
+ const item = storage.getItem(prefix + store);
23
+ const entries = new Map(item === null ? [] : decodeContents(item));
24
+ contents.set(store, entries);
25
+ return entries;
26
+ },
27
+ save: (changes) => {
28
+ const written = [];
29
+ try {
30
+ for (const [store, values] of changes) {
31
+ const next = new Map(contents.get(store));
32
+ for (const [key, value] of values) if (value.kind === "nothing") next.delete(key);
33
+ else next.set(key, value);
34
+ const item = prefix + store;
35
+ const old = storage.getItem(item);
36
+ if (next.size) storage.setItem(item, encodeContents([...next].sort(([a], [b]) => compareText(a, b))));
37
+ else storage.removeItem(item);
38
+ written.push([
39
+ store,
40
+ next,
41
+ old
42
+ ]);
43
+ }
44
+ } catch (error) {
45
+ try {
46
+ for (const [store, , old] of written.reverse()) if (old === null) storage.removeItem(prefix + store);
47
+ else storage.setItem(prefix + store, old);
48
+ } catch {
49
+ throw new StoreStateUnknownError(error.message);
50
+ }
51
+ throw error;
52
+ }
53
+ for (const [store, next] of written) contents.set(store, next);
54
+ }
55
+ };
56
+ };
57
+ /** Stores kept in Web Storage, under items named `prefix` + the Store's name. */
58
+ const webStorageStores = (storage, quotas, prefix) => new Stores(webStorageBackend(storage, prefix), quotas);
59
+ //#endregion
60
+ export { memoryStores as i, webStorageStores as n, memoryBackend as r, webStorageBackend as t };
package/store.d.ts ADDED
@@ -0,0 +1,21 @@
1
+
2
+ import { n as RouterOptions } from "./router-Zhrw5l__.js";
3
+ import { a as Stores, c as sessionQuotas, i as StoreStateUnknownError, l as sessionWatchLimits, n as StoreContentsError, o as decodeContents, r as StoreQuotas, s as encodeContents, t as StoreBackend } from "./engine-vbYInVYF.js";
4
+ //#region src/store/memory.d.ts
5
+ /** A backend that keeps nothing outside the engine's own copy. */
6
+ export declare const memoryBackend: () => StoreBackend;
7
+ /**
8
+ * Stores kept only in memory, each starting empty. `admitted` is told after
9
+ * each Notification a Group admits.
10
+ */
11
+ export declare const memoryStores: (quotas: StoreQuotas, admitted?: RouterOptions["admitted"]) => Stores;
12
+ //#endregion
13
+ //#region src/store/web-storage.d.ts
14
+ /** The part of the Web Storage `Storage` interface a Store uses. */
15
+ type WebStorage = Pick<Storage, 'getItem' | 'removeItem' | 'setItem'>;
16
+ /** Each Store under the item `prefix` + its name. */
17
+ export declare const webStorageBackend: (storage: WebStorage, prefix?: string) => StoreBackend;
18
+ /** Stores kept in Web Storage, under items named `prefix` + the Store's name. */
19
+ export declare const webStorageStores: (storage: WebStorage, quotas: StoreQuotas, prefix?: string) => Stores;
20
+ //#endregion
21
+ export { type StoreBackend, StoreContentsError, type StoreQuotas, StoreStateUnknownError, Stores, type WebStorage, decodeContents, encodeContents, sessionQuotas, sessionWatchLimits };
package/store.js ADDED
@@ -0,0 +1,4 @@
1
+
2
+ import { a as encodeContents, i as decodeContents, n as StoreStateUnknownError, o as sessionQuotas, r as Stores, s as sessionWatchLimits, t as StoreContentsError } from "./engine-BGaJ9dAk.js";
3
+ import { i as memoryStores, n as webStorageStores, r as memoryBackend, t as webStorageBackend } from "./store-hJrRULMx.js";
4
+ export { StoreContentsError, StoreStateUnknownError, Stores, decodeContents, encodeContents, memoryBackend, memoryStores, sessionQuotas, sessionWatchLimits, webStorageBackend, webStorageStores };