{
  "version": 3,
  "sources": ["../../../../src/plugins/core/constants.ts", "../../../../src/workers/core/do-wrapper.worker.ts"],
  "sourcesContent": ["import type {\n\tDoRawQueryResult,\n\tDoSqlWithParams,\n} from \"../../workers/local-explorer/generated\";\n\nexport const CORE_PLUGIN_NAME = \"core\";\n\n// Service for HTTP socket entrypoint (for checking runtime ready, routing, etc)\nexport const SERVICE_ENTRY = `${CORE_PLUGIN_NAME}:entry`;\n// Service for local explorer (API + UI)\nexport const SERVICE_LOCAL_EXPLORER = `${CORE_PLUGIN_NAME}:local-explorer`;\n// Disk service for local explorer UI assets\nexport const LOCAL_EXPLORER_DISK = `${CORE_PLUGIN_NAME}:local-explorer-disk`;\n// Service prefix for all regular user workers\nconst SERVICE_USER_PREFIX = `${CORE_PLUGIN_NAME}:user`;\n// Service prefix for `workerd`'s builtin services (network, external, disk)\nconst SERVICE_BUILTIN_PREFIX = `${CORE_PLUGIN_NAME}:builtin`;\n// Service prefix for custom fetch functions defined in `serviceBindings` option\nconst SERVICE_CUSTOM_FETCH_PREFIX = `${CORE_PLUGIN_NAME}:custom-fetch`;\n// Service prefix for custom Node functions defined in `serviceBindings` option\nconst SERVICE_CUSTOM_NODE_PREFIX = `${CORE_PLUGIN_NAME}:custom-node`;\n\nexport function getUserServiceName(workerName = \"\") {\n\treturn `${SERVICE_USER_PREFIX}:${workerName}`;\n}\n\n// Namespace custom services to avoid conflicts between user-specified names\n// and hardcoded Miniflare names\nexport enum CustomServiceKind {\n\tUNKNOWN = \"#\", // User specified name (i.e. `serviceBindings`)\n\tKNOWN = \"$\", // Miniflare specified name (i.e. `outboundService`)\n}\n\nexport const CUSTOM_SERVICE_KNOWN_OUTBOUND = \"outbound\";\n\nexport function getBuiltinServiceName(\n\tworkerIndex: number,\n\tkind: CustomServiceKind,\n\tbindingName: string\n) {\n\treturn `${SERVICE_BUILTIN_PREFIX}:${workerIndex}:${kind}${bindingName}`;\n}\n\nexport function getCustomFetchServiceName(\n\tworkerIndex: number,\n\tkind: CustomServiceKind,\n\tbindingName: string\n) {\n\treturn `${SERVICE_CUSTOM_FETCH_PREFIX}:${workerIndex}:${kind}${bindingName}`;\n}\n\nexport function getCustomNodeServiceName(\n\tworkerIndex: number,\n\tkind: CustomServiceKind,\n\tbindingName: string\n) {\n\treturn `${SERVICE_CUSTOM_NODE_PREFIX}:${workerIndex}:${kind}${bindingName}`;\n}\n\n/**\n * Used by the local explorer worker.\n * The method name injected into wrapped Durable Objects for SQLite introspection.\n */\nexport const INTROSPECT_SQLITE_METHOD = \"__miniflare_introspectSqlite\";\n\nexport type IntrospectSqliteMethod = (\n\tqueries: DoSqlWithParams[]\n) => Promise<DoRawQueryResult[]>;\n\n/**\n * Used by the local explorer worker.\n * This method is injected into wrapped Durable Objects to allow getting the DO instance name.\n */\nexport const GET_DO_NAME_METHOD = \"__miniflare_getDOName\";\n\nexport type GetDONameMethod = () => string | undefined;\n", "import {\n\tGET_DO_NAME_METHOD,\n\tINTROSPECT_SQLITE_METHOD,\n} from \"../../plugins/core/constants\";\nimport type {\n\tDoRawQueryResult,\n\tDoSqlWithParams,\n} from \"../local-explorer/generated/types.gen\";\nimport type { DurableObject } from \"cloudflare:workers\";\n\ninterface DurableObjectConstructor<T = Cloudflare.Env> {\n\tnew (\n\t\t...args: ConstructorParameters<typeof DurableObject<T>>\n\t): DurableObject<T>;\n}\n\n/**\n * Wraps a user Durable Object class to add an introspection method\n * for querying SQLite storage from the local explorer.\n */\nexport function createDurableObjectWrapper(\n\tUserClass: DurableObjectConstructor\n) {\n\tclass Wrapper extends UserClass {\n\t\tconstructor(ctx: DurableObjectState, env: Cloudflare.Env) {\n\t\t\tsuper(ctx, env);\n\n\t\t\t// Persist DO name to storage if available.\n\t\t\t// This allows the name to be retrieved later even when accessed via its ID.\n\t\t\tif (ctx.id.name !== undefined) {\n\t\t\t\tvoid ctx.blockConcurrencyWhile(async () => {\n\t\t\t\t\tconst sql: SqlStorage | undefined = ctx.storage.sql;\n\t\t\t\t\tif (sql) {\n\t\t\t\t\t\tsql.exec(\n\t\t\t\t\t\t\t`CREATE TABLE IF NOT EXISTS __miniflare_do_name (id INTEGER PRIMARY KEY, name TEXT)`\n\t\t\t\t\t\t);\n\t\t\t\t\t\tsql.exec(\n\t\t\t\t\t\t\t`INSERT OR REPLACE INTO __miniflare_do_name (id, name) VALUES (1, ?)`,\n\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- guarded by outer if\n\t\t\t\t\t\t\tctx.id.name!\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * Returns the DO instance name from ctx.id.name if available:\n\t\t */\n\t\t[GET_DO_NAME_METHOD](): string | undefined {\n\t\t\t//  If the DO instance was instantiated with idFromName(), ctx.id.name will be set\n\t\t\tif (this.ctx.id.name !== undefined) {\n\t\t\t\treturn this.ctx.id.name;\n\t\t\t}\n\n\t\t\t// If the DO instance was instantiated from a stored id using idFromString(),\n\t\t\t// ctx.id.name will be undefined, but the name may be retrievable from storage\n\t\t\t// if it was previously accessed via idFromName().\n\t\t\tconst sql: SqlStorage | undefined = this.ctx.storage.sql;\n\t\t\tif (sql) {\n\t\t\t\ttry {\n\t\t\t\t\tconst row = sql\n\t\t\t\t\t\t.exec(`SELECT name FROM __miniflare_do_name WHERE id = 1`)\n\t\t\t\t\t\t.one();\n\t\t\t\t\tif (typeof row?.name === \"string\") {\n\t\t\t\t\t\treturn row.name;\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\t// Table doesn't exist yet - DO was never accessed via idFromName()\n\t\t\t\t}\n\t\t\t}\n\t\t\t// If the DO instance was instantiated with newUniqueId(), there is no name.\n\t\t\t// We may also just not have seen and stored the name.\n\t\t\treturn undefined;\n\t\t}\n\n\t\t/**\n\t\t * Execute SQL queries against the DO's SQLite storage.\n\t\t * If multiple queries are provided, they run in a transaction.\n\t\t */\n\t\t[INTROSPECT_SQLITE_METHOD](queries: DoSqlWithParams[]): DoRawQueryResult[] {\n\t\t\tconst sql: SqlStorage | undefined = this.ctx.storage.sql;\n\n\t\t\tif (!sql) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"This Durable Object does not have SQLite storage enabled\"\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst executeQuery = (query: DoSqlWithParams): DoRawQueryResult => {\n\t\t\t\tconst cursor = sql.exec(query.sql, ...(query.params ?? []));\n\n\t\t\t\treturn {\n\t\t\t\t\tcolumns: cursor.columnNames,\n\t\t\t\t\trows: Array.from(cursor.raw()),\n\t\t\t\t\tmeta: {\n\t\t\t\t\t\trows_read: cursor.rowsRead,\n\t\t\t\t\t\trows_written: cursor.rowsWritten,\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t};\n\n\t\t\tconst results: DoRawQueryResult[] = [];\n\n\t\t\tif (queries.length > 1) {\n\t\t\t\tthis.ctx.storage.transactionSync(() => {\n\t\t\t\t\tfor (const query of queries) {\n\t\t\t\t\t\tresults.push(executeQuery(query));\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tresults.push(executeQuery(queries[0]));\n\t\t\t}\n\n\t\t\treturn results;\n\t\t}\n\t}\n\n\tObject.defineProperty(Wrapper, \"name\", { value: UserClass.name });\n\treturn Wrapper;\n}\n"],
  "mappings": ";AAKO,IAAM,mBAAmB,QAGnB,gBAAgB,GAAG,gBAAgB,UAEnC,yBAAyB,GAAG,gBAAgB,mBAE5C,sBAAsB,GAAG,gBAAgB,wBAEhD,sBAAsB,GAAG,gBAAgB,SAEzC,yBAAyB,GAAG,gBAAgB,YAE5C,8BAA8B,GAAG,gBAAgB,iBAEjD,6BAA6B,GAAG,gBAAgB;AA2C/C,IAAM,2BAA2B,gCAU3B,qBAAqB;;;ACrD3B,SAAS,2BACf,WACC;AAAA,EACD,MAAM,gBAAgB,UAAU;AAAA,IAC/B,YAAY,KAAyB,KAAqB;AACzD,YAAM,KAAK,GAAG,GAIV,IAA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