cojson 0.8.12 → 0.8.17

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 (164) hide show
  1. package/CHANGELOG.md +95 -83
  2. package/dist/native/PeerKnownStates.js +6 -1
  3. package/dist/native/PeerKnownStates.js.map +1 -1
  4. package/dist/native/PeerState.js +4 -3
  5. package/dist/native/PeerState.js.map +1 -1
  6. package/dist/native/PriorityBasedMessageQueue.js +1 -10
  7. package/dist/native/PriorityBasedMessageQueue.js.map +1 -1
  8. package/dist/native/SyncStateSubscriptionManager.js +70 -0
  9. package/dist/native/SyncStateSubscriptionManager.js.map +1 -0
  10. package/dist/native/base64url.js.map +1 -1
  11. package/dist/native/base64url.test.js +1 -1
  12. package/dist/native/base64url.test.js.map +1 -1
  13. package/dist/native/coValue.js.map +1 -1
  14. package/dist/native/coValueCore.js +141 -149
  15. package/dist/native/coValueCore.js.map +1 -1
  16. package/dist/native/coValueState.js.map +1 -1
  17. package/dist/native/coValues/account.js +6 -6
  18. package/dist/native/coValues/account.js.map +1 -1
  19. package/dist/native/coValues/coList.js +2 -3
  20. package/dist/native/coValues/coList.js.map +1 -1
  21. package/dist/native/coValues/coMap.js +1 -1
  22. package/dist/native/coValues/coMap.js.map +1 -1
  23. package/dist/native/coValues/coStream.js +3 -5
  24. package/dist/native/coValues/coStream.js.map +1 -1
  25. package/dist/native/coValues/group.js +11 -11
  26. package/dist/native/coValues/group.js.map +1 -1
  27. package/dist/native/coreToCoValue.js +2 -2
  28. package/dist/native/coreToCoValue.js.map +1 -1
  29. package/dist/native/crypto/PureJSCrypto.js +4 -4
  30. package/dist/native/crypto/PureJSCrypto.js.map +1 -1
  31. package/dist/native/crypto/crypto.js.map +1 -1
  32. package/dist/native/exports.js +12 -12
  33. package/dist/native/exports.js.map +1 -1
  34. package/dist/native/ids.js.map +1 -1
  35. package/dist/native/jsonStringify.js.map +1 -1
  36. package/dist/native/localNode.js +5 -7
  37. package/dist/native/localNode.js.map +1 -1
  38. package/dist/native/permissions.js +4 -7
  39. package/dist/native/permissions.js.map +1 -1
  40. package/dist/native/priority.js.map +1 -1
  41. package/dist/native/storage/FileSystem.js.map +1 -1
  42. package/dist/native/storage/chunksAndKnownStates.js +2 -4
  43. package/dist/native/storage/chunksAndKnownStates.js.map +1 -1
  44. package/dist/native/storage/index.js +6 -15
  45. package/dist/native/storage/index.js.map +1 -1
  46. package/dist/native/streamUtils.js.map +1 -1
  47. package/dist/native/sync.js +57 -7
  48. package/dist/native/sync.js.map +1 -1
  49. package/dist/native/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  50. package/dist/native/typeUtils/expectGroup.js.map +1 -1
  51. package/dist/native/typeUtils/isAccountID.js.map +1 -1
  52. package/dist/native/typeUtils/isCoValue.js +1 -1
  53. package/dist/native/typeUtils/isCoValue.js.map +1 -1
  54. package/dist/web/PeerKnownStates.js +6 -1
  55. package/dist/web/PeerKnownStates.js.map +1 -1
  56. package/dist/web/PeerState.js +4 -3
  57. package/dist/web/PeerState.js.map +1 -1
  58. package/dist/web/PriorityBasedMessageQueue.js +1 -10
  59. package/dist/web/PriorityBasedMessageQueue.js.map +1 -1
  60. package/dist/web/SyncStateSubscriptionManager.js +70 -0
  61. package/dist/web/SyncStateSubscriptionManager.js.map +1 -0
  62. package/dist/web/base64url.js.map +1 -1
  63. package/dist/web/base64url.test.js +1 -1
  64. package/dist/web/base64url.test.js.map +1 -1
  65. package/dist/web/coValue.js.map +1 -1
  66. package/dist/web/coValueCore.js +141 -149
  67. package/dist/web/coValueCore.js.map +1 -1
  68. package/dist/web/coValueState.js.map +1 -1
  69. package/dist/web/coValues/account.js +6 -6
  70. package/dist/web/coValues/account.js.map +1 -1
  71. package/dist/web/coValues/coList.js +2 -3
  72. package/dist/web/coValues/coList.js.map +1 -1
  73. package/dist/web/coValues/coMap.js +1 -1
  74. package/dist/web/coValues/coMap.js.map +1 -1
  75. package/dist/web/coValues/coStream.js +3 -5
  76. package/dist/web/coValues/coStream.js.map +1 -1
  77. package/dist/web/coValues/group.js +11 -11
  78. package/dist/web/coValues/group.js.map +1 -1
  79. package/dist/web/coreToCoValue.js +2 -2
  80. package/dist/web/coreToCoValue.js.map +1 -1
  81. package/dist/web/crypto/PureJSCrypto.js +4 -4
  82. package/dist/web/crypto/PureJSCrypto.js.map +1 -1
  83. package/dist/web/crypto/WasmCrypto.js +5 -5
  84. package/dist/web/crypto/WasmCrypto.js.map +1 -1
  85. package/dist/web/crypto/crypto.js.map +1 -1
  86. package/dist/web/exports.js +12 -12
  87. package/dist/web/exports.js.map +1 -1
  88. package/dist/web/ids.js.map +1 -1
  89. package/dist/web/jsonStringify.js.map +1 -1
  90. package/dist/web/localNode.js +5 -7
  91. package/dist/web/localNode.js.map +1 -1
  92. package/dist/web/permissions.js +4 -7
  93. package/dist/web/permissions.js.map +1 -1
  94. package/dist/web/priority.js.map +1 -1
  95. package/dist/web/storage/FileSystem.js.map +1 -1
  96. package/dist/web/storage/chunksAndKnownStates.js +2 -4
  97. package/dist/web/storage/chunksAndKnownStates.js.map +1 -1
  98. package/dist/web/storage/index.js +6 -15
  99. package/dist/web/storage/index.js.map +1 -1
  100. package/dist/web/streamUtils.js.map +1 -1
  101. package/dist/web/sync.js +57 -7
  102. package/dist/web/sync.js.map +1 -1
  103. package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  104. package/dist/web/typeUtils/expectGroup.js.map +1 -1
  105. package/dist/web/typeUtils/isAccountID.js.map +1 -1
  106. package/dist/web/typeUtils/isCoValue.js +1 -1
  107. package/dist/web/typeUtils/isCoValue.js.map +1 -1
  108. package/package.json +4 -14
  109. package/src/PeerKnownStates.ts +98 -90
  110. package/src/PeerState.ts +92 -73
  111. package/src/PriorityBasedMessageQueue.ts +42 -49
  112. package/src/SyncStateSubscriptionManager.ts +124 -0
  113. package/src/base64url.test.ts +24 -24
  114. package/src/base64url.ts +44 -45
  115. package/src/coValue.ts +45 -45
  116. package/src/coValueCore.ts +746 -785
  117. package/src/coValueState.ts +82 -72
  118. package/src/coValues/account.ts +143 -150
  119. package/src/coValues/coList.ts +520 -522
  120. package/src/coValues/coMap.ts +283 -285
  121. package/src/coValues/coStream.ts +320 -324
  122. package/src/coValues/group.ts +306 -305
  123. package/src/coreToCoValue.ts +28 -31
  124. package/src/crypto/PureJSCrypto.ts +188 -194
  125. package/src/crypto/WasmCrypto.ts +236 -254
  126. package/src/crypto/crypto.ts +302 -309
  127. package/src/exports.ts +116 -116
  128. package/src/ids.ts +9 -9
  129. package/src/jsonStringify.ts +46 -46
  130. package/src/jsonValue.ts +24 -10
  131. package/src/localNode.ts +635 -660
  132. package/src/media.ts +3 -3
  133. package/src/permissions.ts +272 -278
  134. package/src/priority.ts +21 -19
  135. package/src/storage/FileSystem.ts +91 -99
  136. package/src/storage/chunksAndKnownStates.ts +110 -115
  137. package/src/storage/index.ts +466 -497
  138. package/src/streamUtils.ts +60 -60
  139. package/src/sync.ts +656 -608
  140. package/src/tests/PeerKnownStates.test.ts +38 -34
  141. package/src/tests/PeerState.test.ts +101 -64
  142. package/src/tests/PriorityBasedMessageQueue.test.ts +91 -91
  143. package/src/tests/SyncStateSubscriptionManager.test.ts +232 -0
  144. package/src/tests/account.test.ts +59 -59
  145. package/src/tests/coList.test.ts +65 -65
  146. package/src/tests/coMap.test.ts +137 -137
  147. package/src/tests/coStream.test.ts +254 -257
  148. package/src/tests/coValueCore.test.ts +153 -156
  149. package/src/tests/crypto.test.ts +136 -144
  150. package/src/tests/cryptoImpl.test.ts +205 -197
  151. package/src/tests/group.test.ts +24 -24
  152. package/src/tests/permissions.test.ts +1306 -1371
  153. package/src/tests/priority.test.ts +65 -82
  154. package/src/tests/sync.test.ts +1573 -1263
  155. package/src/tests/testUtils.ts +85 -53
  156. package/src/typeUtils/accountOrAgentIDfromSessionID.ts +4 -4
  157. package/src/typeUtils/expectGroup.ts +9 -9
  158. package/src/typeUtils/isAccountID.ts +1 -1
  159. package/src/typeUtils/isCoValue.ts +9 -9
  160. package/tsconfig.json +4 -6
  161. package/tsconfig.native.json +9 -11
  162. package/tsconfig.web.json +4 -10
  163. package/.eslintrc.cjs +0 -25
  164. package/.prettierrc.js +0 -9
