@based/schema 5.0.4-alpha.1 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/schema",
3
- "version": "5.0.4-alpha.1",
3
+ "version": "5.1.0",
4
4
  "files": [
5
5
  "dist",
6
6
  "README.md",
@@ -15,6 +15,7 @@
15
15
  ".": "./dist/index.js"
16
16
  },
17
17
  "scripts": {
18
+ "clean": "rm -rf .node_modules ./dist ./tsconfig.tsbuildinfo",
18
19
  "build": "tsc",
19
20
  "watch": "tsc --watch",
20
21
  "test": "tsc && tsc $npm_config --noEmit && tsx --test $npm_config"
@@ -30,7 +31,8 @@
30
31
  "typescript": "^5.6.3"
31
32
  },
32
33
  "dependencies": {
33
- "@based/utils": "^1.0.0",
34
- "picocolors": "^1.1.0"
34
+ "@based/utils": "1.2.0",
35
+ "picocolors": "^1.1.0",
36
+ "validator": "^13.15.20"
35
37
  }
36
38
  }
@@ -1,7 +0,0 @@
1
- export default class RefSet {
2
- #private;
3
- add(srcType: number, srcField: number, dstType: number, dstField: number): boolean;
4
- clear(): void;
5
- values(): SetIterator<number>;
6
- }
7
- //# sourceMappingURL=refSet.d.ts.map
@@ -1,25 +0,0 @@
1
- const packOne = (nodeType, field) => ((nodeType & 0xffff) << 8) | (field & 0xff);
2
- const packTwo = (a, b) => (a << 24) | (b & 0xffffff);
3
- const makeTuple = (aType, aField, bType, bField) => {
4
- const a = packOne(aType, aField);
5
- const b = packOne(bType, bField);
6
- return a < b ? packTwo(a, b) : packTwo(b, a);
7
- };
8
- export default class RefSet {
9
- #s = new Set();
10
- add(srcType, srcField, dstType, dstField) {
11
- const t = makeTuple(srcType, srcField, dstType, dstField);
12
- if (this.#s.has(t)) {
13
- return false;
14
- }
15
- this.#s.add(t);
16
- return true;
17
- }
18
- clear() {
19
- this.#s.clear();
20
- }
21
- values() {
22
- return this.#s.values();
23
- }
24
- }
25
- //# sourceMappingURL=refSet.js.map
@@ -1,5 +0,0 @@
1
- import { SchemaTypeDef } from './types.js';
2
- export declare function schemaToSelvaBuffer(schema: {
3
- [key: string]: SchemaTypeDef;
4
- }): ArrayBuffer[];
5
- //# sourceMappingURL=selvaBuffer.d.ts.map
@@ -1,164 +0,0 @@
1
- import { ALIAS, ALIASES, BINARY, EMPTY_MICRO_BUFFER, CARDINALITY, MICRO_BUFFER, REFERENCE, REFERENCES, STRING, TEXT, VECTOR, WEAK_REFERENCE, WEAK_REFERENCES, JSON, COLVEC, VECTOR_BASE_TYPE_SIZE_MAP, } from './types.js';
2
- import RefSet from './refSet.js';
3
- const selvaFieldType = {
4
- NULL: 0,
5
- MICRO_BUFFER: 1,
6
- STRING: 2,
7
- TEXT: 3,
8
- REFERENCE: 4,
9
- REFERENCES: 5,
10
- WEAK_REFERENCE: 6,
11
- WEAK_REFERENCES: 7,
12
- ALIAS: 8,
13
- ALIASES: 9,
14
- COLVEC: 10,
15
- };
16
- const selvaTypeMap = new Uint8Array(32); // 1.2x faster than JS array
17
- selvaTypeMap[MICRO_BUFFER] = selvaFieldType.MICRO_BUFFER;
18
- selvaTypeMap[VECTOR] = selvaFieldType.MICRO_BUFFER;
19
- selvaTypeMap[BINARY] = selvaFieldType.STRING;
20
- selvaTypeMap[CARDINALITY] = selvaFieldType.STRING;
21
- selvaTypeMap[JSON] = selvaFieldType.STRING;
22
- selvaTypeMap[STRING] = selvaFieldType.STRING;
23
- selvaTypeMap[TEXT] = selvaFieldType.TEXT;
24
- selvaTypeMap[REFERENCE] = selvaFieldType.REFERENCE;
25
- selvaTypeMap[REFERENCES] = selvaFieldType.REFERENCES;
26
- selvaTypeMap[WEAK_REFERENCE] = selvaFieldType.WEAK_REFERENCE;
27
- selvaTypeMap[WEAK_REFERENCES] = selvaFieldType.WEAK_REFERENCES;
28
- selvaTypeMap[ALIAS] = selvaFieldType.ALIAS;
29
- selvaTypeMap[ALIASES] = selvaFieldType.ALIASES;
30
- selvaTypeMap[COLVEC] = selvaFieldType.COLVEC;
31
- const EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT = 0x01;
32
- const EDGE_FIELD_CONSTRAINT_FLAG_SKIP_DUMP = 0x80;
33
- function blockCapacity(blockCapacity) {
34
- const buf = new Uint8Array(Uint32Array.BYTES_PER_ELEMENT);
35
- const view = new DataView(buf.buffer);
36
- view.setUint32(0, blockCapacity, true);
37
- return buf;
38
- }
39
- function sepPropCount(props) {
40
- return props.filter((prop) => prop.separate).length;
41
- }
42
- function makeEdgeConstraintFlags(refSet, nodeTypeId, prop, dstNodeTypeId, inverseProp) {
43
- let flags = 0;
44
- flags |= prop.dependent ? EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT : 0x00;
45
- flags |=
46
- prop.typeIndex === REFERENCE &&
47
- inverseProp &&
48
- inverseProp.typeIndex === REFERENCES
49
- ? EDGE_FIELD_CONSTRAINT_FLAG_SKIP_DUMP
50
- : 0x00;
51
- if (refSet) {
52
- const x = refSet.add(nodeTypeId, prop.prop, dstNodeTypeId, inverseProp.prop);
53
- flags |= x ? 0x00 : EDGE_FIELD_CONSTRAINT_FLAG_SKIP_DUMP;
54
- }
55
- return flags;
56
- }
57
- const propDefBuffer = (refSet, nodeTypeId, schema, prop, isEdge) => {
58
- const type = prop.typeIndex;
59
- const selvaType = selvaTypeMap[type];
60
- if (prop.len && (type === MICRO_BUFFER || type === VECTOR)) {
61
- const buf = new Uint8Array(3);
62
- const view = new DataView(buf.buffer);
63
- buf[0] = selvaType;
64
- view.setUint16(1, prop.len, true);
65
- return [...buf];
66
- }
67
- else if (prop.len && type === COLVEC) {
68
- const buf = new Uint8Array(5);
69
- const view = new DataView(buf.buffer);
70
- buf[0] = selvaType;
71
- const baseSize = VECTOR_BASE_TYPE_SIZE_MAP[prop.vectorBaseType];
72
- view.setUint16(1, prop.len / baseSize, true); // elements
73
- view.setUint16(3, baseSize, true); // element size
74
- return [...buf];
75
- }
76
- else if (type === REFERENCE || type === REFERENCES) {
77
- const buf = new Uint8Array(9);
78
- const view = new DataView(buf.buffer);
79
- const dstType = schema[prop.inverseTypeName];
80
- let eschema = [];
81
- // @ts-ignore
82
- buf[0] = selvaType + 2 * !!isEdge; // field type
83
- buf[1] = makeEdgeConstraintFlags(refSet, nodeTypeId, prop, dstType.id, dstType.props[prop.inversePropName]); // flags
84
- view.setUint16(2, dstType.id, true); // dst_node_type
85
- view.setUint32(5, 0, true); // schema_len
86
- if (!isEdge) {
87
- buf[4] = prop.inversePropNumber;
88
- if (prop.edges) {
89
- const edgesS = Object.values(prop.edges);
90
- if (edgesS.length) {
91
- const props = edgesS
92
- .filter((v) => v.separate === true)
93
- .sort((a, b) => (a.prop > b.prop ? 1 : -1));
94
- const p = [
95
- {
96
- ...EMPTY_MICRO_BUFFER,
97
- len: prop.edgeMainLen || 1, // allow zero here... else useless padding
98
- __isEdgeDef: true,
99
- },
100
- // or handle this here...
101
- ...props,
102
- ];
103
- eschema = p
104
- .map((prop) => propDefBuffer(null, 0, schema, prop, true))
105
- .flat(1);
106
- eschema.unshift(0, 0, 0, 0, sepPropCount(p), 0, 0, 0);
107
- view.setUint32(5, eschema.length, true);
108
- }
109
- }
110
- }
111
- return [...buf, ...eschema];
112
- }
113
- else if (type === STRING ||
114
- type === BINARY ||
115
- type === CARDINALITY ||
116
- type === JSON) {
117
- return [selvaType, prop.len < 50 ? prop.len : 0];
118
- }
119
- {
120
- return [selvaType];
121
- }
122
- };
123
- // TODO rewrite
124
- export function schemaToSelvaBuffer(schema) {
125
- const refSet = new RefSet();
126
- return Object.values(schema).map((t) => {
127
- const props = Object.values(t.props);
128
- const rest = [];
129
- const nrFields = 1 + sepPropCount(props);
130
- let refFields = 0;
131
- let virtualFields = 0;
132
- if (nrFields >= 250) {
133
- throw new Error('Too many fields');
134
- }
135
- for (const f of props) {
136
- if (f.separate) {
137
- if (f.typeIndex === REFERENCE || f.typeIndex === REFERENCES) {
138
- refFields++;
139
- }
140
- else if (f.typeIndex === ALIAS ||
141
- f.typeIndex === ALIASES ||
142
- f.typeIndex === COLVEC) {
143
- // We assume that these are always the last props!
144
- virtualFields++;
145
- }
146
- rest.push(f);
147
- }
148
- }
149
- rest.sort((a, b) => a.prop - b.prop);
150
- return Uint8Array.from([
151
- ...blockCapacity(t.blockCapacity), // u32 blockCapacity
152
- nrFields, // u8 nrFields
153
- 1 + refFields, // u8 nrFixedFields
154
- virtualFields, // u8 nrVirtualFields
155
- 0, // u8 spare1
156
- ...propDefBuffer(null, t.id, schema, {
157
- ...EMPTY_MICRO_BUFFER,
158
- len: t.mainLen === 0 ? 1 : t.mainLen,
159
- }),
160
- ...rest.map((f) => propDefBuffer(refSet, t.id, schema, f)).flat(1),
161
- ]).buffer;
162
- });
163
- }
164
- //# sourceMappingURL=selvaBuffer.js.map