@labacacia/nps-sdk 1.0.0-alpha.1

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 (311) hide show
  1. package/CONTRIBUTING.md +33 -0
  2. package/LICENSE +170 -0
  3. package/NOTICE +7 -0
  4. package/README.md +153 -0
  5. package/dist/codec-CmHeovTV.d.cts +120 -0
  6. package/dist/codec-CmHeovTV.d.ts +120 -0
  7. package/dist/core/anchor-cache.d.ts +42 -0
  8. package/dist/core/anchor-cache.d.ts.map +1 -0
  9. package/dist/core/anchor-cache.js +104 -0
  10. package/dist/core/anchor-cache.js.map +1 -0
  11. package/dist/core/cache.d.ts +14 -0
  12. package/dist/core/cache.d.ts.map +1 -0
  13. package/dist/core/cache.js +80 -0
  14. package/dist/core/cache.js.map +1 -0
  15. package/dist/core/canonical-json.d.ts +12 -0
  16. package/dist/core/canonical-json.d.ts.map +1 -0
  17. package/dist/core/canonical-json.js +44 -0
  18. package/dist/core/canonical-json.js.map +1 -0
  19. package/dist/core/codec.d.ts +32 -0
  20. package/dist/core/codec.d.ts.map +1 -0
  21. package/dist/core/codec.js +119 -0
  22. package/dist/core/codec.js.map +1 -0
  23. package/dist/core/codecs/index.d.ts +4 -0
  24. package/dist/core/codecs/index.d.ts.map +1 -0
  25. package/dist/core/codecs/index.js +6 -0
  26. package/dist/core/codecs/index.js.map +1 -0
  27. package/dist/core/codecs/ncp-codec.d.ts +39 -0
  28. package/dist/core/codecs/ncp-codec.d.ts.map +1 -0
  29. package/dist/core/codecs/ncp-codec.js +93 -0
  30. package/dist/core/codecs/ncp-codec.js.map +1 -0
  31. package/dist/core/codecs/tier1-json-codec.d.ts +10 -0
  32. package/dist/core/codecs/tier1-json-codec.d.ts.map +1 -0
  33. package/dist/core/codecs/tier1-json-codec.js +28 -0
  34. package/dist/core/codecs/tier1-json-codec.js.map +1 -0
  35. package/dist/core/codecs/tier2-msgpack-codec.d.ts +10 -0
  36. package/dist/core/codecs/tier2-msgpack-codec.d.ts.map +1 -0
  37. package/dist/core/codecs/tier2-msgpack-codec.js +26 -0
  38. package/dist/core/codecs/tier2-msgpack-codec.js.map +1 -0
  39. package/dist/core/crypto-provider.d.ts +31 -0
  40. package/dist/core/crypto-provider.d.ts.map +1 -0
  41. package/dist/core/crypto-provider.js +10 -0
  42. package/dist/core/crypto-provider.js.map +1 -0
  43. package/dist/core/exceptions.d.ts +27 -0
  44. package/dist/core/exceptions.d.ts.map +1 -0
  45. package/dist/core/exceptions.js +52 -0
  46. package/dist/core/exceptions.js.map +1 -0
  47. package/dist/core/frame-header.d.ts +87 -0
  48. package/dist/core/frame-header.d.ts.map +1 -0
  49. package/dist/core/frame-header.js +185 -0
  50. package/dist/core/frame-header.js.map +1 -0
  51. package/dist/core/frame-registry.d.ts +35 -0
  52. package/dist/core/frame-registry.d.ts.map +1 -0
  53. package/dist/core/frame-registry.js +63 -0
  54. package/dist/core/frame-registry.js.map +1 -0
  55. package/dist/core/frames.d.ts +80 -0
  56. package/dist/core/frames.d.ts.map +1 -0
  57. package/dist/core/frames.js +153 -0
  58. package/dist/core/frames.js.map +1 -0
  59. package/dist/core/index.cjs +371 -0
  60. package/dist/core/index.cjs.map +1 -0
  61. package/dist/core/index.d.cts +41 -0
  62. package/dist/core/index.d.ts +9 -0
  63. package/dist/core/index.d.ts.map +1 -0
  64. package/dist/core/index.js +10 -0
  65. package/dist/core/index.js.map +1 -0
  66. package/dist/core/registry.d.ts +11 -0
  67. package/dist/core/registry.d.ts.map +1 -0
  68. package/dist/core/registry.js +17 -0
  69. package/dist/core/registry.js.map +1 -0
  70. package/dist/core/status-codes.d.ts +28 -0
  71. package/dist/core/status-codes.d.ts.map +1 -0
  72. package/dist/core/status-codes.js +38 -0
  73. package/dist/core/status-codes.js.map +1 -0
  74. package/dist/frames-B3qLdl_g.d.cts +77 -0
  75. package/dist/frames-Ff7-ZPUl.d.ts +77 -0
  76. package/dist/index.cjs +1556 -0
  77. package/dist/index.cjs.map +1 -0
  78. package/dist/index.d.cts +21 -0
  79. package/dist/index.d.ts +2 -0
  80. package/dist/index.d.ts.map +1 -0
  81. package/dist/index.js +10 -0
  82. package/dist/index.js.map +1 -0
  83. package/dist/ncp/frames/anchor-frame.d.ts +29 -0
  84. package/dist/ncp/frames/anchor-frame.d.ts.map +1 -0
  85. package/dist/ncp/frames/anchor-frame.js +54 -0
  86. package/dist/ncp/frames/anchor-frame.js.map +1 -0
  87. package/dist/ncp/frames/caps-frame.d.ts +29 -0
  88. package/dist/ncp/frames/caps-frame.d.ts.map +1 -0
  89. package/dist/ncp/frames/caps-frame.js +29 -0
  90. package/dist/ncp/frames/caps-frame.js.map +1 -0
  91. package/dist/ncp/frames/diff-frame.d.ts +32 -0
  92. package/dist/ncp/frames/diff-frame.d.ts.map +1 -0
  93. package/dist/ncp/frames/diff-frame.js +37 -0
  94. package/dist/ncp/frames/diff-frame.js.map +1 -0
  95. package/dist/ncp/frames/error-frame.d.ts +16 -0
  96. package/dist/ncp/frames/error-frame.d.ts.map +1 -0
  97. package/dist/ncp/frames/error-frame.js +13 -0
  98. package/dist/ncp/frames/error-frame.js.map +1 -0
  99. package/dist/ncp/frames/hello-frame.d.ts +21 -0
  100. package/dist/ncp/frames/hello-frame.d.ts.map +1 -0
  101. package/dist/ncp/frames/hello-frame.js +25 -0
  102. package/dist/ncp/frames/hello-frame.js.map +1 -0
  103. package/dist/ncp/frames/stream-frame.d.ts +16 -0
  104. package/dist/ncp/frames/stream-frame.d.ts.map +1 -0
  105. package/dist/ncp/frames/stream-frame.js +18 -0
  106. package/dist/ncp/frames/stream-frame.js.map +1 -0
  107. package/dist/ncp/frames.d.ts +76 -0
  108. package/dist/ncp/frames.d.ts.map +1 -0
  109. package/dist/ncp/frames.js +147 -0
  110. package/dist/ncp/frames.js.map +1 -0
  111. package/dist/ncp/handshake.d.ts +30 -0
  112. package/dist/ncp/handshake.d.ts.map +1 -0
  113. package/dist/ncp/handshake.js +80 -0
  114. package/dist/ncp/handshake.js.map +1 -0
  115. package/dist/ncp/index.cjs +188 -0
  116. package/dist/ncp/index.cjs.map +1 -0
  117. package/dist/ncp/index.d.cts +6 -0
  118. package/dist/ncp/index.d.ts +11 -0
  119. package/dist/ncp/index.d.ts.map +1 -0
  120. package/dist/ncp/index.js +13 -0
  121. package/dist/ncp/index.js.map +1 -0
  122. package/dist/ncp/ncp-error-codes.d.ts +22 -0
  123. package/dist/ncp/ncp-error-codes.d.ts.map +1 -0
  124. package/dist/ncp/ncp-error-codes.js +32 -0
  125. package/dist/ncp/ncp-error-codes.js.map +1 -0
  126. package/dist/ncp/ncp-patch-format.d.ts +7 -0
  127. package/dist/ncp/ncp-patch-format.d.ts.map +1 -0
  128. package/dist/ncp/ncp-patch-format.js +13 -0
  129. package/dist/ncp/ncp-patch-format.js.map +1 -0
  130. package/dist/ncp/registry.d.ts +3 -0
  131. package/dist/ncp/registry.d.ts.map +1 -0
  132. package/dist/ncp/registry.js +12 -0
  133. package/dist/ncp/registry.js.map +1 -0
  134. package/dist/ncp/stream-manager.d.ts +57 -0
  135. package/dist/ncp/stream-manager.d.ts.map +1 -0
  136. package/dist/ncp/stream-manager.js +163 -0
  137. package/dist/ncp/stream-manager.js.map +1 -0
  138. package/dist/ndp/frames.d.ts +56 -0
  139. package/dist/ndp/frames.d.ts.map +1 -0
  140. package/dist/ndp/frames.js +87 -0
  141. package/dist/ndp/frames.js.map +1 -0
  142. package/dist/ndp/index.cjs +252 -0
  143. package/dist/ndp/index.cjs.map +1 -0
  144. package/dist/ndp/index.d.cts +86 -0
  145. package/dist/ndp/index.d.ts +5 -0
  146. package/dist/ndp/index.d.ts.map +1 -0
  147. package/dist/ndp/index.js +7 -0
  148. package/dist/ndp/index.js.map +1 -0
  149. package/dist/ndp/ndp-registry.d.ts +11 -0
  150. package/dist/ndp/ndp-registry.d.ts.map +1 -0
  151. package/dist/ndp/ndp-registry.js +79 -0
  152. package/dist/ndp/ndp-registry.js.map +1 -0
  153. package/dist/ndp/registry.d.ts +3 -0
  154. package/dist/ndp/registry.d.ts.map +1 -0
  155. package/dist/ndp/registry.js +10 -0
  156. package/dist/ndp/registry.js.map +1 -0
  157. package/dist/ndp/validator.d.ts +18 -0
  158. package/dist/ndp/validator.d.ts.map +1 -0
  159. package/dist/ndp/validator.js +48 -0
  160. package/dist/ndp/validator.js.map +1 -0
  161. package/dist/nip/frames.d.ts +44 -0
  162. package/dist/nip/frames.d.ts.map +1 -0
  163. package/dist/nip/frames.js +81 -0
  164. package/dist/nip/frames.js.map +1 -0
  165. package/dist/nip/identity.d.ts +18 -0
  166. package/dist/nip/identity.d.ts.map +1 -0
  167. package/dist/nip/identity.js +94 -0
  168. package/dist/nip/identity.js.map +1 -0
  169. package/dist/nip/index.cjs +214 -0
  170. package/dist/nip/index.cjs.map +1 -0
  171. package/dist/nip/index.d.cts +65 -0
  172. package/dist/nip/index.d.ts +4 -0
  173. package/dist/nip/index.d.ts.map +1 -0
  174. package/dist/nip/index.js +6 -0
  175. package/dist/nip/index.js.map +1 -0
  176. package/dist/nip/registry.d.ts +3 -0
  177. package/dist/nip/registry.d.ts.map +1 -0
  178. package/dist/nip/registry.js +10 -0
  179. package/dist/nip/registry.js.map +1 -0
  180. package/dist/nop/client.d.ts +34 -0
  181. package/dist/nop/client.d.ts.map +1 -0
  182. package/dist/nop/client.js +90 -0
  183. package/dist/nop/client.js.map +1 -0
  184. package/dist/nop/frames.d.ts +65 -0
  185. package/dist/nop/frames.d.ts.map +1 -0
  186. package/dist/nop/frames.js +148 -0
  187. package/dist/nop/frames.js.map +1 -0
  188. package/dist/nop/index.cjs +762 -0
  189. package/dist/nop/index.cjs.map +1 -0
  190. package/dist/nop/index.d.cts +155 -0
  191. package/dist/nop/index.d.ts +5 -0
  192. package/dist/nop/index.d.ts.map +1 -0
  193. package/dist/nop/index.js +7 -0
  194. package/dist/nop/index.js.map +1 -0
  195. package/dist/nop/models.d.ts +58 -0
  196. package/dist/nop/models.d.ts.map +1 -0
  197. package/dist/nop/models.js +50 -0
  198. package/dist/nop/models.js.map +1 -0
  199. package/dist/nop/nop-types.d.ts +136 -0
  200. package/dist/nop/nop-types.d.ts.map +1 -0
  201. package/dist/nop/nop-types.js +44 -0
  202. package/dist/nop/nop-types.js.map +1 -0
  203. package/dist/nop/registry.d.ts +3 -0
  204. package/dist/nop/registry.d.ts.map +1 -0
  205. package/dist/nop/registry.js +11 -0
  206. package/dist/nop/registry.js.map +1 -0
  207. package/dist/nwp/client.d.ts +22 -0
  208. package/dist/nwp/client.d.ts.map +1 -0
  209. package/dist/nwp/client.js +101 -0
  210. package/dist/nwp/client.js.map +1 -0
  211. package/dist/nwp/frames.d.ts +46 -0
  212. package/dist/nwp/frames.d.ts.map +1 -0
  213. package/dist/nwp/frames.js +81 -0
  214. package/dist/nwp/frames.js.map +1 -0
  215. package/dist/nwp/index.cjs +658 -0
  216. package/dist/nwp/index.cjs.map +1 -0
  217. package/dist/nwp/index.d.cts +65 -0
  218. package/dist/nwp/index.d.ts +4 -0
  219. package/dist/nwp/index.d.ts.map +1 -0
  220. package/dist/nwp/index.js +6 -0
  221. package/dist/nwp/index.js.map +1 -0
  222. package/dist/nwp/registry.d.ts +3 -0
  223. package/dist/nwp/registry.d.ts.map +1 -0
  224. package/dist/nwp/registry.js +9 -0
  225. package/dist/nwp/registry.js.map +1 -0
  226. package/dist/setup.d.ts +10 -0
  227. package/dist/setup.d.ts.map +1 -0
  228. package/dist/setup.js +29 -0
  229. package/dist/setup.js.map +1 -0
  230. package/nip-ca-server/Dockerfile +27 -0
  231. package/nip-ca-server/README.md +45 -0
  232. package/nip-ca-server/db/001_init.sql +25 -0
  233. package/nip-ca-server/docker-compose.yml +29 -0
  234. package/nip-ca-server/package.json +23 -0
  235. package/nip-ca-server/src/ca.ts +155 -0
  236. package/nip-ca-server/src/db.ts +104 -0
  237. package/nip-ca-server/src/index.ts +157 -0
  238. package/nip-ca-server/tsconfig.json +13 -0
  239. package/package.json +47 -0
  240. package/src/core/anchor-cache.ts +129 -0
  241. package/src/core/cache.ts +93 -0
  242. package/src/core/canonical-json.ts +50 -0
  243. package/src/core/codec.ts +158 -0
  244. package/src/core/codecs/index.ts +5 -0
  245. package/src/core/codecs/ncp-codec.ts +170 -0
  246. package/src/core/codecs/tier1-json-codec.ts +33 -0
  247. package/src/core/codecs/tier2-msgpack-codec.ts +30 -0
  248. package/src/core/crypto-provider.ts +47 -0
  249. package/src/core/exceptions.ts +57 -0
  250. package/src/core/frame-header.ts +282 -0
  251. package/src/core/frame-registry.ts +91 -0
  252. package/src/core/frames.ts +183 -0
  253. package/src/core/index.ts +10 -0
  254. package/src/core/registry.ts +28 -0
  255. package/src/core/status-codes.ts +46 -0
  256. package/src/index.ts +10 -0
  257. package/src/ncp/frames/anchor-frame.ts +87 -0
  258. package/src/ncp/frames/caps-frame.ts +59 -0
  259. package/src/ncp/frames/diff-frame.ts +69 -0
  260. package/src/ncp/frames/error-frame.ts +26 -0
  261. package/src/ncp/frames/hello-frame.ts +50 -0
  262. package/src/ncp/frames/stream-frame.ts +35 -0
  263. package/src/ncp/frames.ts +199 -0
  264. package/src/ncp/handshake.ts +95 -0
  265. package/src/ncp/index.ts +12 -0
  266. package/src/ncp/ncp-error-codes.ts +34 -0
  267. package/src/ncp/ncp-patch-format.ts +16 -0
  268. package/src/ncp/registry.ts +14 -0
  269. package/src/ncp/stream-manager.ts +212 -0
  270. package/src/ndp/frames.ts +124 -0
  271. package/src/ndp/index.ts +7 -0
  272. package/src/ndp/ndp-registry.ts +82 -0
  273. package/src/ndp/registry.ts +12 -0
  274. package/src/ndp/validator.ts +64 -0
  275. package/src/nip/frames.ts +106 -0
  276. package/src/nip/identity.ts +113 -0
  277. package/src/nip/index.ts +6 -0
  278. package/src/nip/registry.ts +12 -0
  279. package/src/nop/client.ts +103 -0
  280. package/src/nop/frames.ts +181 -0
  281. package/src/nop/index.ts +7 -0
  282. package/src/nop/models.ts +79 -0
  283. package/src/nop/nop-types.ts +208 -0
  284. package/src/nop/registry.ts +13 -0
  285. package/src/nwp/client.ts +114 -0
  286. package/src/nwp/frames.ts +116 -0
  287. package/src/nwp/index.ts +6 -0
  288. package/src/nwp/registry.ts +11 -0
  289. package/src/setup.ts +32 -0
  290. package/tests/core/anchor-cache.test.ts +242 -0
  291. package/tests/core/codec.test.ts +205 -0
  292. package/tests/core/frame-registry.test.ts +46 -0
  293. package/tests/core.test.ts +327 -0
  294. package/tests/ncp/diff-binary-bitset.test.ts +107 -0
  295. package/tests/ncp/e2e-enc-reject.test.ts +93 -0
  296. package/tests/ncp/err-error-frame.test.ts +152 -0
  297. package/tests/ncp/frames.test.ts +359 -0
  298. package/tests/ncp/framing.test.ts +233 -0
  299. package/tests/ncp/hello-frame.test.ts +122 -0
  300. package/tests/ncp/inline-anchor.test.ts +88 -0
  301. package/tests/ncp/security.test.ts +184 -0
  302. package/tests/ncp/stream-window.test.ts +167 -0
  303. package/tests/ncp/stream.test.ts +242 -0
  304. package/tests/ncp/version-negotiation.test.ts +123 -0
  305. package/tests/ndp.test.ts +271 -0
  306. package/tests/nip.test.ts +184 -0
  307. package/tests/nop.test.ts +344 -0
  308. package/tests/nwp.test.ts +237 -0
  309. package/tsconfig.json +20 -0
  310. package/tsup.config.ts +20 -0
  311. package/vitest.config.ts +10 -0
