spacetimedb 0.0.1
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 +61 -0
- package/src/algebraic_type.ts +460 -0
- package/src/algebraic_value.ts +10 -0
- package/src/autogen/algebraic_type_type.ts +176 -0
- package/src/autogen/algebraic_type_variants.ts +47 -0
- package/src/autogen/index_type_type.ts +65 -0
- package/src/autogen/index_type_variants.ts +20 -0
- package/src/autogen/lifecycle_type.ts +73 -0
- package/src/autogen/lifecycle_variants.ts +21 -0
- package/src/autogen/misc_module_export_type.ts +67 -0
- package/src/autogen/misc_module_export_variants.ts +22 -0
- package/src/autogen/product_type_element_type.ts +67 -0
- package/src/autogen/product_type_type.ts +62 -0
- package/src/autogen/raw_column_def_v_8_type.ts +62 -0
- package/src/autogen/raw_column_default_value_v_9_type.ts +61 -0
- package/src/autogen/raw_constraint_data_v_9_type.ts +67 -0
- package/src/autogen/raw_constraint_data_v_9_variants.ts +23 -0
- package/src/autogen/raw_constraint_def_v_8_type.ts +61 -0
- package/src/autogen/raw_constraint_def_v_9_type.ts +67 -0
- package/src/autogen/raw_index_algorithm_type.ts +70 -0
- package/src/autogen/raw_index_algorithm_variants.ts +21 -0
- package/src/autogen/raw_index_def_v_8_type.ts +69 -0
- package/src/autogen/raw_index_def_v_9_type.ts +74 -0
- package/src/autogen/raw_misc_module_export_v_9_type.ts +67 -0
- package/src/autogen/raw_misc_module_export_v_9_variants.ts +25 -0
- package/src/autogen/raw_module_def_type.ts +77 -0
- package/src/autogen/raw_module_def_v_8_type.ts +90 -0
- package/src/autogen/raw_module_def_v_9_type.ts +110 -0
- package/src/autogen/raw_module_def_variants.ts +26 -0
- package/src/autogen/raw_reducer_def_v_9_type.ts +72 -0
- package/src/autogen/raw_row_level_security_def_v_9_type.ts +52 -0
- package/src/autogen/raw_schedule_def_v_9_type.ts +63 -0
- package/src/autogen/raw_scoped_type_name_v_9_type.ts +61 -0
- package/src/autogen/raw_sequence_def_v_8_type.ts +81 -0
- package/src/autogen/raw_sequence_def_v_9_type.ts +84 -0
- package/src/autogen/raw_table_def_v_8_type.ts +105 -0
- package/src/autogen/raw_table_def_v_9_type.ts +117 -0
- package/src/autogen/raw_type_def_v_9_type.ts +64 -0
- package/src/autogen/raw_unique_constraint_data_v_9_type.ts +57 -0
- package/src/autogen/reducer_def_type.ts +64 -0
- package/src/autogen/sum_type_type.ts +62 -0
- package/src/autogen/sum_type_variant_type.ts +67 -0
- package/src/autogen/table_access_type.ts +67 -0
- package/src/autogen/table_access_variants.ts +20 -0
- package/src/autogen/table_desc_type.ts +62 -0
- package/src/autogen/table_type_type.ts +65 -0
- package/src/autogen/table_type_variants.ts +20 -0
- package/src/autogen/type_alias_type.ts +56 -0
- package/src/autogen/typespace_type.ts +62 -0
- package/src/binary_reader.ts +167 -0
- package/src/binary_writer.ts +169 -0
- package/src/connection_id.ts +88 -0
- package/src/identity.ts +58 -0
- package/src/index.ts +11 -0
- package/src/schedule_at.ts +43 -0
- package/src/server/type_builders.test-d.ts +112 -0
- package/src/server/type_builders.ts +1851 -0
- package/src/server/type_util.ts +24 -0
- package/src/time_duration.ts +41 -0
- package/src/timestamp.ts +74 -0
- package/src/utils.ts +103 -0
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "spacetimedb",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "API and ABI bindings for the SpacetimeDB TypeScript module library",
|
|
5
|
+
"homepage": "https://github.com/clockworklabs/SpacetimeDB#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/clockworklabs/SpacetimeDB/issues"
|
|
8
|
+
},
|
|
9
|
+
"source": "src/index.ts",
|
|
10
|
+
"main": "dist/index.cjs",
|
|
11
|
+
"module": "dist/index.mjs",
|
|
12
|
+
"types": "src/index.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./src/index.ts",
|
|
16
|
+
"source": "./src/index.ts",
|
|
17
|
+
"development": "./src/index.ts",
|
|
18
|
+
"import": "./dist/index.mjs",
|
|
19
|
+
"require": "./dist/index.cjs",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"src",
|
|
25
|
+
"dist",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE.txt"
|
|
28
|
+
],
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/clockworklabs/SpacetimeDB.git"
|
|
32
|
+
},
|
|
33
|
+
"license": "ISC",
|
|
34
|
+
"author": "Clockwork Labs",
|
|
35
|
+
"type": "module",
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc",
|
|
38
|
+
"format": "prettier --write .",
|
|
39
|
+
"lint": "eslint . && prettier . --check",
|
|
40
|
+
"test": "vitest run",
|
|
41
|
+
"coverage": "vitest run --coverage",
|
|
42
|
+
"generate": "cargo run -p spacetimedb-codegen --example regen-typescript-moduledef && prettier --write src/autogen"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@zxing/text-encoding": "^0.9.0",
|
|
46
|
+
"base64-js": "^1.5.1",
|
|
47
|
+
"prettier": "^3.3.3"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@eslint/js": "^9.17.0",
|
|
51
|
+
"@typescript-eslint/eslint-plugin": "^8.18.2",
|
|
52
|
+
"@typescript-eslint/parser": "^8.18.2",
|
|
53
|
+
"eslint": "^9.33.0",
|
|
54
|
+
"globals": "^15.14.0",
|
|
55
|
+
"ts-node": "^10.9.2",
|
|
56
|
+
"typescript": "^5.9.2",
|
|
57
|
+
"typescript-eslint": "^8.18.2",
|
|
58
|
+
"vite": "^7.1.3",
|
|
59
|
+
"vitest": "^3.2.4"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
import { TimeDuration } from './time_duration';
|
|
2
|
+
import { Timestamp } from './timestamp';
|
|
3
|
+
import { ConnectionId } from './connection_id';
|
|
4
|
+
import type BinaryReader from './binary_reader';
|
|
5
|
+
import BinaryWriter from './binary_writer';
|
|
6
|
+
import { Identity } from './identity';
|
|
7
|
+
import {
|
|
8
|
+
AlgebraicType as AlgebraicTypeType,
|
|
9
|
+
AlgebraicType as AlgebraicTypeValue,
|
|
10
|
+
} from './autogen/algebraic_type_type';
|
|
11
|
+
import {
|
|
12
|
+
type ProductType as ProductTypeType,
|
|
13
|
+
ProductType as ProductTypeValue,
|
|
14
|
+
} from './autogen/product_type_type';
|
|
15
|
+
import {
|
|
16
|
+
type SumType as SumTypeType,
|
|
17
|
+
SumType as SumTypeValue,
|
|
18
|
+
} from './autogen/sum_type_type';
|
|
19
|
+
import ScheduleAt from './schedule_at';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A factor / element of a product type.
|
|
23
|
+
*
|
|
24
|
+
* An element consist of an optional name and a type.
|
|
25
|
+
*
|
|
26
|
+
* NOTE: Each element has an implicit element tag based on its order.
|
|
27
|
+
* Uniquely identifies an element similarly to protobuf tags.
|
|
28
|
+
*/
|
|
29
|
+
export * from './autogen/product_type_element_type';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A variant of a sum type.
|
|
33
|
+
*
|
|
34
|
+
* NOTE: Each element has an implicit element tag based on its order.
|
|
35
|
+
* Uniquely identifies an element similarly to protobuf tags.
|
|
36
|
+
*/
|
|
37
|
+
export * from './autogen/sum_type_variant_type';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The variant types of the Algebraic Type tagged union.
|
|
41
|
+
*/
|
|
42
|
+
export type * as AlgebraicTypeVariants from './autogen/algebraic_type_variants';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The SpacetimeDB Algebraic Type System (SATS) is a structural type system in
|
|
46
|
+
* which a nominal type system can be constructed.
|
|
47
|
+
*
|
|
48
|
+
* The type system unifies the concepts sum types, product types, and built-in
|
|
49
|
+
* primitive types into a single type system.
|
|
50
|
+
*/
|
|
51
|
+
export type AlgebraicType = AlgebraicTypeType;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Algebraic Type utilities.
|
|
55
|
+
*/
|
|
56
|
+
export const AlgebraicType: {
|
|
57
|
+
createOptionType(innerType: AlgebraicTypeType): AlgebraicTypeType;
|
|
58
|
+
createIdentityType(): AlgebraicTypeType;
|
|
59
|
+
createConnectionIdType(): AlgebraicTypeType;
|
|
60
|
+
createScheduleAtType(): AlgebraicTypeType;
|
|
61
|
+
createTimestampType(): AlgebraicTypeType;
|
|
62
|
+
createTimeDurationType(): AlgebraicTypeType;
|
|
63
|
+
serializeValue(writer: BinaryWriter, ty: AlgebraicTypeType, value: any): void;
|
|
64
|
+
deserializeValue(reader: BinaryReader, ty: AlgebraicTypeType): any;
|
|
65
|
+
/**
|
|
66
|
+
* Convert a value of the algebraic type into something that can be used as a key in a map.
|
|
67
|
+
* There are no guarantees about being able to order it.
|
|
68
|
+
* This is only guaranteed to be comparable to other values of the same type.
|
|
69
|
+
* @param value A value of the algebraic type
|
|
70
|
+
* @returns Something that can be used as a key in a map.
|
|
71
|
+
*/
|
|
72
|
+
intoMapKey(ty: AlgebraicTypeType, value: any): ComparablePrimitive;
|
|
73
|
+
} & typeof AlgebraicTypeValue = {
|
|
74
|
+
...AlgebraicTypeValue,
|
|
75
|
+
createOptionType: function (innerType: AlgebraicTypeType): AlgebraicTypeType {
|
|
76
|
+
return AlgebraicTypeValue.Sum({
|
|
77
|
+
variants: [
|
|
78
|
+
{ name: 'some', algebraicType: innerType },
|
|
79
|
+
{
|
|
80
|
+
name: 'none',
|
|
81
|
+
algebraicType: AlgebraicTypeValue.Product({ elements: [] }),
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
createIdentityType: function (): AlgebraicTypeType {
|
|
87
|
+
return Identity.getAlgebraicType();
|
|
88
|
+
},
|
|
89
|
+
createConnectionIdType: function (): AlgebraicTypeType {
|
|
90
|
+
return ConnectionId.getAlgebraicType();
|
|
91
|
+
},
|
|
92
|
+
createScheduleAtType: function (): AlgebraicTypeType {
|
|
93
|
+
return ScheduleAt.getAlgebraicType();
|
|
94
|
+
},
|
|
95
|
+
createTimestampType: function (): AlgebraicTypeType {
|
|
96
|
+
return Timestamp.getAlgebraicType();
|
|
97
|
+
},
|
|
98
|
+
createTimeDurationType: function (): AlgebraicTypeType {
|
|
99
|
+
return TimeDuration.getAlgebraicType();
|
|
100
|
+
},
|
|
101
|
+
serializeValue: function (
|
|
102
|
+
writer: BinaryWriter,
|
|
103
|
+
ty: AlgebraicTypeType,
|
|
104
|
+
value: any
|
|
105
|
+
): void {
|
|
106
|
+
switch (ty.tag) {
|
|
107
|
+
case 'Product':
|
|
108
|
+
ProductType.serializeValue(writer, ty.value, value);
|
|
109
|
+
break;
|
|
110
|
+
case 'Sum':
|
|
111
|
+
SumType.serializeValue(writer, ty.value, value);
|
|
112
|
+
break;
|
|
113
|
+
case 'Array':
|
|
114
|
+
if (ty.value.tag === 'U8') {
|
|
115
|
+
writer.writeUInt8Array(value);
|
|
116
|
+
} else {
|
|
117
|
+
const elemType = ty.value;
|
|
118
|
+
writer.writeU32(value.length);
|
|
119
|
+
for (const elem of value) {
|
|
120
|
+
AlgebraicType.serializeValue(writer, elemType, elem);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
break;
|
|
124
|
+
case 'Bool':
|
|
125
|
+
writer.writeBool(value);
|
|
126
|
+
break;
|
|
127
|
+
case 'I8':
|
|
128
|
+
writer.writeI8(value);
|
|
129
|
+
break;
|
|
130
|
+
case 'U8':
|
|
131
|
+
writer.writeU8(value);
|
|
132
|
+
break;
|
|
133
|
+
case 'I16':
|
|
134
|
+
writer.writeI16(value);
|
|
135
|
+
break;
|
|
136
|
+
case 'U16':
|
|
137
|
+
writer.writeU16(value);
|
|
138
|
+
break;
|
|
139
|
+
case 'I32':
|
|
140
|
+
writer.writeI32(value);
|
|
141
|
+
break;
|
|
142
|
+
case 'U32':
|
|
143
|
+
writer.writeU32(value);
|
|
144
|
+
break;
|
|
145
|
+
case 'I64':
|
|
146
|
+
writer.writeI64(value);
|
|
147
|
+
break;
|
|
148
|
+
case 'U64':
|
|
149
|
+
writer.writeU64(value);
|
|
150
|
+
break;
|
|
151
|
+
case 'I128':
|
|
152
|
+
writer.writeI128(value);
|
|
153
|
+
break;
|
|
154
|
+
case 'U128':
|
|
155
|
+
writer.writeU128(value);
|
|
156
|
+
break;
|
|
157
|
+
case 'I256':
|
|
158
|
+
writer.writeI256(value);
|
|
159
|
+
break;
|
|
160
|
+
case 'U256':
|
|
161
|
+
writer.writeU256(value);
|
|
162
|
+
break;
|
|
163
|
+
case 'F32':
|
|
164
|
+
writer.writeF32(value);
|
|
165
|
+
break;
|
|
166
|
+
case 'F64':
|
|
167
|
+
writer.writeF64(value);
|
|
168
|
+
break;
|
|
169
|
+
case 'String':
|
|
170
|
+
writer.writeString(value);
|
|
171
|
+
break;
|
|
172
|
+
default:
|
|
173
|
+
throw new Error(`not implemented, ${ty.tag}`);
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
deserializeValue: function (
|
|
177
|
+
reader: BinaryReader,
|
|
178
|
+
ty: AlgebraicTypeType
|
|
179
|
+
): any {
|
|
180
|
+
switch (ty.tag) {
|
|
181
|
+
case 'Product':
|
|
182
|
+
return ProductType.deserializeValue(reader, ty.value);
|
|
183
|
+
case 'Sum':
|
|
184
|
+
return SumType.deserializeValue(reader, ty.value);
|
|
185
|
+
case 'Array':
|
|
186
|
+
if (ty.value.tag === 'U8') {
|
|
187
|
+
return reader.readUInt8Array();
|
|
188
|
+
} else {
|
|
189
|
+
const elemType = ty.value;
|
|
190
|
+
const length = reader.readU32();
|
|
191
|
+
const result: any[] = [];
|
|
192
|
+
for (let i = 0; i < length; i++) {
|
|
193
|
+
result.push(AlgebraicType.deserializeValue(reader, elemType));
|
|
194
|
+
}
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
case 'Bool':
|
|
198
|
+
return reader.readBool();
|
|
199
|
+
case 'I8':
|
|
200
|
+
return reader.readI8();
|
|
201
|
+
case 'U8':
|
|
202
|
+
return reader.readU8();
|
|
203
|
+
case 'I16':
|
|
204
|
+
return reader.readI16();
|
|
205
|
+
case 'U16':
|
|
206
|
+
return reader.readU16();
|
|
207
|
+
case 'I32':
|
|
208
|
+
return reader.readI32();
|
|
209
|
+
case 'U32':
|
|
210
|
+
return reader.readU32();
|
|
211
|
+
case 'I64':
|
|
212
|
+
return reader.readI64();
|
|
213
|
+
case 'U64':
|
|
214
|
+
return reader.readU64();
|
|
215
|
+
case 'I128':
|
|
216
|
+
return reader.readI128();
|
|
217
|
+
case 'U128':
|
|
218
|
+
return reader.readU128();
|
|
219
|
+
case 'I256':
|
|
220
|
+
return reader.readI256();
|
|
221
|
+
case 'U256':
|
|
222
|
+
return reader.readU256();
|
|
223
|
+
case 'F32':
|
|
224
|
+
return reader.readF32();
|
|
225
|
+
case 'F64':
|
|
226
|
+
return reader.readF64();
|
|
227
|
+
case 'String':
|
|
228
|
+
return reader.readString();
|
|
229
|
+
default:
|
|
230
|
+
throw new Error(`not implemented, ${ty.tag}`);
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
/**
|
|
234
|
+
* Convert a value of the algebraic type into something that can be used as a key in a map.
|
|
235
|
+
* There are no guarantees about being able to order it.
|
|
236
|
+
* This is only guaranteed to be comparable to other values of the same type.
|
|
237
|
+
* @param value A value of the algebraic type
|
|
238
|
+
* @returns Something that can be used as a key in a map.
|
|
239
|
+
*/
|
|
240
|
+
intoMapKey: function (
|
|
241
|
+
ty: AlgebraicTypeType,
|
|
242
|
+
value: any
|
|
243
|
+
): ComparablePrimitive {
|
|
244
|
+
switch (ty.tag) {
|
|
245
|
+
case 'U8':
|
|
246
|
+
case 'U16':
|
|
247
|
+
case 'U32':
|
|
248
|
+
case 'U64':
|
|
249
|
+
case 'U128':
|
|
250
|
+
case 'U256':
|
|
251
|
+
case 'I8':
|
|
252
|
+
case 'I16':
|
|
253
|
+
case 'I64':
|
|
254
|
+
case 'I128':
|
|
255
|
+
case 'F32':
|
|
256
|
+
case 'F64':
|
|
257
|
+
case 'String':
|
|
258
|
+
case 'Bool':
|
|
259
|
+
return value;
|
|
260
|
+
case 'Product':
|
|
261
|
+
return ProductType.intoMapKey(ty.value, value);
|
|
262
|
+
default: {
|
|
263
|
+
const writer = new BinaryWriter(10);
|
|
264
|
+
this.serialize(writer, value);
|
|
265
|
+
return writer.toBase64();
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* A structural product type of the factors given by `elements`.
|
|
273
|
+
*
|
|
274
|
+
* This is also known as `struct` and `tuple` in many languages,
|
|
275
|
+
* but note that unlike most languages, products in SATs are *[structural]* and not nominal.
|
|
276
|
+
* When checking whether two nominal types are the same,
|
|
277
|
+
* their names and/or declaration sites (e.g., module / namespace) are considered.
|
|
278
|
+
* Meanwhile, a structural type system would only check the structure of the type itself,
|
|
279
|
+
* e.g., the names of its fields and their types in the case of a record.
|
|
280
|
+
* The name "product" comes from category theory.
|
|
281
|
+
*
|
|
282
|
+
* See also: https://ncatlab.org/nlab/show/product+type.
|
|
283
|
+
*
|
|
284
|
+
* These structures are known as product types because the number of possible values in product
|
|
285
|
+
* ```ignore
|
|
286
|
+
* { N_0: T_0, N_1: T_1, ..., N_n: T_n }
|
|
287
|
+
* ```
|
|
288
|
+
* is:
|
|
289
|
+
* ```ignore
|
|
290
|
+
* Π (i ∈ 0..n). values(T_i)
|
|
291
|
+
* ```
|
|
292
|
+
* so for example, `values({ A: U64, B: Bool }) = values(U64) * values(Bool)`.
|
|
293
|
+
*
|
|
294
|
+
* [structural]: https://en.wikipedia.org/wiki/Structural_type_system
|
|
295
|
+
*/
|
|
296
|
+
export type ProductType = ProductTypeType;
|
|
297
|
+
|
|
298
|
+
export const ProductType: {
|
|
299
|
+
serializeValue(writer: BinaryWriter, ty: ProductTypeType, value: any): void;
|
|
300
|
+
deserializeValue(reader: BinaryReader, ty: ProductTypeType): any;
|
|
301
|
+
intoMapKey(ty: ProductTypeType, value: any): ComparablePrimitive;
|
|
302
|
+
} = {
|
|
303
|
+
...ProductTypeValue,
|
|
304
|
+
serializeValue(writer: BinaryWriter, ty: ProductTypeType, value: any): void {
|
|
305
|
+
for (const element of ty.elements) {
|
|
306
|
+
AlgebraicType.serializeValue(
|
|
307
|
+
writer,
|
|
308
|
+
element.algebraicType,
|
|
309
|
+
value[element.name!]
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
deserializeValue(reader: BinaryReader, ty: ProductTypeType): any {
|
|
314
|
+
const result: { [key: string]: any } = {};
|
|
315
|
+
if (ty.elements.length === 1) {
|
|
316
|
+
if (ty.elements[0].name === '__time_duration_micros__') {
|
|
317
|
+
return new TimeDuration(reader.readI64());
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (ty.elements[0].name === '__timestamp_micros_since_unix_epoch__') {
|
|
321
|
+
return new Timestamp(reader.readI64());
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (ty.elements[0].name === '__identity__') {
|
|
325
|
+
return new Identity(reader.readU256());
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (ty.elements[0].name === '__connection_id__') {
|
|
329
|
+
return new ConnectionId(reader.readU128());
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
for (const element of ty.elements) {
|
|
334
|
+
result[element.name!] = AlgebraicType.deserializeValue(
|
|
335
|
+
reader,
|
|
336
|
+
element.algebraicType
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
return result;
|
|
340
|
+
},
|
|
341
|
+
intoMapKey(ty: ProductTypeType, value: any): ComparablePrimitive {
|
|
342
|
+
if (ty.elements.length === 1) {
|
|
343
|
+
if (ty.elements[0].name === '__time_duration_micros__') {
|
|
344
|
+
return (value as TimeDuration).__time_duration_micros__;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (ty.elements[0].name === '__timestamp_micros_since_unix_epoch__') {
|
|
348
|
+
return (value as Timestamp).__timestamp_micros_since_unix_epoch__;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (ty.elements[0].name === '__identity__') {
|
|
352
|
+
return (value as Identity).__identity__;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (ty.elements[0].name === '__connection_id__') {
|
|
356
|
+
return (value as ConnectionId).__connection_id__;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
// The fallback is to serialize and base64 encode the bytes.
|
|
360
|
+
const writer = new BinaryWriter(10);
|
|
361
|
+
AlgebraicType.serializeValue(writer, AlgebraicType.Product(ty), value);
|
|
362
|
+
return writer.toBase64();
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Unlike most languages, sums in SATS are *[structural]* and not nominal.
|
|
368
|
+
* When checking whether two nominal types are the same,
|
|
369
|
+
* their names and/or declaration sites (e.g., module / namespace) are considered.
|
|
370
|
+
* Meanwhile, a structural type system would only check the structure of the type itself,
|
|
371
|
+
* e.g., the names of its variants and their inner data types in the case of a sum.
|
|
372
|
+
*
|
|
373
|
+
* This is also known as a discriminated union (implementation) or disjoint union.
|
|
374
|
+
* Another name is [coproduct (category theory)](https://ncatlab.org/nlab/show/coproduct).
|
|
375
|
+
*
|
|
376
|
+
* These structures are known as sum types because the number of possible values a sum
|
|
377
|
+
* ```ignore
|
|
378
|
+
* { N_0(T_0), N_1(T_1), ..., N_n(T_n) }
|
|
379
|
+
* ```
|
|
380
|
+
* is:
|
|
381
|
+
* ```ignore
|
|
382
|
+
* Σ (i ∈ 0..n). values(T_i)
|
|
383
|
+
* ```
|
|
384
|
+
* so for example, `values({ A(U64), B(Bool) }) = values(U64) + values(Bool)`.
|
|
385
|
+
*
|
|
386
|
+
* See also: https://ncatlab.org/nlab/show/sum+type.
|
|
387
|
+
*
|
|
388
|
+
* [structural]: https://en.wikipedia.org/wiki/Structural_type_system
|
|
389
|
+
*/
|
|
390
|
+
export const SumType: {
|
|
391
|
+
serializeValue(writer: BinaryWriter, ty: SumTypeType, value: any): void;
|
|
392
|
+
deserializeValue(reader: BinaryReader, ty: SumTypeType): any;
|
|
393
|
+
} = {
|
|
394
|
+
...SumTypeValue,
|
|
395
|
+
serializeValue: function (
|
|
396
|
+
writer: BinaryWriter,
|
|
397
|
+
ty: SumTypeType,
|
|
398
|
+
value: any
|
|
399
|
+
): void {
|
|
400
|
+
if (
|
|
401
|
+
ty.variants.length == 2 &&
|
|
402
|
+
ty.variants[0].name === 'some' &&
|
|
403
|
+
ty.variants[1].name === 'none'
|
|
404
|
+
) {
|
|
405
|
+
if (value !== null && value !== undefined) {
|
|
406
|
+
writer.writeByte(0);
|
|
407
|
+
AlgebraicType.serializeValue(
|
|
408
|
+
writer,
|
|
409
|
+
ty.variants[0].algebraicType,
|
|
410
|
+
value
|
|
411
|
+
);
|
|
412
|
+
} else {
|
|
413
|
+
writer.writeByte(1);
|
|
414
|
+
}
|
|
415
|
+
} else {
|
|
416
|
+
const variant = value['tag'];
|
|
417
|
+
const index = ty.variants.findIndex(v => v.name === variant);
|
|
418
|
+
if (index < 0) {
|
|
419
|
+
throw `Can't serialize a sum type, couldn't find ${value.tag} tag`;
|
|
420
|
+
}
|
|
421
|
+
writer.writeU8(index);
|
|
422
|
+
AlgebraicType.serializeValue(
|
|
423
|
+
writer,
|
|
424
|
+
ty.variants[index].algebraicType,
|
|
425
|
+
value['value']
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
},
|
|
429
|
+
deserializeValue: function (reader: BinaryReader, ty: SumTypeType): any {
|
|
430
|
+
const tag = reader.readU8();
|
|
431
|
+
// In TypeScript we handle Option values as a special case
|
|
432
|
+
// we don't represent the some and none variants, but instead
|
|
433
|
+
// we represent the value directly.
|
|
434
|
+
if (
|
|
435
|
+
ty.variants.length == 2 &&
|
|
436
|
+
ty.variants[0].name === 'some' &&
|
|
437
|
+
ty.variants[1].name === 'none'
|
|
438
|
+
) {
|
|
439
|
+
if (tag === 0) {
|
|
440
|
+
return AlgebraicType.deserializeValue(
|
|
441
|
+
reader,
|
|
442
|
+
ty.variants[0].algebraicType
|
|
443
|
+
);
|
|
444
|
+
} else if (tag === 1) {
|
|
445
|
+
return undefined;
|
|
446
|
+
} else {
|
|
447
|
+
throw `Can't deserialize an option type, couldn't find ${tag} tag`;
|
|
448
|
+
}
|
|
449
|
+
} else {
|
|
450
|
+
const variant = ty.variants[tag];
|
|
451
|
+
const value = AlgebraicType.deserializeValue(
|
|
452
|
+
reader,
|
|
453
|
+
variant.algebraicType
|
|
454
|
+
);
|
|
455
|
+
return { tag: variant.name, value };
|
|
456
|
+
}
|
|
457
|
+
},
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
export type ComparablePrimitive = number | string | boolean | bigint;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import BinaryReader from './binary_reader';
|
|
2
|
+
|
|
3
|
+
export interface ParseableType<T> {
|
|
4
|
+
deserialize: (reader: BinaryReader) => T;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function parseValue<T>(ty: ParseableType<T>, src: Uint8Array): T {
|
|
8
|
+
const reader = new BinaryReader(src);
|
|
9
|
+
return ty.deserialize(reader);
|
|
10
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
|
|
2
|
+
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
|
|
3
|
+
|
|
4
|
+
/* eslint-disable */
|
|
5
|
+
/* tslint:disable */
|
|
6
|
+
import {
|
|
7
|
+
AlgebraicType as __AlgebraicTypeValue,
|
|
8
|
+
BinaryReader as __BinaryReader,
|
|
9
|
+
BinaryWriter as __BinaryWriter,
|
|
10
|
+
ConnectionId as __ConnectionId,
|
|
11
|
+
Identity as __Identity,
|
|
12
|
+
TimeDuration as __TimeDuration,
|
|
13
|
+
Timestamp as __Timestamp,
|
|
14
|
+
deepEqual as __deepEqual,
|
|
15
|
+
type AlgebraicType as __AlgebraicTypeType,
|
|
16
|
+
type AlgebraicTypeVariants as __AlgebraicTypeVariants,
|
|
17
|
+
} from '../index';
|
|
18
|
+
import { SumType } from './sum_type_type';
|
|
19
|
+
// Mark import as potentially unused
|
|
20
|
+
declare type __keep_SumType = SumType;
|
|
21
|
+
import { ProductType } from './product_type_type';
|
|
22
|
+
// Mark import as potentially unused
|
|
23
|
+
declare type __keep_ProductType = ProductType;
|
|
24
|
+
|
|
25
|
+
import * as AlgebraicTypeVariants from './algebraic_type_variants';
|
|
26
|
+
|
|
27
|
+
// The tagged union or sum type for the algebraic type `AlgebraicType`.
|
|
28
|
+
export type AlgebraicType =
|
|
29
|
+
| AlgebraicTypeVariants.Ref
|
|
30
|
+
| AlgebraicTypeVariants.Sum
|
|
31
|
+
| AlgebraicTypeVariants.Product
|
|
32
|
+
| AlgebraicTypeVariants.Array
|
|
33
|
+
| AlgebraicTypeVariants.String
|
|
34
|
+
| AlgebraicTypeVariants.Bool
|
|
35
|
+
| AlgebraicTypeVariants.I8
|
|
36
|
+
| AlgebraicTypeVariants.U8
|
|
37
|
+
| AlgebraicTypeVariants.I16
|
|
38
|
+
| AlgebraicTypeVariants.U16
|
|
39
|
+
| AlgebraicTypeVariants.I32
|
|
40
|
+
| AlgebraicTypeVariants.U32
|
|
41
|
+
| AlgebraicTypeVariants.I64
|
|
42
|
+
| AlgebraicTypeVariants.U64
|
|
43
|
+
| AlgebraicTypeVariants.I128
|
|
44
|
+
| AlgebraicTypeVariants.U128
|
|
45
|
+
| AlgebraicTypeVariants.I256
|
|
46
|
+
| AlgebraicTypeVariants.U256
|
|
47
|
+
| AlgebraicTypeVariants.F32
|
|
48
|
+
| AlgebraicTypeVariants.F64;
|
|
49
|
+
|
|
50
|
+
// A value with helper functions to construct the type.
|
|
51
|
+
export const AlgebraicType = {
|
|
52
|
+
// Helper functions for constructing each variant of the tagged union.
|
|
53
|
+
// ```
|
|
54
|
+
// const foo = Foo.A(42);
|
|
55
|
+
// assert!(foo.tag === "A");
|
|
56
|
+
// assert!(foo.value === 42);
|
|
57
|
+
// ```
|
|
58
|
+
Ref: (value: number): AlgebraicType => ({ tag: 'Ref', value }),
|
|
59
|
+
Sum: (value: SumType): AlgebraicType => ({ tag: 'Sum', value }),
|
|
60
|
+
Product: (value: ProductType): AlgebraicType => ({ tag: 'Product', value }),
|
|
61
|
+
Array: (value: AlgebraicType): AlgebraicType => ({ tag: 'Array', value }),
|
|
62
|
+
String: { tag: 'String' } as const,
|
|
63
|
+
Bool: { tag: 'Bool' } as const,
|
|
64
|
+
I8: { tag: 'I8' } as const,
|
|
65
|
+
U8: { tag: 'U8' } as const,
|
|
66
|
+
I16: { tag: 'I16' } as const,
|
|
67
|
+
U16: { tag: 'U16' } as const,
|
|
68
|
+
I32: { tag: 'I32' } as const,
|
|
69
|
+
U32: { tag: 'U32' } as const,
|
|
70
|
+
I64: { tag: 'I64' } as const,
|
|
71
|
+
U64: { tag: 'U64' } as const,
|
|
72
|
+
I128: { tag: 'I128' } as const,
|
|
73
|
+
U128: { tag: 'U128' } as const,
|
|
74
|
+
I256: { tag: 'I256' } as const,
|
|
75
|
+
U256: { tag: 'U256' } as const,
|
|
76
|
+
F32: { tag: 'F32' } as const,
|
|
77
|
+
F64: { tag: 'F64' } as const,
|
|
78
|
+
|
|
79
|
+
getTypeScriptAlgebraicType(): __AlgebraicTypeType {
|
|
80
|
+
return __AlgebraicTypeValue.Sum({
|
|
81
|
+
variants: [
|
|
82
|
+
{ name: 'Ref', algebraicType: __AlgebraicTypeValue.U32 },
|
|
83
|
+
{ name: 'Sum', algebraicType: SumType.getTypeScriptAlgebraicType() },
|
|
84
|
+
{
|
|
85
|
+
name: 'Product',
|
|
86
|
+
algebraicType: ProductType.getTypeScriptAlgebraicType(),
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'Array',
|
|
90
|
+
algebraicType: AlgebraicType.getTypeScriptAlgebraicType(),
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
name: 'String',
|
|
94
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: 'Bool',
|
|
98
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
name: 'I8',
|
|
102
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
name: 'U8',
|
|
106
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: 'I16',
|
|
110
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
name: 'U16',
|
|
114
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: 'I32',
|
|
118
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
name: 'U32',
|
|
122
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: 'I64',
|
|
126
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
name: 'U64',
|
|
130
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
name: 'I128',
|
|
134
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
name: 'U128',
|
|
138
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
name: 'I256',
|
|
142
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
name: 'U256',
|
|
146
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
name: 'F32',
|
|
150
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
name: 'F64',
|
|
154
|
+
algebraicType: __AlgebraicTypeValue.Product({ elements: [] }),
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
});
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
serialize(writer: __BinaryWriter, value: AlgebraicType): void {
|
|
161
|
+
__AlgebraicTypeValue.serializeValue(
|
|
162
|
+
writer,
|
|
163
|
+
AlgebraicType.getTypeScriptAlgebraicType(),
|
|
164
|
+
value
|
|
165
|
+
);
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
deserialize(reader: __BinaryReader): AlgebraicType {
|
|
169
|
+
return __AlgebraicTypeValue.deserializeValue(
|
|
170
|
+
reader,
|
|
171
|
+
AlgebraicType.getTypeScriptAlgebraicType()
|
|
172
|
+
);
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
export default AlgebraicType;
|