@based/schema 5.0.0-alpha.10 → 5.0.0-alpha.11

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.
@@ -15,13 +15,13 @@ export const DEFAULT_MAP = {
15
15
  [TYPE_INDEX_MAP.uint8]: 0,
16
16
  [TYPE_INDEX_MAP.uint16]: 0,
17
17
  [TYPE_INDEX_MAP.uint32]: 0,
18
- [TYPE_INDEX_MAP.json]: undefined,
18
+ [TYPE_INDEX_MAP.json]: null,
19
19
  [TYPE_INDEX_MAP.microbuffer]: undefined,
20
20
  [TYPE_INDEX_MAP.reference]: undefined,
21
21
  [TYPE_INDEX_MAP.references]: [],
22
22
  [TYPE_INDEX_MAP.string]: '',
23
23
  [TYPE_INDEX_MAP.aliases]: [],
24
- [TYPE_INDEX_MAP.text]: '',
24
+ [TYPE_INDEX_MAP.text]: {},
25
25
  [TYPE_INDEX_MAP.vector]: undefined, // maybe not can set a vec with 0
26
26
  };
27
27
  //# sourceMappingURL=defaultMap.js.map
@@ -25,6 +25,7 @@ export declare const BINARY = 25;
25
25
  export declare const ID = 26;
26
26
  export declare const VECTOR = 27;
27
27
  export declare const JSON = 28;