package/src/exports.ts CHANGED
@@ -1,149 +1,149 @@
1
+ import { base64URLtoBytes, bytesToBase64url } from "./base64url.js";
2
+ import { type RawCoValue } from "./coValue.js";
1
3
  import {
2
- CoValueCore,
3
- type CoValueUniqueness,
4
- MAX_RECOMMENDED_TX_SIZE,
5
- idforHeader,
4
+ CoValueCore,
5
+ type CoValueUniqueness,
6
+ MAX_RECOMMENDED_TX_SIZE,
7
+ idforHeader,
6
8
  } from "./coValueCore.js";
7
- import { accountOrAgentIDfromSessionID } from "./typeUtils/accountOrAgentIDfromSessionID.js";
8
- import { LocalNode } from "./localNode.js";
9
- import { type RawCoValue } from "./coValue.js";
10
- import { RawCoMap } from "./coValues/coMap.js";
9
+ import { ControlledAgent, RawControlledAccount } from "./coValues/account.js";
10
+ import {
11
+ RawAccount,
12
+ RawProfile,
13
+ accountHeaderForInitialAgentSecret,
14
+ } from "./coValues/account.js";
11
15
  import { RawCoList } from "./coValues/coList.js";
12
- import { RawCoStream, RawBinaryCoStream } from "./coValues/coStream.js";
16
+ import { RawCoMap } from "./coValues/coMap.js";
17
+ import { RawBinaryCoStream, RawCoStream } from "./coValues/coStream.js";
18
+ import { EVERYONE, RawGroup } from "./coValues/group.js";
19
+ import type { Everyone } from "./coValues/group.js";
13
20
  import {
14
- secretSeedLength,
15
- shortHashLength,
16
- StreamingHash,
17
- CryptoProvider,
21
+ CryptoProvider,
22
+ StreamingHash,
23
+ secretSeedLength,
24
+ shortHashLength,
18
25
  } from "./crypto/crypto.js";
