@labacacia/nps-sdk 1.0.0-alpha.5 → 1.0.0-alpha.7

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 (145) hide show
  1. package/CHANGELOG.cn.md +29 -5
  2. package/CHANGELOG.md +29 -5
  3. package/LICENSE +0 -0
  4. package/NOTICE +0 -0
  5. package/README.cn.md +8 -13
  6. package/README.md +8 -13
  7. package/dist/nip/index.d.ts +1 -0
  8. package/dist/nip/index.d.ts.map +1 -1
  9. package/dist/nip/index.js +2 -0
  10. package/dist/nip/index.js.map +1 -1
  11. package/dist/nip/reputation-client.d.ts +116 -0
  12. package/dist/nip/reputation-client.d.ts.map +1 -0
  13. package/dist/nip/reputation-client.js +261 -0
  14. package/dist/nip/reputation-client.js.map +1 -0
  15. package/dist/nip/x509/oids.d.ts +9 -10
  16. package/dist/nip/x509/oids.d.ts.map +1 -1
  17. package/dist/nip/x509/oids.js +3 -4
  18. package/dist/nip/x509/oids.js.map +1 -1
  19. package/dist/nwp/anchor-client.d.ts +109 -0
  20. package/dist/nwp/anchor-client.d.ts.map +1 -0
  21. package/dist/nwp/anchor-client.js +279 -0
  22. package/dist/nwp/anchor-client.js.map +1 -0
  23. package/dist/nwp/index.d.ts +1 -1
  24. package/dist/nwp/index.d.ts.map +1 -1
  25. package/dist/nwp/index.js +1 -1
  26. package/dist/nwp/index.js.map +1 -1
  27. package/doc/nps-sdk.core.cn.md +0 -0
  28. package/doc/nps-sdk.core.md +0 -0
  29. package/doc/nps-sdk.ncp.cn.md +0 -0
  30. package/doc/nps-sdk.ncp.md +0 -0
  31. package/doc/nps-sdk.ndp.cn.md +0 -0
  32. package/doc/nps-sdk.ndp.md +0 -0
  33. package/doc/nps-sdk.nop.cn.md +0 -0
  34. package/doc/nps-sdk.nop.md +0 -0
  35. package/doc/overview.cn.md +0 -0
  36. package/doc/overview.md +0 -0
  37. package/package.json +12 -1
  38. package/CONTRIBUTING.cn.md +0 -35
  39. package/CONTRIBUTING.md +0 -35
  40. package/dist/nwp/error-codes.d.ts +0 -42
  41. package/dist/nwp/error-codes.d.ts.map +0 -1
  42. package/dist/nwp/error-codes.js +0 -53
  43. package/dist/nwp/error-codes.js.map +0 -1
  44. package/nip-ca-server/Dockerfile +0 -27
  45. package/nip-ca-server/README.md +0 -45
  46. package/nip-ca-server/db/001_init.sql +0 -25
  47. package/nip-ca-server/docker-compose.yml +0 -29
  48. package/nip-ca-server/package.json +0 -23
  49. package/nip-ca-server/src/ca.ts +0 -155
  50. package/nip-ca-server/src/db.ts +0 -104
  51. package/nip-ca-server/src/index.ts +0 -157
  52. package/nip-ca-server/tsconfig.json +0 -13
  53. package/src/core/anchor-cache.ts +0 -129
  54. package/src/core/cache.ts +0 -93
  55. package/src/core/canonical-json.ts +0 -50
  56. package/src/core/codec.ts +0 -158
  57. package/src/core/codecs/index.ts +0 -5
  58. package/src/core/codecs/ncp-codec.ts +0 -170
  59. package/src/core/codecs/tier1-json-codec.ts +0 -33
  60. package/src/core/codecs/tier2-msgpack-codec.ts +0 -30
  61. package/src/core/crypto-provider.ts +0 -47
  62. package/src/core/exceptions.ts +0 -57
  63. package/src/core/frame-header.ts +0 -282
  64. package/src/core/frame-registry.ts +0 -91
  65. package/src/core/frames.ts +0 -184
  66. package/src/core/index.ts +0 -42
  67. package/src/core/registry.ts +0 -28
  68. package/src/core/status-codes.ts +0 -47
  69. package/src/index.ts +0 -10
  70. package/src/ncp/frames/anchor-frame.ts +0 -87
  71. package/src/ncp/frames/caps-frame.ts +0 -59
  72. package/src/ncp/frames/diff-frame.ts +0 -69
  73. package/src/ncp/frames/error-frame.ts +0 -26
  74. package/src/ncp/frames/hello-frame.ts +0 -50
  75. package/src/ncp/frames/stream-frame.ts +0 -35
  76. package/src/ncp/frames.ts +0 -251
  77. package/src/ncp/handshake.ts +0 -95
  78. package/src/ncp/index.ts +0 -13
  79. package/src/ncp/ncp-error-codes.ts +0 -36
  80. package/src/ncp/ncp-patch-format.ts +0 -16
  81. package/src/ncp/preamble.ts +0 -79
  82. package/src/ncp/registry.ts +0 -15
  83. package/src/ncp/stream-manager.ts +0 -212
  84. package/src/ndp/dns-txt.ts +0 -86
  85. package/src/ndp/frames.ts +0 -124
  86. package/src/ndp/index.ts +0 -8
  87. package/src/ndp/ndp-registry.ts +0 -116
  88. package/src/ndp/registry.ts +0 -12
  89. package/src/ndp/validator.ts +0 -64
  90. package/src/nip/acme/client.ts +0 -185
  91. package/src/nip/acme/index.ts +0 -8
  92. package/src/nip/acme/jws.ts +0 -109
  93. package/src/nip/acme/messages.ts +0 -85
  94. package/src/nip/acme/server.ts +0 -480
  95. package/src/nip/acme/wire.ts +0 -24
  96. package/src/nip/assurance-level.ts +0 -40
  97. package/src/nip/cert-format.ts +0 -9
  98. package/src/nip/error-codes.ts +0 -38
  99. package/src/nip/frames.ts +0 -138
  100. package/src/nip/identity.ts +0 -113
  101. package/src/nip/index.ts +0 -14
  102. package/src/nip/registry.ts +0 -12
  103. package/src/nip/verifier.ts +0 -122
  104. package/src/nip/x509/builder.ts +0 -91
  105. package/src/nip/x509/index.ts +0 -6
  106. package/src/nip/x509/oids.ts +0 -28
  107. package/src/nip/x509/verifier.ts +0 -214
  108. package/src/nop/client.ts +0 -103
  109. package/src/nop/frames.ts +0 -181
  110. package/src/nop/index.ts +0 -7
  111. package/src/nop/models.ts +0 -79
  112. package/src/nop/nop-types.ts +0 -208
  113. package/src/nop/registry.ts +0 -13
  114. package/src/nwp/client.ts +0 -114
  115. package/src/nwp/error-codes.ts +0 -62
  116. package/src/nwp/frames.ts +0 -116
  117. package/src/nwp/index.ts +0 -7
  118. package/src/nwp/registry.ts +0 -11
  119. package/src/setup.ts +0 -32
  120. package/tests/_rfc0002-keys.ts +0 -57
  121. package/tests/core/anchor-cache.test.ts +0 -242
  122. package/tests/core/codec.test.ts +0 -205
  123. package/tests/core/frame-registry.test.ts +0 -46
  124. package/tests/core.test.ts +0 -327
  125. package/tests/ncp/diff-binary-bitset.test.ts +0 -107
  126. package/tests/ncp/e2e-enc-reject.test.ts +0 -93
  127. package/tests/ncp/err-error-frame.test.ts +0 -152
  128. package/tests/ncp/frames.test.ts +0 -359
  129. package/tests/ncp/framing.test.ts +0 -233
  130. package/tests/ncp/hello-frame.test.ts +0 -122
  131. package/tests/ncp/inline-anchor.test.ts +0 -88
  132. package/tests/ncp/preamble.test.ts +0 -93
  133. package/tests/ncp/security.test.ts +0 -184
  134. package/tests/ncp/stream-window.test.ts +0 -167
  135. package/tests/ncp/stream.test.ts +0 -242
  136. package/tests/ncp/version-negotiation.test.ts +0 -123
  137. package/tests/ndp.test.ts +0 -377
  138. package/tests/nip-acme-agent01.test.ts +0 -192
  139. package/tests/nip-x509.test.ts +0 -280
  140. package/tests/nip.test.ts +0 -184
  141. package/tests/nop.test.ts +0 -344
  142. package/tests/nwp.test.ts +0 -237
  143. package/tsconfig.json +0 -20
  144. package/tsup.config.ts +0 -20
  145. package/vitest.config.ts +0 -10
