33const fs = require ( 'fs' ) ;
44const Module = require ( 'module' ) ;
55const ts = require ( 'typescript' ) ;
6+ const domain = require ( 'domain' ) ;
7+
8+ const d = domain . create ( ) ;
9+ d . on ( 'error' , ( ) => { } ) ;
610
711const functions = [ ] ;
812
913const scanned = new Set ( [
1014 process . mainModule ,
1115] ) ;
16+ const forbiddenPaths = new Set ( [
17+ // duplicate with buffer module
18+ 'globalThis?.Buffer' ,
19+ // prints experimental warning
20+ 'globalThis?.fetch' ,
21+ // prints experimental warning
22+ 'globalThis?.FormData' ,
23+ // prints experimental warning
24+ 'globalThis?.Headers' ,
25+ // prints experimental warning
26+ 'globalThis?.Request' ,
27+ // prints experimental warning
28+ 'globalThis?.Response' ,
29+ ] ) ;
1230const scan = ( ns , path ) => {
1331 if ( scanned . has ( ns ) ) {
1432 return ;
1533 }
16- if ( path === 'globalThis.Buffer' ) {
34+ if ( forbiddenPaths . has ( path ) ) {
1735 return ;
1836 }
1937 scanned . add ( ns ) ;
@@ -29,7 +47,9 @@ const scan = (ns, path) => {
2947 return ;
3048 }
3149 try {
32- ns [ name ] ;
50+ d . run ( ( ) => {
51+ ns [ name ] ;
52+ } ) ;
3353 } catch {
3454 return ;
3555 }
@@ -67,7 +87,7 @@ host.readFile = (name) => {
6787 if ( name === 'index.ts' ) {
6888 return `
6989 ${ [ ...required ] . map ( ( r ) => `import * as ${ r } from '${ r } ';` ) . join ( '\n' ) }
70- ${ functions . join ( '\n' ) }
90+ ${ functions . join ( '\n' ) . replaceAll ( '?' , '' ) }
7191 ` ;
7292 }
7393 return originalReadFile . call ( host , name ) ;
0 commit comments