@based/schema 5.0.0-alpha.2 → 5.0.0-alpha.20

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,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/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?: ArrayBuffer;
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;
@@ -92,19 +98,22 @@ export type SchemaVector = Prop<{
92
98
  }>;
93
99
  export type SchemaTimestamp = Prop<{
94
100
  type: 'timestamp';
95
- default?: number | Date;
101
+ default?: number | Date | string;
96
102
  on?: 'create' | 'update';
97
103
  display?: DateDisplay;
104
+ min?: number | string;
105
+ max?: number | string;
106
+ step?: number | 'any' | string;
98
107
  }>;
99
108
  export type SchemaReferenceOneWay = Prop<{
100
109
  type?: 'reference';
101
- default?: string;
110
+ default?: number;
102
111
  ref: string;
103
112
  mime?: Mime;
104
113
  }>;
105
114
  export type SchemaReference = Prop<{
106
115
  type?: 'reference';
107
- default?: string;
116
+ default?: number;
108
117
  ref: string;
109
118
  prop: string;
110
119
  dependent?: boolean;
@@ -126,7 +135,7 @@ export type SchemaReferencesWithQuery = SchemaReferencesOneWay & {
126
135
  };
127
136
  export type SchemaEnum = Prop<{
128
137
  type?: 'enum';
129
- default?: EnumItem;
138
+ default?: EnumItem | undefined;
130
139
  enum: EnumItem[];
131
140
  }>;
132
141
  export type SchemaAlias = Omit<SchemaString, 'type'> & {
@@ -176,9 +185,9 @@ type GenericSchema<isStrict = false> = {
176
185
  };
177
186
  export type StrictSchema = GenericSchema<true>;
178
187
  export type Schema = GenericSchema<false> | StrictSchema;
179
- export type SchemaLocales = Record<LangName, {
188
+ export type SchemaLocales = Record<LangName, true | {
180
189
  required?: boolean;
181
- fallback?: LangName[];
190
+ fallback?: LangName;
182
191
  }>;
183
192
  export type SchemaPropTypeMap = {
184
193
  references: SchemaReferences;
@@ -198,5 +207,7 @@ export type SchemaPropTypeMap = {
198
207
  } & Record<NumberType, SchemaNumber>;
199
208
  export type SchemaPropTypes = keyof SchemaPropTypeMap;
200
209
  export declare const isPropType: <T extends SchemaPropTypes>(type: T, prop: SchemaProp) => prop is SchemaPropTypeMap[T];
210
+ export declare const MAX_ID = 4294967295;
211
+ export declare const MIN_ID = 1;
201
212
  export {};
202
213
  //# sourceMappingURL=types.d.ts.map
package/dist/types.js CHANGED
@@ -95,4 +95,6 @@ export const stringFormats = [
95
95
  export const isPropType = (type, prop) => {
96
96
  return getPropType(prop) === type;
97
97
  };
98
+ export const MAX_ID = 4294967295;
99
+ export const MIN_ID = 1;
98
100
  //# sourceMappingURL=types.js.map
package/package.json CHANGED
@@ -1,19 +1,22 @@
1
1
  {
2
2
  "name": "@based/schema",
3
- "version": "5.0.0-alpha.2",
3
+ "version": "5.0.0-alpha.20",
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 test/*.ts --noEmit && tsx --test test/*.ts",
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,8 @@
26
29
  "typescript": "^5.6.3"
27
30
  },
28
31
  "dependencies": {
32
+ "fflate": "0.8.1",
33
+ "@saulx/utils": "^6.7.0",
29
34
  "picocolors": "^1.1.0"
30
35
  }
31
36
  }
package/dist/mermaid.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { StrictSchema } from './types.js';
2
- export declare const mermaid: (schema: StrictSchema) => string;
3
- //# sourceMappingURL=mermaid.d.ts.map
package/dist/mermaid.js DELETED
@@ -1,24 +0,0 @@
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