19
- import { connectedPeers, Channel } from "./streamUtils.js";
20
- import { ControlledAgent, RawControlledAccount } from "./coValues/account.js";
21
- import type { Role } from "./permissions.js";
22
- import { rawCoIDtoBytes, rawCoIDfromBytes, isRawCoID } from "./ids.js";
23
- import { RawGroup, EVERYONE } from "./coValues/group.js";
24
- import type { Everyone } from "./coValues/group.js";
25
- import { base64URLtoBytes, bytesToBase64url } from "./base64url.js";
26
+ import { isRawCoID, rawCoIDfromBytes, rawCoIDtoBytes } from "./ids.js";
26
27
  import { parseJSON } from "./jsonStringify.js";
27
- import {
28
- RawAccount,
29
- RawProfile,
30
- accountHeaderForInitialAgentSecret,
31
- } from "./coValues/account.js";
28
+ import { LocalNode } from "./localNode.js";
29
+ import type { Role } from "./permissions.js";
30
+ import { Channel, connectedPeers } from "./streamUtils.js";
31
+ import { accountOrAgentIDfromSessionID } from "./typeUtils/accountOrAgentIDfromSessionID.js";
32
32
  import { expectGroup } from "./typeUtils/expectGroup.js";
33
33
  import { isAccountID } from "./typeUtils/isAccountID.js";
