miniflare
Version:
Fun, full-featured, fully-local simulator for Cloudflare Workers
53 lines (49 loc) • 2.61 kB
JavaScript
// src/workers/core/dev-control.worker.ts
import { WorkerEntrypoint } from "cloudflare:workers";
import workerdUnsafe from "workerd:unsafe";
// src/plugins/core/constants.ts
var CORE_PLUGIN_NAME = "core", SERVICE_ENTRY = `${CORE_PLUGIN_NAME}:entry`, SERVICE_LOCAL_EXPLORER = `${CORE_PLUGIN_NAME}:local-explorer`, LOCAL_EXPLORER_DISK = `${CORE_PLUGIN_NAME}:local-explorer-disk`, SERVICE_USER_PREFIX = `${CORE_PLUGIN_NAME}:user`, SERVICE_BUILTIN_PREFIX = `${CORE_PLUGIN_NAME}:builtin`, SERVICE_CUSTOM_FETCH_PREFIX = `${CORE_PLUGIN_NAME}:custom-fetch`, SERVICE_CUSTOM_NODE_PREFIX = `${CORE_PLUGIN_NAME}:custom-node`;
var INTROSPECT_SQLITE_METHOD = "__miniflare_introspectSqlite";
// src/shared/dev-control.ts
function getDevControlDurableObjectBindingName(scriptName, className) {
return ["MINIFLARE_DEV_CONTROL_DO", scriptName, className].join(":");
}
// src/workers/core/dev-control.worker.ts
function isDurableObjectNamespace(binding) {
return typeof binding == "object" && binding !== null && "idFromName" in binding && typeof binding.idFromName == "function" && "idFromString" in binding && typeof binding.idFromString == "function" && "get" in binding && typeof binding.get == "function";
}
function getDurableObjectStub(env, scriptName, className, identifier) {
let bindingName = getDevControlDurableObjectBindingName(
scriptName,
className
), namespace = env[bindingName];
if (!isDurableObjectNamespace(namespace))
throw new TypeError(
`Expected Durable Object namespace binding for ${scriptName}:${className}`
);
let id = identifier.id === void 0 ? namespace.idFromName(identifier.name) : namespace.idFromString(identifier.id);
return namespace.get(id);
}
var DevControl = class extends WorkerEntrypoint {
async evictDurableObject(scriptName, className, options) {
let stub = getDurableObjectStub(this.env, scriptName, className, options);
await workerdUnsafe.evict(stub, {
webSockets: options.webSockets
});
}
async execDurableObjectSql(scriptName, className, options) {
let stub = getDurableObjectStub(this.env, scriptName, className, options), [result] = await stub[INTROSPECT_SQLITE_METHOD]([
{ sql: options.query, params: options.bindings }
]);
if (result === void 0)
throw new Error("Durable Object SQLite query did not return a result.");
let columns = result.columns ?? [];
return (result.rows ?? []).map((rawRow) => Object.fromEntries(
columns.map((column, index) => [column, rawRow[index]])
));
}
};
export {
DevControl as default
};
//# sourceMappingURL=dev-control.worker.js.map