@peers-app/peers-sdk 0.26.6 → 0.27.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 (182) hide show
  1. package/README.md +3 -1
  2. package/bin/peers-package.js +13 -0
  3. package/define/package.json +4 -0
  4. package/dist/contracts/__tests__/action-dispatch.test.js +56 -76
  5. package/dist/contracts/__tests__/also-implements.test.d.ts +1 -0
  6. package/dist/contracts/__tests__/also-implements.test.js +110 -0
  7. package/dist/contracts/__tests__/builder.test.js +45 -0
  8. package/dist/contracts/__tests__/contract-resolvers.test.js +134 -4
  9. package/dist/contracts/__tests__/frozen-contracts.test.d.ts +1 -0
  10. package/dist/contracts/__tests__/frozen-contracts.test.js +191 -0
  11. package/dist/contracts/__tests__/generic-tool-dispatch.test.js +13 -56
  12. package/dist/contracts/__tests__/host-contract-client.test.d.ts +1 -0
  13. package/dist/contracts/__tests__/host-contract-client.test.js +793 -0
  14. package/dist/contracts/__tests__/host-contract-transport.test.js +15 -0
  15. package/dist/contracts/__tests__/registry.test.js +68 -0
  16. package/dist/contracts/__tests__/target-dispatch.d.ts +11 -0
  17. package/dist/contracts/__tests__/target-dispatch.js +24 -0
  18. package/dist/contracts/__tests__/tool-catalog-service.test.js +37 -1
  19. package/dist/contracts/__tests__/tool-catalog.test.js +12 -3
  20. package/dist/contracts/__tests__/validate.test.js +177 -7
  21. package/dist/contracts/__tests__/workflow-context.test.js +0 -11
  22. package/dist/contracts/builder.d.ts +16 -2
  23. package/dist/contracts/builder.js +44 -3
  24. package/dist/contracts/contract-definition-schema.d.ts +251 -0
  25. package/dist/contracts/contract-definition-schema.js +48 -0
  26. package/dist/contracts/contract-provider-router.d.ts +2 -0
  27. package/dist/contracts/contract-provider-router.js +5 -2
  28. package/dist/contracts/contract-proxy.d.ts +52 -18
  29. package/dist/contracts/contract-proxy.js +144 -203
  30. package/dist/contracts/contract-resolvers.d.ts +6 -4
  31. package/dist/contracts/contract-resolvers.js +32 -9
  32. package/dist/contracts/frozen-contracts.d.ts +59 -0
  33. package/dist/contracts/frozen-contracts.js +86 -0
  34. package/dist/contracts/host-contract-client.d.ts +134 -0
  35. package/dist/contracts/host-contract-client.js +396 -0
  36. package/dist/contracts/host-contract-transport.d.ts +2 -0
  37. package/dist/contracts/host-contract-transport.js +8 -0
  38. package/dist/contracts/index.d.ts +6 -4
  39. package/dist/contracts/index.js +11 -5
  40. package/dist/contracts/registry.d.ts +31 -1
  41. package/dist/contracts/registry.js +100 -6
  42. package/dist/contracts/system/contract-catalog-contract.d.ts +61 -6
  43. package/dist/contracts/system/contract-catalog-contract.js +17 -5
  44. package/dist/contracts/tool-catalog-service.js +26 -6
  45. package/dist/contracts/tool-catalog.d.ts +1 -1
  46. package/dist/contracts/tool-catalog.js +4 -1
  47. package/dist/contracts/types.d.ts +32 -2
  48. package/dist/contracts/validate.d.ts +16 -1
  49. package/dist/contracts/validate.js +139 -25
  50. package/dist/data/change-tracking.d.ts +17 -5
  51. package/dist/data/change-tracking.js +22 -6
  52. package/dist/data/change-tracking.test.d.ts +1 -0
  53. package/dist/data/change-tracking.test.js +62 -0
  54. package/dist/data/orm/sql.data-source.d.ts +9 -0
  55. package/dist/data/orm/sql.data-source.js +47 -2
  56. package/dist/data/orm/sql.data-source.test.js +142 -0
  57. package/dist/data/orm/table-container.js +9 -9
  58. package/dist/data/orm/table-container.test.js +44 -0
  59. package/dist/data/orm/types.d.ts +41 -1
  60. package/dist/data/orm/types.js +159 -28
  61. package/dist/data/orm/types.test.js +129 -0
  62. package/dist/data/peer-types.d.ts +8 -62
  63. package/dist/data/persistent-vars.d.ts +4 -1
  64. package/dist/data/persistent-vars.js +7 -2
  65. package/dist/data/tools.d.ts +29 -221
  66. package/dist/define/also-implements.test.d.ts +1 -0
  67. package/dist/define/also-implements.test.js +123 -0
  68. package/dist/define/context.d.ts +77 -0
  69. package/dist/define/context.js +2 -0
  70. package/dist/define/contract-ref.d.ts +177 -0
  71. package/dist/define/contract-ref.js +67 -0
  72. package/dist/define/declare-contract.d.ts +167 -0
  73. package/dist/define/declare-contract.js +220 -0
  74. package/dist/define/declare-contract.test.d.ts +1 -0
  75. package/dist/define/declare-contract.test.js +573 -0
  76. package/dist/define/define-package.d.ts +203 -0
  77. package/dist/define/define-package.js +289 -0
  78. package/dist/define/define.test.d.ts +1 -0
  79. package/dist/define/define.test.js +1010 -0
  80. package/dist/define/empty-fields.d.ts +15 -0
  81. package/dist/define/empty-fields.js +134 -0
  82. package/dist/define/empty-fields.test.d.ts +1 -0
  83. package/dist/define/empty-fields.test.js +55 -0
  84. package/dist/define/events-observables.test.d.ts +1 -0
  85. package/dist/define/events-observables.test.js +1013 -0
  86. package/dist/define/events.d.ts +81 -0
  87. package/dist/define/events.js +81 -0
  88. package/dist/define/generated-field.d.ts +20 -0
  89. package/dist/define/generated-field.js +25 -0
  90. package/dist/define/index.d.ts +28 -0
  91. package/dist/define/index.js +62 -0
  92. package/dist/define/lazy-members.d.ts +8 -0
  93. package/dist/define/lazy-members.js +38 -0
  94. package/dist/define/manifest.d.ts +63 -0
  95. package/dist/define/manifest.js +653 -0
  96. package/dist/define/observables.d.ts +146 -0
  97. package/dist/define/observables.js +187 -0
  98. package/dist/define/serve-members.d.ts +21 -0
  99. package/dist/define/serve-members.js +152 -0
  100. package/dist/define/serve-members.test.d.ts +1 -0
  101. package/dist/define/serve-members.test.js +630 -0
  102. package/dist/define/serve.d.ts +22 -0
  103. package/dist/define/serve.js +58 -0
  104. package/dist/define/system-contracts.d.ts +41 -0
  105. package/dist/define/system-contracts.js +21 -0
  106. package/dist/define/table-handle.d.ts +13 -0
  107. package/dist/define/table-handle.js +36 -0
  108. package/dist/define/table.d.ts +128 -0
  109. package/dist/define/table.js +122 -0
  110. package/dist/define/tool.d.ts +129 -0
  111. package/dist/define/tool.js +91 -0
  112. package/dist/define/vars.d.ts +82 -0
  113. package/dist/define/vars.js +256 -0
  114. package/dist/define/vars.test.d.ts +1 -0
  115. package/dist/define/vars.test.js +39 -0
  116. package/dist/define/wire-value.d.ts +12 -0
  117. package/dist/define/wire-value.js +76 -0
  118. package/dist/define/worker-state.d.ts +15 -0
  119. package/dist/define/worker-state.js +45 -0
  120. package/dist/device/tx-encoding.test.js +10 -0
  121. package/dist/index.d.ts +3 -0
  122. package/dist/index.js +5 -1
  123. package/dist/observable.d.ts +42 -2
  124. package/dist/observable.js +25 -7
  125. package/dist/package-build/build-package.d.ts +60 -0
  126. package/dist/package-build/build-package.js +312 -0
  127. package/dist/package-build/build-package.test.d.ts +1 -0
  128. package/dist/package-build/build-package.test.js +204 -0
  129. package/dist/package-build/cli.d.ts +7 -0
  130. package/dist/package-build/cli.js +218 -0
  131. package/dist/package-build/contract-add.d.ts +35 -0
  132. package/dist/package-build/contract-add.js +97 -0
  133. package/dist/package-build/contract-add.test.d.ts +1 -0
  134. package/dist/package-build/contract-add.test.js +467 -0
  135. package/dist/package-build/contract-source.d.ts +59 -0
  136. package/dist/package-build/contract-source.js +471 -0
  137. package/dist/package-build/freeze-contracts.d.ts +56 -0
  138. package/dist/package-build/freeze-contracts.js +198 -0
  139. package/dist/package-build/freeze-contracts.test.d.ts +1 -0
  140. package/dist/package-build/freeze-contracts.test.js +355 -0
  141. package/dist/package-installer/package-creator.d.ts +4 -0
  142. package/dist/package-installer/package-creator.js +57 -16
  143. package/dist/package-installer/package-creator.test.js +86 -0
  144. package/dist/package-loader/contract-members.d.ts +53 -0
  145. package/dist/package-loader/contract-members.js +31 -0
  146. package/dist/package-loader/contract-package-loader.js +5 -1
  147. package/dist/package-loader/contract-package-loader.test.js +11 -0
  148. package/dist/package-loader/host-authority-loading.test.js +9 -5
  149. package/dist/package-loader/index.d.ts +1 -0
  150. package/dist/package-loader/index.js +1 -0
  151. package/dist/package-loader/isolated-artifact.d.ts +80 -17
  152. package/dist/package-loader/isolated-artifact.js +257 -51
  153. package/dist/package-loader/isolated-artifact.test.js +294 -21
  154. package/dist/package-loader/isolated-runtime.d.ts +22 -7
  155. package/dist/package-loader/isolated-table-gateway.js +30 -11
  156. package/dist/package-loader/isolated-table-gateway.test.js +243 -0
  157. package/dist/package-loader/package-loader.d.ts +8 -1
  158. package/dist/package-loader/package-loader.js +39 -20
  159. package/dist/package-loader/package-loader.test.js +47 -0
  160. package/dist/peer-id.d.ts +25 -0
  161. package/dist/peer-id.js +99 -0
  162. package/dist/serial-json.compat.test.d.ts +1 -0
  163. package/dist/serial-json.compat.test.js +369 -0
  164. package/dist/serial-json.d.ts +41 -5
  165. package/dist/serial-json.js +216 -157
  166. package/dist/serial-json.oracle.d.ts +21 -0
  167. package/dist/serial-json.oracle.js +222 -0
  168. package/dist/serial-json.test.js +315 -0
  169. package/dist/testing/index.d.ts +4 -0
  170. package/dist/testing/index.js +8 -0
  171. package/dist/testing/mock-host-contract.d.ts +123 -0
  172. package/dist/testing/mock-host-contract.js +434 -0
  173. package/dist/testing/mock-host-contract.test.d.ts +1 -0
  174. package/dist/testing/mock-host-contract.test.js +782 -0
  175. package/dist/types/field-type.d.ts +35 -18
  176. package/dist/types/field-type.js +4 -1
  177. package/dist/types/zod-types.d.ts +9 -0
  178. package/dist/types/zod-types.js +8 -2
  179. package/dist/utils.d.ts +1 -25
  180. package/dist/utils.js +10 -90
  181. package/package.json +9 -2
  182. package/testing/package.json +4 -0
