@pritzlaff/nano-core 0.2.0 → 0.5.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/op.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Ops: an action (input → output) or an event (data out, its `output`). An op is a signature, unnamed: the interface
3
+ * names it by key, and its coordinate (hash of name and schemas) is computed there.
4
+ *
5
+ * const api = Interface.define('acme.Db', { get: Op.action(Key, Value) }) // api.ops.get.name: 'get'
6
+ * fits(Op.event(Num), Op.action(Num, Nothing)) // true
7
+ */
8
+ import type { TSchema } from 'typebox';
9
+ import { type SchemaDef } from './schema.js';
10
+ export type OpKind = 'action' | 'event';
11
+ /** An op as an interface holds it: named by its key, with its coordinate. */
12
+ export type Op<I extends TSchema | undefined = TSchema | undefined, O extends TSchema | undefined = TSchema | undefined, K extends OpKind = OpKind> = {
13
+ readonly name: string;
14
+ readonly kind: K;
15
+ readonly input?: SchemaDef<Exclude<I, undefined>>;
16
+ readonly output?: SchemaDef<Exclude<O, undefined>>;
17
+ readonly coord: string;
18
+ readonly description?: string;
19
+ };
20
+ type Opts = {
21
+ description?: string;
22
+ };
23
+ /** A `SchemaDef`, or a bare TypeBox schema (defined here, unlabelled). */
24
+ type Given = SchemaDef | TSchema;
25
+ type Of<G> = G extends {
26
+ readonly schema: infer S extends TSchema;
27
+ readonly hash: string;
28
+ } ? S : G extends TSchema ? G : never;
29
+ type Unnamed<I extends TSchema | undefined, O extends TSchema | undefined, K extends OpKind> = Omit<Op<I, O, K>, 'name' | 'coord'>;
30
+ export declare const Op: {
31
+ action: <I extends Given, O extends Given>(input: I, output: O, o?: Opts) => Unnamed<Of<I>, Of<O>, 'action'>;
32
+ event: <O extends Given>(data: O, o?: Opts) => Unnamed<undefined, Of<O>, 'event'>;
33
+ };
34
+ /** The source's data (an event's, or an action's result) is the target action's input, or the action takes anything. */
35
+ export declare function fits(source: Pick<Op, 'output'>, action: Pick<Op, 'input'>): boolean;
36
+ export {};
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@pritzlaff/nano-core",
3
- "version": "0.2.0",
3
+ "version": "0.5.0",
4
+ "description": "The nano model: schemas, ops, interfaces, pods, runtimes, deployments and the one live Application.",
4
5
  "license": "MIT",
5
6
  "type": "module",
6
7
  "repository": {
7
8
  "type": "git",
8
9
  "url": "git+https://github.com/aaronp/nano.git",
9
- "directory": "packages/core"
10
+ "directory": "packages/nano"
10
11
  },
