@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.
@@ -0,0 +1,208 @@
1
+ import { N as objectValue, t as HostError } from "./errors-DHwz1Vho.js";
2
+ import { n as quoteJSON, pt as languageVersion, ut as compareText } from "./encoding-DWlbPq-e.js";
3
+ //#region src/objects.ts
4
+ const elementCost = (cost, name) => {
5
+ const c = cost;
6
+ if (!c || typeof c !== "object" || !Number.isSafeInteger(c.fuel) || c.fuel < 0 || c.alloc !== void 0 && (!Number.isSafeInteger(c.alloc) || c.alloc < 0)) throw new HostError("invalid value", `Invalid or missing elements ${name}`);
7
+ return Object.freeze({
8
+ fuel: c.fuel,
9
+ ...c.alloc === void 0 ? {} : { alloc: c.alloc }
10
+ });
11
+ };
12
+ const elementShape = (shape) => {
13
+ const s = shape;
14
+ const kinds = s?.k === "object" ? [s.kind] : s?.k === "oneOf" && s.of.length && s.of.every((o) => o.k === "object") ? s.of.map((o) => o.kind) : null;
15
+ if (!kinds || new Set(kinds).size !== kinds.length) throw new HostError("invalid value", "An Object Collection Shape must be an Object Shape or a choice of distinct ones");
16
+ return s;
17
+ };
18
+ const objectElements = (e) => {
19
+ if (e === void 0) return null;
20
+ if (!e || typeof e.count !== "function" || typeof e.at !== "function" || typeof e.find !== "function") throw new HostError("invalid value", "An Object Collection needs count, at and find");
21
+ return Object.freeze({
22
+ shape: elementShape(e.shape),
23
+ count: e.count,
24
+ at: e.at,
25
+ find: e.find,
26
+ countCost: elementCost(e.countCost, "countCost"),
27
+ atCost: elementCost(e.atCost, "atCost"),
28
+ findCost: elementCost(e.findCost, "findCost")
29
+ });
30
+ };
31
+ const kinds = /* @__PURE__ */ new Map();
32
+ /** A restored handle uses the process's definition of its saved kind. */
33
+ const objectKind = (name) => kinds.get(name);
34
+ /** Define an Object Kind, once per process. */
35
+ const defineObjectKind = (k) => {
36
+ const kind = Object.freeze({
37
+ name: k.name,
38
+ parentKinds: Object.freeze([...k.parentKinds ?? []]),
39
+ props: new Map(Object.entries(k.props)),
40
+ elements: objectElements(k.elements)
41
+ });
42
+ kinds.set(kind.name, kind);
43
+ return kind;
44
+ };
45
+ /** A new handle and its state, for the Group that makes it. */
46
+ const makeObject = (kind, id, native) => {
47
+ const state = {};
48
+ const handle = Object.freeze({
49
+ id,
50
+ kind,
51
+ native,
52
+ value: objectValue({
53
+ kind: kind.name,
54
+ id,
55
+ handle: state
56
+ })
57
+ });
58
+ Object.assign(state, {
59
+ disposed: false,
60
+ handle,
61
+ owner: null,
62
+ parent: null
63
+ });
64
+ return state;
65
+ };
66
+ /** The Group's state of an object value. */
67
+ const stateOf = (v) => v.asObjectRef()?.handle;
68
+ /** Rebind native state while preserving every saved reference to this object. */
69
+ const rebindObject = (state, native) => {
70
+ const { kind, id } = state.handle;
71
+ state.handle = Object.freeze({
72
+ kind,
73
+ id,
74
+ native,
75
+ value: objectValue({
76
+ kind: kind.name,
77
+ id,
78
+ handle: state
79
+ })
80
+ });
81
+ };
82
+ /** Loading checks declarations without retaining Host callbacks in checked code. */
83
+ const propertyChecks = (objects, owner) => {
84
+ const props = (kind) => Object.fromEntries([...kind.props].map(([name, prop]) => [name, !!prop.set]));
85
+ return {
86
+ objectProperties: Object.fromEntries(Object.entries(objects).map(([name, object]) => [name, props(object.kind)])),
87
+ ownerProperties: owner ? props(owner.kind) : void 0
88
+ };
89
+ };
90
+ //#endregion
91
+ //#region src/manifest.ts
92
+ const shapeData = (shape) => {
93
+ switch (shape.k) {
94
+ case "any": return "any";
95
+ case "value": return "value";
96
+ case "kind": return shape.kind;
97
+ case "object": return { object: shape.kind };
98
+ case "quantity": return { quantity: shape.unit };
99
+ case "unitKind": return { unitKind: shape.kind };
100
+ case "list": return { list: shapeData(shape.of) };
101
+ case "oneOf": return { oneOf: shape.of.map(shapeData) };
102
+ case "optional": return { optional: shapeData(shape.of) };
103
+ case "map": return {
104
+ map: shape.fields.map((f) => ({
105
+ key: f.key,
106
+ shape: shapeData(f.shape),
107
+ ...f.optional ? { optional: true } : {}
108
+ })),
109
+ ...shape.open ? { open: true } : {}
110
+ };
111
+ }
112
+ };
113
+ const operationData = (name, op) => ({
114
+ name,
115
+ mode: op.mode,
116
+ args: (op.args ?? []).map(shapeData),
117
+ ...op.result ? { result: shapeData(op.result) } : {},
118
+ cost: {
119
+ fuel: op.cost.fuel,
120
+ alloc: op.cost.alloc ?? 0
121
+ },
122
+ ...op.mode === "suspending" && op.maxPendingMs !== void 0 ? { maxPending: op.maxPendingMs } : {},
123
+ ...op.errors === void 0 ? {} : { errors: [...op.errors].sort((a, b) => compareText(a.code, b.code)).map((e) => ({
124
+ code: e.code,
125
+ fields: Object.entries(e.fields ?? {}).map(([key, f]) => ({
126
+ key,
127
+ shape: shapeData("shape" in f ? f.shape : f),
128
+ ..."shape" in f ? { optional: true } : {}
129
+ }))
130
+ })) },
131
+ ...op.mode === "immediate" && op.scope ? { scope: "opens" in op.scope ? {
132
+ opens: op.scope.opens,
133
+ abandon: op.scope.abandon
134
+ } : { closes: op.scope.closes } } : {},
135
+ ...op.mode === "immediate" && op.segmentBound ? { segmentBound: true } : {},
136
+ ...op.command ? { command: true } : {}
137
+ });
138
+ const grantData = (grant, operation = operationData) => ({
139
+ capability: grant.capability.name,
140
+ operations: [...grant.ops].sort(compareText).map((name) => operation(name, grant.capability.operations.get(name)))
141
+ });
142
+ /** Operation Data plus the manifest-only documentation fields. */
143
+ const documentedOperation = (name, op) => ({
144
+ ...operationData(name, op),
145
+ ...op.description ? { description: op.description } : {},
146
+ ...op.examples?.length ? { examples: op.examples } : {}
147
+ });
148
+ const canonicalJSON = (value) => {
149
+ if (typeof value === "string") return quoteJSON(value);
150
+ if (Array.isArray(value)) return `[${value.map(canonicalJSON).join(",")}]`;
151
+ if (value && typeof value === "object") return `{${Object.entries(value).map(([k, v]) => `${quoteJSON(k)}:${canonicalJSON(v)}`).join(",")}}`;
152
+ return JSON.stringify(value);
153
+ };
154
+ const byName = (values) => [...values].sort((a, b) => compareText(a.name, b.name));
155
+ const costData = (cost) => ({
156
+ fuel: cost?.fuel ?? 0,
157
+ alloc: cost?.alloc ?? 0
158
+ });
159
+ const needsData = (needs) => [...needs].sort((a, b) => compareText(a.capability, b.capability) || compareText(a.operation, b.operation)).map(({ capability, operation }) => ({
160
+ capability,
161
+ operation
162
+ }));
163
+ /** Chapter 9's deterministic JSON file, ending in LF; never calls the Host. */
164
+ const exportManifest = (m) => `${canonicalJSON({
165
+ kind: m.kind,
166
+ version: m.version,
167
+ language: languageVersion,
168
+ grants: Object.entries(m.grants).sort(([a], [b]) => compareText(a, b)).map(([name, grant]) => ({
169
+ name,
170
+ ...grantData(grant, documentedOperation)
171
+ })),
172
+ libraries: byName(m.libraries ?? []).map((l) => ({
173
+ name: l.name,
174
+ version: l.version,
175
+ source: l.source,
176
+ needs: needsData(l.needs)
177
+ })),
178
+ messages: byName(m.messages ?? []).map((message) => ({
179
+ name: message.name,
180
+ args: (message.args ?? []).map(shapeData),
181
+ receivers: byName(message.receivers ?? []).map((kind) => kind.name),
182
+ ...message.description ? { description: message.description } : {},
183
+ ...message.examples?.length ? { examples: message.examples } : {}
184
+ })),
185
+ objectKinds: byName(m.objectKinds ?? []).map((kind) => ({
186
+ name: kind.name,
187
+ props: [...kind.props].sort(([a], [b]) => compareText(a, b)).map(([name, prop]) => ({
188
+ name,
189
+ shape: prop.shape ? shapeData(prop.shape) : "value",
190
+ readOnly: !prop.set,
191
+ getCost: costData(prop.getCost),
192
+ setCost: costData(prop.setCost)
193
+ })),
194
+ parentKinds: [...kind.parentKinds].sort(compareText),
195
+ ...kind.elements ? { elements: {
196
+ shape: shapeData(kind.elements.shape),
197
+ countCost: costData(kind.elements.countCost),
198
+ atCost: costData(kind.elements.atCost),
199
+ findCost: costData(kind.elements.findCost)
200
+ } } : {}
201
+ })),
202
+ objects: Object.entries(m.objects ?? {}).sort(([a], [b]) => compareText(a, b)).map(([name, kind]) => ({
203
+ name,
204
+ kind: kind.name
205
+ }))
206
+ })}\n`;
207
+ //#endregion
208
+ export { shapeData as a, objectKind as c, stateOf as d, operationData as i, propertyChecks as l, exportManifest as n, defineObjectKind as o, grantData as r, makeObject as s, canonicalJSON as t, rebindObject as u };
package/notify.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { a as changed, i as WatchLimits, n as RouterOptions, o as createRouter, r as RoutingResult, t as NotificationRouter } from "./router-DpDpAJjz.js";
2
+ export { type NotificationRouter, type RouterOptions, type RoutingResult, type WatchLimits, changed, createRouter };
package/notify.js ADDED
@@ -0,0 +1 @@
1
+ export { t as changed, n as createRouter } from "./router-DHPJNvbt.js";