@orthograph/compiler 0.0.0-stage → 0.1.0-next.1
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/LICENSE +202 -0
- package/README.md +4 -2
- package/dist/index.d.ts +12 -0
- package/dist/index.js +9 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +18 -0
- package/dist/modules/app/internal/checks.d.ts +30 -0
- package/dist/modules/app/internal/checks.js +99 -0
- package/dist/modules/app/internal/describe.d.ts +6 -0
- package/dist/modules/app/internal/describe.js +59 -0
- package/dist/modules/app/internal/hash.d.ts +1 -0
- package/dist/modules/app/internal/hash.js +7 -0
- package/dist/modules/app/internal/known.d.ts +19 -0
- package/dist/modules/app/internal/known.js +45 -0
- package/dist/modules/app/internal/read.d.ts +6 -0
- package/dist/modules/app/internal/read.js +45 -0
- package/dist/modules/app/service.d.ts +2 -0
- package/dist/modules/app/service.js +3 -0
- package/dist/modules/app/types.d.ts +87 -0
- package/dist/modules/app/types.js +1 -0
- package/dist/modules/compare/internal/assets.d.ts +4 -0
- package/dist/modules/compare/internal/assets.js +40 -0
- package/dist/modules/compare/internal/chain.d.ts +4 -0
- package/dist/modules/compare/internal/chain.js +37 -0
- package/dist/modules/compare/internal/diff.d.ts +3 -0
- package/dist/modules/compare/internal/diff.js +27 -0
- package/dist/modules/compare/internal/versions.d.ts +9 -0
- package/dist/modules/compare/internal/versions.js +75 -0
- package/dist/modules/compare/service.d.ts +2 -0
- package/dist/modules/compare/service.js +43 -0
- package/dist/modules/compare/types.d.ts +47 -0
- package/dist/modules/compare/types.js +1 -0
- package/dist/modules/lock/internal/evolve.d.ts +7 -0
- package/dist/modules/lock/internal/evolve.js +52 -0
- package/dist/modules/lock/internal/runtime.d.ts +3 -0
- package/dist/modules/lock/internal/runtime.js +12 -0
- package/dist/modules/lock/service.d.ts +4 -0
- package/dist/modules/lock/service.js +20 -0
- package/dist/modules/lock/types.d.ts +101 -0
- package/dist/modules/lock/types.js +6 -0
- package/dist/modules/model/internal/allowed.d.ts +26 -0
- package/dist/modules/model/internal/allowed.js +78 -0
- package/dist/modules/model/internal/approval.d.ts +4 -0
- package/dist/modules/model/internal/approval.js +10 -0
- package/dist/modules/model/internal/assets.d.ts +5 -0
- package/dist/modules/model/internal/assets.js +31 -0
- package/dist/modules/model/internal/bits.d.ts +6 -0
- package/dist/modules/model/internal/bits.js +9 -0
- package/dist/modules/model/internal/chain.d.ts +9 -0
- package/dist/modules/model/internal/chain.js +47 -0
- package/dist/modules/model/internal/codes.d.ts +59 -0
- package/dist/modules/model/internal/codes.js +80 -0
- package/dist/modules/model/internal/derive.d.ts +11 -0
- package/dist/modules/model/internal/derive.js +31 -0
- package/dist/modules/model/internal/describe.d.ts +8 -0
- package/dist/modules/model/internal/describe.js +248 -0
- package/dist/modules/model/internal/device.d.ts +5 -0
- package/dist/modules/model/internal/device.js +12 -0
- package/dist/modules/model/internal/environments.d.ts +7 -0
- package/dist/modules/model/internal/environments.js +90 -0
- package/dist/modules/model/internal/exits.d.ts +49 -0
- package/dist/modules/model/internal/exits.js +85 -0
- package/dist/modules/model/internal/fields.d.ts +23 -0
- package/dist/modules/model/internal/fields.js +50 -0
- package/dist/modules/model/internal/fillable.d.ts +3 -0
- package/dist/modules/model/internal/fillable.js +9 -0
- package/dist/modules/model/internal/forms.d.ts +47 -0
- package/dist/modules/model/internal/forms.js +75 -0
- package/dist/modules/model/internal/found.d.ts +40 -0
- package/dist/modules/model/internal/found.js +60 -0
- package/dist/modules/model/internal/handler-files.d.ts +18 -0
- package/dist/modules/model/internal/handler-files.js +23 -0
- package/dist/modules/model/internal/hash.d.ts +4 -0
- package/dist/modules/model/internal/hash.js +7 -0
- package/dist/modules/model/internal/holds.d.ts +19 -0
- package/dist/modules/model/internal/holds.js +65 -0
- package/dist/modules/model/internal/invariants.d.ts +38 -0
- package/dist/modules/model/internal/invariants.js +134 -0
- package/dist/modules/model/internal/journeys.d.ts +15 -0
- package/dist/modules/model/internal/journeys.js +135 -0
- package/dist/modules/model/internal/layout.d.ts +143 -0
- package/dist/modules/model/internal/layout.js +203 -0
- package/dist/modules/model/internal/lock-fields.d.ts +3 -0
- package/dist/modules/model/internal/lock-fields.js +17 -0
- package/dist/modules/model/internal/money.d.ts +17 -0
- package/dist/modules/model/internal/money.js +49 -0
- package/dist/modules/model/internal/nullable.d.ts +7 -0
- package/dist/modules/model/internal/nullable.js +21 -0
- package/dist/modules/model/internal/operations.d.ts +27 -0
- package/dist/modules/model/internal/operations.js +81 -0
- package/dist/modules/model/internal/reads.d.ts +16 -0
- package/dist/modules/model/internal/reads.js +58 -0
- package/dist/modules/model/internal/rules.d.ts +2 -0
- package/dist/modules/model/internal/rules.js +33 -0
- package/dist/modules/model/internal/same-value.d.ts +10 -0
- package/dist/modules/model/internal/same-value.js +50 -0
- package/dist/modules/model/internal/secrets.d.ts +4 -0
- package/dist/modules/model/internal/secrets.js +35 -0
- package/dist/modules/model/internal/source-refusals.d.ts +7 -0
- package/dist/modules/model/internal/source-refusals.js +52 -0
- package/dist/modules/model/internal/sources.d.ts +12 -0
- package/dist/modules/model/internal/sources.js +65 -0
- package/dist/modules/model/internal/stores.d.ts +20 -0
- package/dist/modules/model/internal/stores.js +44 -0
- package/dist/modules/model/internal/things.d.ts +33 -0
- package/dist/modules/model/internal/things.js +99 -0
- package/dist/modules/model/service.d.ts +4 -0
- package/dist/modules/model/service.js +105 -0
- package/dist/modules/model/types.d.ts +485 -0
- package/dist/modules/model/types.js +5 -0
- package/dist/modules/mounts/fixtures/both/things/echo/thing.d.ts +1 -0
- package/dist/modules/mounts/fixtures/both/things/echo/thing.js +1 -0
- package/dist/modules/mounts/fixtures/both/things/echo/verbs/only.d.ts +1 -0
- package/dist/modules/mounts/fixtures/both/things/echo/verbs/only.js +1 -0
- package/dist/modules/mounts/internal/attribute.d.ts +2 -0
- package/dist/modules/mounts/internal/attribute.js +4 -0
- package/dist/modules/mounts/internal/calls.d.ts +12 -0
- package/dist/modules/mounts/internal/calls.js +42 -0
- package/dist/modules/mounts/internal/choices.d.ts +11 -0
- package/dist/modules/mounts/internal/choices.js +34 -0
- package/dist/modules/mounts/internal/fold.d.ts +6 -0
- package/dist/modules/mounts/internal/fold.js +8 -0
- package/dist/modules/mounts/internal/needs.d.ts +14 -0
- package/dist/modules/mounts/internal/needs.js +26 -0
- package/dist/modules/mounts/internal/verbs.d.ts +11 -0
- package/dist/modules/mounts/internal/verbs.js +44 -0
- package/dist/modules/mounts/service.d.ts +2 -0
- package/dist/modules/mounts/service.js +165 -0
- package/dist/modules/mounts/types.d.ts +78 -0
- package/dist/modules/mounts/types.js +1 -0
- package/dist/modules/source/internal/plugins.d.ts +25 -0
- package/dist/modules/source/internal/plugins.js +49 -0
- package/dist/modules/source/service.d.ts +6 -0
- package/dist/modules/source/service.js +86 -0
- package/dist/modules/source/types.d.ts +55 -0
- package/dist/modules/source/types.js +6 -0
- package/dist/modules/time/internal/anchor.d.ts +2 -0
- package/dist/modules/time/internal/anchor.js +48 -0
- package/dist/modules/time/internal/keeper.d.ts +13 -0
- package/dist/modules/time/internal/keeper.js +19 -0
- package/dist/modules/time/internal/zones.d.ts +1 -0
- package/dist/modules/time/internal/zones.js +3 -0
- package/dist/modules/time/service.d.ts +2 -0
- package/dist/modules/time/service.js +56 -0
- package/dist/modules/time/types.d.ts +89 -0
- package/dist/modules/time/types.js +1 -0
- package/package.json +30 -3
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Allowed, FieldsInput, Role, Thing } from "orthograph";
|
|
2
|
+
import type { AllowedModel } from "../types.ts";
|
|
3
|
+
import { type Layout } from "./layout.ts";
|
|
4
|
+
/**
|
|
5
|
+
* `allowedModel` reads one entry of a declaration's `allowed` (or a journey's cast). A narrowed entry (`.via`, `.notVia`) says the ways in it
|
|
6
|
+
* keeps, sorted; a `service` role says the name of the adapter it admits (M2-auth, fork 3).
|
|
7
|
+
*/
|
|
8
|
+
export declare const allowedOf: (layout: Layout, roleNames: ReadonlyMap<Role<any>, string>, problems: Array<string>) => (entry: Allowed, where: string) => AllowedModel;
|
|
9
|
+
/**
|
|
10
|
+
* The problem with a role path that walks to nothing (#767). `owner.of("<path>")` is decided by walking the path field by field to an
|
|
11
|
+
* instance of the role's thing, as language-effect's access does: from the payload field its first key names, else from the subject.
|
|
12
|
+
* On a read by id the first key may name the subject's own thing, spelt as its name in camel case (`bankAccount`), the name it has
|
|
13
|
+
* before any mount renames it (`ownName`); the runtime takes any key that is no field of the subject so, and `check` holds it to that
|
|
14
|
+
* spelling. A service role is decided by its mount, not a path.
|
|
15
|
+
*/
|
|
16
|
+
export declare const rolePathProblem: (spec: {
|
|
17
|
+
readonly entry: Allowed;
|
|
18
|
+
readonly payload: FieldsInput;
|
|
19
|
+
readonly own: Thing;
|
|
20
|
+
readonly ownName: string;
|
|
21
|
+
readonly names: {
|
|
22
|
+
readonly role: (r: unknown) => string;
|
|
23
|
+
readonly thing: (t: Thing) => string;
|
|
24
|
+
};
|
|
25
|
+
readonly where: string;
|
|
26
|
+
}) => string | undefined;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { referenced } from "./found.js";
|
|
2
|
+
import { camel } from "./layout.js";
|
|
3
|
+
/** Records a problem and answers the `?` the model shows for a name it could not find. */
|
|
4
|
+
const unknown = (problems, p) => {
|
|
5
|
+
problems.push(p);
|
|
6
|
+
return "?";
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* `allowedModel` reads one entry of a declaration's `allowed` (or a journey's cast). A narrowed entry (`.via`, `.notVia`) says the ways in it
|
|
10
|
+
* keeps, sorted; a `service` role says the name of the adapter it admits (M2-auth, fork 3).
|
|
11
|
+
*/
|
|
12
|
+
export const allowedOf = (layout, roleNames, problems) => {
|
|
13
|
+
/** The name an adapter is mounted under: its file's. A `service` role admits the service of that mount. */
|
|
14
|
+
const mountName = (mount, where) => {
|
|
15
|
+
const found = [...layout.mounts].find(([, m]) => m.mount === mount);
|
|
16
|
+
if (!found)
|
|
17
|
+
return unknown(problems, `${where}: a service role names a mount that is not mounted: mount its adapter in blueprint/adapters/`);
|
|
18
|
+
if (found[1].kind !== "adapter")
|
|
19
|
+
unknown(problems, `${where}: a service role names ${found[0]}, which is not an adapter: only an adapter's door makes a service`);
|
|
20
|
+
return found[0];
|
|
21
|
+
};
|
|
22
|
+
const allowedModel = (entry, where) => {
|
|
23
|
+
// A narrowed entry keeps the ways in it names (sorted by the declaration), and none may be empty.
|
|
24
|
+
const a = entry.kind === "narrowed" ? entry.entry : entry;
|
|
25
|
+
if (entry.kind === "narrowed" && entry.via.length === 0)
|
|
26
|
+
problems.push(`${where}: .via(...) names no way in, so nobody could call it`);
|
|
27
|
+
const via = entry.kind === "narrowed" ? { via: [...entry.via] } : {};
|
|
28
|
+
if (a.kind === "anyone")
|
|
29
|
+
return { kind: "anyone" };
|
|
30
|
+
if (a.kind === "system")
|
|
31
|
+
return { kind: "system" };
|
|
32
|
+
if (a.kind === "keeper")
|
|
33
|
+
return { kind: "keeper" };
|
|
34
|
+
if (a.kind === "signed-in")
|
|
35
|
+
return { kind: "signed-in", ...via };
|
|
36
|
+
const name = (r) => roleNames.get(r) ?? unknown(problems, `${where}: uses a role not exported from a roles.ts`);
|
|
37
|
+
const role = a.kind === "role" ? a : a.role;
|
|
38
|
+
const service = role.service === undefined ? {} : { service: mountName(role.service, where) };
|
|
39
|
+
if (a.kind === "role")
|
|
40
|
+
return { kind: "role", role: name(a), ...service, ...via };
|
|
41
|
+
return { kind: "role-of", role: name(a.role), path: a.path, ...service, ...via };
|
|
42
|
+
};
|
|
43
|
+
return allowedModel;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* The problem with a role path that walks to nothing (#767). `owner.of("<path>")` is decided by walking the path field by field to an
|
|
47
|
+
* instance of the role's thing, as language-effect's access does: from the payload field its first key names, else from the subject.
|
|
48
|
+
* On a read by id the first key may name the subject's own thing, spelt as its name in camel case (`bankAccount`), the name it has
|
|
49
|
+
* before any mount renames it (`ownName`); the runtime takes any key that is no field of the subject so, and `check` holds it to that
|
|
50
|
+
* spelling. A service role is decided by its mount, not a path.
|
|
51
|
+
*/
|
|
52
|
+
export const rolePathProblem = (spec) => {
|
|
53
|
+
const { payload, own, names, where } = spec;
|
|
54
|
+
const a = spec.entry.kind === "narrowed" ? spec.entry.entry : spec.entry;
|
|
55
|
+
if (a.kind !== "role-of" || a.role.service !== undefined)
|
|
56
|
+
return undefined;
|
|
57
|
+
const role = a.role;
|
|
58
|
+
const said = `${where}: ${names.role(role)} is decided on "${a.path}"`;
|
|
59
|
+
const [first = "", ...rest] = a.path.split(".");
|
|
60
|
+
const subject = Object.keys(payload).find((k) => referenced(payload[k]) === own);
|
|
61
|
+
const ownName = camel(spec.ownName);
|
|
62
|
+
let thing = first in payload ? referenced(payload[first]) : subject !== undefined ? own : undefined;
|
|
63
|
+
if (thing === undefined) {
|
|
64
|
+
return first in payload
|
|
65
|
+
? `${said}, but the payload's ${first} holds no thing's id`
|
|
66
|
+
: `${said}, but ${first} is no payload field, and the payload names no ${ownName} to start from: its fields are ${Object.keys(payload).join(", ")}`;
|
|
67
|
+
}
|
|
68
|
+
const namesSubject = !(first in payload) && subject === "id" && first === ownName && !(first in own.fields);
|
|
69
|
+
for (const key of first in payload || namesSubject ? rest : [first, ...rest]) {
|
|
70
|
+
if (!(key in thing.fields))
|
|
71
|
+
return `${said}, but ${names.thing(thing)} has no field ${key}: its fields are ${Object.keys(thing.fields).join(", ")}`;
|
|
72
|
+
const next = referenced(thing.fields[key]);
|
|
73
|
+
if (next === undefined)
|
|
74
|
+
return `${said}, but ${names.thing(thing)}.${key} holds no thing's id, so the walk stops there`;
|
|
75
|
+
thing = next;
|
|
76
|
+
}
|
|
77
|
+
return thing === role.thing ? undefined : `${said}, which reaches a ${names.thing(thing)}, but ${names.role(role)} is a role on ${names.thing(role.thing)}`;
|
|
78
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// `needsApproval`: an operation says a person must say yes before it runs (#466). `true` is the one value; absence says no.
|
|
2
|
+
/** The model's key, only when declared. Anything but `true` is a problem naming the file, and adds no key. */
|
|
3
|
+
export const approvalOf = (op, file, problems) => {
|
|
4
|
+
if (!("needsApproval" in op))
|
|
5
|
+
return {};
|
|
6
|
+
if (op.needsApproval === true)
|
|
7
|
+
return { needsApproval: true };
|
|
8
|
+
problems.push(`${file}: needsApproval must be \`true\` or left out (a person must say yes before it runs); leave it out for an operation that asks nobody`);
|
|
9
|
+
return {};
|
|
10
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AssetModel, OperationModel, ThingModel } from "../types.ts";
|
|
2
|
+
import type { Layout } from "./layout.ts";
|
|
3
|
+
export declare const assetsOf: (layout: Layout, problems: Array<string>) => Readonly<Record<string, AssetModel>>;
|
|
4
|
+
/** For each chain store, the fixed token assets its things count in their fields, events, errors and operations. */
|
|
5
|
+
export declare const tokensUsed: (things: Readonly<Record<string, ThingModel>>, operations: Readonly<Record<string, OperationModel>>) => ReadonlyMap<string, ReadonlySet<string>>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Assets in the model (M6-money item 2): each named by its file, refused where it is declared when it is wrong, and which fixed tokens a chain store counts.
|
|
2
|
+
import { placesOf } from "orthograph/server";
|
|
3
|
+
const MAX_DECIMALS = 255;
|
|
4
|
+
export const assetsOf = (layout, problems) => Object.fromEntries([...layout.assets].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)).map(([name, { file, asset }]) => {
|
|
5
|
+
if (asset.of === "currency") {
|
|
6
|
+
const places = placesOf(asset.code);
|
|
7
|
+
if (places === undefined)
|
|
8
|
+
problems.push(`${file}: ${JSON.stringify(asset.code)} is an unknown currency code: use an ISO 4217 code in capitals, such as EUR`);
|
|
9
|
+
return [name, { name, file, kind: "currency", code: asset.code, decimals: places ?? 0 }];
|
|
10
|
+
}
|
|
11
|
+
if (typeof asset.symbol !== "string" || asset.symbol === "")
|
|
12
|
+
problems.push(`${file}: a token asset needs a symbol, such as "USDC"`);
|
|
13
|
+
if (!Number.isInteger(asset.decimals) || asset.decimals < 0 || asset.decimals > MAX_DECIMALS)
|
|
14
|
+
problems.push(`${file}: a token asset's decimals is a whole number from 0 to ${MAX_DECIMALS}, as the token's own decimals() says`);
|
|
15
|
+
return [name, { name, file, kind: "token", symbol: asset.symbol, decimals: asset.decimals }];
|
|
16
|
+
}));
|
|
17
|
+
const fixedTokens = (fields) => Object.values(fields).flatMap((f) => (f.value.type === "money" && "token" in f.value.asset ? [f.value.asset.token] : []));
|
|
18
|
+
/** For each chain store, the fixed token assets its things count in their fields, events, errors and operations. */
|
|
19
|
+
export const tokensUsed = (things, operations) => {
|
|
20
|
+
const used = new Map();
|
|
21
|
+
const note = (store, tokens) => {
|
|
22
|
+
if (store === undefined || tokens.length === 0)
|
|
23
|
+
return;
|
|
24
|
+
used.set(store, new Set([...(used.get(store) ?? []), ...tokens]));
|
|
25
|
+
};
|
|
26
|
+
for (const t of Object.values(things))
|
|
27
|
+
note(t.store, [...fixedTokens(t.fields), ...Object.values(t.events).flatMap(fixedTokens), ...Object.values(t.errors).flatMap((e) => fixedTokens(e.fields))]);
|
|
28
|
+
for (const op of Object.values(operations))
|
|
29
|
+
note(things[op.thing]?.store, [...fixedTokens(op.payload), ...fixedTokens(op.answer)]);
|
|
30
|
+
return used;
|
|
31
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Role } from "orthograph";
|
|
2
|
+
import type { OperationModel } from "../types.ts";
|
|
3
|
+
/** What a role's rule is in the model: its function's text, or the words of an `actor:` or `agent:` role. */
|
|
4
|
+
export declare const roleRuleOf: (r: Role) => string;
|
|
5
|
+
/** The refusal of a read not served yet is in the model only when some read can make it, so other blueprints are unchanged. */
|
|
6
|
+
export declare const withReadNotServed: (refusals: Readonly<Record<string, string>>, operations: Readonly<Record<string, OperationModel>>) => Readonly<Record<string, string>>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { READ_NOT_SERVED, READ_NOT_SERVED_CODE } from "./codes.js";
|
|
2
|
+
/** What a role's rule is in the model: its function's text, or the words of an `actor:` or `agent:` role. */
|
|
3
|
+
export const roleRuleOf = (r) => r.actor ? `actor(${r.actor.mount.key}, ${r.actor.id.key}) within ${r.actor.within?.key ?? "nothing"} while [${r.actor.while.map((s) => s.key).join(", ")}]`
|
|
4
|
+
: r.agent ? `agent(${r.agent.key})`
|
|
5
|
+
: r.rule.toString();
|
|
6
|
+
/** The refusal of a read not served yet is in the model only when some read can make it, so other blueprints are unchanged. */
|
|
7
|
+
export const withReadNotServed = (refusals, operations) => Object.values(operations).some((o) => o.refusals.includes(READ_NOT_SERVED))
|
|
8
|
+
? Object.fromEntries(Object.entries({ ...refusals, [READ_NOT_SERVED]: READ_NOT_SERVED_CODE }).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)))
|
|
9
|
+
: refusals;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Value, Visibility } from "orthograph";
|
|
2
|
+
import type { ValueMeta } from "../types.ts";
|
|
3
|
+
/** The Solidity type of a value: `uint<bits>`, `int<bits>` for signed money, `bytes32` for an id, `string`, `address`; undefined for none. */
|
|
4
|
+
export declare const solidityOf: (meta: ValueMeta) => string | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* The Solidity type of a field of a chain-stored thing (its own, its events', its errors', its operations' payloads and
|
|
7
|
+
* answers), or undefined with the problem found: an email, a secret or private field, and a value with no Solidity type.
|
|
8
|
+
*/
|
|
9
|
+
export declare const chainField: (value: Value, meta: ValueMeta, visibility: Visibility, where: string, problems: Array<string>) => string | undefined;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** A minted id is its prefix, `_` and 16 characters (`language-effect/src/modules/ids/service.ts`), which must fit `bytes32`. */
|
|
2
|
+
const MAX_PREFIX = 15;
|
|
3
|
+
const widthOk = (bits) => Number.isInteger(bits) && bits >= 8 && bits <= 256 && bits % 8 === 0;
|
|
4
|
+
/** The Solidity type of a value: `uint<bits>`, `int<bits>` for signed money, `bytes32` for an id, `string`, `address`; undefined for none. */
|
|
5
|
+
export const solidityOf = (meta) => {
|
|
6
|
+
switch (meta.type) {
|
|
7
|
+
case "uint": return widthOk(meta.bits) ? `uint${meta.bits}` : undefined;
|
|
8
|
+
case "money": return widthOk(meta.bits) ? `${meta.signed ? "int" : "uint"}${meta.bits}` : undefined;
|
|
9
|
+
case "id": return meta.prefix.length <= MAX_PREFIX ? "bytes32" : undefined;
|
|
10
|
+
case "text": return "string";
|
|
11
|
+
case "account": return "address";
|
|
12
|
+
default: return undefined;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
/** Why a value has no Solidity type, or undefined when it has one. */
|
|
16
|
+
const noType = (meta) => {
|
|
17
|
+
switch (meta.type) {
|
|
18
|
+
case "uint":
|
|
19
|
+
case "money": return widthOk(meta.bits) ? undefined : `a ${meta.type === "uint" ? "uint" : "money"} of ${meta.bits} bits is no Solidity width: uint and int are 8 to 256 bits, in steps of 8`;
|
|
20
|
+
case "id": return meta.prefix.length <= MAX_PREFIX ? undefined : `the id prefix "${meta.prefix}" leaves no room to mint a suffix in 32 bytes (a minted id is the prefix, an underscore and 16 characters): use a prefix of at most ${MAX_PREFIX} characters`;
|
|
21
|
+
case "instant": return "an instant is ISO text, which a contract can't hold: keep the moment as a uint of seconds";
|
|
22
|
+
case "day": return "a day is ISO text, which a contract can't hold: keep the day as a uint of days since 1970-01-01";
|
|
23
|
+
case "decimal": return "a decimal is exact decimal text, which a contract can't hold: keep it as a scaled uint or int, as money keeps base units";
|
|
24
|
+
case "schema": return "a schema value declares no Solidity type, so a contract can't hold it";
|
|
25
|
+
default: return undefined;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const seen = (v) => v === "public" ? undefined : v === "secret" ? "is secret, and a public chain shows every field" : "is private to some roles, and a public chain shows every field";
|
|
29
|
+
/**
|
|
30
|
+
* The Solidity type of a field of a chain-stored thing (its own, its events', its errors', its operations' payloads and
|
|
31
|
+
* answers), or undefined with the problem found: an email, a secret or private field, and a value with no Solidity type.
|
|
32
|
+
*/
|
|
33
|
+
export const chainField = (value, meta, visibility, where, problems) => {
|
|
34
|
+
const before = problems.length;
|
|
35
|
+
if (value.format === "email")
|
|
36
|
+
problems.push(`${where}: is an email address, which a public chain would show to everyone: keep it in a database store`);
|
|
37
|
+
const hidden = seen(visibility);
|
|
38
|
+
if (hidden)
|
|
39
|
+
problems.push(`${where}: ${hidden}: keep it in a database store`);
|
|
40
|
+
const why = noType(meta);
|
|
41
|
+
if (why)
|
|
42
|
+
problems.push(`${where}: ${why}`);
|
|
43
|
+
if (meta.type === "money" && "currency" in meta.asset) {
|
|
44
|
+
problems.push(`${where}: money of the currency ${meta.asset.currency}: a currency has no chain binding, so a chain store counts a token (a fixed token asset, or money.of a field holding one)`);
|
|
45
|
+
}
|
|
46
|
+
return problems.length > before ? undefined : solidityOf(meta);
|
|
47
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { FromModel } from "../types.ts";
|
|
2
|
+
import type { AnyErrorClass, Thing } from "orthograph";
|
|
3
|
+
/** The refusal of a machine's move that starts from the wrong state. */
|
|
4
|
+
export declare const MACHINE_NUMBER = 900;
|
|
5
|
+
/** The runtime's refusal for a request key reused with another payload: its name, written once. */
|
|
6
|
+
export declare const IDEMPOTENCY_CONFLICT = "IdempotencyConflict";
|
|
7
|
+
/**
|
|
8
|
+
* The runtime's refusal of a read it does not serve yet: one that pins a field (`only`) or works its answer out (`derive`) is served from
|
|
9
|
+
* slice 7 of Goal 17. Present in a model only when some read is such a read, so a blueprint without one generates what it did.
|
|
10
|
+
*/
|
|
11
|
+
export declare const READ_NOT_SERVED = "ReadNotServed";
|
|
12
|
+
export declare const READ_NOT_SERVED_CODE = "AR-008";
|
|
13
|
+
/**
|
|
14
|
+
* Refusals the runtime makes, numbered in the order it gained them: who may call (before any handler runs), a request key
|
|
15
|
+
* reused with another payload, and the four a chain adds (M6-chain item 4): a signature past its deadline, a nonce that is
|
|
16
|
+
* not the account's next, a signature that does not check, and a create on an id that holds a row. All six names are
|
|
17
|
+
* reserved project-wide; the last four are in a project's refusals only when it has a chain store.
|
|
18
|
+
*/
|
|
19
|
+
export declare const RUNTIME_CODES: {
|
|
20
|
+
readonly NotAllowed: "AR-001";
|
|
21
|
+
readonly IdempotencyConflict: "AR-002";
|
|
22
|
+
readonly SignatureExpired: "AR-003";
|
|
23
|
+
readonly InvalidNonce: "AR-004";
|
|
24
|
+
readonly BadSignature: "AR-005";
|
|
25
|
+
readonly AlreadyExists: "AR-006";
|
|
26
|
+
};
|
|
27
|
+
/** The runtime's refusals a project has: every one when it has a chain store, else the two Effect's server raises. */
|
|
28
|
+
export declare const runtimeCodes: (chain: boolean) => Readonly<Record<string, string>>;
|
|
29
|
+
/** Codes held for refusals the Worker never returns, so no model lists them: the caller's door makes this one when a person says no (#466). Kept apart from RUNTIME_CODES, which feeds every model's `refusals`. */
|
|
30
|
+
export declare const RESERVED_CODES: {
|
|
31
|
+
readonly ApprovalDeclined: "AR-007";
|
|
32
|
+
};
|
|
33
|
+
/** The thing's own prefix, or the first two letters of its name. */
|
|
34
|
+
export declare const prefixOf: (name: string, thing: Thing) => string;
|
|
35
|
+
export declare const codeOf: (prefix: string, n: number) => string;
|
|
36
|
+
/** Each thing's prefix: valid, distinct from other things, and outside every runtime refusal's namespace. */
|
|
37
|
+
export declare const prefixes: (things: ReadonlyMap<string, {
|
|
38
|
+
readonly thing?: Thing | undefined;
|
|
39
|
+
readonly base?: string;
|
|
40
|
+
}>, problems: Array<string>) => ReadonlyMap<string, string>;
|
|
41
|
+
/** A thing's declared errors in declaration order. A class made before errors were numbered falls back to its name. */
|
|
42
|
+
export declare const inDeclarationOrder: (errors: ReadonlyMap<string, AnyErrorClass>) => ReadonlyArray<readonly [string, AnyErrorClass]>;
|
|
43
|
+
/** Every refusal's name and code in the project: the declared ones, each machine's, and the runtime's. */
|
|
44
|
+
export declare const refusalsOf: (things: Readonly<Record<string, {
|
|
45
|
+
readonly errors: Readonly<Record<string, {
|
|
46
|
+
readonly code: string;
|
|
47
|
+
}>>;
|
|
48
|
+
readonly machine: {
|
|
49
|
+
readonly notFrom: {
|
|
50
|
+
readonly error: string;
|
|
51
|
+
readonly code: string;
|
|
52
|
+
};
|
|
53
|
+
} | null;
|
|
54
|
+
readonly notFound?: {
|
|
55
|
+
readonly error: string;
|
|
56
|
+
readonly code: string;
|
|
57
|
+
};
|
|
58
|
+
readonly from: FromModel;
|
|
59
|
+
}>>, problems: Array<string>, chain?: boolean) => Readonly<Record<string, string>>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/** The refusal of a machine's move that starts from the wrong state. */
|
|
2
|
+
export const MACHINE_NUMBER = 900;
|
|
3
|
+
/** The runtime's refusal for a request key reused with another payload: its name, written once. */
|
|
4
|
+
export const IDEMPOTENCY_CONFLICT = "IdempotencyConflict";
|
|
5
|
+
/**
|
|
6
|
+
* The runtime's refusal of a read it does not serve yet: one that pins a field (`only`) or works its answer out (`derive`) is served from
|
|
7
|
+
* slice 7 of Goal 17. Present in a model only when some read is such a read, so a blueprint without one generates what it did.
|
|
8
|
+
*/
|
|
9
|
+
export const READ_NOT_SERVED = "ReadNotServed";
|
|
10
|
+
export const READ_NOT_SERVED_CODE = "AR-008";
|
|
11
|
+
/**
|
|
12
|
+
* Refusals the runtime makes, numbered in the order it gained them: who may call (before any handler runs), a request key
|
|
13
|
+
* reused with another payload, and the four a chain adds (M6-chain item 4): a signature past its deadline, a nonce that is
|
|
14
|
+
* not the account's next, a signature that does not check, and a create on an id that holds a row. All six names are
|
|
15
|
+
* reserved project-wide; the last four are in a project's refusals only when it has a chain store.
|
|
16
|
+
*/
|
|
17
|
+
export const RUNTIME_CODES = { NotAllowed: "AR-001", [IDEMPOTENCY_CONFLICT]: "AR-002", SignatureExpired: "AR-003", InvalidNonce: "AR-004", BadSignature: "AR-005", AlreadyExists: "AR-006" };
|
|
18
|
+
/** The runtime's refusals a project has: every one when it has a chain store, else the two Effect's server raises. */
|
|
19
|
+
export const runtimeCodes = (chain) => Object.fromEntries(Object.entries(RUNTIME_CODES).filter(([, code]) => chain || code <= "AR-002"));
|
|
20
|
+
/** Codes held for refusals the Worker never returns, so no model lists them: the caller's door makes this one when a person says no (#466). Kept apart from RUNTIME_CODES, which feeds every model's `refusals`. */
|
|
21
|
+
export const RESERVED_CODES = { ApprovalDeclined: "AR-007" };
|
|
22
|
+
const PREFIX = /^[A-Z]{2,4}$/;
|
|
23
|
+
/** The thing's own prefix, or the first two letters of its name. */
|
|
24
|
+
export const prefixOf = (name, thing) => thing.code ?? name.replace(/[^a-z]/g, "").slice(0, 2).toUpperCase();
|
|
25
|
+
export const codeOf = (prefix, n) => `${prefix}-${String(n).padStart(3, "0")}`;
|
|
26
|
+
/** Each thing's prefix: valid, distinct from other things, and outside every runtime refusal's namespace. */
|
|
27
|
+
export const prefixes = (things, problems) => {
|
|
28
|
+
const out = new Map();
|
|
29
|
+
const owner = new Map(Object.entries(RUNTIME_CODES).map(([name, code]) => [code.slice(0, code.indexOf("-")), { runtime: name }]));
|
|
30
|
+
for (const [name, t] of [...things].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
31
|
+
if (!t.thing)
|
|
32
|
+
continue;
|
|
33
|
+
const prefix = prefixOf(name, t.thing);
|
|
34
|
+
const file = t.base?.startsWith("blueprint/") === false ? `${t.base}/thing.js` : `blueprint/things/${name}/thing.ts`;
|
|
35
|
+
if (!PREFIX.test(prefix))
|
|
36
|
+
problems.push(`${file}: the refusal code prefix "${prefix}" must be 2 to 4 capital letters; set \`code\` on the thing`);
|
|
37
|
+
const other = owner.get(prefix);
|
|
38
|
+
if (other && "runtime" in other)
|
|
39
|
+
problems.push(`${file}: ${prefix}- is reserved for runtime refusals (${other.runtime}); set \`code\` on the thing to a distinct prefix`);
|
|
40
|
+
else if (other)
|
|
41
|
+
problems.push(`${file}: ${name} and ${other.thing} (${other.file}) would both have refusal codes starting ${prefix}-; give one of them a \`code\` of its own`);
|
|
42
|
+
else
|
|
43
|
+
owner.set(prefix, { thing: name, file });
|
|
44
|
+
out.set(name, prefix);
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
};
|
|
48
|
+
/** A thing's declared errors in declaration order. A class made before errors were numbered falls back to its name. */
|
|
49
|
+
export const inDeclarationOrder = (errors) => [...errors].sort(([an, a], [bn, b]) => (typeof a.sequence === "number" && typeof b.sequence === "number" ? a.sequence - b.sequence : an < bn ? -1 : an > bn ? 1 : 0));
|
|
50
|
+
/** Every refusal's name and code in the project: the declared ones, each machine's, and the runtime's. */
|
|
51
|
+
export const refusalsOf = (things, problems, chain = false) => {
|
|
52
|
+
const out = { ...runtimeCodes(chain) };
|
|
53
|
+
const owner = new Map(Object.entries(RUNTIME_CODES).map(([name, code]) => [code, name]));
|
|
54
|
+
const declaredIn = new Map();
|
|
55
|
+
const add = (name, code, where) => {
|
|
56
|
+
if (name in RESERVED_CODES)
|
|
57
|
+
problems.push(`${where}: ${name} is reserved (${RESERVED_CODES[name]}): the caller makes it when a person declines, so a thing cannot declare it`);
|
|
58
|
+
else if (name in RUNTIME_CODES || name in out)
|
|
59
|
+
problems.push(`${where}: the refusal ${name} is declared twice${declaredIn.has(name) ? `, also in ${declaredIn.get(name)}` : " (the runtime has the name)"}, and refusals are named for the whole project`);
|
|
60
|
+
else
|
|
61
|
+
declaredIn.set(name, where);
|
|
62
|
+
const other = owner.get(code);
|
|
63
|
+
if (other !== undefined)
|
|
64
|
+
problems.push(`${where}: ${other} and ${name} share ${code}; refusal codes must be unique (a machine reserves number ${MACHINE_NUMBER}); codes are never silently renumbered`);
|
|
65
|
+
else
|
|
66
|
+
owner.set(code, name);
|
|
67
|
+
out[name] = code;
|
|
68
|
+
};
|
|
69
|
+
for (const [thing, t] of Object.entries(things)) {
|
|
70
|
+
const at = (file) => (t.from.kind !== "app" ? `${t.from.package}/blueprint/things/${t.from.thing}/${file}.js` : `blueprint/things/${thing}/${file}.ts`);
|
|
71
|
+
for (const [name, e] of Object.entries(t.errors))
|
|
72
|
+
add(name, e.code, at("errors"));
|
|
73
|
+
if (t.machine)
|
|
74
|
+
add(t.machine.notFrom.error, t.machine.notFrom.code, at("machine"));
|
|
75
|
+
// A generated not-found refusal; one the thing declares under that name is already added, with its own code.
|
|
76
|
+
if (t.notFound && !(t.notFound.error in t.errors))
|
|
77
|
+
add(t.notFound.error, t.notFound.code, at("thing"));
|
|
78
|
+
}
|
|
79
|
+
return Object.fromEntries(Object.entries(out).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
80
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Derive, Thing } from "orthograph";
|
|
2
|
+
import type { FieldsModel } from "../types.ts";
|
|
3
|
+
export interface DeriveContext {
|
|
4
|
+
readonly thingNames: ReadonlyMap<Thing, string>;
|
|
5
|
+
readonly problems: Array<string>;
|
|
6
|
+
}
|
|
7
|
+
export declare const deriveModel: (derive: Derive, file: string, adds: FieldsModel, ctx: DeriveContext) => {
|
|
8
|
+
looks: any;
|
|
9
|
+
adds: Readonly<Record<string, import("../types.ts").FieldModel>>;
|
|
10
|
+
hash: string;
|
|
11
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { hashOf } from "./hash.js";
|
|
2
|
+
/** Whether a field holds `root`'s id: followed back through the fields it was taken from, as the runtime's role check does. */
|
|
3
|
+
const holdsIdOf = (field, root) => {
|
|
4
|
+
for (let f = field; f; f = f.from)
|
|
5
|
+
if (f.key === "id")
|
|
6
|
+
return f.of === root;
|
|
7
|
+
return false;
|
|
8
|
+
};
|
|
9
|
+
export const deriveModel = (derive, file, adds, ctx) => {
|
|
10
|
+
const root = ctx.thingNames.get(derive.root) ?? "?";
|
|
11
|
+
const looks = Object.fromEntries(Object.entries(derive.looks).map(([name, look]) => {
|
|
12
|
+
if (look.kind === "problems")
|
|
13
|
+
return [name, { kind: "problems" }];
|
|
14
|
+
const thing = ctx.thingNames.get(look.thing) ?? "?";
|
|
15
|
+
if (!holdsIdOf(look.by, derive.root))
|
|
16
|
+
ctx.problems.push(`${file}: look ${name} follows ${thing}.${look.by.key}, which does not hold ${root}'s id: a look's \`by\` is a field taken from ${root}.fields.id`);
|
|
17
|
+
if (look.thing.lives === "actor")
|
|
18
|
+
ctx.problems.push(`${file}: look ${name} looks at ${thing}, which lives as actors: a look reads rows, and an actor has none to list`);
|
|
19
|
+
if (!Number.isInteger(look.max) || look.max < 1 || look.max > 100)
|
|
20
|
+
ctx.problems.push(`${file}: look ${name}: max ${look.max} is outside 1 to 100`);
|
|
21
|
+
return [name, {
|
|
22
|
+
thing, by: look.by.key, ...(look.state === undefined ? {} : { state: look.state }), ...(look.where === undefined ? {} : { where: look.where }),
|
|
23
|
+
take: look.take, max: look.max,
|
|
24
|
+
}];
|
|
25
|
+
}));
|
|
26
|
+
for (const name of Object.keys(derive.adds)) {
|
|
27
|
+
if (name in derive.root.fields)
|
|
28
|
+
ctx.problems.push(`${file}: derive adds ${name}, which is a stored field of ${root}: a derived field has a name of its own`);
|
|
29
|
+
}
|
|
30
|
+
return { looks, adds, hash: hashOf({ looks, is: derive.is.toString() }) };
|
|
31
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Model, MountModel } from "../types.ts";
|
|
2
|
+
import type { Lock } from "#modules/lock/types.js";
|
|
3
|
+
import { type Layout } from "./layout.ts";
|
|
4
|
+
export declare const describe: (layout: Layout, before?: Lock, mounts?: Readonly<Record<string, MountModel>>) => {
|
|
5
|
+
model: Model;
|
|
6
|
+
problems: Array<string>;
|
|
7
|
+
lock: Lock;
|
|
8
|
+
};
|