11
12
  "exports": {
12
13
  ".": {
@@ -17,6 +18,10 @@
17
18
  "types": "./index.d.ts",
18
19
  "module": "./index.js",
19
20
  "sideEffects": false,
21
+ "dependencies": {
22
+ "@noble/hashes": "^1.8.0",
23
+ "json-canonicalize": "^1.0.6"
24
+ },
20
25
  "peerDependencies": {
21
26
  "typebox": "^1.4.0"
22
27
  },
package/plain.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Plain TypeScript on both sides of nano (not exported from the package): a plain object's methods become an
3
+ * interface's handlers, and a client becomes a plain object again (`Interface.Type`).
4
+ *
5
+ * provide(KV, handlers(KV, inMemory())) // a value is success (nothing is null), a throw is threw
6
+ * const store = plain<KV>(KV, () => ctx.one(KV)) // await store.get('a'): a Fail throws an Error carrying it
7
+ */
8
+ import { type Handlers, type Provided } from './implementation.js';
9
+ import { type Interface } from './iface.js';
10
+ import type { Op } from './op.js';
11
+ import { type Fail, type NanoResult } from './result.js';
12
+ /** Each action of `i` calls the method of the same name (with the invocation context): its value is `success` (nothing is `null`), a throw is `threw`. */
13
+ export declare function handlers<I extends Interface>(i: I, obj: object): Handlers<I>;
14
+ /** What `plain` reads at each use: the client's calls, and its emissions of an op (`on`, or the host's `subscribe`). */
15
+ type Reached = {
16
+ call: object;
17
+ on(op: Op): AsyncIterable<{
18
+ output: unknown;
19
+ }>;
20
+ };
21
+ /** An `Error` carrying `fail` (`err.fail`): how nano throws a `Fail` wherever it throws one. */
22
+ export declare const thrown: (fail: Fail) => Error & {
23
+ fail: Fail;
24
+ };
25
+ /** A plain object over a client, resolved on every call (so a rewired edge is followed): a `Fail` throws an Error carrying
26
+ * it. An event of `i` is a method giving the `output`s emitted from the call on (it connects at the call; see iterate.ts). */
27
+ export declare function plain<T>(i: Interface | undefined, client: () => NanoResult<Reached>): T;
28
+ /** A plain object for `i` as a part: its actions' methods as handlers, its events' methods as sources. */
29
+ export declare function lift(i: Interface, obj: object): Provided;
30
+ export {};
package/pod.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Pods: a description of one component, as plain data — an implementation's coordinate, its config, and whether it is
3
+ * built as soon as it is in an application (eager) or on its first call (lazy). No state, methods or instance.
4
+ *
5
+ * Pod.lazy(counter, { config: { start: 0 } }) // { implementation: 'acme/counter', config: { start: 0 }, eager: false }
6
+ * Pod.eager(ticker) // { implementation: 'acme/ticker', eager: true }
7
+ */
8
+ import type { Implementation } from './implementation.js';
9
+ export type Pod = {
10
+ implementation: string;
11
+ config?: unknown;
12
+ eager: boolean;
13
+ /** Where it runs; absent means `host`. */
14
+ isolation?: 'host' | 'sandbox' | 'isolated-sandbox' | 'home-origin';
15
+ };
16
+ export declare const Pod: {
17
+ lazy: (impl: Implementation, opts?: {
18
+ config?: unknown;
19
+ isolation?: Pod['isolation'];
20
+ }) => Pod;
21
+ eager: (impl: Implementation, opts?: {
22
+ config?: unknown;
23
+ isolation?: Pod['isolation'];
24
+ }) => Pod;
25
+ };
package/pods.d.ts ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Pods: which components exist, as a checked, immutable lookup of pod descriptions by name. Building one builds
3
+ * nothing; `select` gives a scope over the same descriptions.
4
+ *
5
+ * const pods = Pods.create({ a: Pod.lazy(counter), b: Pod.eager(ticker) }, { registry, tags: { a: ['primary'] } }) // NanoResult<Pods>
6
+ * pods.value.select(['a']) // success(a Pods naming only 'a'); an unknown name is not-found
7
+ */
8
+ import { Type, type TSchema } from 'typebox';
9
+ import type { Pod } from './pod.js';
10
+ import type { Registry } from './registry.js';
11
+ import { type NanoResult } from './result.js';
12
+ export type PodDeployment = Record<string, Pod>;
13
+ export type Pods = {
14
+ names(): string[];
15
+ get(name: string): Pod | undefined;
16
+ select(names: string[]): NanoResult<Pods>;
17
+ deployment(): PodDeployment;
18
+ };
19
+ /** Pod names and runtime ids. */
20
+ export declare const Id: Type.TString;
21
+ declare const Tags: Type.TRecord<"^.*$", Type.TArray<Type.TString>>;
22
+ /** The first way `d` breaks `schema`, as a reason naming `what`; or undefined. */
23
+ export declare function shapeError(schema: TSchema, d: unknown, what: string): string | undefined;
24
+ type Tags = Record<string, string[]>;
25
+ /**
26
+ * Where a `Pods` comes from: the registry it was checked against, the `Pods.create` result it was selected from, and
27
+ * the tags of its pods, when `Pods.create` was given tags; and its descriptions by name, to read without copying.
28
+ */
29
+ type Origin = {
30
+ readonly registry: Registry;
31
+ readonly root: Pods;
32
+ readonly tags?: Tags;
33
+ readonly pods: ReadonlyMap<string, Pod>;
34
+ };
35
+ export declare const originOf: (pods: Pods) => Origin | undefined;
36
+ /** `d` as `Pods`, if its names, implementations, configs, size and `tags` (by pod name, each a pod of `d`) check out. Never throws. */
37
+ declare function create(d: unknown, opts: {
38
+ registry: Registry;
39
+ tags?: Tags;
40
+ }): NanoResult<Pods>;
41
+ export declare const Pods: {
42
+ create: typeof create;
43
+ };
44
+ export {};
package/policy.d.ts ADDED
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Policy: the yes/no check a runtime makes before it delivers a message or adds a subscription.
3
+ *
4
+ * const p = Policies.allowAll()
5
+ * p.isAllowed(ask) // { approved: true }
6
+ * PolicyRules([{ allow: 'call', outside: true, to: 'acme.Kv' }, { deny: '*' }]).isAllowed(ask) // first match wins
7
+ */
8
+ import { Type, type Static } from 'typebox';
9
+ export type Verdict = {
10
+ approved: true;
11
+ } | {
12
+ approved: false;
13
+ reason: string;
14
+ };
15
+ /** What a policy is asked: a message's envelope. `outside` is true when `from` is not placed in `runtime` (the host, or another runtime's pod). */
16
+ export type Ask = {
17
+ runtime: string;
18
+ kind: 'call' | 'pipe' | 'on' | 'subscribe' | 'unsubscribe' | 'lifecycle' | 'start';
19
+ from: string;
20
+ to: string;
21
+ port?: string;
22
+ outside: boolean;
23
+ data?: unknown;
24
+ };
25
+ export type Policy = {
26
+ isAllowed(ask: Ask): Verdict;
27
+ };
28
+ /** `allowAll()` is one shared policy, so saving can tell it apart from a custom one. */
29
+ export declare const Policies: {
30
+ allowAll: () => Policy;
31
+ };
32
+ /**
33
+ * A rule: `allow` or `deny` these kinds of message, from a pod or 'host' (`from`), to a target named by its resource
34
+ * type: `toPod` (a pod name), `toInterface` (an interface coordinate: any of its ops), `toPort` (a port coordinate,
35
+ * `iface/op`: that op only). Each field absent or '*' matches anything; all given fields must match.
36
+ */
37
+ export declare const Rule: Type.TUnion<[Type.TObject<{
38
+ from: Type.TOptional<Type.TString>;
39
+ toPod: Type.TOptional<Type.TString>;
40
+ toInterface: Type.TOptional<Type.TString>;
41
+ toPort: Type.TOptional<Type.TString>;
42
+ outside: Type.TOptional<Type.TBoolean>;
43
+ allow: Type.TUnion<[Type.TUnion<[Type.TLiteral<"call">, Type.TLiteral<"pipe">, Type.TLiteral<"on">, Type.TLiteral<"subscribe">, Type.TLiteral<"unsubscribe">, Type.TLiteral<"lifecycle">, Type.TLiteral<"start">, Type.TLiteral<"*">]>, Type.TArray<Type.TUnion<[Type.TLiteral<"call">, Type.TLiteral<"pipe">, Type.TLiteral<"on">, Type.TLiteral<"subscribe">, Type.TLiteral<"unsubscribe">, Type.TLiteral<"lifecycle">, Type.TLiteral<"start">, Type.TLiteral<"*">]>>]>;
44
+ }>, Type.TObject<{
45
+ from: Type.TOptional<Type.TString>;
46
+ toPod: Type.TOptional<Type.TString>;
47
+ toInterface: Type.TOptional<Type.TString>;
48
+ toPort: Type.TOptional<Type.TString>;
49
+ outside: Type.TOptional<Type.TBoolean>;
50
+ deny: Type.TUnion<[Type.TUnion<[Type.TLiteral<"call">, Type.TLiteral<"pipe">, Type.TLiteral<"on">, Type.TLiteral<"subscribe">, Type.TLiteral<"unsubscribe">, Type.TLiteral<"lifecycle">, Type.TLiteral<"start">, Type.TLiteral<"*">]>, Type.TArray<Type.TUnion<[Type.TLiteral<"call">, Type.TLiteral<"pipe">, Type.TLiteral<"on">, Type.TLiteral<"subscribe">, Type.TLiteral<"unsubscribe">, Type.TLiteral<"lifecycle">, Type.TLiteral<"start">, Type.TLiteral<"*">]>>]>;
51
+ }>]>;
52
+ export type Rule = Static<typeof Rule>;
53
+ /**
54
+ * The first rule matching the ask decides; none denies (`no rule allows it`); a deny's reason is `rules[i]`.
55
+ * Compiled once: rules are indexed by `from` (a named caller's rules, and those for any caller), so an ask looks only
56
+ * at rules that could match its caller, still in their order.
57
+ */
58
+ export declare function PolicyRules(rules: Rule[]): Policy;
package/provider.d.ts ADDED
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Providers: a namespace of implementations, plus free-form metadata.
3
+ *
4
+ * const mem = Provider.define('acme/mem', [memDb, memLog], { author: 'acme' })
5
+ * mem.implementations.get('acme/mem/db') // memDb
6
+ *
7
+ * The package's `Coord` is layer 1's with a namespace that also builds (layer 2): `ns.interface(name, ops)` is
8
+ * `Interface.define(ns.interfaceId(name), ops)` and `ns.provider(impls, meta?)` is `Provider.define(ns, impls, meta)`.
9
+ *
10
+ * const acme = Coord.namespace('acme'); const DB = acme.interface('DB', { get: Op.action(Key, Value) })
11
+ */
12
+ import { type Namespace } from './coord.js';
13
+ import { Interface } from './iface.js';
14
+ import type { Implementation } from './implementation.js';
15
+ export type Provider = {
16
+ readonly coord: string;
17
+ readonly implementations: ReadonlyMap<string, Implementation>;
18
+ readonly meta: Readonly<Record<string, string>>;
19
+ };
20
+ declare function define(at: string | Namespace, implementations: Implementation[], meta?: Record<string, string>): Provider;
21
+ /** `Provider.define(ns, implementations, meta?)`: `ns` a `Namespace` or its string. */
22
+ export declare const Provider: {
23
+ define: typeof define;
24
+ };
25
+ type Ops = Parameters<typeof Interface.define>[1];
26
+ declare function namespace(name: string): Readonly<{
27
+ kind: 'namespace';
28
+ name: string;
29
+ interfaceId: (n: string) => ReturnType<(name: string) => import("./coord.js").InterfaceCoord>;
30
+ implementationId: (n: string) => ReturnType<(name: string) => import("./coord.js").ImplCoord>;
31
+ interface: <U extends Ops>(n: string, ops: U) => Interface<{ [K in keyof U]: U[K] & {
32
+ readonly name: K & string;
33
+ readonly coord: string;
34
+ }; }>;
35
+ provider: (implementations: Implementation[], meta?: Record<string, string>) => Provider;
36
+ }>;
37
+ /** Layer 1's `Coord`, its `namespace` also building interfaces and providers. */
38
+ export declare const Coord: {
39
+ port: (i: import("./coord.js").InterfaceCoord, op: string) => import("./coord.js").PortCoord;
40
+ runtime: (id: string) => import("./coord.js").RuntimeCoord;
41
+ pod: (implementation: import("./coord.js").ImplCoord, runtime: import("./coord.js").RuntimeCoord, name: string) => import("./coord.js").PodCoord;
42
+ parse: <K extends keyof {
43
+ namespace: Namespace;
44
+ interface: import("./coord.js").InterfaceCoord;
45
+ implementation: import("./coord.js").ImplCoord;
46
+ port: import("./coord.js").PortCoord;
47
+ runtime: import("./coord.js").RuntimeCoord;
48
+ pod: import("./coord.js").PodCoord;
49
+ }>(kind: K, s: string) => {
50
+ namespace: Namespace;
51
+ interface: import("./coord.js").InterfaceCoord;
52
+ implementation: import("./coord.js").ImplCoord;
53
+ port: import("./coord.js").PortCoord;
54
+ runtime: import("./coord.js").RuntimeCoord;
55
+ pod: import("./coord.js").PodCoord;
56
+ }[K];
57
+ namespace: typeof namespace;
58
+ };
59
+ export {};
package/registry.d.ts CHANGED
@@ -1,30 +1,63 @@
1
- import type { Instance } from './component.js';
2
- import { type Delta, type Op, type Result, type Subscription } from './data.js';
3
- /** What a registry holds, as plain data. */
4
- export type Snapshot = {
5
- id: string;
6
- instances: {
7
- id: string;
8
- ops: Op[];
1
+ /**
2
+ * The registry: a pool of providers, answering lookups by coord in O(1) and describing itself as a plain-data manifest.
3
+ *
4
+ * const r = Registry.create()
5
+ * r.add(mem) // NanoResult<void>; fails `invalid-input` on a clash
6
+ * r.implementationsOf('acme.Db') // [memDb]
7
+ */
8
+ import type { Interface } from './iface.js';
9
+ import type { Implementation } from './implementation.js';
10
+ import type { Op, OpKind } from './op.js';
11
+ import type { Provider } from './provider.js';
12
+ import { type NanoResult } from './result.js';
13
+ import type { SelectorData } from './selector.js';
14
+ export type Manifest = {
15
+ providers: {
16
+ coord: string;
17
+ implementations: string[];
18
+ }[];
19
+ /** Each dependency's selector written out: every coordinate as its string. */
20
+ implementations: {
21
+ coord: string;
22
+ provides: string[];
23
+ dependencies: Record<string, {
24
+ [K in keyof SelectorData]: K extends 'tags' ? string[] : string;
25
+ }>;
26
+ }[];
27
+ interfaces: {
28
+ coord: string;
29
+ ops: {
30
+ name: string;
31
+ kind: OpKind;
32
+ coord: string;
33
+ input?: string;
34
+ output?: string;
35
+ }[];
9
36
  }[];
10
- subscriptions: Subscription[];
11
37
  };
12
- /** Stores instances and subscriptions, and tells its listeners about every change as a Delta. */
13
38
  export type Registry = {
14
- id: string;
15
- get(id: string): Instance | undefined;
16
- add(id: string, inst: Instance): Result<void>;
17
- remove(id: string): Result<void>;
18
- subscribe(s: Omit<Subscription, 'id'>): Result<Subscription>;
19
- unsubscribe(id: string): Result<void>;
20
- /** The subscriptions a publish of `op` by `source` reaches. */
21
- matching(source: string, op: string): Subscription[];
22
- snapshot(): Snapshot;
23
- /** Records that an instance's ops changed; refused for an unknown instance. */
24
- announce(instance: string, type: 'op-added' | 'op-removed', op: Op): Result<void>;
25
- listen(fn: (d: Delta) => void): () => void;
39
+ add(p: Provider): NanoResult<void>;
40
+ manifest(): Manifest;
41
+ provider(coord: string): Provider | undefined;
42
+ implementation(coord: string): Implementation | undefined;
43
+ implementationsOf(ifaceCoord: string): readonly Implementation[];
44
+ interface(coord: string): Interface | undefined;
45
+ op(coord: string): Op | undefined;
26
46
  };
27
- /** Makes an empty registry. */
28
- export declare const Registries: {
29
- create(id: string): Registry;
47
+ declare function create(): Registry;
48
+ /**
49
+ * `Registry.load(modules, base)`: import each module (a URL relative to `base`), add its default `Provider` to a new
50
+ * registry, and return each module's namespace under its key. The first failure is the result, never a throw:
51
+ * `not-found` (the import failed), `invalid-input` (no default Provider, or the registry refused it).
52
+ * The modules are code that will run: load only trusted ones.
53
+ */
54
+ declare function load(modules: Record<string, string>, base: string | URL): Promise<NanoResult<{
55
+ registry: Registry;
56
+ exports: Record<string, Record<string, unknown>>;
57
+ }>>;
58
+ /** `Registry.create()`; `Registry.load(modules, base)`. */
59
+ export declare const Registry: {
60
+ create: typeof create;
61
+ load: typeof load;
30
62
  };
63
+ export {};
package/result.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * NanoResult: the value-or-Fail result every platform call returns instead of throwing.
3
+ *
4
+ * const r = await client.get('k') // NanoResult<string>
5
+ * if (!r.ok) console.log(r.fail.type) // 'denied', 'timeout', ...
6
+ */
7
+ export type FailType = 'threw' | 'timeout' | 'denied' | 'invalid-input' | 'invalid-output' | 'unknown-op' | 'not-unique' | 'not-found';
8
+ export type Fail = {
9
+ type: FailType;
10
+ reason: string;
11
+ coord?: string;
12
+ };
13
+ export type NanoResult<T> = {
14
+ ok: true;
15
+ value: T;
16
+ } | {
17
+ ok: false;
18
+ fail: Fail;
19
+ };
20
+ export declare const success: <T>(value: T) => NanoResult<T>;
21
+ export declare const failure: (type: FailType, reason: string, coord?: string) => NanoResult<never>;
22
+ export declare function isNanoResult(v: unknown): v is NanoResult<unknown>;
package/runtime.d.ts CHANGED
@@ -1,65 +1,43 @@
1
- import { Type } from 'typebox';
2
- import { Ops, type Coord, type Delta, type Event, type Result } from './data.js';
3
- import type { Registry } from './registry.js';
4
- /** Calls and publishes by name within one registry. In-process, remote or policy-checked, it is these two functions. */
5
- export type Runtime = {
6
- registry: Registry;
7
- call(from: Coord, target: string, op: string, arg: unknown): Promise<Result>;
8
- /** Outer Result: was the publish allowed. Inner: one Result per delivery. */
9
- notify(e: Event): Promise<Result<Result[]>>;
10
- /** Tells `fn` about every call, publish and delivery as it ends. Returns a function that stops telling it. */
11
- listen(fn: (t: Trace) => void): () => void;
12
- };
13
- /** One message through a runtime, as data: what a histogram, a live wire view or a replay is built from. */
14
- export type Trace = {
1
+ import type { Implementation } from './implementation.js';
2
+ import type { Op } from './op.js';
3
+ import { Rule } from './policy.js';
4
+ import { type Pods } from './pods.js';
5
+ import { type NanoResult } from './result.js';
6
+ export type Subscription = {
15
7
  kind: 'call';
16
- time: number;
17
- from: Coord;
18
- target: string;
19
- op: string;
20
- data: unknown;
21
- result: Result;
8
+ from: string;
9
+ to: string;
10
+ ops?: {
11
+ source: string;
12
+ target: string;
13
+ }[];
22
14
  } | {
23
- kind: 'publish';
24
- time: number;
25
- from: Coord;
26
- op: string;
27
- data: unknown;
28
- result: Result<Result[]>;
29
- } | {
30
- kind: 'delivery';
31
- time: number;
32
- from: Coord;
15
+ kind: 'pipe';
16
+ from: string;
17
+ source: string;
18
+ to: string;
33
19
  target: string;
34
- op: string;
35
- data: unknown;
36
- subscription: string;
37
- result: Result;
38
20
  };
39
- /** Rules around a runtime: takes the runtime and returns one that enforces them. */
40
- export type Policy = (inner: Runtime) => Runtime;
41
- declare const any: Type.TUnknown;
42
- /** The registry's own ops: change it, read it, and the changes it publishes. */
43
- export declare const RegistryOps: {
44
- add: import("./data.js").Op<Type.TObject<{
45
- id: Type.TString;
46
- instance: Type.TUnknown;
47
- }>, Type.TUnknown>;
48
- remove: import("./data.js").Op<Type.TString, Type.TUnknown>;
49
- subscribe: import("./data.js").Op<Type.TObject<{
50
- source: Type.TString;
51
- op: Type.TString;
52
- listener: Type.TString;
53
- target: Type.TString;
54
- }>, Type.TUnknown>;
55
- unsubscribe: import("./data.js").Op<Type.TString, Type.TUnknown>;
56
- snapshot: import("./data.js").Op<Type.TNull, Type.TUnknown>;
57
- deltas: Record<Delta['type'], ReturnType<typeof Ops.source<typeof any>>>;
21
+ export type RuntimeDeployment = {
22
+ pods: string[];
23
+ subscriptions: Subscription[];
24
+ policy?: {
25
+ kind: 'allow-all';
26
+ } | {
27
+ kind: 'rules';
28
+ rules: Rule[];
29
+ };
30
+ };
31
+ export type Runtime = {
32
+ readonly id: string;
33
+ readonly pods: Pods;
34
+ deployment(): RuntimeDeployment;
58
35
  };
59
- /** Makes the runtime for a registry, adds the registry to itself as 'registry', and publishes its changes. */
60
- declare function create(registry: Registry, policy?: Policy): Runtime;
61
- /** Makes runtimes. */
62
- export declare const Runtimes: {
36
+ export declare const opAt: (impl: Implementation, p: string) => Op | undefined;
37
+ export declare const isRuntime: (r: unknown) => r is Runtime;
38
+ /** Runtime `id` placing and wiring pods of `pods`, if `d` checks out. Never throws. */
39
+ declare function create(id: string, d: unknown, pods: Pods): NanoResult<Runtime>;
40
+ export declare const Runtime: {
63
41
  create: typeof create;
64
42
  };
65
43
  export {};
package/schema.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ import type { Static, TSchema } from 'typebox';
2
+ export type SchemaDef<S extends TSchema = TSchema> = S & {
3
+ readonly label?: string;
4
+ readonly schema: S;
5
+ readonly hash: string;
6
+ check(v: unknown): v is Static<S>;
7
+ encode(v: Static<S>): string;
8
+ decode(json: string): Static<S> | undefined;
9
+ };
10
+ /** RFC 8785 canonical JSON. */
11
+ export declare const canonical: (value: unknown) => string;
12
+ /** blake3 of the UTF-8 text, lower-case hex. */
13
+ export declare const hashOf: (text: string) => string;
14
+ /** Plain JSON data: null, booleans, finite numbers, strings, arrays and plain objects of them, without cycles. */
15
+ export declare function isJson(v: unknown, seen?: Set<unknown>): boolean;
16
+ export declare const canonicalSchema: (schema: TSchema) => string;
17
+ /** A copy of `schema` (non-enumerable keys kept), with the definition's members added out of sight of hashing. */
18
+ declare function define<S extends TSchema>(schema: S, label?: string): SchemaDef<S>;
19
+ /** `Schema.define(typeboxSchema, label?)`. */
20
+ export declare const Schema: {
21
+ define: typeof define;
22
+ };
23
+ export {};
package/selector.d.ts ADDED
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Selectors: which call subscriptions a call may use — an interface, or data narrowing by tags, implementation,
3
+ * provider, or the runtime the subscription is in.
4
+ *
5
+ * selectorData(DB) // its `implements` is DB's InterfaceCoord, acme.DB
6
+ * matches({ implements: acme.interfaceId('DB'), tags: ['primary'] }, target)
7
+ * Selector.of(DB, { runtime: Coord.runtime('v2') }) // still DB (typed clients survive), plus implements and runtime
8
+ *
9
+ * The fields are coordinates; written out (JSON, the registry manifest) each is its string.
10
+ */
11
+ import { type ImplCoord, type InterfaceCoord, type Namespace, type RuntimeCoord } from './coord.js';
12
+ import type { Interface } from './iface.js';
13
+ export type SelectorData = {
14
+ implements?: InterfaceCoord;
15
+ tags?: string[];
16
+ implementation?: ImplCoord;
17
+ provider?: Namespace;
18
+ runtime?: RuntimeCoord;
19
+ };
20
+ export type Selector = Interface | SelectorData;
21
+ /** A pod as reached over one call subscription: its name, the runtime of that subscription, and what it is. */
22
+ export type Target = {
23
+ name: string;
24
+ runtime: string;
25
+ implementation: string;
26
+ provider: string;
27
+ provides: ReadonlySet<string>;
28
+ tags: ReadonlySet<string>;
29
+ };
30
+ /** An interface (possibly narrowed by `Selector.of`) or plain selector data, as plain data. */
31
+ export declare function selectorData(s: Selector): SelectorData;
32
+ /** An interface narrowed by tags, implementation, provider or runtime; still the interface, so a call's client stays typed. */
33
+ declare const of: <I extends Interface>(i: I, s?: Omit<SelectorData, 'implements'>) => I & SelectorData;
34
+ /** Every given field holds; all tags must be present on the target. */
35
+ export declare const matches: (s: SelectorData, t: Target) => boolean;
36
+ /** `Selector.of(interface, narrowing?)`. */
37
+ export declare const Selector: {
38
+ of: typeof of;
39
+ };
40
+ export {};
package/component.d.ts DELETED
@@ -1,42 +0,0 @@
1
- import type { Static, TSchema } from 'typebox';
2
- import { type Op, type Result } from './data.js';
3
- import { type Environment } from './env.js';
4
- type StaticOf<T> = T extends TSchema ? Static<T> : void;
5
- /** What an action does: given the instance, the argument and the environment, produce the output. */
6
- export type Run<A, I = any, O = any> = (instance: A, arg: I, env: Environment) => O | Promise<O>;
7
- /** An op bound to a run. With no run, it is a source that is only published by `notify`. */
8
- export type Action<A> = {
9
- op: Op;
10
- run?: Run<A>;
11
- emits?: Op[];
12
- };
13
- /** A definition: how to make an A, and the actions over it. */
14
- export type Component<A = any> = {
15
- id: {
16
- ns: string;
17
- name: string;
18
- };
19
- make(env: Environment): A;
20
- actions: Map<string, Action<A>>;
21
- action<I extends TSchema | undefined, O extends TSchema | undefined>(op: Op<I, O>, run?: Run<A, StaticOf<I>, StaticOf<O>>, opts?: {
22
- emits?: Op[];
23
- }): 'added' | 'replaced';
24
- };
25
- /** One made value, with its own copy of the actions and its environment. */
26
- export type Instance<A = any> = {
27
- component: Component<A>;
28
- value: A;
29
- actions: Map<string, Action<A>>;
30
- env: Environment;
31
- };
32
- /** Makes components, instances of them, and invokes their actions. */
33
- export declare const Components: {
34
- create<A>(id: {
35
- ns: string;
36
- name: string;
37
- }, make: (env: Environment) => A): Component<A>;
38
- instantiate<A>(component: Component<A>): Instance<A>;
39
- /** Runs the action, then publishes each op it emits with that op's own reading. The action stands if an emit fails. */
40
- invoke(inst: Instance, key: string, arg: unknown): Promise<Result>;
41
- };
42
- export {};