{"version":3,"sources":["../src/index.ts","../../../node_modules/.pnpm/tsup@8.3.0_@microsoft+api-extractor@7.47.7_@types+node@20.16.11__postcss@8.4.47_tsx@4.19.2_typescript@5.6.3/node_modules/tsup/assets/cjs_shims.js","../../pglite-utils/src/utils.ts"],"sourcesContent":["import { pglUtils } from '@electric-sql/pglite-utils'\n\nexport async function dataDir(): Promise<Blob> {\n  const moduleUrl = new URL('../dist/prepopulatedfs.tgz', import.meta.url)\n  if (pglUtils.IN_NODE) {\n    const fs = await import('fs/promises')\n    const buffer = await fs.readFile(moduleUrl)\n    return new Blob([new Uint8Array(buffer)])\n  } else {\n    const downloadPromise = await fetch(moduleUrl)\n    return downloadPromise.blob()\n  }\n}\n","// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () =>\n  typeof document === 'undefined'\n    ? new URL(`file:${__filename}`).href\n    : (document.currentScript && document.currentScript.src) ||\n      new URL('main.js', document.baseURI).href\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n","// Electron exposes a Node-like `process` in its renderer/worker/service-worker\n// contexts, which must use the browser fs path. Its main and utility processes\n// are real Node environments (#813).\nfunction isElectronWebContext(): boolean {\n  const type = (process as { type?: string }).type\n  return type === 'renderer' || type === 'worker' || type === 'service-worker'\n}\n\nexport const IN_NODE =\n  typeof process === 'object' &&\n  typeof process.versions === 'object' &&\n  typeof process.versions.node === 'string' &&\n  !isElectronWebContext()\n\nexport const WASM_PREFIX = '/pglite'\n\nconst artifactDownloadPromises = new Map<string, Promise<Response>>()\n\nexport async function startArtifactDownload(url: URL) {\n  if (IN_NODE || artifactDownloadPromises.has(url.toString())) {\n    return\n  }\n  artifactDownloadPromises.set(url.toString(), fetch(url))\n}\n\n// This is a global cache of the Wasm modules to avoid having to re-download or\n// compile them on subsequent calls.\nconst cachedWasmModules = new Map<string, WebAssembly.Module>()\n\nexport const pgliteProc =\n  globalThis && typeof globalThis.process !== 'undefined'\n    ? globalThis.process\n    : { exitCode: undefined }\n\nexport async function instantiateWasm(\n  imports: WebAssembly.Imports,\n  moduleUrl: URL,\n  module?: WebAssembly.Module,\n): Promise<{\n  instance: WebAssembly.Instance\n  module: WebAssembly.Module\n}> {\n  if (module || cachedWasmModules.has(moduleUrl.toString())) {\n    const mod = module || cachedWasmModules.get(moduleUrl.toString())!\n    return {\n      instance: await WebAssembly.instantiate(mod, imports),\n      module: mod,\n    }\n  }\n  if (IN_NODE) {\n    const fs = await import('fs/promises')\n    const buffer = await fs.readFile(moduleUrl)\n    const { module: newModule, instance } = await WebAssembly.instantiate(\n      buffer,\n      imports,\n    )\n    cachedWasmModules.set(moduleUrl.toString(), newModule)\n    return {\n      instance,\n      module: newModule,\n    }\n  } else {\n    if (!artifactDownloadPromises.has(moduleUrl.toString())) {\n      startArtifactDownload(moduleUrl)\n      // wasmDownloadPromises.set(moduleUrl, fetch(moduleUrl))\n    }\n    const response = await artifactDownloadPromises.get(moduleUrl.toString())\n    const { module: newModule, instance } =\n      await WebAssembly.instantiateStreaming(response!.clone(), imports)\n    cachedWasmModules.set(moduleUrl.toString(), newModule)\n    return {\n      instance,\n      module: newModule,\n    }\n  }\n}\n\nexport async function getFsBundle(fsBundleUrl: URL): Promise<ArrayBuffer> {\n  if (IN_NODE) {\n    const fs = await import('fs/promises')\n    const fileData = await fs.readFile(fsBundleUrl)\n    return fileData.buffer\n  } else {\n    startArtifactDownload(fsBundleUrl)\n    const response = await artifactDownloadPromises.get(fsBundleUrl.toString())\n    return response!.clone().arrayBuffer()\n  }\n}\n\nexport const uuid = (): string => {\n  // best case, `crypto.randomUUID` is available\n  if (globalThis.crypto?.randomUUID) {\n    return globalThis.crypto.randomUUID()\n  }\n\n  const bytes = new Uint8Array(16)\n\n  if (globalThis.crypto?.getRandomValues) {\n    // `crypto.getRandomValues` is available even in non-secure contexts\n    globalThis.crypto.getRandomValues(bytes)\n  } else {\n    // fallback to Math.random, if the Crypto API is completely missing\n    for (let i = 0; i < bytes.length; i++) {\n      bytes[i] = Math.floor(Math.random() * 256)\n    }\n  }\n\n  bytes[6] = (bytes[6] & 0x0f) | 0x40 // Set the 4 most significant bits to 0100\n  bytes[8] = (bytes[8] & 0x3f) | 0x80 // Set the 2 most significant bits to 10\n\n  const hexValues: string[] = []\n  bytes.forEach((byte) => {\n    hexValues.push(byte.toString(16).padStart(2, '0'))\n  })\n\n  return (\n    hexValues.slice(0, 4).join('') +\n    '-' +\n    hexValues.slice(4, 6).join('') +\n    '-' +\n    hexValues.slice(6, 8).join('') +\n    '-' +\n    hexValues.slice(8, 10).join('') +\n    '-' +\n    hexValues.slice(10).join('')\n  )\n}\n\n/**\n * Postgresql handles quoted names as CaseSensitive and unquoted as lower case.\n * If input is quoted, returns an unquoted string (same casing)\n * If input is unquoted, returns a lower-case string\n */\nexport function toPostgresName(input: string): string {\n  let output\n  if (input.startsWith('\"') && input.endsWith('\"')) {\n    // Postgres sensitive case\n    output = input.substring(1, input.length - 1)\n  } else {\n    // Postgres case insensitive - all to lower\n    output = input.toLowerCase()\n  }\n  return output\n}\n\ninterface MinimalFS {\n  readdir(path: string): string[]\n  unlink(path: string): void\n  rmdir(path: string): void\n}\n\nexport function rmdirRecursive(fs: MinimalFS, path: string) {\n  try {\n    // If readdir succeeds it's a directory\n    const entries = fs.readdir(path).filter((n: any) => n !== '.' && n !== '..')\n    for (const name of entries) {\n      const child = path + '/' + name\n      // Recurse or unlink depending on whether child is a directory\n      try {\n        fs.readdir(child)\n        rmdirRecursive(fs, child)\n      } catch (e) {\n        // readdir failed => not a directory\n        fs.unlink(child)\n      }\n    }\n    fs.rmdir(path)\n  } catch (e) {\n    // not a directory: try unlink\n    try {\n      fs.unlink(path)\n    } catch (_) {\n      /* ignore if already gone */\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,IAAM,mB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M_PREFIX","getFsBundle","instantiateWasm","pgliteProc","rmdirRecursive","startArtifactDownload","toPostgresName","uuid","isElectronWebContext","type","artifactDownloadPromises","url","cachedWasmModules","imports","moduleUrl","module","mod","buffer","newModule","instance","response","fsBundleUrl","bytes","i","hexValues","byte","input","output","fs","path","entries","name","child"]}