@@ -8,6 +8,32 @@ import type { URI } from 'vscode-uri'
8
8
const textCache = new Map < string , Promise < string | undefined > > ( )
9
9
const jsonCache = new Map < string , Promise < any > > ( )
10
10
11
+ export type CreateNpmFileSystemOptions = {
12
+ getPackageLatestVersionUrl ?: ( pkgName : string ) => string
13
+ getPackageDirectoryUrl ?: (
14
+ pkgName : string ,
15
+ pkgVersion : string ,
16
+ pkgPath : string ,
17
+ ) => string
18
+ getPackageFileTextUrl ?: (
19
+ path : string ,
20
+ pkgName : string ,
21
+ pkgVersion : string | undefined ,
22
+ pkgPath : string ,
23
+ ) => string
24
+ }
25
+
26
+ const defaultUnpkgOptions : Required < CreateNpmFileSystemOptions > = {
27
+ getPackageLatestVersionUrl : ( pkgName : string ) =>
28
+ `https://unpkg.com/${ pkgName } @latest/package.json` ,
29
+ getPackageDirectoryUrl : (
30
+ pkgName : string ,
31
+ pkgVersion : string ,
32
+ pkgPath : string ,
33
+ ) => `https://unpkg.com/${ pkgName } @${ pkgVersion } /${ pkgPath } /?meta` ,
34
+ getPackageFileTextUrl : ( path : string ) => `https://unpkg.com/${ path } ` ,
35
+ }
36
+
11
37
export function createNpmFileSystem (
12
38
getCdnPath = ( uri : URI ) : string | undefined => {
13
39
if ( uri . path === '/node_modules' ) {
@@ -18,7 +44,14 @@ export function createNpmFileSystem(
18
44
} ,
19
45
getPackageVersion ?: ( pkgName : string ) => string | undefined ,
20
46
onFetch ?: ( path : string , content : string ) => void ,
47
+ options ?: CreateNpmFileSystemOptions ,
21
48
) : FileSystem {
49
+ const {
50
+ getPackageDirectoryUrl = defaultUnpkgOptions . getPackageDirectoryUrl ,
51
+ getPackageFileTextUrl = defaultUnpkgOptions . getPackageFileTextUrl ,
52
+ getPackageLatestVersionUrl = defaultUnpkgOptions . getPackageLatestVersionUrl ,
53
+ } = options || { }
54
+
22
55
const fetchResults = new Map < string , Promise < string | undefined > > ( )
23
56
const statCache = new Map < string , { type : FileType } > ( )
24
57
const dirCache = new Map < string , [ string , FileType ] [ ] > ( )
@@ -128,7 +161,7 @@ export function createNpmFileSystem(
128
161
if ( resolvedVersion === 'latest' ) {
129
162
try {
130
163
const data = await fetchJson < { version : string } > (
131
- `https://unpkg.com/ ${ pkgName } @ ${ resolvedVersion } /package.json` ,
164
+ getPackageLatestVersionUrl ( pkgName ) ,
132
165
)
133
166
if ( data ?. version ) {
134
167
actualVersion = data . version
@@ -138,8 +171,7 @@ export function createNpmFileSystem(
138
171
}
139
172
}
140
173
141
- const endpoint = `https://unpkg.com/${ pkgName } @${ actualVersion } /${ pkgPath } /?meta`
142
-
174
+ const endpoint = getPackageDirectoryUrl ( pkgName , actualVersion , pkgPath )
143
175
try {
144
176
const data = await fetchJson < {
145
177
files : {
@@ -202,7 +234,9 @@ export function createNpmFileSystem(
202
234
if ( ( await _stat ( path ) ) ?. type !== ( 1 satisfies FileType . File ) ) {
203
235
return
204
236
}
205
- const text = await fetchText ( `https://unpkg.com/${ path } ` )
237
+ const text = await fetchText (
238
+ getPackageFileTextUrl ( path , pkgName , _version , pkgFilePath ) ,
239
+ )
206
240
if ( text !== undefined ) {
207
241
onFetch ?.( path , text )
208
242
}
0 commit comments