@sproutboat/wire 0.2.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/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ // @sproutboat/wire: broker frame protocol and JSON validation utilities.
2
+ //
3
+ // Moved verbatim from sproutboat-cli/src/broker.ts, broker.test.ts and
4
+ // json.ts. The broker is a trust boundary process; this package is its
5
+ // protocol and storage behavior, shared by the CLI dev broker and the
6
+ // platform's edge/supervisor side.
7
+ export * from "./broker";
8
+ export * from "./json";
package/src/json.ts ADDED
@@ -0,0 +1,38 @@
1
+ /**
2
+ * The one JSON contract the CLI decodes external payloads through: registry
3
+ * responses, control-plane responses, broker request bodies. Parse at the I/O
4
+ * boundary with `parseJsonValue`, then narrow with these guards — nothing
5
+ * downstream should see an unparsed value.
6
+ */
7
+ export type JsonValue = string | number | boolean | null | JsonObject | JsonValue[];
8
+ export type JsonObject = { [key: string]: JsonValue };
9
+
10
+ export function isString(value: JsonValue | undefined): value is string {
11
+ return value !== undefined && value === String(value);
12
+ }
13
+
14
+ export function isSafeInteger(value: JsonValue | undefined): value is number {
15
+ return Number.isSafeInteger(value);
16
+ }
17
+
18
+ export function isBoolean(value: JsonValue | undefined): value is boolean {
19
+ return value === true || value === false;
20
+ }
21
+
22
+ export function parseJsonValue(source: string): JsonValue {
23
+ const value = JSON.parse(source);
24
+ if (
25
+ value === null ||
26
+ value === true ||
27
+ value === false ||
28
+ value === String(value) ||
29
+ Number.isFinite(value) ||
30
+ value instanceof Object
31
+ )
32
+ return value;
33
+ throw new Error("response was not valid JSON");
34
+ }
35
+
36
+ export function jsonObject(value: JsonValue): JsonObject | undefined {
37
+ return value instanceof Object && !Array.isArray(value) ? value : undefined;
38
+ }