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

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.
@@ -16,6 +16,8 @@ export declare const createEmptyDef: (typeName: string, type: StrictSchemaType |
16
16
  lastId: number;
17
17
  locales: {};
18
18
  main: {};
19
+ separateSortProps: number;
20
+ separateSortText: number;
19
21
  localeSize: number;
20
22
  hasSeperateSort: boolean;
21
23
  seperateSort: {
@@ -18,6 +18,8 @@ export const createEmptyDef = (typeName, type, locales) => {
18
18
  lastId: 0,
19
19
  locales: {},
20
20
  main: {},
21
+ separateSortProps: 0,
22
+ separateSortText: 0,
21
23
  localeSize: 0,
22
24
  hasSeperateSort: false,
23
25
  seperateSort: {
@@ -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
@@ -2,7 +2,6 @@ export * from './types.js';
2
2
  export * from './typeDef.js';
3
3
  export * from './utils.js';
4
4
  export * from './selvaBuffer.js';
5
- export * from './readFromPacked.js';
6
5
  export * from './createEmptyDef.js';
7
6
  export * from './defaultMap.js';
8
7
  export * from './validation.js';
package/dist/def/index.js CHANGED
@@ -3,7 +3,6 @@ export * from './types.js';
3
3
  export * from './typeDef.js';
4
4
  export * from './utils.js';
5
5
  export * from './selvaBuffer.js';
6
- export * from './readFromPacked.js';
7
6
  export * from './createEmptyDef.js';
8
7
  export * from './defaultMap.js';
9
8
  export * from './validation.js';
@@ -2,7 +2,6 @@ import { isPropType, getPropType, } from '../index.js';
2
2
  import { setByPath } from '@saulx/utils';
3
3
  import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, NUMBER, } from './types.js';
4
4
  import { DEFAULT_MAP } from './defaultMap.js';
5
- import { makePacked } from './makePacked.js';
6
5
  import { makeSeparateTextSort } from './makeSeparateTextSort.js';
7
6
  import { makeSeparateSort } from './makeSeparateSort.js';
8
7
  import { getPropLen } from './getPropLen.js';
@@ -58,8 +57,6 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
58
57
  result.idUint8[0] = result.id & 255;
59
58
  result.idUint8[1] = result.id >> 8;
60
59
  const target = type.props;
61
- let separateSortProps = 0;
62
- let separateSortText = 0;
63
60
  for (const key in target) {
64
61
  // Create prop def
65
62
  const schemaProp = target[key];
@@ -76,15 +73,15 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
76
73
  if (typeof schemaProp === 'object') {
77
74
  if (!(schemaProp.maxBytes < 61) ||
78
75
  !('max' in schemaProp && schemaProp.max < 31)) {
79
- separateSortProps++;
76
+ result.separateSortProps++;
80
77
  }
81
78
  }
82
79
  else {
83
- separateSortProps++;
80
+ result.separateSortProps++;
84
81
  }
85
82
  }
86
83
  else if (isPropType('text', schemaProp)) {
87
- separateSortText++;
84
+ result.separateSortText++;
88
85
  }
89
86
  const isseparate = isSeparate(schemaProp, len);
90
87
  const typeIndex = TYPE_INDEX_MAP[propType];
@@ -193,11 +190,10 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
193
190
  }
194
191
  result.mainEmpty = fillEmptyMain(vals, result.mainLen);
195
192
  result.mainEmptyAllZeroes = isZeroes(result.mainEmpty);
196
- makePacked(result, typeName, vals, len);
197
- if (separateSortText > 0) {
193
+ if (result.separateSortText > 0) {
198
194
  makeSeparateTextSort(result);
199
195
  }
200
- if (separateSortProps > 0) {
196
+ if (result.separateSortProps > 0) {
201
197
  makeSeparateSort(result);
202
198
  }
203
199
  for (const p in result.props) {
@@ -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];
@@ -123,7 +125,6 @@ export type SchemaTypeDef = {
123
125
  mainLen: number;
124
126
  buf: Uint8Array;
125
127
  propNames: Uint8Array;
126
- packed: Uint8Array;
127
128
  props: {
128
129
  [path: string]: PropDef;
129
130
  };
@@ -139,6 +140,8 @@ export type SchemaTypeDef = {
139
140
  mainEmpty: Uint8Array;
140
141
  mainEmptyAllZeroes: boolean;
141
142
  tree: SchemaPropTree;
143
+ separateSortProps: number;
144
+ separateSortText: number;
142
145
  hasSeperateSort: boolean;
143
146
  seperateSort: SchemaSortUndefinedHandler;
144
147
  hasSeperateTextSort: boolean;
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
@@ -1,6 +1,6 @@
1
1
  export * from './types.js';
2
2
  export * from './parse/index.js';
3
3
  export * from './lang.js';
4
- export * from './mermaid.js';
5
4
  export * from './def/validation.js';
5
+ export * from './serialize.js';
6
6
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from './types.js';
2
2
  export * from './parse/index.js';
3
3
  export * from './lang.js';
4
- export * from './mermaid.js';
5
4
  export * from './def/validation.js';
5
+ export * from './serialize.js';
6
6
  //# sourceMappingURL=index.js.map
@@ -1,4 +1,3 @@
1
- import { isFloat32Array } from 'util/types';
2
1
  import { EXPECTED_ARR, EXPECTED_BOOL, EXPECTED_FN, EXPECTED_NUM, EXPECTED_OBJ, EXPECTED_STR, } from './errors.js';
3
2
  export const expectObject = (obj, msg) => {
4
3
  if (typeof obj !== 'object' || obj === null) {
@@ -6,7 +5,7 @@ export const expectObject = (obj, msg) => {
6
5
  }
7
6
  };
8
7
  export const expectFloat32Array = (arr) => {
9
- if (!isFloat32Array(arr)) {
8
+ if (!(arr instanceof Float32Array)) {
10
9
  throw Error(EXPECTED_ARR);
11
10
  }
12
11
  };
@@ -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,142 @@
1
+ import * as deflate from 'fflate';
2
+ import { REVERSE_TYPE_INDEX_MAP, TYPE_INDEX_MAP } from './def/types.js';
3
+ const ENCODER = new TextEncoder();
4
+ let schemaBuffer;
5
+ // 3 level
6
+ // 0 for queries (min)
7
+ // 1 for modify
8
+ // 2 fulls schema
9
+ const walk = (obj, prev, prev2, fromObject, schemaBuffer) => {
10
+ let start = schemaBuffer.len;
11
+ // HANDLE ENUM
12
+ const isSchemaProp = 'type' in obj && (prev2?.type === 'object' || fromObject === false);
13
+ if (isSchemaProp) {
14
+ schemaBuffer.buf[schemaBuffer.len++] = 254;
15
+ const typeIndex = TYPE_INDEX_MAP[obj.type];
16
+ schemaBuffer.buf[schemaBuffer.len++] = typeIndex;
17
+ }
18
+ else {
19
+ schemaBuffer.buf[schemaBuffer.len++] = 255;
20
+ }
21
+ let sizeIndex = schemaBuffer.len;
22
+ schemaBuffer.len += 2;
23
+ for (const key in obj) {
24
+ if (key === 'type' && isSchemaProp) {
25
+ continue;
26
+ }
27
+ else {
28
+ let address = schemaBuffer.dictMap[key];
29
+ // if len == 1 never from address
30
+ if (!address) {
31
+ address = schemaBuffer.len;
32
+ schemaBuffer.len += 1;
33
+ const r = ENCODER.encodeInto(key, schemaBuffer.buf.subarray(schemaBuffer.len));
34
+ schemaBuffer.buf[address] = r.written;
35
+ schemaBuffer.len += r.written;
36
+ schemaBuffer.dictMap[key] = address;
37
+ }
38
+ else {
39
+ schemaBuffer.buf[schemaBuffer.len] = 0;
40
+ schemaBuffer.len += 1;
41
+ schemaBuffer.buf[schemaBuffer.len] = address;
42
+ schemaBuffer.buf[schemaBuffer.len + 1] = address >>> 8;
43
+ schemaBuffer.len += 2;
44
+ }
45
+ const val = obj[key];
46
+ const type = typeof val;
47
+ // typed Array
48
+ if (Array.isArray(val)) {
49
+ // derp
50
+ }
51
+ else if (type === 'function') {
52
+ // derp
53
+ }
54
+ else if (type === 'object') {
55
+ // fromObject
56
+ if (val === null) {
57
+ }
58
+ else {
59
+ if (!fromObject && key === 'props' && obj.type === 'object') {
60
+ walk(val, obj, prev, true, schemaBuffer);
61
+ }
62
+ else {
63
+ walk(val, obj, prev, fromObject, schemaBuffer);
64
+ }
65
+ }
66
+ }
67
+ else if (type === 'string') {
68
+ // derp
69
+ }
70
+ else if (type === 'number') {
71
+ // do stuff
72
+ }
73
+ }
74
+ }
75
+ const size = schemaBuffer.len - start;
76
+ schemaBuffer.buf[sizeIndex] = size;
77
+ schemaBuffer.buf[sizeIndex + 1] = size >>> 8;
78
+ };
79
+ export const serialize = (schema,
80
+ // schema: StrictSchema,
81
+ noCompression = false) => {
82
+ if (!schemaBuffer) {
83
+ // 1mb buffer add check if its large enough else increase
84
+ schemaBuffer = {
85
+ buf: new Uint8Array(1e6),
86
+ len: 0,
87
+ dictMap: {},
88
+ };
89
+ }
90
+ schemaBuffer.dictMap = {};
91
+ schemaBuffer.len = 0;
92
+ const isDeflate = noCompression ? 0 : 1;
93
+ walk(schema, undefined, undefined, false, schemaBuffer);
94
+ const packed = new Uint8Array(schemaBuffer.buf.subarray(0, schemaBuffer.len));
95
+ if (isDeflate) {
96
+ return deflate.deflateSync(packed);
97
+ }
98
+ else {
99
+ return packed;
100
+ }
101
+ };
102
+ const decoder = new TextDecoder();
103
+ export const deSerializeInner = (buf, obj, start) => {
104
+ let i = start;
105
+ const isSchemaProp = buf[i] === 254;
106
+ i += 1;
107
+ if (isSchemaProp) {
108
+ const type = buf[i];
109
+ const parsedType = REVERSE_TYPE_INDEX_MAP[type];
110
+ obj.type = parsedType;
111
+ i += 1;
112
+ }
113
+ const size = buf[i] | ((buf[i + 1] << 8) >>> 0);
114
+ i += 2;
115
+ const end = size + start;
116
+ while (i < end) {
117
+ let keySize = buf[i];
118
+ i += 1;
119
+ let key;
120
+ if (keySize === 0) {
121
+ const dictAddress = buf[i] | ((buf[i + 1] << 8) >>> 0);
122
+ i += 2;
123
+ keySize = buf[dictAddress];
124
+ key = decoder.decode(buf.subarray(dictAddress + 1, keySize + dictAddress + 1));
125
+ }
126
+ else {
127
+ key = decoder.decode(buf.subarray(i, keySize + i));
128
+ i += keySize;
129
+ }
130
+ const nest = (obj[key] = {});
131
+ const fieldSize = deSerializeInner(buf, nest, i);
132
+ i += fieldSize;
133
+ }
134
+ return i - start;
135
+ };
136
+ export const deSerialize = (buf) => {
137
+ // if first byte is deflate
138
+ const schema = {};
139
+ deSerializeInner(buf, schema, 0);
140
+ return schema;
141
+ };
142
+ //# 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.12",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",
@@ -29,7 +29,8 @@
29
29
  "typescript": "^5.6.3"
30
30
  },
31
31
  "dependencies": {
32
- "@saulx/utils": "^6.4.0",
32
+ "fflate": "0.8.1",
33
+ "@saulx/utils": "^6.5.0",
33
34
  "picocolors": "^1.1.0"
34
35
  }
35
36
  }