diff --git a/packages/pglite/src/fs/tarUtils.ts b/packages/pglite/src/fs/tarUtils.ts index 0c80d43e..b87c6539 100644 --- a/packages/pglite/src/fs/tarUtils.ts +++ b/packages/pglite/src/fs/tarUtils.ts @@ -2,13 +2,20 @@ import { tar, untar, type TarFile, REGTYPE, DIRTYPE } from "tinytar"; import { FS } from "../postgres.js"; import { PGDATA } from "./index.js"; -export async function dumpTar(FS: FS, dbname?: string): Promise { +export async function dumpTar(FS: FS, dbname?: string): Promise { const tarball = createTarball(FS, PGDATA); const [compressed, zipped] = await maybeZip(tarball); const filename = (dbname || "pgdata") + (zipped ? ".tar.gz" : ".tar"); - return new File([compressed], filename, { - type: zipped ? "application/x-gtar" : "application/x-tar", - }); + const type = zipped ? "application/x-gzip" : "application/x-tar"; + if (typeof File !== "undefined") { + return new File([compressed], filename, { + type, + }); + } else { + return new Blob([compressed], { + type, + }); + } } const compressedMimeTypes = [ @@ -20,7 +27,8 @@ const compressedMimeTypes = [ export async function loadTar(FS: FS, file: File | Blob): Promise { let tarball = new Uint8Array(await file.arrayBuffer()); - const filename = file instanceof File ? file.name : undefined; + const filename = + typeof File !== "undefined" && file instanceof File ? file.name : undefined; const compressed = compressedMimeTypes.includes(file.type) || filename?.endsWith(".tgz") || diff --git a/packages/pglite/src/fs/types.ts b/packages/pglite/src/fs/types.ts index ed09167d..c3b6f0a7 100644 --- a/packages/pglite/src/fs/types.ts +++ b/packages/pglite/src/fs/types.ts @@ -25,7 +25,7 @@ export interface Filesystem { /** * Dump the PGDATA dir from the filesystem to a gziped tarball. */ - dumpTar(FS: FS, dbname: string): Promise; + dumpTar(FS: FS, dbname: string): Promise; } export abstract class FilesystemBase implements Filesystem { @@ -38,5 +38,5 @@ export abstract class FilesystemBase implements Filesystem { ): Promise>; async syncToFs(FS: FS) {} async initialSyncFs(mod: FS) {} - abstract dumpTar(mod: FS, dbname: string): Promise; + abstract dumpTar(mod: FS, dbname: string): Promise; } diff --git a/packages/pglite/src/interface.ts b/packages/pglite/src/interface.ts index 15f3c544..c2fa7976 100644 --- a/packages/pglite/src/interface.ts +++ b/packages/pglite/src/interface.ts @@ -90,7 +90,7 @@ export type PGliteInterface = { callback: (channel: string, payload: string) => void, ): () => void; offNotification(callback: (channel: string, payload: string) => void): void; - dumpDataDir(): Promise; + dumpDataDir(): Promise; }; export type PGliteInterfaceExtensions = E extends Extensions diff --git a/packages/pglite/src/pglite.ts b/packages/pglite/src/pglite.ts index 1885d482..679accf2 100644 --- a/packages/pglite/src/pglite.ts +++ b/packages/pglite/src/pglite.ts @@ -747,6 +747,7 @@ export class PGlite implements PGliteInterface { /** * Dump the PGDATA dir from the filesystem to a gziped tarball. + * @returns The tarball as a File object where available, and fallback to a Blob */ async dumpDataDir() { let dbname = this.dataDir?.split("/").pop() ?? "pgdata";