@statewalker/cellb 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +323 -2
- package/dist/adapters/files-backup.d.ts +16 -0
- package/dist/adapters/files-backup.js +32 -0
- package/dist/adapters/files-backup.js.map +1 -0
- package/dist/adapters/files-changelog.d.ts +33 -0
- package/dist/adapters/files-changelog.js +96 -0
- package/dist/adapters/files-changelog.js.map +1 -0
- package/dist/adapters/files-ltx.d.ts +35 -0
- package/dist/adapters/files-ltx.js +9 -0
- package/dist/adapters/files-ltx.js.map +1 -0
- package/dist/adapters/ltx-vfs.d.ts +21 -0
- package/dist/adapters/ltx-vfs.js +286 -0
- package/dist/adapters/ltx-vfs.js.map +1 -0
- package/dist/adapters/node-sqlite.d.ts +45 -0
- package/dist/adapters/node-sqlite.js +254 -0
- package/dist/adapters/node-sqlite.js.map +1 -0
- package/dist/adapters/wasm-page-hook.d.ts +26 -0
- package/dist/adapters/wasm-page-hook.js +55 -0
- package/dist/adapters/wasm-page-hook.js.map +1 -0
- package/dist/adapters/wasm-session.d.ts +11 -0
- package/dist/adapters/wasm-session.js +76 -0
- package/dist/adapters/wasm-session.js.map +1 -0
- package/dist/adapters/wasm-sqlite.d.ts +89 -0
- package/dist/adapters/wasm-sqlite.js +258 -0
- package/dist/adapters/wasm-sqlite.js.map +1 -0
- package/dist/bindings/assets.d.ts +26 -0
- package/dist/bindings/assets.js +260 -0
- package/dist/bindings/assets.js.map +1 -0
- package/dist/bindings/cache.d.ts +16 -0
- package/dist/bindings/cache.js +34 -0
- package/dist/bindings/cache.js.map +1 -0
- package/dist/bindings/celld-kv.d.ts +26 -0
- package/dist/bindings/celld-kv.js +81 -0
- package/dist/bindings/celld-kv.js.map +1 -0
- package/dist/bindings/celld-queues.d.ts +56 -0
- package/dist/bindings/celld-queues.js +223 -0
- package/dist/bindings/celld-queues.js.map +1 -0
- package/dist/bindings/celld-workflows.d.ts +22 -0
- package/dist/bindings/celld-workflows.js +54 -0
- package/dist/bindings/celld-workflows.js.map +1 -0
- package/dist/bindings/cron.d.ts +27 -0
- package/dist/bindings/cron.js +232 -0
- package/dist/bindings/cron.js.map +1 -0
- package/dist/bindings/d1-migrations.d.ts +22 -0
- package/dist/bindings/d1-migrations.js +32 -0
- package/dist/bindings/d1-migrations.js.map +1 -0
- package/dist/bindings/html-rewriter.d.ts +8 -0
- package/dist/bindings/html-rewriter.js +75 -0
- package/dist/bindings/html-rewriter.js.map +1 -0
- package/dist/bindings/r2-celld.d.ts +3 -0
- package/dist/bindings/r2-celld.js +113 -0
- package/dist/bindings/r2-celld.js.map +1 -0
- package/dist/bindings/r2.d.ts +236 -0
- package/dist/bindings/r2.js +419 -0
- package/dist/bindings/r2.js.map +1 -0
- package/dist/bindings/worker-loader.d.ts +83 -0
- package/dist/bindings/worker-loader.js +183 -0
- package/dist/bindings/worker-loader.js.map +1 -0
- package/dist/bindings/workers-ai.d.ts +39 -0
- package/dist/bindings/workers-ai.js +175 -0
- package/dist/bindings/workers-ai.js.map +1 -0
- package/dist/browser.d.ts +7 -0
- package/dist/browser.js +8 -0
- package/dist/browser.js.map +1 -0
- package/dist/bucket/celld-cells.d.ts +86 -0
- package/dist/bucket/celld-cells.js +273 -0
- package/dist/bucket/celld-cells.js.map +1 -0
- package/dist/bucket/celld-format.d.ts +10 -0
- package/dist/bucket/celld-format.js +27 -0
- package/dist/bucket/celld-format.js.map +1 -0
- package/dist/bucket/idb-object-store.d.ts +9 -0
- package/dist/bucket/idb-object-store.js +73 -0
- package/dist/bucket/idb-object-store.js.map +1 -0
- package/dist/bucket/ltx-log.d.ts +34 -0
- package/dist/bucket/ltx-log.js +289 -0
- package/dist/bucket/ltx-log.js.map +1 -0
- package/dist/bucket/object-store.d.ts +51 -0
- package/dist/bucket/object-store.js +114 -0
- package/dist/bucket/object-store.js.map +1 -0
- package/dist/bucket/s3-object-store.d.ts +25 -0
- package/dist/bucket/s3-object-store.js +151 -0
- package/dist/bucket/s3-object-store.js.map +1 -0
- package/dist/bucket/sqlite-object-store.d.ts +10 -0
- package/dist/bucket/sqlite-object-store.js +107 -0
- package/dist/bucket/sqlite-object-store.js.map +1 -0
- package/dist/core/alarms.d.ts +27 -0
- package/dist/core/alarms.js +88 -0
- package/dist/core/alarms.js.map +1 -0
- package/dist/core/async-context.d.ts +34 -0
- package/dist/core/async-context.js +50 -0
- package/dist/core/async-context.js.map +1 -0
- package/dist/core/celld-schema.d.ts +14 -0
- package/dist/core/celld-schema.js +34 -0
- package/dist/core/celld-schema.js.map +1 -0
- package/dist/core/durable-object.d.ts +28 -0
- package/dist/core/durable-object.js +31 -0
- package/dist/core/durable-object.js.map +1 -0
- package/dist/core/gates.d.ts +33 -0
- package/dist/core/gates.js +89 -0
- package/dist/core/gates.js.map +1 -0
- package/dist/core/handles.d.ts +90 -0
- package/dist/core/handles.js +224 -0
- package/dist/core/handles.js.map +1 -0
- package/dist/core/id.d.ts +19 -0
- package/dist/core/id.js +64 -0
- package/dist/core/id.js.map +1 -0
- package/dist/core/namespace.d.ts +38 -0
- package/dist/core/namespace.js +141 -0
- package/dist/core/namespace.js.map +1 -0
- package/dist/core/object-host.d.ts +91 -0
- package/dist/core/object-host.js +616 -0
- package/dist/core/object-host.js.map +1 -0
- package/dist/core/request-body.d.ts +9 -0
- package/dist/core/request-body.js +55 -0
- package/dist/core/request-body.js.map +1 -0
- package/dist/core/runtime.d.ts +121 -0
- package/dist/core/runtime.js +366 -0
- package/dist/core/runtime.js.map +1 -0
- package/dist/core/shard.d.ts +7 -0
- package/dist/core/shard.js +10 -0
- package/dist/core/shard.js.map +1 -0
- package/dist/core/sql-split.d.ts +15 -0
- package/dist/core/sql-split.js +101 -0
- package/dist/core/sql-split.js.map +1 -0
- package/dist/core/sql-storage.d.ts +53 -0
- package/dist/core/sql-storage.js +407 -0
- package/dist/core/sql-storage.js.map +1 -0
- package/dist/core/state.d.ts +44 -0
- package/dist/core/state.js +93 -0
- package/dist/core/state.js.map +1 -0
- package/dist/core/storage.d.ts +103 -0
- package/dist/core/storage.js +351 -0
- package/dist/core/storage.js.map +1 -0
- package/dist/core/worker-rejections.d.ts +21 -0
- package/dist/core/worker-rejections.js +55 -0
- package/dist/core/worker-rejections.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/isolate/browser.d.ts +16 -0
- package/dist/isolate/browser.js +37 -0
- package/dist/isolate/browser.js.map +1 -0
- package/dist/isolate/host.d.ts +24 -0
- package/dist/isolate/host.js +74 -0
- package/dist/isolate/host.js.map +1 -0
- package/dist/isolate/isolate-worker.d.ts +1 -0
- package/dist/isolate/isolate-worker.js +53 -0
- package/dist/isolate/isolate-worker.js.map +1 -0
- package/dist/isolate/isolate.d.ts +10 -0
- package/dist/isolate/isolate.js +169 -0
- package/dist/isolate/isolate.js.map +1 -0
- package/dist/isolate/node-entry.d.ts +1 -0
- package/dist/isolate/node-entry.js +12 -0
- package/dist/isolate/node-entry.js.map +1 -0
- package/dist/isolate/protocol.d.ts +66 -0
- package/dist/isolate/protocol.js +205 -0
- package/dist/isolate/protocol.js.map +1 -0
- package/dist/ltx/crc64.d.ts +6 -0
- package/dist/ltx/crc64.js +46 -0
- package/dist/ltx/crc64.js.map +1 -0
- package/dist/ltx/ltx-page-server.d.ts +65 -0
- package/dist/ltx/ltx-page-server.js +309 -0
- package/dist/ltx/ltx-page-server.js.map +1 -0
- package/dist/ltx/ltx-pages.d.ts +81 -0
- package/dist/ltx/ltx-pages.js +315 -0
- package/dist/ltx/ltx-pages.js.map +1 -0
- package/dist/ltx/ltx-store-worker.d.ts +1 -0
- package/dist/ltx/ltx-store-worker.js +38 -0
- package/dist/ltx/ltx-store-worker.js.map +1 -0
- package/dist/ltx/ltx.d.ts +45 -0
- package/dist/ltx/ltx.js +203 -0
- package/dist/ltx/ltx.js.map +1 -0
- package/dist/ltx/lz4.d.ts +9 -0
- package/dist/ltx/lz4.js +156 -0
- package/dist/ltx/lz4.js.map +1 -0
- package/dist/ltx/opfs-sync-files.d.ts +23 -0
- package/dist/ltx/opfs-sync-files.js +191 -0
- package/dist/ltx/opfs-sync-files.js.map +1 -0
- package/dist/ltx/xxh32.d.ts +1 -0
- package/dist/ltx/xxh32.js +43 -0
- package/dist/ltx/xxh32.js.map +1 -0
- package/dist/miniflare-compat/blob-service.d.ts +13 -0
- package/dist/miniflare-compat/blob-service.js +66 -0
- package/dist/miniflare-compat/blob-service.js.map +1 -0
- package/dist/miniflare-compat/kv-binding.d.ts +44 -0
- package/dist/miniflare-compat/kv-binding.js +99 -0
- package/dist/miniflare-compat/kv-binding.js.map +1 -0
- package/dist/miniflare-compat/shims/assert.d.ts +15 -0
- package/dist/miniflare-compat/shims/assert.js +24 -0
- package/dist/miniflare-compat/shims/assert.js.map +1 -0
- package/dist/miniflare-compat/shims/workerd-cloudflare.d.ts +7 -0
- package/dist/miniflare-compat/shims/workerd-cloudflare.js +6 -0
- package/dist/miniflare-compat/shims/workerd-cloudflare.js.map +1 -0
- package/dist/miniflare-compat/shims/workerd-globals.d.ts +1 -0
- package/dist/miniflare-compat/shims/workerd-globals.js +30 -0
- package/dist/miniflare-compat/shims/workerd-globals.js.map +1 -0
- package/dist/miniflare-compat/shims/workerd-internal.d.ts +20 -0
- package/dist/miniflare-compat/shims/workerd-internal.js +23 -0
- package/dist/miniflare-compat/shims/workerd-internal.js.map +1 -0
- package/dist/miniflare-compat/shims/workerd-performance.d.ts +4 -0
- package/dist/miniflare-compat/shims/workerd-performance.js +17 -0
- package/dist/miniflare-compat/shims/workerd-performance.js.map +1 -0
- package/dist/miniflare-compat/simulator-fetcher.d.ts +9 -0
- package/dist/miniflare-compat/simulator-fetcher.js +14 -0
- package/dist/miniflare-compat/simulator-fetcher.js.map +1 -0
- package/dist/miniflare-compat/vendored.d.ts +15 -0
- package/dist/miniflare-compat/vendored.js +27 -0
- package/dist/miniflare-compat/vendored.js.map +1 -0
- package/dist/node-compat/async_hooks.d.ts +22 -0
- package/dist/node-compat/async_hooks.js +176 -0
- package/dist/node-compat/async_hooks.js.map +1 -0
- package/dist/node-compat/browser-registry.d.ts +3 -0
- package/dist/node-compat/browser-registry.js +39 -0
- package/dist/node-compat/browser-registry.js.map +1 -0
- package/dist/node-compat/crypto.d.ts +60 -0
- package/dist/node-compat/crypto.js +143 -0
- package/dist/node-compat/crypto.js.map +1 -0
- package/dist/node-compat/diagnostics_channel.d.ts +27 -0
- package/dist/node-compat/diagnostics_channel.js +46 -0
- package/dist/node-compat/diagnostics_channel.js.map +1 -0
- package/dist/node-compat/fs.d.ts +35 -0
- package/dist/node-compat/fs.js +40 -0
- package/dist/node-compat/fs.js.map +1 -0
- package/dist/node-compat/globals.d.ts +3 -0
- package/dist/node-compat/globals.js +4 -0
- package/dist/node-compat/globals.js.map +1 -0
- package/dist/node-compat/install.d.ts +5 -0
- package/dist/node-compat/install.js +12 -0
- package/dist/node-compat/install.js.map +1 -0
- package/dist/node-compat/module.d.ts +12 -0
- package/dist/node-compat/module.js +36 -0
- package/dist/node-compat/module.js.map +1 -0
- package/dist/node-compat/modules.d.ts +8 -0
- package/dist/node-compat/modules.js +23 -0
- package/dist/node-compat/modules.js.map +1 -0
- package/dist/node-compat/os.d.ts +36 -0
- package/dist/node-compat/os.js +36 -0
- package/dist/node-compat/os.js.map +1 -0
- package/dist/node-compat/process.d.ts +14 -0
- package/dist/node-compat/process.js +16 -0
- package/dist/node-compat/process.js.map +1 -0
- package/dist/node-compat/shims.d.ts +4 -0
- package/dist/node.d.ts +5 -0
- package/dist/node.js +6 -0
- package/dist/node.js.map +1 -0
- package/dist/peer/celld-peer.d.ts +53 -0
- package/dist/peer/celld-peer.js +555 -0
- package/dist/peer/celld-peer.js.map +1 -0
- package/dist/ports.d.ts +183 -0
- package/dist/ports.js +10 -0
- package/dist/ports.js.map +1 -0
- package/dist/project/browser-bundle.d.ts +17 -0
- package/dist/project/browser-bundle.js +125 -0
- package/dist/project/browser-bundle.js.map +1 -0
- package/dist/project/browser-cookies.d.ts +12 -0
- package/dist/project/browser-cookies.js +90 -0
- package/dist/project/browser-cookies.js.map +1 -0
- package/dist/project/browser-host.d.ts +173 -0
- package/dist/project/browser-host.js +200 -0
- package/dist/project/browser-host.js.map +1 -0
- package/dist/project/browser-modules.d.ts +17 -0
- package/dist/project/browser-modules.js +89 -0
- package/dist/project/browser-modules.js.map +1 -0
- package/dist/project/browser-shards.d.ts +62 -0
- package/dist/project/browser-shards.js +210 -0
- package/dist/project/browser-shards.js.map +1 -0
- package/dist/project/browser-tabs.d.ts +103 -0
- package/dist/project/browser-tabs.js +418 -0
- package/dist/project/browser-tabs.js.map +1 -0
- package/dist/project/cloudflare-email.d.ts +8 -0
- package/dist/project/cloudflare-email.js +11 -0
- package/dist/project/cloudflare-email.js.map +1 -0
- package/dist/project/cloudflare-workers.d.ts +12 -0
- package/dist/project/cloudflare-workers.js +16 -0
- package/dist/project/cloudflare-workers.js.map +1 -0
- package/dist/project/host-modules.d.ts +4 -0
- package/dist/project/host-modules.js +40 -0
- package/dist/project/host-modules.js.map +1 -0
- package/dist/project/jsonc.d.ts +2 -0
- package/dist/project/jsonc.js +27 -0
- package/dist/project/jsonc.js.map +1 -0
- package/dist/project/lower-await.d.ts +2 -0
- package/dist/project/lower-await.js +151 -0
- package/dist/project/lower-await.js.map +1 -0
- package/dist/project/node-project.d.ts +38 -0
- package/dist/project/node-project.js +362 -0
- package/dist/project/node-project.js.map +1 -0
- package/dist/project/own-module.d.ts +6 -0
- package/dist/project/own-module.js +13 -0
- package/dist/project/own-module.js.map +1 -0
- package/dist/project/scheduler.d.ts +2 -0
- package/dist/project/scheduler.js +3 -0
- package/dist/project/scheduler.js.map +1 -0
- package/dist/project/site.d.ts +74 -0
- package/dist/project/site.js +206 -0
- package/dist/project/site.js.map +1 -0
- package/dist/project/virtual-modules.d.ts +3 -0
- package/dist/project/worker-scope.d.ts +27 -0
- package/dist/project/worker-scope.js +46 -0
- package/dist/project/worker-scope.js.map +1 -0
- package/dist/project/workerd-message-channel.d.ts +13 -0
- package/dist/project/workerd-message-channel.js +137 -0
- package/dist/project/workerd-message-channel.js.map +1 -0
- package/dist/project/workers-global-names.d.ts +6 -0
- package/dist/project/workers-global-names.js +15 -0
- package/dist/project/workers-global-names.js.map +1 -0
- package/dist/project/workers-globals-module.d.ts +8 -0
- package/dist/project/workers-globals-module.js +9 -0
- package/dist/project/workers-globals-module.js.map +1 -0
- package/dist/project/workers-globals.d.ts +9 -0
- package/dist/project/workers-globals.js +19 -0
- package/dist/project/workers-globals.js.map +1 -0
- package/dist/project/wrangler.d.ts +116 -0
- package/dist/project/wrangler.js +342 -0
- package/dist/project/wrangler.js.map +1 -0
- package/dist/router/cell-router.d.ts +72 -0
- package/dist/router/cell-router.js +247 -0
- package/dist/router/cell-router.js.map +1 -0
- package/dist/router/shard-worker.d.ts +44 -0
- package/dist/router/shard-worker.js +163 -0
- package/dist/router/shard-worker.js.map +1 -0
- package/dist/transport/cells-http.d.ts +25 -0
- package/dist/transport/cells-http.js +141 -0
- package/dist/transport/cells-http.js.map +1 -0
- package/dist/transport/cells-link.d.ts +27 -0
- package/dist/transport/cells-link.js +71 -0
- package/dist/transport/cells-link.js.map +1 -0
- package/dist/transport/port.d.ts +26 -0
- package/dist/transport/port.js +100 -0
- package/dist/transport/port.js.map +1 -0
- package/dist/v8/codec.d.ts +6 -0
- package/dist/v8/codec.js +9 -0
- package/dist/v8/codec.js.map +1 -0
- package/dist/v8/serialize.d.ts +10 -0
- package/dist/v8/serialize.js +601 -0
- package/dist/v8/serialize.js.map +1 -0
- package/dist/ws/client.d.ts +25 -0
- package/dist/ws/client.js +203 -0
- package/dist/ws/client.js.map +1 -0
- package/dist/ws/control.d.ts +48 -0
- package/dist/ws/control.js +44 -0
- package/dist/ws/control.js.map +1 -0
- package/dist/ws/frames.d.ts +38 -0
- package/dist/ws/frames.js +39 -0
- package/dist/ws/frames.js.map +1 -0
- package/dist/ws/server.d.ts +99 -0
- package/dist/ws/server.js +295 -0
- package/dist/ws/server.js.map +1 -0
- package/dist/ws/sw-connector.d.ts +10 -0
- package/dist/ws/sw-connector.js +25 -0
- package/dist/ws/sw-connector.js.map +1 -0
- package/dist/ws/upgrade.d.ts +28 -0
- package/dist/ws/upgrade.js +75 -0
- package/dist/ws/upgrade.js.map +1 -0
- package/package.json +111 -3
- package/vendor/celld/LICENSE +202 -0
- package/vendor/celld/kv.js +1131 -0
- package/vendor/celld/queues.js +1656 -0
- package/vendor/celld/workflows.js +1610 -0
- package/vendor/html-rewriter-wasm/html_rewriter.js +925 -0
- package/vendor/html-rewriter-wasm/html_rewriter_bg.wasm +0 -0
- package/vendor/miniflare/d1-api.js +738 -0
- package/vendor/miniflare/d1-database.js +6978 -0
- package/vendor/miniflare/kv-namespace.js +3061 -0
|
@@ -0,0 +1,738 @@
|
|
|
1
|
+
// GENERATED by scripts/vendor-miniflare.mjs from cloudflare/workerd@2e36a384412f6eb5ccec513635728041b5da9533 src/cloudflare/internal/d1-api.ts (Apache-2.0). Do not edit.
|
|
2
|
+
|
|
3
|
+
// src/miniflare-compat/shims/workerd-globals.ts
|
|
4
|
+
var g = globalThis;
|
|
5
|
+
if (typeof g.IdentityTransformStream === "undefined") {
|
|
6
|
+
g.IdentityTransformStream = class IdentityTransformStream extends TransformStream {
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
var BYOB = globalThis.ReadableStreamBYOBReader;
|
|
10
|
+
if (BYOB && typeof BYOB.prototype.readAtLeast !== "function") {
|
|
11
|
+
BYOB.prototype.readAtLeast = async function readAtLeast(min, view) {
|
|
12
|
+
let offset = 0;
|
|
13
|
+
let buffer = view.buffer;
|
|
14
|
+
while (offset < min) {
|
|
15
|
+
const { value, done } = await this.read(
|
|
16
|
+
new Uint8Array(buffer, view.byteOffset + offset, view.byteLength - offset)
|
|
17
|
+
);
|
|
18
|
+
if (value) {
|
|
19
|
+
buffer = value.buffer;
|
|
20
|
+
offset += value.byteLength;
|
|
21
|
+
}
|
|
22
|
+
if (done)
|
|
23
|
+
return { value: new Uint8Array(buffer, view.byteOffset, offset), done: offset === 0 };
|
|
24
|
+
}
|
|
25
|
+
return { value: new Uint8Array(buffer, view.byteOffset, offset), done: false };
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/miniflare-compat/shims/workerd-performance.ts
|
|
30
|
+
var real = globalThis.performance;
|
|
31
|
+
|
|
32
|
+
// src/miniflare-compat/shims/workerd-cloudflare.ts
|
|
33
|
+
var Cloudflare = { compatibilityFlags: {} };
|
|
34
|
+
|
|
35
|
+
// src/miniflare-compat/shims/workerd-internal.ts
|
|
36
|
+
var WrappedBinding = class {
|
|
37
|
+
// biome-ignore lint/complexity/noUselessConstructor: mirrors workerd's signature
|
|
38
|
+
constructor(_inner) {
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var workerd_internal_default = { WrappedBinding };
|
|
42
|
+
var noopSpan = {
|
|
43
|
+
setAttribute() {
|
|
44
|
+
},
|
|
45
|
+
setAttributes() {
|
|
46
|
+
},
|
|
47
|
+
end() {
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
function withSpan(_name, fn) {
|
|
51
|
+
return fn(noopSpan);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ../../../../../tmp/wd/src/cloudflare/internal/d1-api.ts
|
|
55
|
+
var d1BindingJsrpc = !!Cloudflare.compatibilityFlags["d1_binding_jsrpc"];
|
|
56
|
+
var D1_SESSION_CONSTRAINT_FIRST_PRIMARY = "first-primary";
|
|
57
|
+
var D1_SESSION_CONSTRAINT_FIRST_UNCONSTRAINED = "first-unconstrained";
|
|
58
|
+
var D1_SESSION_COMMIT_TOKEN_HTTP_HEADER = "x-cf-d1-session-commit-token";
|
|
59
|
+
var D1Database = class extends workerd_internal_default.WrappedBinding {
|
|
60
|
+
// TODO(soon): Can we use the # syntax here?
|
|
61
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
62
|
+
alwaysPrimarySession;
|
|
63
|
+
fetcher;
|
|
64
|
+
constructor(fetcher) {
|
|
65
|
+
super(fetcher);
|
|
66
|
+
this.fetcher = fetcher;
|
|
67
|
+
this.alwaysPrimarySession = new D1DatabaseSessionAlwaysPrimary(
|
|
68
|
+
this.fetcher
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
prepare(query) {
|
|
72
|
+
return new D1PreparedStatement(this.alwaysPrimarySession, query);
|
|
73
|
+
}
|
|
74
|
+
async batch(statements) {
|
|
75
|
+
return this.alwaysPrimarySession.batch(statements);
|
|
76
|
+
}
|
|
77
|
+
async exec(query) {
|
|
78
|
+
return this.alwaysPrimarySession.exec(query);
|
|
79
|
+
}
|
|
80
|
+
withSession(constraintOrBookmark) {
|
|
81
|
+
constraintOrBookmark = constraintOrBookmark?.trim();
|
|
82
|
+
if (constraintOrBookmark == null || constraintOrBookmark === "") {
|
|
83
|
+
constraintOrBookmark = D1_SESSION_CONSTRAINT_FIRST_UNCONSTRAINED;
|
|
84
|
+
}
|
|
85
|
+
return new D1DatabaseSession(this.fetcher, constraintOrBookmark);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Send one or more SQL queries directly to D1 and return raw rows-and-columns results.
|
|
89
|
+
*
|
|
90
|
+
* Unlike `prepare().all()`, `prepare().raw()`, `prepare().first()`, or
|
|
91
|
+
* `batch()`, this method does not reshape rows into objects or apply the
|
|
92
|
+
* public convenience return types. It returns the lower-level D1 response
|
|
93
|
+
* format so callers can inspect result metadata and manage session bookmarks
|
|
94
|
+
* directly.
|
|
95
|
+
*
|
|
96
|
+
* Successful responses contain `results` and an optional `bookmark`. Errors
|
|
97
|
+
* are thrown without binding-level wrapping.
|
|
98
|
+
* Legacy RpcResponse support is temporary (CFSQL-1814) and will be removed
|
|
99
|
+
* once the D1 eyeball entrypoint returns direct successes and throws errors.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```
|
|
103
|
+
* await env.D1.query({
|
|
104
|
+
* queries: [{ sql: "select * from users where id=?", params: [1] }],
|
|
105
|
+
* bookmark: "<insert_bookmark>"
|
|
106
|
+
* });
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
async query(request) {
|
|
110
|
+
if (!d1BindingJsrpc) {
|
|
111
|
+
const message = "D1Database.query() requires the d1_binding_jsrpc compatibility flag. Enable d1_binding_jsrpc before calling query().";
|
|
112
|
+
throw new Error(`D1_ERROR: ${message}`);
|
|
113
|
+
}
|
|
114
|
+
return await fetcherQuery(this.fetcher, request);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* @deprecated
|
|
118
|
+
*/
|
|
119
|
+
async dump() {
|
|
120
|
+
return this.alwaysPrimarySession.dump();
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
var D1DatabaseSession = class {
|
|
124
|
+
fetcher;
|
|
125
|
+
bookmarkOrConstraint;
|
|
126
|
+
constructor(fetcher, bookmarkOrConstraint) {
|
|
127
|
+
this.fetcher = fetcher;
|
|
128
|
+
if (bookmarkOrConstraint == null) {
|
|
129
|
+
this.bookmarkOrConstraint = null;
|
|
130
|
+
} else {
|
|
131
|
+
this.bookmarkOrConstraint = bookmarkOrConstraint.trim();
|
|
132
|
+
if (!this.bookmarkOrConstraint) {
|
|
133
|
+
throw new Error("D1_SESSION_ERROR: invalid bookmark or constraint");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// Update the bookmark IFF the given newBookmark is more recent.
|
|
138
|
+
// The bookmark held in the session should always be the latest value we
|
|
139
|
+
// have observed in the responses to our API. There can be cases where we have concurrent
|
|
140
|
+
// queries running within the same session, and therefore here we ensure we only
|
|
141
|
+
// retain the latest bookmark received.
|
|
142
|
+
// @returns the final bookmark after the update.
|
|
143
|
+
_updateBookmark(newBookmark) {
|
|
144
|
+
newBookmark = newBookmark.trim();
|
|
145
|
+
if (!newBookmark) {
|
|
146
|
+
return this.getBookmark();
|
|
147
|
+
}
|
|
148
|
+
const currentBookmark = this.getBookmark();
|
|
149
|
+
if (currentBookmark === null || currentBookmark.localeCompare(newBookmark) < 0) {
|
|
150
|
+
this.bookmarkOrConstraint = newBookmark;
|
|
151
|
+
}
|
|
152
|
+
return this.getBookmark();
|
|
153
|
+
}
|
|
154
|
+
prepare(sql) {
|
|
155
|
+
return new D1PreparedStatement(this, sql);
|
|
156
|
+
}
|
|
157
|
+
async batch(statements) {
|
|
158
|
+
return withSpan("d1_batch", async (span) => {
|
|
159
|
+
span.setAttributes({
|
|
160
|
+
"db.system.name": "cloudflare-d1",
|
|
161
|
+
"db.operation.name": "batch",
|
|
162
|
+
"db.query.text": (
|
|
163
|
+
// TODO: Joining all the statement without truncating it can lead to truncated
|
|
164
|
+
// span tags. We should find out if that's the case and where is the limit, and
|
|
165
|
+
// then decide if we need to truncate it ourselves.
|
|
166
|
+
statements.map((s) => s.statement).join("\n")
|
|
167
|
+
),
|
|
168
|
+
"db.operation.batch.size": statements.length,
|
|
169
|
+
"cloudflare.binding.type": "D1",
|
|
170
|
+
"cloudflare.d1.query.bookmark": this.getBookmark() ?? void 0
|
|
171
|
+
});
|
|
172
|
+
const exec = d1BindingJsrpc ? await this._queryOrThrow(
|
|
173
|
+
{
|
|
174
|
+
queries: statements.map((s) => ({
|
|
175
|
+
sql: s.statement,
|
|
176
|
+
params: s.params
|
|
177
|
+
}))
|
|
178
|
+
},
|
|
179
|
+
span
|
|
180
|
+
) : await this._sendOrThrow(
|
|
181
|
+
"/query",
|
|
182
|
+
statements.map((s) => s.statement),
|
|
183
|
+
statements.map((s) => s.params),
|
|
184
|
+
"ROWS_AND_COLUMNS",
|
|
185
|
+
span
|
|
186
|
+
);
|
|
187
|
+
span.setAttributes({
|
|
188
|
+
"cloudflare.d1.response.bookmark": this.getBookmark() ?? void 0
|
|
189
|
+
});
|
|
190
|
+
addAggregatedD1MetaToSpan(
|
|
191
|
+
span,
|
|
192
|
+
exec.map((e) => e.meta)
|
|
193
|
+
);
|
|
194
|
+
return exec.map(toArrayOfObjects);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
// Returns the latest bookmark we received from all responses processed so far.
|
|
198
|
+
// It does not return constraints that might have be passed during the session creation.
|
|
199
|
+
getBookmark() {
|
|
200
|
+
switch (this.bookmarkOrConstraint) {
|
|
201
|
+
// First to any replica, and then anywhere that satisfies the bookmark.
|
|
202
|
+
case D1_SESSION_CONSTRAINT_FIRST_UNCONSTRAINED:
|
|
203
|
+
return null;
|
|
204
|
+
// First to primary, and then anywhere that satisfies the bookmark.
|
|
205
|
+
case D1_SESSION_CONSTRAINT_FIRST_PRIMARY:
|
|
206
|
+
return null;
|
|
207
|
+
default:
|
|
208
|
+
return this.bookmarkOrConstraint;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
// fetch will append the bookmark header to all outgoing fetch calls.
|
|
212
|
+
// The response headers are parsed automatically, extracting the bookmark
|
|
213
|
+
// from the response headers and updating it through `_updateBookmark(token)`.
|
|
214
|
+
async _wrappedFetch(input, init) {
|
|
215
|
+
const h = new Headers(init?.headers);
|
|
216
|
+
if (this.bookmarkOrConstraint) {
|
|
217
|
+
h.set(D1_SESSION_COMMIT_TOKEN_HTTP_HEADER, this.bookmarkOrConstraint);
|
|
218
|
+
}
|
|
219
|
+
if (!init) {
|
|
220
|
+
init = { headers: h };
|
|
221
|
+
} else {
|
|
222
|
+
init.headers = h;
|
|
223
|
+
}
|
|
224
|
+
return this.fetcher.fetch(input, init).then((resp) => {
|
|
225
|
+
const newBookmark = resp.headers.get(D1_SESSION_COMMIT_TOKEN_HTTP_HEADER);
|
|
226
|
+
if (newBookmark) {
|
|
227
|
+
this._updateBookmark(newBookmark);
|
|
228
|
+
}
|
|
229
|
+
return resp;
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
async _sendOrThrow(endpoint, query, params, resultsFormat, span) {
|
|
233
|
+
const results = await this._send(
|
|
234
|
+
endpoint,
|
|
235
|
+
query,
|
|
236
|
+
params,
|
|
237
|
+
resultsFormat,
|
|
238
|
+
span
|
|
239
|
+
);
|
|
240
|
+
const firstResult = firstIfArray(results);
|
|
241
|
+
if (!firstResult.success) {
|
|
242
|
+
span.setAttribute("error.type", firstResult.error);
|
|
243
|
+
throw new Error(`D1_ERROR: ${firstResult.error}`, {
|
|
244
|
+
cause: new Error(firstResult.error)
|
|
245
|
+
});
|
|
246
|
+
} else {
|
|
247
|
+
return results;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
async _send(endpoint, query, params, resultsFormat, span) {
|
|
251
|
+
const body = JSON.stringify(
|
|
252
|
+
Array.isArray(query) ? query.map((s, index) => {
|
|
253
|
+
return { sql: s, params: params[index] };
|
|
254
|
+
}) : {
|
|
255
|
+
sql: query,
|
|
256
|
+
params
|
|
257
|
+
}
|
|
258
|
+
);
|
|
259
|
+
const url = new URL(endpoint, "http://d1");
|
|
260
|
+
url.searchParams.set("resultsFormat", resultsFormat);
|
|
261
|
+
const response = await this._wrappedFetch(url.href, {
|
|
262
|
+
method: "POST",
|
|
263
|
+
headers: {
|
|
264
|
+
"content-type": "application/json"
|
|
265
|
+
},
|
|
266
|
+
body
|
|
267
|
+
});
|
|
268
|
+
try {
|
|
269
|
+
const answer = await toJson(response);
|
|
270
|
+
if (Array.isArray(answer)) {
|
|
271
|
+
return answer.map((r) => mapD1Result(r));
|
|
272
|
+
} else {
|
|
273
|
+
return mapD1Result(answer);
|
|
274
|
+
}
|
|
275
|
+
} catch (_e) {
|
|
276
|
+
const e = _e;
|
|
277
|
+
const message = e.cause?.message || e.message || "Something went wrong";
|
|
278
|
+
span.setAttribute("error.type", message);
|
|
279
|
+
throw new Error(`D1_ERROR: ${message}`, {
|
|
280
|
+
cause: new Error(message)
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
async _queryOrThrow(request, span, errorPrefix = "D1_ERROR") {
|
|
285
|
+
try {
|
|
286
|
+
const queryParams = this.bookmarkOrConstraint ? { ...request, bookmark: this.bookmarkOrConstraint } : request;
|
|
287
|
+
const response = await fetcherQuery(this.fetcher, queryParams);
|
|
288
|
+
if (response.bookmark) {
|
|
289
|
+
this._updateBookmark(response.bookmark);
|
|
290
|
+
}
|
|
291
|
+
return response.results.map(mapQueryResult);
|
|
292
|
+
} catch (e) {
|
|
293
|
+
const message = Error.isError(e) ? e.message : String(e);
|
|
294
|
+
span.setAttribute("error.type", message);
|
|
295
|
+
throw new Error(`${errorPrefix}: ${message}`, { cause: e });
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
var D1DatabaseSessionAlwaysPrimary = class extends D1DatabaseSession {
|
|
300
|
+
constructor(fetcher) {
|
|
301
|
+
super(fetcher, null);
|
|
302
|
+
}
|
|
303
|
+
// We ignore bookmarks for this special type of session,
|
|
304
|
+
// since all queries are sent to the primary.
|
|
305
|
+
_updateBookmark(_newBookmark) {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
// There is no bookmark returned ever by this special type of session,
|
|
309
|
+
// since all queries are sent to the primary.
|
|
310
|
+
getBookmark() {
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
//////////////////////////////////////////////////////////////////////////////////////////////
|
|
314
|
+
// These are only used by the D1Database which is our existing API pre-Sessions API.
|
|
315
|
+
// For backwards compatibility they always go to the primary database.
|
|
316
|
+
//
|
|
317
|
+
async exec(query) {
|
|
318
|
+
return withSpan("d1_exec", async (span) => {
|
|
319
|
+
span.setAttributes({
|
|
320
|
+
"db.system.name": "cloudflare-d1",
|
|
321
|
+
"db.operation.name": "exec",
|
|
322
|
+
"db.query.text": query,
|
|
323
|
+
"cloudflare.binding.type": "D1"
|
|
324
|
+
});
|
|
325
|
+
const lines = query.trim().split("\n");
|
|
326
|
+
let execResults;
|
|
327
|
+
if (d1BindingJsrpc) {
|
|
328
|
+
execResults = await this._queryOrThrow(
|
|
329
|
+
{ queries: lines.map((sql) => ({ sql })) },
|
|
330
|
+
span,
|
|
331
|
+
"D1_EXEC_ERROR"
|
|
332
|
+
);
|
|
333
|
+
} else {
|
|
334
|
+
execResults = await this._send("/execute", lines, [], "NONE", span);
|
|
335
|
+
}
|
|
336
|
+
const results = Array.isArray(execResults) ? execResults : [execResults];
|
|
337
|
+
let duration = 0;
|
|
338
|
+
const metas = [];
|
|
339
|
+
for (const [i, res] of results.entries()) {
|
|
340
|
+
if (!res.success) {
|
|
341
|
+
const message = res.error || "Something went wrong";
|
|
342
|
+
const lineNumber = i + 1;
|
|
343
|
+
const line = lines[lineNumber - 1];
|
|
344
|
+
const legacyMessage = `Error in line ${lineNumber}: ${line}: ${message}`;
|
|
345
|
+
span.setAttribute("error.type", `Error in line ${lineNumber}`);
|
|
346
|
+
throw new Error(`D1_EXEC_ERROR: ${legacyMessage}`, {
|
|
347
|
+
cause: new Error(legacyMessage)
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
duration += res.meta.duration;
|
|
351
|
+
metas.push(res.meta);
|
|
352
|
+
}
|
|
353
|
+
if (metas.length) {
|
|
354
|
+
addAggregatedD1MetaToSpan(span, metas);
|
|
355
|
+
}
|
|
356
|
+
return {
|
|
357
|
+
count: results.length,
|
|
358
|
+
duration
|
|
359
|
+
};
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* DEPRECATED, TO BE REMOVED WITH NEXT BREAKING CHANGE
|
|
364
|
+
* Only applies to the deprecated v1 alpha databases.
|
|
365
|
+
*/
|
|
366
|
+
async dump() {
|
|
367
|
+
const response = await this._wrappedFetch("http://d1/dump", {
|
|
368
|
+
method: "POST",
|
|
369
|
+
headers: {
|
|
370
|
+
"content-type": "application/json"
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
if (response.status !== 200) {
|
|
374
|
+
try {
|
|
375
|
+
const err = await response.json();
|
|
376
|
+
throw new Error(`D1_DUMP_ERROR: ${err.error}`, {
|
|
377
|
+
cause: new Error(err.error)
|
|
378
|
+
});
|
|
379
|
+
} catch {
|
|
380
|
+
throw new Error(`D1_DUMP_ERROR: Status + ${response.status}`, {
|
|
381
|
+
cause: new Error(`Status ${response.status}`)
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return await response.arrayBuffer();
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
var D1PreparedStatement = class _D1PreparedStatement {
|
|
389
|
+
// TODO(soon): Can we use the # syntax here?
|
|
390
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
391
|
+
dbSession;
|
|
392
|
+
statement;
|
|
393
|
+
params;
|
|
394
|
+
constructor(dbSession, statement, values) {
|
|
395
|
+
this.dbSession = dbSession;
|
|
396
|
+
this.statement = statement;
|
|
397
|
+
this.params = values || [];
|
|
398
|
+
}
|
|
399
|
+
bind(...values) {
|
|
400
|
+
const transformedValues = values.map((r) => {
|
|
401
|
+
const rType = typeof r;
|
|
402
|
+
if (rType === "number" || rType === "string") {
|
|
403
|
+
return r;
|
|
404
|
+
} else if (rType === "boolean") {
|
|
405
|
+
return r ? 1 : 0;
|
|
406
|
+
} else if (rType === "object") {
|
|
407
|
+
if (r == null) return r;
|
|
408
|
+
if (Array.isArray(r) && r.every((b) => {
|
|
409
|
+
return typeof b == "number" && b >= 0 && b < 256;
|
|
410
|
+
}))
|
|
411
|
+
return r;
|
|
412
|
+
if (r instanceof ArrayBuffer) {
|
|
413
|
+
return Array.from(new Uint8Array(r));
|
|
414
|
+
}
|
|
415
|
+
if (ArrayBuffer.isView(r)) {
|
|
416
|
+
return Array.from(r);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
throw new Error(
|
|
420
|
+
`D1_TYPE_ERROR: Type '${rType}' not supported for value '${r}'`,
|
|
421
|
+
{
|
|
422
|
+
cause: new Error(`Type '${rType}' not supported for value '${r}'`)
|
|
423
|
+
}
|
|
424
|
+
);
|
|
425
|
+
});
|
|
426
|
+
return new _D1PreparedStatement(
|
|
427
|
+
this.dbSession,
|
|
428
|
+
this.statement,
|
|
429
|
+
transformedValues
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
async first(colName) {
|
|
433
|
+
return withSpan("d1_first", async (span) => {
|
|
434
|
+
span.setAttributes({
|
|
435
|
+
"db.system.name": "cloudflare-d1",
|
|
436
|
+
"db.operation.name": "first",
|
|
437
|
+
"db.query.text": this.statement,
|
|
438
|
+
"cloudflare.binding.type": "D1",
|
|
439
|
+
"cloudflare.d1.query.bookmark": this.dbSession.getBookmark() ?? void 0
|
|
440
|
+
});
|
|
441
|
+
const info = firstIfArray(
|
|
442
|
+
d1BindingJsrpc ? await this.dbSession._queryOrThrow(
|
|
443
|
+
{ queries: [{ sql: this.statement, params: this.params }] },
|
|
444
|
+
span
|
|
445
|
+
) : await this.dbSession._sendOrThrow(
|
|
446
|
+
"/query",
|
|
447
|
+
this.statement,
|
|
448
|
+
this.params,
|
|
449
|
+
"ROWS_AND_COLUMNS",
|
|
450
|
+
span
|
|
451
|
+
)
|
|
452
|
+
);
|
|
453
|
+
span.setAttributes({
|
|
454
|
+
"cloudflare.d1.response.bookmark": this.dbSession.getBookmark() ?? void 0
|
|
455
|
+
});
|
|
456
|
+
addD1MetaToSpan(span, info.meta);
|
|
457
|
+
const results = toArrayOfObjects(info).results;
|
|
458
|
+
const hasResults = results.length > 0;
|
|
459
|
+
if (!hasResults) return null;
|
|
460
|
+
const firstResult = results.at(0);
|
|
461
|
+
if (colName !== void 0) {
|
|
462
|
+
if (firstResult?.[colName] === void 0) {
|
|
463
|
+
span.setAttribute("error.type", "Column not found");
|
|
464
|
+
throw new Error(`D1_COLUMN_NOTFOUND: Column not found (${colName})`, {
|
|
465
|
+
cause: new Error("Column not found")
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
return firstResult[colName];
|
|
469
|
+
} else {
|
|
470
|
+
return firstResult;
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
// TODO: Update this return type because we always includes a `results` array
|
|
475
|
+
// and people are relying on that.
|
|
476
|
+
/* eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters */
|
|
477
|
+
async run() {
|
|
478
|
+
return withSpan("d1_run", async (span) => {
|
|
479
|
+
span.setAttributes({
|
|
480
|
+
"db.system.name": "cloudflare-d1",
|
|
481
|
+
"db.operation.name": "run",
|
|
482
|
+
"db.query.text": this.statement,
|
|
483
|
+
"cloudflare.binding.type": "D1",
|
|
484
|
+
"cloudflare.d1.query.bookmark": this.dbSession.getBookmark() ?? void 0
|
|
485
|
+
});
|
|
486
|
+
const result = firstIfArray(
|
|
487
|
+
d1BindingJsrpc ? await this.dbSession._queryOrThrow(
|
|
488
|
+
{ queries: [{ sql: this.statement, params: this.params }] },
|
|
489
|
+
span
|
|
490
|
+
) : await this.dbSession._sendOrThrow(
|
|
491
|
+
"/execute",
|
|
492
|
+
this.statement,
|
|
493
|
+
this.params,
|
|
494
|
+
"NONE",
|
|
495
|
+
span
|
|
496
|
+
)
|
|
497
|
+
);
|
|
498
|
+
span.setAttributes({
|
|
499
|
+
"cloudflare.d1.response.bookmark": this.dbSession.getBookmark() ?? void 0
|
|
500
|
+
});
|
|
501
|
+
addD1MetaToSpan(span, result.meta);
|
|
502
|
+
return d1BindingJsrpc ? toArrayOfObjects(result) : result;
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
async all() {
|
|
506
|
+
return withSpan("d1_all", async (span) => {
|
|
507
|
+
span.setAttributes({
|
|
508
|
+
"db.system.name": "cloudflare-d1",
|
|
509
|
+
"db.operation.name": "all",
|
|
510
|
+
"db.query.text": this.statement,
|
|
511
|
+
"cloudflare.binding.type": "D1",
|
|
512
|
+
"cloudflare.d1.query.bookmark": this.dbSession.getBookmark() ?? void 0
|
|
513
|
+
});
|
|
514
|
+
const result = firstIfArray(
|
|
515
|
+
d1BindingJsrpc ? await this.dbSession._queryOrThrow(
|
|
516
|
+
{ queries: [{ sql: this.statement, params: this.params }] },
|
|
517
|
+
span
|
|
518
|
+
) : await this.dbSession._sendOrThrow(
|
|
519
|
+
"/query",
|
|
520
|
+
this.statement,
|
|
521
|
+
this.params,
|
|
522
|
+
"ROWS_AND_COLUMNS",
|
|
523
|
+
span
|
|
524
|
+
)
|
|
525
|
+
);
|
|
526
|
+
span.setAttributes({
|
|
527
|
+
"cloudflare.d1.response.bookmark": this.dbSession.getBookmark() ?? void 0
|
|
528
|
+
});
|
|
529
|
+
addD1MetaToSpan(span, result.meta);
|
|
530
|
+
return toArrayOfObjects(result);
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
async raw(options) {
|
|
534
|
+
return withSpan("d1_all", async (span) => {
|
|
535
|
+
span.setAttributes({
|
|
536
|
+
"db.system.name": "cloudflare-d1",
|
|
537
|
+
"db.operation.name": "raw",
|
|
538
|
+
"db.query.text": this.statement,
|
|
539
|
+
"cloudflare.binding.type": "D1",
|
|
540
|
+
"cloudflare.d1.query.bookmark": this.dbSession.getBookmark() ?? void 0
|
|
541
|
+
});
|
|
542
|
+
if (d1BindingJsrpc) {
|
|
543
|
+
const s2 = firstIfArray(
|
|
544
|
+
await this.dbSession._queryOrThrow(
|
|
545
|
+
{ queries: [{ sql: this.statement, params: this.params }] },
|
|
546
|
+
span
|
|
547
|
+
)
|
|
548
|
+
);
|
|
549
|
+
span.setAttributes({
|
|
550
|
+
"cloudflare.d1.response.bookmark": this.dbSession.getBookmark() ?? void 0
|
|
551
|
+
});
|
|
552
|
+
addD1MetaToSpan(span, s2.meta);
|
|
553
|
+
return [
|
|
554
|
+
...options?.columnNames ? [s2.results.columns] : [],
|
|
555
|
+
...s2.results.rows
|
|
556
|
+
];
|
|
557
|
+
}
|
|
558
|
+
const s = firstIfArray(
|
|
559
|
+
await this.dbSession._sendOrThrow(
|
|
560
|
+
"/query",
|
|
561
|
+
this.statement,
|
|
562
|
+
this.params,
|
|
563
|
+
"ROWS_AND_COLUMNS",
|
|
564
|
+
span
|
|
565
|
+
)
|
|
566
|
+
);
|
|
567
|
+
span.setAttributes({
|
|
568
|
+
"cloudflare.d1.response.bookmark": this.dbSession.getBookmark() ?? void 0
|
|
569
|
+
});
|
|
570
|
+
addD1MetaToSpan(span, s.meta);
|
|
571
|
+
if (!("results" in s)) return [];
|
|
572
|
+
if (Array.isArray(s.results)) {
|
|
573
|
+
const raw = [];
|
|
574
|
+
for (const row of s.results) {
|
|
575
|
+
if (options?.columnNames && raw.length === 0) {
|
|
576
|
+
raw.push(Array.from(Object.keys(row)));
|
|
577
|
+
}
|
|
578
|
+
const entry = Object.keys(row).map((k) => {
|
|
579
|
+
return row[k];
|
|
580
|
+
});
|
|
581
|
+
raw.push(entry);
|
|
582
|
+
}
|
|
583
|
+
return raw;
|
|
584
|
+
} else {
|
|
585
|
+
return [
|
|
586
|
+
...options?.columnNames ? [s.results.columns] : [],
|
|
587
|
+
...s.results.rows
|
|
588
|
+
];
|
|
589
|
+
}
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
async function fetcherQuery(fetcher, request) {
|
|
594
|
+
const response = await fetcher.query(request);
|
|
595
|
+
if ("success" in response) {
|
|
596
|
+
if (!response.success) {
|
|
597
|
+
throw response.error;
|
|
598
|
+
}
|
|
599
|
+
const { queryResults, bookmark } = response.results;
|
|
600
|
+
const normalizedResponse = { results: queryResults };
|
|
601
|
+
if (bookmark !== void 0) {
|
|
602
|
+
normalizedResponse.bookmark = bookmark;
|
|
603
|
+
}
|
|
604
|
+
return normalizedResponse;
|
|
605
|
+
}
|
|
606
|
+
return response;
|
|
607
|
+
}
|
|
608
|
+
function mapQueryResult(queryResult) {
|
|
609
|
+
const { data } = queryResult;
|
|
610
|
+
switch (data.kind) {
|
|
611
|
+
// Keep the switch so future result kinds have an obvious extension point.
|
|
612
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
613
|
+
case "raw":
|
|
614
|
+
return {
|
|
615
|
+
success: true,
|
|
616
|
+
meta: queryResult.meta,
|
|
617
|
+
results: {
|
|
618
|
+
columns: data.columns,
|
|
619
|
+
rows: data.rows
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
default: {
|
|
623
|
+
data.kind;
|
|
624
|
+
throw new Error(`Unsupported D1 query result kind: ${String(data.kind)}`);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
function firstIfArray(results) {
|
|
629
|
+
return Array.isArray(results) ? results.at(0) : results;
|
|
630
|
+
}
|
|
631
|
+
function toArrayOfObjects(response) {
|
|
632
|
+
if (!("results" in response))
|
|
633
|
+
return {
|
|
634
|
+
...response,
|
|
635
|
+
results: []
|
|
636
|
+
};
|
|
637
|
+
const results = response.results;
|
|
638
|
+
if (Array.isArray(results)) {
|
|
639
|
+
return { ...response, results };
|
|
640
|
+
} else {
|
|
641
|
+
const { rows, columns } = results;
|
|
642
|
+
return {
|
|
643
|
+
...response,
|
|
644
|
+
results: rows.map(
|
|
645
|
+
(row) => Object.fromEntries(row.map((cell, i) => [columns[i], cell]))
|
|
646
|
+
)
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
function mapD1Result(result) {
|
|
651
|
+
return result.error ? {
|
|
652
|
+
success: false,
|
|
653
|
+
error: result.error
|
|
654
|
+
} : {
|
|
655
|
+
success: true,
|
|
656
|
+
meta: result.meta,
|
|
657
|
+
..."results" in result ? { results: result.results } : {}
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
async function toJson(response) {
|
|
661
|
+
const body = await response.text();
|
|
662
|
+
try {
|
|
663
|
+
return JSON.parse(body);
|
|
664
|
+
} catch {
|
|
665
|
+
throw new Error(`Failed to parse body as JSON, got: ${body}`);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
function addAggregatedD1MetaToSpan(span, metas) {
|
|
669
|
+
if (!metas.length) {
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
const aggregatedMeta = aggregateD1Meta(metas);
|
|
673
|
+
addD1MetaToSpan(span, aggregatedMeta);
|
|
674
|
+
}
|
|
675
|
+
function addD1MetaToSpan(span, meta) {
|
|
676
|
+
span.setAttributes({
|
|
677
|
+
"cloudflare.d1.response.size_after": meta.size_after,
|
|
678
|
+
"cloudflare.d1.response.rows_read": meta.rows_read,
|
|
679
|
+
"cloudflare.d1.response.rows_written": meta.rows_written,
|
|
680
|
+
"cloudflare.d1.response.last_row_id": meta.last_row_id,
|
|
681
|
+
"cloudflare.d1.response.changed_db": meta.changed_db,
|
|
682
|
+
"cloudflare.d1.response.changes": meta.changes,
|
|
683
|
+
"cloudflare.d1.response.served_by_region": meta.served_by_region,
|
|
684
|
+
"cloudflare.d1.response.served_by_colo": meta.served_by_colo,
|
|
685
|
+
"cloudflare.d1.response.served_by_primary": meta.served_by_primary,
|
|
686
|
+
"cloudflare.d1.response.sql_duration_ms": meta.timings?.sql_duration_ms ?? void 0,
|
|
687
|
+
"cloudflare.d1.response.total_attempts": meta.total_attempts
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
function aggregateD1Meta(metas) {
|
|
691
|
+
const aggregatedMeta = {
|
|
692
|
+
duration: 0,
|
|
693
|
+
size_after: 0,
|
|
694
|
+
rows_read: 0,
|
|
695
|
+
rows_written: 0,
|
|
696
|
+
last_row_id: 0,
|
|
697
|
+
changed_db: false,
|
|
698
|
+
changes: 0
|
|
699
|
+
};
|
|
700
|
+
for (const meta of metas) {
|
|
701
|
+
if (!meta) {
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
aggregatedMeta.duration += meta.duration ?? 0;
|
|
705
|
+
aggregatedMeta.size_after = meta.size_after ?? 0;
|
|
706
|
+
aggregatedMeta.rows_read += meta.rows_read ?? 0;
|
|
707
|
+
aggregatedMeta.rows_written += meta.rows_written ?? 0;
|
|
708
|
+
aggregatedMeta.last_row_id = meta.last_row_id ?? 0;
|
|
709
|
+
if (meta.served_by_region) {
|
|
710
|
+
aggregatedMeta.served_by_region = meta.served_by_region;
|
|
711
|
+
}
|
|
712
|
+
if (meta.served_by_colo) {
|
|
713
|
+
aggregatedMeta.served_by_colo = meta.served_by_colo;
|
|
714
|
+
}
|
|
715
|
+
if (meta.served_by_primary) {
|
|
716
|
+
aggregatedMeta.served_by_primary = meta.served_by_primary;
|
|
717
|
+
}
|
|
718
|
+
if (meta.timings?.sql_duration_ms) {
|
|
719
|
+
aggregatedMeta.timings = {
|
|
720
|
+
sql_duration_ms: (aggregatedMeta.timings?.sql_duration_ms ?? 0) + meta.timings.sql_duration_ms
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
if (meta.total_attempts) {
|
|
724
|
+
aggregatedMeta.total_attempts = (aggregatedMeta.total_attempts ?? 0) + meta.total_attempts;
|
|
725
|
+
}
|
|
726
|
+
aggregatedMeta.changes += meta.changes ?? 0;
|
|
727
|
+
if (meta.changed_db) {
|
|
728
|
+
aggregatedMeta.changed_db = true;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
return aggregatedMeta;
|
|
732
|
+
}
|
|
733
|
+
function makeBinding(env) {
|
|
734
|
+
return new D1Database(env.fetcher);
|
|
735
|
+
}
|
|
736
|
+
export {
|
|
737
|
+
makeBinding as default
|
|
738
|
+
};
|