@northtalk/ts 0.0.0-stage → 0.1.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/setup.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { A as shapeOf, C as Setup, D as capabilityOperations, E as buildSetupGroup, M as tsCore, O as immutableSetup, S as OperationSpec, T as ShapeSpec, b as MissingCapabilityError, j as shapeSpec, k as operationDeclarations, w as SetupGroup, x as ObjectRefSpec, y as CaseCore } from "./objects-MeXIDDFo.js";
2
+ export { CaseCore, MissingCapabilityError, ObjectRefSpec, OperationSpec, Setup, SetupGroup, ShapeSpec, buildSetupGroup, capabilityOperations, immutableSetup, operationDeclarations, shapeOf, shapeSpec, tsCore };
package/setup.js ADDED
@@ -0,0 +1,189 @@
1
+ import { n as LoadError } from "./errors-DHwz1Vho.js";
2
+ import { o as defineObjectKind } from "./manifest-DKe2Je6Z.js";
3
+ import { xt as shape } from "./encoding-DWlbPq-e.js";
4
+ import { f as compileLibrary, p as replacementLibraries, r as newGroup, s as restore } from "./group-B4eyJKTA.js";
5
+ //#region src/setup.ts
6
+ const operationDeclarations = (capabilities) => {
7
+ const declarations = Object.create(null);
8
+ for (const [name, capability] of capabilities) declarations[name] = Object.fromEntries([...capability.operations].map(([operation, op]) => [operation, {
9
+ args: op.args ?? [],
10
+ mode: op.mode,
11
+ ...op.command ? { command: true } : {}
12
+ }]));
13
+ return declarations;
14
+ };
15
+ const tsCore = {
16
+ compileLibrary,
17
+ defineObjectKind,
18
+ newGroup,
19
+ replacementLibraries,
20
+ restore
21
+ };
22
+ const compileLibraries = (readSource, setup, declarations, core = tsCore) => {
23
+ const out = /* @__PURE__ */ new Map();
24
+ let pending = setup.libraries ?? [];
25
+ while (pending.length) {
26
+ const failed = [];
27
+ for (const library of pending) try {
28
+ const done = [...out.values()].filter((l) => !(l instanceof LoadError));
29
+ out.set(library.name, core.compileLibrary({
30
+ ...library,
31
+ source: library.text ?? readSource(library.source)
32
+ }, done, declarations));
33
+ } catch (error) {
34
+ if (!(error instanceof LoadError)) throw error;
35
+ out.set(library.name, error);
36
+ failed.push(library);
37
+ }
38
+ if (failed.length === pending.length) break;
39
+ pending = failed;
40
+ }
41
+ return out;
42
+ };
43
+ const shapeSpec = (s) => {
44
+ switch (s.k) {
45
+ case "kind": return s.kind;
46
+ case "any":
47
+ case "value": return s.k;
48
+ case "quantity": return { quantity: s.unit };
49
+ case "unitKind": return { unitKind: s.kind };
50
+ case "object": return { object: s.kind };
51
+ case "list": return { list: shapeSpec(s.of) };
52
+ case "oneOf": return { oneOf: s.of.map(shapeSpec) };
53
+ case "optional": return { optional: shapeSpec(s.of) };
54
+ case "map": return {
55
+ map: s.fields.map((f) => ({
56
+ key: f.key,
57
+ optional: f.optional,
58
+ shape: shapeSpec(f.shape)
59
+ })),
60
+ open: s.open
61
+ };
62
+ }
63
+ };
64
+ const capabilityOperations = (capabilities) => capabilities.flatMap((c) => [...c.operations].map(([name, op]) => ({
65
+ capability: c.name,
66
+ name,
67
+ mode: op.mode,
68
+ args: (op.args ?? []).map(shapeSpec),
69
+ cost: { ...op.cost },
70
+ ...op.command ? { command: true } : {},
71
+ ...op.result ? { result: shapeSpec(op.result) } : {},
72
+ ...op.errors ? { errors: op.errors.map((e) => ({
73
+ code: e.code,
74
+ fields: Object.entries(e.fields ?? {}).map(([key, field]) => ({
75
+ key,
76
+ ..."shape" in field ? {
77
+ optional: true,
78
+ shape: shapeSpec(field.shape)
79
+ } : { shape: shapeSpec(field) }
80
+ }))
81
+ })) } : {}
82
+ })));
83
+ /** Freeze a new declaration without modifying any previous Session Setup. */
84
+ const immutableSetup = (setup) => {
85
+ const freeze = (value) => {
86
+ if (value && typeof value === "object" && !Object.isFrozen(value)) {
87
+ for (const child of Object.values(value)) freeze(child);
88
+ Object.freeze(value);
89
+ }
90
+ };
91
+ freeze(setup);
92
+ return setup;
93
+ };
94
+ /** Make a Group whose Scripts are loaded only when the Host requests them. */
95
+ const buildSetupGroup = (setup, capabilities, options) => {
96
+ const core = options.core ?? tsCore;
97
+ const group = core.newGroup({
98
+ name: options.name,
99
+ trace: options.trace,
100
+ ...setup.seed === void 0 ? {} : { seed: BigInt(setup.seed) }
101
+ });
102
+ const readSource = options.readSource ?? ((file) => {
103
+ throw new Error(`No source reader for ${file}`);
104
+ });
105
+ const declarations = operationDeclarations(capabilities);
106
+ const libraries = compileLibraries(readSource, setup, declarations, core);
107
+ const bound = /* @__PURE__ */ new Map();
108
+ const objectOf = (o) => {
109
+ const found = options.resolveObject?.(o.kind, o.id);
110
+ if (!found) throw new Error(`case.toml has no object ${o.kind} ${o.id}`);
111
+ return found;
112
+ };
113
+ return {
114
+ group,
115
+ libraries,
116
+ declarations,
117
+ grants: bound,
118
+ load(group, name, objects) {
119
+ const script = setup.scripts?.find((s) => s.name === name);
120
+ if (!script) throw new Error(`case.toml has no Script ${name}`);
121
+ const grants = Object.create(null);
122
+ for (const [granted, g] of Object.entries(script.grants ?? {})) {
123
+ const capability = capabilities.get(g.capability ?? granted);
124
+ if (!capability) throw new MissingCapabilityError(g.capability ?? granted);
125
+ if (g.coordinator !== void 0 && !capability.coordinator) throw new Error(`${capability.name} can't take a coordinator in case.toml`);
126
+ const make = () => capability.grant(g.ops, g.binding ?? (capability.name === "locale" ? "und" : void 0));
127
+ grants[granted] = options.withCoordinator ? options.withCoordinator(g.coordinator, make) : make();
128
+ }
129
+ bound.set(script.name, grants);
130
+ return group.load({
131
+ grants,
132
+ grantsAsUsed: script.grantsAsUsed,
133
+ name: script.name,
134
+ source: script.text ?? readSource(script.source),
135
+ limits: script.limits,
136
+ objects: objects ?? Object.fromEntries(Object.entries(script.objects ?? {}).map(([name, o]) => [name, objectOf(o)])),
137
+ ...script.owner ? { owner: objectOf(script.owner) } : {}
138
+ });
139
+ }
140
+ };
141
+ };
142
+ /** The caller decides whether an unavailable Capability is deferred or refused. */
143
+ var MissingCapabilityError = class extends Error {
144
+ capability;
145
+ constructor(capability) {
146
+ super(`the Standard Capability ${capability}`);
147
+ this.capability = capability;
148
+ }
149
+ };
150
+ const kinds = {
151
+ any: shape.any,
152
+ value: shape.value,
153
+ nothing: shape.nothing,
154
+ boolean: shape.bool,
155
+ number: shape.number,
156
+ text: shape.text,
157
+ bytes: shape.bytes,
158
+ instant: shape.instant,
159
+ "civil date": shape.civilDate,
160
+ range: shape.range,
161
+ pattern: shape.pattern,
162
+ function: shape.function
163
+ };
164
+ const shapeOf = (s) => {
165
+ if (typeof s === "string") {
166
+ const k = kinds[s];
167
+ if (!k) throw new Error(`Unknown Shape ${s}`);
168
+ return k;
169
+ }
170
+ if ("quantity" in s) return shape.quantityOf(s.quantity);
171
+ if ("unitKind" in s) return shape.quantityKind(s.unitKind);
172
+ if ("object" in s) return {
173
+ k: "object",
174
+ kind: s.object
175
+ };
176
+ if ("list" in s) return shape.listOf(shapeOf(s.list));
177
+ if ("oneOf" in s) return shape.oneOf(...s.oneOf.map(shapeOf));
178
+ if ("optional" in s) return shape.optional(shapeOf(s.optional));
179
+ if ("map" in s) {
180
+ const fields = Object.fromEntries(s.map.map((f) => [f.key, f.optional ? {
181
+ shape: shapeOf(f.shape),
182
+ optional: true
183
+ } : shapeOf(f.shape)]));
184
+ return s.open ? shape.openMap(fields) : shape.map(fields);
185
+ }
186
+ throw new Error("Unknown Shape");
187
+ };
188
+ //#endregion
189
+ export { MissingCapabilityError, buildSetupGroup, capabilityOperations, immutableSetup, operationDeclarations, shapeOf, shapeSpec, tsCore };
@@ -0,0 +1,123 @@
1
+ //#region src/sha256.ts
2
+ const K = new Uint32Array([
3
+ 1116352408,
4
+ 1899447441,
5
+ 3049323471,
6
+ 3921009573,
7
+ 961987163,
8
+ 1508970993,
9
+ 2453635748,
10
+ 2870763221,
11
+ 3624381080,
12
+ 310598401,
13
+ 607225278,
14
+ 1426881987,
15
+ 1925078388,
16
+ 2162078206,
17
+ 2614888103,
18
+ 3248222580,
19
+ 3835390401,
20
+ 4022224774,
21
+ 264347078,
22
+ 604807628,
23
+ 770255983,
24
+ 1249150122,
25
+ 1555081692,
26
+ 1996064986,
27
+ 2554220882,
28
+ 2821834349,
29
+ 2952996808,
30
+ 3210313671,
31
+ 3336571891,
32
+ 3584528711,
33
+ 113926993,
34
+ 338241895,
35
+ 666307205,
36
+ 773529912,
37
+ 1294757372,
38
+ 1396182291,
39
+ 1695183700,
40
+ 1986661051,
41
+ 2177026350,
42
+ 2456956037,
43
+ 2730485921,
44
+ 2820302411,
45
+ 3259730800,
46
+ 3345764771,
47
+ 3516065817,
48
+ 3600352804,
49
+ 4094571909,
50
+ 275423344,
51
+ 430227734,
52
+ 506948616,
53
+ 659060556,
54
+ 883997877,
55
+ 958139571,
56
+ 1322822218,
57
+ 1537002063,
58
+ 1747873779,
59
+ 1955562222,
60
+ 2024104815,
61
+ 2227730452,
62
+ 2361852424,
63
+ 2428436474,
64
+ 2756734187,
65
+ 3204031479,
66
+ 3329325298
67
+ ]);
68
+ const rotr = (x, n) => x >>> n | x << 32 - n;
69
+ const sha256 = (input) => {
70
+ const bytes = typeof input === "string" ? new TextEncoder().encode(input) : input;
71
+ const length = bytes.length;
72
+ const padded = new Uint8Array(Math.ceil((length + 9) / 64) * 64);
73
+ padded.set(bytes);
74
+ padded[length] = 128;
75
+ const view = new DataView(padded.buffer);
76
+ view.setUint32(padded.length - 8, Math.floor(length / 536870912));
77
+ view.setUint32(padded.length - 4, length * 8 >>> 0);
78
+ const h = new Uint32Array([
79
+ 1779033703,
80
+ 3144134277,
81
+ 1013904242,
82
+ 2773480762,
83
+ 1359893119,
84
+ 2600822924,
85
+ 528734635,
86
+ 1541459225
87
+ ]);
88
+ const w = /* @__PURE__ */ new Uint32Array(64);
89
+ for (let block = 0; block < padded.length; block += 64) {
90
+ for (let i = 0; i < 16; i++) w[i] = view.getUint32(block + i * 4);
91
+ for (let i = 16; i < 64; i++) {
92
+ const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ w[i - 15] >>> 3;
93
+ const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ w[i - 2] >>> 10;
94
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1 >>> 0;
95
+ }
96
+ let a = h[0], b = h[1], c = h[2], d = h[3], e = h[4], f = h[5], g = h[6], hh = h[7];
97
+ for (let i = 0; i < 64; i++) {
98
+ const s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
99
+ const ch = e & f ^ ~e & g;
100
+ const t1 = hh + s1 + ch + K[i] + w[i] >>> 0;
101
+ const t2 = (rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + (a & b ^ a & c ^ b & c) >>> 0;
102
+ hh = g;
103
+ g = f;
104
+ f = e;
105
+ e = d + t1 >>> 0;
106
+ d = c;
107
+ c = b;
108
+ b = a;
109
+ a = t1 + t2 >>> 0;
110
+ }
111
+ h[0] = h[0] + a >>> 0;
112
+ h[1] = h[1] + b >>> 0;
113
+ h[2] = h[2] + c >>> 0;
114
+ h[3] = h[3] + d >>> 0;
115
+ h[4] = h[4] + e >>> 0;
116
+ h[5] = h[5] + f >>> 0;
117
+ h[6] = h[6] + g >>> 0;
118
+ h[7] = h[7] + hh >>> 0;
119
+ }
120
+ return Array.from(h, (x) => x.toString(16).padStart(8, "0")).join("");
121
+ };
122
+ //#endregion
123
+ export { sha256 as t };
@@ -0,0 +1,87 @@
1
+ import { At as Call, Nt as Cost, at as Value, jt as CapabilityDef, qt as SegmentCoordinator } from "./debug-D8Z_u244.js";
2
+ //#region src/standard-capabilities.d.ts
3
+ type Costs = Readonly<Record<string, Cost>>;
4
+ type ConsoleImpl = {
5
+ read(call: Call<unknown>): void;
6
+ write(call: Call<unknown>, value: Value): void;
7
+ };
8
+ type TimerImpl = {
9
+ cancel(call: Call<unknown>, name: string): void;
10
+ schedule(call: Call<unknown>, name: string, at: Value, message: string, args: Value): void;
11
+ };
12
+ /** The Pump's Clock reading; there is no Host clock implementation. */
13
+ declare const clockCapability: (costs: Costs) => CapabilityDef<void>;
14
+ /** The Host owns durable timers, scoped by Script and name. */
15
+ declare const timerCapability: (impl: TimerImpl, costs: Costs) => CapabilityDef<unknown>;
16
+ /** The Host shows Values in text form and answers read with a line of text. */
17
+ declare const consoleCapability: (impl: ConsoleImpl, costs: Costs) => CapabilityDef<unknown>;
18
+ type CalendarImpl = {
19
+ now(call: Call<string>, zone?: string): Value;
20
+ offset(call: Call<string>, instant: Value, zone?: string): Value;
21
+ toCivil(call: Call<string>, instant: Value, zone?: string): Value;
22
+ today(call: Call<string>, zone?: string): Value;
23
+ toInstant(call: Call<string>, civil: Value, disambiguation: string, zone?: string): Value;
24
+ zone(call: Call<string>, zone?: string): Value;
25
+ };
26
+ /** The Host owns zone data; the Grant binding supplies its default zone. */
27
+ declare const calendarCapability: (impl: CalendarImpl, costs: Costs) => CapabilityDef<string>;
28
+ //#endregion
29
+ //#region src/locale-capability.d.ts
30
+ type LocaleImpl = {
31
+ compare(call: Call<string>, a: Value, b: Value, opts: Value, tag?: string): Value;
32
+ dayNames(call: Call<string>, opts: Value, tag?: string): Value;
33
+ lower(call: Call<string>, s: Value, tag?: string): Value;
34
+ monthNames(call: Call<string>, opts: Value, tag?: string): Value;
35
+ numberSymbols(call: Call<string>, tag?: string): Value;
36
+ rank(call: Call<string>, texts: Value, opts: Value, tag?: string): Value;
37
+ tag(call: Call<string>, tag?: string): Value;
38
+ upper(call: Call<string>, s: Value, tag?: string): Value;
39
+ };
40
+ /** The Host resolves tags and provides every Operation for each Locale. */
41
+ declare const localeCapability: (impl: LocaleImpl, costs: Costs) => CapabilityDef<string>;
42
+ //#endregion
43
+ //#region src/sqlite-capability.d.ts
44
+ /**
45
+ * sqlite is optional (chapter 7, ADR 0070). The binding names a database the
46
+ * Host keeps; tables, when present, limits the tables a Grant may use, and
47
+ * maxRows caps the rows a call may give.
48
+ */
49
+ type SqliteBinding = {
50
+ readonly database: string;
51
+ readonly maxRows: number;
52
+ readonly tables?: readonly string[];
53
+ };
54
+ /** NULL, INTEGER, REAL, TEXT and BLOB. */
55
+ type SqlValue = null | bigint | number | string | Uint8Array;
56
+ /** A list binds `?` and `?NNN`; a Map binds `:name`, keyed without the colon. */
57
+ type SqlParams = readonly SqlValue[] | ReadonlyMap<string, SqlValue>;
58
+ type SqlRows = {
59
+ readonly columns: readonly string[];
60
+ readonly rows: readonly (readonly SqlValue[])[];
61
+ };
62
+ /**
63
+ * The Core has checked the Shapes and max, converted params by the chapter 7
64
+ * rules and charged max × perRow. It converts the result, raising `sql` for a
65
+ * column named twice and `unrepresentable` for a double it can't hold.
66
+ * change, begin, commit and rollback are Segment-bound: they act inside the
67
+ * calling Segment's transaction, which the coordinator publishes or discards.
68
+ * begin, commit and rollback open, release and roll back a savepoint. A failed
69
+ * call leaves the database as it was. Throw ScriptError `sql` {reason},
70
+ * `constraint` {kind}, `sqlite busy`, `not read-only`, `too many rows` {max},
71
+ * or `unrepresentable` {column} for TEXT that isn't valid UTF-8 (chapter 9).
72
+ */
73
+ type SqliteImpl = {
74
+ begin(call: Call<SqliteBinding>): void;
75
+ change(call: Call<SqliteBinding>, sql: string, params: SqlParams, max: number): SqlRows & {
76
+ readonly changes: number;
77
+ };
78
+ commit(call: Call<SqliteBinding>): void;
79
+ /** Called once per Grant. The same database gives the same coordinator. */
80
+ coordinator(database: string): SegmentCoordinator;
81
+ query(call: Call<SqliteBinding>, sql: string, params: SqlParams, max: number): SqlRows;
82
+ rollback(call: Call<SqliteBinding>): void;
83
+ };
84
+ /** Optional for Hosts. perRow is whole Fuel charged per row of max. */
85
+ declare const sqliteCapability: (impl: SqliteImpl, costs: Costs, perRow: number) => CapabilityDef<SqliteBinding>;
86
+ //#endregion
87
+ export { SqliteImpl as a, localeCapability as c, Costs as d, TimerImpl as f, timerCapability as g, consoleCapability as h, SqliteBinding as i, CalendarImpl as l, clockCapability as m, SqlRows as n, sqliteCapability as o, calendarCapability as p, SqlValue as r, LocaleImpl as s, SqlParams as t, ConsoleImpl as u };