Skip to content

Commit c16a1ce

Browse files
committed
Move browser and node http fs into seporate imports
1 parent 5f40f2d commit c16a1ce

File tree

8 files changed

+36
-17
lines changed

8 files changed

+36
-17
lines changed

packages/pglite/examples/httpfs-worker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PGlite } from "../dist/index.js";
22
import { worker } from "../dist/worker/index.js";
3-
import { HttpFs } from "../dist/fs/http.js";
3+
import { HttpFs } from "../dist/fs/http/browser.js";
44

55
console.log("Starting worker...");
66

packages/pglite/examples/httpfs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PGlite } from "../dist/index.js";
2-
import { HttpFs } from "../dist/fs/http.js";
2+
import { HttpFs } from "../dist/fs/http/node.js";
33

44
console.log("Starting PGLite...");
55
const pg = await PGlite.create({

packages/pglite/package.json

+13-5
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,15 @@
5454
"require": "./dist/fs/opfs-ahp.cjs",
5555
"types": "./dist/fs/opfs-ahp.d.ts"
5656
},
57-
"./httpfs": {
58-
"import": "./dist/fs/http.js",
59-
"require": "./dist/fs/http.cjs",
60-
"types": "./dist/fs/http.d.ts"
57+
"./httpfs/browser": {
58+
"import": "./dist/fs/http/browser.js",
59+
"require": "./dist/fs/http/browser.cjs",
60+
"types": "./dist/fs/http/browser.d.ts"
61+
},
62+
"./httpfs/node": {
63+
"import": "./dist/fs/http/node.js",
64+
"require": "./dist/fs/http/node.cjs",
65+
"types": "./dist/fs/http/node.d.ts"
6166
},
6267
"./contrib/*": {
6368
"import": "./dist/contrib/*.js",
@@ -120,6 +125,9 @@
120125
"crypto": false,
121126
"ws": false,
122127
"child_process": false,
123-
"module": false
128+
"module": false,
129+
"worker_threads": false,
130+
"http": false,
131+
"https": false
124132
}
125133
}

packages/pglite/src/fs/http.ts packages/pglite/src/fs/http/base.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { PGlite } from '../pglite.js'
2-
import type { PostgresMod } from '../postgresMod.js'
3-
import { BaseFilesystem, FsStats, ERRNO_CODES } from './base.js'
4-
import type { TarIndex, TarIndexFile } from './tarUtils.js'
5-
import { makeSyncFetch, type SyncFetch } from '../sync-fetch/index.js'
1+
import type { PGlite } from '../../pglite.js'
2+
import type { PostgresMod } from '../../postgresMod.js'
3+
import { SyncFetch } from '../../sync-fetch/index.js'
4+
import { BaseFilesystem, FsStats, ERRNO_CODES } from '../base.js'
5+
import type { TarIndex, TarIndexFile } from '../tarUtils.js'
66

77
type Node = TarIndexFile & {
88
isDir: boolean
@@ -34,14 +34,14 @@ export interface HttpFsOptions {
3434
* Requires an index.json file at the root of the filesystem with a list of
3535
* files available to fetch
3636
*/
37-
export class HttpFs extends BaseFilesystem {
37+
export abstract class HttpFsBase extends BaseFilesystem {
3838
index?: TarIndex
3939
tree?: Node
4040
httpPaths = new Set<string>()
4141
#handleMap: Map<number, Node> = new Map()
4242
#handleCounter = 0
4343
fetchGranularity: FetchGranularity
44-
fetch?: SyncFetch
44+
abstract fetch: SyncFetch
4545

4646
constructor(
4747
baseUrl: string,
@@ -53,7 +53,6 @@ export class HttpFs extends BaseFilesystem {
5353

5454
async init(pg: PGlite, opts: Partial<PostgresMod>) {
5555
await this.#init()
56-
this.fetch = await makeSyncFetch()
5756
return super.init(pg, opts)
5857
}
5958

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { HttpFsBase } from './base.js'
2+
import { syncFetchBrowser } from '../../sync-fetch/browser.js'
3+
4+
export class HttpFs extends HttpFsBase {
5+
fetch = syncFetchBrowser
6+
}

packages/pglite/src/fs/http/node.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { HttpFsBase } from './base.js'
2+
import { syncFetchNode } from '../../sync-fetch/node.js'
3+
4+
export class HttpFs extends HttpFsBase {
5+
fetch = syncFetchNode
6+
}

packages/pglite/src/sync-fetch/node-worker.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { workerData } from 'worker_threads'
22
import * as http from 'http'
33
import * as https from 'https'
4-
import { URL } from 'url'
54

65
function fetch(
76
url: string,

packages/pglite/tsup.config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const entryPoints = [
2020
'src/index.ts',
2121
'src/fs/nodefs.ts',
2222
'src/fs/opfs-ahp.ts',
23-
'src/fs/http.ts',
23+
'src/fs/http/browser.ts',
24+
'src/fs/http/node.ts',
2425
'src/templating.ts',
2526
'src/live/index.ts',
2627
'src/vector/index.ts',

0 commit comments

Comments
 (0)