34
34
 
35
- import type { SessionID, AgentID } from "./ids.js";
36
- import type { CoID, AnyRawCoValue } from "./coValue.js";
35
+ import type { AnyRawCoValue, CoID } from "./coValue.js";
37
36
  import type {
38
- BinaryStreamInfo,
39
- BinaryCoStreamMeta,
37
+ AccountMeta,
38
+ RawAccountID,
39
+ RawAccountMigration,
40
+ } from "./coValues/account.js";
41
+ import type {
42
+ BinaryCoStreamMeta,
43
+ BinaryStreamInfo,
40
44
  } from "./coValues/coStream.js";
45
+ import type { InviteSecret } from "./coValues/group.js";
46
+ import type { AgentSecret } from "./crypto/crypto.js";
47
+ import type { AgentID, SessionID } from "./ids.js";
41
48
  import type { JsonValue } from "./jsonValue.js";
49
+ import type * as Media from "./media.js";
42
50
  import type {
43
- SyncMessage,
44
- Peer,
45
- IncomingSyncStream,
46
- OutgoingSyncQueue,
51
+ IncomingSyncStream,
52
+ OutgoingSyncQueue,
53
+ Peer,
54
+ SyncMessage,
47
55
  } from "./sync.js";
48
56
  import { DisconnectedError, PingTimeoutError } from "./sync.js";
49
- import type { AgentSecret } from "./crypto/crypto.js";
50
- import type {
51
- RawAccountID,
52
- AccountMeta,
53
- RawAccountMigration,
54
- } from "./coValues/account.js";
55
- import type { InviteSecret } from "./coValues/group.js";
56
- import type * as Media from "./media.js";
57
57
 
58
58
  type Value = JsonValue | AnyRawCoValue;
59
59
 
60
- import { LSMStorage, BlockFilename, WalFilename } from "./storage/index.js";
61
- import { FileSystem } from "./storage/FileSystem.js";
62
60
  import { getPriorityFromHeader } from "./priority.js";
61
+ import { FileSystem } from "./storage/FileSystem.js";
62
+ import { BlockFilename, LSMStorage, WalFilename } from "./storage/index.js";
63
63
 
64
64
  /** @hidden */
