@based/schema 5.0.0-alpha.2 → 5.0.0-alpha.21
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/dist/def/DEFAULT_MAP.d.ts +3 -0
- package/dist/def/DEFAULT_MAP.js +29 -0
- package/dist/def/addEdges.d.ts +4 -0
- package/dist/def/addEdges.js +76 -0
- package/dist/def/createEmptyDef.d.ts +42 -0
- package/dist/def/createEmptyDef.js +45 -0
- package/dist/def/defaultMap.d.ts +3 -0
- package/dist/def/defaultMap.js +28 -0
- package/dist/def/fillEmptyMain.d.ts +5 -0
- package/dist/def/fillEmptyMain.js +61 -0
- package/dist/def/getPropLen.d.ts +3 -0
- package/dist/def/getPropLen.js +23 -0
- package/dist/def/index.d.ts +8 -0
- package/dist/def/index.js +9 -0
- package/dist/def/makePacked.d.ts +3 -0
- package/dist/def/makePacked.js +50 -0
- package/dist/def/makeSeparateSort.d.ts +3 -0
- package/dist/def/makeSeparateSort.js +27 -0
- package/dist/def/makeSeparateTextSort.d.ts +3 -0
- package/dist/def/makeSeparateTextSort.js +38 -0
- package/dist/def/readFromPacked.d.ts +3 -0
- package/dist/def/readFromPacked.js +140 -0
- package/dist/def/selvaBuffer.d.ts +5 -0
- package/dist/def/selvaBuffer.js +145 -0
- package/dist/def/timestamp.d.ts +2 -0
- package/dist/def/timestamp.js +67 -0
- package/dist/def/typeDef.d.ts +13 -0
- package/dist/def/typeDef.js +231 -0
- package/dist/def/types.d.ts +186 -0
- package/dist/def/types.js +138 -0
- package/dist/def/utils.d.ts +8 -0
- package/dist/def/utils.js +62 -0
- package/dist/def/validation.d.ts +7 -0
- package/dist/def/validation.js +259 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/lang.d.ts +3 -1
- package/dist/lang.js +2 -0
- package/dist/parse/assert.js +1 -2
- package/dist/parse/index.d.ts +1 -1
- package/dist/parse/index.js +8 -5
- package/dist/parse/props.d.ts +1 -0
- package/dist/parse/props.js +118 -54
- package/dist/serialize.d.ts +14 -0
- package/dist/serialize.js +473 -0
- package/dist/types.d.ts +29 -9
- package/dist/types.js +2 -0
- package/dist/validation/validation.d.ts +2 -0
- package/dist/validation/validation.js +6 -0
- package/package.json +8 -4
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
// import * as deflate from 'fflate'
|
|
2
|
+
import { stringFormats } from './types.js';
|
|
3
|
+
import { REVERSE_TYPE_INDEX_MAP, TYPE_INDEX_MAP } from './def/types.js';
|
|
4
|
+
import { readDoubleLE, readUint16, readUint24, readUint32, writeDoubleLE, writeUint16, writeUint24, writeUint32, } from '@saulx/utils';
|
|
5
|
+
const ENCODER = new TextEncoder();
|
|
6
|
+
const UINT8 = 245;
|
|
7
|
+
const FALSE = 246;
|
|
8
|
+
const TRUE = 247;
|
|
9
|
+
const FUNCTION = 248;
|
|
10
|
+
const STRING = 249;
|
|
11
|
+
const ARRAY = 250;
|
|
12
|
+
const BINARY = 251;
|
|
13
|
+
const UINT32 = 252;
|
|
14
|
+
const FLOAT64 = 253;
|
|
15
|
+
const SCHEMA_PROP = 254;
|
|
16
|
+
const OBJECT = 255;
|
|
17
|
+
// Key Address encoding types
|
|
18
|
+
const KEY_ADDRESS_1_BYTE = 0;
|
|
19
|
+
const KEY_ADDRESS_2_BYTES = 1;
|
|
20
|
+
const KEY_ADDRESS_3_BYTES = 2;
|
|
21
|
+
// Key types
|
|
22
|
+
const PROPS = 3;
|
|
23
|
+
const TYPES = 4;
|
|
24
|
+
const READONLY = 5;
|
|
25
|
+
const FORMAT = 6;
|
|
26
|
+
const REQUIRED = 7;
|
|
27
|
+
const REF = 8;
|
|
28
|
+
const PROP = 9;
|
|
29
|
+
const KEY_OPTS = PROP;
|
|
30
|
+
const ensureCapacity = (required) => {
|
|
31
|
+
if (schemaBuffer.len + required > schemaBuffer.buf.length) {
|
|
32
|
+
const newBuf = new Uint8Array(Math.max(schemaBuffer.buf.length * 2, schemaBuffer.len + required));
|
|
33
|
+
newBuf.set(schemaBuffer.buf);
|
|
34
|
+
schemaBuffer.buf = newBuf;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
let schemaBuffer;
|
|
38
|
+
const handleSingleValue = (ops, val, obj, prev, fromObject, key, isTypes) => {
|
|
39
|
+
const type = typeof val;
|
|
40
|
+
// typed Array - single PROP
|
|
41
|
+
if (val instanceof Uint8Array) {
|
|
42
|
+
ensureCapacity(1 + 2 + val.byteLength);
|
|
43
|
+
schemaBuffer.buf[schemaBuffer.len] = BINARY;
|
|
44
|
+
schemaBuffer.len += 1;
|
|
45
|
+
schemaBuffer.buf[schemaBuffer.len] = val.byteLength;
|
|
46
|
+
schemaBuffer.len += 1;
|
|
47
|
+
schemaBuffer.buf[schemaBuffer.len] = val.byteLength >>> 8;
|
|
48
|
+
schemaBuffer.len += 1;
|
|
49
|
+
schemaBuffer.buf.set(val, schemaBuffer.len);
|
|
50
|
+
schemaBuffer.len += val.byteLength;
|
|
51
|
+
}
|
|
52
|
+
else if (type === 'function') {
|
|
53
|
+
const str = val.toString();
|
|
54
|
+
// Pessimistically assume 4 bytes per char for UTF-8 to be safe.
|
|
55
|
+
ensureCapacity(1 + 2 + str.length * 4);
|
|
56
|
+
schemaBuffer.buf[schemaBuffer.len] = FUNCTION;
|
|
57
|
+
schemaBuffer.len += 1;
|
|
58
|
+
const sizeIndex = schemaBuffer.len;
|
|
59
|
+
schemaBuffer.len += 2;
|
|
60
|
+
// encodeInto is much faster as it avoids intermediate allocation.
|
|
61
|
+
const r = ENCODER.encodeInto(str, schemaBuffer.buf.subarray(schemaBuffer.len));
|
|
62
|
+
schemaBuffer.len += r.written;
|
|
63
|
+
schemaBuffer.buf[sizeIndex] = r.written;
|
|
64
|
+
schemaBuffer.buf[sizeIndex + 1] = r.written >>> 8;
|
|
65
|
+
}
|
|
66
|
+
else if (type === 'object') {
|
|
67
|
+
// fromObject
|
|
68
|
+
if (val === null) {
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
if (!fromObject && key === 'props' && obj.type === 'object') {
|
|
72
|
+
walk(ops, val, obj, prev, true, schemaBuffer, false);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
walk(ops, val, obj, prev, fromObject, schemaBuffer, isTypes);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else if (type === 'boolean') {
|
|
80
|
+
ensureCapacity(1);
|
|
81
|
+
schemaBuffer.buf[schemaBuffer.len] = val ? TRUE : FALSE;
|
|
82
|
+
schemaBuffer.len += 1;
|
|
83
|
+
}
|
|
84
|
+
else if (type === 'string') {
|
|
85
|
+
// Pessimistically assume 4 bytes per char for UTF-8 to be safe.
|
|
86
|
+
ensureCapacity(1 + 2 + val.length * 4);
|
|
87
|
+
schemaBuffer.buf[schemaBuffer.len] = STRING;
|
|
88
|
+
schemaBuffer.len += 1;
|
|
89
|
+
const sizeIndex = schemaBuffer.len;
|
|
90
|
+
schemaBuffer.len += 2;
|
|
91
|
+
// encodeInto is much faster as it avoids intermediate allocation.
|
|
92
|
+
const r = ENCODER.encodeInto(val, schemaBuffer.buf.subarray(schemaBuffer.len));
|
|
93
|
+
schemaBuffer.len += r.written;
|
|
94
|
+
schemaBuffer.buf[sizeIndex] = r.written;
|
|
95
|
+
schemaBuffer.buf[sizeIndex + 1] = r.written >>> 8;
|
|
96
|
+
}
|
|
97
|
+
else if (type === 'number') {
|
|
98
|
+
const isInt = val % 1 === 0;
|
|
99
|
+
if (val < 256 && val > 0 && isInt) {
|
|
100
|
+
ensureCapacity(2);
|
|
101
|
+
schemaBuffer.buf[schemaBuffer.len] = UINT8;
|
|
102
|
+
schemaBuffer.len += 1;
|
|
103
|
+
schemaBuffer.buf[schemaBuffer.len] = val;
|
|
104
|
+
schemaBuffer.len += 1;
|
|
105
|
+
}
|
|
106
|
+
else if ((val < 4294967295 || val > 0) && isInt) {
|
|
107
|
+
ensureCapacity(5);
|
|
108
|
+
schemaBuffer.buf[schemaBuffer.len] = UINT32;
|
|
109
|
+
schemaBuffer.len += 1;
|
|
110
|
+
writeUint32(schemaBuffer.buf, val, schemaBuffer.len);
|
|
111
|
+
schemaBuffer.len += 4;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
ensureCapacity(9);
|
|
115
|
+
schemaBuffer.buf[schemaBuffer.len] = FLOAT64;
|
|
116
|
+
schemaBuffer.len += 1;
|
|
117
|
+
writeDoubleLE(schemaBuffer.buf, val, schemaBuffer.len);
|
|
118
|
+
schemaBuffer.len += 8;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
const encodeKey = (key, schemaBuffer) => {
|
|
123
|
+
let address = schemaBuffer.dictMap[key];
|
|
124
|
+
// if len == 1 never from address
|
|
125
|
+
if (!address) {
|
|
126
|
+
// pessimistically assume 4 bytes per char for UTF-8 to be safe.
|
|
127
|
+
ensureCapacity(1 + key.length * 4);
|
|
128
|
+
address = schemaBuffer.len;
|
|
129
|
+
schemaBuffer.len += 1;
|
|
130
|
+
const r = ENCODER.encodeInto(key, schemaBuffer.buf.subarray(schemaBuffer.len));
|
|
131
|
+
schemaBuffer.buf[address] = r.written + KEY_OPTS;
|
|
132
|
+
schemaBuffer.len += r.written;
|
|
133
|
+
schemaBuffer.dictMap[key] = address;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
ensureCapacity(4);
|
|
137
|
+
if (address > 65025) {
|
|
138
|
+
schemaBuffer.buf[schemaBuffer.len] = KEY_ADDRESS_3_BYTES;
|
|
139
|
+
schemaBuffer.len += 1;
|
|
140
|
+
writeUint24(schemaBuffer.buf, address, schemaBuffer.len);
|
|
141
|
+
schemaBuffer.len += 3;
|
|
142
|
+
}
|
|
143
|
+
else if (address > 255) {
|
|
144
|
+
schemaBuffer.buf[schemaBuffer.len] = KEY_ADDRESS_2_BYTES;
|
|
145
|
+
schemaBuffer.len += 1;
|
|
146
|
+
writeUint16(schemaBuffer.buf, address, schemaBuffer.len);
|
|
147
|
+
schemaBuffer.len += 2;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
schemaBuffer.buf[schemaBuffer.len] = KEY_ADDRESS_1_BYTE;
|
|
151
|
+
schemaBuffer.len += 1;
|
|
152
|
+
schemaBuffer.buf[schemaBuffer.len] = address;
|
|
153
|
+
schemaBuffer.len += 1;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
// 3 level
|
|
158
|
+
// 0 for queries (min)
|
|
159
|
+
// 1 for modify
|
|
160
|
+
// 2 fulls schema
|
|
161
|
+
const walk = (opts, obj, prev, prev2, fromObject, schemaBuffer, isTypes) => {
|
|
162
|
+
let start = schemaBuffer.len;
|
|
163
|
+
const isArray = Array.isArray(obj);
|
|
164
|
+
const isFromObj = prev2?.type === 'object' || fromObject === false;
|
|
165
|
+
const isSchemaProp = 'type' in obj && isFromObj;
|
|
166
|
+
ensureCapacity(1 + 4); // Type byte + size
|
|
167
|
+
if (isSchemaProp) {
|
|
168
|
+
schemaBuffer.buf[schemaBuffer.len++] = SCHEMA_PROP;
|
|
169
|
+
const typeIndex = TYPE_INDEX_MAP[obj.type];
|
|
170
|
+
schemaBuffer.buf[schemaBuffer.len++] = typeIndex;
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
schemaBuffer.buf[schemaBuffer.len++] = isArray ? ARRAY : OBJECT;
|
|
174
|
+
}
|
|
175
|
+
let sizeIndex = schemaBuffer.len;
|
|
176
|
+
schemaBuffer.len += 2;
|
|
177
|
+
if (isArray) {
|
|
178
|
+
const len = obj.length;
|
|
179
|
+
ensureCapacity(2 * len + 2);
|
|
180
|
+
writeUint16(schemaBuffer.buf, len, schemaBuffer.len);
|
|
181
|
+
schemaBuffer.len += 2;
|
|
182
|
+
for (let j = 0; j < len; j++) {
|
|
183
|
+
if (len < 256) {
|
|
184
|
+
schemaBuffer.buf[schemaBuffer.len] = j;
|
|
185
|
+
schemaBuffer.len += 1;
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
writeUint16(schemaBuffer.buf, j, schemaBuffer.len);
|
|
189
|
+
schemaBuffer.len += 2;
|
|
190
|
+
}
|
|
191
|
+
handleSingleValue(opts, obj[j], obj, prev, fromObject, j);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
for (const key in obj) {
|
|
196
|
+
if (opts.readOnly &&
|
|
197
|
+
isFromObj &&
|
|
198
|
+
(key === 'validation' || key === 'default')) {
|
|
199
|
+
if (key === 'validation' && typeof obj[key] === 'function') {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
else if (key === 'default') {
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else if (isFromObj &&
|
|
207
|
+
(opts.stripMetaInformation || opts.readOnly) &&
|
|
208
|
+
(key === 'title' ||
|
|
209
|
+
key === 'description' ||
|
|
210
|
+
key === 'format' ||
|
|
211
|
+
key === 'display') &&
|
|
212
|
+
typeof obj[key] === 'string') {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
else if (key === 'type' && isSchemaProp) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
else if (key === 'required' && obj[key] === true) {
|
|
219
|
+
ensureCapacity(1);
|
|
220
|
+
schemaBuffer.buf[schemaBuffer.len] = REQUIRED;
|
|
221
|
+
schemaBuffer.len += 1;
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
// Add this later
|
|
225
|
+
else if (key == 'ref' && isFromObj && typeof obj.ref === 'string') {
|
|
226
|
+
ensureCapacity(1);
|
|
227
|
+
schemaBuffer.buf[schemaBuffer.len] = REF;
|
|
228
|
+
schemaBuffer.len += 1;
|
|
229
|
+
encodeKey(obj[key], schemaBuffer);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
else if (key === 'prop' && isFromObj && typeof obj.prop === 'string') {
|
|
233
|
+
ensureCapacity(1);
|
|
234
|
+
schemaBuffer.buf[schemaBuffer.len] = PROP;
|
|
235
|
+
schemaBuffer.len += 1;
|
|
236
|
+
encodeKey(obj[key], schemaBuffer);
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
else if (key === 'readOnly' && obj[key] === true) {
|
|
240
|
+
ensureCapacity(1);
|
|
241
|
+
schemaBuffer.buf[schemaBuffer.len] = READONLY;
|
|
242
|
+
schemaBuffer.len += 1;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
else if (key === 'format' && isFromObj) {
|
|
246
|
+
ensureCapacity(2);
|
|
247
|
+
schemaBuffer.buf[schemaBuffer.len] = FORMAT;
|
|
248
|
+
schemaBuffer.len += 1;
|
|
249
|
+
schemaBuffer.buf[schemaBuffer.len] = stringFormats.indexOf(obj.format);
|
|
250
|
+
schemaBuffer.len += 1;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
let isTypes = false;
|
|
255
|
+
if (key === 'types') {
|
|
256
|
+
// undefined undefined false
|
|
257
|
+
if (!prev && !prev2 && !fromObject) {
|
|
258
|
+
isTypes = true;
|
|
259
|
+
}
|
|
260
|
+
ensureCapacity(1);
|
|
261
|
+
schemaBuffer.buf[schemaBuffer.len] = TYPES;
|
|
262
|
+
schemaBuffer.len += 1;
|
|
263
|
+
}
|
|
264
|
+
else if (key === 'props') {
|
|
265
|
+
ensureCapacity(1);
|
|
266
|
+
schemaBuffer.buf[schemaBuffer.len] = PROPS;
|
|
267
|
+
schemaBuffer.len += 1;
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
encodeKey(key, schemaBuffer);
|
|
271
|
+
}
|
|
272
|
+
handleSingleValue(opts, obj[key], obj, prev, fromObject, key, isTypes);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
let size = schemaBuffer.len - start;
|
|
277
|
+
schemaBuffer.buf[sizeIndex] = size;
|
|
278
|
+
schemaBuffer.buf[sizeIndex + 1] = size >>> 8;
|
|
279
|
+
};
|
|
280
|
+
export const serialize = (schema, opts = {}) => {
|
|
281
|
+
if (!schemaBuffer) {
|
|
282
|
+
schemaBuffer = {
|
|
283
|
+
buf: new Uint8Array(10e3), // 10kb default
|
|
284
|
+
len: 0,
|
|
285
|
+
dictMap: {},
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
schemaBuffer.len = 0;
|
|
289
|
+
schemaBuffer.dictMap = {};
|
|
290
|
+
// defalte not supported in unpacking yet
|
|
291
|
+
const isDeflate = 0; // opts.deflate ? 1 : 0
|
|
292
|
+
walk(opts, schema, undefined, undefined, false, schemaBuffer, false);
|
|
293
|
+
const packed = new Uint8Array(schemaBuffer.buf.subarray(0, schemaBuffer.len));
|
|
294
|
+
// if (isDeflate) {
|
|
295
|
+
// // add extra byte! see if nessecary
|
|
296
|
+
// return deflate.deflateSync(packed)
|
|
297
|
+
// } else {
|
|
298
|
+
return packed;
|
|
299
|
+
// }
|
|
300
|
+
};
|
|
301
|
+
const decoder = new TextDecoder();
|
|
302
|
+
export const deSerializeKey = (buf, keySize, i) => {
|
|
303
|
+
let size = 0;
|
|
304
|
+
let value;
|
|
305
|
+
if (keySize === KEY_ADDRESS_3_BYTES) {
|
|
306
|
+
const dictAddress = readUint24(buf, i);
|
|
307
|
+
size += 3;
|
|
308
|
+
const actualKeySize = buf[dictAddress] - KEY_OPTS;
|
|
309
|
+
value = decoder.decode(buf.subarray(dictAddress + 1, actualKeySize + dictAddress + 1));
|
|
310
|
+
}
|
|
311
|
+
else if (keySize === KEY_ADDRESS_2_BYTES) {
|
|
312
|
+
const dictAddress = readUint16(buf, i);
|
|
313
|
+
size += 2;
|
|
314
|
+
const actualKeySize = buf[dictAddress] - KEY_OPTS;
|
|
315
|
+
value = decoder.decode(buf.subarray(dictAddress + 1, actualKeySize + dictAddress + 1));
|
|
316
|
+
}
|
|
317
|
+
else if (keySize === KEY_ADDRESS_1_BYTE) {
|
|
318
|
+
const dictAddress = buf[i];
|
|
319
|
+
size += 1;
|
|
320
|
+
const actualKeySize = buf[dictAddress] - KEY_OPTS;
|
|
321
|
+
value = decoder.decode(buf.subarray(dictAddress + 1, actualKeySize + dictAddress + 1));
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
const actualKeySize = keySize - KEY_OPTS;
|
|
325
|
+
value = decoder.decode(buf.subarray(i, actualKeySize + i));
|
|
326
|
+
size += actualKeySize;
|
|
327
|
+
}
|
|
328
|
+
return { size, value };
|
|
329
|
+
};
|
|
330
|
+
export const deSerializeInner = (buf, obj, start, fromArray) => {
|
|
331
|
+
let i = start;
|
|
332
|
+
const isSchemaProp = buf[i] === SCHEMA_PROP;
|
|
333
|
+
i += 1;
|
|
334
|
+
if (isSchemaProp) {
|
|
335
|
+
const type = buf[i];
|
|
336
|
+
const parsedType = REVERSE_TYPE_INDEX_MAP[type];
|
|
337
|
+
obj.type = parsedType;
|
|
338
|
+
i += 1;
|
|
339
|
+
}
|
|
340
|
+
const size = readUint16(buf, i);
|
|
341
|
+
i += 2;
|
|
342
|
+
const end = size + start;
|
|
343
|
+
if (fromArray) {
|
|
344
|
+
i += 2;
|
|
345
|
+
}
|
|
346
|
+
while (i < end) {
|
|
347
|
+
let key;
|
|
348
|
+
if (fromArray) {
|
|
349
|
+
if (obj.length < 256) {
|
|
350
|
+
key = buf[i];
|
|
351
|
+
i += 1;
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
key = readUint16(buf, i);
|
|
355
|
+
i += 2;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
let keySize = buf[i];
|
|
360
|
+
i += 1;
|
|
361
|
+
// format!
|
|
362
|
+
if (keySize === REQUIRED) {
|
|
363
|
+
obj.required = true;
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
else if (keySize === FORMAT) {
|
|
367
|
+
obj.format = stringFormats[buf[i]];
|
|
368
|
+
i += 1;
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
else if (keySize === READONLY) {
|
|
372
|
+
obj.readOnly = true;
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
else if (keySize === TYPES) {
|
|
376
|
+
key = 'types';
|
|
377
|
+
}
|
|
378
|
+
else if (keySize === PROPS) {
|
|
379
|
+
key = 'props';
|
|
380
|
+
}
|
|
381
|
+
else if (keySize === REF) {
|
|
382
|
+
const valueKeySize = buf[i];
|
|
383
|
+
i += 1;
|
|
384
|
+
const { size, value } = deSerializeKey(buf, valueKeySize, i);
|
|
385
|
+
i += size;
|
|
386
|
+
obj.ref = value;
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
else if (keySize === PROP) {
|
|
390
|
+
const valueKeySize = buf[i];
|
|
391
|
+
i += 1;
|
|
392
|
+
const { size, value } = deSerializeKey(buf, valueKeySize, i);
|
|
393
|
+
i += size;
|
|
394
|
+
obj.prop = value;
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
const { size, value } = deSerializeKey(buf, keySize, i);
|
|
399
|
+
i += size;
|
|
400
|
+
key = value;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (buf[i] === UINT8) {
|
|
404
|
+
i += 1;
|
|
405
|
+
obj[key] = buf[i];
|
|
406
|
+
i += 1;
|
|
407
|
+
}
|
|
408
|
+
else if (buf[i] === FALSE) {
|
|
409
|
+
i += 1;
|
|
410
|
+
obj[key] = false;
|
|
411
|
+
}
|
|
412
|
+
else if (buf[i] === TRUE) {
|
|
413
|
+
i += 1;
|
|
414
|
+
obj[key] = true;
|
|
415
|
+
}
|
|
416
|
+
else if (buf[i] === FUNCTION) {
|
|
417
|
+
i += 1;
|
|
418
|
+
const size = readUint16(buf, i);
|
|
419
|
+
i += 2;
|
|
420
|
+
const fn = `return (${decoder.decode(buf.subarray(i, i + size))})(payload, prop)`;
|
|
421
|
+
obj[key] = new Function('payload', 'prop', fn);
|
|
422
|
+
i += size;
|
|
423
|
+
}
|
|
424
|
+
else if (buf[i] === STRING) {
|
|
425
|
+
i += 1;
|
|
426
|
+
const size = readUint16(buf, i);
|
|
427
|
+
i += 2;
|
|
428
|
+
obj[key] = decoder.decode(buf.subarray(i, i + size));
|
|
429
|
+
i += size;
|
|
430
|
+
}
|
|
431
|
+
else if (buf[i] === BINARY) {
|
|
432
|
+
i += 1;
|
|
433
|
+
const size = readUint16(buf, i);
|
|
434
|
+
i += 2;
|
|
435
|
+
obj[key] = buf.subarray(i, size + i);
|
|
436
|
+
i += size;
|
|
437
|
+
}
|
|
438
|
+
else if (buf[i] === UINT32) {
|
|
439
|
+
obj[key] = readUint32(buf, i + 1);
|
|
440
|
+
i += 5;
|
|
441
|
+
}
|
|
442
|
+
else if (buf[i] === FLOAT64) {
|
|
443
|
+
obj[key] = readDoubleLE(buf, i + 1);
|
|
444
|
+
i += 9;
|
|
445
|
+
}
|
|
446
|
+
else if (buf[i] === OBJECT || buf[i] === SCHEMA_PROP) {
|
|
447
|
+
const nest = (obj[key] = {});
|
|
448
|
+
const fieldSize = deSerializeInner(buf, nest, i, false);
|
|
449
|
+
i += fieldSize;
|
|
450
|
+
}
|
|
451
|
+
else if (buf[i] === ARRAY) {
|
|
452
|
+
const len = readUint16(buf, i + 3);
|
|
453
|
+
const nest = (obj[key] = new Array(len));
|
|
454
|
+
const fieldSize = deSerializeInner(buf, nest, i, true);
|
|
455
|
+
i += fieldSize;
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
console.warn('Invalid value type', buf[i], 'skip');
|
|
459
|
+
// Invalid value type
|
|
460
|
+
i += 1;
|
|
461
|
+
const size = buf[i] | ((buf[i + 1] << 8) >>> 0);
|
|
462
|
+
i += size;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return i - start;
|
|
466
|
+
};
|
|
467
|
+
export const deSerialize = (buf) => {
|
|
468
|
+
// if first byte is deflate
|
|
469
|
+
const schema = {};
|
|
470
|
+
deSerializeInner(buf, schema, 0, false);
|
|
471
|
+
return schema;
|
|
472
|
+
};
|
|
473
|
+
//# sourceMappingURL=serialize.js.map
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LangName } from './lang.js';
|
|
2
|
+
import { Validation } from './def/validation.js';
|
|
2
3
|
type Role = 'title' | 'source' | 'media' | string;
|
|
3
4
|
export declare const numberDisplays: readonly ["short", "human", "ratio", "bytes", "euro", "dollar", "pound", "meter"];
|
|
4
5
|
export declare const dateDisplays: readonly ["date", "date-time", "date-time-text", "date-time-human", "time", "time-precise"];
|
|
@@ -14,6 +15,7 @@ type QueryFn = Function;
|
|
|
14
15
|
type PropValues = {
|
|
15
16
|
type?: string;
|
|
16
17
|
default?: any;
|
|
18
|
+
validation?: Validation;
|
|
17
19
|
};
|
|
18
20
|
type Prop<V extends PropValues> = {
|
|
19
21
|
required?: boolean;
|
|
@@ -24,6 +26,7 @@ type Prop<V extends PropValues> = {
|
|
|
24
26
|
role?: Role;
|
|
25
27
|
readOnly?: boolean;
|
|
26
28
|
examples?: string[];
|
|
29
|
+
validation?: Validation;
|
|
27
30
|
} & V;
|
|
28
31
|
type EnumItem = string | number | boolean;
|
|
29
32
|
type NeverInItems = {
|
|
@@ -31,15 +34,19 @@ type NeverInItems = {
|
|
|
31
34
|
};
|
|
32
35
|
export type SchemaReferences = Prop<{
|
|
33
36
|
type?: 'references';
|
|
37
|
+
default?: number[];
|
|
34
38
|
items: SchemaReference & NeverInItems;
|
|
35
39
|
}>;
|
|
36
40
|
export type SchemaReferencesOneWay = Prop<{
|
|
37
41
|
type?: 'references';
|
|
42
|
+
default?: number[];
|
|
38
43
|
items: SchemaReferenceOneWay & NeverInItems;
|
|
39
44
|
}>;
|
|
40
45
|
export type SchemaText = Prop<{
|
|
41
46
|
type: 'text';
|
|
42
47
|
default?: Record<string, string>;
|
|
48
|
+
format?: StringFormat;
|
|
49
|
+
compression?: 'none' | 'deflate';
|
|
43
50
|
}>;
|
|
44
51
|
type NumberType = 'number' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32';
|
|
45
52
|
export type SchemaNumber = Prop<{
|
|
@@ -65,7 +72,7 @@ export type SchemaString = Prop<{
|
|
|
65
72
|
}>;
|
|
66
73
|
export type SchemaBinary = Prop<{
|
|
67
74
|
type: 'binary';
|
|
68
|
-
default?:
|
|
75
|
+
default?: Uint8Array;
|
|
69
76
|
maxBytes?: number;
|
|
70
77
|
mime?: Mime;
|
|
71
78
|
format?: StringFormat;
|
|
@@ -80,7 +87,6 @@ export type SchemaBoolean = Prop<{
|
|
|
80
87
|
}>;
|
|
81
88
|
export type SchemaCardinality = Prop<{
|
|
82
89
|
type: 'cardinality';
|
|
83
|
-
default?: string;
|
|
84
90
|
maxBytes?: number;
|
|
85
91
|
mime?: Mime;
|
|
86
92
|
format?: NumberDisplay;
|
|
@@ -90,21 +96,29 @@ export type SchemaVector = Prop<{
|
|
|
90
96
|
default?: Float32Array;
|
|
91
97
|
size: number;
|
|
92
98
|
}>;
|
|
99
|
+
export type SchemaColvec = Prop<{
|
|
100
|
+
type: 'colvec';
|
|
101
|
+
default?: Float32Array;
|
|
102
|
+
size: number;
|
|
103
|
+
}>;
|
|
93
104
|
export type SchemaTimestamp = Prop<{
|
|
94
105
|
type: 'timestamp';
|
|
95
|
-
default?: number | Date;
|
|
106
|
+
default?: number | Date | string;
|
|
96
107
|
on?: 'create' | 'update';
|
|
97
108
|
display?: DateDisplay;
|
|
109
|
+
min?: number | string;
|
|
110
|
+
max?: number | string;
|
|
111
|
+
step?: number | 'any' | string;
|
|
98
112
|
}>;
|
|
99
113
|
export type SchemaReferenceOneWay = Prop<{
|
|
100
114
|
type?: 'reference';
|
|
101
|
-
default?:
|
|
115
|
+
default?: number;
|
|
102
116
|
ref: string;
|
|
103
117
|
mime?: Mime;
|
|
104
118
|
}>;
|
|
105
119
|
export type SchemaReference = Prop<{
|
|
106
120
|
type?: 'reference';
|
|
107
|
-
default?:
|
|
121
|
+
default?: number;
|
|
108
122
|
ref: string;
|
|
109
123
|
prop: string;
|
|
110
124
|
dependent?: boolean;
|
|
@@ -126,7 +140,7 @@ export type SchemaReferencesWithQuery = SchemaReferencesOneWay & {
|
|
|
126
140
|
};
|
|
127
141
|
export type SchemaEnum = Prop<{
|
|
128
142
|
type?: 'enum';
|
|
129
|
-
default?: EnumItem;
|
|
143
|
+
default?: EnumItem | undefined;
|
|
130
144
|
enum: EnumItem[];
|
|
131
145
|
}>;
|
|
132
146
|
export type SchemaAlias = Omit<SchemaString, 'type'> & {
|
|
@@ -141,7 +155,7 @@ export type SchemaSet<ItemsType extends SetItems = SetItems> = Prop<{
|
|
|
141
155
|
} ? ItemsType['default'][] : undefined;
|
|
142
156
|
items: ItemsType & NeverInItems;
|
|
143
157
|
}>;
|
|
144
|
-
type NonRefSchemaProps<isStrict = false> = SchemaTimestamp | SchemaBoolean | SchemaNumber | SchemaString | SchemaAlias | SchemaText | SchemaEnum | SchemaJson | SchemaBinary | SchemaCardinality | SchemaVector | (isStrict extends true ? SchemaSet<SetItems<true>> : SchemaPropShorthand | SchemaSet);
|
|
158
|
+
type NonRefSchemaProps<isStrict = false> = SchemaTimestamp | SchemaBoolean | SchemaNumber | SchemaString | SchemaAlias | SchemaText | SchemaEnum | SchemaJson | SchemaBinary | SchemaCardinality | SchemaVector | SchemaColvec | (isStrict extends true ? SchemaSet<SetItems<true>> : SchemaPropShorthand | SchemaSet);
|
|
145
159
|
export type SchemaProp<isStrict = false> = SchemaReferencesWithQuery | SchemaReferenceWithQuery | NonRefSchemaProps<isStrict> | SchemaReferences | SchemaReference | SchemaObject | SchemaBinary;
|
|
146
160
|
export type SchemaPropOneWay<isStrict = false> = SchemaReferencesOneWay | SchemaReferenceOneWay | SchemaObjectOneWay | NonRefSchemaProps<isStrict>;
|
|
147
161
|
export type SchemaAnyProp = SchemaPropOneWay | SchemaProp;
|
|
@@ -156,6 +170,9 @@ type GenericSchemaType<isStrict = false> = {
|
|
|
156
170
|
delete?: SchemaHook;
|
|
157
171
|
};
|
|
158
172
|
id?: number;
|
|
173
|
+
blockCapacity?: number;
|
|
174
|
+
insertOnly?: boolean;
|
|
175
|
+
partial?: boolean;
|
|
159
176
|
props: SchemaProps<isStrict>;
|
|
160
177
|
};
|
|
161
178
|
export type StrictSchemaType = GenericSchemaType<true>;
|
|
@@ -176,9 +193,9 @@ type GenericSchema<isStrict = false> = {
|
|
|
176
193
|
};
|
|
177
194
|
export type StrictSchema = GenericSchema<true>;
|
|
178
195
|
export type Schema = GenericSchema<false> | StrictSchema;
|
|
179
|
-
export type SchemaLocales = Record<LangName, {
|
|
196
|
+
export type SchemaLocales = Record<LangName, true | {
|
|
180
197
|
required?: boolean;
|
|
181
|
-
fallback?: LangName
|
|
198
|
+
fallback?: LangName;
|
|
182
199
|
}>;
|
|
183
200
|
export type SchemaPropTypeMap = {
|
|
184
201
|
references: SchemaReferences;
|
|
@@ -195,8 +212,11 @@ export type SchemaPropTypeMap = {
|
|
|
195
212
|
binary: SchemaBinary;
|
|
196
213
|
cardinality: SchemaCardinality;
|
|
197
214
|
vector: SchemaVector;
|
|
215
|
+
colvec: SchemaColvec;
|
|
198
216
|
} & Record<NumberType, SchemaNumber>;
|
|
199
217
|
export type SchemaPropTypes = keyof SchemaPropTypeMap;
|
|
200
218
|
export declare const isPropType: <T extends SchemaPropTypes>(type: T, prop: SchemaProp) => prop is SchemaPropTypeMap[T];
|
|
219
|
+
export declare const MAX_ID = 4294967295;
|
|
220
|
+
export declare const MIN_ID = 1;
|
|
201
221
|
export {};
|
|
202
222
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@based/schema",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.21",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
5
|
"files": [
|
|
7
6
|
"dist",
|
|
8
7
|
"README.md",
|
|
9
8
|
"package.json",
|
|
10
9
|
"!dist/**/*.map"
|
|
11
10
|
],
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"exports": {
|
|
13
|
+
"./def": "./dist/def/index.js",
|
|
14
|
+
".": "./dist/index.js"
|
|
15
|
+
},
|
|
12
16
|
"scripts": {
|
|
13
17
|
"build": "tsc",
|
|
14
18
|
"watch": "tsc --watch",
|
|
15
|
-
"test": "tsc && tsc
|
|
16
|
-
"test1": "tsc && tsc $npm_config --noEmit && tsx --test $npm_config"
|
|
19
|
+
"test": "tsc && tsc $npm_config --noEmit && tsx --test $npm_config"
|
|
17
20
|
},
|
|
18
21
|
"prettier": "@saulx/prettier-config",
|
|
19
22
|
"sideEffects": false,
|
|
@@ -26,6 +29,7 @@
|
|
|
26
29
|
"typescript": "^5.6.3"
|
|
27
30
|
},
|
|
28
31
|
"dependencies": {
|
|
32
|
+
"@saulx/utils": "^6.7.0",
|
|
29
33
|
"picocolors": "^1.1.0"
|
|
30
34
|
}
|
|
31
35
|
}
|