@based/schema 5.0.0-alpha.20 → 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.
@@ -0,0 +1,3 @@
1
+ import { TypeIndex } from './types.js';
2
+ export declare const DEFAULT_MAP: Record<TypeIndex, any>;
3
+ //# sourceMappingURL=DEFAULT_MAP.d.ts.map
@@ -0,0 +1,29 @@
1
+ import { TYPE_INDEX_MAP } from './types.js';
2
+ // TODO update defaults
3
+ export const DEFAULT_MAP = {
4
+ [TYPE_INDEX_MAP.alias]: '',
5
+ [TYPE_INDEX_MAP.binary]: undefined,
6
+ [TYPE_INDEX_MAP.boolean]: false,
7
+ [TYPE_INDEX_MAP.cardinality]: 0,
8
+ [TYPE_INDEX_MAP.created]: 0,
9
+ [TYPE_INDEX_MAP.updated]: 0,
10
+ [TYPE_INDEX_MAP.number]: 0,
11
+ [TYPE_INDEX_MAP.timestamp]: 0,
12
+ [TYPE_INDEX_MAP.enum]: 0,
13
+ [TYPE_INDEX_MAP.id]: 0,
14
+ [TYPE_INDEX_MAP.int16]: 0,
15
+ [TYPE_INDEX_MAP.int32]: 0,
16
+ [TYPE_INDEX_MAP.int8]: 0,
17
+ [TYPE_INDEX_MAP.uint8]: 0,
18
+ [TYPE_INDEX_MAP.uint16]: 0,
19
+ [TYPE_INDEX_MAP.uint32]: 0,
20
+ [TYPE_INDEX_MAP.json]: undefined,
21
+ [TYPE_INDEX_MAP.microbuffer]: undefined,
22
+ [TYPE_INDEX_MAP.reference]: undefined,
23
+ [TYPE_INDEX_MAP.references]: [],
24
+ [TYPE_INDEX_MAP.string]: '',
25
+ [TYPE_INDEX_MAP.aliases]: [],
26
+ [TYPE_INDEX_MAP.text]: '',
27
+ [TYPE_INDEX_MAP.vector]: undefined, // maybe not can set a vec with 0
28
+ };
29
+ //# sourceMappingURL=DEFAULT_MAP.js.map
@@ -2,6 +2,8 @@ import { SchemaLocales, SchemaObject, StrictSchemaType } from '../types.js';
2
2
  export declare const createEmptyDef: (typeName: string, type: StrictSchemaType | SchemaObject, locales: Partial<SchemaLocales>) => {
3
3
  cnt: number;
4
4
  blockCapacity: number;
5
+ insertOnly: boolean;
6
+ partial: boolean;
5
7
  checksum: number;
6
8
  type: string;
7
9
  props: {};
@@ -3,6 +3,8 @@ export const createEmptyDef = (typeName, type, locales) => {
3
3
  return {
4
4
  cnt: 0,
5
5
  blockCapacity: 0,
6
+ insertOnly: false,
7
+ partial: false,
6
8
  checksum: hashObjectIgnoreKeyOrder(type),
7
9
  type: typeName,
8
10
  props: {},
@@ -23,5 +23,6 @@ export const DEFAULT_MAP = {
23
23
  [TYPE_INDEX_MAP.aliases]: [],
24
24
  [TYPE_INDEX_MAP.text]: {},
25
25
  [TYPE_INDEX_MAP.vector]: undefined, // maybe not can set a vec with 0
26
+ [TYPE_INDEX_MAP.colvec]: undefined, // maybe not can set a vec with 0
26
27
  };
27
28
  //# sourceMappingURL=defaultMap.js.map
@@ -0,0 +1,3 @@
1
+ import { SchemaProp } from '../types.js';
2
+ export declare function getPropLen(schemaProp: SchemaProp): any;
3
+ //# sourceMappingURL=getPropLen.d.ts.map
@@ -0,0 +1,23 @@
1
+ import { getPropType } from '../parse/utils.js';
2
+ import { isPropType } from '../types.js';
3
+ import { SIZE_MAP } from './types.js';
4
+ export function getPropLen(schemaProp) {
5
+ let len = SIZE_MAP[getPropType(schemaProp)];
6
+ if (isPropType('string', schemaProp) ||
7
+ isPropType('alias', schemaProp) ||
8
+ isPropType('cardinality', schemaProp)) {
9
+ if (typeof schemaProp === 'object') {
10
+ if (schemaProp.maxBytes < 61) {
11
+ len = schemaProp.maxBytes + 1;
12
+ }
13
+ else if ('max' in schemaProp && schemaProp.max < 31) {
14
+ len = schemaProp.max * 2 + 1;
15
+ }
16
+ }
17
+ }
18
+ else if (isPropType('vector', schemaProp)) {
19
+ len = 4 * schemaProp.size;
20
+ }
21
+ return len;
22
+ }
23
+ //# sourceMappingURL=getPropLen.js.map
@@ -0,0 +1,3 @@
1
+ import { SchemaTypeDef, PropDef } from './types.js';
2
+ export declare function makePacked(result: Partial<SchemaTypeDef>, typeName: string, vals: PropDef[], len: number): void;
3
+ //# sourceMappingURL=makePacked.d.ts.map
@@ -0,0 +1,50 @@
1
+ export function makePacked(result, typeName, vals, len) {
2
+ const encoder = new TextEncoder();
3
+ result.buf = new Uint8Array(len);
4
+ result.buf[0] = result.idUint8[0];
5
+ result.buf[1] = result.idUint8[1];
6
+ const fieldNames = [];
7
+ const tNameBuf = encoder.encode(typeName);
8
+ fieldNames.push(tNameBuf);
9
+ let fieldNameLen = tNameBuf.byteLength + 1;
10
+ let i = 2;
11
+ if (result.mainLen) {
12
+ result.buf[i] = 0;
13
+ for (const f of vals) {
14
+ if (!f.separate) {
15
+ i++;
16
+ result.buf[i] = f.typeIndex;
17
+ const name = encoder.encode(f.path.join('.'));
18
+ fieldNames.push(name);
19
+ fieldNameLen += name.byteLength + 1;
20
+ }
21
+ }
22
+ i++;
23
+ result.buf[i] = 0;
24
+ }
25
+ for (const f of vals) {
26
+ if (f.separate) {
27
+ i++;
28
+ result.buf[i] = f.prop;
29
+ i++;
30
+ result.buf[i] = f.typeIndex;
31
+ const name = encoder.encode(f.path.join('.'));
32
+ fieldNames.push(name);
33
+ fieldNameLen += name.byteLength + 1;
34
+ }
35
+ }
36
+ result.propNames = new Uint8Array(fieldNameLen);
37
+ let lastWritten = 0;
38
+ for (const f of fieldNames) {
39
+ result.propNames[lastWritten] = f.byteLength;
40
+ result.propNames.set(f, lastWritten + 1);
41
+ lastWritten += f.byteLength + 1;
42
+ }
43
+ let bufLen = result.buf.length;
44
+ result.packed = new Uint8Array(2 + bufLen + result.propNames.length);
45
+ result.packed[0] = bufLen;
46
+ result.packed[1] = bufLen >>>= 8;
47
+ result.packed.set(result.buf, 2);
48
+ result.packed.set(result.propNames, result.buf.length + 2);
49
+ }
50
+ //# sourceMappingURL=makePacked.js.map
@@ -0,0 +1,3 @@
1
+ import { SchemaTypeDef } from './types.js';
2
+ export declare const readFromPacked: (packed: Uint8Array) => SchemaTypeDef;
3
+ //# sourceMappingURL=readFromPacked.d.ts.map
@@ -0,0 +1,140 @@
1
+ import { REVERSE_SIZE_MAP } from './types.js';
2
+ import { DEFAULT_MAP } from './defaultMap.js';
3
+ import { VALIDATION_MAP } from './validation.js';
4
+ export const readFromPacked = (packed) => {
5
+ const size = (packed[0] | (packed[1] << 8)) >>> 0;
6
+ const props = [];
7
+ const b = packed.subarray(2, 2 + size);
8
+ let collectMain = false;
9
+ const mainProps = [];
10
+ const typeId = b.subarray(0, 2);
11
+ const typeIdNr = (typeId[0] | (typeId[1] << 8)) >>> 0;
12
+ for (let i = 2; i < b.length; i++) {
13
+ const prop = b[i];
14
+ if (collectMain) {
15
+ if (prop === 0) {
16
+ collectMain = false;
17
+ }
18
+ else {
19
+ mainProps.push({
20
+ prop: 0,
21
+ typeIndex: b[i],
22
+ });
23
+ }
24
+ }
25
+ else {
26
+ if (prop == 0) {
27
+ collectMain = true;
28
+ }
29
+ else {
30
+ props.push({ prop, typeIndex: b[i + 1] });
31
+ i++;
32
+ }
33
+ }
34
+ }
35
+ const decoder = new TextDecoder();
36
+ const fields = [];
37
+ const f = packed.subarray(2 + size, packed.length);
38
+ for (let i = 0; i < f.length; i++) {
39
+ const size = f[i];
40
+ fields.push(decoder.decode(f.subarray(i + 1, i + 1 + size)));
41
+ i += size;
42
+ }
43
+ for (let i = 0; i < mainProps.length; i++) {
44
+ mainProps[i].path = fields[i + 1];
45
+ }
46
+ for (let i = 0; i < props.length; i++) {
47
+ props[i].path = fields[i + 1 + mainProps.length];
48
+ }
49
+ // Fixed len strings not supported
50
+ // Refs also not supported
51
+ // Text not supported yet
52
+ // Ref not supported yet
53
+ // compression: 1 (0)
54
+ const result = {
55
+ cnt: 0,
56
+ checksum: 0,
57
+ total: 0,
58
+ type: fields[0],
59
+ lastId: 0,
60
+ blockCapacity: 0,
61
+ mainLen: mainProps.length,
62
+ buf: b,
63
+ propNames: f,
64
+ packed,
65
+ props: {},
66
+ reverseProps: {}, // in a bit
67
+ id: typeIdNr,
68
+ idUint8: typeId,
69
+ separate: [],
70
+ main: {},
71
+ tree: {},
72
+ // not nessecary...
73
+ hasSeperateSort: false,
74
+ seperateSort: {
75
+ size: 0,
76
+ buffer: new Uint8Array([]),
77
+ bufferTmp: new Uint8Array([]),
78
+ props: [],
79
+ },
80
+ hasSeperateTextSort: false,
81
+ seperateTextSort: {
82
+ localeToIndex: new Map(),
83
+ localeStringToIndex: new Map(),
84
+ noUndefined: new Uint8Array([]),
85
+ size: 0,
86
+ buffer: new Uint8Array([]),
87
+ bufferTmp: new Uint8Array([]),
88
+ props: [],
89
+ },
90
+ mainEmpty: new Uint8Array([]),
91
+ mainEmptyAllZeroes: true,
92
+ // need this...
93
+ locales: {},
94
+ localeSize: 0,
95
+ };
96
+ let s = 0;
97
+ for (const p of mainProps) {
98
+ const len = REVERSE_SIZE_MAP[p.typeIndex];
99
+ const prop = {
100
+ prop: p.prop,
101
+ separate: false,
102
+ __isPropDef: true,
103
+ validation: VALIDATION_MAP[p.typeIndex],
104
+ start: s,
105
+ default: DEFAULT_MAP[p.typeIndex], // tmp
106
+ typeIndex: p.typeIndex,
107
+ path: p.path.split('.'),
108
+ len,
109
+ };
110
+ result.props[p.path] = prop;
111
+ result.main[prop.start] = prop;
112
+ s += len;
113
+ }
114
+ for (const p of props) {
115
+ const prop = {
116
+ prop: p.prop,
117
+ separate: true,
118
+ __isPropDef: true,
119
+ validation: VALIDATION_MAP[p.typeIndex],
120
+ start: 0,
121
+ typeIndex: p.typeIndex,
122
+ default: DEFAULT_MAP[p.typeIndex], // tmp
123
+ path: p.path.split('.'),
124
+ len: 0,
125
+ compression: 1,
126
+ };
127
+ result.props[p.path] = prop;
128
+ result.reverseProps[prop.prop] = prop;
129
+ }
130
+ // make this into a typeDef
131
+ // return {
132
+ // type: fields[0],
133
+ // fields,
134
+ // typeId,
135
+ // props,
136
+ // mainProps,
137
+ // }
138
+ return result;
139
+ };
140
+ //# sourceMappingURL=readFromPacked.js.map
@@ -1,4 +1,4 @@
1
- import { ALIAS, ALIASES, BINARY, EMPTY_MICRO_BUFFER, CARDINALITY, MICRO_BUFFER, REFERENCE, REFERENCES, STRING, TEXT, VECTOR, WEAK_REFERENCE, WEAK_REFERENCES, JSON, } from './types.js';
1
+ import { ALIAS, ALIASES, BINARY, EMPTY_MICRO_BUFFER, CARDINALITY, MICRO_BUFFER, REFERENCE, REFERENCES, STRING, TEXT, VECTOR, WEAK_REFERENCE, WEAK_REFERENCES, JSON, COLVEC, } from './types.js';
2
2
  const selvaFieldType = {
3
3
  NULL: 0,
4
4
  MICRO_BUFFER: 1,
@@ -26,6 +26,7 @@ selvaTypeMap[WEAK_REFERENCE] = selvaFieldType.WEAK_REFERENCE;
26
26
  selvaTypeMap[WEAK_REFERENCES] = selvaFieldType.WEAK_REFERENCES;
27
27
  selvaTypeMap[ALIAS] = selvaFieldType.ALIAS;
28
28
  selvaTypeMap[ALIASES] = selvaFieldType.ALIASES;
29
+ selvaTypeMap[COLVEC] = selvaFieldType.COLVEC;
29
30
  const EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT = 0x01;
30
31
  const EDGE_FIELD_CONSTRAINT_FLAG_SKIP_DUMP = 0x80;
31
32
  function blockCapacity(blockCapacity) {
@@ -53,6 +54,14 @@ const propDefBuffer = (schema, prop, isEdge) => {
53
54
  view.setUint16(1, prop.len, true);
54
55
  return [...buf];
55
56
  }
57
+ else if (prop.len && (type === COLVEC)) {
58
+ const buf = new Uint8Array(5);
59
+ const view = new DataView(buf.buffer);
60
+ buf[0] = selvaType;
61
+ view.setUint16(1, prop.len, true);
62
+ view.setUint16(3, 4, true); // TODO Other types than f32
63
+ return [...buf];
64
+ }
56
65
  else if (type === REFERENCE || type === REFERENCES) {
57
66
  const buf = new Uint8Array(9);
58
67
  const view = new DataView(buf.buffer);
@@ -0,0 +1,2 @@
1
+ export declare const convertToTimestamp: (value: string | Date | number) => number;
2
+ //# sourceMappingURL=timestamp.d.ts.map
@@ -0,0 +1,67 @@
1
+ const timeToNumber = (ex) => {
2
+ if (ex === 's') {
3
+ return 1000;
4
+ }
5
+ if (ex === 'm') {
6
+ return 1000 * 60;
7
+ }
8
+ if (ex === 'h') {
9
+ return 1000 * 60 * 60;
10
+ }
11
+ if (ex === 'd') {
12
+ return 1000 * 60 * 60 * 24;
13
+ }
14
+ if (ex === 'y') {
15
+ return 31556952000;
16
+ }
17
+ return 1;
18
+ };
19
+ export const convertToTimestamp = (value) => {
20
+ if (value instanceof Date) {
21
+ return value.valueOf();
22
+ }
23
+ if (typeof value === 'string') {
24
+ if (value === 'now') {
25
+ return Date.now();
26
+ }
27
+ const y = value.replace(/([+-])/g, ' $1 ');
28
+ const arr = y.split(/ +/);
29
+ let newValue = 0;
30
+ let now;
31
+ let op = 1;
32
+ for (const seg of arr) {
33
+ if (seg === '-') {
34
+ op = -1;
35
+ }
36
+ else if (seg === '+') {
37
+ op = 1;
38
+ }
39
+ else {
40
+ var v = 0;
41
+ if (seg === 'now') {
42
+ if (!now) {
43
+ now = Date.now();
44
+ }
45
+ v = now;
46
+ }
47
+ else if (/[smhdy]$/.test(seg)) {
48
+ const ex = seg[seg.length - 1];
49
+ const number = parseInt(seg, 10);
50
+ v = number * timeToNumber(ex);
51
+ }
52
+ else if (seg) {
53
+ v = new Date(seg).valueOf();
54
+ }
55
+ if (op === -1) {
56
+ newValue -= v;
57
+ }
58
+ else {
59
+ newValue += v;
60
+ }
61
+ }
62
+ }
63
+ return newValue;
64
+ }
65
+ return value;
66
+ };
67
+ //# sourceMappingURL=timestamp.js.map
@@ -1,7 +1,6 @@
1
1
  import { SchemaObject, StrictSchemaType, SchemaLocales } from '../index.js';
2
2
  import { SchemaTypeDef, SchemaTypesParsed } from './types.js';
3
3
  import { StrictSchema } from '../types.js';
4
- export declare const DEFAULT_BLOCK_CAPACITY = 100000;
5
4
  export declare const updateTypeDefs: (schema: StrictSchema) => {
6
5
  schemaTypesParsed: {
7
6
  [key: string]: SchemaTypeDef;
@@ -1,6 +1,6 @@
1
1
  import { isPropType, getPropType, } from '../index.js';
2
2
  import { setByPath } from '@saulx/utils';
3
- import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, NUMBER, } from './types.js';
3
+ import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, NUMBER, BLOCK_CAPACITY_MAX, BLOCK_CAPACITY_DEFAULT, BLOCK_CAPACITY_MIN, } from './types.js';
4
4
  import { DEFAULT_MAP } from './defaultMap.js';
5
5
  import { makeSeparateTextSort } from './makeSeparateTextSort.js';
6
6
  import { makeSeparateSort } from './makeSeparateSort.js';
@@ -9,39 +9,56 @@ import { addEdges } from './addEdges.js';
9
9
  import { createEmptyDef } from './createEmptyDef.js';
10
10
  import { fillEmptyMain, isZeroes } from './fillEmptyMain.js';
11
11
  import { defaultValidation, VALIDATION_MAP } from './validation.js';
12
- export const DEFAULT_BLOCK_CAPACITY = 100_000;
13
12
  export const updateTypeDefs = (schema) => {
14
13
  const schemaTypesParsed = {};
15
14
  const schemaTypesParsedById = {};
16
- for (const field in schemaTypesParsed) {
17
- if (field in schema.types) {
15
+ for (const typeName in schemaTypesParsed) {
16
+ if (typeName in schema.types) {
18
17
  continue;
19
18
  }
20
- const id = schemaTypesParsed[field].id;
21
- delete schemaTypesParsed[field];
19
+ const id = schemaTypesParsed[typeName].id;
20
+ delete schemaTypesParsed[typeName];
22
21
  delete schemaTypesParsedById[id];
23
22
  }
24
- for (const field in schema.types) {
25
- const type = schema.types[field];
23
+ for (const typeName in schema.types) {
24
+ const type = schema.types[typeName];
26
25
  if (!type.id) {
27
26
  throw new Error('NEED ID ON TYPE');
28
27
  }
29
- const def = createSchemaTypeDef(field, type, schemaTypesParsed, schema.locales ?? {
28
+ const def = createSchemaTypeDef(typeName, type, schemaTypesParsed, schema.locales ?? {
30
29
  en: {},
31
30
  });
32
- def.blockCapacity = field === '_root' ? 2147483647 : DEFAULT_BLOCK_CAPACITY;
33
- schemaTypesParsed[field] = def;
31
+ schemaTypesParsed[typeName] = def;
34
32
  schemaTypesParsedById[type.id] = def;
35
33
  }
36
34
  return { schemaTypesParsed, schemaTypesParsedById };
37
35
  };
38
36
  export const createSchemaTypeDef = (typeName, type, parsed, locales, result = createEmptyDef(typeName, type, locales), path = [], top = true) => {
39
- if (result.id == 0 && top) {
40
- if ('id' in type) {
41
- result.id = type.id;
37
+ if (top) {
38
+ if (result.id == 0) {
39
+ if ('id' in type) {
40
+ result.id = type.id;
41
+ }
42
+ else {
43
+ throw new Error(`Invalid schema type id ${result.type}`);
44
+ }
42
45
  }
43
- else {
44
- throw new Error(`Invalid schema type id ${result.type}`);
46
+ if (result.blockCapacity == 0) {
47
+ if ('blockCapacity' in type) {
48
+ if (typeof type.blockCapacity !== 'number' || type.blockCapacity < BLOCK_CAPACITY_MIN || type.blockCapacity > BLOCK_CAPACITY_MAX) {
49
+ throw new Error('Invalid blockCapacity');
50
+ }
51
+ result.blockCapacity = type.blockCapacity;
52
+ }
53
+ else {
54
+ result.blockCapacity = typeName === '_root' ? BLOCK_CAPACITY_MAX : BLOCK_CAPACITY_DEFAULT;
55
+ }
56
+ }
57
+ if (result.insertOnly == false && 'insertOnly' in type) {
58
+ result.insertOnly = !!type.insertOnly;
59
+ }
60
+ if (result.partial == false && 'partial' in type) {
61
+ result.partial = !!type.partial;
45
62
  }
46
63
  }
47
64
  result.locales = locales;
@@ -75,6 +92,11 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
75
92
  else if (isPropType('text', schemaProp)) {
76
93
  result.separateSortText++;
77
94
  }
95
+ else if (isPropType('colvec', schemaProp)) {
96
+ if (!result.insertOnly) {
97
+ throw new Error('colvec requires insertOnly');
98
+ }
99
+ }
78
100
  const isseparate = isSeparate(schemaProp, len);
79
101
  const typeIndex = TYPE_INDEX_MAP[propType];
80
102
  const prop = {
@@ -110,12 +132,18 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
110
132
  }
111
133
  }
112
134
  else if (isPropType('references', schemaProp)) {
135
+ if (result.partial) {
136
+ throw new Error('references is not supported with partial');
137
+ }
113
138
  prop.inversePropName = schemaProp.items.prop;
114
139
  prop.inverseTypeName = schemaProp.items.ref;
115
140
  prop.dependent = schemaProp.items.dependent;
116
141
  addEdges(prop, schemaProp.items);
117
142
  }
118
143
  else if (isPropType('reference', schemaProp)) {
144
+ if (result.partial) {
145
+ throw new Error('reference is not supported with partial');
146
+ }
119
147
  prop.inversePropName = schemaProp.prop;
120
148
  prop.inverseTypeName = schemaProp.ref;
121
149
  prop.dependent = schemaProp.dependent;
@@ -26,6 +26,7 @@ export declare const ID = 26;
26
26
  export declare const VECTOR = 27;
27
27
  export declare const JSON = 28;
28
28
  export declare const OBJECT = 29;
29
+ export declare const COLVEC = 30;
29
30
  export declare const TYPE_INDEX_MAP: {
30
31
  alias: number;
31
32
  aliases: number;
@@ -50,6 +51,7 @@ export declare const TYPE_INDEX_MAP: {
50
51
  cardinality: number;
51
52
  json: number;
52
53
  object: number;
54
+ colvec: number;
53
55
  };
54
56
  export declare const enum numberTypes {
55
57
  number = 4,
@@ -126,6 +128,9 @@ export type SchemaSortUndefinedHandler = {
126
128
  bufferTmp: Uint8Array;
127
129
  props: PropDef[];
128
130
  };
131
+ export declare const BLOCK_CAPACITY_MIN = 1025;
132
+ export declare const BLOCK_CAPACITY_MAX = 2147483647;
133
+ export declare const BLOCK_CAPACITY_DEFAULT = 100000;
129
134
  export type SchemaTypeDef = {
130
135
  cnt: number;
131
136
  checksum: number;
@@ -133,6 +138,8 @@ export type SchemaTypeDef = {
133
138
  lastId: number;
134
139
  blockCapacity: number;
135
140
  mainLen: number;
141
+ insertOnly: boolean;
142
+ partial: boolean;
136
143
  buf: Uint8Array;
137
144
  propNames: Uint8Array;
138
145
  props: {
package/dist/def/types.js CHANGED
@@ -25,6 +25,7 @@ export const ID = 26;
25
25
  export const VECTOR = 27;
26
26
  export const JSON = 28;
27
27
  export const OBJECT = 29;
28
+ export const COLVEC = 30;
28
29
  export const TYPE_INDEX_MAP = {
29
30
  alias: ALIAS,
30
31
  aliases: ALIASES,
@@ -49,6 +50,7 @@ export const TYPE_INDEX_MAP = {
49
50
  cardinality: CARDINALITY,
50
51
  json: JSON,
51
52
  object: OBJECT,
53
+ colvec: COLVEC,
52
54
  };
53
55
  const numberTypeValues = [
54
56
  NUMBER,
@@ -63,6 +65,9 @@ const numberTypeValues = [
63
65
  export function isNumberType(type) {
64
66
  return numberTypeValues.includes(type);
65
67
  }
68
+ export const BLOCK_CAPACITY_MIN = 1025;
69
+ export const BLOCK_CAPACITY_MAX = 2147483647;
70
+ export const BLOCK_CAPACITY_DEFAULT = 100_000;
66
71
  export const SIZE_MAP = {
67
72
  timestamp: 8, // 64bit
68
73
  // double-precision 64-bit binary format IEEE 754 value
@@ -88,6 +93,7 @@ export const SIZE_MAP = {
88
93
  vector: 0, // separate
89
94
  json: 0,
90
95
  object: 0,
96
+ colvec: 0, // separate
91
97
  };
92
98
  const reverseMap = {};
93
99
  for (const k in TYPE_INDEX_MAP) {
package/dist/def/utils.js CHANGED
@@ -3,7 +3,7 @@ import { isPropType } from '../types.js';
3
3
  import { getPropType } from '../parse/utils.js';
4
4
  import { convertToTimestamp } from '@saulx/utils';
5
5
  export function isSeparate(schemaProp, len) {
6
- return len === 0 || isPropType('vector', schemaProp);
6
+ return len === 0 || isPropType('vector', schemaProp) || isPropType('colvec', schemaProp);
7
7
  }
8
8
  export const propIsSigned = (prop) => {
9
9
  const t = prop.typeIndex;
@@ -43,6 +43,9 @@ export function getPropLen(schemaProp) {
43
43
  else if (isPropType('vector', schemaProp)) {
44
44
  len = 4 * schemaProp.size;
45
45
  }
46
+ else if (isPropType('colvec', schemaProp)) {
47
+ len = schemaProp.size;
48
+ }
46
49
  return len;
47
50
  }
48
51
  export const parseMinMaxStep = (val) => {
@@ -10,6 +10,7 @@ export const VALIDATION_MAP = {
10
10
  return true;
11
11
  },
12
12
  [TYPE_INDEX_MAP.binary]: (value) => {
13
+ console.log('DERP ', value, typeof value);
13
14
  if (value instanceof Uint8Array) {
14
15
  return true;
15
16
  }
package/dist/lang.d.ts CHANGED
@@ -147,7 +147,7 @@ declare const langCodes: {
147
147
  readonly ka: 145;
148
148
  readonly cnr: 146;
149
149
  };
150
- export declare const langCodesMap: Map<string, 0 | 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>;
150
+ export declare const langCodesMap: Map<string, 0 | 2 | 1 | 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>;
151
151
  export declare const inverseLangMap: Map<any, any>;
152
152
  export type LangName = keyof typeof langCodes;
153
153
  export type LangCode = (typeof langCodes)[LangName];
@@ -0,0 +1,3 @@
1
+ import { StrictSchema } from './types.js';
2
+ export declare const mermaid: (schema: StrictSchema) => string;
3
+ //# sourceMappingURL=mermaid.d.ts.map
@@ -0,0 +1,24 @@
1
+ import { getPropType } from './parse/utils.js';
2
+ import { isPropType } from './types.js';
3
+ export const mermaid = (schema) => {
4
+ let mermaid = 'classDiagram';
5
+ if (schema.types) {
6
+ for (const type in schema.types) {
7
+ for (const key in schema.types[type].props) {
8
+ const prop = schema.types[type].props[key];
9
+ const propType = getPropType(prop);
10
+ if (isPropType('reference', prop)) {
11
+ mermaid += `\n${type} --> ${prop.ref} : ${key}`;
12
+ }
13
+ else if (isPropType('references', prop)) {
14
+ mermaid += `\n${type} --> ${prop.items.ref} : ${key}[]`;
15
+ }
16
+ else {
17
+ mermaid += `\n${type} : ${propType} ${key}`;
18
+ }
19
+ }
20
+ }
21
+ }
22
+ return mermaid;
23
+ };
24
+ //# sourceMappingURL=mermaid.js.map
@@ -65,7 +65,6 @@ export class SchemaParser {
65
65
  for (const locale in locales) {
66
66
  const opts = locales[locale];
67
67
  if (opts === true) {
68
- console.log(locale, opts);
69
68
  continue;
70
69
  }
71
70
  expectObject(opts);
@@ -157,6 +157,15 @@ p.vector = propParser({
157
157
  return isDefault(val, prop, ctx);
158
158
  },
159
159
  }, 0);
160
+ p.colvec = propParser({
161
+ size(val) {
162
+ expectNumber(val);
163
+ },
164
+ }, {
165
+ default(val, prop, ctx) {
166
+ return isDefault(val, prop, ctx);
167
+ },
168
+ }, 0);
160
169
  p.enum = propParser({
161
170
  enum(items) {
162
171
  if (!Array.isArray(items)) {
@@ -1,5 +1,14 @@
1
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;
2
+ type Opts = {
3
+ readOnly?: boolean;
4
+ stripMetaInformation?: boolean;
5
+ };
6
+ export declare const serialize: (schema: any, opts?: Opts) => Uint8Array;
7
+ export declare const deSerializeKey: (buf: Uint8Array, keySize: number, i: number) => {
8
+ size: number;
9
+ value: string;
10
+ };
11
+ export declare const deSerializeInner: (buf: Uint8Array, obj: any, start: number, fromArray: boolean) => number;
4
12
  export declare const deSerialize: (buf: Uint8Array) => StrictSchema;
13
+ export {};
5
14
  //# sourceMappingURL=serialize.d.ts.map
package/dist/serialize.js CHANGED
@@ -1,108 +1,335 @@
1
- import * as deflate from 'fflate';
1
+ // import * as deflate from 'fflate'
2
+ import { stringFormats } from './types.js';
2
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';
3
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
+ };
4
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
+ };
5
157
  // 3 level
6
158
  // 0 for queries (min)
7
159
  // 1 for modify
8
160
  // 2 fulls schema
9
- const walk = (obj, prev, prev2, fromObject, schemaBuffer) => {
161
+ const walk = (opts, obj, prev, prev2, fromObject, schemaBuffer, isTypes) => {
10
162
  let start = schemaBuffer.len;
11
- // HANDLE ENUM
12
- const isSchemaProp = 'type' in obj && (prev2?.type === 'object' || fromObject === false);
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
13
167
  if (isSchemaProp) {
14
- schemaBuffer.buf[schemaBuffer.len++] = 254;
168
+ schemaBuffer.buf[schemaBuffer.len++] = SCHEMA_PROP;
15
169
  const typeIndex = TYPE_INDEX_MAP[obj.type];
16
170
  schemaBuffer.buf[schemaBuffer.len++] = typeIndex;
17
171
  }
18
172
  else {
19
- schemaBuffer.buf[schemaBuffer.len++] = 255;
173
+ schemaBuffer.buf[schemaBuffer.len++] = isArray ? ARRAY : OBJECT;
20
174
  }
21
175
  let sizeIndex = schemaBuffer.len;
22
176
  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;
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;
32
185
  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
186
  }
38
187
  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;
188
+ writeUint16(schemaBuffer.buf, j, schemaBuffer.len);
43
189
  schemaBuffer.len += 2;
44
190
  }
45
- const val = obj[key];
46
- const type = typeof val;
47
- // typed Array
48
- if (Array.isArray(val)) {
49
- // derp
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;
50
217
  }
51
- else if (type === 'function') {
52
- // derp
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;
53
231
  }
54
- else if (type === 'object') {
55
- // fromObject
56
- if (val === null) {
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;
57
268
  }
58
269
  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
- }
270
+ encodeKey(key, schemaBuffer);
65
271
  }
66
- }
67
- else if (type === 'string') {
68
- // derp
69
- }
70
- else if (type === 'number') {
71
- // do stuff
272
+ handleSingleValue(opts, obj[key], obj, prev, fromObject, key, isTypes);
72
273
  }
73
274
  }
74
275
  }
75
- const size = schemaBuffer.len - start;
276
+ let size = schemaBuffer.len - start;
76
277
  schemaBuffer.buf[sizeIndex] = size;
77
278
  schemaBuffer.buf[sizeIndex + 1] = size >>> 8;
78
279
  };
79
- export const serialize = (schema,
80
- // schema: StrictSchema,
81
- noCompression = false) => {
280
+ export const serialize = (schema, opts = {}) => {
82
281
  if (!schemaBuffer) {
83
- // 1mb buffer add check if its large enough else increase
84
282
  schemaBuffer = {
85
- buf: new Uint8Array(1e6),
283
+ buf: new Uint8Array(10e3), // 10kb default
86
284
  len: 0,
87
285
  dictMap: {},
88
286
  };
89
287
  }
90
- schemaBuffer.dictMap = {};
91
288
  schemaBuffer.len = 0;
92
- const isDeflate = noCompression ? 0 : 1;
93
- walk(schema, undefined, undefined, false, schemaBuffer);
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);
94
293
  const packed = new Uint8Array(schemaBuffer.buf.subarray(0, schemaBuffer.len));
95
- if (isDeflate) {
96
- return deflate.deflateSync(packed);
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));
97
322
  }
98
323
  else {
99
- return packed;
324
+ const actualKeySize = keySize - KEY_OPTS;
325
+ value = decoder.decode(buf.subarray(i, actualKeySize + i));
326
+ size += actualKeySize;
100
327
  }
328
+ return { size, value };
101
329
  };
102
- const decoder = new TextDecoder();
103
- export const deSerializeInner = (buf, obj, start) => {
330
+ export const deSerializeInner = (buf, obj, start, fromArray) => {
104
331
  let i = start;
105
- const isSchemaProp = buf[i] === 254;
332
+ const isSchemaProp = buf[i] === SCHEMA_PROP;
106
333
  i += 1;
107
334
  if (isSchemaProp) {
108
335
  const type = buf[i];
@@ -110,33 +337,137 @@ export const deSerializeInner = (buf, obj, start) => {
110
337
  obj.type = parsedType;
111
338
  i += 1;
112
339
  }
113
- const size = buf[i] | ((buf[i + 1] << 8) >>> 0);
340
+ const size = readUint16(buf, i);
114
341
  i += 2;
115
342
  const end = size + start;
343
+ if (fromArray) {
344
+ i += 2;
345
+ }
116
346
  while (i < end) {
117
- let keySize = buf[i];
118
- i += 1;
119
347
  let key;
120
- if (keySize === 0) {
121
- const dictAddress = buf[i] | ((buf[i + 1] << 8) >>> 0);
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);
122
419
  i += 2;
123
- keySize = buf[dictAddress];
124
- key = decoder.decode(buf.subarray(dictAddress + 1, keySize + dictAddress + 1));
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;
125
456
  }
126
457
  else {
127
- key = decoder.decode(buf.subarray(i, keySize + i));
128
- i += keySize;
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;
129
463
  }
130
- const nest = (obj[key] = {});
131
- const fieldSize = deSerializeInner(buf, nest, i);
132
- i += fieldSize;
133
464
  }
134
465
  return i - start;
135
466
  };
136
467
  export const deSerialize = (buf) => {
137
468
  // if first byte is deflate
138
469
  const schema = {};
139
- deSerializeInner(buf, schema, 0);
470
+ deSerializeInner(buf, schema, 0, false);
140
471
  return schema;
141
472
  };
142
473
  //# sourceMappingURL=serialize.js.map
package/dist/types.d.ts CHANGED
@@ -96,6 +96,11 @@ export type SchemaVector = Prop<{
96
96
  default?: Float32Array;
97
97
  size: number;
98
98
  }>;
99
+ export type SchemaColvec = Prop<{
100
+ type: 'colvec';
101
+ default?: Float32Array;
102
+ size: number;
103
+ }>;
99
104
  export type SchemaTimestamp = Prop<{
100
105
  type: 'timestamp';
101
106
  default?: number | Date | string;
@@ -150,7 +155,7 @@ export type SchemaSet<ItemsType extends SetItems = SetItems> = Prop<{
150
155
  } ? ItemsType['default'][] : undefined;
151
156
  items: ItemsType & NeverInItems;
152
157
  }>;
153
- 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);
154
159
  export type SchemaProp<isStrict = false> = SchemaReferencesWithQuery | SchemaReferenceWithQuery | NonRefSchemaProps<isStrict> | SchemaReferences | SchemaReference | SchemaObject | SchemaBinary;
155
160
  export type SchemaPropOneWay<isStrict = false> = SchemaReferencesOneWay | SchemaReferenceOneWay | SchemaObjectOneWay | NonRefSchemaProps<isStrict>;
156
161
  export type SchemaAnyProp = SchemaPropOneWay | SchemaProp;
@@ -165,6 +170,9 @@ type GenericSchemaType<isStrict = false> = {
165
170
  delete?: SchemaHook;
166
171
  };
167
172
  id?: number;
173
+ blockCapacity?: number;
174
+ insertOnly?: boolean;
175
+ partial?: boolean;
168
176
  props: SchemaProps<isStrict>;
169
177
  };
170
178
  export type StrictSchemaType = GenericSchemaType<true>;
@@ -204,6 +212,7 @@ export type SchemaPropTypeMap = {
204
212
  binary: SchemaBinary;
205
213
  cardinality: SchemaCardinality;
206
214
  vector: SchemaVector;
215
+ colvec: SchemaColvec;
207
216
  } & Record<NumberType, SchemaNumber>;
208
217
  export type SchemaPropTypes = keyof SchemaPropTypeMap;
209
218
  export declare const isPropType: <T extends SchemaPropTypes>(type: T, prop: SchemaProp) => prop is SchemaPropTypeMap[T];
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=validation.d.ts.map
@@ -0,0 +1,6 @@
1
+ export {};
2
+ // use typeIndex here
3
+ // end export them per type
4
+ // can also add validate on the prop def
5
+ // this way we can actually write custom ones
6
+ //# sourceMappingURL=validation.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/schema",
3
- "version": "5.0.0-alpha.20",
3
+ "version": "5.0.0-alpha.21",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",
@@ -29,7 +29,6 @@
29
29
  "typescript": "^5.6.3"
30
30
  },
31
31
  "dependencies": {
32
- "fflate": "0.8.1",
33
32
  "@saulx/utils": "^6.7.0",
34
33
  "picocolors": "^1.1.0"
35
34
  }