Skip to content

Commit

Permalink
Fix datadir dump in node <v20 by falling back from File to Blob
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis committed Jul 22, 2024
1 parent 5585289 commit 0e030c2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
18 changes: 13 additions & 5 deletions packages/pglite/src/fs/tarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> {
export async function dumpTar(FS: FS, dbname?: string): Promise<File | Blob> {
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 = [
Expand All @@ -20,7 +27,8 @@ const compressedMimeTypes = [

export async function loadTar(FS: FS, file: File | Blob): Promise<void> {
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") ||
Expand Down
4 changes: 2 additions & 2 deletions packages/pglite/src/fs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface Filesystem {
/**
* Dump the PGDATA dir from the filesystem to a gziped tarball.
*/
dumpTar(FS: FS, dbname: string): Promise<File>;
dumpTar(FS: FS, dbname: string): Promise<File | Blob>;
}

export abstract class FilesystemBase implements Filesystem {
Expand All @@ -38,5 +38,5 @@ export abstract class FilesystemBase implements Filesystem {
): Promise<Partial<PostgresMod>>;
async syncToFs(FS: FS) {}
async initialSyncFs(mod: FS) {}
abstract dumpTar(mod: FS, dbname: string): Promise<File>;
abstract dumpTar(mod: FS, dbname: string): Promise<File | Blob>;
}
2 changes: 1 addition & 1 deletion packages/pglite/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export type PGliteInterface = {
callback: (channel: string, payload: string) => void,
): () => void;
offNotification(callback: (channel: string, payload: string) => void): void;
dumpDataDir(): Promise<File>;
dumpDataDir(): Promise<File | Blob>;
};

export type PGliteInterfaceExtensions<E> = E extends Extensions
Expand Down
1 change: 1 addition & 0 deletions packages/pglite/src/pglite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 0e030c2

Please sign in to comment.