@peers-app/peers-sdk 0.27.2 → 0.27.4

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 (260) hide show
  1. package/dist/contracts/contract-proxy.d.ts +7 -0
  2. package/dist/contracts/contract-proxy.js +19 -2
  3. package/dist/contracts/create-local-consumer.js +16 -1
  4. package/dist/contracts/index.d.ts +2 -0
  5. package/dist/contracts/index.js +2 -0
  6. package/dist/contracts/system/index.d.ts +2 -0
  7. package/dist/contracts/system/index.js +2 -0
  8. package/dist/contracts/system/invites-contract.js +2 -1
  9. package/dist/contracts/system/sync-check-contract.d.ts +97 -0
  10. package/dist/contracts/system/sync-check-contract.js +115 -0
  11. package/dist/contracts/tool-arguments.d.ts +15 -0
  12. package/dist/contracts/tool-arguments.js +41 -0
  13. package/dist/contracts/tool-catalog-service.js +2 -12
  14. package/dist/data/change-tracking.d.ts +40 -0
  15. package/dist/data/change-tracking.js +89 -0
  16. package/dist/data/orm/types.d.ts +24 -7
  17. package/dist/data/orm/types.js +97 -24
  18. package/dist/data/persistent-vars.d.ts +5 -0
  19. package/dist/data/persistent-vars.js +24 -6
  20. package/dist/define/manifest.js +6 -34
  21. package/dist/package-loader/package-loader.js +27 -22
  22. package/dist/system-ids.d.ts +5 -0
  23. package/dist/system-ids.js +6 -1
  24. package/package.json +4 -2
  25. package/dist/context/user-context-identity.test.d.ts +0 -1
  26. package/dist/context/user-context-identity.test.js +0 -220
  27. package/dist/contracts/__tests__/action-dispatch.test.d.ts +0 -1
  28. package/dist/contracts/__tests__/action-dispatch.test.js +0 -319
  29. package/dist/contracts/__tests__/also-implements.test.d.ts +0 -1
  30. package/dist/contracts/__tests__/also-implements.test.js +0 -110
  31. package/dist/contracts/__tests__/builder.test.d.ts +0 -1
  32. package/dist/contracts/__tests__/builder.test.js +0 -669
  33. package/dist/contracts/__tests__/contract-events.test.d.ts +0 -1
  34. package/dist/contracts/__tests__/contract-events.test.js +0 -199
  35. package/dist/contracts/__tests__/contract-observables.test.d.ts +0 -1
  36. package/dist/contracts/__tests__/contract-observables.test.js +0 -290
  37. package/dist/contracts/__tests__/contract-provider-router.test.d.ts +0 -1
  38. package/dist/contracts/__tests__/contract-provider-router.test.js +0 -315
  39. package/dist/contracts/__tests__/contract-proxy.test.d.ts +0 -1
  40. package/dist/contracts/__tests__/contract-proxy.test.js +0 -657
  41. package/dist/contracts/__tests__/contract-resolvers.test.d.ts +0 -1
  42. package/dist/contracts/__tests__/contract-resolvers.test.js +0 -758
  43. package/dist/contracts/__tests__/contract-subscription-lifecycle.test.d.ts +0 -1
  44. package/dist/contracts/__tests__/contract-subscription-lifecycle.test.js +0 -353
  45. package/dist/contracts/__tests__/contract-table-events.test.d.ts +0 -1
  46. package/dist/contracts/__tests__/contract-table-events.test.js +0 -209
  47. package/dist/contracts/__tests__/contract-transport.test.d.ts +0 -1
  48. package/dist/contracts/__tests__/contract-transport.test.js +0 -163
  49. package/dist/contracts/__tests__/extract.test.d.ts +0 -1
  50. package/dist/contracts/__tests__/extract.test.js +0 -145
  51. package/dist/contracts/__tests__/frozen-contracts.test.d.ts +0 -1
  52. package/dist/contracts/__tests__/frozen-contracts.test.js +0 -191
  53. package/dist/contracts/__tests__/generic-tool-dispatch.test.d.ts +0 -1
  54. package/dist/contracts/__tests__/generic-tool-dispatch.test.js +0 -130
  55. package/dist/contracts/__tests__/host-contract-client.test.d.ts +0 -1
  56. package/dist/contracts/__tests__/host-contract-client.test.js +0 -793
  57. package/dist/contracts/__tests__/host-contract-consumer.test.d.ts +0 -1
  58. package/dist/contracts/__tests__/host-contract-consumer.test.js +0 -163
  59. package/dist/contracts/__tests__/host-contract-transport.test.d.ts +0 -1
  60. package/dist/contracts/__tests__/host-contract-transport.test.js +0 -66
  61. package/dist/contracts/__tests__/integration.test.d.ts +0 -1
  62. package/dist/contracts/__tests__/integration.test.js +0 -348
  63. package/dist/contracts/__tests__/legacy-removal.test.d.ts +0 -1
  64. package/dist/contracts/__tests__/legacy-removal.test.js +0 -128
  65. package/dist/contracts/__tests__/persistent-registry.test.d.ts +0 -1
  66. package/dist/contracts/__tests__/persistent-registry.test.js +0 -109
  67. package/dist/contracts/__tests__/playground.test.d.ts +0 -1
  68. package/dist/contracts/__tests__/playground.test.js +0 -216
  69. package/dist/contracts/__tests__/registry.test.d.ts +0 -1
  70. package/dist/contracts/__tests__/registry.test.js +0 -238
  71. package/dist/contracts/__tests__/remote-contract-transport.test.d.ts +0 -1
  72. package/dist/contracts/__tests__/remote-contract-transport.test.js +0 -119
  73. package/dist/contracts/__tests__/system-contracts.test.d.ts +0 -1
  74. package/dist/contracts/__tests__/system-contracts.test.js +0 -247
  75. package/dist/contracts/__tests__/target-dispatch.d.ts +0 -11
  76. package/dist/contracts/__tests__/target-dispatch.js +0 -24
  77. package/dist/contracts/__tests__/tool-catalog-inventory.test.d.ts +0 -1
  78. package/dist/contracts/__tests__/tool-catalog-inventory.test.js +0 -44
  79. package/dist/contracts/__tests__/tool-catalog-service.test.d.ts +0 -1
  80. package/dist/contracts/__tests__/tool-catalog-service.test.js +0 -482
  81. package/dist/contracts/__tests__/tool-catalog.test.d.ts +0 -1
  82. package/dist/contracts/__tests__/tool-catalog.test.js +0 -1199
  83. package/dist/contracts/__tests__/validate.test.d.ts +0 -1
  84. package/dist/contracts/__tests__/validate.test.js +0 -991
  85. package/dist/contracts/__tests__/workflow-context.test.d.ts +0 -1
  86. package/dist/contracts/__tests__/workflow-context.test.js +0 -50
  87. package/dist/data/access-level.test.d.ts +0 -1
  88. package/dist/data/access-level.test.js +0 -53
  89. package/dist/data/change-tracking.test.d.ts +0 -1
  90. package/dist/data/change-tracking.test.js +0 -62
  91. package/dist/data/files/files.test.d.ts +0 -1
  92. package/dist/data/files/files.test.js +0 -770
  93. package/dist/data/group-members.test.d.ts +0 -1
  94. package/dist/data/group-members.test.js +0 -336
  95. package/dist/data/group-share.test.d.ts +0 -1
  96. package/dist/data/group-share.test.js +0 -110
  97. package/dist/data/groups.test.d.ts +0 -1
  98. package/dist/data/groups.test.js +0 -153
  99. package/dist/data/orm/cursor.test.d.ts +0 -1
  100. package/dist/data/orm/cursor.test.js +0 -314
  101. package/dist/data/orm/data-query.mongo.test.d.ts +0 -1
  102. package/dist/data/orm/data-query.mongo.test.js +0 -367
  103. package/dist/data/orm/data-query.sqlite.test.d.ts +0 -1
  104. package/dist/data/orm/data-query.sqlite.test.js +0 -365
  105. package/dist/data/orm/data-query.test.d.ts +0 -1
  106. package/dist/data/orm/data-query.test.js +0 -545
  107. package/dist/data/orm/dependency-injection.test.d.ts +0 -1
  108. package/dist/data/orm/dependency-injection.test.js +0 -171
  109. package/dist/data/orm/event-registry.test.d.ts +0 -1
  110. package/dist/data/orm/event-registry.test.js +0 -44
  111. package/dist/data/orm/multi-cursors.test.d.ts +0 -1
  112. package/dist/data/orm/multi-cursors.test.js +0 -463
  113. package/dist/data/orm/sql.data-source.test.d.ts +0 -1
  114. package/dist/data/orm/sql.data-source.test.js +0 -731
  115. package/dist/data/orm/table-container-events.test.d.ts +0 -1
  116. package/dist/data/orm/table-container-events.test.js +0 -101
  117. package/dist/data/orm/table-container.test.d.ts +0 -1
  118. package/dist/data/orm/table-container.test.js +0 -273
  119. package/dist/data/orm/table.event-source.test.d.ts +0 -1
  120. package/dist/data/orm/table.event-source.test.js +0 -331
  121. package/dist/data/orm/types.test.d.ts +0 -1
  122. package/dist/data/orm/types.test.js +0 -409
  123. package/dist/data/package-table-identity.test.d.ts +0 -1
  124. package/dist/data/package-table-identity.test.js +0 -46
  125. package/dist/data/package-version-permissions.test.d.ts +0 -1
  126. package/dist/data/package-version-permissions.test.js +0 -107
  127. package/dist/data/package-version-resolver.test.d.ts +0 -4
  128. package/dist/data/package-version-resolver.test.js +0 -543
  129. package/dist/data/package-versions.test.d.ts +0 -1
  130. package/dist/data/package-versions.test.js +0 -227
  131. package/dist/data/persistent-vars.conflict.test.d.ts +0 -1
  132. package/dist/data/persistent-vars.conflict.test.js +0 -126
  133. package/dist/data/persistent-vars.migration.test.d.ts +0 -1
  134. package/dist/data/persistent-vars.migration.test.js +0 -133
  135. package/dist/data/persistent-vars.package-scope.test.d.ts +0 -1
  136. package/dist/data/persistent-vars.package-scope.test.js +0 -27
  137. package/dist/data/sync-recovery-queue.test.d.ts +0 -1
  138. package/dist/data/sync-recovery-queue.test.js +0 -215
  139. package/dist/data/table-signing.test.d.ts +0 -1
  140. package/dist/data/table-signing.test.js +0 -118
  141. package/dist/data/tool-access.test.d.ts +0 -1
  142. package/dist/data/tool-access.test.js +0 -166
  143. package/dist/data/user-permissions.test.d.ts +0 -1
  144. package/dist/data/user-permissions.test.js +0 -305
  145. package/dist/data/user-trust-levels.test.d.ts +0 -1
  146. package/dist/data/user-trust-levels.test.js +0 -37
  147. package/dist/define/also-implements.test.d.ts +0 -1
  148. package/dist/define/also-implements.test.js +0 -123
  149. package/dist/define/declare-contract.test.d.ts +0 -1
  150. package/dist/define/declare-contract.test.js +0 -573
  151. package/dist/define/define.test.d.ts +0 -1
  152. package/dist/define/define.test.js +0 -1010
  153. package/dist/define/empty-fields.test.d.ts +0 -1
  154. package/dist/define/empty-fields.test.js +0 -55
  155. package/dist/define/events-observables.test.d.ts +0 -1
  156. package/dist/define/events-observables.test.js +0 -1013
  157. package/dist/define/serve-members.test.d.ts +0 -1
  158. package/dist/define/serve-members.test.js +0 -630
  159. package/dist/define/vars.test.d.ts +0 -1
  160. package/dist/define/vars.test.js +0 -39
  161. package/dist/device/binary-peer-connection-v2.test.d.ts +0 -1
  162. package/dist/device/binary-peer-connection-v2.test.js +0 -730
  163. package/dist/device/binary-peer-connection.test.d.ts +0 -1
  164. package/dist/device/binary-peer-connection.test.js +0 -184
  165. package/dist/device/connection.test.d.ts +0 -1
  166. package/dist/device/connection.test.js +0 -613
  167. package/dist/device/device-election.test.d.ts +0 -1
  168. package/dist/device/device-election.test.js +0 -516
  169. package/dist/device/device.test.d.ts +0 -1
  170. package/dist/device/device.test.js +0 -243
  171. package/dist/device/frame-cipher.test.d.ts +0 -1
  172. package/dist/device/frame-cipher.test.js +0 -219
  173. package/dist/device/get-trust-level-fn.test.d.ts +0 -1
  174. package/dist/device/get-trust-level-fn.test.js +0 -685
  175. package/dist/device/streamed-socket.test.d.ts +0 -1
  176. package/dist/device/streamed-socket.test.js +0 -216
  177. package/dist/device/tx-encoding.test.d.ts +0 -1
  178. package/dist/device/tx-encoding.test.js +0 -295
  179. package/dist/device-pairing/cpace.test.d.ts +0 -1
  180. package/dist/device-pairing/cpace.test.js +0 -116
  181. package/dist/device-pairing/device-pairing.test.d.ts +0 -1
  182. package/dist/device-pairing/device-pairing.test.js +0 -211
  183. package/dist/hlc.test.d.ts +0 -1
  184. package/dist/hlc.test.js +0 -45
  185. package/dist/host-credentials.test.d.ts +0 -1
  186. package/dist/host-credentials.test.js +0 -88
  187. package/dist/identity/account-email.test.d.ts +0 -1
  188. package/dist/identity/account-email.test.js +0 -59
  189. package/dist/identity/anchor-document.test.d.ts +0 -1
  190. package/dist/identity/anchor-document.test.js +0 -16
  191. package/dist/identity/anchor-resolution.test.d.ts +0 -1
  192. package/dist/identity/anchor-resolution.test.js +0 -130
  193. package/dist/identity/key-change.test.d.ts +0 -1
  194. package/dist/identity/key-change.test.js +0 -811
  195. package/dist/invites/group-approval.test.d.ts +0 -1
  196. package/dist/invites/group-approval.test.js +0 -223
  197. package/dist/invites/invite-token.test.d.ts +0 -1
  198. package/dist/invites/invite-token.test.js +0 -123
  199. package/dist/keys.test.d.ts +0 -1
  200. package/dist/keys.test.js +0 -336
  201. package/dist/logging/console-logger.test.d.ts +0 -1
  202. package/dist/logging/console-logger.test.js +0 -300
  203. package/dist/logging/console-logs.table.test.d.ts +0 -1
  204. package/dist/logging/console-logs.table.test.js +0 -156
  205. package/dist/observable.test.d.ts +0 -1
  206. package/dist/observable.test.js +0 -309
  207. package/dist/package-build/build-package.test.d.ts +0 -1
  208. package/dist/package-build/build-package.test.js +0 -204
  209. package/dist/package-build/contract-add.test.d.ts +0 -1
  210. package/dist/package-build/contract-add.test.js +0 -467
  211. package/dist/package-build/freeze-contracts.test.d.ts +0 -1
  212. package/dist/package-build/freeze-contracts.test.js +0 -355
  213. package/dist/package-installer/package-author-signing.test.d.ts +0 -1
  214. package/dist/package-installer/package-author-signing.test.js +0 -200
  215. package/dist/package-installer/package-cloner.test.d.ts +0 -1
  216. package/dist/package-installer/package-cloner.test.js +0 -278
  217. package/dist/package-installer/package-creator.test.d.ts +0 -1
  218. package/dist/package-installer/package-creator.test.js +0 -908
  219. package/dist/package-installer/package-dev-tools.test.d.ts +0 -1
  220. package/dist/package-installer/package-dev-tools.test.js +0 -64
  221. package/dist/package-installer/package-installer.test.d.ts +0 -1
  222. package/dist/package-installer/package-installer.test.js +0 -882
  223. package/dist/package-installer/package-propagation.test.d.ts +0 -1
  224. package/dist/package-installer/package-propagation.test.js +0 -1269
  225. package/dist/package-installer/package-publisher.test.d.ts +0 -1
  226. package/dist/package-installer/package-publisher.test.js +0 -153
  227. package/dist/package-installer/package-remote-checker.test.d.ts +0 -1
  228. package/dist/package-installer/package-remote-checker.test.js +0 -334
  229. package/dist/package-installer/package-seed-installer.test.d.ts +0 -1
  230. package/dist/package-installer/package-seed-installer.test.js +0 -123
  231. package/dist/package-installer/package-tarball.test.d.ts +0 -1
  232. package/dist/package-installer/package-tarball.test.js +0 -75
  233. package/dist/package-installer/peers-core-installer.test.d.ts +0 -1
  234. package/dist/package-installer/peers-core-installer.test.js +0 -49
  235. package/dist/package-installer/peers-core-trust-anchor.test.d.ts +0 -1
  236. package/dist/package-installer/peers-core-trust-anchor.test.js +0 -155
  237. package/dist/package-loader/contract-package-loader.test.d.ts +0 -1
  238. package/dist/package-loader/contract-package-loader.test.js +0 -274
  239. package/dist/package-loader/host-authority-loading.test.d.ts +0 -1
  240. package/dist/package-loader/host-authority-loading.test.js +0 -104
  241. package/dist/package-loader/host-dispatch-policy.test.d.ts +0 -1
  242. package/dist/package-loader/host-dispatch-policy.test.js +0 -56
  243. package/dist/package-loader/isolated-artifact.test.d.ts +0 -1
  244. package/dist/package-loader/isolated-artifact.test.js +0 -688
  245. package/dist/package-loader/isolated-persistent-var-gateway.test.d.ts +0 -1
  246. package/dist/package-loader/isolated-persistent-var-gateway.test.js +0 -56
  247. package/dist/package-loader/isolated-table-gateway.test.d.ts +0 -1
  248. package/dist/package-loader/isolated-table-gateway.test.js +0 -633
  249. package/dist/package-loader/package-loader.test.d.ts +0 -1
  250. package/dist/package-loader/package-loader.test.js +0 -1764
  251. package/dist/package-loader/resolve-consumed-contracts.test.d.ts +0 -1
  252. package/dist/package-loader/resolve-consumed-contracts.test.js +0 -166
  253. package/dist/serial-json.compat.test.d.ts +0 -1
  254. package/dist/serial-json.compat.test.js +0 -369
  255. package/dist/serial-json.test.d.ts +0 -1
  256. package/dist/serial-json.test.js +0 -401
  257. package/dist/testing/mock-host-contract.test.d.ts +0 -1
  258. package/dist/testing/mock-host-contract.test.js +0 -782
  259. package/dist/utils.test.d.ts +0 -1
  260. package/dist/utils.test.js +0 -232
