@verft/sdk 0.0.0-stage → 1.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/LICENSE +22 -0
- package/README.md +14 -2
- package/dist/activity.d.ts +12 -0
- package/dist/activity.js +59 -0
- package/dist/connection.d.ts +135 -0
- package/dist/connection.js +1032 -0
- package/dist/drafts.d.ts +13 -0
- package/dist/drafts.js +21 -0
- package/dist/encoding.d.ts +112 -0
- package/dist/encoding.js +428 -0
- package/dist/files.d.ts +31 -0
- package/dist/files.js +56 -0
- package/dist/functions.d.ts +26 -0
- package/dist/functions.js +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +8 -0
- package/dist/landing.d.ts +25 -0
- package/dist/landing.js +19 -0
- package/dist/platform.d.ts +70 -0
- package/dist/platform.js +11 -0
- package/dist/protocol.d.ts +1198 -0
- package/dist/protocol.js +334 -0
- package/dist/react-native.d.ts +30 -0
- package/dist/react-native.js +34 -0
- package/dist/react.d.ts +23 -0
- package/dist/react.js +43 -0
- package/dist/readable.d.ts +14 -0
- package/dist/readable.js +31 -0
- package/dist/storage.d.ts +6 -0
- package/dist/storage.js +8 -0
- package/dist/subscriptions.d.ts +70 -0
- package/dist/subscriptions.js +168 -0
- package/dist/svelte.d.ts +26 -0
- package/dist/svelte.js +73 -0
- package/dist/tables.d.ts +17 -0
- package/dist/tables.js +18 -0
- package/dist/vue.d.ts +20 -0
- package/dist/vue.js +60 -0
- package/package.json +72 -4
package/dist/drafts.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Bytes } from "./encoding.js";
|
|
2
|
+
import type { Result } from "./subscriptions.js";
|
|
3
|
+
export interface Drafted {
|
|
4
|
+
readonly job: Bytes;
|
|
5
|
+
readonly run: number;
|
|
6
|
+
readonly piece: number;
|
|
7
|
+
}
|
|
8
|
+
export interface DraftedRow {
|
|
9
|
+
readonly at: Drafted;
|
|
10
|
+
}
|
|
11
|
+
export declare function jobOf(row: DraftedRow): string;
|
|
12
|
+
export declare function drafts<R extends DraftedRow>(rows: readonly R[]): ReadonlyMap<string, R[]>;
|
|
13
|
+
export declare function drafted<R extends DraftedRow>(result: Result<readonly R[]>): Result<ReadonlyMap<string, R[]>>;
|
package/dist/drafts.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function jobOf(row) {
|
|
2
|
+
return Array.from(row.at.job, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
3
|
+
}
|
|
4
|
+
export function drafts(rows) {
|
|
5
|
+
const newest = new Map();
|
|
6
|
+
for (const row of rows) {
|
|
7
|
+
const job = jobOf(row);
|
|
8
|
+
const kept = newest.get(job);
|
|
9
|
+
const run = kept?.[0]?.at.run ?? -1;
|
|
10
|
+
if (row.at.run > run)
|
|
11
|
+
newest.set(job, [row]);
|
|
12
|
+
else if (row.at.run === run)
|
|
13
|
+
kept.push(row);
|
|
14
|
+
}
|
|
15
|
+
for (const pieces of newest.values())
|
|
16
|
+
pieces.sort((left, right) => left.at.piece - right.at.piece);
|
|
17
|
+
return newest;
|
|
18
|
+
}
|
|
19
|
+
export function drafted(result) {
|
|
20
|
+
return result.state === "ready" ? { state: "ready", value: drafts(result.value) } : result;
|
|
21
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
export type Bytes = Uint8Array;
|
|
2
|
+
export type Timestamp = {
|
|
3
|
+
readonly microsSinceUnixEpoch: bigint;
|
|
4
|
+
};
|
|
5
|
+
export type Duration = {
|
|
6
|
+
readonly micros: bigint;
|
|
7
|
+
};
|
|
8
|
+
declare const identityBrand: unique symbol;
|
|
9
|
+
export type Identity = string & {
|
|
10
|
+
readonly [identityBrand]: true;
|
|
11
|
+
};
|
|
12
|
+
declare const connectionIdBrand: unique symbol;
|
|
13
|
+
export type ConnectionId = string & {
|
|
14
|
+
readonly [connectionIdBrand]: true;
|
|
15
|
+
};
|
|
16
|
+
declare const fileHandleBrand: unique symbol;
|
|
17
|
+
export type FileHandle = string & {
|
|
18
|
+
readonly [fileHandleBrand]: true;
|
|
19
|
+
};
|
|
20
|
+
export type DecodeErrorReason = "truncated" | "trailingBytes" | "invalidBool" | "invalidOption" | "invalidUtf8" | "unknownVariant" | "tooDeep";
|
|
21
|
+
export declare class DecodeError extends Error {
|
|
22
|
+
readonly reason: DecodeErrorReason;
|
|
23
|
+
constructor(reason: DecodeErrorReason);
|
|
24
|
+
}
|
|
25
|
+
export declare class Writer {
|
|
26
|
+
private buffer;
|
|
27
|
+
private view;
|
|
28
|
+
private length;
|
|
29
|
+
private depth;
|
|
30
|
+
finish(): Uint8Array;
|
|
31
|
+
enterNamed(): void;
|
|
32
|
+
leaveNamed(): void;
|
|
33
|
+
u8(value: number): void;
|
|
34
|
+
u16(value: number): void;
|
|
35
|
+
u32(value: number): void;
|
|
36
|
+
u64(value: bigint): void;
|
|
37
|
+
f32(value: number): void;
|
|
38
|
+
f64(value: number): void;
|
|
39
|
+
raw(bytes: Uint8Array): void;
|
|
40
|
+
counted(bytes: Uint8Array): void;
|
|
41
|
+
private reserve;
|
|
42
|
+
}
|
|
43
|
+
export declare class Reader {
|
|
44
|
+
private readonly input;
|
|
45
|
+
private readonly view;
|
|
46
|
+
private offset;
|
|
47
|
+
private depth;
|
|
48
|
+
constructor(input: Uint8Array);
|
|
49
|
+
finish(): void;
|
|
50
|
+
enterNamed(): void;
|
|
51
|
+
leaveNamed(): void;
|
|
52
|
+
u8(): number;
|
|
53
|
+
u16(): number;
|
|
54
|
+
u32(): number;
|
|
55
|
+
u64(): bigint;
|
|
56
|
+
f32(): number;
|
|
57
|
+
f64(): number;
|
|
58
|
+
raw(count: number): Uint8Array;
|
|
59
|
+
count(): number;
|
|
60
|
+
counted(): Uint8Array;
|
|
61
|
+
private take;
|
|
62
|
+
}
|
|
63
|
+
export interface Codec<T> {
|
|
64
|
+
write(writer: Writer, value: T): void;
|
|
65
|
+
read(reader: Reader): T;
|
|
66
|
+
}
|
|
67
|
+
export declare function encode<T>(codec: Codec<T>, value: T): Uint8Array;
|
|
68
|
+
export declare function decode<T>(codec: Codec<T>, bytes: Uint8Array): T;
|
|
69
|
+
export declare function decodeKnown<T>(codec: Codec<T>, bytes: Uint8Array): T | undefined;
|
|
70
|
+
export declare const unit: Codec<undefined>;
|
|
71
|
+
export declare const bool: Codec<boolean>;
|
|
72
|
+
export declare const i8: Codec<number>;
|
|
73
|
+
export declare const i16: Codec<number>;
|
|
74
|
+
export declare const i32: Codec<number>;
|
|
75
|
+
export declare const u8: Codec<number>;
|
|
76
|
+
export declare const u16: Codec<number>;
|
|
77
|
+
export declare const u32: Codec<number>;
|
|
78
|
+
export declare const i64: Codec<bigint>;
|
|
79
|
+
export declare const u64: Codec<bigint>;
|
|
80
|
+
export declare const f32: Codec<number>;
|
|
81
|
+
export declare const f64: Codec<number>;
|
|
82
|
+
export declare const string: Codec<string>;
|
|
83
|
+
export declare const bytes: Codec<Bytes>;
|
|
84
|
+
export declare const timestamp: Codec<Timestamp>;
|
|
85
|
+
export declare const duration: Codec<Duration>;
|
|
86
|
+
export declare const identity: Codec<Identity>;
|
|
87
|
+
export declare const connectionId: Codec<ConnectionId>;
|
|
88
|
+
export declare const fileHandle: Codec<FileHandle>;
|
|
89
|
+
export declare function option<T>(inner: Codec<T>): Codec<T | null>;
|
|
90
|
+
export declare function wrappedOption<T>(inner: Codec<T>): Codec<{
|
|
91
|
+
some: T;
|
|
92
|
+
} | null>;
|
|
93
|
+
export declare function list<T>(item: Codec<T>): Codec<T[]>;
|
|
94
|
+
export type Fields<T> = {
|
|
95
|
+
readonly [K in keyof T]-?: Codec<T[K]>;
|
|
96
|
+
};
|
|
97
|
+
export declare function record<T>(fields: Fields<T>): Codec<T>;
|
|
98
|
+
export declare function tuple<T extends unknown[]>(...fields: {
|
|
99
|
+
[K in keyof T]: Codec<T[K]>;
|
|
100
|
+
}): Codec<T>;
|
|
101
|
+
export type Variants<T extends {
|
|
102
|
+
tag: string;
|
|
103
|
+
}> = {
|
|
104
|
+
readonly [V in T as V["tag"]]: V extends {
|
|
105
|
+
value: infer X;
|
|
106
|
+
} ? Codec<X> : null;
|
|
107
|
+
};
|
|
108
|
+
export declare function variants<T extends {
|
|
109
|
+
tag: string;
|
|
110
|
+
}>(codecs: Variants<T>): Codec<T>;
|
|
111
|
+
export declare function named<T>(define: () => Codec<T>): Codec<T>;
|
|
112
|
+
export {};
|
package/dist/encoding.js
ADDED
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
export class DecodeError extends Error {
|
|
2
|
+
constructor(reason) {
|
|
3
|
+
super(`the input is not a valid Verft encoding: ${reason}`);
|
|
4
|
+
this.name = "DecodeError";
|
|
5
|
+
this.reason = reason;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
const namedDepthLimit = 128;
|
|
9
|
+
export class Writer {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.buffer = new Uint8Array(64);
|
|
12
|
+
this.view = new DataView(this.buffer.buffer);
|
|
13
|
+
this.length = 0;
|
|
14
|
+
this.depth = 0;
|
|
15
|
+
}
|
|
16
|
+
finish() {
|
|
17
|
+
return this.buffer.slice(0, this.length);
|
|
18
|
+
}
|
|
19
|
+
enterNamed() {
|
|
20
|
+
if (this.depth === namedDepthLimit) {
|
|
21
|
+
throw new RangeError(`named types nest more than ${namedDepthLimit} deep`);
|
|
22
|
+
}
|
|
23
|
+
this.depth++;
|
|
24
|
+
}
|
|
25
|
+
leaveNamed() {
|
|
26
|
+
this.depth--;
|
|
27
|
+
}
|
|
28
|
+
u8(value) {
|
|
29
|
+
const at = this.reserve(1);
|
|
30
|
+
this.buffer[at] = value;
|
|
31
|
+
}
|
|
32
|
+
u16(value) {
|
|
33
|
+
const at = this.reserve(2);
|
|
34
|
+
this.view.setUint16(at, value, true);
|
|
35
|
+
}
|
|
36
|
+
u32(value) {
|
|
37
|
+
const at = this.reserve(4);
|
|
38
|
+
this.view.setUint32(at, value, true);
|
|
39
|
+
}
|
|
40
|
+
u64(value) {
|
|
41
|
+
this.u32(Number(value & 0xffffffffn));
|
|
42
|
+
this.u32(Number(value >> 32n));
|
|
43
|
+
}
|
|
44
|
+
f32(value) {
|
|
45
|
+
const at = this.reserve(4);
|
|
46
|
+
this.view.setFloat32(at, value, true);
|
|
47
|
+
}
|
|
48
|
+
f64(value) {
|
|
49
|
+
const at = this.reserve(8);
|
|
50
|
+
this.view.setFloat64(at, value, true);
|
|
51
|
+
}
|
|
52
|
+
raw(bytes) {
|
|
53
|
+
const at = this.reserve(bytes.length);
|
|
54
|
+
this.buffer.set(bytes, at);
|
|
55
|
+
}
|
|
56
|
+
counted(bytes) {
|
|
57
|
+
this.u32(bytes.length);
|
|
58
|
+
this.raw(bytes);
|
|
59
|
+
}
|
|
60
|
+
reserve(count) {
|
|
61
|
+
const at = this.length;
|
|
62
|
+
if (at + count > this.buffer.length) {
|
|
63
|
+
const grown = new Uint8Array(Math.max(this.buffer.length * 2, at + count));
|
|
64
|
+
grown.set(this.buffer);
|
|
65
|
+
this.buffer = grown;
|
|
66
|
+
this.view = new DataView(grown.buffer);
|
|
67
|
+
}
|
|
68
|
+
this.length += count;
|
|
69
|
+
return at;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export class Reader {
|
|
73
|
+
constructor(input) {
|
|
74
|
+
this.offset = 0;
|
|
75
|
+
this.depth = 0;
|
|
76
|
+
this.input = input;
|
|
77
|
+
this.view = new DataView(input.buffer, input.byteOffset, input.byteLength);
|
|
78
|
+
}
|
|
79
|
+
finish() {
|
|
80
|
+
if (this.offset !== this.input.length)
|
|
81
|
+
throw new DecodeError("trailingBytes");
|
|
82
|
+
}
|
|
83
|
+
enterNamed() {
|
|
84
|
+
if (this.depth === namedDepthLimit)
|
|
85
|
+
throw new DecodeError("tooDeep");
|
|
86
|
+
this.depth++;
|
|
87
|
+
}
|
|
88
|
+
leaveNamed() {
|
|
89
|
+
this.depth--;
|
|
90
|
+
}
|
|
91
|
+
u8() {
|
|
92
|
+
return this.view.getUint8(this.take(1));
|
|
93
|
+
}
|
|
94
|
+
u16() {
|
|
95
|
+
return this.view.getUint16(this.take(2), true);
|
|
96
|
+
}
|
|
97
|
+
u32() {
|
|
98
|
+
return this.view.getUint32(this.take(4), true);
|
|
99
|
+
}
|
|
100
|
+
u64() {
|
|
101
|
+
const low = BigInt(this.u32());
|
|
102
|
+
return (BigInt(this.u32()) << 32n) | low;
|
|
103
|
+
}
|
|
104
|
+
f32() {
|
|
105
|
+
return this.view.getFloat32(this.take(4), true);
|
|
106
|
+
}
|
|
107
|
+
f64() {
|
|
108
|
+
return this.view.getFloat64(this.take(8), true);
|
|
109
|
+
}
|
|
110
|
+
raw(count) {
|
|
111
|
+
const at = this.take(count);
|
|
112
|
+
return this.input.slice(at, at + count);
|
|
113
|
+
}
|
|
114
|
+
count() {
|
|
115
|
+
const count = this.u32();
|
|
116
|
+
if (count > this.input.length - this.offset)
|
|
117
|
+
throw new DecodeError("truncated");
|
|
118
|
+
return count;
|
|
119
|
+
}
|
|
120
|
+
counted() {
|
|
121
|
+
return this.raw(this.count());
|
|
122
|
+
}
|
|
123
|
+
take(count) {
|
|
124
|
+
const at = this.offset;
|
|
125
|
+
if (at + count > this.input.length)
|
|
126
|
+
throw new DecodeError("truncated");
|
|
127
|
+
this.offset += count;
|
|
128
|
+
return at;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
export function encode(codec, value) {
|
|
132
|
+
const writer = new Writer();
|
|
133
|
+
codec.write(writer, value);
|
|
134
|
+
return writer.finish();
|
|
135
|
+
}
|
|
136
|
+
export function decode(codec, bytes) {
|
|
137
|
+
const reader = new Reader(bytes);
|
|
138
|
+
const value = codec.read(reader);
|
|
139
|
+
reader.finish();
|
|
140
|
+
return value;
|
|
141
|
+
}
|
|
142
|
+
export function decodeKnown(codec, bytes) {
|
|
143
|
+
try {
|
|
144
|
+
return decode(codec, bytes);
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
if (error instanceof DecodeError && error.reason === "unknownVariant")
|
|
148
|
+
return undefined;
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function refuse(value, type) {
|
|
153
|
+
throw new RangeError(`${String(value)} is not a valid ${type}`);
|
|
154
|
+
}
|
|
155
|
+
export const unit = {
|
|
156
|
+
write() { },
|
|
157
|
+
read() {
|
|
158
|
+
return undefined;
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
export const bool = {
|
|
162
|
+
write(writer, value) {
|
|
163
|
+
if (typeof value !== "boolean")
|
|
164
|
+
refuse(value, "bool");
|
|
165
|
+
writer.u8(value ? 1 : 0);
|
|
166
|
+
},
|
|
167
|
+
read(reader) {
|
|
168
|
+
const byte = reader.u8();
|
|
169
|
+
if (byte > 1)
|
|
170
|
+
throw new DecodeError("invalidBool");
|
|
171
|
+
return byte === 1;
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
function small(type, bits, signed) {
|
|
175
|
+
const span = 2 ** bits;
|
|
176
|
+
const min = signed ? -span / 2 : 0;
|
|
177
|
+
const write = { 8: "u8", 16: "u16", 32: "u32" };
|
|
178
|
+
return {
|
|
179
|
+
write(writer, value) {
|
|
180
|
+
if (!Number.isInteger(value) || value < min || value >= min + span)
|
|
181
|
+
refuse(value, type);
|
|
182
|
+
writer[write[bits]](value < 0 ? value + span : value);
|
|
183
|
+
},
|
|
184
|
+
read(reader) {
|
|
185
|
+
const value = reader[write[bits]]();
|
|
186
|
+
return signed && value >= span / 2 ? value - span : value;
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
export const i8 = small("i8", 8, true);
|
|
191
|
+
export const i16 = small("i16", 16, true);
|
|
192
|
+
export const i32 = small("i32", 32, true);
|
|
193
|
+
export const u8 = small("u8", 8, false);
|
|
194
|
+
export const u16 = small("u16", 16, false);
|
|
195
|
+
export const u32 = small("u32", 32, false);
|
|
196
|
+
function large(type, signed) {
|
|
197
|
+
const fits = signed ? (value) => BigInt.asIntN(64, value) === value : (value) => BigInt.asUintN(64, value) === value;
|
|
198
|
+
return {
|
|
199
|
+
write(writer, value) {
|
|
200
|
+
if (typeof value !== "bigint" || !fits(value))
|
|
201
|
+
refuse(value, type);
|
|
202
|
+
writer.u64(BigInt.asUintN(64, value));
|
|
203
|
+
},
|
|
204
|
+
read(reader) {
|
|
205
|
+
const value = reader.u64();
|
|
206
|
+
return signed ? BigInt.asIntN(64, value) : value;
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
export const i64 = large("i64", true);
|
|
211
|
+
export const u64 = large("u64", false);
|
|
212
|
+
export const f32 = {
|
|
213
|
+
write(writer, value) {
|
|
214
|
+
if (typeof value !== "number")
|
|
215
|
+
refuse(value, "f32");
|
|
216
|
+
writer.f32(value);
|
|
217
|
+
},
|
|
218
|
+
read: (reader) => reader.f32(),
|
|
219
|
+
};
|
|
220
|
+
export const f64 = {
|
|
221
|
+
write(writer, value) {
|
|
222
|
+
if (typeof value !== "number")
|
|
223
|
+
refuse(value, "f64");
|
|
224
|
+
writer.f64(value);
|
|
225
|
+
},
|
|
226
|
+
read: (reader) => reader.f64(),
|
|
227
|
+
};
|
|
228
|
+
export const string = {
|
|
229
|
+
write(writer, value) {
|
|
230
|
+
if (typeof value !== "string")
|
|
231
|
+
refuse(value, "string");
|
|
232
|
+
writer.counted(utf8Encode(value));
|
|
233
|
+
},
|
|
234
|
+
read: (reader) => utf8Decode(reader.counted()),
|
|
235
|
+
};
|
|
236
|
+
function utf8Encode(text) {
|
|
237
|
+
const bytes = [];
|
|
238
|
+
for (const character of text) {
|
|
239
|
+
const point = character.codePointAt(0) ?? 0;
|
|
240
|
+
if (point >= 0xd800 && point <= 0xdfff)
|
|
241
|
+
throw new RangeError("a string holds a lone surrogate, which UTF-8 cannot encode");
|
|
242
|
+
if (point < 0x80)
|
|
243
|
+
bytes.push(point);
|
|
244
|
+
else if (point < 0x800)
|
|
245
|
+
bytes.push(0xc0 | (point >> 6), 0x80 | (point & 0x3f));
|
|
246
|
+
else if (point < 0x10000)
|
|
247
|
+
bytes.push(0xe0 | (point >> 12), 0x80 | ((point >> 6) & 0x3f), 0x80 | (point & 0x3f));
|
|
248
|
+
else
|
|
249
|
+
bytes.push(0xf0 | (point >> 18), 0x80 | ((point >> 12) & 0x3f), 0x80 | ((point >> 6) & 0x3f), 0x80 | (point & 0x3f));
|
|
250
|
+
}
|
|
251
|
+
return Uint8Array.from(bytes);
|
|
252
|
+
}
|
|
253
|
+
function utf8Decode(bytes) {
|
|
254
|
+
let text = "";
|
|
255
|
+
let index = 0;
|
|
256
|
+
while (index < bytes.length) {
|
|
257
|
+
const first = bytes[index] ?? 0;
|
|
258
|
+
const size = first < 0x80 ? 1 : first >= 0xc2 && first < 0xe0 ? 2 : first >= 0xe0 && first < 0xf0 ? 3 : first >= 0xf0 && first < 0xf5 ? 4 : 0;
|
|
259
|
+
if (size === 0 || index + size > bytes.length)
|
|
260
|
+
throw new DecodeError("invalidUtf8");
|
|
261
|
+
let point = size === 1 ? first : first & (0xff >> (size + 1));
|
|
262
|
+
for (let next = index + 1; next < index + size; next++) {
|
|
263
|
+
const byte = bytes[next] ?? 0;
|
|
264
|
+
if ((byte & 0xc0) !== 0x80)
|
|
265
|
+
throw new DecodeError("invalidUtf8");
|
|
266
|
+
point = (point << 6) | (byte & 0x3f);
|
|
267
|
+
}
|
|
268
|
+
const overlong = point < [0, 0, 0x80, 0x800, 0x10000][size];
|
|
269
|
+
if (overlong || (point >= 0xd800 && point <= 0xdfff) || point > 0x10ffff)
|
|
270
|
+
throw new DecodeError("invalidUtf8");
|
|
271
|
+
text += String.fromCodePoint(point);
|
|
272
|
+
index += size;
|
|
273
|
+
}
|
|
274
|
+
return text;
|
|
275
|
+
}
|
|
276
|
+
export const bytes = {
|
|
277
|
+
write(writer, value) {
|
|
278
|
+
if (!(value instanceof Uint8Array))
|
|
279
|
+
refuse(value, "bytes");
|
|
280
|
+
writer.counted(value);
|
|
281
|
+
},
|
|
282
|
+
read: (reader) => reader.counted(),
|
|
283
|
+
};
|
|
284
|
+
export const timestamp = {
|
|
285
|
+
write: (writer, value) => i64.write(writer, value.microsSinceUnixEpoch),
|
|
286
|
+
read: (reader) => ({ microsSinceUnixEpoch: i64.read(reader) }),
|
|
287
|
+
};
|
|
288
|
+
export const duration = {
|
|
289
|
+
write: (writer, value) => i64.write(writer, value.micros),
|
|
290
|
+
read: (reader) => ({ micros: i64.read(reader) }),
|
|
291
|
+
};
|
|
292
|
+
const hex128Pattern = /^[0-9a-f]{32}$/;
|
|
293
|
+
function hex128(name) {
|
|
294
|
+
return {
|
|
295
|
+
write(writer, value) {
|
|
296
|
+
if (typeof value !== "string" || !hex128Pattern.test(value))
|
|
297
|
+
refuse(value, name);
|
|
298
|
+
for (let index = 0; index < 32; index += 2)
|
|
299
|
+
writer.u8(parseInt(value.slice(index, index + 2), 16));
|
|
300
|
+
},
|
|
301
|
+
read(reader) {
|
|
302
|
+
let hex = "";
|
|
303
|
+
for (const byte of reader.raw(16))
|
|
304
|
+
hex += byte.toString(16).padStart(2, "0");
|
|
305
|
+
return hex;
|
|
306
|
+
},
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
export const identity = hex128("Identity");
|
|
310
|
+
export const connectionId = hex128("ConnectionId");
|
|
311
|
+
export const fileHandle = hex128("FileHandle");
|
|
312
|
+
export function option(inner) {
|
|
313
|
+
return {
|
|
314
|
+
write(writer, value) {
|
|
315
|
+
if (value === null)
|
|
316
|
+
return writer.u8(0);
|
|
317
|
+
writer.u8(1);
|
|
318
|
+
inner.write(writer, value);
|
|
319
|
+
},
|
|
320
|
+
read(reader) {
|
|
321
|
+
const byte = reader.u8();
|
|
322
|
+
if (byte > 1)
|
|
323
|
+
throw new DecodeError("invalidOption");
|
|
324
|
+
return byte === 1 ? inner.read(reader) : null;
|
|
325
|
+
},
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
export function wrappedOption(inner) {
|
|
329
|
+
return {
|
|
330
|
+
write(writer, value) {
|
|
331
|
+
if (value === null)
|
|
332
|
+
return writer.u8(0);
|
|
333
|
+
writer.u8(1);
|
|
334
|
+
inner.write(writer, value.some);
|
|
335
|
+
},
|
|
336
|
+
read(reader) {
|
|
337
|
+
const byte = reader.u8();
|
|
338
|
+
if (byte > 1)
|
|
339
|
+
throw new DecodeError("invalidOption");
|
|
340
|
+
return byte === 1 ? { some: inner.read(reader) } : null;
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
export function list(item) {
|
|
345
|
+
return {
|
|
346
|
+
write(writer, value) {
|
|
347
|
+
if (!Array.isArray(value))
|
|
348
|
+
refuse(value, "list");
|
|
349
|
+
writer.u32(value.length);
|
|
350
|
+
for (const each of value)
|
|
351
|
+
item.write(writer, each);
|
|
352
|
+
},
|
|
353
|
+
read(reader) {
|
|
354
|
+
const count = reader.count();
|
|
355
|
+
const items = [];
|
|
356
|
+
for (let index = 0; index < count; index++)
|
|
357
|
+
items.push(item.read(reader));
|
|
358
|
+
return items;
|
|
359
|
+
},
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
export function record(fields) {
|
|
363
|
+
const entries = Object.entries(fields);
|
|
364
|
+
return {
|
|
365
|
+
write(writer, value) {
|
|
366
|
+
if (typeof value !== "object" || value === null)
|
|
367
|
+
refuse(value, "struct");
|
|
368
|
+
for (const [name, field] of entries)
|
|
369
|
+
field.write(writer, value[name]);
|
|
370
|
+
},
|
|
371
|
+
read(reader) {
|
|
372
|
+
const value = {};
|
|
373
|
+
for (const [name, field] of entries)
|
|
374
|
+
value[name] = field.read(reader);
|
|
375
|
+
return value;
|
|
376
|
+
},
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
export function tuple(...fields) {
|
|
380
|
+
return {
|
|
381
|
+
write(writer, value) {
|
|
382
|
+
if (!Array.isArray(value) || value.length !== fields.length)
|
|
383
|
+
refuse(value, `tuple of ${fields.length}`);
|
|
384
|
+
fields.forEach((field, index) => field.write(writer, value[index]));
|
|
385
|
+
},
|
|
386
|
+
read(reader) {
|
|
387
|
+
return fields.map((field) => field.read(reader));
|
|
388
|
+
},
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
export function variants(codecs) {
|
|
392
|
+
const entries = Object.entries(codecs);
|
|
393
|
+
const indices = new Map(entries.map(([tag], index) => [tag, index]));
|
|
394
|
+
return {
|
|
395
|
+
write(writer, value) {
|
|
396
|
+
const index = typeof value === "object" && value !== null ? indices.get(value.tag) : undefined;
|
|
397
|
+
if (index === undefined)
|
|
398
|
+
refuse(value?.tag, "variant tag");
|
|
399
|
+
writer.u32(index);
|
|
400
|
+
entries[index][1]?.write(writer, value.value);
|
|
401
|
+
},
|
|
402
|
+
read(reader) {
|
|
403
|
+
const index = reader.u32();
|
|
404
|
+
const entry = entries[index];
|
|
405
|
+
if (entry === undefined)
|
|
406
|
+
throw new DecodeError("unknownVariant");
|
|
407
|
+
const [tag, codec] = entry;
|
|
408
|
+
return (codec === null ? { tag } : { tag, value: codec.read(reader) });
|
|
409
|
+
},
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
export function named(define) {
|
|
413
|
+
let codec;
|
|
414
|
+
const resolved = () => (codec ?? (codec = define()));
|
|
415
|
+
return {
|
|
416
|
+
write(writer, value) {
|
|
417
|
+
writer.enterNamed();
|
|
418
|
+
resolved().write(writer, value);
|
|
419
|
+
writer.leaveNamed();
|
|
420
|
+
},
|
|
421
|
+
read(reader) {
|
|
422
|
+
reader.enterNamed();
|
|
423
|
+
const value = resolved().read(reader);
|
|
424
|
+
reader.leaveNamed();
|
|
425
|
+
return value;
|
|
426
|
+
},
|
|
427
|
+
};
|
|
428
|
+
}
|
package/dist/files.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type BlobLike } from "./platform.js";
|
|
2
|
+
export type { BlobLike } from "./platform.js";
|
|
3
|
+
type Field = {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly value: string;
|
|
6
|
+
};
|
|
7
|
+
export type Upload = {
|
|
8
|
+
readonly tag: "Parts";
|
|
9
|
+
readonly value: {
|
|
10
|
+
readonly url: string;
|
|
11
|
+
readonly part_size: bigint;
|
|
12
|
+
};
|
|
13
|
+
} | {
|
|
14
|
+
readonly tag: "Form";
|
|
15
|
+
readonly value: {
|
|
16
|
+
readonly url: string;
|
|
17
|
+
readonly fields: readonly Field[];
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export type UploadGrant = {
|
|
21
|
+
readonly upload: Upload;
|
|
22
|
+
};
|
|
23
|
+
export type DownloadGrant = {
|
|
24
|
+
readonly url: string;
|
|
25
|
+
};
|
|
26
|
+
export declare class FileRefused extends Error {
|
|
27
|
+
readonly status: number;
|
|
28
|
+
constructor(status: number);
|
|
29
|
+
}
|
|
30
|
+
export declare function upload(grant: UploadGrant, blob: BlobLike): Promise<void>;
|
|
31
|
+
export declare function download(grant: DownloadGrant): Promise<BlobLike>;
|
package/dist/files.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { platform } from "./platform.js";
|
|
2
|
+
export class FileRefused extends Error {
|
|
3
|
+
constructor(status) {
|
|
4
|
+
super(`storage refused the transfer with status ${status}`);
|
|
5
|
+
this.name = "FileRefused";
|
|
6
|
+
this.status = status;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
async function sent(url, method, body) {
|
|
10
|
+
const response = await platform.fetch(url, body === undefined ? { method } : { method, body });
|
|
11
|
+
if (!response.ok)
|
|
12
|
+
throw new FileRefused(response.status);
|
|
13
|
+
return response.json().catch(() => undefined);
|
|
14
|
+
}
|
|
15
|
+
async function inParts(url, partSize, blob) {
|
|
16
|
+
const { upload } = (await sent(`${url}?parts`, "POST"));
|
|
17
|
+
const parts = [];
|
|
18
|
+
try {
|
|
19
|
+
for (let start = 0, part = 1; start < blob.size; start += partSize, part += 1) {
|
|
20
|
+
const answer = (await sent(`${url}?upload=${upload}&part=${part}`, "PUT", blob.slice(start, start + partSize)));
|
|
21
|
+
parts.push({ part, etag: answer.etag });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
catch (refused) {
|
|
25
|
+
await platform.fetch(`${url}?upload=${upload}`, { method: "DELETE" }).catch(() => undefined);
|
|
26
|
+
throw refused;
|
|
27
|
+
}
|
|
28
|
+
await sent(`${url}?upload=${upload}`, "POST", JSON.stringify({ parts }));
|
|
29
|
+
}
|
|
30
|
+
export async function upload(grant, blob) {
|
|
31
|
+
const { upload } = grant;
|
|
32
|
+
if (upload.tag === "Form") {
|
|
33
|
+
const form = new platform.FormData();
|
|
34
|
+
for (const field of upload.value.fields)
|
|
35
|
+
form.append(field.name, field.value);
|
|
36
|
+
form.append("file", blob);
|
|
37
|
+
const response = await platform.fetch(upload.value.url, { method: "POST", body: form });
|
|
38
|
+
if (!response.ok)
|
|
39
|
+
throw new FileRefused(response.status);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (upload.tag !== "Parts")
|
|
43
|
+
throw new Error(`this SDK does not know how to upload by ${upload.tag}`);
|
|
44
|
+
const partSize = Number(upload.value.part_size);
|
|
45
|
+
if (blob.size <= partSize) {
|
|
46
|
+
await sent(upload.value.url, "PUT", blob);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
await inParts(upload.value.url, partSize, blob);
|
|
50
|
+
}
|
|
51
|
+
export async function download(grant) {
|
|
52
|
+
const response = await platform.fetch(grant.url);
|
|
53
|
+
if (!response.ok)
|
|
54
|
+
throw new FileRefused(response.status);
|
|
55
|
+
return response.blob();
|
|
56
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Codec, type Fields } from "./encoding.js";
|
|
2
|
+
export interface Action<A, R, E> {
|
|
3
|
+
readonly kind: "action";
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly arguments: Fields<A>;
|
|
6
|
+
readonly returns: Codec<R>;
|
|
7
|
+
readonly error: Codec<E> | undefined;
|
|
8
|
+
}
|
|
9
|
+
export interface Query<A, R, E> {
|
|
10
|
+
readonly kind: "query";
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly arguments: Fields<A>;
|
|
13
|
+
readonly returns: Codec<R>;
|
|
14
|
+
readonly error: Codec<E> | undefined;
|
|
15
|
+
}
|
|
16
|
+
export type Outcome<R, E> = {
|
|
17
|
+
readonly ok: true;
|
|
18
|
+
readonly value: R;
|
|
19
|
+
} | {
|
|
20
|
+
readonly ok: false;
|
|
21
|
+
readonly error: E;
|
|
22
|
+
};
|
|
23
|
+
export type Reply<R, E> = [E] extends [never] ? R : Outcome<R, E>;
|
|
24
|
+
export declare function action<A, R, E = never>(name: string, args: Fields<A>, returns: Codec<R>, error?: Codec<E>): Action<A, R, E>;
|
|
25
|
+
export declare function query<A, R, E = never>(name: string, args: Fields<A>, returns: Codec<R>, error?: Codec<E>): Query<A, R, E>;
|
|
26
|
+
export declare function encodeArguments<A, R, E>(fn: Action<A, R, E> | Query<A, R, E>, args: NoInfer<A>): Uint8Array;
|