Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/sixty-buses-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@electric-sql/pglite-utils': patch
'@electric-sql/pglite': patch
---

Fix caching of artifacts such that they are not downloaded multiple times
5 changes: 4 additions & 1 deletion docs/repl/ReplPlayground.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ const showReloadMsg = computed(() => {
})

async function doLoadPg() {
pg.value = await loadPg()
if (!pg.value) {
pg.value = await loadPg()
}
}

async function loadPg() {
Expand Down Expand Up @@ -162,6 +164,7 @@ watch(
async function clearDb() {
if (pg.value) {
await pg.value.close()
pg.value = null
}
while (true) {
const closed = await new Promise((resolve, reject) => {
Expand Down
32 changes: 17 additions & 15 deletions packages/pglite-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ export const IN_NODE =
typeof process.versions === 'object' &&
typeof process.versions.node === 'string'

const wasmDownloadPromises = new Map<URL, Promise<Response>>()
const artifactDownloadPromises = new Map<string, Promise<Response>>()

export async function startWasmDownload(url: URL) {
if (IN_NODE || wasmDownloadPromises.has(url)) {
export async function startArtifactDownload(url: URL) {
if (IN_NODE || artifactDownloadPromises.has(url.toString())) {
return
}
wasmDownloadPromises.set(url, fetch(url))
artifactDownloadPromises.set(url.toString(), fetch(url))
}

// This is a global cache of the Wasm modules to avoid having to re-download or
// compile them on subsequent calls.
const cachedWasmModules = new Map<URL, WebAssembly.Module>()
const cachedWasmModules = new Map<string, WebAssembly.Module>()

export async function instantiateWasm(
imports: WebAssembly.Imports,
Expand All @@ -24,8 +24,8 @@ export async function instantiateWasm(
instance: WebAssembly.Instance
module: WebAssembly.Module
}> {
if (module || cachedWasmModules.has(moduleUrl)) {
const mod = module || cachedWasmModules.get(moduleUrl)!
if (module || cachedWasmModules.has(moduleUrl.toString())) {
const mod = module || cachedWasmModules.get(moduleUrl.toString())!
return {
instance: await WebAssembly.instantiate(mod, imports),
module: mod,
Expand All @@ -38,19 +38,20 @@ export async function instantiateWasm(
buffer,
imports,
)
cachedWasmModules.set(moduleUrl, newModule)
cachedWasmModules.set(moduleUrl.toString(), newModule)
return {
instance,
module: newModule,
}
} else {
if (!wasmDownloadPromises.has(moduleUrl)) {
wasmDownloadPromises.set(moduleUrl, fetch(moduleUrl))
if (!artifactDownloadPromises.has(moduleUrl.toString())) {
startArtifactDownload(moduleUrl)
// wasmDownloadPromises.set(moduleUrl, fetch(moduleUrl))
}
const response = await wasmDownloadPromises.get(moduleUrl)
const response = await artifactDownloadPromises.get(moduleUrl.toString())
const { module: newModule, instance } =
await WebAssembly.instantiateStreaming(response!, imports)
cachedWasmModules.set(moduleUrl, newModule)
await WebAssembly.instantiateStreaming(response!.clone(), imports)
cachedWasmModules.set(moduleUrl.toString(), newModule)
return {
instance,
module: newModule,
Expand All @@ -64,8 +65,9 @@ export async function getFsBundle(fsBundleUrl: URL): Promise<ArrayBuffer> {
const fileData = await fs.readFile(fsBundleUrl)
return fileData.buffer
} else {
const response = await fetch(fsBundleUrl)
return response.arrayBuffer()
startArtifactDownload(fsBundleUrl)
const response = await artifactDownloadPromises.get(fsBundleUrl.toString())
return response!.clone().arrayBuffer()
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/pglite/src/pglite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,14 @@ export class PGlite

if (!options.pgliteWasmModule) {
// Start the wasm download in the background so it's ready when we need it
pglUtils.startWasmDownload(
pglUtils.startArtifactDownload(
new URL('../release/pglite.wasm', import.meta.url),
)
}

if (!options.initdbWasmModule) {
// Start the wasm download in the background so it's ready when we need it
pglUtils.startWasmDownload(
pglUtils.startArtifactDownload(
new URL('../release/initdb.wasm', import.meta.url),
)
}
Expand Down
Loading