@printobserver/sdk 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Whether a parsed document is of one declared type of the contracts.
3
+ *
4
+ * What a type is comes from `SHAPES`, the table generated from the same
5
+ * declarations as every type in `contract.ts`; this module only walks it. An
6
+ * object admits members it does not declare, as the Rust client's types do,
7
+ * and an optional member is either left out or of its declared shape: one sent
8
+ * as null is refused unless that shape is nullable. Every closed set admits
9
+ * only what it declares: an enumeration's names, a union's arms, a tagged
10
+ * object's tags.
11
+ */
12
+ /** Whether `value` is a document of the type the contracts declare as `declared`. */
13
+ export declare function conforms(value: unknown, declared: string): boolean;
14
+ /** A shape table: each declaration's name, and what it declares. */
15
+ export type Table = Readonly<Record<string, unknown>>;
16
+ /**
17
+ * Whether `value` is a document of `declared` as one shape table writes it.
18
+ * `conforms` walks the generated table; a table that is not one this walker
19
+ * reads admits nothing, rather than everything.
20
+ */
21
+ export declare function conformsIn(table: Table, value: unknown, declared: string): boolean;
package/dist/shapes.js ADDED
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Whether a parsed document is of one declared type of the contracts.
3
+ *
4
+ * What a type is comes from `SHAPES`, the table generated from the same
5
+ * declarations as every type in `contract.ts`; this module only walks it. An
6
+ * object admits members it does not declare, as the Rust client's types do,
7
+ * and an optional member is either left out or of its declared shape: one sent
8
+ * as null is refused unless that shape is nullable. Every closed set admits
9
+ * only what it declares: an enumeration's names, a union's arms, a tagged
10
+ * object's tags.
11
+ */
12
+ import { SHAPES } from "./contract.js";
13
+ /** Whether one parsed value is a JSON object, whose members can then be read. */
14
+ function isObject(value) {
15
+ return typeof value === "object" && value !== null && !Array.isArray(value);
16
+ }
17
+ /** Whether `value` is a document of the type the contracts declare as `declared`. */
18
+ export function conforms(value, declared) {
19
+ return conformsIn(SHAPES, value, declared);
20
+ }
21
+ /**
22
+ * Whether `value` is a document of `declared` as one shape table writes it.
23
+ * `conforms` walks the generated table; a table that is not one this walker
24
+ * reads admits nothing, rather than everything.
25
+ */
26
+ export function conformsIn(table, value, declared) {
27
+ return isDeclared(table, value, table[declared]);
28
+ }
29
+ /** Whether `value` is of one type expression of the table. */
30
+ function isOf(table, value, shape) {
31
+ if (!isObject(shape)) {
32
+ return false;
33
+ }
34
+ if (typeof shape.ref === "string") {
35
+ return isDeclared(table, value, table[shape.ref]);
36
+ }
37
+ if (typeof shape.scalar === "string") {
38
+ return isScalar(value, shape.scalar);
39
+ }
40
+ if ("list" in shape) {
41
+ return (Array.isArray(value) &&
42
+ (value.length > 0 || shape.non_empty !== true) &&
43
+ value.every((entry) => isOf(table, entry, shape.list)));
44
+ }
45
+ if ("map" in shape) {
46
+ return isObject(value) && Object.values(value).every((entry) => isOf(table, entry, shape.map));
47
+ }
48
+ if ("nullable" in shape) {
49
+ return value === null || isOf(table, value, shape.nullable);
50
+ }
51
+ return shape.any === true;
52
+ }
53
+ /** Whether `value` is one scalar of JSON: a number is finite. */
54
+ function isScalar(value, kind) {
55
+ switch (kind) {
56
+ case "string":
57
+ return typeof value === "string";
58
+ case "null":
59
+ return value === null;
60
+ case "boolean":
61
+ return typeof value === "boolean";
62
+ case "integer":
63
+ return Number.isInteger(value);
64
+ case "number":
65
+ return typeof value === "number" && Number.isFinite(value);
66
+ default:
67
+ return false;
68
+ }
69
+ }
70
+ /** Whether `value` is of one declaration of the table. */
71
+ function isDeclared(table, value, declaration) {
72
+ if (!isObject(declaration)) {
73
+ return false;
74
+ }
75
+ if ("alias" in declaration) {
76
+ return isOf(table, value, declaration.alias);
77
+ }
78
+ if (Array.isArray(declaration.enum)) {
79
+ return typeof value === "string" && declaration.enum.includes(value);
80
+ }
81
+ const { struct, union } = declaration;
82
+ if (isObject(struct) && isObject(struct.fields) && isObject(struct.variants)) {
83
+ if (!(isObject(value) && hasFields(table, value, struct.fields))) {
84
+ return false;
85
+ }
86
+ return !struct.tag_field || isTagged(table, value, String(struct.tag_field), struct.variants);
87
+ }
88
+ if (isObject(union) && isObject(union.variants)) {
89
+ if (typeof union.tag_field === "string" && union.tag_field !== "") {
90
+ return isObject(value) && isTagged(table, value, union.tag_field, union.variants);
91
+ }
92
+ return isExternal(table, value, union.variants);
93
+ }
94
+ return false;
95
+ }
96
+ /** Whether a flag and the sibling it is held in step with agree. */
97
+ function isPaired(flag, value, pair) {
98
+ const sent = Object.hasOwn(value, pair);
99
+ if (sent && value[pair] === null) {
100
+ return false;
101
+ }
102
+ return typeof flag === "boolean" && flag !== sent;
103
+ }
104
+ /**
105
+ * Whether an object carries each member its declaration requires, each of its
106
+ * type. A member paired with an optional sibling is false exactly when that
107
+ * sibling is sent, and the sibling is never sent as null.
108
+ */
109
+ function hasFields(table, value, fields) {
110
+ return Object.entries(fields).every(([name, field]) => {
111
+ if (!isObject(field) || typeof field.required !== "boolean") {
112
+ return false;
113
+ }
114
+ if (typeof field.paired_with === "string" && field.paired_with !== "") {
115
+ if (!isPaired(value[name], value, field.paired_with)) {
116
+ return false;
117
+ }
118
+ }
119
+ const member = value[name];
120
+ if (member === undefined) {
121
+ return !field.required;
122
+ }
123
+ return isOf(table, member, field.shape);
124
+ });
125
+ }
126
+ /** Whether an object names one declared arm in `tagField`, and carries that arm. */
127
+ function isTagged(table, value, tagField, variants) {
128
+ const tag = value[tagField];
129
+ if (typeof tag !== "string" || !Object.hasOwn(variants, tag)) {
130
+ return false;
131
+ }
132
+ return isArm(table, value, variants[tag]);
133
+ }
134
+ /**
135
+ * Whether a value is one arm of an externally tagged union: an arm carrying
136
+ * nothing travels as its own name, and any other as an object of exactly one
137
+ * member, its name, holding what it carries.
138
+ */
139
+ function isExternal(table, value, variants) {
140
+ if (typeof value === "string") {
141
+ const variant = Object.hasOwn(variants, value) ? variants[value] : undefined;
142
+ return isObject(variant) && variant.unit === true;
143
+ }
144
+ if (!isObject(value)) {
145
+ return false;
146
+ }
147
+ const entries = Object.entries(value);
148
+ const [entry] = entries;
149
+ if (entries.length !== 1 || entry === undefined) {
150
+ return false;
151
+ }
152
+ const [tag, carried] = entry;
153
+ const variant = Object.hasOwn(variants, tag) ? variants[tag] : undefined;
154
+ if (!isObject(variant) || variant.unit !== false) {
155
+ return false;
156
+ }
157
+ if (isObject(variant.payload)) {
158
+ return isOf(table, carried, variant.payload);
159
+ }
160
+ return isObject(carried) && isObject(variant.fields) && hasFields(table, carried, variant.fields);
161
+ }
162
+ /** Whether an object carries one arm of an internally tagged set beside its tag. */
163
+ function isArm(table, value, variant) {
164
+ if (!isObject(variant)) {
165
+ return false;
166
+ }
167
+ if (isObject(variant.payload)) {
168
+ return isOf(table, value, variant.payload);
169
+ }
170
+ return isObject(variant.fields) && hasFields(table, value, variant.fields);
171
+ }
package/dist/surface.d.ts CHANGED
@@ -10,7 +10,7 @@
10
10
  * are that module's, imported as types alone, so that the two files can name
