binary-structures-values 0.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/dist/cjs/bit-stream-reader.d.ts +33 -0
- package/dist/cjs/bit-stream-reader.js +340 -0
- package/dist/cjs/bit-stream-writer.d.ts +36 -0
- package/dist/cjs/bit-stream-writer.js +336 -0
- package/dist/cjs/compiler-old.d.ts +9 -0
- package/dist/cjs/compiler-old.js +268 -0
- package/dist/cjs/data-stream-reader.d.ts +23 -0
- package/dist/cjs/data-stream-reader.js +132 -0
- package/dist/cjs/data-stream-writer.d.ts +22 -0
- package/dist/cjs/data-stream-writer.js +179 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/schema-structure.d.ts +54 -0
- package/dist/cjs/schema-structure.js +273 -0
- package/dist/esm/bit-stream-reader.d.ts +33 -0
- package/dist/esm/bit-stream-reader.js +336 -0
- package/dist/esm/bit-stream-writer.d.ts +36 -0
- package/dist/esm/bit-stream-writer.js +335 -0
- package/dist/esm/compiler-old.d.ts +9 -0
- package/dist/esm/compiler-old.js +264 -0
- package/dist/esm/data-stream-reader.d.ts +23 -0
- package/dist/esm/data-stream-reader.js +128 -0
- package/dist/esm/data-stream-writer.d.ts +22 -0
- package/dist/esm/data-stream-writer.js +175 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/schema-structure.d.ts +54 -0
- package/dist/esm/schema-structure.js +267 -0
- package/package.json +38 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DataStreamReader = void 0;
|
|
4
|
+
const bit_stream_reader_1 = require("./bit-stream-reader");
|
|
5
|
+
const schema_structure_1 = require("./schema-structure");
|
|
6
|
+
const compiler_old_1 = require("./compiler-old");
|
|
7
|
+
class DataStreamReader extends bit_stream_reader_1.BitStreamReader {
|
|
8
|
+
constructor(schema_or_data, data_or_complete, isComplete) {
|
|
9
|
+
let initialData;
|
|
10
|
+
let schema;
|
|
11
|
+
if (schema_or_data) {
|
|
12
|
+
if (schema_or_data instanceof Uint8Array) {
|
|
13
|
+
initialData = schema_or_data;
|
|
14
|
+
isComplete = !!data_or_complete;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
if (typeof schema_or_data == 'string')
|
|
18
|
+
[schema] = (0, schema_structure_1.decodeSchemaString)(schema_or_data);
|
|
19
|
+
else
|
|
20
|
+
[schema] = (0, schema_structure_1.decodeSchemaString)((0, schema_structure_1.encodeSchema)(schema_or_data));
|
|
21
|
+
if (data_or_complete) {
|
|
22
|
+
if (data_or_complete instanceof Uint8Array) {
|
|
23
|
+
initialData = data_or_complete;
|
|
24
|
+
isComplete = !!isComplete;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
super(initialData, isComplete);
|
|
30
|
+
this.schema = this.schema ?? schema;
|
|
31
|
+
}
|
|
32
|
+
static compile(schema) {
|
|
33
|
+
return (0, compiler_old_1.compileReader)(schema);
|
|
34
|
+
}
|
|
35
|
+
readCompiled(compiledFn) {
|
|
36
|
+
return compiledFn(this);
|
|
37
|
+
}
|
|
38
|
+
addData(data) {
|
|
39
|
+
super.addData(data);
|
|
40
|
+
if (this.metaData === undefined && this.canReadString())
|
|
41
|
+
this.metaData = this.readString();
|
|
42
|
+
let schemaString;
|
|
43
|
+
if (!this.schema && this.canReadString() && (schemaString = this.readString())) {
|
|
44
|
+
const [schema, meta] = (0, schema_structure_1.decodeSchemaString)(schemaString);
|
|
45
|
+
this.schema = schema;
|
|
46
|
+
this.metaData = this.metaData || meta;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
isReadyToRead() {
|
|
50
|
+
return this.metaData !== undefined && !!this.schema;
|
|
51
|
+
}
|
|
52
|
+
// if not enough bytes available to read, throws error if isComplete otherwise returns null
|
|
53
|
+
read() {
|
|
54
|
+
if (!this.schema)
|
|
55
|
+
throw new Error('Schema not found');
|
|
56
|
+
if (this.getAvailableBits() <= 0)
|
|
57
|
+
return null;
|
|
58
|
+
const savedPosition = this.bitPosition;
|
|
59
|
+
try {
|
|
60
|
+
return this.readObject(this.schema);
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
this.bitPosition = savedPosition;
|
|
64
|
+
if (!this.isDataComplete()) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
throw e;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
readObject(schema) {
|
|
71
|
+
const result = {};
|
|
72
|
+
for (const key of Object.keys(schema)) {
|
|
73
|
+
const def = schema[key];
|
|
74
|
+
const isOptional = ('optional' in def && def.optional) || ('types' in def &&
|
|
75
|
+
typeof def.types[0] === 'object' && 'optional' in def.types[0] && def.types[0].optional);
|
|
76
|
+
if (isOptional && !this.readBit()) {
|
|
77
|
+
if ('default' in def && def.default !== undefined)
|
|
78
|
+
result[key] = def.default;
|
|
79
|
+
else if ('types' in def && typeof def.types[0] === 'object' && 'default' in def.types[0] && def.types[0].default !== undefined)
|
|
80
|
+
result[key] = def.types[0].default;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
result[key] = 'types' in def ? this.readMultiType(def) : this.readSingleType(def);
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
readSingleType(def) {
|
|
88
|
+
if (def.type === 'null')
|
|
89
|
+
return null;
|
|
90
|
+
if (def.type === 'boolean')
|
|
91
|
+
return this.readBit();
|
|
92
|
+
if (def.type === 'string')
|
|
93
|
+
return this.readString();
|
|
94
|
+
if (def.type === 'int-8')
|
|
95
|
+
return this.readInt8();
|
|
96
|
+
if (def.type === 'int-16')
|
|
97
|
+
return this.readInt16();
|
|
98
|
+
if (def.type === 'int-32')
|
|
99
|
+
return this.readInt32();
|
|
100
|
+
if (def.type === 'bigint')
|
|
101
|
+
return this.readBigInt();
|
|
102
|
+
if (def.type === 'float-32')
|
|
103
|
+
return this.readFloat32();
|
|
104
|
+
if (def.type === 'float-64')
|
|
105
|
+
return this.readFloat64();
|
|
106
|
+
if (def.type === 'array')
|
|
107
|
+
return this.readArray(def.items);
|
|
108
|
+
if (def.type === 'object')
|
|
109
|
+
return this.readObject(def.items);
|
|
110
|
+
throw new Error(`Invalid type: ${def.type}`);
|
|
111
|
+
}
|
|
112
|
+
readMultiType(def) {
|
|
113
|
+
const noOfDefs = def.types.length;
|
|
114
|
+
const minBits = Math.ceil(Math.log2(noOfDefs));
|
|
115
|
+
const bits = this.readBits(minBits);
|
|
116
|
+
const typeIndex = bits.reduce((acc, bit, i) => acc | (bit ? 1 << i : 0), 0);
|
|
117
|
+
let selectedDef = def.types[typeIndex];
|
|
118
|
+
if (typeof selectedDef === 'string')
|
|
119
|
+
selectedDef = { type: selectedDef };
|
|
120
|
+
return this.readSingleType(selectedDef);
|
|
121
|
+
}
|
|
122
|
+
readArray(itemDef) {
|
|
123
|
+
const length = this.readInt32();
|
|
124
|
+
const items = [];
|
|
125
|
+
for (let i = 0; i < length; i++) {
|
|
126
|
+
const item = 'types' in itemDef ? this.readMultiType(itemDef) : this.readSingleType(itemDef);
|
|
127
|
+
items.push(item);
|
|
128
|
+
}
|
|
129
|
+
return items;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.DataStreamReader = DataStreamReader;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { BitStreamWriter } from "./bit-stream-writer";
|
|
2
|
+
import { DataType, DataTypeValue, DataValue, MultiType, NonOptionalSingleType, SchemaBase, SingleType } from "./schema-structure";
|
|
3
|
+
export declare class DataStreamWriter extends BitStreamWriter {
|
|
4
|
+
protected readonly schema: SchemaBase;
|
|
5
|
+
protected readonly schemaString: string;
|
|
6
|
+
constructor(schema: SchemaBase, includeSchemaInData: boolean, metaData?: string);
|
|
7
|
+
static compile(schema: SchemaBase): string;
|
|
8
|
+
writeCompiled(compiledFn: (writer: DataStreamWriter, data: any) => void, data: any): void;
|
|
9
|
+
intToBits(int: number, bitCount: number): (0 | 1)[];
|
|
10
|
+
protected writeArray(value: DataTypeValue[], def: {
|
|
11
|
+
type: "array";
|
|
12
|
+
items: DataType;
|
|
13
|
+
optional?: boolean;
|
|
14
|
+
} | ({
|
|
15
|
+
type: "array";
|
|
16
|
+
items: DataType;
|
|
17
|
+
})): void;
|
|
18
|
+
protected writeSingleType(value: DataTypeValue, def: NonOptionalSingleType | SingleType): void;
|
|
19
|
+
protected writeMultiType(value: DataTypeValue, def: MultiType): void;
|
|
20
|
+
protected writeObject(data: DataValue, schema: SchemaBase): void;
|
|
21
|
+
write<D extends DataValue>(data: D): void;
|
|
22
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DataStreamWriter = void 0;
|
|
4
|
+
const bit_stream_writer_1 = require("./bit-stream-writer");
|
|
5
|
+
const schema_structure_1 = require("./schema-structure");
|
|
6
|
+
const compiler_old_1 = require("./compiler-old");
|
|
7
|
+
// int8
|
|
8
|
+
const INT8_MIN = Math.floor(-0xFF / 2);
|
|
9
|
+
const INT8_MAX = Math.floor(0xFF / 2);
|
|
10
|
+
// int16
|
|
11
|
+
const INT16_MIN = Math.floor(-0xFFFF / 2);
|
|
12
|
+
const INT16_MAX = Math.floor(0xFFFF / 2);
|
|
13
|
+
// int32
|
|
14
|
+
const INT32_MIN = Math.floor(-0xFFFFFFFF / 2);
|
|
15
|
+
const INT32_MAX = Math.floor(0xFFFFFFFF / 2);
|
|
16
|
+
class DataStreamWriter extends bit_stream_writer_1.BitStreamWriter {
|
|
17
|
+
constructor(schema, includeSchemaInData, metaData = '') {
|
|
18
|
+
super();
|
|
19
|
+
this.schemaString = (0, schema_structure_1.encodeSchema)(schema);
|
|
20
|
+
this.writeString(metaData);
|
|
21
|
+
if (includeSchemaInData)
|
|
22
|
+
this.writeString(this.schemaString);
|
|
23
|
+
else
|
|
24
|
+
this.writeString('');
|
|
25
|
+
this.schema = (0, schema_structure_1.decodeSchemaString)(this.schemaString)[0];
|
|
26
|
+
}
|
|
27
|
+
static compile(schema) {
|
|
28
|
+
return (0, compiler_old_1.compileWriter)(schema);
|
|
29
|
+
}
|
|
30
|
+
writeCompiled(compiledFn, data) {
|
|
31
|
+
compiledFn(this, data);
|
|
32
|
+
}
|
|
33
|
+
intToBits(int, bitCount) {
|
|
34
|
+
// TODO: in stead directly write in loop
|
|
35
|
+
const bits = [];
|
|
36
|
+
for (let i = 0; i < bitCount; i++) {
|
|
37
|
+
bits.push((int & (1 << i)) !== 0 ? 1 : 0);
|
|
38
|
+
}
|
|
39
|
+
return bits;
|
|
40
|
+
}
|
|
41
|
+
writeArray(value, def) {
|
|
42
|
+
const itemDef = def.items;
|
|
43
|
+
this.writeInt32(value.length);
|
|
44
|
+
if ('types' in itemDef) {
|
|
45
|
+
for (let i = 0; i < value.length; i++)
|
|
46
|
+
this.writeMultiType(value[i], itemDef);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
for (let i = 0; i < value.length; i++)
|
|
50
|
+
this.writeSingleType(value[i], itemDef);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
writeSingleType(value, def) {
|
|
54
|
+
if (def.type === 'null' && value === null)
|
|
55
|
+
return;
|
|
56
|
+
if (def.type === 'boolean' && typeof value === 'boolean')
|
|
57
|
+
return this.writeBit(value ? 1 : 0);
|
|
58
|
+
if (def.type === 'string' && typeof value === 'string')
|
|
59
|
+
return this.writeString(value);
|
|
60
|
+
if (def.type === 'int-8' && typeof value === 'number')
|
|
61
|
+
return this.writeInt8(value);
|
|
62
|
+
if (def.type === 'int-16' && typeof value === 'number')
|
|
63
|
+
return this.writeInt16(value);
|
|
64
|
+
if (def.type === 'int-32' && typeof value === 'number')
|
|
65
|
+
return this.writeInt32(value);
|
|
66
|
+
if (def.type === 'bigint' && typeof value === 'bigint')
|
|
67
|
+
return this.writeBigInt(value);
|
|
68
|
+
if (def.type === 'float-32' && typeof value === 'number')
|
|
69
|
+
return this.writeFloat32(value);
|
|
70
|
+
if (def.type === 'float-64' && typeof value === 'number')
|
|
71
|
+
return this.writeFloat64(value);
|
|
72
|
+
if (def.type === 'array' && value !== null && typeof value === 'object' && Array.isArray(value))
|
|
73
|
+
return this.writeArray(value, def);
|
|
74
|
+
if (def.type === 'object' && value !== null && typeof value === 'object' && !Array.isArray(value))
|
|
75
|
+
return this.writeObject(value, def.items);
|
|
76
|
+
throw new Error(`Invalid type: ${def.type}, Value: ${value}`);
|
|
77
|
+
}
|
|
78
|
+
writeMultiType(value, def) {
|
|
79
|
+
let matchedTypes = [];
|
|
80
|
+
const noOfDefs = def.types.length;
|
|
81
|
+
const allTypes = def.types.map(t => typeof t === 'string' ? { type: t } : t);
|
|
82
|
+
// calculate min bits required to store type index
|
|
83
|
+
const minBits = Math.ceil(Math.log2(noOfDefs));
|
|
84
|
+
for (let i = 0; i < noOfDefs; i++) {
|
|
85
|
+
let typeDef = allTypes[i];
|
|
86
|
+
if (typeDef.type === 'null' && value === null)
|
|
87
|
+
matchedTypes.push(i);
|
|
88
|
+
else if (typeDef.type === 'boolean' && typeof value === 'boolean')
|
|
89
|
+
matchedTypes.push(i);
|
|
90
|
+
else if (typeDef.type === 'string' && typeof value === 'string')
|
|
91
|
+
matchedTypes.push(i);
|
|
92
|
+
else if (typeDef.type === 'int-8' && typeof value === 'number' && value % 1 === 0 && value >= INT8_MIN && value <= INT8_MAX)
|
|
93
|
+
matchedTypes.push(i);
|
|
94
|
+
else if (typeDef.type === 'int-16' && typeof value === 'number' && value % 1 === 0 && value >= INT16_MIN && value <= INT16_MAX)
|
|
95
|
+
matchedTypes.push(i);
|
|
96
|
+
else if (typeDef.type === 'int-32' && typeof value === 'number' && value % 1 === 0 && value >= INT32_MIN && value <= INT32_MAX)
|
|
97
|
+
matchedTypes.push(i);
|
|
98
|
+
else if (typeDef.type === 'bigint' && typeof value === 'bigint')
|
|
99
|
+
matchedTypes.push(i);
|
|
100
|
+
else if (typeDef.type === 'float-32' && typeof value === 'number' && Math.abs(value) <= 3.4028235e38)
|
|
101
|
+
matchedTypes.push(i);
|
|
102
|
+
else if (typeDef.type === 'float-64' && typeof value === 'number')
|
|
103
|
+
matchedTypes.push(i);
|
|
104
|
+
else if (typeDef.type === 'array' && value !== null && typeof value === 'object' && Array.isArray(value))
|
|
105
|
+
matchedTypes.push(i);
|
|
106
|
+
else if (typeDef.type === 'object' && value !== null && typeof value === 'object') {
|
|
107
|
+
const allKeys = Object.keys(typeDef.items);
|
|
108
|
+
const optionalKeys = allKeys.filter(key => {
|
|
109
|
+
const itemDef = typeDef.items[key];
|
|
110
|
+
return ('optional' in itemDef && itemDef.optional) || ('types' in itemDef &&
|
|
111
|
+
typeof itemDef.types[0] === 'object' && 'optional' in itemDef.types[0] && itemDef.types[0].optional);
|
|
112
|
+
});
|
|
113
|
+
const requiredKeys = allKeys.filter(key => {
|
|
114
|
+
const itemDef = typeDef.items[key];
|
|
115
|
+
return !(('optional' in itemDef && itemDef.optional) || ('types' in itemDef &&
|
|
116
|
+
typeof itemDef.types[0] === 'object' && 'optional' in itemDef.types[0] && itemDef.types[0].optional));
|
|
117
|
+
});
|
|
118
|
+
const valueKeys = Object.keys(value);
|
|
119
|
+
// 1. has all required keys
|
|
120
|
+
if (!requiredKeys.every(key => valueKeys.includes(key)))
|
|
121
|
+
continue;
|
|
122
|
+
const remainingValueKeys = valueKeys.filter(key => !requiredKeys.includes(key));
|
|
123
|
+
// 2. The remaining keys are in optional (no extra key allowed)
|
|
124
|
+
if (!remainingValueKeys.every(key => optionalKeys.includes(key)))
|
|
125
|
+
continue;
|
|
126
|
+
matchedTypes.push(i);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (matchedTypes.length > 1) {
|
|
130
|
+
if (typeof value === "number") {
|
|
131
|
+
// use the least number of bytes when available
|
|
132
|
+
if (matchedTypes.some(i => allTypes[i].type === 'int-8'))
|
|
133
|
+
matchedTypes = matchedTypes.filter(i => allTypes[i].type === 'int-8');
|
|
134
|
+
else if (matchedTypes.some(i => allTypes[i].type === 'int-16'))
|
|
135
|
+
matchedTypes = matchedTypes.filter(i => allTypes[i].type === 'int-16');
|
|
136
|
+
else if (matchedTypes.some(i => allTypes[i].type === 'int-32'))
|
|
137
|
+
matchedTypes = matchedTypes.filter(i => allTypes[i].type === 'int-32');
|
|
138
|
+
else if (matchedTypes.some(i => allTypes[i].type === 'float-32'))
|
|
139
|
+
matchedTypes = matchedTypes.filter(i => allTypes[i].type === 'float-32');
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (matchedTypes.length === 0)
|
|
143
|
+
throw new Error(`No matching type found for value: ${value}, possibles: ${allTypes.map(a => a.type)}`);
|
|
144
|
+
if (matchedTypes.length > 1)
|
|
145
|
+
throw new Error(`Multiple matching types found for value: ${value}, types: ${matchedTypes.map(i => {
|
|
146
|
+
return allTypes[i].type;
|
|
147
|
+
}).join(', ')}`);
|
|
148
|
+
this.writeBits(this.intToBits(matchedTypes[0], minBits));
|
|
149
|
+
let selectedDef = allTypes[matchedTypes[0]];
|
|
150
|
+
return this.writeSingleType(value, selectedDef);
|
|
151
|
+
}
|
|
152
|
+
writeObject(data, schema) {
|
|
153
|
+
const keys = Object.keys(schema);
|
|
154
|
+
for (const key of keys) {
|
|
155
|
+
let def = schema[key];
|
|
156
|
+
const isOptional = ('optional' in def && def.optional) || ('types' in def &&
|
|
157
|
+
typeof def.types[0] === 'object' && 'optional' in def.types[0] && def.types[0].optional);
|
|
158
|
+
if (!(key in data)) {
|
|
159
|
+
if (isOptional) {
|
|
160
|
+
this.writeBit(0);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
throw new Error(`Missing key: ${key}`);
|
|
164
|
+
}
|
|
165
|
+
if (isOptional) {
|
|
166
|
+
this.writeBit(1);
|
|
167
|
+
}
|
|
168
|
+
const value = data[key];
|
|
169
|
+
if ('types' in def)
|
|
170
|
+
this.writeMultiType(value, def);
|
|
171
|
+
else
|
|
172
|
+
this.writeSingleType(value, def);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
write(data) {
|
|
176
|
+
this.writeObject(data, this.schema);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
exports.DataStreamWriter = DataStreamWriter;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./schema-structure"), exports);
|
|
18
|
+
__exportStar(require("./data-stream-writer"), exports);
|
|
19
|
+
__exportStar(require("./data-stream-reader"), exports);
|
|
20
|
+
__exportStar(require("./bit-stream-reader"), exports);
|
|
21
|
+
__exportStar(require("./bit-stream-writer"), exports);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
type CreateSingleType<T extends string, V> = ({
|
|
2
|
+
optional: true;
|
|
3
|
+
default?: V;
|
|
4
|
+
} | {
|
|
5
|
+
optional?: false;
|
|
6
|
+
default?: never;
|
|
7
|
+
}) & ({
|
|
8
|
+
type: T;
|
|
9
|
+
});
|
|
10
|
+
type AllSingleTypes = CreateSingleType<'string', string> | CreateSingleType<`int-${8 | 16 | 32}`, number> | CreateSingleType<'bigint', bigint> | CreateSingleType<`float-${32 | 64}`, number> | CreateSingleType<'boolean', boolean>;
|
|
11
|
+
export type SingleType = AllSingleTypes | ({
|
|
12
|
+
type: 'null';
|
|
13
|
+
optional?: boolean;
|
|
14
|
+
default?: never;
|
|
15
|
+
}) | ({
|
|
16
|
+
type: 'object';
|
|
17
|
+
items: SchemaBase;
|
|
18
|
+
optional?: boolean;
|
|
19
|
+
}) | ({
|
|
20
|
+
type: 'array';
|
|
21
|
+
items: DataType;
|
|
22
|
+
optional?: boolean;
|
|
23
|
+
});
|
|
24
|
+
export type AllTypes = 'null' | 'string' | `int-${8 | 16 | 32}` | `float-${32 | 64}` | 'boolean' | 'bigint' | 'object' | 'array';
|
|
25
|
+
export type SimpleTypes = 'null' | 'string' | `int-${8 | 16 | 32}` | `float-${32 | 64}` | 'boolean' | 'bigint';
|
|
26
|
+
export type NonOptionalSingleType = ({
|
|
27
|
+
type: 'null';
|
|
28
|
+
} | {
|
|
29
|
+
type: 'string' | `int-${8 | 16 | 32}` | `float-${32 | 64}` | 'boolean' | 'bigint';
|
|
30
|
+
} | {
|
|
31
|
+
type: 'object';
|
|
32
|
+
items: SchemaBase;
|
|
33
|
+
} | {
|
|
34
|
+
type: 'array';
|
|
35
|
+
items: DataType;
|
|
36
|
+
}) & ({
|
|
37
|
+
optional?: never;
|
|
38
|
+
default?: never;
|
|
39
|
+
});
|
|
40
|
+
export type MultiType = {
|
|
41
|
+
types: [SingleType, ...Array<NonOptionalSingleType>] | [SimpleTypes, ...SimpleTypes[]];
|
|
42
|
+
};
|
|
43
|
+
export type DataType = SingleType | MultiType;
|
|
44
|
+
export type SchemaBase = Record<string, DataType>;
|
|
45
|
+
export type DataTypeValue = string | number | bigint | boolean | null | {
|
|
46
|
+
[key: string]: DataTypeValue;
|
|
47
|
+
} | DataTypeValue[];
|
|
48
|
+
export type DataValue = {
|
|
49
|
+
[key: string]: DataTypeValue;
|
|
50
|
+
};
|
|
51
|
+
export declare const createSchema: <S extends SchemaBase>(schema: S) => S;
|
|
52
|
+
export declare const encodeSchema: <S extends SchemaBase>(schema: S) => string;
|
|
53
|
+
export declare const decodeSchemaString: <S extends SchemaBase = SchemaBase>(schemaString: string) => [schema: S, metadata: string];
|
|
54
|
+
export {};
|