api-ape 3.0.1 → 4.1.0
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/README.md +58 -570
- package/client/README.md +73 -14
- package/client/auth/crypto/aead.js +214 -0
- package/client/auth/crypto/constants.js +32 -0
- package/client/auth/crypto/encoding.js +104 -0
- package/client/auth/crypto/files.md +27 -0
- package/client/auth/crypto/kdf.js +217 -0
- package/client/auth/crypto-utils.js +118 -0
- package/client/auth/files.md +52 -0
- package/client/auth/key-recovery.js +288 -0
- package/client/auth/recovery/constants.js +37 -0
- package/client/auth/recovery/files.md +23 -0
- package/client/auth/recovery/key-derivation.js +61 -0
- package/client/auth/recovery/sss-browser.js +189 -0
- package/client/auth/share-storage.js +205 -0
- package/client/auth/storage/constants.js +18 -0
- package/client/auth/storage/db.js +132 -0
- package/client/auth/storage/files.md +27 -0
- package/client/auth/storage/keys.js +173 -0
- package/client/auth/storage/shares.js +200 -0
- package/client/browser.js +190 -23
- package/client/connectSocket.js +418 -988
- package/client/connection/README.md +23 -0
- package/client/connection/fileDownload.js +256 -0
- package/client/connection/fileHandling.js +450 -0
- package/client/connection/fileUtils.js +346 -0
- package/client/connection/files.md +71 -0
- package/client/connection/messageHandler.js +105 -0
- package/client/connection/network.js +350 -0
- package/client/connection/proxy.js +233 -0
- package/client/connection/sender.js +333 -0
- package/client/connection/state.js +321 -0
- package/client/connection/subscriptions.js +151 -0
- package/client/files.md +53 -0
- package/client/index.js +298 -142
- package/client/transports/README.md +50 -0
- package/client/transports/files.md +41 -0
- package/client/transports/streamParser.js +195 -0
- package/client/transports/streaming.js +555 -202
- package/dist/ape.js +6 -1
- package/dist/ape.js.map +4 -4
- package/index.d.ts +38 -16
- package/package.json +32 -7
- package/server/README.md +287 -53
- package/server/adapters/README.md +28 -19
- package/server/adapters/files.md +68 -0
- package/server/adapters/firebase.js +543 -160
- package/server/adapters/index.js +362 -112
- package/server/adapters/mongo.js +530 -140
- package/server/adapters/postgres.js +534 -155
- package/server/adapters/redis.js +508 -143
- package/server/adapters/supabase.js +555 -186
- package/server/client/README.md +43 -0
- package/server/client/connection.js +586 -0
- package/server/client/files.md +40 -0
- package/server/client/index.js +342 -0
- package/server/files.md +54 -0
- package/server/index.js +332 -27
- package/server/lib/README.md +26 -0
- package/server/lib/broadcast/clients.js +219 -0
- package/server/lib/broadcast/files.md +58 -0
- package/server/lib/broadcast/index.js +57 -0
- package/server/lib/broadcast/publishProxy.js +110 -0
- package/server/lib/broadcast/pubsub.js +137 -0
- package/server/lib/broadcast/sendProxy.js +103 -0
- package/server/lib/bun.js +315 -99
- package/server/lib/fileTransfer/README.md +63 -0
- package/server/lib/fileTransfer/files.md +30 -0
- package/server/lib/fileTransfer/streaming.js +435 -0
- package/server/lib/fileTransfer.js +710 -326
- package/server/lib/files.md +111 -0
- package/server/lib/httpUtils.js +283 -0
- package/server/lib/loader.js +208 -7
- package/server/lib/longPolling/README.md +63 -0
- package/server/lib/longPolling/files.md +44 -0
- package/server/lib/longPolling/getHandler.js +365 -0
- package/server/lib/longPolling/postHandler.js +327 -0
- package/server/lib/longPolling.js +174 -221
- package/server/lib/main.js +369 -532
- package/server/lib/runtimes/README.md +42 -0
- package/server/lib/runtimes/bun.js +586 -0
- package/server/lib/runtimes/files.md +56 -0
- package/server/lib/runtimes/node.js +511 -0
- package/server/lib/wiring.js +539 -98
- package/server/lib/ws/README.md +35 -0
- package/server/lib/ws/adapters/README.md +54 -0
- package/server/lib/ws/adapters/bun.js +538 -170
- package/server/lib/ws/adapters/deno.js +623 -149
- package/server/lib/ws/adapters/files.md +42 -0
- package/server/lib/ws/files.md +74 -0
- package/server/lib/ws/frames.js +532 -154
- package/server/lib/ws/index.js +207 -10
- package/server/lib/ws/server.js +385 -92
- package/server/lib/ws/socket.js +549 -181
- package/server/lib/wsProvider.js +363 -89
- package/server/plugins/binary.js +282 -0
- package/server/security/README.md +92 -0
- package/server/security/auth/README.md +319 -0
- package/server/security/auth/adapters/files.md +95 -0
- package/server/security/auth/adapters/ldap/constants.js +37 -0
- package/server/security/auth/adapters/ldap/files.md +19 -0
- package/server/security/auth/adapters/ldap/helpers.js +111 -0
- package/server/security/auth/adapters/ldap.js +353 -0
- package/server/security/auth/adapters/oauth2/constants.js +41 -0
- package/server/security/auth/adapters/oauth2/files.md +19 -0
- package/server/security/auth/adapters/oauth2/helpers.js +123 -0
- package/server/security/auth/adapters/oauth2.js +273 -0
- package/server/security/auth/adapters/opaque-handlers.js +314 -0
- package/server/security/auth/adapters/opaque.js +205 -0
- package/server/security/auth/adapters/saml/constants.js +52 -0
- package/server/security/auth/adapters/saml/files.md +19 -0
- package/server/security/auth/adapters/saml/helpers.js +74 -0
- package/server/security/auth/adapters/saml.js +173 -0
- package/server/security/auth/adapters/totp.js +703 -0
- package/server/security/auth/adapters/webauthn.js +625 -0
- package/server/security/auth/files.md +61 -0
- package/server/security/auth/framework/constants.js +27 -0
- package/server/security/auth/framework/files.md +23 -0
- package/server/security/auth/framework/handlers.js +272 -0
- package/server/security/auth/framework/socket-auth.js +177 -0
- package/server/security/auth/handlers/auth-messages.js +143 -0
- package/server/security/auth/handlers/files.md +28 -0
- package/server/security/auth/index.js +290 -0
- package/server/security/auth/mfa/crypto/aead.js +148 -0
- package/server/security/auth/mfa/crypto/constants.js +35 -0
- package/server/security/auth/mfa/crypto/files.md +27 -0
- package/server/security/auth/mfa/crypto/kdf.js +120 -0
- package/server/security/auth/mfa/crypto/utils.js +68 -0
- package/server/security/auth/mfa/crypto-utils.js +80 -0
- package/server/security/auth/mfa/files.md +77 -0
- package/server/security/auth/mfa/ledger/constants.js +75 -0
- package/server/security/auth/mfa/ledger/errors.js +73 -0
- package/server/security/auth/mfa/ledger/files.md +23 -0
- package/server/security/auth/mfa/ledger/share-record.js +32 -0
- package/server/security/auth/mfa/ledger.js +255 -0
- package/server/security/auth/mfa/recovery/constants.js +67 -0
- package/server/security/auth/mfa/recovery/files.md +19 -0
- package/server/security/auth/mfa/recovery/handlers.js +216 -0
- package/server/security/auth/mfa/recovery.js +191 -0
- package/server/security/auth/mfa/sss/constants.js +21 -0
- package/server/security/auth/mfa/sss/files.md +23 -0
- package/server/security/auth/mfa/sss/gf256.js +103 -0
- package/server/security/auth/mfa/sss/serialization.js +82 -0
- package/server/security/auth/mfa/sss.js +161 -0
- package/server/security/auth/mfa/two-of-three/constants.js +58 -0
- package/server/security/auth/mfa/two-of-three/files.md +23 -0
- package/server/security/auth/mfa/two-of-three/handlers.js +241 -0
- package/server/security/auth/mfa/two-of-three/helpers.js +71 -0
- package/server/security/auth/mfa/two-of-three.js +136 -0
- package/server/security/auth/nonce-manager.js +89 -0
- package/server/security/auth/state-machine-mfa.js +269 -0
- package/server/security/auth/state-machine.js +257 -0
- package/server/security/extractRootDomain.js +144 -16
- package/server/security/files.md +51 -0
- package/server/security/origin.js +197 -15
- package/server/security/reply.js +274 -16
- package/server/socket/README.md +119 -0
- package/server/socket/authMiddleware.js +299 -0
- package/server/socket/files.md +86 -0
- package/server/socket/open.js +154 -8
- package/server/socket/pluginHooks.js +334 -0
- package/server/socket/receive.js +184 -225
- package/server/socket/receiveContext.js +117 -0
- package/server/socket/send.js +416 -78
- package/server/socket/tagUtils.js +402 -0
- package/server/utils/README.md +19 -0
- package/server/utils/deepRequire.js +255 -30
- package/server/utils/files.md +57 -0
- package/server/utils/genId.js +182 -20
- package/server/utils/parseUserAgent.js +313 -251
- package/server/utils/userAgent/README.md +65 -0
- package/server/utils/userAgent/files.md +46 -0
- package/server/utils/userAgent/patterns.js +545 -0
- package/utils/README.md +21 -0
- package/utils/files.md +66 -0
- package/utils/jss/README.md +21 -0
- package/utils/jss/decode.js +471 -0
- package/utils/jss/encode.js +312 -0
- package/utils/jss/files.md +68 -0
- package/utils/jss/plugins.js +210 -0
- package/utils/jss.js +219 -273
- package/utils/messageHash.js +238 -35
- package/dist/api-ape.min.js +0 -2
- package/dist/api-ape.min.js.map +0 -7
- package/server/client.js +0 -308
- package/server/lib/broadcast.js +0 -146
package/utils/jss.js
CHANGED
|
@@ -1,273 +1,219 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
} // END decodeValue
|
|
222
|
-
|
|
223
|
-
function parseKeyWithTags(key) {
|
|
224
|
-
const match = key.match(/(.+)(<!(.)>)/);
|
|
225
|
-
if (match) {
|
|
226
|
-
return [match[1], match[3]];
|
|
227
|
-
}
|
|
228
|
-
// Try multi-character tags like array types [,D,]
|
|
229
|
-
const multiMatch = key.match(/(.+)(<!!(.+)>)/);
|
|
230
|
-
if (multiMatch) {
|
|
231
|
-
return [multiMatch[1], multiMatch[3]];
|
|
232
|
-
}
|
|
233
|
-
// Also handle array type tags that start with [
|
|
234
|
-
const arrayMatch = key.match(/(.+)(<!\[(.*)>)/);
|
|
235
|
-
if (arrayMatch) {
|
|
236
|
-
return [arrayMatch[1], '[' + arrayMatch[3]];
|
|
237
|
-
}
|
|
238
|
-
return [key, undefined];
|
|
239
|
-
} // END parseKeyWithTags
|
|
240
|
-
|
|
241
|
-
for (const key in data) {
|
|
242
|
-
const [name, tag] = parseKeyWithTags(key);
|
|
243
|
-
// Start with path containing just the key name
|
|
244
|
-
result[name] = decodeValue.call([name], name, tag, data[key]);
|
|
245
|
-
}
|
|
246
|
-
pointers2Res.forEach(changeAttributeReference.bind(null, result));
|
|
247
|
-
return result;
|
|
248
|
-
} // END decode
|
|
249
|
-
|
|
250
|
-
function changeAttributeReference(obj, [refPath, attrPath]) {
|
|
251
|
-
// refPath and attrPath are now arrays, no splitting needed
|
|
252
|
-
const refKeys = refPath || [];
|
|
253
|
-
const attrKeys = attrPath || [];
|
|
254
|
-
|
|
255
|
-
// Get the reference target by traversing refPath
|
|
256
|
-
let ref = obj;
|
|
257
|
-
for (let i = 0; i < refKeys.length; i++) {
|
|
258
|
-
ref = ref[refKeys[i]];
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// Get the parent of the attribute to set
|
|
262
|
-
let attr = obj;
|
|
263
|
-
for (let i = 0; i < attrKeys.length - 1; i++) {
|
|
264
|
-
attr = attr[attrKeys[i]];
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// Set the attribute to point to the reference
|
|
268
|
-
attr[attrKeys[attrKeys.length - 1]] = ref;
|
|
269
|
-
return obj;
|
|
270
|
-
} // END changeAttributeReference
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
module.exports = { parse, stringify, encode, decode };
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview JSON Super Set (JSS) - Extended JSON Serialization
|
|
3
|
+
*
|
|
4
|
+
* JSS extends standard JSON to support additional JavaScript types that
|
|
5
|
+
* JSON.stringify/parse cannot handle. This enables api-ape to transparently
|
|
6
|
+
* serialize and deserialize rich data types over WebSocket connections.
|
|
7
|
+
*
|
|
8
|
+
* ## Supported Types
|
|
9
|
+
*
|
|
10
|
+
* | Type | Tag | Description |
|
|
11
|
+
* |----------------|-----|---------------------------------------|
|
|
12
|
+
* | Date | `D` | Serialized as timestamp |
|
|
13
|
+
* | RegExp | `R` | Serialized as string pattern |
|
|
14
|
+
* | Error | `E` | Preserves name, message, and stack |
|
|
15
|
+
* | undefined | `U` | Explicitly represents undefined |
|
|
16
|
+
* | Map | `M` | Converted to/from object entries |
|
|
17
|
+
* | Set | `S` | Converted to/from array |
|
|
18
|
+
* | Circular Refs | `P` | Preserved via path pointers |
|
|
19
|
+
*
|
|
20
|
+
* ## Wire Format
|
|
21
|
+
*
|
|
22
|
+
* JSS encodes type information into object keys using a tag suffix:
|
|
23
|
+
*
|
|
24
|
+
* ```javascript
|
|
25
|
+
* // Original object
|
|
26
|
+
* { createdAt: new Date('2024-01-01'), pattern: /hello/i }
|
|
27
|
+
*
|
|
28
|
+
* // JSS encoded
|
|
29
|
+
* { "createdAt<!D>": 1704067200000, "pattern<!R>": "/hello/i" }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* ## Usage
|
|
33
|
+
*
|
|
34
|
+
* JSS provides a drop-in replacement for JSON.stringify/parse:
|
|
35
|
+
*
|
|
36
|
+
* ```javascript
|
|
37
|
+
* const jss = require('./jss')
|
|
38
|
+
*
|
|
39
|
+
* // Stringify (like JSON.stringify but handles extended types)
|
|
40
|
+
* const str = jss.stringify({ date: new Date(), regex: /test/ })
|
|
41
|
+
*
|
|
42
|
+
* // Parse (like JSON.parse but restores extended types)
|
|
43
|
+
* const obj = jss.parse(str)
|
|
44
|
+
* // obj.date is a Date instance
|
|
45
|
+
* // obj.regex is a RegExp instance
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* ## API Methods
|
|
49
|
+
*
|
|
50
|
+
* - `stringify(obj)` - Convert object to JSS string (high-level)
|
|
51
|
+
* - `parse(str)` - Parse JSS string back to object (high-level)
|
|
52
|
+
* - `encode(obj)` - Convert object to JSS-encoded plain object
|
|
53
|
+
* - `decode(obj)` - Convert JSS-encoded object back to original
|
|
54
|
+
*
|
|
55
|
+
* ## Circular Reference Handling
|
|
56
|
+
*
|
|
57
|
+
* JSS can handle circular references using path pointers:
|
|
58
|
+
*
|
|
59
|
+
* ```javascript
|
|
60
|
+
* const obj = { name: 'root' }
|
|
61
|
+
* obj.self = obj // Circular reference
|
|
62
|
+
*
|
|
63
|
+
* const str = jss.stringify(obj) // No error!
|
|
64
|
+
* const restored = jss.parse(str)
|
|
65
|
+
* console.log(restored.self === restored) // true
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @module utils/jss
|
|
69
|
+
* @see {@link module:utils/jss/encode} for encoding implementation
|
|
70
|
+
* @see {@link module:utils/jss/decode} for decoding implementation
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* // Basic usage with dates
|
|
74
|
+
* const jss = require('./jss')
|
|
75
|
+
*
|
|
76
|
+
* const data = {
|
|
77
|
+
* user: 'Alice',
|
|
78
|
+
* loginAt: new Date(),
|
|
79
|
+
* settings: new Map([['theme', 'dark'], ['lang', 'en']])
|
|
80
|
+
* }
|
|
81
|
+
*
|
|
82
|
+
* const serialized = jss.stringify(data)
|
|
83
|
+
* // Can be sent over WebSocket
|
|
84
|
+
*
|
|
85
|
+
* const restored = jss.parse(serialized)
|
|
86
|
+
* console.log(restored.loginAt instanceof Date) // true
|
|
87
|
+
* console.log(restored.settings instanceof Map) // true
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* // Error serialization
|
|
91
|
+
* const jss = require('./jss')
|
|
92
|
+
*
|
|
93
|
+
* try {
|
|
94
|
+
* throw new TypeError('Invalid input')
|
|
95
|
+
* } catch (err) {
|
|
96
|
+
* const serialized = jss.stringify({ error: err })
|
|
97
|
+
* const restored = jss.parse(serialized)
|
|
98
|
+
*
|
|
99
|
+
* console.log(restored.error instanceof TypeError) // true
|
|
100
|
+
* console.log(restored.error.message) // 'Invalid input'
|
|
101
|
+
* console.log(restored.error.stack) // Original stack trace
|
|
102
|
+
* }
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* // Low-level encode/decode for inspection
|
|
106
|
+
* const jss = require('./jss')
|
|
107
|
+
*
|
|
108
|
+
* const encoded = jss.encode({
|
|
109
|
+
* date: new Date('2024-01-01'),
|
|
110
|
+
* items: new Set([1, 2, 3])
|
|
111
|
+
* })
|
|
112
|
+
*
|
|
113
|
+
* console.log(encoded)
|
|
114
|
+
* // {
|
|
115
|
+
* // "date<!D>": 1704067200000,
|
|
116
|
+
* // "items<!S>": [1, 2, 3]
|
|
117
|
+
* // }
|
|
118
|
+
*
|
|
119
|
+
* const decoded = jss.decode(encoded)
|
|
120
|
+
* // Original types restored
|
|
121
|
+
*/
|
|
122
|
+
|
|
123
|
+
const { encode, stringify } = require("./jss/encode");
|
|
124
|
+
const { decode, parse } = require("./jss/decode");
|
|
125
|
+
const { register: custom, clearPlugins } = require("./jss/plugins");
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Parse a JSS-encoded string back into an object with restored types
|
|
129
|
+
*
|
|
130
|
+
* This is the primary method for deserializing JSS data. It combines
|
|
131
|
+
* JSON.parse with type restoration for Date, RegExp, Error, Map, Set,
|
|
132
|
+
* undefined, and circular references.
|
|
133
|
+
*
|
|
134
|
+
* @function parse
|
|
135
|
+
* @param {string} encoded - JSS-encoded JSON string
|
|
136
|
+
* @returns {any} Decoded object with original types restored
|
|
137
|
+
* @throws {SyntaxError} If the string is not valid JSON
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* const obj = jss.parse('{"date<!D>":1704067200000}')
|
|
141
|
+
* console.log(obj.date instanceof Date) // true
|
|
142
|
+
*/
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Convert an object to a JSS-encoded JSON string
|
|
146
|
+
*
|
|
147
|
+
* This is the primary method for serializing objects with extended types.
|
|
148
|
+
* It handles Date, RegExp, Error, Map, Set, undefined, and circular
|
|
149
|
+
* references that would cause JSON.stringify to fail or lose information.
|
|
150
|
+
*
|
|
151
|
+
* @function stringify
|
|
152
|
+
* @param {any} obj - Object to serialize
|
|
153
|
+
* @returns {string} JSS-encoded JSON string
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* const str = jss.stringify({
|
|
157
|
+
* when: new Date(),
|
|
158
|
+
* pattern: /\d+/g,
|
|
159
|
+
* items: new Set([1, 2, 3])
|
|
160
|
+
* })
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Encode an object to JSS format (without stringifying)
|
|
165
|
+
*
|
|
166
|
+
* Low-level method that converts extended types to their tagged
|
|
167
|
+
* representations. Useful for inspection or custom serialization.
|
|
168
|
+
*
|
|
169
|
+
* @function encode
|
|
170
|
+
* @param {any} obj - Object to encode
|
|
171
|
+
* @returns {Object} Plain object with tagged keys for extended types
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* const encoded = jss.encode({ date: new Date() })
|
|
175
|
+
* // { "date<!D>": 1704067200000 }
|
|
176
|
+
*/
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Decode a JSS-encoded object (without parsing from string)
|
|
180
|
+
*
|
|
181
|
+
* Low-level method that restores extended types from their tagged
|
|
182
|
+
* representations. Useful when working with already-parsed data.
|
|
183
|
+
*
|
|
184
|
+
* @function decode
|
|
185
|
+
* @param {Object} data - JSS-encoded plain object
|
|
186
|
+
* @returns {any} Object with original types restored
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* const decoded = jss.decode({ "date<!D>": 1704067200000 })
|
|
190
|
+
* console.log(decoded.date instanceof Date) // true
|
|
191
|
+
*/
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Register a custom type handler plugin
|
|
195
|
+
*
|
|
196
|
+
* Plugins extend JSS to handle custom types beyond the built-in set.
|
|
197
|
+
* Each plugin is identified by a single-character tag that appears in
|
|
198
|
+
* the serialized format (e.g., `"key<!X>": value`).
|
|
199
|
+
*
|
|
200
|
+
* @function custom
|
|
201
|
+
* @param {string} tag - Single character tag identifier (e.g., 'X', 'Z')
|
|
202
|
+
* @param {Object} config - Plugin configuration object
|
|
203
|
+
* @param {function(string|number, any): boolean} config.check - Determines if plugin handles value
|
|
204
|
+
* @param {function(string[], string|number, any, Object): any} config.encode - Transform for serialization
|
|
205
|
+
* @param {function(any, string[], Object): any} config.decode - Restore from serialization
|
|
206
|
+
* @param {function=} config.onSend - Optional send lifecycle hook
|
|
207
|
+
* @param {function=} config.onReceive - Optional receive lifecycle hook
|
|
208
|
+
* @throws {Error} If tag conflicts with built-in or existing custom type
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* // Register a custom BigInt handler
|
|
212
|
+
* jss.custom('B', {
|
|
213
|
+
* check: (key, value) => typeof value === 'bigint',
|
|
214
|
+
* encode: (path, key, value) => value.toString(),
|
|
215
|
+
* decode: (value) => BigInt(value)
|
|
216
|
+
* })
|
|
217
|
+
*/
|
|
218
|
+
|
|
219
|
+
module.exports = { parse, stringify, encode, decode, custom, clearPlugins };
|