@@ -0,0 +1,87 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
+ //
4
+ // AnchorFrame (0x01) — Schema anchor for global reference
5
+ // NPS-1 §4.1
6
+
7
+ import { createHash } from "node:crypto";
8
+ // canonicalize ships CJS with TS `export default` — NodeNext resolves as namespace
9
+ import canonicalizeDefault from "canonicalize";
10
+ const canonicalize = canonicalizeDefault as unknown as (input: unknown) => string | undefined;
11
+ import { NcpError } from "../../core/frame-header.js";
12
+
13
+ // ---------------------------------------------------------------------------
14
+ // Types
15
+ // ---------------------------------------------------------------------------
16
+
17
+ const VALID_FIELD_TYPES = [
18
+ "string", "uint64", "int64", "decimal", "bool",
19
+ "timestamp", "bytes", "object", "array",
20
+ ] as const;
21
+
22
+ export type SchemaFieldType = (typeof VALID_FIELD_TYPES)[number];
23
+
24
+ export interface SchemaField {
25
+ name: string;
26
+ type: string;
27
+ semantic?: string;
28
+ nullable?: boolean;
29
+ }
30
+
31
+ export interface FrameSchema {
32
+ fields: SchemaField[];
33
+ }
34
+
35
+ export interface AnchorFrame {
36
+ frame: string;
37
+ anchor_id: string;
38
+ schema: FrameSchema;
39
+ ttl?: number;
40
+ }
41
+
42
+ // ---------------------------------------------------------------------------
43
+ // anchor_id computation (RFC 8785 JCS + SHA-256)
44
+ // ---------------------------------------------------------------------------
45
+
46
+ /**
47
+ * Compute anchor_id from schema using RFC 8785 JCS canonicalization + SHA-256.
48
+ * Format: "sha256:{64 lowercase hex chars}"
49
+ */
50
+ export function computeAnchorId(schema: FrameSchema): string {
51
+ const canonical = canonicalize(schema);
52
+ if (!canonical) {
53
+ throw new NcpError("NCP-ANCHOR-SCHEMA-INVALID", "Schema cannot be canonicalized");
54
+ }
55
+ const hash = createHash("sha256").update(canonical).digest("hex");
56
+ return `sha256:${hash}`;
57
+ }
58
+
59
+ // ---------------------------------------------------------------------------
60
+ // Validation
61
+ // ---------------------------------------------------------------------------
62
+
63
+ /**
64
+ * Validate an AnchorFrame.
65
+ * @throws {NcpError} NCP-ANCHOR-SCHEMA-INVALID if anchor_id doesn't match or schema is invalid.
66
+ */
67
+ export function validateAnchorFrame(frame: AnchorFrame): void {
68
+ // Validate schema field types
69
+ for (const field of frame.schema.fields) {
70
+ if (!VALID_FIELD_TYPES.includes(field.type as SchemaFieldType)) {
71
+ throw new NcpError(
72
+ "NCP-ANCHOR-SCHEMA-INVALID",
73
+ `Unsupported field type "${field.type}" for field "${field.name}". ` +
74
+ `Valid types: ${VALID_FIELD_TYPES.join(", ")}`,
75
+ );
76
+ }
77
+ }
78
+
79
+ // Validate anchor_id matches computed hash
80
+ const expected = computeAnchorId(frame.schema);
81
+ if (frame.anchor_id !== expected) {
82
+ throw new NcpError(
83
+ "NCP-ANCHOR-SCHEMA-INVALID",
84
+ `anchor_id mismatch: expected ${expected}, got ${frame.anchor_id}`,
85
+ );
86
+ }
87
+ }
@@ -0,0 +1,59 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
+ //
4
+ // CapsFrame (0x04) — Capsule response envelope
5
+ // NPS-1 §4.4
6
+
7
+ import { NcpError } from "../../core/frame-header.js";
8
+ import {
9
+ computeAnchorId,
10
+ type AnchorFrame,
11
+ type FrameSchema,
12
+ } from "./anchor-frame.js";
13
+
14
+ export interface CapsFrameInlineAnchor {
15
+ anchor_id: string;
16
+ schema: FrameSchema;
17
+ ttl?: number;
18
+ }
19
+
20
+ export interface CapsFrame {
21
+ frame: string;
22
+ anchor_ref: string;
23
+ count: number;
24
+ data: unknown[];
25
+ next_cursor?: string | null;
26
+ token_est?: number;
27
+ tokenizer_used?: string;
28
+ cached?: boolean;
29
+ inline_anchor?: CapsFrameInlineAnchor;
30
+ }
31
+
32
+ /**
33
+ * Validate a CapsFrame.
34
+ *
35
+ * Checks:
36
+ * - count matches data.length (NPS-CLIENT-BAD-FRAME)
37
+ * - if inline_anchor present, recomputes anchor_id and validates match (NCP-ANCHOR-SCHEMA-INVALID)
38
+ *
39
+ * @throws {NcpError} NPS-CLIENT-BAD-FRAME if count doesn't match data length.
40
+ * @throws {NcpError} NCP-ANCHOR-SCHEMA-INVALID if inline_anchor.anchor_id doesn't match schema.
41
+ */
42
+ export function validateCapsFrame(frame: CapsFrame): void {
43
+ if (frame.count !== frame.data.length) {
44
+ throw new NcpError(
45
+ "NPS-CLIENT-BAD-FRAME",
46
+ `CapsFrame count mismatch: count=${frame.count}, data.length=${frame.data.length}`,
47
+ );
48
+ }
49
+
50
+ if (frame.inline_anchor !== undefined) {
51
+ const computed = computeAnchorId(frame.inline_anchor.schema);
52
+ if (frame.inline_anchor.anchor_id !== computed) {
53
+ throw new NcpError(
54
+ "NCP-ANCHOR-SCHEMA-INVALID",
55
+ `inline_anchor anchor_id mismatch: expected ${computed}, got ${frame.inline_anchor.anchor_id}`,
56
+ );
57
+ }
58
+ }
59
+ }
@@ -0,0 +1,69 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
+ //
4
+ // DiffFrame (0x02) — Incremental data patch
5
+ // NPS-1 §4.2
6
+
7
+ import { NcpError, EncodingTier } from "../../core/frame-header.js";
8
+ import { isValidPatchFormat, type PatchFormat } from "../ncp-patch-format.js";
9
+
10
+ export interface JsonPatchOperation {
11
+ op: "add" | "remove" | "replace" | "move" | "copy" | "test";
12
+ path: string;
13
+ value?: unknown;
14
+ from?: string;
15
+ }
16
+
17
+ export interface DiffFrame {
18
+ frame: string;
19
+ anchor_ref: string;
20
+ base_seq: number;
21
+ patch_format?: PatchFormat;
22
+ patch: JsonPatchOperation[] | Uint8Array;
23
+ entity_id?: string;
24
+ }
25
+
26
+ /**
27
+ * Validate DiffFrame base_seq against current sequence.
28
+ * @throws {NcpError} NCP-STREAM-SEQ-GAP if sequences don't match.
29
+ */
30
+ export function validateDiffSeq(frame: DiffFrame, currentSeq: number): void {
31
+ if (frame.base_seq !== currentSeq) {
32
+ throw new NcpError(
33
+ "NCP-STREAM-SEQ-GAP",
34
+ `DiffFrame base_seq=${frame.base_seq} does not match current seq=${currentSeq}`,
35
+ );
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Validate DiffFrame patch_format against the encoding tier.
41
+ *
42
+ * binary_bitset is only supported on Tier-2 (MsgPack) frames.
43
+ * Unknown patch_format values are also rejected.
44
+ *
45
+ * @throws {NcpError} NCP-DIFF-FORMAT-UNSUPPORTED if binary_bitset on non-Tier-2,
46
+ * or if patch_format is an unknown value.
47
+ */
48
+ export function validateDiffFrame(
49
+ frame: DiffFrame,
50
+ encodingTier: EncodingTier | number,
51
+ ): void {
52
+ const fmt = frame.patch_format;
53
+
54
+ // Unknown patch_format
55
+ if (fmt !== undefined && !isValidPatchFormat(fmt)) {
56
+ throw new NcpError(
57
+ "NCP-DIFF-FORMAT-UNSUPPORTED",
58
+ `Unknown patch_format "${String(fmt)}"`,
59
+ );
60
+ }
61
+
62
+ // binary_bitset requires Tier-2 MsgPack
63
+ if (fmt === "binary_bitset" && encodingTier !== EncodingTier.MsgPack) {
64
+ throw new NcpError(
65
+ "NCP-DIFF-FORMAT-UNSUPPORTED",
66
+ "patch_format=binary_bitset requires Tier-2 MsgPack encoding",
67
+ );
68
+ }
69
+ }
@@ -0,0 +1,26 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
+ //
4
+ // ErrorFrame (0xFE) — Unified error frame for all NPS protocol layers
5
+ // NPS-1 §4.7
6
+
7
+ /** Unified error frame shared across all NPS protocol layers. */
8
+ export interface ErrorFrame {
9
+ /** Fixed value "0xFE". */
10
+ frame: string;
11
+ /** NPS status code, e.g. "NPS-CLIENT-NOT-FOUND". */
12
+ status: string;
13
+ /** Protocol-level error code, e.g. "NCP-ANCHOR-NOT-FOUND". */
14
+ error: string;
15
+ /** Human-readable error description. */
16
+ message?: string;
17
+ /** Structured error details (e.g. anchor_ref, stream_id). */
18
+ details?: Record<string, unknown>;
19
+ }
20
+
21
+ /** Type guard for ErrorFrame. */
22
+ export function isErrorFrame(obj: unknown): obj is ErrorFrame {
23
+ if (typeof obj !== "object" || obj === null) return false;
24
+ const o = obj as Record<string, unknown>;
25
+ return o.frame === "0xFE" && typeof o.status === "string" && typeof o.error === "string";
26
+ }
@@ -0,0 +1,50 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
+ //
4
+ // HelloFrame (0x06) — Native-mode client handshake
5
+ // NPS-1 §4.6
6
+
7
+ import { NcpError } from "../../core/frame-header.js";
8
+
9
+ export interface HelloFrame {
10
+ frame: string;
11
+ nps_version: string;
12
+ min_version?: string;
13
+ supported_encodings: string[];
14
+ supported_protocols: string[];
15
+ agent_id?: string;
16
+ max_frame_payload?: number;
17
+ ext_support?: boolean;
18
+ max_concurrent_streams?: number;
19
+ e2e_enc_algorithms?: string[];
20
+ }
21
+
22
+ /**
23
+ * Validate a HelloFrame.
24
+ *
25
+ * Required fields: nps_version, supported_encodings (non-empty), supported_protocols (non-empty).
26
+ *
27
+ * @throws {NcpError} NPS-CLIENT-BAD-FRAME if any required field is missing or empty.
28
+ */
29
+ export function validateHelloFrame(frame: HelloFrame): void {
30
+ if (!frame.nps_version) {
31
+ throw new NcpError(
32
+ "NPS-CLIENT-BAD-FRAME",
33
+ "HelloFrame missing required field: nps_version",
34
+ );
35
+ }
36
+
37
+ if (!frame.supported_encodings || frame.supported_encodings.length === 0) {
38
+ throw new NcpError(
39
+ "NPS-CLIENT-BAD-FRAME",
40
+ "HelloFrame missing required field: supported_encodings (must be non-empty)",
41
+ );
42
+ }
43
+
44
+ if (!frame.supported_protocols || frame.supported_protocols.length === 0) {
45
+ throw new NcpError(
46
+ "NPS-CLIENT-BAD-FRAME",
47
+ "HelloFrame missing required field: supported_protocols (must be non-empty)",
48
+ );
49
+ }
50
+ }
@@ -0,0 +1,35 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
+ //
4
+ // StreamFrame (0x03) — Streaming data chunks with flow control
5
+ // NPS-1 §4.3
6
+
7
+ import { NcpError } from "../../core/frame-header.js";
8
+
9
+ export interface StreamFrame {
10
+ frame: string;
11
+ stream_id: string;
12
+ seq: number;
13
+ is_last: boolean;
14
+ anchor_ref?: string;
15
+ data: unknown[];
16
+ window_size?: number;
17
+ error_code?: string;
18
+ }
19
+
20
+ // UUID v4 format: xxxxxxxx-xxxx-4xxx-[89ab]xxx-xxxxxxxxxxxx
21
+ const UUID_V4_RE =
22
+ /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
23
+
24
+ /**
25
+ * Validate stream_id is a valid UUID v4.
26
+ * @throws {NcpError} NPS-CLIENT-BAD-FRAME if stream_id is not a valid UUID v4.
27
+ */
28
+ export function validateStreamFrame(frame: StreamFrame): void {
29
+ if (!UUID_V4_RE.test(frame.stream_id)) {
30
+ throw new NcpError(
31
+ "NPS-CLIENT-BAD-FRAME",
32
+ `stream_id "${frame.stream_id}" is not a valid UUID v4`,
33
+ );
34
+ }
35
+ }
@@ -0,0 +1,199 @@
1
+ // Copyright 2026 INNO LOTUS PTY LTD
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { EncodingTier, FrameType } from "../core/frames.js";
5
+ import type { NpsFrame } from "../core/codec.js";
6
+
7
+ // ── FrameSchema ───────────────────────────────────────────────────────────────
8
+
9
+ export interface SchemaField {
10
+ name: string;
11
+ type: string;
12
+ semantic?: string;
13
+ nullable?: boolean;
14
+ }
15
+
16
+ export interface FrameSchema {
17
+ fields: readonly SchemaField[];
18
+ }
19
+
20
+ // ── AnchorFrame ───────────────────────────────────────────────────────────────
21
+
22
+ export class AnchorFrame implements NpsFrame {
23
+ readonly frameType = FrameType.ANCHOR;
24
+ readonly preferredTier = EncodingTier.MSGPACK;
25
+
26
+ constructor(
27
+ public readonly anchorId: string,
28
+ public readonly schema: FrameSchema,
29
+ public readonly ttl: number = 3600,
30
+ ) {}
31
+
32
+ toDict(): Record<string, unknown> {
33
+ return {
34
+ anchor_id: this.anchorId,
35
+ schema: { fields: this.schema.fields.map((f) => ({ ...f })) },
36
+ ttl: this.ttl,
37
+ };
38
+ }
39
+
40
+ static fromDict(data: Record<string, unknown>): AnchorFrame {
41
+ const schemaRaw = data["schema"] as { fields: SchemaField[] };
42
+ return new AnchorFrame(
43
+ data["anchor_id"] as string,
44
+ { fields: schemaRaw.fields },
45
+ (data["ttl"] as number | undefined) ?? 3600,
46
+ );
47
+ }
48
+ }
49
+
50
+ // ── JsonPatchOperation ────────────────────────────────────────────────────────
51
+
52
+ export interface JsonPatchOperation {
53
+ op: string;
54
+ path: string;
55
+ value?: unknown;
56
+ }
57
+
58
+ // ── DiffFrame ─────────────────────────────────────────────────────────────────
59
+
60
+ export class DiffFrame implements NpsFrame {
61
+ readonly frameType = FrameType.DIFF;
62
+ readonly preferredTier = EncodingTier.MSGPACK;
63
+
64
+ constructor(
65
+ public readonly anchorRef: string,
66
+ public readonly baseSeq: number,
67
+ public readonly patch: readonly JsonPatchOperation[],
68
+ public readonly entityId?: string,
69
+ ) {}
70
+
71
+ toDict(): Record<string, unknown> {
72
+ return {
73
+ anchor_ref: this.anchorRef,
74
+ base_seq: this.baseSeq,
75
+ patch: this.patch.map((p) => ({ ...p })),
76
+ entity_id: this.entityId ?? null,
77
+ };
78
+ }
79
+
80
+ static fromDict(data: Record<string, unknown>): DiffFrame {
81
+ return new DiffFrame(
82
+ data["anchor_ref"] as string,
83
+ data["base_seq"] as number,
84
+ data["patch"] as JsonPatchOperation[],
85
+ (data["entity_id"] as string | null) ?? undefined,
86
+ );
87
+ }
88
+ }
89
+
90
+ // ── StreamFrame ───────────────────────────────────────────────────────────────
91
+
92
+ export class StreamFrame implements NpsFrame {
93
+ readonly frameType = FrameType.STREAM;
94
+ readonly preferredTier = EncodingTier.MSGPACK;
95
+
96
+ constructor(
97
+ public readonly streamId: string,
98
+ public readonly seq: number,
99
+ public readonly isLast: boolean,
100
+ public readonly data: readonly Record<string, unknown>[],
101
+ public readonly anchorRef?: string,
102
+ public readonly windowSize?: number,
103
+ ) {}
104
+
105
+ toDict(): Record<string, unknown> {
106
+ return {
107
+ stream_id: this.streamId,
108
+ seq: this.seq,
109
+ is_last: this.isLast,
110
+ data: this.data,
111
+ anchor_ref: this.anchorRef ?? null,
112
+ window_size: this.windowSize ?? null,
113
+ };
114
+ }
115
+
116
+ static fromDict(data: Record<string, unknown>): StreamFrame {
117
+ return new StreamFrame(
118
+ data["stream_id"] as string,
119
+ data["seq"] as number,
120
+ data["is_last"] as boolean,
121
+ data["data"] as Record<string, unknown>[],
122
+ (data["anchor_ref"] as string | null) ?? undefined,
123
+ (data["window_size"] as number | null) ?? undefined,
124
+ );
125
+ }
126
+ }
127
+
128
+ // ── CapsFrame ─────────────────────────────────────────────────────────────────
129
+
130
+ export class CapsFrame implements NpsFrame {
131
+ readonly frameType = FrameType.CAPS;
132
+ readonly preferredTier = EncodingTier.MSGPACK;
133
+
134
+ constructor(
135
+ public readonly anchorRef: string,
136
+ public readonly count: number,
137
+ public readonly data: readonly Record<string, unknown>[],
138
+ public readonly nextCursor?: string,
139
+ public readonly tokenEst?: number,
140
+ public readonly cached?: boolean,
141
+ public readonly tokenizerUsed?: string,
142
+ ) {}
143
+
144
+ toDict(): Record<string, unknown> {
145
+ return {
146
+ anchor_ref: this.anchorRef,
147
+ count: this.count,
148
+ data: this.data,
149
+ next_cursor: this.nextCursor ?? null,
150
+ token_est: this.tokenEst ?? null,
151
+ cached: this.cached ?? null,
152
+ tokenizer_used: this.tokenizerUsed ?? null,
153
+ };
154
+ }
155
+
156
+ static fromDict(data: Record<string, unknown>): CapsFrame {
157
+ return new CapsFrame(
158
+ data["anchor_ref"] as string,
159
+ data["count"] as number,
160
+ data["data"] as Record<string, unknown>[],
161
+ (data["next_cursor"] as string | null) ?? undefined,
162
+ (data["token_est"] as number | null) ?? undefined,
163
+ (data["cached"] as boolean | null) ?? undefined,
164
+ (data["tokenizer_used"] as string | null) ?? undefined,
165
+ );
166
+ }
167
+ }
168
+
169
+ // ── ErrorFrame ────────────────────────────────────────────────────────────────
170
+
171
+ export class ErrorFrame implements NpsFrame {
172
+ readonly frameType = FrameType.ERROR;
173
+ readonly preferredTier = EncodingTier.MSGPACK;
174
+
175
+ constructor(
176
+ public readonly status: string,
177
+ public readonly error: string,
178
+ public readonly message?: string,
179
+ public readonly details?: Record<string, unknown>,
180
+ ) {}
181
+
182
+ toDict(): Record<string, unknown> {
183
+ return {
184
+ status: this.status,
185
+ error: this.error,
186
+ message: this.message ?? null,
187
+ details: this.details ?? null,
188
+ };
189
+ }
190
+
191
+ static fromDict(data: Record<string, unknown>): ErrorFrame {
192
+ return new ErrorFrame(
193
+ data["status"] as string,
194
+ data["error"] as string,
195
+ (data["message"] as string | null) ?? undefined,
196
+ (data["details"] as Record<string, unknown> | null) ?? undefined,
197
+ );
198
+ }
199
+ }
@@ -0,0 +1,95 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
+ //
4
+ // Handshake — Version negotiation and encoding negotiation
5
+ // NPS-1 §2.6
6
+
7
+ /**
8
+ * Parse a "major.minor" (optionally "major.minor.patch") version string into a
9
+ * tuple of numeric components. Invalid parts become NaN which makes subsequent
10
+ * comparisons return false in both directions (safe failure).
11
+ */
12
+ function parseVersion(v: string): number[] {
13
+ return v.split(".").map((p) => Number.parseInt(p, 10));
14
+ }
15
+
16
+ /**
17
+ * Numeric component-wise comparison of two version strings.
18
+ * Returns negative if a < b, zero if equal, positive if a > b.
19
+ * Avoids the lexicographic pitfall where "0.9" > "0.10".
20
+ */
21
+ function compareVersions(a: string, b: string): number {
22
+ const partsA = parseVersion(a);
23
+ const partsB = parseVersion(b);
24
+ const len = Math.max(partsA.length, partsB.length);
25
+ for (let i = 0; i < len; i += 1) {
26
+ const x = partsA[i] ?? 0;
27
+ const y = partsB[i] ?? 0;
28
+ if (x !== y) return x - y;
29
+ }
30
+ return 0;
31
+ }
32
+
33
+ /**
34
+ * Negotiate the session NPS version between client and server.
35
+ *
36
+ * Session version = numeric min of client.nps_version and server.nps_version
37
+ * (component-wise — "0.9" < "0.10" < "1.0").
38
+ * If the effective client minimum (min_version ?? nps_version) > server.nps_version,
39
+ * the versions are incompatible.
40
+ *
41
+ * Spec: NPS-1 §2.6
42
+ */
43
+ export function negotiateVersion(
44
+ client: { nps_version: string; min_version?: string },
45
+ server: { nps_version: string },
46
+ ): { session_version: string; compatible: boolean; error_code?: string } {
47
+ const clientMin = client.min_version ?? client.nps_version;
48
+ const serverVersion = server.nps_version;
49
+
50
+ if (compareVersions(clientMin, serverVersion) > 0) {
51
+ return {
52
+ session_version: serverVersion,
53
+ compatible: false,
54
+ error_code: "NCP-VERSION-INCOMPATIBLE",
55
+ };
56
+ }
57
+
58
+ // Session version = component-wise min of client.nps_version and server.nps_version
59
+ const sessionVersion =
60
+ compareVersions(client.nps_version, serverVersion) <= 0
61
+ ? client.nps_version
62
+ : serverVersion;
63
+
64
+ return { session_version: sessionVersion, compatible: true };
65
+ }
66
+
67
+ /**
68
+ * Negotiate the encoding between client and server preferred lists.
69
+ *
70
+ * Returns the first mutually supported encoding, preferring "msgpack" over "json".
71
+ * Returns null if there is no intersection.
72
+ */
73
+ export function negotiateEncoding(
74
+ client: string[],
75
+ server: string[],
76
+ ): { encoding: string | null } {
77
+ const serverSet = new Set(server);
78
+
79
+ // Prefer msgpack over json (and over any other encoding)
80
+ if (client.includes("msgpack") && serverSet.has("msgpack")) {
81
+ return { encoding: "msgpack" };
82
+ }
83
+ if (client.includes("json") && serverSet.has("json")) {
84
+ return { encoding: "json" };
85
+ }
86
+
87
+ // Fall back to first intersection in client-preference order
88
+ for (const enc of client) {
89
+ if (serverSet.has(enc)) {
90
+ return { encoding: enc };
91
+ }
92
+ }
93
+
94
+ return { encoding: null };
95
+ }
@@ -0,0 +1,12 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
+ export * from "./frames/anchor-frame.js";
4
+ export * from "./frames/caps-frame.js";
5
+ export * from "./frames/diff-frame.js";
6
+ export * from "./frames/error-frame.js";
7
+ export * from "./frames/hello-frame.js";
8
+ export * from "./frames/stream-frame.js";
9
+ export * from "./ncp-error-codes.js";
10
+ export * from "./ncp-patch-format.js";
11
+ export * from "./handshake.js";
12
+ export * from "./stream-manager.js";
@@ -0,0 +1,34 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
+ //
4
+ // NCP Error Codes — All v0.4 protocol error codes
5
+ // NPS-1 §6 + §7.4
6
+ //
7
+ // Implementation-only codes (NCP_FRAME_PARSE_ERROR, NCP_FRAME_INCOMPLETE) cover
8
+ // wire-layer parse failures not in spec §6. See test/ncp_test_results.md spec
9
+ // question 2 for the proposal to register them upstream.
10
+
11
+ export const NCP_ERROR_CODES = {
12
+ // Implementation-only codes (not in spec §6 — see test_results.md spec question 2)
13
+ NCP_FRAME_PARSE_ERROR: "NCP-FRAME-PARSE-ERROR",
14
+ NCP_FRAME_INCOMPLETE: "NCP-FRAME-INCOMPLETE",
15
+ // Spec-defined codes
16
+ NCP_FRAME_UNKNOWN_TYPE: "NCP-FRAME-UNKNOWN-TYPE",
17
+ NCP_FRAME_PAYLOAD_TOO_LARGE: "NCP-FRAME-PAYLOAD-TOO-LARGE",
18
+ NCP_FRAME_FLAGS_INVALID: "NCP-FRAME-FLAGS-INVALID",
19
+ NCP_ANCHOR_NOT_FOUND: "NCP-ANCHOR-NOT-FOUND",
20
+ NCP_ANCHOR_SCHEMA_INVALID: "NCP-ANCHOR-SCHEMA-INVALID",
21
+ NCP_ANCHOR_ID_MISMATCH: "NCP-ANCHOR-ID-MISMATCH",
22
+ NCP_ANCHOR_STALE: "NCP-ANCHOR-STALE",
23
+ NCP_STREAM_SEQ_GAP: "NCP-STREAM-SEQ-GAP",
24
+ NCP_STREAM_NOT_FOUND: "NCP-STREAM-NOT-FOUND",
25
+ NCP_STREAM_LIMIT_EXCEEDED: "NCP-STREAM-LIMIT-EXCEEDED",
26
+ NCP_STREAM_WINDOW_OVERFLOW: "NCP-STREAM-WINDOW-OVERFLOW",
27
+ NCP_ENCODING_UNSUPPORTED: "NCP-ENCODING-UNSUPPORTED",
28
+ NCP_DIFF_FORMAT_UNSUPPORTED: "NCP-DIFF-FORMAT-UNSUPPORTED",
29
+ NCP_VERSION_INCOMPATIBLE: "NCP-VERSION-INCOMPATIBLE",
30
+ NCP_ENC_NOT_NEGOTIATED: "NCP-ENC-NOT-NEGOTIATED",
31
+ NCP_ENC_AUTH_FAILED: "NCP-ENC-AUTH-FAILED",
32
+ } as const;
33
+
34
+ export type NcpErrorCode = typeof NCP_ERROR_CODES[keyof typeof NCP_ERROR_CODES];
@@ -0,0 +1,16 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (c) 2026 LabAcacia / INNO LOTUS PTY LTD
3
+ //
4
+ // NCP Patch Format — DiffFrame patch encoding types
5
+ // NPS-1 §4.2
6
+
7
+ export const PATCH_FORMAT = {
8
+ JSON_PATCH: "json_patch",
9
+ BINARY_BITSET: "binary_bitset",
10
+ } as const;
11
+
12
+ export type PatchFormat = typeof PATCH_FORMAT[keyof typeof PATCH_FORMAT];
13
+
14
+ export function isValidPatchFormat(v: unknown): v is PatchFormat {
15
+ return v === PATCH_FORMAT.JSON_PATCH || v === PATCH_FORMAT.BINARY_BITSET;
16
+ }