toiljs 0.0.126 → 0.0.128
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/CHANGELOG.md +28 -0
- package/assets/image-codec-LICENSES.txt +4599 -0
- package/assets/image-codec-build.json +10 -0
- package/assets/image-codec.wasm +0 -0
- package/build/backend/index.d.ts +1 -0
- package/build/backend/index.js +8 -3
- package/build/backend/security.d.ts +4 -0
- package/build/backend/security.js +31 -0
- package/build/cli/index.js +11 -39
- package/build/client/components/Script.js +28 -17
- package/build/client/files.d.ts +17 -0
- package/build/client/files.js +119 -0
- package/build/client/html.d.ts +1 -0
- package/build/client/html.js +8 -0
- package/build/client/index.d.ts +1 -0
- package/build/client/index.js +1 -0
- package/build/client/routing/Router.js +1 -1
- package/build/client/security.d.ts +4 -0
- package/build/client/security.js +55 -0
- package/build/client/ssr/markers.js +6 -1
- package/build/compiler/config.d.ts +2 -0
- package/build/compiler/config.js +1 -0
- package/build/compiler/generate.d.ts +1 -1
- package/build/compiler/generate.js +1 -37
- package/build/compiler/index.d.ts +1 -0
- package/build/compiler/index.js +54 -1
- package/build/compiler/security.d.ts +7 -0
- package/build/compiler/security.js +74 -0
- package/build/compiler/sri.js +10 -3
- package/build/compiler/toil-docs.generated.js +33 -27
- package/build/compiler/vite.js +2 -0
- package/build/devserver/config/env.d.ts +6 -0
- package/build/devserver/config/env.js +10 -2
- package/build/devserver/daemon/catalog.d.ts +1 -0
- package/build/devserver/daemon/catalog.js +10 -4
- package/build/devserver/daemon/host.d.ts +5 -1
- package/build/devserver/daemon/host.js +8 -4
- package/build/devserver/daemon/http.d.ts +14 -0
- package/build/devserver/daemon/http.js +90 -0
- package/build/devserver/daemon/index.d.ts +21 -1
- package/build/devserver/daemon/index.js +109 -21
- package/build/devserver/db/atomic.d.ts +8 -0
- package/build/devserver/db/atomic.js +133 -0
- package/build/devserver/db/catalog.d.ts +7 -0
- package/build/devserver/db/catalog.js +17 -0
- package/build/devserver/db/changes.d.ts +5 -0
- package/build/devserver/db/changes.js +49 -0
- package/build/devserver/db/database.d.ts +7 -0
- package/build/devserver/db/database.js +251 -41
- package/build/devserver/db/derives.d.ts +4 -5
- package/build/devserver/db/derives.js +7 -76
- package/build/devserver/db/document-scan.d.ts +2 -0
- package/build/devserver/db/document-scan.js +123 -0
- package/build/devserver/db/limits.d.ts +16 -0
- package/build/devserver/db/limits.js +92 -14
- package/build/devserver/db/types.d.ts +15 -0
- package/build/devserver/db/types.js +9 -0
- package/build/devserver/files/dev.d.ts +3 -0
- package/build/devserver/files/dev.js +29 -0
- package/build/devserver/files/disk.d.ts +17 -0
- package/build/devserver/files/disk.js +369 -0
- package/build/devserver/files/signatures.d.ts +16 -0
- package/build/devserver/files/signatures.js +151 -0
- package/build/devserver/http/proxy.js +11 -3
- package/build/devserver/http/runtime.js +36 -9
- package/build/devserver/images/index.d.ts +9 -0
- package/build/devserver/images/index.js +87 -0
- package/build/devserver/images/worker.d.mts +1 -0
- package/build/devserver/images/worker.mjs +24 -0
- package/build/devserver/index.d.ts +3 -1
- package/build/devserver/production.js +5 -3
- package/build/devserver/runtime/host.d.ts +4 -0
- package/build/devserver/runtime/host.js +6 -2
- package/build/devserver/runtime/module.d.ts +15 -0
- package/build/devserver/runtime/module.js +105 -7
- package/build/devserver/server.js +13 -1
- package/build/devserver/stream/host.d.ts +2 -0
- package/build/devserver/stream/host.js +2 -1
- package/build/devserver/stream/index.js +2 -0
- package/build/io/derive-metadata.d.ts +7 -0
- package/build/io/derive-metadata.js +44 -0
- package/build/io/index.d.ts +1 -0
- package/build/io/index.js +1 -0
- package/build/shared/index.d.ts +1 -0
- package/build/shared/index.js +1 -0
- package/build/shared/security.d.ts +5 -0
- package/build/shared/security.js +64 -0
- package/examples/basic/client/routes/rest.tsx +1 -1
- package/examples/basic/server/routes/Guestbook.ts +1 -1
- package/package.json +19 -7
- package/scripts/build-image-codec.mjs +79 -0
- package/scripts/gen-auth-vectors.mjs +132 -0
- package/scripts/gen-native-atomic-fixture.mjs +39 -0
- package/scripts/gen-native-blob-fixture.mjs +42 -0
- package/scripts/gen-native-image-fixture.mjs +65 -0
- package/scripts/gen-native-staging-fixture.mjs +41 -0
- package/scripts/gen-server-types.mjs +82 -0
- package/server/auth/AuthController.ts +15 -234
- package/server/auth/AuthMaintenance.ts +177 -0
- package/server/auth/AuthStore.ts +198 -0
- package/server/auth/SessionEpochs.ts +38 -9
- package/server/globals/auth.ts +56 -0
- package/server/globals.d.ts +37 -0
- package/server/runtime/envelope.ts +2 -1
- package/server/runtime/exports/index.ts +2 -1
- package/server/runtime/files/blobs.ts +341 -0
- package/server/runtime/files/images.ts +36 -0
- package/server/runtime/http/percent.ts +5 -4
- package/server/runtime/index.ts +5 -0
- package/server/runtime/response.ts +20 -0
- package/server/runtime/rest/RouteContext.ts +5 -4
- package/std/server/abort/abort.d.ts +25 -0
- package/std/server/env/Server.d.ts +47 -0
- package/std/server/envelope.d.ts +38 -0
- package/std/server/exports/index.d.ts +20 -0
- package/std/server/exports/render.d.ts +15 -0
- package/std/server/files/blobs.d.ts +50 -0
- package/std/server/files/images.d.ts +36 -0
- package/std/server/handlers/ToilHandler.d.ts +29 -0
- package/std/server/http/base64.d.ts +21 -0
- package/std/server/http/cookie.d.ts +127 -0
- package/std/server/http/cookies.d.ts +56 -0
- package/std/server/http/date.d.ts +17 -0
- package/std/server/http/percent.d.ts +19 -0
- package/std/server/http/securecookies.d.ts +55 -0
- package/std/server/index.d.ts +34 -0
- package/std/server/lang/Potential.d.ts +6 -0
- package/std/server/memory.d.ts +42 -0
- package/std/server/request.d.ts +47 -0
- package/std/server/response.d.ts +90 -0
- package/std/server/rest/Rest.d.ts +23 -0
- package/std/server/rest/RestHandler.d.ts +14 -0
- package/std/server/rest/RouteContext.d.ts +33 -0
- package/std/server/rest/match.d.ts +14 -0
- package/std/server/rpc/Rpc.d.ts +37 -0
- package/std/server/ssr/Ssr.d.ts +27 -0
- package/std/server/ssr/encode.d.ts +26 -0
- package/std/server/ssr/escape.d.ts +22 -0
- package/std/server/ssr/slots.d.ts +91 -0
- package/std/server/time.d.ts +24 -0
- package/tsconfig.json +11 -2
- package/build/backend/.tsbuildinfo +0 -1
- package/build/cli/.tsbuildinfo +0 -1
- package/build/client/.tsbuildinfo +0 -1
- package/build/compiler/.tsbuildinfo +0 -1
- package/build/devserver/.tsbuildinfo +0 -1
- package/build/io/.tsbuildinfo +0 -1
- package/build/logger/.tsbuildinfo +0 -1
- package/build/shared/.tsbuildinfo +0 -1
- package/build/testing/.tsbuildinfo +0 -1
|
@@ -1,41 +1,17 @@
|
|
|
1
|
+
import { readDeriveCatalog, deriveIdentity } from 'toiljs/io';
|
|
1
2
|
import { customSection } from '../wasm/sections.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const MAX_NAME_BYTES = 1024;
|
|
7
|
-
const UTF8_DECODER = new TextDecoder('utf-8', { fatal: true });
|
|
3
|
+
export function deriveCheckpointKey(entry) {
|
|
4
|
+
return JSON.stringify([entry.dbName, entry.methodName]);
|
|
5
|
+
}
|
|
6
|
+
export const deriveContinuationKey = deriveIdentity;
|
|
8
7
|
export function parseDerives(wasm) {
|
|
9
|
-
let section;
|
|
10
8
|
try {
|
|
11
|
-
section = customSection(wasm,
|
|
9
|
+
const section = customSection(wasm, 'toildb.derives');
|
|
10
|
+
return section === null ? [] : (readDeriveCatalog(section) ?? []);
|
|
12
11
|
}
|
|
13
12
|
catch {
|
|
14
13
|
return [];
|
|
15
14
|
}
|
|
16
|
-
if (section === null)
|
|
17
|
-
return [];
|
|
18
|
-
if (section.length > MAX_SECTION_BYTES)
|
|
19
|
-
return [];
|
|
20
|
-
const r = new Reader(section);
|
|
21
|
-
const version = r.u16();
|
|
22
|
-
if (!r.ok || version !== VERSION)
|
|
23
|
-
return [];
|
|
24
|
-
const count = r.u16();
|
|
25
|
-
if (!r.ok || count > MAX_DERIVES)
|
|
26
|
-
return [];
|
|
27
|
-
const derives = [];
|
|
28
|
-
for (let i = 0; i < count && r.ok; i++) {
|
|
29
|
-
const deriveId = r.u16();
|
|
30
|
-
const dbName = r.string();
|
|
31
|
-
const methodName = r.string();
|
|
32
|
-
if (!r.ok || dbName.length === 0)
|
|
33
|
-
return [];
|
|
34
|
-
derives.push({ deriveId, dbName, methodName });
|
|
35
|
-
}
|
|
36
|
-
if (!r.ok || r.remaining() !== 0)
|
|
37
|
-
return [];
|
|
38
|
-
return derives;
|
|
39
15
|
}
|
|
40
16
|
export function derivesForWrites(derives, written) {
|
|
41
17
|
if (derives.length === 0 || written.size === 0)
|
|
@@ -47,48 +23,3 @@ export function derivesForWrites(derives, written) {
|
|
|
47
23
|
}
|
|
48
24
|
return derives.filter((d) => dbs.has(d.dbName));
|
|
49
25
|
}
|
|
50
|
-
class Reader {
|
|
51
|
-
bytes;
|
|
52
|
-
pos = 0;
|
|
53
|
-
ok = true;
|
|
54
|
-
constructor(bytes) {
|
|
55
|
-
this.bytes = bytes;
|
|
56
|
-
}
|
|
57
|
-
remaining() {
|
|
58
|
-
return this.bytes.length - this.pos;
|
|
59
|
-
}
|
|
60
|
-
u16() {
|
|
61
|
-
if (!this.ok || this.pos + 2 > this.bytes.length) {
|
|
62
|
-
this.ok = false;
|
|
63
|
-
return 0;
|
|
64
|
-
}
|
|
65
|
-
const out = this.bytes.readUInt16LE(this.pos);
|
|
66
|
-
this.pos += 2;
|
|
67
|
-
return out;
|
|
68
|
-
}
|
|
69
|
-
u32() {
|
|
70
|
-
if (!this.ok || this.pos + 4 > this.bytes.length) {
|
|
71
|
-
this.ok = false;
|
|
72
|
-
return 0;
|
|
73
|
-
}
|
|
74
|
-
const out = this.bytes.readUInt32LE(this.pos);
|
|
75
|
-
this.pos += 4;
|
|
76
|
-
return out;
|
|
77
|
-
}
|
|
78
|
-
string() {
|
|
79
|
-
const len = this.u32();
|
|
80
|
-
if (!this.ok || len > MAX_NAME_BYTES || this.pos + len > this.bytes.length) {
|
|
81
|
-
this.ok = false;
|
|
82
|
-
return '';
|
|
83
|
-
}
|
|
84
|
-
try {
|
|
85
|
-
const out = UTF8_DECODER.decode(this.bytes.subarray(this.pos, this.pos + len));
|
|
86
|
-
this.pos += len;
|
|
87
|
-
return out;
|
|
88
|
-
}
|
|
89
|
-
catch {
|
|
90
|
-
this.ok = false;
|
|
91
|
-
return '';
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { DataWriter } from 'toiljs/io';
|
|
3
|
+
import { chargeDbReadProbe, dbBudgetSnapshot } from './limits.js';
|
|
4
|
+
export function scanDocuments(collection, records, versions, db, after, limit) {
|
|
5
|
+
const binding = createHash('sha256')
|
|
6
|
+
.update(collection)
|
|
7
|
+
.update('\0scan-v1')
|
|
8
|
+
.digest()
|
|
9
|
+
.subarray(0, 16);
|
|
10
|
+
let bucket = 0;
|
|
11
|
+
let hash = null;
|
|
12
|
+
let afterHash = null;
|
|
13
|
+
let afterKey = null;
|
|
14
|
+
if (after !== null) {
|
|
15
|
+
const invalid = () => {
|
|
16
|
+
throw new Error('data.documents_scan: invalid cursor');
|
|
17
|
+
};
|
|
18
|
+
if (after.length < 21 ||
|
|
19
|
+
after.length > 4137 ||
|
|
20
|
+
after[0] !== 1 ||
|
|
21
|
+
!after.subarray(1, 17).equals(binding) ||
|
|
22
|
+
after[20] !== 0)
|
|
23
|
+
invalid();
|
|
24
|
+
bucket = after.readUInt16LE(17);
|
|
25
|
+
const flags = after[19];
|
|
26
|
+
if (bucket > 255 || flags > 3)
|
|
27
|
+
invalid();
|
|
28
|
+
let pos = 21;
|
|
29
|
+
if (flags !== 0) {
|
|
30
|
+
if (after.length < pos + 16 || after[pos] !== bucket)
|
|
31
|
+
invalid();
|
|
32
|
+
const h = after.subarray(pos, pos + 16);
|
|
33
|
+
pos += 16;
|
|
34
|
+
if (flags === 1)
|
|
35
|
+
afterHash = h;
|
|
36
|
+
else
|
|
37
|
+
hash = h;
|
|
38
|
+
}
|
|
39
|
+
if (flags === 3) {
|
|
40
|
+
if (after.length < pos + 4)
|
|
41
|
+
invalid();
|
|
42
|
+
const n = after.readUInt32LE(pos);
|
|
43
|
+
pos += 4;
|
|
44
|
+
if (n > 4096 || after.length !== pos + n)
|
|
45
|
+
invalid();
|
|
46
|
+
afterKey = after.subarray(pos);
|
|
47
|
+
pos += n;
|
|
48
|
+
}
|
|
49
|
+
if (pos !== after.length)
|
|
50
|
+
invalid();
|
|
51
|
+
}
|
|
52
|
+
const prefix = collection + '\0';
|
|
53
|
+
const rows = Array.from(records)
|
|
54
|
+
.filter(([key]) => key.startsWith(prefix))
|
|
55
|
+
.map(([sk, value]) => {
|
|
56
|
+
const key = Buffer.from(sk.slice(prefix.length), 'latin1');
|
|
57
|
+
const hash = createHash('sha256').update(prefix).update(key).digest().subarray(0, 16);
|
|
58
|
+
return { sk, key, value, hash };
|
|
59
|
+
})
|
|
60
|
+
.sort((a, b) => Buffer.compare(a.hash, b.hash) || Buffer.compare(a.key, b.key));
|
|
61
|
+
const entries = [];
|
|
62
|
+
let frameBytes = 5;
|
|
63
|
+
for (let probe = 0; probe < 8 && bucket < 256 && entries.length < limit; probe++) {
|
|
64
|
+
if (probe > 0)
|
|
65
|
+
chargeDbReadProbe(db);
|
|
66
|
+
if (hash === null) {
|
|
67
|
+
const next = rows.find((row) => row.hash[0] === bucket &&
|
|
68
|
+
(afterHash === null || Buffer.compare(row.hash, afterHash) > 0));
|
|
69
|
+
if (next === undefined) {
|
|
70
|
+
bucket++;
|
|
71
|
+
afterHash = null;
|
|
72
|
+
afterKey = null;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
hash = next.hash;
|
|
76
|
+
afterHash = null;
|
|
77
|
+
afterKey = null;
|
|
78
|
+
}
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const currentHash = hash;
|
|
82
|
+
const row = rows.find((row) => row.hash.equals(currentHash) &&
|
|
83
|
+
(afterKey === null || Buffer.compare(row.key, afterKey) > 0));
|
|
84
|
+
if (row === undefined) {
|
|
85
|
+
afterHash = hash;
|
|
86
|
+
hash = null;
|
|
87
|
+
afterKey = null;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const entryBytes = 12 + row.key.length + row.value.length;
|
|
91
|
+
if (frameBytes + entryBytes + 45 + row.key.length > dbBudgetSnapshot(db).maxResultBytes) {
|
|
92
|
+
if (entries.length === 0)
|
|
93
|
+
throw new Error('TDL021: document scan entry exceeds result budget');
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
entries.push(row);
|
|
97
|
+
frameBytes += entryBytes;
|
|
98
|
+
afterKey = row.key;
|
|
99
|
+
}
|
|
100
|
+
const writer = new DataWriter().writeU32(entries.length);
|
|
101
|
+
for (const row of entries)
|
|
102
|
+
writer
|
|
103
|
+
.writeBytes(row.key)
|
|
104
|
+
.writeU32(versions.get(row.sk) ?? 0)
|
|
105
|
+
.writeBytes(row.value);
|
|
106
|
+
writer.writeU8(bucket < 256 ? 1 : 0);
|
|
107
|
+
if (bucket < 256) {
|
|
108
|
+
const flags = hash !== null ? (afterKey !== null ? 3 : 2) : afterHash !== null ? 1 : 0;
|
|
109
|
+
const cursor = new DataWriter().writeU8(1);
|
|
110
|
+
for (const byte of binding)
|
|
111
|
+
cursor.writeU8(byte);
|
|
112
|
+
cursor.writeU16(bucket).writeU8(flags).writeU8(0);
|
|
113
|
+
const cursorHash = hash ?? afterHash;
|
|
114
|
+
if (cursorHash !== null) {
|
|
115
|
+
for (const byte of cursorHash)
|
|
116
|
+
cursor.writeU8(byte);
|
|
117
|
+
}
|
|
118
|
+
if (afterKey !== null)
|
|
119
|
+
cursor.writeBytes(afterKey);
|
|
120
|
+
writer.writeBytes(cursor.toBytes());
|
|
121
|
+
}
|
|
122
|
+
return Buffer.from(writer.toBytes());
|
|
123
|
+
}
|
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import { type DbDevState } from './types.js';
|
|
2
2
|
type Imports = Record<string, (...args: number[]) => number | bigint>;
|
|
3
|
+
export interface DbBudgetSnapshot {
|
|
4
|
+
ops: number;
|
|
5
|
+
writes: number;
|
|
6
|
+
bytes: number;
|
|
7
|
+
maxOps: number;
|
|
8
|
+
maxWrites: number;
|
|
9
|
+
maxBytes: number;
|
|
10
|
+
maxResultBytes: number;
|
|
11
|
+
remainingOps: number;
|
|
12
|
+
remainingWrites: number;
|
|
13
|
+
remainingBytes: number;
|
|
14
|
+
}
|
|
15
|
+
export declare function dbBudgetSnapshot(db: DbDevState): DbBudgetSnapshot;
|
|
16
|
+
export declare function formatDbBudget(db: DbDevState): string;
|
|
3
17
|
export declare function resetDbLimits(db: DbDevState): void;
|
|
18
|
+
export declare function chargeDbReadProbe(db: DbDevState): void;
|
|
19
|
+
export declare function chargeDbControlWrite(db: DbDevState, bytes: number, operation?: string): void;
|
|
4
20
|
export declare function checkDbResultSize(db: DbDevState, bytes: number): void;
|
|
5
21
|
export declare function withDbLimits(imports: Imports, db: DbDevState): Imports;
|
|
6
22
|
export {};
|
|
@@ -5,8 +5,16 @@ const exempt = new Set([
|
|
|
5
5
|
'data.take_result',
|
|
6
6
|
'data.result_schema_version',
|
|
7
7
|
'data.write_allowed',
|
|
8
|
+
'data.job_allowed',
|
|
9
|
+
'data.atomic_batch',
|
|
10
|
+
'data.budget',
|
|
11
|
+
'data.changes',
|
|
12
|
+
'data.begin_derive',
|
|
13
|
+
'data.end_derive',
|
|
14
|
+
'data.continue_derive',
|
|
8
15
|
]);
|
|
9
16
|
const reads = new Set([
|
|
17
|
+
'data.atomic_get',
|
|
10
18
|
'data.get',
|
|
11
19
|
'data.get_current',
|
|
12
20
|
'data.get_many',
|
|
@@ -16,6 +24,7 @@ const reads = new Set([
|
|
|
16
24
|
'data.membership_contains',
|
|
17
25
|
'data.membership_list',
|
|
18
26
|
'data.membership_page',
|
|
27
|
+
'data.documents_scan',
|
|
19
28
|
'data.counter_get',
|
|
20
29
|
'data.events_get',
|
|
21
30
|
'data.events_reference',
|
|
@@ -26,6 +35,7 @@ const reads = new Set([
|
|
|
26
35
|
'data.capacity_available',
|
|
27
36
|
]);
|
|
28
37
|
const results = new Set([
|
|
38
|
+
'data.atomic_get',
|
|
29
39
|
'data.get',
|
|
30
40
|
'data.get_current',
|
|
31
41
|
'data.get_many',
|
|
@@ -35,6 +45,7 @@ const results = new Set([
|
|
|
35
45
|
'data.view_get',
|
|
36
46
|
'data.membership_list',
|
|
37
47
|
'data.membership_page',
|
|
48
|
+
'data.documents_scan',
|
|
38
49
|
'data.counter_get',
|
|
39
50
|
'data.events_get',
|
|
40
51
|
'data.events_reference',
|
|
@@ -58,9 +69,71 @@ const writeLength = {
|
|
|
58
69
|
'data.append': 4,
|
|
59
70
|
'data.append_once': 6,
|
|
60
71
|
};
|
|
72
|
+
export function dbBudgetSnapshot(db) {
|
|
73
|
+
const kind = db.budgetKind ?? db.functionKind;
|
|
74
|
+
const count = counters.get(db);
|
|
75
|
+
const ops = count?.kind === kind ? count.ops : 0;
|
|
76
|
+
const writes = count?.kind === kind ? count.writes : 0;
|
|
77
|
+
const bytes = count?.kind === kind ? count.bytes : 0;
|
|
78
|
+
const maxOps = kind === DbFunctionKind.Query || kind === DbFunctionKind.Action ? 64 : 65535;
|
|
79
|
+
const maxWrites = kind === DbFunctionKind.Query ? 0 : kind === DbFunctionKind.Action ? 32 : 65535;
|
|
80
|
+
const maxBytes = kind === DbFunctionKind.Query
|
|
81
|
+
? 131072
|
|
82
|
+
: kind === DbFunctionKind.Action
|
|
83
|
+
? 2097152
|
|
84
|
+
: 16777216;
|
|
85
|
+
const maxResultBytes = kind === DbFunctionKind.Query ? 65536 : kind === DbFunctionKind.Action ? 131072 : 1048576;
|
|
86
|
+
return {
|
|
87
|
+
ops,
|
|
88
|
+
writes,
|
|
89
|
+
bytes,
|
|
90
|
+
maxOps,
|
|
91
|
+
maxWrites,
|
|
92
|
+
maxBytes,
|
|
93
|
+
maxResultBytes,
|
|
94
|
+
remainingOps: Math.max(0, maxOps - ops),
|
|
95
|
+
remainingWrites: Math.max(0, maxWrites - writes),
|
|
96
|
+
remainingBytes: Math.max(0, maxBytes - bytes),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
export function formatDbBudget(db) {
|
|
100
|
+
const b = dbBudgetSnapshot(db);
|
|
101
|
+
return `${db.functionKind}: ops=${b.ops}/${b.maxOps} writes=${b.writes}/${b.maxWrites} bytes=${b.bytes}/${b.maxBytes}; remaining ops=${b.remainingOps} writes=${b.remainingWrites} bytes=${b.remainingBytes}`;
|
|
102
|
+
}
|
|
61
103
|
export function resetDbLimits(db) {
|
|
62
104
|
counters.delete(db);
|
|
63
105
|
}
|
|
106
|
+
export function chargeDbReadProbe(db) {
|
|
107
|
+
const kind = db.budgetKind ?? db.functionKind;
|
|
108
|
+
let count = counters.get(db);
|
|
109
|
+
if (count === undefined || count.kind !== kind) {
|
|
110
|
+
count = { kind, ops: 0, writes: 0, bytes: 0 };
|
|
111
|
+
counters.set(db, count);
|
|
112
|
+
}
|
|
113
|
+
if (process.env.TOIL_DEV_CHECK_DB_LIMITS === '1' && count.ops + 1 > dbBudgetSnapshot(db).maxOps)
|
|
114
|
+
throw new Error('TDL020: document scan exceeds operation budget');
|
|
115
|
+
count.ops++;
|
|
116
|
+
}
|
|
117
|
+
export function chargeDbControlWrite(db, bytes, operation = 'data.continue_derive') {
|
|
118
|
+
const kind = db.budgetKind ?? db.functionKind;
|
|
119
|
+
let count = counters.get(db);
|
|
120
|
+
if (count === undefined || count.kind !== kind) {
|
|
121
|
+
count = { kind, ops: 0, writes: 0, bytes: 0 };
|
|
122
|
+
counters.set(db, count);
|
|
123
|
+
}
|
|
124
|
+
const limits = dbBudgetSnapshot(db);
|
|
125
|
+
const enforcing = process.env.TOIL_DEV_CHECK_DB_LIMITS === '1';
|
|
126
|
+
if (enforcing && (count.ops + 1 > limits.maxOps || count.writes + 1 > limits.maxWrites))
|
|
127
|
+
throw new Error(`TDL020: ${operation} exceeds DB budget; ${formatDbBudget(db)}`);
|
|
128
|
+
count.ops++;
|
|
129
|
+
count.writes++;
|
|
130
|
+
checkDbResultSize(db, bytes);
|
|
131
|
+
if (enforcing && count.bytes + bytes > limits.maxBytes)
|
|
132
|
+
throw new Error(`TDL021: ${operation} exceeds DB byte budget; ${formatDbBudget(db)}`);
|
|
133
|
+
count.bytes += bytes;
|
|
134
|
+
if (process.env.TOIL_DEV_TRACE_DB_LIMITS === '1')
|
|
135
|
+
console.error(`[toil db] ${operation} ${formatDbBudget(db)}`);
|
|
136
|
+
}
|
|
64
137
|
export function checkDbResultSize(db, bytes) {
|
|
65
138
|
if (process.env.TOIL_DEV_CHECK_DB_LIMITS !== '1')
|
|
66
139
|
return;
|
|
@@ -76,29 +149,32 @@ export function withDbLimits(imports, db) {
|
|
|
76
149
|
return Object.fromEntries(Object.entries(imports).map(([name, operation]) => [
|
|
77
150
|
name,
|
|
78
151
|
(...args) => {
|
|
79
|
-
if (
|
|
152
|
+
if (exempt.has(name))
|
|
80
153
|
return operation(...args);
|
|
81
|
-
const kind = db.functionKind;
|
|
154
|
+
const kind = db.budgetKind ?? db.functionKind;
|
|
82
155
|
let count = counters.get(db);
|
|
83
156
|
if (count === undefined || count.kind !== kind) {
|
|
84
157
|
count = { kind, ops: 0, writes: 0, bytes: 0 };
|
|
85
158
|
counters.set(db, count);
|
|
86
159
|
}
|
|
87
|
-
const
|
|
88
|
-
const
|
|
89
|
-
const maxBytes = kind === DbFunctionKind.Query
|
|
90
|
-
? 131072
|
|
91
|
-
: kind === DbFunctionKind.Action
|
|
92
|
-
? 2097152
|
|
93
|
-
: 16777216;
|
|
160
|
+
const enforcing = process.env.TOIL_DEV_CHECK_DB_LIMITS === '1';
|
|
161
|
+
const { maxOps, maxWrites, maxBytes } = dbBudgetSnapshot(db);
|
|
94
162
|
const writing = !reads.has(name);
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
163
|
+
const nextOps = count.ops + 1;
|
|
164
|
+
const nextWrites = count.writes + (writing ? 1 : 0);
|
|
165
|
+
if (enforcing && (nextOps > maxOps || nextWrites > maxWrites))
|
|
166
|
+
throw new Error(`TDL020: ${name} exceeds DB budget (${nextOps}/${maxOps} operations, ${nextWrites}/${maxWrites} writes; ${formatDbBudget(db)})`);
|
|
167
|
+
const writeBytes = (name === 'data.events_redact'
|
|
168
|
+
? 16
|
|
169
|
+
: writeLength[name] === undefined
|
|
170
|
+
? 0
|
|
171
|
+
: args[writeLength[name]]) +
|
|
98
172
|
(name === 'data.compare_exchange' ? Math.max(0, args[4]) : 0);
|
|
99
173
|
checkDbResultSize(db, writeBytes);
|
|
100
|
-
if (count.bytes + writeBytes > maxBytes)
|
|
174
|
+
if (enforcing && count.bytes + writeBytes > maxBytes)
|
|
101
175
|
throw new Error(`TDL021: ${kind} cumulative DB byte budget exceeds ${String(maxBytes)}`);
|
|
176
|
+
count.ops = nextOps;
|
|
177
|
+
count.writes = nextWrites;
|
|
102
178
|
count.bytes += writeBytes;
|
|
103
179
|
const status = operation(...args);
|
|
104
180
|
if (typeof status === 'number' && status >= 0) {
|
|
@@ -108,10 +184,12 @@ export function withDbLimits(imports, db) {
|
|
|
108
184
|
? (db.lastResult?.length ?? 0)
|
|
109
185
|
: 0;
|
|
110
186
|
checkDbResultSize(db, bytes);
|
|
111
|
-
if (count.bytes + bytes > maxBytes)
|
|
187
|
+
if (enforcing && count.bytes + bytes > maxBytes)
|
|
112
188
|
throw new Error(`TDL021: ${kind} cumulative DB byte budget exceeds ${String(maxBytes)}`);
|
|
113
189
|
count.bytes += bytes;
|
|
114
190
|
}
|
|
191
|
+
if (process.env.TOIL_DEV_TRACE_DB_LIMITS === '1')
|
|
192
|
+
console.info(`[toil db] ${name}: ${formatDbBudget(db)}`);
|
|
115
193
|
return status;
|
|
116
194
|
},
|
|
117
195
|
]));
|
|
@@ -46,7 +46,20 @@ export interface DbDevState {
|
|
|
46
46
|
lastResultVersion: number;
|
|
47
47
|
functionKind: DbFunctionKind;
|
|
48
48
|
writtenCollections: Set<string>;
|
|
49
|
+
changeCandidates: Map<string, {
|
|
50
|
+
database: string;
|
|
51
|
+
collection: string;
|
|
52
|
+
key: Buffer;
|
|
53
|
+
}>;
|
|
54
|
+
changeCandidateBytes: number;
|
|
55
|
+
changesOverflow: boolean;
|
|
56
|
+
deriveChanges: Buffer;
|
|
57
|
+
manualDerive: boolean;
|
|
58
|
+
knownDerives: Set<string>;
|
|
59
|
+
continuationRequested: boolean;
|
|
60
|
+
budgetKind: DbFunctionKind | null;
|
|
49
61
|
deriveId: number;
|
|
62
|
+
deriveKey: string | null;
|
|
50
63
|
pendingCheckpoints: Map<string, number>;
|
|
51
64
|
}
|
|
52
65
|
export declare function freshDbState(): DbDevState;
|
|
@@ -90,6 +103,8 @@ export interface DbSnapshot {
|
|
|
90
103
|
eventDedup: Record<string, string[]>;
|
|
91
104
|
eventPositions?: Record<string, [string, number][]>;
|
|
92
105
|
deriveCheckpoints?: Record<string, number>;
|
|
106
|
+
deriveContinuations?: Record<string, string>;
|
|
107
|
+
deriveContinuationSequence?: string;
|
|
93
108
|
capacity: Record<string, {
|
|
94
109
|
total: string;
|
|
95
110
|
nextId: string;
|
|
@@ -27,7 +27,16 @@ export function freshDbState() {
|
|
|
27
27
|
lastResultVersion: -1,
|
|
28
28
|
functionKind: DbFunctionKind.Job,
|
|
29
29
|
writtenCollections: new Set(),
|
|
30
|
+
changeCandidates: new Map(),
|
|
31
|
+
changeCandidateBytes: 0,
|
|
32
|
+
changesOverflow: false,
|
|
33
|
+
deriveChanges: Buffer.from([1, 0, 0, 0, 0]),
|
|
34
|
+
manualDerive: false,
|
|
35
|
+
knownDerives: new Set(),
|
|
36
|
+
continuationRequested: false,
|
|
37
|
+
budgetKind: null,
|
|
30
38
|
deriveId: 0,
|
|
39
|
+
deriveKey: null,
|
|
31
40
|
pendingCheckpoints: new Map(),
|
|
32
41
|
};
|
|
33
42
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { loadEnvFiles } from '../config/dotenv.js';
|
|
4
|
+
import { installDevBlobEnvironment } from '../config/env.js';
|
|
5
|
+
import { startDiskBlobServer } from './disk.js';
|
|
6
|
+
export async function startDevBlobStorage(root) {
|
|
7
|
+
const env = loadEnvFiles(root);
|
|
8
|
+
if (['BLOB_ENDPOINT', 'BLOB_BUCKET', 'BLOB_REGION'].some((key) => env.vars.has(key)) ||
|
|
9
|
+
['BLOB_ACCESS_KEY_ID', 'BLOB_SECRET_ACCESS_KEY', 'BLOB_SESSION_TOKEN'].some((key) => env.secrets.has(key)))
|
|
10
|
+
return null;
|
|
11
|
+
const store = await startDiskBlobServer({
|
|
12
|
+
directory: join(root, '.toil', 'blobs'),
|
|
13
|
+
bucket: 'toil-dev',
|
|
14
|
+
region: 'us-east-1',
|
|
15
|
+
accessKey: 'toil-dev',
|
|
16
|
+
secretKey: randomBytes(32).toString('hex'),
|
|
17
|
+
});
|
|
18
|
+
const vars = new Map(store.vars);
|
|
19
|
+
const prefix = env.vars.get('BLOB_PREFIX');
|
|
20
|
+
if (prefix !== undefined)
|
|
21
|
+
vars.set('BLOB_PREFIX', prefix);
|
|
22
|
+
const uninstall = installDevBlobEnvironment({ vars, secrets: store.secrets });
|
|
23
|
+
return {
|
|
24
|
+
async close() {
|
|
25
|
+
uninstall();
|
|
26
|
+
await store.close();
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type BlobDiskCredentials } from './signatures.js';
|
|
2
|
+
export interface DiskBlobOptions extends BlobDiskCredentials {
|
|
3
|
+
directory: string;
|
|
4
|
+
now?: () => number;
|
|
5
|
+
maxBytes?: number;
|
|
6
|
+
}
|
|
7
|
+
export interface DiskBlobServer {
|
|
8
|
+
endpoint: string;
|
|
9
|
+
vars: ReadonlyMap<string, string>;
|
|
10
|
+
secrets: ReadonlyMap<string, string>;
|
|
11
|
+
close(): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
export declare function blobRange(header: string, size: number): {
|
|
14
|
+
start: number;
|
|
15
|
+
end: number;
|
|
16
|
+
} | null;
|
|
17
|
+
export declare function startDiskBlobServer(options: DiskBlobOptions): Promise<DiskBlobServer>;
|