Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | 198x 198x 198x 198x 198x 197x 197x 41x 41x 147x 147x 188x 188x 188x 54x 2x 156x 52x 52x 52x 156x 156x 156x 26x 26x 130x 130x 130x 52x 134x 2x 132x 132x 57x 75x 75x 13x 13x 13x 13x 13x 13x 13x 13x 36x 75x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 165x 165x 165x 165x 16x 7x 7x 9x 424x 424x 424x 394x 30x 14x 16x 1x 15x 14x 1x 423x 423x 4x 1x 3x 419x 20x 1x 19x 421x 421x 248x 247x 1x 246x 419x 1x 1x 1x 1x 1x 630x 629x 196x 196x 629x | import { D1NamespaceOptions } from "./d1-options.js";
import { D1_SQL, sanitizeTableName } from "./d1-sql.js";
import { base64Decode, base64Encode, readStream } from "./utils.js";
export class D1Namespace<Key extends string = string> implements KVNamespace<Key> {
/**
* Final, normalized, fully-resolved configuration.
*/
readonly options: Readonly<{
namespace: string;
table: { name: string; autoCreate: boolean };
}>;
/** Text encoder/decoder for UTF-8 value handling. */
readonly #encoder = new TextEncoder();
readonly #decoder = new TextDecoder();
/**
* Lazily prepared SQL statements for this table.
*/
readonly #stmt: {
get: D1PreparedStatement;
getWithMetadata: D1PreparedStatement;
list: D1PreparedStatement;
put: D1PreparedStatement;
delete: D1PreparedStatement;
deleteExpired: D1PreparedStatement;
ensureTable: D1PreparedStatement;
};
/** Ensures CREATE TABLE runs once per instance. */
#tableReady?: Promise<void>;
constructor(
private readonly d1: D1Database,
options?: D1NamespaceOptions
) {
const namespace = options?.namespace ?? "";
const tableName = sanitizeTableName(options?.table?.name ?? `${namespace}_kv_entries`);
this.options = Object.freeze({
namespace,
table: {
name: tableName,
autoCreate: options?.table?.autoCreate ?? true,
}
});
this.#stmt = {
get: this.d1.prepare(D1_SQL.get(tableName)),
getWithMetadata: this.d1.prepare(D1_SQL.getWithMetadata(tableName)),
list: this.d1.prepare(D1_SQL.list(tableName)),
put: this.d1.prepare(D1_SQL.put(tableName)),
delete: this.d1.prepare(D1_SQL.delete(tableName)),
deleteExpired: this.d1.prepare(D1_SQL.deleteExpired(tableName)),
ensureTable: this.d1.prepare(D1_SQL.ensureTable(tableName)),
};
}
async get<ExpectedValue = unknown>(
key: Key | Key[],
options?: { type?: "text" | "json" | "arrayBuffer" | "stream" } | string
) {
const type = typeof options === "string" ? options : options?.type ?? "text";
return this.#getImpl<ExpectedValue>(key, type, false);
}
async getWithMetadata<ExpectedValue = unknown>(
key: Key | Key[],
options?: { type?: "text" | "json" | "arrayBuffer" | "stream" } | string
) {
const type = typeof options === "string" ? options : options?.type ?? "text";
return this.#getImpl<ExpectedValue>(key, type, true);
}
/**
* Internal unified implementation for both `get()` and `getWithMetadata()`.
* Handles single-key and multi-key fetches, supporting `text`, `json`, `arrayBuffer`, and `stream` types.
*/
async #getImpl<ExpectedValue = unknown>(
key: Key | Array<Key>,
type: string,
withMetadata: boolean
) {
await this.#ensureTable();
const stmt = withMetadata
? this.#stmt.getWithMetadata
: this.#stmt.get;
// Multi-key batch mode
if (Array.isArray(key)) {
if (type !== "json" && type !== "text") {
throw new Error(`"${type}" is not a valid type. Use "json" or "text"`);
}
const stmts = key.map(k => stmt.bind(k));
const results = await this.d1.batch<{ value: ArrayBuffer, metadata: ArrayBuffer | null }>(stmts);
const out = new Map<
string,
| ExpectedValue
| KVNamespaceGetWithMetadataResult<ExpectedValue, unknown>
| null
>();
for (let i = 0; i < key.length; i++) {
const k = key[i]!;
const row = results[i]?.results?.[0] ?? null;
if (!row) {
out.set(k, null);
continue;
}
const text = this.#decoder.decode(new Uint8Array(row.value));
const value = type === "json" ? JSON.parse(text) : text;
out.set(k, withMetadata ? {
value,
metadata: row.metadata ? JSON.parse(this.#decoder.decode(new Uint8Array(row.metadata))) : null,
} as KVNamespaceGetWithMetadataResult<ExpectedValue, unknown> : value);
}
return out;
}
// Single key
if (
type !== "text" &&
type !== "json" &&
type !== "arrayBuffer" &&
type !== "stream"
) {
throw new TypeError('Unknown response type. Possible types are "text", "json", "arrayBuffer", and "stream".');
}
const row = await stmt
.bind(key)
.first<{ value: ArrayBuffer; metadata: ArrayBuffer | null }>();
if (!row) {
return withMetadata ? {
value: null,
metadata: null,
cacheStatus: null,
} as KVNamespaceGetWithMetadataResult<ExpectedValue, unknown> : null;
}
const u8 = new Uint8Array(row.value);
let value: any;
switch (type) {
case "arrayBuffer":
value = u8.buffer;
break;
case "stream":
value = new ReadableStream({
start(c) {
c.enqueue(u8);
c.close();
},
});
break;
case "json":
value = JSON.parse(this.#decoder.decode(u8));
break;
default:
value = this.#decoder.decode(u8);
}
return withMetadata ? {
value,
metadata: row.metadata ? JSON.parse(this.#decoder.decode(new Uint8Array(row.metadata))) : null,
cacheStatus: null,
} as KVNamespaceGetWithMetadataResult<ExpectedValue, unknown> : value;
}
async list<Metadata = unknown>(
options?: KVNamespaceListOptions
): Promise<KVNamespaceListResult<Metadata, Key>> {
await this.#ensureTable();
// https://github.com/cloudflare/workers-sdk/blob/main/packages/miniflare/src/workers/shared/keyvalue.worker.ts#L233
const prefix = options?.prefix ?? "";
const lower = prefix;
const upper = prefix + "\u{10FFFF}";
const limit = Math.max(1, Number(options?.limit ?? 1000));
const startAfter = options?.cursor ? base64Decode(options.cursor) : "";
const rows = await this.#stmt.list
.bind(lower, upper, startAfter, limit + 1)
.all<{ key: string; expiration: number | null; metadata: ArrayBuffer | null }>();
const hasMore = rows.results.length > limit;
const page = hasMore
? rows.results.slice(0, limit)
: rows.results;
const keys = page.map(r => {
const item: {
name: Key;
expiration?: number;
metadata?: Metadata;
} = { name: r.key as Key };
if (typeof r.expiration === "number") item.expiration = r.expiration;
if (r.metadata) item.metadata = JSON.parse(this.#decoder.decode(new Uint8Array(r.metadata)));
return item;
});
if (hasMore) {
const last = page[page.length - 1]!.key;
return {
keys,
list_complete: false,
cursor: base64Encode(last),
cacheStatus: null,
};
}
return { keys, list_complete: true, cacheStatus: null };
}
async put(
key: Key,
value: any,
options?: KVNamespacePutOptions
): Promise<void> {
await this.#ensureTable();
const now = Math.floor(Date.now() / 1000);
// Value → Uint8Array
let bytes: Uint8Array;
if (typeof value === "string") {
bytes = this.#encoder.encode(value);
} else if (value instanceof ArrayBuffer) {
bytes = new Uint8Array(value);
} else if (ArrayBuffer.isView(value)) {
bytes = new Uint8Array(
value.buffer,
value.byteOffset,
value.byteLength
);
} else if (value instanceof ReadableStream) {
bytes = await readStream(value);
} else {
throw new TypeError("KV put() accepts only strings, ArrayBuffers, ArrayBufferViews, and ReadableStreams as values.");
}
// Expiration → ABSOLUTE timestamp
let expiration: number | null = null;
if (options?.expirationTtl != null) {
if (options.expirationTtl <= 0)
throw new RangeError(
`Invalid expiration_ttl of ${options.expirationTtl}. Please specify integer greater than 0.`
);
expiration = now + options.expirationTtl;
} else if (options?.expiration != null) {
if (options.expiration <= now)
throw new RangeError(
`Invalid expiration of ${options.expiration}. Please specify integer greater than the current number of seconds since the UNIX epoch.`
);
expiration = options.expiration;
}
// Metadata → encoded JSON
let metadata: Uint8Array | null = null;
if (options?.metadata !== undefined) {
const str = JSON.stringify(options.metadata);
if (str === undefined)
throw new TypeError(
"Metadata could not be serialized to JSON."
);
metadata = this.#encoder.encode(str);
}
// UPSERT + delete expired entries
await this.d1.batch([
this.#stmt.put.bind(key, bytes, expiration, metadata),
this.#stmt.deleteExpired.bind()
]);
}
async delete(key: Key): Promise<void> {
await this.#ensureTable();
await this.d1.batch([this.#stmt.delete.bind(key), this.#stmt.deleteExpired.bind()]);
}
async deleteExpired(): Promise<number> {
await this.#ensureTable();
const res = await this.#stmt.deleteExpired.run();
return res.meta.changes;
}
async #ensureTable() {
if (!this.options.table.autoCreate) return;
// If schema initialization already started, reuse the same Promise.
if (!this.#tableReady) {
this.#tableReady = (async () => {
await this.#stmt.ensureTable.run();
})();
}
return this.#tableReady;
}
}
export interface D1Namespace<Key extends string = string> extends KVNamespace<Key> {
get(
key: Key,
options?: Partial<KVNamespaceGetOptions<undefined>>,
): Promise<string | null>;
get(key: Key, type: "text"): Promise<string | null>;
get<ExpectedValue = unknown>(
key: Key,
type: "json",
): Promise<ExpectedValue | null>;
get(key: Key, type: "arrayBuffer"): Promise<ArrayBuffer | null>;
get(key: Key, type: "stream"): Promise<ReadableStream | null>;
get(
key: Key,
options?: KVNamespaceGetOptions<"text">,
): Promise<string | null>;
get<ExpectedValue = unknown>(
key: Key,
options?: KVNamespaceGetOptions<"json">,
): Promise<ExpectedValue | null>;
get(
key: Key,
options?: KVNamespaceGetOptions<"arrayBuffer">,
): Promise<ArrayBuffer | null>;
get(
key: Key,
options?: KVNamespaceGetOptions<"stream">,
): Promise<ReadableStream | null>;
get(key: Array<Key>, type: "text"): Promise<Map<string, string | null>>;
get<ExpectedValue = unknown>(
key: Array<Key>,
type: "json",
): Promise<Map<string, ExpectedValue | null>>;
get(
key: Array<Key>,
options?: Partial<KVNamespaceGetOptions<undefined>>,
): Promise<Map<string, string | null>>;
get(
key: Array<Key>,
options?: KVNamespaceGetOptions<"text">,
): Promise<Map<string, string | null>>;
get<ExpectedValue = unknown>(
key: Array<Key>,
options?: KVNamespaceGetOptions<"json">,
): Promise<Map<string, ExpectedValue | null>>;
list<Metadata = unknown>(
options?: KVNamespaceListOptions,
): Promise<KVNamespaceListResult<Metadata, Key>>;
put(
key: Key,
value: string | ArrayBuffer | ArrayBufferView | ReadableStream,
options?: KVNamespacePutOptions,
): Promise<void>;
getWithMetadata<Metadata = unknown>(
key: Key,
options?: Partial<KVNamespaceGetOptions<undefined>>,
): Promise<KVNamespaceGetWithMetadataResult<string, Metadata>>;
getWithMetadata<Metadata = unknown>(
key: Key,
type: "text",
): Promise<KVNamespaceGetWithMetadataResult<string, Metadata>>;
getWithMetadata<ExpectedValue = unknown, Metadata = unknown>(
key: Key,
type: "json",
): Promise<KVNamespaceGetWithMetadataResult<ExpectedValue, Metadata>>;
getWithMetadata<Metadata = unknown>(
key: Key,
type: "arrayBuffer",
): Promise<KVNamespaceGetWithMetadataResult<ArrayBuffer, Metadata>>;
getWithMetadata<Metadata = unknown>(
key: Key,
type: "stream",
): Promise<KVNamespaceGetWithMetadataResult<ReadableStream, Metadata>>;
getWithMetadata<Metadata = unknown>(
key: Key,
options: KVNamespaceGetOptions<"text">,
): Promise<KVNamespaceGetWithMetadataResult<string, Metadata>>;
getWithMetadata<ExpectedValue = unknown, Metadata = unknown>(
key: Key,
options: KVNamespaceGetOptions<"json">,
): Promise<KVNamespaceGetWithMetadataResult<ExpectedValue, Metadata>>;
getWithMetadata<Metadata = unknown>(
key: Key,
options: KVNamespaceGetOptions<"arrayBuffer">,
): Promise<KVNamespaceGetWithMetadataResult<ArrayBuffer, Metadata>>;
getWithMetadata<Metadata = unknown>(
key: Key,
options: KVNamespaceGetOptions<"stream">,
): Promise<KVNamespaceGetWithMetadataResult<ReadableStream, Metadata>>;
getWithMetadata<Metadata = unknown>(
key: Array<Key>,
type: "text",
): Promise<Map<string, KVNamespaceGetWithMetadataResult<string, Metadata>>>;
getWithMetadata<ExpectedValue = unknown, Metadata = unknown>(
key: Array<Key>,
type: "json",
): Promise<
Map<string, KVNamespaceGetWithMetadataResult<ExpectedValue, Metadata>>
>;
getWithMetadata<Metadata = unknown>(
key: Array<Key>,
options?: Partial<KVNamespaceGetOptions<undefined>>,
): Promise<Map<string, KVNamespaceGetWithMetadataResult<string, Metadata>>>;
getWithMetadata<Metadata = unknown>(
key: Array<Key>,
options?: KVNamespaceGetOptions<"text">,
): Promise<Map<string, KVNamespaceGetWithMetadataResult<string, Metadata>>>;
getWithMetadata<ExpectedValue = unknown, Metadata = unknown>(
key: Array<Key>,
options?: KVNamespaceGetOptions<"json">,
): Promise<
Map<string, KVNamespaceGetWithMetadataResult<ExpectedValue, Metadata>>
>;
delete(key: Key): Promise<void>;
deleteExpired(): Promise<number>;
}
|