11
11
  * each other's shapes without either being unloadable.
12
12
  */
13
- import type { ActionAnswer, Actor, Range, RejectionReason, SpoolCommandAnswer } from "./contract.ts";
13
+ import type { ActionAnswer, Actor, Range, RecoveryAnswer, RecoveryRejection, RejectionReason, SpoolCommandAnswer } from "./contract.ts";
14
14
  /** Why a call did not answer what it asked for. */
15
15
  export declare class ClientError extends Error {
16
16
  constructor(said: string);
@@ -66,19 +66,19 @@ export declare class PrinterRefused extends ClientError {
66
66
  * when it ruled on something else.
67
67
  */
68
68
  export declare class Rejected extends ClientError {
69
- /** Why the action was refused. */
70
- readonly reason: RejectionReason;
69
+ /** Why the action was refused: the policy's reason, or a recovery's own refusal. */
70
+ readonly reason: RejectionReason | RecoveryRejection;
71
71
  /** The value that was asked for, where the policy ruled on one. */
72
72
  readonly requested: number | undefined;
73
73
  /** The range that is allowed, where the policy ruled on a value. */
74
74
  readonly allowed: Range | undefined;
75
75
  /**
76
76
  * The whole answer: an action's, whose record carries the request and the
77
- * decision, or a spool command's, carrying the decision and the events its
78
- * refusal wrote.
77
+ * decision, a spool command's, carrying the decision and the events its
78
+ * refusal wrote, or a recovery's, carrying its own refusal.
79
79
  */
80
- readonly answer: ActionAnswer | SpoolCommandAnswer;
81
- constructor(reason: RejectionReason, requested: number | undefined, allowed: Range | undefined, answer: ActionAnswer | SpoolCommandAnswer);
80
+ readonly answer: ActionAnswer | SpoolCommandAnswer | RecoveryAnswer;
81
+ constructor(reason: RejectionReason | RecoveryRejection, requested: number | undefined, allowed: Range | undefined, answer: ActionAnswer | SpoolCommandAnswer | RecoveryAnswer);
82
82
  }
83
83
  /**
84
84
  * Refuse a mutating call that carries no reason, before it is made.
@@ -103,10 +103,11 @@ export declare class GeneratedSurface {
103
103
  readonly actor: Actor;
104
104
  constructor(actor: Actor);
105
105
  /**
106
- * Make one call and answer the document that came back.
106
+ * Make one call and answer the document that came back, held to the
107
+ * declared type `answer` names.
107
108
  *
108
109
  * @throws {Error} Always. `Client` is what makes a request; a surface that
109
110
  * answered one would be a second transport.
110
111
  */
111
- protected call<T>(method: string, target: string, asked: Array<[string, string]>, sending: Record<string, unknown> | undefined): Promise<T>;
112
+ protected call<T>(method: string, target: string, asked: Array<[string, string]>, sending: Record<string, unknown> | undefined, answer: string): Promise<T>;
112
113
  }
package/dist/surface.js CHANGED
@@ -93,7 +93,7 @@ export class PrinterRefused extends ClientError {
93
93
  * when it ruled on something else.
94
94
  */
95
95
  export class Rejected extends ClientError {
96
- /** Why the action was refused. */
96
+ /** Why the action was refused: the policy's reason, or a recovery's own refusal. */
97
97
  reason;
98
98
  /** The value that was asked for, where the policy ruled on one. */
99
99
  requested;
@@ -101,8 +101,8 @@ export class Rejected extends ClientError {
101
101
  allowed;
102
102
  /**
103
103
  * The whole answer: an action's, whose record carries the request and the
104
- * decision, or a spool command's, carrying the decision and the events its
105
- * refusal wrote.
104
+ * decision, a spool command's, carrying the decision and the events its
105
+ * refusal wrote, or a recovery's, carrying its own refusal.
106
106
  */
107
107
  answer;
108
108
  constructor(reason, requested, allowed, answer) {
@@ -146,13 +146,14 @@ export class GeneratedSurface {
146
146
  this.actor = actor;
147
147
  }
148
148
  /**
149
- * Make one call and answer the document that came back.
149
+ * Make one call and answer the document that came back, held to the
150
+ * declared type `answer` names.
150
151
  *
151
152
  * @throws {Error} Always. `Client` is what makes a request; a surface that
152
153
  * answered one would be a second transport.
153
154
  */
154
- call(method, target, asked, sending) {
155
+ call(method, target, asked, sending, answer) {
155
156
  throw new Error(`a surface makes no request: ${method} ${target} with ${asked.length} asked ` +
156
- `for and ${sending === undefined ? "no" : "a"} body`);
157
+ `for and ${sending === undefined ? "no" : "a"} body, answering ${answer}`);
157
158
  }
158
159
  }
package/package.json CHANGED
@@ -12,12 +12,12 @@
12
12
  "license": "MIT",
13
13
  "main": "dist/index.js",
14
14
  "name": "@printobserver/sdk",
15
- "printobserverContractVersion": "0.9.0",
15
+ "printobserverContractVersion": "0.10.0",
16
16
  "repository": {
17
17
  "type": "git",
18
18
  "url": "https://github.com/nickderobertis/printobserver"
19
19
  },
20
20
  "type": "module",
21
21
  "types": "dist/index.d.ts",
22
- "version": "0.9.0"
22
+ "version": "0.10.0"
23
23
  }