cojson 0.8.12 → 0.8.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +89 -83
- package/dist/native/PeerKnownStates.js +1 -1
- package/dist/native/PeerKnownStates.js.map +1 -1
- package/dist/native/PeerState.js +1 -1
- package/dist/native/PeerState.js.map +1 -1
- package/dist/native/PriorityBasedMessageQueue.js +1 -10
- package/dist/native/PriorityBasedMessageQueue.js.map +1 -1
- package/dist/native/base64url.js.map +1 -1
- package/dist/native/base64url.test.js +1 -1
- package/dist/native/base64url.test.js.map +1 -1
- package/dist/native/coValue.js.map +1 -1
- package/dist/native/coValueCore.js +141 -149
- package/dist/native/coValueCore.js.map +1 -1
- package/dist/native/coValueState.js.map +1 -1
- package/dist/native/coValues/account.js +6 -6
- package/dist/native/coValues/account.js.map +1 -1
- package/dist/native/coValues/coList.js +2 -3
- package/dist/native/coValues/coList.js.map +1 -1
- package/dist/native/coValues/coMap.js +1 -1
- package/dist/native/coValues/coMap.js.map +1 -1
- package/dist/native/coValues/coStream.js +3 -5
- package/dist/native/coValues/coStream.js.map +1 -1
- package/dist/native/coValues/group.js +11 -11
- package/dist/native/coValues/group.js.map +1 -1
- package/dist/native/coreToCoValue.js +2 -2
- package/dist/native/coreToCoValue.js.map +1 -1
- package/dist/native/crypto/PureJSCrypto.js +4 -4
- package/dist/native/crypto/PureJSCrypto.js.map +1 -1
- package/dist/native/crypto/crypto.js.map +1 -1
- package/dist/native/exports.js +12 -12
- package/dist/native/exports.js.map +1 -1
- package/dist/native/ids.js.map +1 -1
- package/dist/native/jsonStringify.js.map +1 -1
- package/dist/native/localNode.js +5 -7
- package/dist/native/localNode.js.map +1 -1
- package/dist/native/permissions.js +4 -7
- package/dist/native/permissions.js.map +1 -1
- package/dist/native/priority.js.map +1 -1
- package/dist/native/storage/FileSystem.js.map +1 -1
- package/dist/native/storage/chunksAndKnownStates.js +2 -4
- package/dist/native/storage/chunksAndKnownStates.js.map +1 -1
- package/dist/native/storage/index.js +6 -15
- package/dist/native/storage/index.js.map +1 -1
- package/dist/native/streamUtils.js.map +1 -1
- package/dist/native/sync.js +2 -4
- package/dist/native/sync.js.map +1 -1
- package/dist/native/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
- package/dist/native/typeUtils/expectGroup.js.map +1 -1
- package/dist/native/typeUtils/isAccountID.js.map +1 -1
- package/dist/native/typeUtils/isCoValue.js +1 -1
- package/dist/native/typeUtils/isCoValue.js.map +1 -1
- package/dist/web/PeerKnownStates.js +1 -1
- package/dist/web/PeerKnownStates.js.map +1 -1
- package/dist/web/PeerState.js +1 -1
- package/dist/web/PeerState.js.map +1 -1
- package/dist/web/PriorityBasedMessageQueue.js +1 -10
- package/dist/web/PriorityBasedMessageQueue.js.map +1 -1
- package/dist/web/base64url.js.map +1 -1
- package/dist/web/base64url.test.js +1 -1
- package/dist/web/base64url.test.js.map +1 -1
- package/dist/web/coValue.js.map +1 -1
- package/dist/web/coValueCore.js +141 -149
- package/dist/web/coValueCore.js.map +1 -1
- package/dist/web/coValueState.js.map +1 -1
- package/dist/web/coValues/account.js +6 -6
- package/dist/web/coValues/account.js.map +1 -1
- package/dist/web/coValues/coList.js +2 -3
- package/dist/web/coValues/coList.js.map +1 -1
- package/dist/web/coValues/coMap.js +1 -1
- package/dist/web/coValues/coMap.js.map +1 -1
- package/dist/web/coValues/coStream.js +3 -5
- package/dist/web/coValues/coStream.js.map +1 -1
- package/dist/web/coValues/group.js +11 -11
- package/dist/web/coValues/group.js.map +1 -1
- package/dist/web/coreToCoValue.js +2 -2
- package/dist/web/coreToCoValue.js.map +1 -1
- package/dist/web/crypto/PureJSCrypto.js +4 -4
- package/dist/web/crypto/PureJSCrypto.js.map +1 -1
- package/dist/web/crypto/WasmCrypto.js +5 -5
- package/dist/web/crypto/WasmCrypto.js.map +1 -1
- package/dist/web/crypto/crypto.js.map +1 -1
- package/dist/web/exports.js +12 -12
- package/dist/web/exports.js.map +1 -1
- package/dist/web/ids.js.map +1 -1
- package/dist/web/jsonStringify.js.map +1 -1
- package/dist/web/localNode.js +5 -7
- package/dist/web/localNode.js.map +1 -1
- package/dist/web/permissions.js +4 -7
- package/dist/web/permissions.js.map +1 -1
- package/dist/web/priority.js.map +1 -1
- package/dist/web/storage/FileSystem.js.map +1 -1
- package/dist/web/storage/chunksAndKnownStates.js +2 -4
- package/dist/web/storage/chunksAndKnownStates.js.map +1 -1
- package/dist/web/storage/index.js +6 -15
- package/dist/web/storage/index.js.map +1 -1
- package/dist/web/streamUtils.js.map +1 -1
- package/dist/web/sync.js +2 -4
- package/dist/web/sync.js.map +1 -1
- package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
- package/dist/web/typeUtils/expectGroup.js.map +1 -1
- package/dist/web/typeUtils/isAccountID.js.map +1 -1
- package/dist/web/typeUtils/isCoValue.js +1 -1
- package/dist/web/typeUtils/isCoValue.js.map +1 -1
- package/package.json +4 -14
- package/src/PeerKnownStates.ts +91 -89
- package/src/PeerState.ts +72 -73
- package/src/PriorityBasedMessageQueue.ts +42 -49
- package/src/base64url.test.ts +24 -24
- package/src/base64url.ts +44 -45
- package/src/coValue.ts +45 -45
- package/src/coValueCore.ts +746 -785
- package/src/coValueState.ts +82 -72
- package/src/coValues/account.ts +143 -150
- package/src/coValues/coList.ts +520 -522
- package/src/coValues/coMap.ts +283 -285
- package/src/coValues/coStream.ts +320 -324
- package/src/coValues/group.ts +306 -305
- package/src/coreToCoValue.ts +28 -31
- package/src/crypto/PureJSCrypto.ts +188 -194
- package/src/crypto/WasmCrypto.ts +236 -254
- package/src/crypto/crypto.ts +302 -309
- package/src/exports.ts +116 -116
- package/src/ids.ts +9 -9
- package/src/jsonStringify.ts +46 -46
- package/src/jsonValue.ts +24 -10
- package/src/localNode.ts +635 -660
- package/src/media.ts +3 -3
- package/src/permissions.ts +272 -278
- package/src/priority.ts +21 -19
- package/src/storage/FileSystem.ts +91 -99
- package/src/storage/chunksAndKnownStates.ts +110 -115
- package/src/storage/index.ts +466 -497
- package/src/streamUtils.ts +60 -60
- package/src/sync.ts +593 -615
- package/src/tests/PeerKnownStates.test.ts +38 -34
- package/src/tests/PeerState.test.ts +101 -64
- package/src/tests/PriorityBasedMessageQueue.test.ts +91 -91
- package/src/tests/account.test.ts +59 -59
- package/src/tests/coList.test.ts +65 -65
- package/src/tests/coMap.test.ts +137 -137
- package/src/tests/coStream.test.ts +254 -257
- package/src/tests/coValueCore.test.ts +153 -156
- package/src/tests/crypto.test.ts +136 -144
- package/src/tests/cryptoImpl.test.ts +205 -197
- package/src/tests/group.test.ts +24 -24
- package/src/tests/permissions.test.ts +1306 -1371
- package/src/tests/priority.test.ts +65 -82
- package/src/tests/sync.test.ts +1300 -1291
- package/src/tests/testUtils.ts +52 -53
- package/src/typeUtils/accountOrAgentIDfromSessionID.ts +4 -4
- package/src/typeUtils/expectGroup.ts +9 -9
- package/src/typeUtils/isAccountID.ts +1 -1
- package/src/typeUtils/isCoValue.ts +9 -9
- package/tsconfig.json +4 -6
- package/tsconfig.native.json +9 -11
- package/tsconfig.web.json +4 -10
- package/.eslintrc.cjs +0 -25
- 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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
CoValueCore,
|
|
5
|
+
type CoValueUniqueness,
|
|
6
|
+
MAX_RECOMMENDED_TX_SIZE,
|
|
7
|
+
idforHeader,
|
|
6
8
|
} from "./coValueCore.js";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
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 {
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
CryptoProvider,
|
|
22
|
+
StreamingHash,
|
|
23
|
+
secretSeedLength,
|
|
24
|
+
shortHashLength,
|
|
18
25
|
} from "./crypto/crypto.js";
|
|
19
|
-
import {
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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 {
|
|
36
|
-
import type { CoID, AnyRawCoValue } from "./coValue.js";
|
|
35
|
+
import type { AnyRawCoValue, CoID } from "./coValue.js";
|
|
37
36
|
import type {
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
-
|
|
8
|
+
return typeof id === "string" && id.startsWith("co_z");
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export function rawCoIDtoBytes(id: RawCoID): Uint8Array {
|
|
12
|
-
|
|
12
|
+
return base58.decode(id.substring("co_z".length));
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export function rawCoIDfromBytes(bytes: Uint8Array): RawCoID {
|
|
16
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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}`;
|
package/src/jsonStringify.ts
CHANGED
|
@@ -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
|
-
|
|
8
|
+
data: T | undefined,
|
|
9
9
|
): Stringified<T> | undefined {
|
|
10
|
-
|
|
10
|
+
const cycles = false;
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
if (node && node.toJSON && typeof node.toJSON === "function") {
|
|
18
|
+
node = node.toJSON();
|
|
19
|
+
}
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
44
|
+
if (node === null) return "null" as Stringified<T>;
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
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> &
|
|
9
|
-
|
|
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> =
|
|
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<{
|
|
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> =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
export type
|
|
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;
|