@@ -0,0 +1,81 @@
1
+ import type { z } from "zod";
2
+ import type { IField } from "../types/field-type";
3
+ /** An event declared with a zod payload schema. */
4
+ export interface IPackageEventSchemaDefinition<P extends z.AnyZodObject = z.AnyZodObject> {
5
+ /** Event name, unique on its contract. */
6
+ name: string;
7
+ /** What the event signals, for people and agents subscribing to it. */
8
+ description: string;
9
+ /** Payload schema. `emit` parses the payload with it; the manifest carries its fields as IR. */
10
+ payloadSchema: P;
11
+ }
12
+ /** An event declared directly in Peers IR, for a contract defined elsewhere. */
13
+ export interface IPackageEventFieldsDefinition {
14
+ /** Event name, unique on its contract. */
15
+ name: string;
16
+ /** What the event signals, for people and agents subscribing to it. */
17
+ description: string;
18
+ /** Payload fields. Omit for an event without a payload. */
19
+ payloadFields?: IField[];
20
+ }
21
+ /** Either form of event declaration. */
22
+ export type PackageEventDefinition = IPackageEventSchemaDefinition | IPackageEventFieldsDefinition;
23
+ /**
24
+ * An event declared with {@link defineEvent}. List it on a provided contract's `events`. `P` is
25
+ * what `emit` takes, `N` the event's name, and `O` what subscribers receive (the parsed payload).
26
+ */
27
+ export interface IPackageEvent<P = unknown, N extends string = string, O = P> {
28
+ /** Marks an event declaration. */
29
+ readonly kind: "event";
30
+ /** What was passed to {@link defineEvent}. */
31
+ readonly definition: Readonly<PackageEventDefinition> & {
32
+ readonly name: N;
33
+ };
34
+ /** Type-only: what subscribers receive. Always undefined at runtime. */
35
+ readonly types?: {
36
+ payload: O;
37
+ };
38
+ /**
39
+ * Send the event on every provided contract that lists it. The payload is parsed with the
40
+ * schema first, so a bad one fails here. Resolves once the host has accepted it. Needs no
41
+ * `ctx`: the package emits for itself, from a handler or from background work. Outside a
42
+ * worker (tests, the manifest build) it checks the payload and sends nothing. Captures a
43
+ * snapshot, omitting undefined object fields and mapping undefined array entries to null; a
44
+ * value the bridge can't carry (a function, a Map, a cycle) fails, in a worker or not.
45
+ */
46
+ emit(...args: undefined extends P ? [payload?: P] : [payload: P]): Promise<void>;
47
+ }
48
+ /**
49
+ * Declare an event from a zod payload schema.
50
+ *
51
+ * @param definition - Name, description, and payload schema.
52
+ */
53
+ export declare function defineEvent<P extends z.AnyZodObject, N extends string>(definition: IPackageEventSchemaDefinition<P> & {
54
+ name: N;
55
+ }): IPackageEvent<z.input<P>, NoInfer<N>, z.output<P>>;
56
+ /**
57
+ * Declare an event from Peers IR fields, for a contract defined elsewhere. Its payload isn't
58
+ * parsed with a schema; the host checks the emitted snapshot against the fields.
59
+ *
60
+ * @param definition - Name, description, and payload fields.
61
+ */
62
+ export declare function defineEvent<N extends string>(definition: IPackageEventFieldsDefinition & {
63
+ name: N;
64
+ }): IPackageEvent<unknown, NoInfer<N>>;
65
+ /**
66
+ * True for an event from {@link defineEvent}, as opposed to an IR declaration.
67
+ *
68
+ * @param value - An entry in a contract's `events`.
69
+ */
70
+ export declare function isPackageEvent(value: unknown): value is IPackageEvent<any, string, any>;
71
+ /**
72
+ * Record which provided contracts list each event, so `emit` knows where to send it. Called by
73
+ * `startPackage`.
74
+ *
75
+ * @param provides - The package's provided contracts.
76
+ */
77
+ export declare function bindPackageEvents(provides: readonly {
78
+ contractId: string;
79
+ version: number;
80
+ events: readonly unknown[];
81
+ }[]): void;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defineEvent = defineEvent;
4
+ exports.isPackageEvent = isPackageEvent;
5
+ exports.bindPackageEvents = bindPackageEvents;
6
+ const empty_fields_1 = require("./empty-fields");
7
+ const wire_value_1 = require("./wire-value");
8
+ const worker_state_1 = require("./worker-state");
9
+ /** Contracts each event is listed on, set when the worker starts. */
10
+ let eventTargets = new WeakMap();
11
+ /**
12
+ * Declare an event a provided contract may emit.
13
+ *
14
+ * @param definition - Name, description, and payload schema (or IR fields).
15
+ */
16
+ function defineEvent(definition) {
17
+ if (typeof definition?.name !== "string" || definition.name.trim() === "") {
18
+ throw new Error("defineEvent requires a name");
19
+ }
20
+ const { name } = definition;
21
+ if (typeof definition.description !== "string") {
22
+ throw new Error(`Event ${name} needs a description`);
23
+ }
24
+ const schema = "payloadSchema" in definition ? definition.payloadSchema : undefined;
25
+ if (schema !== undefined && "payloadFields" in definition) {
26
+ throw new Error(`Event ${name} needs either payloadSchema or payloadFields, not both`);
27
+ }
28
+ if (schema !== undefined && typeof schema?.shape !== "object") {
29
+ throw new Error(`Event ${name} payloadSchema must be a zod object`);
30
+ }
31
+ if ("payloadFields" in definition &&
32
+ definition.payloadFields !== undefined &&
33
+ !Array.isArray(definition.payloadFields)) {
34
+ throw new Error(`Event ${name} payloadFields must be an array`);
35
+ }
36
+ const event = Object.freeze({
37
+ kind: "event",
38
+ definition: Object.freeze({ ...definition }),
39
+ async emit(payload) {
40
+ const value = (0, wire_value_1.snapshotPackageValue)(schema ? schema.parse((0, empty_fields_1.withoutEmptySchemaFields)(schema, payload)) : payload);
41
+ // Read synchronously when started, so emits reach the host in the order they were made.
42
+ const ctx = (0, worker_state_1.packageContext)() ?? (await (0, worker_state_1.whenPackageStarts)());
43
+ if (!ctx) {
44
+ return;
45
+ }
46
+ const targets = eventTargets.get(event);
47
+ if (!targets?.length) {
48
+ throw new Error(`Event "${name}" isn't listed on a contract this package provides`);
49
+ }
50
+ await Promise.all(targets.map((target) => ctx.emitContractEvent(target.contractId, target.version, name, value)));
51
+ },
52
+ });
53
+ return event;
54
+ }
55
+ /**
56
+ * True for an event from {@link defineEvent}, as opposed to an IR declaration.
57
+ *
58
+ * @param value - An entry in a contract's `events`.
59
+ */
60
+ function isPackageEvent(value) {
61
+ return value?.kind === "event";
62
+ }
63
+ /**
64
+ * Record which provided contracts list each event, so `emit` knows where to send it. Called by
65
+ * `startPackage`.
66
+ *
67
+ * @param provides - The package's provided contracts.
68
+ */
69
+ function bindPackageEvents(provides) {
70
+ const targets = new WeakMap();
71
+ for (const contract of provides) {
72
+ for (const event of contract.events) {
73
+ if (isPackageEvent(event)) {
74
+ const list = targets.get(event) ?? [];
75
+ list.push({ contractId: contract.contractId, version: contract.version });
76
+ targets.set(event, list);
77
+ }
78
+ }
79
+ }
80
+ eventTargets = targets;
81
+ }
@@ -0,0 +1,20 @@
1
+ import type { z } from "zod";
2
+ import type { FieldType, IFieldShape } from "../types/field-type";
3
+ interface IGeneratedField {
4
+ shape: IFieldShape;
5
+ valueType?: FieldType;
6
+ }
7
+ /**
8
+ * Keep the original IR on a generated schema when zod's inferred IR would lose information
9
+ * (such as a stored default or a blob's type). The schema still validates the described value;
10
+ * this annotation only controls its manifest. Used by `peers-package contract add`.
11
+ *
12
+ * @internal
13
+ * @param schema - The generated schema, retaining its inferred TypeScript type.
14
+ * @param shape - The original field shape, without its name.
15
+ * @param valueType - An observable's separate host-facing value type, when needed.
16
+ */
17
+ export declare function generatedField<S extends z.ZodTypeAny>(schema: S, shape: IFieldShape, valueType?: FieldType): S;
18
+ /** Read the original IR annotation of a generated schema. Internal to manifest generation. */
19
+ export declare function generatedFieldMetadata(schema: z.ZodTypeAny): IGeneratedField | undefined;
20
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generatedField = generatedField;
4
+ exports.generatedFieldMetadata = generatedFieldMetadata;
5
+ const generatedShape = Symbol.for("@peers-app/peers-sdk/generated-field");
6
+ /**
7
+ * Keep the original IR on a generated schema when zod's inferred IR would lose information
8
+ * (such as a stored default or a blob's type). The schema still validates the described value;
9
+ * this annotation only controls its manifest. Used by `peers-package contract add`.
10
+ *
11
+ * @internal
12
+ * @param schema - The generated schema, retaining its inferred TypeScript type.
13
+ * @param shape - The original field shape, without its name.
14
+ * @param valueType - An observable's separate host-facing value type, when needed.
15
+ */
16
+ function generatedField(schema, shape, valueType) {
17
+ // describe clones the schema, including shared schemas such as zodPeerId.
18
+ const copy = schema.describe(schema.description ?? "");
19
+ Object.defineProperty(copy, generatedShape, { value: { shape, valueType } });
20
+ return copy;
21
+ }
22
+ /** Read the original IR annotation of a generated schema. Internal to manifest generation. */
23
+ function generatedFieldMetadata(schema) {
24
+ return schema[generatedShape];
25
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * `@peers-app/peers-sdk/define`: declare an isolated package.
3
+ *
4
+ * A package is its tools, events, observables, and tables, plus the contracts it consumes and
5
+ * provides. Declare them in `src/package.ts` with {@link definePackage}; `peers-package` turns
6
+ * the declaration into the manifest and bundles the same code for the sandbox. This entry stays
7
+ * small so a provider bundle doesn't carry the whole SDK.
8
+ */
9
+ export { z } from "zod";
10
+ export { AccessLevel } from "../data/access-level";
11
+ export type { DataFilter, IDataQueryParams } from "../data/orm/data-query";
12
+ export type { IDataChangedEvent } from "../data/orm/subscribable.data-source";
13
+ export { type Observable, observable, type Subscription } from "../observable";
14
+ export { isid, newid } from "../peer-id";
15
+ export { FieldType, type IField } from "../types/field-type";
16
+ export { zodPeerId } from "../types/zod-types";
17
+ export * from "./context";
18
+ export * from "./contract-ref";
19
+ export * from "./declare-contract";
20
+ export * from "./define-package";
21
+ export { defineEvent, type IPackageEvent, type IPackageEventFieldsDefinition, type IPackageEventSchemaDefinition, type PackageEventDefinition, } from "./events";
22
+ export { generatedField } from "./generated-field";
23
+ export { declareObservable, defineObservable, type IPackageObservable, type IPackageObservableDeclaration, type IPackageObservableSchemaDefinition, type IPackageObservableSchemaShape, type IPackageObservableTypeDefinition, type IPackageObservableTypeShape, isImplementedObservable, type PackageObservableDefinition, type PackageObservableShape, } from "./observables";
24
+ export * from "./serve";
25
+ export * from "./system-contracts";
26
+ export * from "./table";
27
+ export * from "./tool";
28
+ export { deviceVar, groupDeviceVar, groupUserVar, groupVar, type IPackageVarBinding, type IPackageVarFactory, type IPackageVarOptions, type PersistentVar, readPackageVar, userVar, writePackageVar, } from "./vars";
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.writePackageVar = exports.userVar = exports.readPackageVar = exports.groupVar = exports.groupUserVar = exports.groupDeviceVar = exports.deviceVar = exports.isImplementedObservable = exports.defineObservable = exports.declareObservable = exports.generatedField = exports.defineEvent = exports.zodPeerId = exports.FieldType = exports.newid = exports.isid = exports.observable = exports.AccessLevel = exports.z = void 0;
18
+ /**
19
+ * `@peers-app/peers-sdk/define`: declare an isolated package.
20
+ *
21
+ * A package is its tools, events, observables, and tables, plus the contracts it consumes and
22
+ * provides. Declare them in `src/package.ts` with {@link definePackage}; `peers-package` turns
23
+ * the declaration into the manifest and bundles the same code for the sandbox. This entry stays
24
+ * small so a provider bundle doesn't carry the whole SDK.
25
+ */
26
+ var zod_1 = require("zod");
27
+ Object.defineProperty(exports, "z", { enumerable: true, get: function () { return zod_1.z; } });
28
+ var access_level_1 = require("../data/access-level");
29
+ Object.defineProperty(exports, "AccessLevel", { enumerable: true, get: function () { return access_level_1.AccessLevel; } });
30
+ var observable_1 = require("../observable");
31
+ Object.defineProperty(exports, "observable", { enumerable: true, get: function () { return observable_1.observable; } });
32
+ var peer_id_1 = require("../peer-id");
33
+ Object.defineProperty(exports, "isid", { enumerable: true, get: function () { return peer_id_1.isid; } });
34
+ Object.defineProperty(exports, "newid", { enumerable: true, get: function () { return peer_id_1.newid; } });
35
+ var field_type_1 = require("../types/field-type");
36
+ Object.defineProperty(exports, "FieldType", { enumerable: true, get: function () { return field_type_1.FieldType; } });
37
+ var zod_types_1 = require("../types/zod-types");
38
+ Object.defineProperty(exports, "zodPeerId", { enumerable: true, get: function () { return zod_types_1.zodPeerId; } });
39
+ __exportStar(require("./context"), exports);
40
+ __exportStar(require("./contract-ref"), exports);
41
+ __exportStar(require("./declare-contract"), exports);
42
+ __exportStar(require("./define-package"), exports);
43
+ var events_1 = require("./events");
44
+ Object.defineProperty(exports, "defineEvent", { enumerable: true, get: function () { return events_1.defineEvent; } });
45
+ var generated_field_1 = require("./generated-field");
46
+ Object.defineProperty(exports, "generatedField", { enumerable: true, get: function () { return generated_field_1.generatedField; } });
47
+ var observables_1 = require("./observables");
48
+ Object.defineProperty(exports, "declareObservable", { enumerable: true, get: function () { return observables_1.declareObservable; } });
49
+ Object.defineProperty(exports, "defineObservable", { enumerable: true, get: function () { return observables_1.defineObservable; } });
50
+ Object.defineProperty(exports, "isImplementedObservable", { enumerable: true, get: function () { return observables_1.isImplementedObservable; } });
51
+ __exportStar(require("./serve"), exports);
52
+ __exportStar(require("./system-contracts"), exports);
53
+ __exportStar(require("./table"), exports);
54
+ __exportStar(require("./tool"), exports);
55
+ var vars_1 = require("./vars");
56
+ Object.defineProperty(exports, "deviceVar", { enumerable: true, get: function () { return vars_1.deviceVar; } });
57
+ Object.defineProperty(exports, "groupDeviceVar", { enumerable: true, get: function () { return vars_1.groupDeviceVar; } });
58
+ Object.defineProperty(exports, "groupUserVar", { enumerable: true, get: function () { return vars_1.groupUserVar; } });
59
+ Object.defineProperty(exports, "groupVar", { enumerable: true, get: function () { return vars_1.groupVar; } });
60
+ Object.defineProperty(exports, "readPackageVar", { enumerable: true, get: function () { return vars_1.readPackageVar; } });
61
+ Object.defineProperty(exports, "userVar", { enumerable: true, get: function () { return vars_1.userVar; } });
62
+ Object.defineProperty(exports, "writePackageVar", { enumerable: true, get: function () { return vars_1.writePackageVar; } });
@@ -0,0 +1,8 @@
1
+ /**
2
+ * A bag whose every other property is made by `member` on first access, then kept. Members
3
+ * can't be replaced, added, or deleted.
4
+ * Inspection and `await` see nothing.
5
+ *
6
+ * @param member - Makes the member for a name.
7
+ */
8
+ export declare function lazyMembers<M>(member: (name: string) => M): Readonly<Record<string, M>>;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.lazyMembers = lazyMembers;
4
+ // Names JavaScript looks up on any object (`await`, `JSON.stringify`, string conversion). As
5
+ // members they would turn `await client.tools` or logging a client into host calls.
6
+ const NOT_MEMBERS = new Set(["then", "toJSON", "toString", "valueOf", "constructor"]);
7
+ /**
8
+ * A bag whose every other property is made by `member` on first access, then kept. Members
9
+ * can't be replaced, added, or deleted.
10
+ * Inspection and `await` see nothing.
11
+ *
12
+ * @param member - Makes the member for a name.
13
+ */
14
+ function lazyMembers(member) {
15
+ const made = new Map();
16
+ return new Proxy(Object.create(null), {
17
+ get(_target, name) {
18
+ if (typeof name !== "string" || NOT_MEMBERS.has(name)) {
19
+ return undefined;
20
+ }
21
+ let value = made.get(name);
22
+ if (value === undefined) {
23
+ value = member(name);
24
+ made.set(name, value);
25
+ }
26
+ return value;
27
+ },
28
+ set() {
29
+ return false;
30
+ },
31
+ defineProperty() {
32
+ return false;
33
+ },
34
+ deleteProperty() {
35
+ return false;
36
+ },
37
+ });
38
+ }
@@ -0,0 +1,63 @@
1
+ import { z } from "zod";
2
+ import type { IFrozenContracts } from "../contracts/frozen-contracts";
3
+ import { type IIsolatedPackageManifest, type IIsolatedPackageTable, type IIsolatedProvidedContract } from "../package-loader/isolated-artifact";
4
+ import { FieldType, type IField } from "../types/field-type";
5
+ import type { IContractMembers, IPackageDefinition } from "./define-package";
6
+ /**
7
+ * A zod object schema as Peers IR. Defaults stay in the package. Defaulted tool inputs may be
8
+ * omitted; table fields describe complete stored rows, after the provider parses a save. Shared
9
+ * with the mock host; not a public SDK export.
10
+ *
11
+ * @internal
12
+ * @param schema - A tool's input or output, a table's row, or an event's payload schema.
13
+ * @param input - True for tool inputs, where a defaulted field may be omitted.
14
+ * @param depth - How deep this field list nests in object `fields` (top level is 1).
15
+ * @param parent - The path of the object field these fields belong to, for error messages.
16
+ */
17
+ export declare function irFields(schema: z.AnyZodObject, input?: boolean, depth?: number, parent?: string): IField[];
18
+ /**
19
+ * The IR value type for an observable's schema, which the host checks values against.
20
+ * Values are JSON, so a Date can't be one. Shared with the mock host; not a public SDK export.
21
+ *
22
+ * @internal
23
+ * @param name - The observable's name, for schema errors.
24
+ * @param schema - The observable's value schema.
25
+ */
26
+ export declare function schemaValueType(name: string, schema: z.ZodTypeAny): FieldType;
27
+ /**
28
+ * True when the host's observable `valueType` check accepts `value`. Shared with the mock host;
29
+ * not a public SDK export.
30
+ *
31
+ * @internal
32
+ * @param value - An incoming observable write or published value.
33
+ * @param type - The manifest's declared value type.
34
+ */
35
+ export declare function fitsValueType(value: unknown, type: FieldType): boolean;
36
+ /**
37
+ * A contract's manifest entry and its tables' definitions, from a contract or a declaration:
38
+ * the IR a provider of it would register. Used to generate a declaration from another one; not
39
+ * a public SDK export.
40
+ *
41
+ * @internal
42
+ * @param contract - From `defineContract`, `implementContract`, or a declaration's members.
43
+ */
44
+ export declare function contractManifest(contract: IContractMembers): {
45
+ contract: IIsolatedProvidedContract;
46
+ tables: IIsolatedPackageTable[];
47
+ };
48
+ /** What {@link buildPackageManifest} needs besides the package definition. */
49
+ export interface IPackageManifestOptions {
50
+ /**
51
+ * The package's `frozen-contracts.json`. A contract that also implements an older version of
52
+ * itself, by number, takes that version's shape from here.
53
+ */
54
+ frozen?: IFrozenContracts;
55
+ }
56
+ /**
57
+ * Turn a package definition into its manifest: plain JSON in Peers IR, which the Runtime reads
58
+ * without running any package code. `peers-package manifest` writes it to `dist/manifest.json`.
59
+ *
60
+ * @param definition - The default export of the package's `src/package.ts`.
61
+ * @param options - The package's frozen contracts, for `alsoImplements` by version number.
62
+ */
63
+ export declare function buildPackageManifest(definition: IPackageDefinition, options?: IPackageManifestOptions): IIsolatedPackageManifest;