@runeditrun/rer 0.2.36 → 0.2.37
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/global/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.37
|
|
4
|
+
|
|
5
|
+
- **A native demo's D1 answers `withSession`, `dump()` and `meta.size_after`, as production D1 does.** In a native hosted demo, `env.DB.withSession(...)` now returns a session whose `prepare`, `batch` and `getBookmark` work against the visitor's own database. Each visitor's database is a single primary, so every constraint or bookmark is already met and the bookmark stays the same. Before, a seed that read through a session hung: Slate's feature routes never answered. `dump()` now exists and rejects with `D1_DUMP_ERROR`, as production D1 does. Before, Uptime's owner-session revoke bound `db.dump` and answered 503 `auth_unavailable`; it now answers 404 `session_not_found`, once Uptime takes this release. Query `meta` adds `size_after`, which Orbit's archive budget reads.
|
|
6
|
+
- **A native demo's D1 errors read as D1's do.** SQL errors are `D1_ERROR: <SQLite message>`, and `exec`, type and missing-column errors carry D1's codes. Each carries the original error as its `cause`. Seeds that classify errors by their D1 text, such as Mango, Oncall, Spool, Uptime and Chest, behave as they do in production.
|
|
7
|
+
- **An unknown D1 property reads `undefined`, as on real D1.** Feature detection takes its fallback. The first read of each unknown name per isolate logs `D1_UNSUPPORTED: <Kind>.<name> is not available in hosted demos` in the Worker's tail, so a seed that calls a missing method leaves a trace.
|
|
8
|
+
|
|
3
9
|
## 0.2.36
|
|
4
10
|
|
|
5
11
|
- **A native demo keeps each visitor's R2 files in that visitor's own storage.** A native hosted demo now stores R2 objects, their metadata and multipart uploads per binding in the visitor's own Durable Object storage, next to the app. Before, every visitor shared one demo-owned bucket, often far from the visitor. File reads and writes now answer from where the visitor is. Measured from Queensland on Beam: opening a file took 55 ms instead of 256 ms, uploading 207 ms instead of 1543 ms, and starting the demo 1.64 s instead of 2.65 s. One visitor's objects may total 256 MiB; a write past that fails with R2's error for an oversized upload. Expiry erases a visitor's objects along with the rest of their storage. A new demo creates no R2 bucket.
|
|
@@ -1,33 +1,46 @@
|
|
|
1
1
|
// The synchronous D1 statement builder stays inside the app isolate. Only SQL
|
|
2
2
|
// and bound values cross RPC; a batch remains one SQLite transaction.
|
|
3
3
|
export class DemoD1 {
|
|
4
|
-
constructor(sql) { this.sql = sql; }
|
|
4
|
+
constructor(sql) { this.sql = sql; return strict(this, 'D1Database'); }
|
|
5
5
|
prepare(query) { return new Statement(this, query); }
|
|
6
6
|
async batch(statements) {
|
|
7
7
|
if (statements.some(statement => statement.db !== this)) throw new Error('Foreign D1 statement');
|
|
8
|
-
return (await this.sql.batch(statements.map(({ query, args }) => ({ query, args })))).map(result);
|
|
8
|
+
return (await d1(() => this.sql.batch(statements.map(({ query, args }) => ({ query, args }))))).map(result);
|
|
9
9
|
}
|
|
10
|
-
exec(query) { return this.sql.exec(query); }
|
|
10
|
+
exec(query) { return d1(() => this.sql.exec(query), 'D1_EXEC_ERROR'); }
|
|
11
|
+
// One visitor's SQLite has no replicas: every session reads the primary,
|
|
12
|
+
// so any constraint or bookmark is already satisfied.
|
|
13
|
+
withSession(constraintOrBookmark = 'first-unconstrained') {
|
|
14
|
+
if (typeof constraintOrBookmark !== 'string' || !constraintOrBookmark) throw new Error('D1_SESSION_ERROR: invalid constraint or bookmark');
|
|
15
|
+
return strict(new Session(this), 'D1DatabaseSession');
|
|
16
|
+
}
|
|
17
|
+
async dump() { throw failure('D1_DUMP_ERROR', 'dump() is not available in hosted demos'); }
|
|
18
|
+
}
|
|
19
|
+
class Session {
|
|
20
|
+
constructor(db) { this.db = db; }
|
|
21
|
+
prepare(query) { return this.db.prepare(query); }
|
|
22
|
+
batch(statements) { return this.db.batch(statements); }
|
|
23
|
+
getBookmark() { return '00000000-00000000-00000000-rer-native-demo'; }
|
|
11
24
|
}
|
|
12
25
|
const result = value => ({ success: value.success, results: value.results, meta: value.meta });
|
|
13
26
|
class Statement {
|
|
14
|
-
constructor(db, query, args = []) { this.db = db; this.query = query; this.args = args; }
|
|
27
|
+
constructor(db, query, args = []) { this.db = db; this.query = query; this.args = args; return strict(this, 'D1PreparedStatement'); }
|
|
15
28
|
bind(...args) {
|
|
16
29
|
return new Statement(this.db, this.query, args.map(value => {
|
|
17
30
|
if (typeof value === 'boolean') return Number(value);
|
|
18
31
|
if (value === null || typeof value === 'string' || typeof value === 'number' || value instanceof ArrayBuffer || ArrayBuffer.isView(value)) return value;
|
|
19
32
|
if (Array.isArray(value) && value.every(byte => Number.isInteger(byte) && byte >= 0 && byte <= 255)) return new Uint8Array(value);
|
|
20
|
-
throw
|
|
33
|
+
throw failure('D1_TYPE_ERROR', `Type '${typeof value}' not supported for value '${value}'`);
|
|
21
34
|
}));
|
|
22
35
|
}
|
|
23
|
-
execute() { return this.db.sql.query(this.query, this.args); }
|
|
36
|
+
execute() { return d1(() => this.db.sql.query(this.query, this.args)); }
|
|
24
37
|
async all() { return result(await this.execute()); }
|
|
25
38
|
run() { return this.all(); }
|
|
26
39
|
async first(column) {
|
|
27
40
|
const row = (await this.execute()).results[0];
|
|
28
41
|
if (!row) return null;
|
|
29
42
|
if (column === undefined) return row;
|
|
30
|
-
if (!Object.hasOwn(row, column)) throw new Error(`D1_COLUMN_NOTFOUND: ${column}
|
|
43
|
+
if (!Object.hasOwn(row, column)) throw new Error(`D1_COLUMN_NOTFOUND: Column not found (${column})`, { cause: new Error('Column not found') });
|
|
31
44
|
return row[column];
|
|
32
45
|
}
|
|
33
46
|
async raw({ columnNames = false } = {}) {
|
|
@@ -35,3 +48,28 @@ class Statement {
|
|
|
35
48
|
return columnNames ? [columns, ...rows] : rows;
|
|
36
49
|
}
|
|
37
50
|
}
|
|
51
|
+
// Errors read as D1's do ("D1_ERROR: UNIQUE constraint failed: ...: SQLITE_CONSTRAINT"),
|
|
52
|
+
// with the unprefixed error as their cause.
|
|
53
|
+
const failure = (code, message, cause = new Error(message)) => new Error(`${code}: ${message}`, { cause });
|
|
54
|
+
async function d1(operation, code = 'D1_ERROR') {
|
|
55
|
+
try { return await operation(); }
|
|
56
|
+
catch (error) {
|
|
57
|
+
const message = String(error?.message ?? error);
|
|
58
|
+
throw /^D1_[A-Z_]+:/.test(message) ? error : failure(code, message, error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// An unknown property reads undefined, as on real D1, so feature detection
|
|
62
|
+
// takes its fallback. The first read of each unknown name per isolate warns in
|
|
63
|
+
// the Worker tail: a seed calling a method this facade lacks inside its own
|
|
64
|
+
// queueMicrotask (Slate's read batching) otherwise hangs with no trace.
|
|
65
|
+
const warned = new Set();
|
|
66
|
+
function strict(target, kind) {
|
|
67
|
+
return new Proxy(target, { get(object, key, receiver) {
|
|
68
|
+
const name = `${kind}.${String(key)}`;
|
|
69
|
+
if (typeof key === 'string' && !(key in object) && key !== 'then' && key !== 'toJSON' && !warned.has(name)) {
|
|
70
|
+
warned.add(name);
|
|
71
|
+
console.warn(`D1_UNSUPPORTED: ${name} is not available in hosted demos`);
|
|
72
|
+
}
|
|
73
|
+
return Reflect.get(object, key, receiver);
|
|
74
|
+
} });
|
|
75
|
+
}
|
|
@@ -10,7 +10,8 @@ export function sqlite(storage) {
|
|
|
10
10
|
const after = read ? { n: 0, id: lastRowId } : storage.sql.exec('SELECT total_changes() AS n, last_insert_rowid() AS id').one();
|
|
11
11
|
lastRowId = after.id;
|
|
12
12
|
return { success: true, results: rows.map(row => Object.fromEntries(columns.map((column, index) => [column, row[index]]))), columns, rows,
|
|
13
|
-
meta: { duration: performance.now() - started, changes: after.n - before, last_row_id: after.id, changed_db: after.n !== before, rows_read: cursor.rowsRead, rows_written: cursor.rowsWritten
|
|
13
|
+
meta: { duration: performance.now() - started, changes: after.n - before, last_row_id: after.id, changed_db: after.n !== before, rows_read: cursor.rowsRead, rows_written: cursor.rowsWritten,
|
|
14
|
+
size_after: storage.sql.databaseSize, served_by: 'rer-native-demo', served_by_primary: true } };
|
|
14
15
|
}
|
|
15
16
|
return { query, batch: statements => storage.transactionSync(() => statements.map(statement => query(statement.query, statement.args))),
|
|
16
17
|
exec(query) { const started = performance.now(); storage.sql.exec(query).toArray(); return { count: query.split('\n').filter(line => line.trim()).length, duration: performance.now() - started }; } };
|