65
65
  export const cojsonInternals = {
66
- connectedPeers,
67
- rawCoIDtoBytes,
68
- rawCoIDfromBytes,
69
- secretSeedLength,
70
- shortHashLength,
71
- expectGroup,
72
- base64URLtoBytes,
73
- bytesToBase64url,
74
- parseJSON,
75
- accountOrAgentIDfromSessionID,
76
- isAccountID,
77
- accountHeaderForInitialAgentSecret,
78
- idforHeader,
79
- StreamingHash,
80
- Channel,
81
- getPriorityFromHeader,
66
+ connectedPeers,
67
+ rawCoIDtoBytes,
68
+ rawCoIDfromBytes,
69
+ secretSeedLength,
70
+ shortHashLength,
71
+ expectGroup,
72
+ base64URLtoBytes,
73
+ bytesToBase64url,
74
+ parseJSON,
75
+ accountOrAgentIDfromSessionID,
76
+ isAccountID,
77
+ accountHeaderForInitialAgentSecret,
78
+ idforHeader,
79
+ StreamingHash,
80
+ Channel,
81
+ getPriorityFromHeader,
82
82
  };
83
83
 
84
84
  export {
85
- LocalNode,
86
- RawGroup,
87
- Role,
88
- EVERYONE,
89
- Everyone,
90
- RawCoMap,
91
- RawCoList,
92
- RawCoStream,
93
- RawBinaryCoStream,
94
- RawCoValue,
95
- CoID,
96
- AnyRawCoValue,
97
- RawAccount,
98
- RawAccountID,
99
- AccountMeta,
100
- RawAccountMigration,
101
- RawProfile as Profile,
102
- SessionID,
103
- Media,
104
- CoValueCore,
105
- ControlledAgent,
106
- RawControlledAccount,
107
- MAX_RECOMMENDED_TX_SIZE,
108
- JsonValue,
109
- Peer,
110
- BinaryStreamInfo,
111
- BinaryCoStreamMeta,
112
- AgentID,
113
- AgentSecret,
114
- InviteSecret,
115
- CryptoProvider,
116
- SyncMessage,
117
- isRawCoID,
118
- LSMStorage,
85
+ LocalNode,
86
+ RawGroup,
87
+ Role,
88
+ EVERYONE,
89
+ Everyone,
90
+ RawCoMap,
91
+ RawCoList,
92
+ RawCoStream,
93
+ RawBinaryCoStream,
94
+ RawCoValue,
95
+ CoID,
96
+ AnyRawCoValue,
97
+ RawAccount,
98
+ RawAccountID,
99
+ AccountMeta,
100
+ RawAccountMigration,
101
+ RawProfile as Profile,
102
+ SessionID,
103
+ Media,
104
+ CoValueCore,
105
+ ControlledAgent,
106
+ RawControlledAccount,
107
+ MAX_RECOMMENDED_TX_SIZE,
108
+ JsonValue,
109
+ Peer,
110
+ BinaryStreamInfo,
111
+ BinaryCoStreamMeta,
112
+ AgentID,
113
+ AgentSecret,
114
+ InviteSecret,
115
+ CryptoProvider,
116
+ SyncMessage,
117
+ isRawCoID,
118
+ LSMStorage,
119
119
  };
120
120
 
121
121
  export type {
122
- Value,
123
- FileSystem,
124
- BlockFilename,
125
- WalFilename,
126
- IncomingSyncStream,
127
- OutgoingSyncQueue,
128
- DisconnectedError,
129
- PingTimeoutError,
130
- CoValueUniqueness,
122
+ Value,
123
+ FileSystem,
124
+ BlockFilename,
125
+ WalFilename,
126
+ IncomingSyncStream,
127
+ OutgoingSyncQueue,
128
+ DisconnectedError,
129
+ PingTimeoutError,
130
+ CoValueUniqueness,
131
131
  };
132
132
 
133
133
  // eslint-disable-next-line @typescript-eslint/no-namespace