28
+ export declare const OBJECT = 29;
28
29
  export declare const TYPE_INDEX_MAP: {
29
30
  alias: number;
30
31
  aliases: number;
@@ -48,6 +49,7 @@ export declare const TYPE_INDEX_MAP: {
48
49
  vector: number;
49
50
  cardinality: number;
50
51
  json: number;
52
+ object: number;
51
53
  };
52
54
  export type InternalSchemaProp = keyof typeof TYPE_INDEX_MAP;
53
55
  export type TypeIndex = (typeof TYPE_INDEX_MAP)[InternalSchemaProp];
package/dist/def/types.js CHANGED
@@ -24,6 +24,7 @@ export const BINARY = 25;
24
24
  export const ID = 26;
25
25
  export const VECTOR = 27;
26
26
  export const JSON = 28;
27
+ export const OBJECT = 29;
27
28
  export const TYPE_INDEX_MAP = {
28
29
  alias: ALIAS,
29
30
  aliases: ALIASES,
@@ -47,6 +48,7 @@ export const TYPE_INDEX_MAP = {
47
48
  vector: VECTOR,
48
49
  cardinality: CARDINALITY,
49
50
  json: JSON,
51
+ object: OBJECT,
50
52
  };
51
53
  export const SIZE_MAP = {
52
54
  timestamp: 8, // 64bit
@@ -72,6 +74,7 @@ export const SIZE_MAP = {
72
74
  binary: 0,
73
75
  vector: 0, // separate
74
76
  json: 0,
77
+ object: 0,
75
78
  };
76
79
  const reverseMap = {};
77
80
  for (const k in TYPE_INDEX_MAP) {
package/dist/index.d.ts CHANGED
@@ -3,4 +3,5 @@ export * from './parse/index.js';
3
3
  export * from './lang.js';
4
4
  export * from './mermaid.js';
5
5
  export * from './def/validation.js';
6
+ export * from './serialize.js';
6
7
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -3,4 +3,5 @@ export * from './parse/index.js';
3
3
  export * from './lang.js';
4
4
  export * from './mermaid.js';
5
5
  export * from './def/validation.js';
6
+ export * from './serialize.js';
6
7
  //# sourceMappingURL=index.js.map
@@ -348,7 +348,7 @@ p.reference = propParser({
348
348
  throw Error(MISSING_TYPE);
349
349
  }
350
350
  },
351
- prop(propKey, prop, { schema, type, inQuery, path }) {
351
+ prop(propKey, prop, { schema, type, inQuery, path, lvl }) {
352
352
  const propAllowed = type && !inQuery;
353
353
  if (propAllowed) {
354
354
  expectString(propKey);
@@ -366,7 +366,7 @@ p.reference = propParser({
366
366
  if (create) {
367
367
  const ref = path[1];
368
368
  let prop = '';
369
- for (let i = 3; i < path.length - 1; i += 2) {
369
+ for (let i = 3; i < lvl; i += 2) {
370
370
  prop += prop ? `.${path[i]}` : path[i];
371
371
  }
372
372
  targetProp.readOnly = true;
@@ -0,0 +1,5 @@
1
+ import { StrictSchema } from './types.js';
2
+ export declare const serialize: (schema: any, noCompression?: boolean) => Uint8Array;
3
+ export declare const deSerializeInner: (buf: Uint8Array, obj: any, start: number) => number;
4
+ export declare const deSerialize: (buf: Uint8Array) => StrictSchema;
5
+ //# sourceMappingURL=serialize.d.ts.map
@@ -0,0 +1,139 @@
1
+ import * as deflate from 'fflate';
2
+ import { REVERSE_TYPE_INDEX_MAP, TYPE_INDEX_MAP } from './def/types.js';
3
+ const hasNative = '__basedDb__native__' in global;
4
+ const ENCODER = new TextEncoder();
5
+ let schemaBuffer;
6
+ const walk = (obj, prev, prev2, fromObject, schemaBuffer) => {
7
+ let start = schemaBuffer.len;
8
+ // HANDLE ENUM
9
+ const isSchemaProp = 'type' in obj && (prev2?.type === 'object' || fromObject === false);
10
+ if (isSchemaProp) {
11
+ schemaBuffer.buf[schemaBuffer.len++] = 254;
12
+ const typeIndex = TYPE_INDEX_MAP[obj.type];
13
+ schemaBuffer.buf[schemaBuffer.len++] = typeIndex;
14
+ }
15
+ else {
16
+ schemaBuffer.buf[schemaBuffer.len++] = 255;
17
+ }
18
+ let sizeIndex = schemaBuffer.len;
19
+ schemaBuffer.len += 2;
20
+ for (const key in obj) {
21
+ if (key === 'type' && isSchemaProp) {
22
+ continue;
23
+ }
24
+ else {
25
+ let address = schemaBuffer.dictMap[key];
26
+ // if len == 1 never from address
27
+ if (!address) {
28
+ address = schemaBuffer.len;
29
+ schemaBuffer.len += 1;
30
+ const r = ENCODER.encodeInto(key, schemaBuffer.buf.subarray(schemaBuffer.len));
31
+ schemaBuffer.buf[address] = r.written;
32
+ schemaBuffer.len += r.written;
33
+ schemaBuffer.dictMap[key] = address;
34
+ }
35
+ else {
36
+ schemaBuffer.buf[schemaBuffer.len] = 0;
37
+ schemaBuffer.len += 1;
38
+ schemaBuffer.buf[schemaBuffer.len] = address;
39
+ schemaBuffer.buf[schemaBuffer.len + 1] = address >>> 8;
40
+ schemaBuffer.len += 2;
41
+ }
42
+ const val = obj[key];
43
+ const type = typeof val;
44
+ // typed Array
45
+ if (Array.isArray(val)) {
46
+ // derp
47
+ }
48
+ else if (type === 'function') {
49
+ // derp
50
+ }
51
+ else if (type === 'object') {
52
+ // fromObject
53
+ if (val === null) {
54
+ }
55
+ else {
56
+ if (!fromObject && key === 'props' && obj.type === 'object') {
57
+ walk(val, obj, prev, true, schemaBuffer);
58
+ }
59
+ else {
60
+ walk(val, obj, prev, fromObject, schemaBuffer);
61
+ }
62
+ }
63
+ }
64
+ else if (type === 'string') {
65
+ // derp
66
+ }
67
+ else if (type === 'number') {
68
+ // do stuff
69
+ }
70
+ }
71
+ }
72
+ const size = schemaBuffer.len - start;
73
+ schemaBuffer.buf[sizeIndex] = size;
74
+ schemaBuffer.buf[sizeIndex + 1] = size >>> 8;
75
+ };
76
+ export const serialize = (schema,
77
+ // schema: StrictSchema,
78
+ noCompression = false) => {
79
+ if (!schemaBuffer) {
80
+ // 1mb buffer add check if its large enough else increase
81
+ schemaBuffer = {
82
+ buf: new Uint8Array(1e6),
83
+ len: 0,
84
+ dictMap: {},
85
+ };
86
+ }
87
+ schemaBuffer.dictMap = {};
88
+ schemaBuffer.len = 0;
89
+ const isDeflate = noCompression ? 0 : 1;
90
+ walk(schema, undefined, undefined, false, schemaBuffer);
91
+ const packed = new Uint8Array(schemaBuffer.buf.subarray(0, schemaBuffer.len));
92
+ if (isDeflate) {
93
+ return deflate.deflateSync(packed);
94
+ }
95
+ else {
96
+ return packed;
97
+ }
98
+ };
99
+ const decoder = new TextDecoder();
100
+ export const deSerializeInner = (buf, obj, start) => {
101
+ let i = start;
102
+ const isSchemaProp = buf[i] === 254;
103
+ i += 1;
104
+ if (isSchemaProp) {
105
+ const type = buf[i];
106
+ const parsedType = REVERSE_TYPE_INDEX_MAP[type];
107
+ obj.type = parsedType;
108
+ i += 1;
109
+ }
110
+ const size = buf[i] | ((buf[i + 1] << 8) >>> 0);
111
+ i += 2;
112
+ const end = size + start;
113
+ while (i < end) {
114
+ let keySize = buf[i];
115
+ i += 1;
116
+ let key;
117
+ if (keySize === 0) {
118
+ const dictAddress = buf[i] | ((buf[i + 1] << 8) >>> 0);
119
+ i += 2;
120
+ keySize = buf[dictAddress];
121
+ key = decoder.decode(buf.subarray(dictAddress + 1, keySize + dictAddress + 1));
122
+ }
123
+ else {
124
+ key = decoder.decode(buf.subarray(i, keySize + i));
125
+ i += keySize;
126
+ }
127
+ const nest = (obj[key] = {});
128
+ const fieldSize = deSerializeInner(buf, nest, i);
129
+ i += fieldSize;
130
+ }
131
+ return i - start;
132
+ };
133
+ export const deSerialize = (buf) => {
134
+ // if first byte is deflate
135
+ const schema = {};
136
+ deSerializeInner(buf, schema, 0);
137
+ return schema;
138
+ };
139
+ //# sourceMappingURL=serialize.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/schema",
3
- "version": "5.0.0-alpha.10",
3
+ "version": "5.0.0-alpha.11",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",
@@ -29,6 +29,7 @@
29
29
  "typescript": "^5.6.3"
30
30
  },
31
31
  "dependencies": {
32
+ "fflate": "0.8.1",
32
33
  "@saulx/utils": "^6.4.0",
33
34
  "picocolors": "^1.1.0"
34
35
  }