@@ -1,129 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
- //
4
- // AnchorCache — Schema cache with TTL, LRU eviction, poison detection
5
- // NPS-1 §5.3, §7.2, §9
6
-
7
- import { NcpError } from "./frame-header.js";
8
- import type { AnchorFrame } from "../ncp/frames/anchor-frame.js";
9
-
10
- interface CacheEntry {
11
- frame: AnchorFrame;
12
- expiresAt: number; // epoch ms
13
- lastAccessed: number; // epoch ms
14
- }
15
-
16
- /**
17
- * AnchorFrame cache with:
18
- * - TTL-based expiry (NPS-1 §5.3)
19
- * - LRU eviction at maxSize (NPS-1 §9, default 1000)
20
- * - Anchor poisoning detection (NPS-1 §7.2)
21
- */
22
- export class AnchorCache {
23
- private readonly cache = new Map<string, CacheEntry>();
24
- private readonly maxSize: number;
25
- private readonly getNow: () => number;
26
-
27
- constructor(options?: { maxSize?: number; getNow?: () => number }) {
28
- this.maxSize = options?.maxSize ?? 1000;
29
- this.getNow = options?.getNow ?? (() => Date.now());
30
- }
31
-
32
- /**
33
- * Cache an AnchorFrame.
34
- *
35
- * - ttl=0: frame is valid but not cached (NPS-1 §4.1)
36
- * - Same anchor_id + same schema: idempotent (no-op)
37
- * - Same anchor_id + different schema: NCP-ANCHOR-ID-MISMATCH (poison detection)
38
- *
39
- * @throws {NcpError} NCP-ANCHOR-ID-MISMATCH on anchor poisoning.
40
- */
41
- set(frame: AnchorFrame): void {
42
- // ttl=0 means use once, don't cache
43
- if (frame.ttl === 0) return;
44
-
45
- const existing = this.cache.get(frame.anchor_id);
46
- if (existing) {
47
- // Poison detection: same ID, different schema
48
- const existingJson = JSON.stringify(existing.frame.schema);
49
- const newJson = JSON.stringify(frame.schema);
50
- if (existingJson !== newJson) {
51
- throw new NcpError(
52
- "NCP-ANCHOR-ID-MISMATCH",
53
- `Anchor poisoning detected: anchor_id ${frame.anchor_id} received with different schema`,
54
- );
55
- }
56
- // Same schema — idempotent, update access time
57
- existing.lastAccessed = this.getNow();
58
- return;
59
- }
60
-
61
- // LRU eviction if at capacity
62
- if (this.cache.size >= this.maxSize) {
63
- this.evictLru();
64
- }
65
-
66
- const ttlMs = (frame.ttl ?? 3600) * 1000;
67
- this.cache.set(frame.anchor_id, {
68
- frame,
69
- expiresAt: this.getNow() + ttlMs,
70
- lastAccessed: this.getNow(),
71
- });
72
- }
73
-
74
- /**
75
- * Get a cached AnchorFrame by anchor_id.
76
- *
77
- * @returns The cached frame, or null if not found or expired.
78
- */
79
- get(anchorId: string): AnchorFrame | null {
80
- const entry = this.cache.get(anchorId);
81
- if (!entry) return null;
82
-
83
- // Check TTL expiry
84
- if (this.getNow() >= entry.expiresAt) {
85
- this.cache.delete(anchorId);
86
- return null;
87
- }
88
-
89
- entry.lastAccessed = this.getNow();
90
- return entry.frame;
91
- }
92
-
93
- /**
94
- * Get a cached AnchorFrame, throwing if not found.
95
- * @throws {NcpError} NCP-ANCHOR-NOT-FOUND if not in cache or expired.
96
- */
97
- getRequired(anchorId: string): AnchorFrame {
98
- const frame = this.get(anchorId);
99
- if (!frame) {
100
- throw new NcpError(
101
- "NCP-ANCHOR-NOT-FOUND",
102
- `Schema anchor ${anchorId} not found in cache`,
103
- );
104
- }
105
- return frame;
106
- }
107
-
108
- /** Current cache size. */
109
- get size(): number {
110
- return this.cache.size;
111
- }
112
-
113
- /** Evict the least recently accessed entry. */
114
- private evictLru(): void {
115
- let oldestKey: string | undefined;
116
- let oldestTime = Infinity;
117
-
118
- for (const [key, entry] of this.cache) {
119
- if (entry.lastAccessed < oldestTime) {
120
- oldestTime = entry.lastAccessed;
121
- oldestKey = key;
122
- }
123
- }
124
-
125
- if (oldestKey) {
126
- this.cache.delete(oldestKey);
127
- }
128
- }
129
- }
package/src/core/cache.ts DELETED
@@ -1,93 +0,0 @@
1
- // Copyright 2026 INNO LOTUS PTY LTD
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- /**
5
- * AnchorFrameCache — in-process cache for AnchorFrame instances (NPS-1 §4.1).
6
- */
7
-
8
- import { createHash } from "node:crypto";
9
- import { NpsAnchorNotFoundError, NpsAnchorPoisonError } from "./exceptions.js";
10
- import type { AnchorFrame, FrameSchema } from "../ncp/frames.js";
11
-
12
- export class AnchorFrameCache {
13
- private readonly _store = new Map<string, { frame: AnchorFrame; expiresAt: number }>();
14
-
15
- // Allow clock injection for testing
16
- clock: () => number = () => Date.now();
17
-
18
- // ── Public API ────────────────────────────────────────────────────────────
19
-
20
- set(frame: AnchorFrame): string {
21
- const anchorId = frame.anchorId.startsWith("sha256:")
22
- ? frame.anchorId
23
- : AnchorFrameCache.computeAnchorId(frame.schema);
24
-
25
- const existing = this._store.get(anchorId);
26
- if (existing !== undefined && this.clock() < existing.expiresAt) {
27
- if (!AnchorFrameCache._schemasEqual(existing.frame.schema, frame.schema)) {
28
- throw new NpsAnchorPoisonError(anchorId);
29
- }
30
- // Same schema — idempotent; refresh TTL below
31
- }
32
-
33
- const ttlMs = (frame.ttl ?? 3600) * 1000;
34
- const expiresAt = this.clock() + ttlMs;
35
- this._store.set(anchorId, { frame, expiresAt });
36
- return anchorId;
37
- }
38
-
39
- get(anchorId: string): AnchorFrame | undefined {
40
- const entry = this._store.get(anchorId);
41
- if (entry === undefined) return undefined;
42
- if (this.clock() > entry.expiresAt) {
43
- this._store.delete(anchorId);
44
- return undefined;
45
- }
46
- return entry.frame;
47
- }
48
-
49
- getRequired(anchorId: string): AnchorFrame {
50
- const frame = this.get(anchorId);
51
- if (frame === undefined) throw new NpsAnchorNotFoundError(anchorId);
52
- return frame;
53
- }
54
-
55
- invalidate(anchorId: string): void {
56
- this._store.delete(anchorId);
57
- }
58
-
59
- get size(): number {
60
- this._evictExpired();
61
- return this._store.size;
62
- }
63
-
64
- // ── Static helpers ────────────────────────────────────────────────────────
65
-
66
- static computeAnchorId(schema: FrameSchema): string {
67
- const sorted = [...schema.fields]
68
- .map((f) => {
69
- const obj: Record<string, unknown> = { name: f.name, type: f.type };
70
- if (f.semantic !== undefined) obj["semantic"] = f.semantic;
71
- if (f.nullable !== undefined) obj["nullable"] = f.nullable;
72
- return obj;
73
- })
74
- .sort((a, b) => String(a["name"]).localeCompare(String(b["name"])));
75
-
76
- const canonical = JSON.stringify(sorted);
77
- const digest = createHash("sha256").update(canonical, "utf8").digest("hex");
78
- return `sha256:${digest}`;
79
- }
80
-
81
- // ── Private ───────────────────────────────────────────────────────────────
82
-
83
- private _evictExpired(): void {
84
- const now = this.clock();
85
- for (const [k, entry] of this._store) {
86
- if (now > entry.expiresAt) this._store.delete(k);
87
- }
88
- }
89
-
90
- private static _schemasEqual(a: FrameSchema, b: FrameSchema): boolean {
91
- return AnchorFrameCache.computeAnchorId(a) === AnchorFrameCache.computeAnchorId(b);
92
- }
93
- }
@@ -1,50 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
- //
4
- // Canonical JSON helpers — two distinct serialisation paths used in NPS.
5
- //
6
- // AnchorFrame uses JCS (RFC 8785) for anchor_id hashing → jcsStringify.
7
- // NIP signing uses Python-compatible sorted-key JSON → sortKeysStringify.
8
-
9
- import canonicalizeImport from "canonicalize";
10
-
11
- // `canonicalize` is a CJS module whose default export resolves to a namespace
12
- // under NodeNext. Cast once to the call signature its .d.ts promises.
13
- const canonicalize = canonicalizeImport as unknown as (
14
- input: unknown,
15
- ) => string | undefined;
16
-
17
- /**
18
- * JCS (RFC 8785) canonical JSON stringify.
19
- * Used for AnchorFrame anchor_id computation.
20
- */
21
- export function jcsStringify(obj: unknown): string {
22
- const result = canonicalize(obj);
23
- if (result === undefined) {
24
- throw new Error("canonicalize returned undefined for input");
25
- }
26
- return result;
27
- }
28
-
29
- /**
30
- * Sorted-key JSON stringify — matches Python's
31
- * `json.dumps(obj, sort_keys=True, separators=(",",":"))`.
32
- * Used for NIP signing.
33
- */
34
- export function sortKeysStringify(obj: unknown): string {
35
- return JSON.stringify(sortKeys(obj));
36
- }
37
-
38
- function sortKeys(value: unknown): unknown {
39
- if (Array.isArray(value)) {
40
- return value.map(sortKeys);
41
- }
42
- if (value !== null && typeof value === "object") {
43
- const sorted: Record<string, unknown> = {};
44
- for (const key of Object.keys(value as object).sort()) {
45
- sorted[key] = sortKeys((value as Record<string, unknown>)[key]);
46
- }
47
- return sorted;
48
- }
49
- return value;
50
- }
package/src/core/codec.ts DELETED
@@ -1,158 +0,0 @@
1
- // Copyright 2026 INNO LOTUS PTY LTD
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- /**
5
- * NPS frame codec: Tier-1 (JSON) and Tier-2 (MsgPack) encode/decode,
6
- * plus the top-level NpsFrameCodec dispatcher.
7
- */
8
-
9
- import * as msgpack from "@msgpack/msgpack";
10
- import { NpsCodecError } from "./exceptions.js";
11
- import {
12
- DEFAULT_MAX_PAYLOAD,
13
- EncodingTier,
14
- FrameFlags,
15
- FrameHeader,
16
- FrameType,
17
- } from "./frames.js";
18
- import type { FrameRegistry } from "./registry.js";
19
-
20
- // ── NpsFrame interface ────────────────────────────────────────────────────────
21
-
22
- export interface NpsFrame {
23
- readonly frameType: FrameType;
24
- readonly preferredTier: EncodingTier;
25
- toDict(): Record<string, unknown>;
26
- }
27
-
28
- // ── Tier-1 JSON codec ─────────────────────────────────────────────────────────
29
-
30
- export class Tier1JsonCodec {
31
- encode(frame: NpsFrame): Uint8Array {
32
- try {
33
- const json = JSON.stringify(frame.toDict());
34
- return new TextEncoder().encode(json);
35
- } catch (err) {
36
- throw new NpsCodecError(
37
- `Tier-1 JSON encode failed for 0x${frame.frameType.toString(16).padStart(2, "0")}: ${String(err)}`,
38
- );
39
- }
40
- }
41
-
42
- decode(frameType: FrameType, payload: Uint8Array, registry: FrameRegistry): NpsFrame {
43
- const cls = registry.resolve(frameType);
44
- try {
45
- const text = new TextDecoder().decode(payload);
46
- const data = JSON.parse(text) as Record<string, unknown>;
47
- return cls.fromDict(data);
48
- } catch (err) {
49
- throw new NpsCodecError(
50
- `Tier-1 JSON decode failed for 0x${frameType.toString(16).padStart(2, "0")}: ${String(err)}`,
51
- );
52
- }
53
- }
54
- }
55
-
56
- // ── Tier-2 MsgPack codec ──────────────────────────────────────────────────────
57
-
58
- export class Tier2MsgPackCodec {
59
- encode(frame: NpsFrame): Uint8Array {
60
- try {
61
- return msgpack.encode(frame.toDict());
62
- } catch (err) {
63
- throw new NpsCodecError(
64
- `Tier-2 MsgPack encode failed for 0x${frame.frameType.toString(16).padStart(2, "0")}: ${String(err)}`,
65
- );
66
- }
67
- }
68
-
69
- decode(frameType: FrameType, payload: Uint8Array, registry: FrameRegistry): NpsFrame {
70
- const cls = registry.resolve(frameType);
71
- try {
72
- const data = msgpack.decode(payload) as Record<string, unknown>;
73
- return cls.fromDict(data);
74
- } catch (err) {
75
- throw new NpsCodecError(
76
- `Tier-2 MsgPack decode failed for 0x${frameType.toString(16).padStart(2, "0")}: ${String(err)}`,
77
- );
78
- }
79
- }
80
- }
81
-
82
- // ── NpsFrameCodec (dispatcher) ────────────────────────────────────────────────
83
-
84
- export class NpsFrameCodec {
85
- private readonly _registry: FrameRegistry;
86
- private readonly _maxPayload: number;
87
- private readonly _json = new Tier1JsonCodec();
88
- private readonly _msgpack = new Tier2MsgPackCodec();
89
-
90
- constructor(registry: FrameRegistry, options: { maxPayload?: number } = {}) {
91
- this._registry = registry;
92
- this._maxPayload = options.maxPayload ?? DEFAULT_MAX_PAYLOAD;
93
- }
94
-
95
- // ── Encode ────────────────────────────────────────────────────────────────
96
-
97
- encode(frame: NpsFrame, options: { overrideTier?: EncodingTier } = {}): Uint8Array {
98
- const tier = options.overrideTier ?? frame.preferredTier;
99
- const tierCodec = this._selectCodec(tier);
100
-
101
- let payload: Uint8Array;
102
- try {
103
- payload = tierCodec.encode(frame);
104
- } catch (err) {
105
- if (err instanceof NpsCodecError) throw err;
106
- throw new NpsCodecError(`Encode failed for 0x${frame.frameType.toString(16)}.`);
107
- }
108
-
109
- if (payload.length > this._maxPayload) {
110
- throw new NpsCodecError(
111
- `Encoded payload for 0x${frame.frameType.toString(16).padStart(2, "0")} exceeds max_frame_payload` +
112
- ` (${payload.length} bytes > ${this._maxPayload}). Use StreamFrame (0x03) for large payloads.`,
113
- );
114
- }
115
-
116
- const useExt = payload.length > DEFAULT_MAX_PAYLOAD;
117
- let flags = this._buildFlags(frame, tier);
118
- if (useExt) flags |= FrameFlags.EXT;
119
-
120
- const header = new FrameHeader(frame.frameType, flags, payload.length);
121
- const headerBytes = header.toBytes();
122
- const wire = new Uint8Array(headerBytes.length + payload.length);
123
- wire.set(headerBytes, 0);
124
- wire.set(payload, headerBytes.length);
125
- return wire;
126
- }
127
-
128
- // ── Decode ────────────────────────────────────────────────────────────────
129
-
130
- decode(wire: Uint8Array): NpsFrame {
131
- const header = FrameHeader.parse(wire);
132
- const payload = wire.slice(header.headerSize, header.headerSize + header.payloadLength);
133
- const codec = this._selectCodec(header.encodingTier);
134
- return codec.decode(header.frameType, payload, this._registry);
135
- }
136
-
137
- static peekHeader(wire: Uint8Array): FrameHeader {
138
- return FrameHeader.parse(wire);
139
- }
140
-
141
- // ── Private ───────────────────────────────────────────────────────────────
142
-
143
- private _buildFlags(frame: NpsFrame, tier: EncodingTier): number {
144
- let flags = tier === EncodingTier.JSON ? FrameFlags.TIER1_JSON : FrameFlags.TIER2_MSGPACK;
145
-
146
- const isStreamFrame = "isLast" in frame;
147
- const isFinal = !isStreamFrame || (frame as { isLast: boolean }).isLast;
148
- if (isFinal) flags |= FrameFlags.FINAL;
149
-
150
- return flags;
151
- }
152
-
153
- private _selectCodec(tier: EncodingTier): Tier1JsonCodec | Tier2MsgPackCodec {
154
- if (tier === EncodingTier.JSON) return this._json;
155
- if (tier === EncodingTier.MSGPACK) return this._msgpack;
156
- throw new NpsCodecError(`Unsupported encoding tier: 0x${(tier as number).toString(16).padStart(2, "0")}.`);
157
- }
158
- }
@@ -1,5 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
- export * from "./ncp-codec.js";
4
- export { encodeJson, decodeJson } from "./tier1-json-codec.js";
5
- export { encodeMsgPack, decodeMsgPack } from "./tier2-msgpack-codec.js";
@@ -1,170 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
- //
4
- // NCP Codec — Top-level encode/decode dispatcher
5
- // Routes to Tier-1 JSON or Tier-2 MsgPack based on frame header flags.
6
-
7
- import {
8
- parseFrameHeader,
9
- writeFrameHeader,
10
- buildFlags,
11
- EncodingTier,
12
- NcpError,
13
- DEFAULT_MAX_PAYLOAD,
14
- type FrameHeader,
15
- } from "../frame-header.js";
16
- import { encodeJson, decodeJson } from "./tier1-json-codec.js";
17
- import { encodeMsgPack, decodeMsgPack } from "./tier2-msgpack-codec.js";
18
-
19
- // ---------------------------------------------------------------------------
20
- // Types
21
- // ---------------------------------------------------------------------------
22
-
23
- export interface DecodeResult {
24
- /** Parsed frame header. */
25
- header: FrameHeader;
26
- /** Decoded payload object. */
27
- payload: unknown;
28
- /** Total bytes consumed (header + payload). */
29
- bytesConsumed: number;
30
- }
31
-
32
- export interface EncodeOptions {
33
- /** Frame type byte. */
34
- frameType: number;
35
- /** Encoding tier (default: JSON). */
36
- tier?: EncodingTier;
37
- /** Set FINAL flag. */
38
- final?: boolean;
39
- /** Set ENC flag. */
40
- encrypted?: boolean;
41
- /** Use extended header (for payloads > 64KB). */
42
- extended?: boolean;
43
- }
44
-
45
- export interface CodecOptions {
46
- /** Maximum frame payload in bytes (negotiated via CapsFrame). Default: 65535. */
47
- maxFramePayload?: number;
48
- }
49
-
50
- // ---------------------------------------------------------------------------
51
- // Decode
52
- // ---------------------------------------------------------------------------
53
-
54
- /**
55
- * Decode a complete NCP frame from a buffer.
56
- *
57
- * @returns Decoded header + payload + bytes consumed.
58
- * @throws {NcpError} NCP-ENCODING-UNSUPPORTED for reserved tiers.
59
- * @throws {NcpError} NCP-FRAME-PAYLOAD-TOO-LARGE if payload exceeds max.
60
- * @throws {NcpError} NCP-FRAME-INCOMPLETE if buffer doesn't have enough data.
61
- */
62
- export function decodeFrame(
63
- buffer: Uint8Array,
64
- options?: CodecOptions,
65
- ): DecodeResult {
66
- const header = parseFrameHeader(buffer);
67
- const maxPayload = options?.maxFramePayload ?? DEFAULT_MAX_PAYLOAD;
68
-
69
- // NCP-F-09: Validate against negotiated max_frame_payload
70
- if (header.payloadLength > maxPayload) {
71
- throw new NcpError(
72
- "NCP-FRAME-PAYLOAD-TOO-LARGE",
73
- `Payload ${header.payloadLength} exceeds max_frame_payload ${maxPayload}`,
74
- );
75
- }
76
-
77
- const totalSize = header.headerSize + header.payloadLength;
78
-
79
- // NCP-F-05: Buffer underrun — not enough data for payload
80
- if (buffer.length < totalSize) {
81
- throw new NcpError(
82
- "NCP-FRAME-INCOMPLETE",
83
- `Buffer has ${buffer.length} bytes but frame needs ${totalSize}`,
84
- );
85
- }
86
-
87
- // Extract payload bytes
88
- const payloadBytes = buffer.subarray(
89
- header.headerSize,
90
- header.headerSize + header.payloadLength,
91
- );
92
-
93
- // Route to codec by tier
94
- let payload: unknown;
95
- switch (header.tier) {
96
- case EncodingTier.Json:
97
- payload = decodeJson(payloadBytes);
98
- break;
99
- case EncodingTier.MsgPack:
100
- payload = decodeMsgPack(payloadBytes);
101
- break;
102
- default:
103
- // NCP-E-05: Reserved tiers (0x02, 0x03)
104
- throw new NcpError(
105
- "NCP-ENCODING-UNSUPPORTED",
106
- `Unsupported encoding tier: 0x${(header.tier as number).toString(16).padStart(2, "0")}`,
107
- );
108
- }
109
-
110
- return { header, payload, bytesConsumed: totalSize };
111
- }
112
-
113
- // ---------------------------------------------------------------------------
114
- // Encode
115
- // ---------------------------------------------------------------------------
116
-
117
- /**
118
- * Encode a payload into a complete NCP frame (header + payload bytes).
119
- */
120
- export function encodeFrame(
121
- payload: unknown,
122
- options: EncodeOptions,
123
- ): Uint8Array {
124
- const tier = options.tier ?? EncodingTier.Json;
125
-
126
- // Encode payload
127
- let payloadBytes: Uint8Array;
128
- switch (tier) {
129
- case EncodingTier.Json:
130
- payloadBytes = encodeJson(payload);
131
- break;
132
- case EncodingTier.MsgPack:
133
- payloadBytes = encodeMsgPack(payload);
134
- break;
135
- default:
136
- throw new NcpError(
137
- "NCP-ENCODING-UNSUPPORTED",
138
- `Unsupported encoding tier for encode: ${tier}`,
139
- );
140
- }
141
-
142
- // Determine if extended header is needed
143
- const needsExtended =
144
- options.extended || payloadBytes.length > DEFAULT_MAX_PAYLOAD;
145
-
146
- const flags = buildFlags({
147
- tier,
148
- final: options.final,
149
- encrypted: options.encrypted,
150
- extended: needsExtended,
151
- });
152
-
153
- const header: FrameHeader = {
154
- frameType: options.frameType,
155
- flags,
156
- payloadLength: payloadBytes.length,
157
- tier,
158
- isFinal: options.final ?? false,
159
- isEncrypted: options.encrypted ?? false,
160
- isExtended: needsExtended,
161
- headerSize: needsExtended ? 8 : 4,
162
- };
163
-
164
- // Write frame
165
- const frame = new Uint8Array(header.headerSize + payloadBytes.length);
166
- writeFrameHeader(header, frame);
167
- frame.set(payloadBytes, header.headerSize);
168
-
169
- return frame;
170
- }
@@ -1,33 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
- //
4
- // Tier-1 JSON Codec — UTF-8 JSON encode/decode
5
- // NPS-1 §8
6
-
7
- import { NcpError } from "../frame-header.js";
8
-
9
- const encoder = new TextEncoder();
10
- const decoder = new TextDecoder();
11
-
12
- /**
13
- * Encode a frame payload to Tier-1 JSON bytes.
14
- */
15
- export function encodeJson(payload: unknown): Uint8Array {
16
- return encoder.encode(JSON.stringify(payload));
17
- }
18
-
19
- /**
20
- * Decode Tier-1 JSON bytes to a parsed object.
21
- * @throws {NcpError} NPS-CLIENT-BAD-FRAME on malformed JSON.
22
- */
23
- export function decodeJson(bytes: Uint8Array): unknown {
24
- const text = decoder.decode(bytes);
25
- try {
26
- return JSON.parse(text) as unknown;
27
- } catch {
28
- throw new NcpError(
29
- "NPS-CLIENT-BAD-FRAME",
30
- "Malformed JSON payload",
31
- );
32
- }
33
- }
@@ -1,30 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
- //
4
- // Tier-2 MsgPack Codec — Binary encode/decode
5
- // NPS-1 §8
6
-
7
- import { encode, decode } from "@msgpack/msgpack";
8
- import { NcpError } from "../frame-header.js";
9
-
10
- /**
11
- * Encode a frame payload to Tier-2 MsgPack bytes.
12
- */
13
- export function encodeMsgPack(payload: unknown): Uint8Array {
14
- return encode(payload);
15
- }
16
-
17
- /**
18
- * Decode Tier-2 MsgPack bytes to a parsed object.
19
- * @throws {NcpError} NPS-CLIENT-BAD-FRAME on malformed MsgPack.
20
- */
21
- export function decodeMsgPack(bytes: Uint8Array): unknown {
22
- try {
23
- return decode(bytes) as unknown;
24
- } catch {
25
- throw new NcpError(
26
- "NPS-CLIENT-BAD-FRAME",
27
- "Malformed MsgPack payload",
28
- );
29
- }
30
- }