Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pglite): Fix Windows path resolution #54

Merged
merged 2 commits into from
Mar 27, 2024
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
20 changes: 3 additions & 17 deletions packages/pglite/src/initdb.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { PGDATA } from "./fs/index.js";
import EmPostgresFactory, { type EmPostgres } from "../release/postgres.js";
import loadPgShare from "../release/share.js";
import { nodeValues } from "./utils.js";
import { makeLocateFile, nodeValues } from "./utils.js";
import { DebugLevel } from "./index.js";

const PGWASM_URL = new URL("../release/postgres.wasm", import.meta.url);
const PGSHARE_URL = new URL("../release/share.data", import.meta.url);

export const DIRS = [
"global",
"pg_wal",
Expand Down Expand Up @@ -61,21 +58,10 @@ export async function initDb(dataDir?: string, debug?: DebugLevel) {
mod.FS.writeFile(PGDATA + "/base/1/PG_VERSION", "15devel");
},
],
locateFile: (base: string, _path: any) => {
let path = "";
if (base === "share.data") {
path = PGSHARE_URL.toString();
} else if (base === "postgres.wasm") {
path = PGWASM_URL.toString();
}
if (path?.startsWith("file://")) {
path = path.slice(7);
}
return path;
},
locateFile: await makeLocateFile(),
...(debugMode
? { print: console.info, printErr: console.error }
: { print: () => {}, printErr: () => {} }),
: { print: () => { }, printErr: () => { } }),
arguments: [
"--boot",
"-x1",
Expand Down
18 changes: 2 additions & 16 deletions packages/pglite/src/pglite.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Mutex } from "async-mutex";
import EmPostgresFactory, { type EmPostgres } from "../release/postgres.js";
import { type Filesystem, parseDataDir, loadFs } from "./fs/index.js";
import { nodeValues } from "./utils.js";
import { makeLocateFile } from "./utils.js";
import { PGEvent } from "./event.js";
import { parseResults } from "./parse.js";
import { serializeType } from "./types.js";
Expand All @@ -24,9 +24,6 @@ import {
NoticeMessage,
} from "pg-protocol/dist/messages.js";

const PGWASM_URL = new URL("../release/postgres.wasm", import.meta.url);
const PGSHARE_URL = new URL("../release/share.data", import.meta.url);

export class PGlite implements PGliteInterface {
readonly dataDir?: string;
readonly fsType: FilesystemType;
Expand Down Expand Up @@ -119,18 +116,7 @@ export class PGlite implements PGliteInterface {
"/pgdata",
"template1",
],
locateFile: (base: string, _path: any) => {
let path = "";
if (base === "share.data") {
path = PGSHARE_URL.toString();
} else if (base === "postgres.wasm") {
path = PGWASM_URL.toString();
}
if (path?.startsWith("file://")) {
path = path.slice(7);
}
return path;
},
locateFile: await makeLocateFile(),
...(this.debug > 0
? { print: console.info, printErr: console.error }
: { print: () => {}, printErr: () => {} }),
Expand Down
27 changes: 27 additions & 0 deletions packages/pglite/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,30 @@ export async function nodeValues() {
}
return { dirname, require };
}


export async function makeLocateFile() {
const PGWASM_URL = new URL("../release/postgres.wasm", import.meta.url);
const PGSHARE_URL = new URL("../release/share.data", import.meta.url);
let fileURLToPath = (fileUrl: URL) => fileUrl.pathname
if (IN_NODE) {
fileURLToPath = (await import("url")).fileURLToPath
}
return (base: string) => {
let url: URL | null = null;
switch (base) {
case "share.data":
url = PGSHARE_URL;
break;
case "postgres.wasm":
url = PGWASM_URL;
break;
default:
}

if (url?.protocol === "file:") {
return fileURLToPath(url);
}
return url?.toString() ?? '';
}
}
3 changes: 2 additions & 1 deletion packages/pglite/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { defineConfig } from 'tsup'
import path from 'path'
import { fileURLToPath } from 'url'

const thisFile = new URL(import.meta.url).pathname
const thisFile = fileURLToPath(new URL(import.meta.url))
const root = path.dirname(thisFile)

let replaceAssertPlugin = {
Expand Down