@@ -1,401 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const serial_json_1 = require("./serial-json");
4
- describe("serial-json", () => {
5
- describe("Uint8Array serialization", () => {
6
- it("should serialize and deserialize Uint8Array correctly", () => {
7
- const originalData = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]); // "Hello World"
8
- const testObj = {
9
- data: originalData,
10
- name: "test",
11
- number: 42,
12
- };
13
- // Test toJSON/fromJSON
14
- const serialized = (0, serial_json_1.toJSON)(testObj);
15
- expect(serialized.data).toMatch(/^__UINT8ARRAY /);
16
- const deserialized = (0, serial_json_1.fromJSON)(serialized);
17
- expect(deserialized.data).toBeInstanceOf(Uint8Array);
18
- expect(deserialized.data).toEqual(originalData);
19
- expect(deserialized.name).toBe("test");
20
- expect(deserialized.number).toBe(42);
21
- });
22
- it("should serialize and deserialize nested Uint8Array correctly", () => {
23
- const data1 = new Uint8Array([1, 2, 3]);
24
- const data2 = new Uint8Array([4, 5, 6]);
25
- const testObj = {
26
- chunks: [data1, data2],
27
- metadata: {
28
- firstChunk: data1,
29
- totalSize: 6,
30
- },
31
- };
32
- const serialized = (0, serial_json_1.toJSON)(testObj);
33
- const deserialized = (0, serial_json_1.fromJSON)(serialized);
34
- expect(deserialized.chunks).toHaveLength(2);
35
- expect(deserialized.chunks[0]).toBeInstanceOf(Uint8Array);
36
- expect(deserialized.chunks[1]).toBeInstanceOf(Uint8Array);
37
- expect(deserialized.chunks[0]).toEqual(data1);
38
- expect(deserialized.chunks[1]).toEqual(data2);
39
- expect(deserialized.metadata.firstChunk).toBeInstanceOf(Uint8Array);
40
- expect(deserialized.metadata.firstChunk).toEqual(data1);
41
- expect(deserialized.metadata.totalSize).toBe(6);
42
- });
43
- it("should work with JSON string serialization", () => {
44
- const originalData = new Uint8Array([255, 0, 128, 64]);
45
- const testObj = { binaryData: originalData };
46
- const jsonString = (0, serial_json_1.toJSONString)(testObj);
47
- expect(typeof jsonString).toBe("string");
48
- expect(jsonString).toContain("__UINT8ARRAY");
49
- const deserialized = (0, serial_json_1.fromJSONString)(jsonString);
50
- expect(deserialized.binaryData).toBeInstanceOf(Uint8Array);
51
- expect(deserialized.binaryData).toEqual(originalData);
52
- });
53
- });
54
- describe("Buffer backward compatibility", () => {
55
- it("should serialize and deserialize Buffer correctly", () => {
56
- const originalBuffer = Buffer.from("Hello Buffer");
57
- const testObj = { bufferData: originalBuffer };
58
- const serialized = (0, serial_json_1.toJSON)(testObj);
59
- expect(serialized.bufferData).toMatch(/^__BUFFER /);
60
- const deserialized = (0, serial_json_1.fromJSON)(serialized);
61
- expect(Buffer.isBuffer(deserialized.bufferData)).toBe(true);
62
- expect(deserialized.bufferData).toEqual(originalBuffer);
63
- });
64
- });
65
- // Signatures sign toJSON output (`canonicalJSONString` in keys.ts), so these strings are frozen.
66
- describe("encoded form (pinned)", () => {
67
- it("encodes each extended type as a sentinel string", () => {
68
- const value = {
69
- d: new Date("2023-01-01T00:00:00.000Z"),
70
- u8: new Uint8Array([1, 2, 3]),
71
- buf: Buffer.from([1, 2, 3]),
72
- u: undefined,
73
- nan: NaN,
74
- inf: Infinity,
75
- re: /a+b/gi,
76
- n: null,
77
- t: true,
78
- s: "x",
79
- num: 1.5,
80
- };
81
- const encoded = (0, serial_json_1.toJSONString)(value);
82
- expect(encoded).toBe('{"d":"__DATE 2023-01-01T00:00:00.000Z","u8":"__UINT8ARRAY AQID","buf":"__BUFFER AQID",' +
83
- '"u":"__UNDEFINED","nan":"__NAN","inf":"__INFINITY","re":"__REGEXP /a+b/gi","n":null,' +
84
- '"t":true,"s":"x","num":1.5}');
85
- const decoded = (0, serial_json_1.fromJSONString)(encoded);
86
- expect(decoded.d).toEqual(value.d);
87
- expect(decoded.u8).toBeInstanceOf(Uint8Array);
88
- expect(Buffer.isBuffer(decoded.u8)).toBe(false);
89
- expect(Buffer.isBuffer(decoded.buf)).toBe(true);
90
- expect("u" in decoded).toBe(true);
91
- expect(decoded.u).toBeUndefined();
92
- expect(decoded.nan).toBeNaN();
93
- expect(decoded.inf).toBe(Infinity);
94
- expect(decoded.re).toEqual(/a+b/gi);
95
- expect(decoded).toMatchObject({ n: null, t: true, s: "x", num: 1.5 });
96
- });
97
- it("encodes an Error as its stack and a function as one-way source text", () => {
98
- const encoded = (0, serial_json_1.toJSON)({
99
- e: new Error("boom"),
100
- f: function named() {
101
- return 1;
102
- },
103
- });
104
- expect(encoded.e).toMatch(/^__ERROR Error: boom\n/);
105
- expect(encoded.f).toMatch(/^__FUNCTION function named\(\)/);
106
- const decoded = (0, serial_json_1.fromJSON)(encoded);
107
- expect(decoded.e).toBeInstanceOf(Error);
108
- expect(decoded.e.stack).toBe(encoded.e.substring(8));
109
- expect(decoded.f).toBe(encoded.f);
110
- });
111
- it("tags the first copy of a shared object and points the others at it", () => {
112
- const shared = { s: 1 };
113
- const encoded = (0, serial_json_1.toJSONString)({ a: shared, b: shared, c: [shared] });
114
- expect(encoded).toBe('{"a":{"s":1,"__this_ref":"__duplicate_ref_obj_2"},"b":"__duplicate_ref_obj_2",' +
115
- '"c":["__duplicate_ref_obj_2"]}');
116
- const decoded = (0, serial_json_1.fromJSONString)(encoded);
117
- expect(decoded.a).toEqual({ s: 1 });
118
- expect(decoded.b).toBe(decoded.a);
119
- expect(decoded.c[0]).toBe(decoded.a);
120
- });
121
- it("tags a shared array with an extra first element", () => {
122
- const shared = ["a", "b"];
123
- const encoded = (0, serial_json_1.toJSONString)({ x: shared, y: shared });
124
- expect(encoded).toBe('{"x":["__this_ref:__duplicate_ref_ary_2","a","b"],"y":"__duplicate_ref_ary_2"}');
125
- const decoded = (0, serial_json_1.fromJSONString)(encoded);
126
- expect(decoded.x).toEqual(["a", "b"]);
127
- expect(decoded.y).toBe(decoded.x);
128
- });
129
- it("encodes cycles, placing the tag wherever the copy has got to", () => {
130
- const obj = { n: 1 };
131
- obj.self = obj;
132
- const list = [1];
133
- list.push(list);
134
- const root = { child: { grand: {} } };
135
- root.child.grand.up = root;
136
- expect((0, serial_json_1.toJSONString)(obj)).toBe('{"n":1,"__this_ref":"__duplicate_ref_obj_1","self":"__duplicate_ref_obj_1"}');
137
- expect((0, serial_json_1.toJSONString)(list)).toBe('["__this_ref:__duplicate_ref_ary_1",1,"__duplicate_ref_ary_1"]');
138
- expect((0, serial_json_1.toJSONString)(root)).toBe('{"__this_ref":"__duplicate_ref_obj_1","child":{"grand":{"up":"__duplicate_ref_obj_1"}}}');
139
- const decodedObj = (0, serial_json_1.fromJSONString)((0, serial_json_1.toJSONString)(obj));
140
- expect(decodedObj.self).toBe(decodedObj);
141
- expect(decodedObj.n).toBe(1);
142
- const decodedList = (0, serial_json_1.fromJSONString)((0, serial_json_1.toJSONString)(list));
143
- expect(decodedList).toHaveLength(2);
144
- expect(decodedList[1]).toBe(decodedList);
145
- const decodedRoot = (0, serial_json_1.fromJSONString)((0, serial_json_1.toJSONString)(root));
146
- expect(decodedRoot.child.grand.up).toBe(decodedRoot);
147
- });
148
- it("resolves a reference that appears before its target", () => {
149
- const decoded = (0, serial_json_1.fromJSONString)('{"b":"__duplicate_ref_obj_2","a":{"k":1,"__this_ref":"__duplicate_ref_obj_2"}}');
150
- expect(decoded.a).toEqual({ k: 1 });
151
- expect(decoded.b).toBe(decoded.a);
152
- });
153
- it("escapes keys that start with $", () => {
154
- const encoded = (0, serial_json_1.toJSONString)({ $a: 1, $: 2, plain: { $nested: 3 } });
155
- expect(encoded).toBe('{"__DOLLAR_a":1,"__DOLLAR_":2,"plain":{"__DOLLAR_nested":3}}');
156
- expect((0, serial_json_1.fromJSONString)(encoded)).toEqual({ $a: 1, $: 2, plain: { $nested: 3 } });
157
- });
158
- it("leaves top-level primitives alone", () => {
159
- expect((0, serial_json_1.toJSON)(NaN)).toBeNaN();
160
- expect((0, serial_json_1.toJSONString)(NaN)).toBe("null");
161
- expect((0, serial_json_1.toJSON)(undefined)).toBeUndefined();
162
- expect((0, serial_json_1.toJSONString)(undefined)).toBeUndefined();
163
- expect((0, serial_json_1.toJSON)("__UNDEFINED")).toBe("__UNDEFINED");
164
- expect((0, serial_json_1.toJSON)(5)).toBe(5);
165
- expect((0, serial_json_1.toJSON)(new Date(0))).toBe("__DATE 1970-01-01T00:00:00.000Z");
166
- expect((0, serial_json_1.fromJSON)("__DATE 1970-01-01T00:00:00.000Z")).toEqual(new Date(0));
167
- });
168
- });
169
- // Behavior that is wrong or lossy today. Each test changes when its gap is fixed.
170
- describe("known gaps (pinned until fixed)", () => {
171
- it("decodes ordinary strings that look like markers as the marked value", () => {
172
- // The orphan marker warns; restore so the spy doesn't leak into later tests.
173
- const warn = jest.spyOn(console, "warn").mockImplementation(() => undefined);
174
- const decoded = (0, serial_json_1.fromJSON)((0, serial_json_1.toJSON)({
175
- u: "__UNDEFINED",
176
- n: "__NAN",
177
- d: "__DATE tomorrow",
178
- e: "__ERROR oops",
179
- r: "__duplicate_ref_obj_9",
180
- }));
181
- warn.mockRestore();
182
- expect(decoded.u).toBeUndefined();
183
- expect(decoded.n).toBeNaN();
184
- expect(decoded.d).toBeInstanceOf(Date);
185
- expect(decoded.d.getTime()).toBeNaN();
186
- expect(decoded.e).toBeInstanceOf(Error);
187
- expect(decoded.r).toEqual({});
188
- });
189
- it("turns a __DOLLAR_ key into a $ key", () => {
190
- expect((0, serial_json_1.fromJSONString)((0, serial_json_1.toJSONString)({ __DOLLAR_b: 2 }))).toEqual({ $b: 2 });
191
- });
192
- it("treats a __this_ref key as a shared-reference tag unless its value is falsy", () => {
193
- expect((0, serial_json_1.fromJSONString)((0, serial_json_1.toJSONString)({ __this_ref: "mine", a: 1 }))).toEqual({ a: 1 });
194
- expect((0, serial_json_1.fromJSONString)((0, serial_json_1.toJSONString)({ __this_ref: "", a: 1 }))).toEqual({
195
- __this_ref: "",
196
- a: 1,
197
- });
198
- });
199
- it("loses -Infinity and -0 in JSON text", () => {
200
- expect((0, serial_json_1.toJSONString)({ a: -Infinity, z: -0 })).toBe('{"a":null,"z":0}');
201
- });
202
- it("stores Map, Set and class instances as plain objects", () => {
203
- class Point {
204
- x = 1;
205
- }
206
- const encoded = (0, serial_json_1.toJSONString)({ m: new Map([[1, 2]]), s: new Set([1]), p: new Point() });
207
- expect(encoded).toBe('{"m":{},"s":{},"p":{"x":1}}');
208
- expect(Object.getPrototypeOf((0, serial_json_1.fromJSONString)(encoded).p)).toBe(Object.prototype);
209
- });
210
- it("drops symbols and throws on BigInt and invalid dates", () => {
211
- expect((0, serial_json_1.toJSONString)({ s: Symbol("x"), a: [Symbol("y")] })).toBe('{"a":[null]}');
212
- expect(() => (0, serial_json_1.toJSONString)({ b: BigInt(1) })).toThrow(TypeError);
213
- expect(() => (0, serial_json_1.toJSONString)({ d: new Date(Number.NaN) })).toThrow(RangeError);
214
- });
215
- it("throws SyntaxError on a malformed regular expression", () => {
216
- // working/tools/foreman/test/host.test.mjs relies on this error.
217
- expect(() => (0, serial_json_1.fromJSON)("__REGEXP /[/")).toThrow(SyntaxError);
218
- });
219
- });
220
- // Intended differences from the frozen oracle (serial-json.compat.test.ts covers the rest).
221
- describe("decoding changes since the oracle", () => {
222
- let warn;
223
- beforeEach(() => {
224
- warn = jest.spyOn(console, "warn").mockImplementation(() => undefined);
225
- });
226
- afterEach(() => {
227
- warn.mockRestore();
228
- });
229
- it("returns values that are already decoded as they are", () => {
230
- const value = {
231
- bytes: new Uint8Array([1, 2]),
232
- buffer: Buffer.from([3]),
233
- error: new Error("boom"),
234
- pattern: /x/g,
235
- when: new Date(0),
236
- };
237
- const decoded = (0, serial_json_1.fromJSON)({ ...value, encoded: "__DATE 1970-01-01T00:00:00.000Z" });
238
- for (const key of Object.keys(value)) {
239
- expect(decoded[key]).toBe(value[key]);
240
- }
241
- expect(decoded.encoded).toEqual(new Date(0));
242
- });
243
- it("builds new arrays and objects and leaves the input untouched", () => {
244
- const input = Object.freeze({
245
- list: Object.freeze(["__this_ref:__duplicate_ref_ary_2", 1]),
246
- tagged: Object.freeze({ __this_ref: "__duplicate_ref_obj_3", $: Object.freeze({}) }),
247
- again: "__duplicate_ref_ary_2",
248
- });
249
- const decoded = (0, serial_json_1.fromJSON)(input);
250
- expect(decoded).toEqual({ list: [1], tagged: { $: {} }, again: [1] });
251
- expect(decoded.again).toBe(decoded.list);
252
- expect(decoded.list).not.toBe(input.list);
253
- expect(decoded.tagged).not.toBe(input.tagged);
254
- expect(decoded.tagged.$).not.toBe(input.tagged.$);
255
- });
256
- it("copies untagged nested values instead of sharing live change values", () => {
257
- const input = { nested: { list: [{ value: 1 }] } };
258
- const decoded = (0, serial_json_1.fromJSON)(input);
259
- decoded.nested.list[0].value = 2;
260
- decoded.nested.list.push({});
261
- expect(input).toEqual({ nested: { list: [{ value: 1 }] } });
262
- });
263
- it("does not let an object fill an array target, or an array an object target", () => {
264
- // The oracle shared them, so `{"length":4e9}` under an array marker decoded to a huge
265
- // array that hung whatever re-encoded or cloned it, and `{"length":-1}` threw.
266
- const decoded = (0, serial_json_1.fromJSONString)('["__duplicate_ref_ary_1",{"__this_ref":"__duplicate_ref_ary_1","length":4000000000,"a":1},' +
267
- '["__this_ref:__duplicate_ref_ary_1","last"],["__this_ref:__duplicate_ref_obj_1",1],' +
268
- '{"__this_ref":"__duplicate_ref_obj_1","b":2}]');
269
- expect(decoded[0]).toBe(decoded[2]);
270
- expect(decoded[0]).toEqual(["last"]);
271
- expect(decoded[1]).toEqual({ length: 4000000000, a: 1 });
272
- expect(decoded[3]).toEqual([1]);
273
- expect(decoded[4]).toEqual({ b: 2 });
274
- expect(warn).not.toHaveBeenCalled();
275
- expect((0, serial_json_1.fromJSONString)('["__duplicate_ref_ary_2",{"__this_ref":"__duplicate_ref_ary_2","length":-1}]')).toEqual([[], { length: -1 }]);
276
- expect(warn).toHaveBeenCalledTimes(1);
277
- expect(warn).toHaveBeenCalledWith(expect.stringContaining("__duplicate_ref_ary_2"));
278
- });
279
- it("warns about an object marker with only a rejected array tag", () => {
280
- const marker = "__duplicate_ref_obj_rejected_array";
281
- expect((0, serial_json_1.fromJSON)([marker, [`__this_ref:${marker}`, 1]])).toEqual([{}, [1]]);
282
- (0, serial_json_1.fromJSON)([marker, [`__this_ref:${marker}`, 2]]);
283
- expect(warn).toHaveBeenCalledTimes(1);
284
- expect(warn).toHaveBeenCalledWith(expect.stringContaining(marker));
285
- });
286
- it("accepts a later matching tag after rejecting a conflicting tag", () => {
287
- const marker = "__duplicate_ref_obj_later_target";
288
- const decoded = (0, serial_json_1.fromJSON)([
289
- marker,
290
- [`__this_ref:${marker}`, 1],
291
- { __this_ref: marker, value: 2 },
292
- ]);
293
- expect(decoded).toEqual([{ value: 2 }, [1], { value: 2 }]);
294
- expect(decoded[0]).toBe(decoded[2]);
295
- expect(warn).not.toHaveBeenCalled();
296
- });
297
- it("leaves the browser-only __HTML and __WINDOW sentinels as strings", () => {
298
- expect((0, serial_json_1.fromJSON)({ html: "__HTML <b>x</b>", win: "__WINDOW" })).toEqual({
299
- html: "__HTML <b>x</b>",
300
- win: "__WINDOW",
301
- });
302
- });
303
- it("warns once, in one string, about a marker with no target", () => {
304
- const marker = "__duplicate_ref_obj_9001";
305
- expect((0, serial_json_1.fromJSON)({ a: marker, b: { __this_ref: "__duplicate_ref_obj_9002" } })).toEqual({
306
- a: {},
307
- b: {},
308
- });
309
- (0, serial_json_1.fromJSON)({ again: marker });
310
- expect(warn).toHaveBeenCalledTimes(1);
311
- expect(warn.mock.calls[0]).toHaveLength(1);
312
- expect(warn.mock.calls[0][0]).toContain(marker);
313
- expect(warn.mock.calls[0][0]).not.toContain("9002");
314
- });
315
- it("does not warn about a marker whose target comes later", () => {
316
- (0, serial_json_1.fromJSON)({ b: "__duplicate_ref_obj_9003", a: { __this_ref: "__duplicate_ref_obj_9003" } });
317
- expect(warn).not.toHaveBeenCalled();
318
- });
319
- it("caps warnings at 100 distinct markers across calls", () => {
320
- jest.isolateModules(() => {
321
- const isolated = require("./serial-json");
322
- for (let i = 0; i < 110; i++) {
323
- const marker = `__duplicate_ref_obj_cap_${i}`;
324
- isolated.fromJSON([marker, marker]);
325
- isolated.fromJSON(marker);
326
- }
327
- expect(warn).toHaveBeenCalledTimes(100);
328
- expect(warn.mock.calls.every((args) => args.length === 1 && typeof args[0] === "string")).toBe(true);
329
- });
330
- });
331
- it("can decode its warning through the log pipeline without warning again", () => {
332
- warn.mockImplementation((message) => {
333
- expect((0, serial_json_1.fromJSONString)((0, serial_json_1.toJSONString)({ message }))).toEqual({ message });
334
- });
335
- (0, serial_json_1.fromJSON)("__duplicate_ref_obj_log_pipeline");
336
- expect(warn).toHaveBeenCalledTimes(1);
337
- });
338
- });
339
- describe("untrusted input", () => {
340
- afterEach(() => {
341
- // A failing case must not leak a polluted prototype into later tests.
342
- delete Object.prototype.polluted;
343
- delete Object.prototype[0];
344
- delete Object.polluted;
345
- });
346
- it("does not resolve an object's shared-reference tag to Object.prototype", () => {
347
- const decoded = (0, serial_json_1.fromJSONString)('{"__this_ref":"__proto__","polluted":"yes"}');
348
- expect({}.polluted).toBeUndefined();
349
- expect(decoded).toEqual({ polluted: "yes" });
350
- });
351
- it("does not resolve an array's shared-reference tag to Object.prototype", () => {
352
- const decoded = (0, serial_json_1.fromJSONString)('["__this_ref:__proto__","yes"]');
353
- expect({}[0]).toBeUndefined();
354
- expect(decoded).toEqual(["yes"]);
355
- });
356
- it("does not resolve a shared-reference tag to the Object constructor", () => {
357
- const decoded = (0, serial_json_1.fromJSONString)('{"__this_ref":"constructor","polluted":"yes"}');
358
- expect(Object.polluted).toBeUndefined();
359
- expect(decoded).toEqual({ polluted: "yes" });
360
- });
361
- it("keeps constructor and prototype properties local to the decoded value", () => {
362
- const decoded = (0, serial_json_1.fromJSONString)('{"constructor":{"prototype":{"polluted":"yes"}},"prototype":{"polluted":"yes"}}');
363
- expect({}.polluted).toBeUndefined();
364
- expect(Object.polluted).toBeUndefined();
365
- expect(decoded.constructor.prototype.polluted).toBe("yes");
366
- });
367
- it("ignores a shared-reference tag that is an object instead of coercing it", () => {
368
- const decoded = (0, serial_json_1.fromJSONString)('{"a":{"__this_ref":{"toString":"x"},"k":1},"b":{"__this_ref":{},"k":2}}');
369
- expect(decoded).toEqual({ a: { k: 1 }, b: { k: 2 } });
370
- expect(decoded.a).not.toBe(decoded.b);
371
- });
372
- it("drops __proto__ keys instead of setting the decoded object's prototype", () => {
373
- const decoded = (0, serial_json_1.fromJSONString)('{"__proto__":{"polluted":"yes"},"b":{"__proto__":[]}}');
374
- expect(Object.getPrototypeOf(decoded)).toBe(Object.prototype);
375
- expect(Object.getPrototypeOf(decoded.b)).toBe(Object.prototype);
376
- expect(decoded.polluted).toBeUndefined();
377
- expect(Object.keys(decoded)).toEqual(["b"]);
378
- });
379
- });
380
- describe("existing functionality", () => {
381
- it("should still handle other special types", () => {
382
- const testObj = {
383
- date: new Date("2023-01-01T00:00:00.000Z"),
384
- regex: /test/gi,
385
- error: new Error("test error"),
386
- nan: NaN,
387
- infinity: Infinity,
388
- undef: undefined,
389
- };
390
- const deserialized = (0, serial_json_1.fromJSON)((0, serial_json_1.toJSON)(testObj));
391
- expect(deserialized.date).toBeInstanceOf(Date);
392
- expect(deserialized.date.toISOString()).toBe("2023-01-01T00:00:00.000Z");
393
- expect(deserialized.regex).toBeInstanceOf(RegExp);
394
- expect(deserialized.regex.toString()).toBe("/test/gi");
395
- expect(deserialized.error).toBeInstanceOf(Error);
396
- expect(Number.isNaN(deserialized.nan)).toBe(true);
397
- expect(deserialized.infinity).toBe(Infinity);
398
- expect(deserialized.undef).toBeUndefined();
399
- });
400
- });
401
- });
@@ -1 +0,0 @@
1
- export {};