1
1
import ErrorStackParser from "./vendor/stackframe/error-stack-parser" ;
2
2
import {
3
- IN_NODE ,
4
- IN_NODE_ESM ,
5
3
IN_BROWSER_MAIN_THREAD ,
6
4
IN_BROWSER_WEB_WORKER ,
7
- IN_NODE_COMMONJS ,
8
5
} from "./environments" ;
9
6
import { Lockfile } from "./types" ;
10
7
11
- let nodeUrlMod : typeof import ( "node:url" ) ;
12
- let nodePath : typeof import ( "node:path" ) ;
13
- let nodeVmMod : typeof import ( "node:vm" ) ;
14
- /** @private */
15
- export let nodeFSMod : typeof import ( "node:fs" ) ;
16
- /** @private */
17
- export let nodeFsPromisesMod : typeof import ( "node:fs/promises" ) ;
18
-
19
8
declare var globalThis : {
20
9
importScripts : ( url : string ) => void ;
21
10
document ?: typeof document ;
22
11
fetch ?: typeof fetch ;
23
12
} ;
24
13
25
- /**
26
- * If we're in node, it's most convenient to import various node modules on
27
- * initialization. Otherwise, this does nothing.
28
- * @private
29
- */
30
- export async function initNodeModules ( ) {
31
- if ( ! IN_NODE ) {
32
- return ;
33
- }
34
- // @ts -ignore
35
- nodeUrlMod = ( await import ( "node:url" ) ) . default ;
36
- nodeFSMod = await import ( "node:fs" ) ;
37
- nodeFsPromisesMod = await import ( "node:fs/promises" ) ;
38
-
39
- // @ts -ignore
40
- nodeVmMod = ( await import ( "node:vm" ) ) . default ;
41
- nodePath = await import ( "node:path" ) ;
42
- pathSep = nodePath . sep ;
43
-
44
- // Emscripten uses `require`, so if it's missing (because we were imported as
45
- // an ES6 module) we need to polyfill `require` with `import`. `import` is
46
- // async and `require` is synchronous, so we import all packages that might be
47
- // required up front and define require to look them up in this table.
48
-
49
- if ( typeof require !== "undefined" ) {
50
- return ;
51
- }
52
- // These are all the packages required in pyodide.asm.js. You can get this
53
- // list with:
54
- // $ grep -o 'require("[a-z]*")' pyodide.asm.js | sort -u
55
- const fs = nodeFSMod ;
56
- const crypto = await import ( "node:crypto" ) ;
57
- const ws = await import ( "ws" ) ;
58
- const child_process = await import ( "node:child_process" ) ;
59
- const node_modules : { [ mode : string ] : any } = {
60
- fs,
61
- crypto,
62
- ws,
63
- child_process,
64
- } ;
65
- // Since we're in an ES6 module, this is only modifying the module namespace,
66
- // it's still private to Pyodide.
67
- ( globalThis as any ) . require = function ( mod : string ) : any {
68
- return node_modules [ mod ] ;
69
- } ;
70
- }
71
-
72
- function node_resolvePath ( path : string , base ?: string ) : string {
73
- return nodePath . resolve ( base || "." , path ) ;
74
- }
75
-
76
14
function browser_resolvePath ( path : string , base ?: string ) : string {
77
15
if ( base === undefined ) {
78
16
// @ts -ignore
@@ -82,11 +20,7 @@ function browser_resolvePath(path: string, base?: string): string {
82
20
}
83
21
84
22
export let resolvePath : ( rest : string , base ?: string ) => string ;
85
- if ( IN_NODE ) {
86
- resolvePath = node_resolvePath ;
87
- } else {
88
- resolvePath = browser_resolvePath ;
89
- }
23
+ resolvePath = browser_resolvePath ;
90
24
91
25
/**
92
26
* Get the path separator. If we are on Linux or in the browser, it's /.
@@ -95,45 +29,6 @@ if (IN_NODE) {
95
29
*/
96
30
export let pathSep : string ;
97
31
98
- if ( ! IN_NODE ) {
99
- pathSep = "/" ;
100
- }
101
-
102
- /**
103
- * Load a binary file, only for use in Node. If the path explicitly is a URL,
104
- * then fetch from a URL, else load from the file system.
105
- * @param indexURL base path to resolve relative paths
106
- * @param path the path to load
107
- * @param checksum sha-256 checksum of the package
108
- * @returns An ArrayBuffer containing the binary data
109
- * @private
110
- */
111
- function node_getBinaryResponse (
112
- path : string ,
113
- _file_sub_resource_hash ?: string | undefined , // Ignoring sub resource hash. See issue-2431.
114
- ) :
115
- | { response : Promise < Response > ; binary ?: undefined }
116
- | { binary : Promise < Uint8Array > } {
117
- if ( path . startsWith ( "file://" ) ) {
118
- // handle file:// with filesystem operations rather than with fetch.
119
- path = path . slice ( "file://" . length ) ;
120
- }
121
- if ( path . includes ( "://" ) ) {
122
- // If it has a protocol, make a fetch request
123
- return { response : fetch ( path ) } ;
124
- } else {
125
- // Otherwise get it from the file system
126
- return {
127
- binary : nodeFsPromisesMod
128
- . readFile ( path )
129
- . then (
130
- ( data : Buffer ) =>
131
- new Uint8Array ( data . buffer , data . byteOffset , data . byteLength ) ,
132
- ) ,
133
- } ;
134
- }
135
- }
136
-
137
32
/**
138
33
* Load a binary file, only for use in browser. Resolves relative paths against
139
34
* indexURL.
@@ -159,11 +54,7 @@ export let getBinaryResponse: (
159
54
) =>
160
55
| { response : Promise < Response > ; binary ?: undefined }
161
56
| { response ?: undefined ; binary : Promise < Uint8Array > } ;
162
- if ( IN_NODE ) {
163
- getBinaryResponse = node_getBinaryResponse ;
164
- } else {
165
- getBinaryResponse = browser_getBinaryResponse ;
166
- }
57
+ getBinaryResponse = browser_getBinaryResponse ;
167
58
168
59
export async function loadBinaryFile (
169
60
path : string ,
@@ -206,53 +97,21 @@ if (IN_BROWSER_MAIN_THREAD) {
206
97
}
207
98
}
208
99
} ;
209
- } else if ( IN_NODE ) {
210
- loadScript = nodeLoadScript ;
211
100
} else {
212
101
throw new Error ( "Cannot determine runtime environment" ) ;
213
102
}
214
103
215
- /**
216
- * Load a text file and executes it as Javascript
217
- * @param url The path to load. May be a url or a relative file system path.
218
- * @private
219
- */
220
- async function nodeLoadScript ( url : string ) {
221
- if ( url . startsWith ( "file://" ) ) {
222
- // handle file:// with filesystem operations rather than with fetch.
223
- url = url . slice ( "file://" . length ) ;
224
- }
225
- if ( url . includes ( "://" ) ) {
226
- // If it's a url, load it with fetch then eval it.
227
- nodeVmMod . runInThisContext ( await ( await fetch ( url ) ) . text ( ) ) ;
228
- } else {
229
- // Otherwise, hopefully it is a relative path we can load from the file
230
- // system.
231
- await import ( /* webpackIgnore: true */ nodeUrlMod . pathToFileURL ( url ) . href ) ;
232
- }
233
- }
234
104
235
105
export async function loadLockFile ( lockFileURL : string ) : Promise < Lockfile > {
236
- if ( IN_NODE ) {
237
- await initNodeModules ( ) ;
238
- const package_string = await nodeFsPromisesMod . readFile ( lockFileURL , {
239
- encoding : "utf8" ,
240
- } ) ;
241
- return JSON . parse ( package_string ) ;
242
- } else {
243
- let response = await fetch ( lockFileURL ) ;
244
- return await response . json ( ) ;
245
- }
106
+ let response = await fetch ( lockFileURL ) ;
107
+ return await response . json ( ) ;
246
108
}
247
109
248
110
/**
249
111
* Calculate the directory name of the current module.
250
112
* This is used to guess the indexURL when it is not provided.
251
113
*/
252
114
export async function calculateDirname ( ) : Promise < string > {
253
- if ( IN_NODE_COMMONJS ) {
254
- return __dirname ;
255
- }
256
115
257
116
let err : Error ;
258
117
try {
@@ -262,19 +121,6 @@ export async function calculateDirname(): Promise<string> {
262
121
}
263
122
let fileName = ErrorStackParser . parse ( err ) [ 0 ] . fileName ! ;
264
123
265
- if ( IN_NODE && ! fileName . startsWith ( "file://" ) ) {
266
- fileName = `file://${ fileName } ` ; // Error stack filenames are not starting with `file://` in `Bun`
267
- }
268
-
269
- if ( IN_NODE_ESM ) {
270
- const nodePath = await import ( "node:path" ) ;
271
- const nodeUrl = await import ( "node:url" ) ;
272
-
273
- // FIXME: We would like to use import.meta.url here,
274
- // but mocha seems to mess with compiling typescript files to ES6.
275
- return nodeUrl . fileURLToPath ( nodePath . dirname ( fileName ) ) ;
276
- }
277
-
278
124
const indexOfLastSlash = fileName . lastIndexOf ( pathSep ) ;
279
125
if ( indexOfLastSlash === - 1 ) {
280
126
throw new Error (
0 commit comments