134
134
  export namespace CojsonInternalTypes {
135
- export type CoValueKnownState = import("./sync.js").CoValueKnownState;
136
- export type DoneMessage = import("./sync.js").DoneMessage;
137
- export type KnownStateMessage = import("./sync.js").KnownStateMessage;
138
- export type LoadMessage = import("./sync.js").LoadMessage;
139
- export type NewContentMessage = import("./sync.js").NewContentMessage;
140
- export type CoValueHeader = import("./coValueCore.js").CoValueHeader;
141
- export type Transaction = import("./coValueCore.js").Transaction;
142
- export type TransactionID = import("./ids.js").TransactionID;
143
- export type Signature = import("./crypto/crypto.js").Signature;
144
- export type RawCoID = import("./ids.js").RawCoID;
145
- export type ProfileShape = import("./coValues/account.js").ProfileShape;
146
- export type SealerSecret = import("./crypto/crypto.js").SealerSecret;
147
- export type SignerSecret = import("./crypto/crypto.js").SignerSecret;
148
- export type JsonObject = import("./jsonValue.js").JsonObject;
135
+ export type CoValueKnownState = import("./sync.js").CoValueKnownState;
136
+ export type DoneMessage = import("./sync.js").DoneMessage;
137
+ export type KnownStateMessage = import("./sync.js").KnownStateMessage;
138
+ export type LoadMessage = import("./sync.js").LoadMessage;
139
+ export type NewContentMessage = import("./sync.js").NewContentMessage;
140
+ export type CoValueHeader = import("./coValueCore.js").CoValueHeader;
141
+ export type Transaction = import("./coValueCore.js").Transaction;
142
+ export type TransactionID = import("./ids.js").TransactionID;
143
+ export type Signature = import("./crypto/crypto.js").Signature;
144
+ export type RawCoID = import("./ids.js").RawCoID;
145
+ export type ProfileShape = import("./coValues/account.js").ProfileShape;
146
+ export type SealerSecret = import("./crypto/crypto.js").SealerSecret;
147
+ export type SignerSecret = import("./crypto/crypto.js").SignerSecret;
148
+ export type JsonObject = import("./jsonValue.js").JsonObject;
149
149
  }
package/src/ids.ts CHANGED
@@ -1,19 +1,19 @@
1
- import { RawAccountID } from "./coValues/account.js";
2
1
  import { base58 } from "@scure/base";
2
+ import { RawAccountID } from "./coValues/account.js";
3
3
  import { shortHashLength } from "./crypto/crypto.js";
4
4
 
5
5
  export type RawCoID = `co_z${string}`;
6
6
 
7
7
  export function isRawCoID(id: unknown): id is RawCoID {
8
- return typeof id === "string" && id.startsWith("co_z");
8
+ return typeof id === "string" && id.startsWith("co_z");
9
9
  }
10
10
 
11
11
  export function rawCoIDtoBytes(id: RawCoID): Uint8Array {
12
- return base58.decode(id.substring("co_z".length));
12
+ return base58.decode(id.substring("co_z".length));
13
13
  }
14
14
 
15
15
  export function rawCoIDfromBytes(bytes: Uint8Array): RawCoID {
16
- return `co_z${base58.encode(bytes.slice(0, shortHashLength))}` as RawCoID;
16
+ return `co_z${base58.encode(bytes.slice(0, shortHashLength))}` as RawCoID;
17
17
  }
18
18
 
19
19
  export type TransactionID = { sessionID: SessionID; txIndex: number };
@@ -21,11 +21,11 @@ export type TransactionID = { sessionID: SessionID; txIndex: number };
21
21
  export type AgentID = `sealer_z${string}/signer_z${string}`;
22
22
 
23
23
  export function isAgentID(id: string): id is AgentID {
24
- return (
25
- typeof id === "string" &&
26
- id.startsWith("sealer_") &&
27
- id.includes("/signer_")
28
- );
24
+ return (
25
+ typeof id === "string" &&
26
+ id.startsWith("sealer_") &&
27
+ id.includes("/signer_")
28
+ );
29
29
  }
30
30
 
31
31
  export type SessionID = `${RawAccountID | AgentID}_session_z${string}`;
@@ -5,64 +5,64 @@ export type Stringified<T> = string & { __type: T };
5
5
  export function stableStringify<T>(data: T): Stringified<T>;
6
6
  export function stableStringify(data: undefined): undefined;
7
7
  export function stableStringify<T>(
8
- data: T | undefined,
8
+ data: T | undefined,
9
9
  ): Stringified<T> | undefined {
10
- const cycles = false;
10
+ const cycles = false;
11
11
 
12
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
- const seen: any[] = [];
14
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
- let node = data as any;
12
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
+ const seen: any[] = [];
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
+ let node = data as any;
16
16
 
17
- if (node && node.toJSON && typeof node.toJSON === "function") {
18
- node = node.toJSON();
19
- }
17
+ if (node && node.toJSON && typeof node.toJSON === "function") {
18
+ node = node.toJSON();
19
+ }
20
20
 
21
- if (node === undefined) return;
22
- if (typeof node == "number")
23
- return (isFinite(node) ? "" + node : "null") as Stringified<T>;
24
- if (typeof node !== "object") {
25
- if (
26
- typeof node === "string" &&
27
- (node.startsWith("encrypted_U") || node.startsWith("binary_U"))
28
- ) {
29
- return `"${node}"` as Stringified<T>;
30
- }
31
- return JSON.stringify(node) as Stringified<T>;
21
+ if (node === undefined) return;
22
+ if (typeof node == "number")
23
+ return (isFinite(node) ? "" + node : "null") as Stringified<T>;
24
+ if (typeof node !== "object") {
25
+ if (
26
+ typeof node === "string" &&
27
+ (node.startsWith("encrypted_U") || node.startsWith("binary_U"))
28
+ ) {
29
+ return `"${node}"` as Stringified<T>;
32
30
  }
31
+ return JSON.stringify(node) as Stringified<T>;
32
+ }
33
33
 
34
- let i, out;
35
- if (Array.isArray(node)) {
36
- out = "[";
37
- for (i = 0; i < node.length; i++) {
38
- if (i) out += ",";
39
- out += stableStringify(node[i]) || "null";
40
- }
41
- return (out + "]") as Stringified<T>;
34
+ let i, out;
35
+ if (Array.isArray(node)) {
36
+ out = "[";
37
+ for (i = 0; i < node.length; i++) {
38
+ if (i) out += ",";
39
+ out += stableStringify(node[i]) || "null";
42
40
  }
41
+ return (out + "]") as Stringified<T>;
42
+ }
43
43
 
44
- if (node === null) return "null" as Stringified<T>;
44
+ if (node === null) return "null" as Stringified<T>;
45
45
 
46
- if (seen.indexOf(node) !== -1) {
47
- if (cycles) return JSON.stringify("__cycle__") as Stringified<T>;
48
- throw new TypeError("Converting circular structure to JSON");
49
- }
46
+ if (seen.indexOf(node) !== -1) {
47
+ if (cycles) return JSON.stringify("__cycle__") as Stringified<T>;
48
+ throw new TypeError("Converting circular structure to JSON");
49
+ }
50
50
 
51
- const seenIndex = seen.push(node) - 1;
52
- const keys = Object.keys(node).sort();
53
- out = "";
54
- for (i = 0; i < keys.length; i++) {
55
- const key = keys[i]!;
56
- const value = stableStringify(node[key]);
51
+ const seenIndex = seen.push(node) - 1;
52
+ const keys = Object.keys(node).sort();
53
+ out = "";
54
+ for (i = 0; i < keys.length; i++) {
55
+ const key = keys[i]!;
56
+ const value = stableStringify(node[key]);
57
57
 
58
- if (!value) continue;
59
- if (out) out += ",";
60
- out += JSON.stringify(key) + ":" + value;
61
- }
62
- seen.splice(seenIndex, 1);
63
- return ("{" + out + "}") as Stringified<T>;
58
+ if (!value) continue;
59
+ if (out) out += ",";
60
+ out += JSON.stringify(key) + ":" + value;
61
+ }
62
+ seen.splice(seenIndex, 1);
63
+ return ("{" + out + "}") as Stringified<T>;
64
64
  }
65
65
 
66
66
  export function parseJSON<T>(json: Stringified<T>): T {
67
- return JSON.parse(json);
67
+ return JSON.parse(json);
68
68
  }
package/src/jsonValue.ts CHANGED
@@ -5,27 +5,41 @@ export type JsonValue = JsonAtom | JsonArray | JsonObject | RawCoID;
5
5
  export type JsonArray = JsonValue[] | readonly JsonValue[];
6
6
  export type JsonObject = { [key: string]: JsonValue | undefined };
7
7
 
8
- type AtLeastOne<T, U = {[K in keyof T]: Pick<T, K> }> = Partial<T> & U[keyof U];
9
- type ExcludeEmpty<T> = T extends AtLeastOne<T> ? T : never;
8
+ type AtLeastOne<T, U = { [K in keyof T]: Pick<T, K> }> = Partial<T> &
9
+ U[keyof U];
10
+ type ExcludeEmpty<T> = T extends AtLeastOne<T> ? T : never;
10
11
 
11
- export type CoJsonValue<T> = JsonValue | CoJsonObjectWithIndex<T> | CoJsonArray<T>;
12
+ export type CoJsonValue<T> =
13
+ | JsonValue
14
+ | CoJsonObjectWithIndex<T>
15
+ | CoJsonArray<T>;
12
16
  export type CoJsonArray<T> = CoJsonValue<T>[] | readonly CoJsonValue<T>[];
13
17
 
14
18
  /**
15
19
  * Since we are forcing Typescript to elaborate the indexes from the given type passing
16
20
  * non-object values to CoJsonObjectWithIndex will return an empty object
17
- * E.g.
21
+ * E.g.
18
22
  * CoJsonObjectWithIndex<() => void> --> {}
19
23
  * CoJsonObjectWithIndex<RegExp> --> {}
20
- *
24
+ *
21
25
  * Applying the ExcludeEmpty type here to make sure we don't accept functions or non-serializable values
22
26
  */
23
- export type CoJsonObjectWithIndex<T> = ExcludeEmpty<{ [K in keyof T & string]: CoJsonValue1L<T[K]> | undefined }>;
27
+ export type CoJsonObjectWithIndex<T> = ExcludeEmpty<{
28
+ [K in keyof T & string]: CoJsonValue1L<T[K]> | undefined;
29
+ }>;
24
30
 
25
31
  /**
26
32
  * Manually handling the nested interface types to not get into infinite recursion issues.
27
33
  */
28
- export type CoJsonValue1L<T> = ExcludeEmpty<{ [K in keyof T & string]: CoJsonValue2L<T[K]> | undefined }> | JsonValue;
29
- export type CoJsonValue2L<T> = ExcludeEmpty<{ [K in keyof T & string]: CoJsonValue3L<T[K]> | undefined }> | JsonValue;
30
- export type CoJsonValue3L<T> = ExcludeEmpty<{ [K in keyof T & string]: CoJsonValue4L<T[K]> | undefined }> | JsonValue;
31
- export type CoJsonValue4L<T> = ExcludeEmpty<{ [K in keyof T & string]: JsonValue | undefined }> | JsonValue;
34
+ export type CoJsonValue1L<T> =
35
+ | ExcludeEmpty<{ [K in keyof T & string]: CoJsonValue2L<T[K]> | undefined }>
36
+ | JsonValue;
37
+ export type CoJsonValue2L<T> =
38
+ | ExcludeEmpty<{ [K in keyof T & string]: CoJsonValue3L<T[K]> | undefined }>
39
+ | JsonValue;
40
+ export type CoJsonValue3L<T> =
41
+ | ExcludeEmpty<{ [K in keyof T & string]: CoJsonValue4L<T[K]> | undefined }>
42
+ | JsonValue;
43
+ export type CoJsonValue4L<T> =
44
+ | ExcludeEmpty<{ [K in keyof T & string]: JsonValue | undefined }>
45
+ | JsonValue;