@@ -2,10 +2,10 @@ import * as spec from '@jsii/spec';
2
2
import { loadAssemblyFromPath } from '@jsii/spec' ;
3
3
import * as cp from 'child_process' ;
4
4
import * as fs from 'fs-extra' ;
5
+ import { createRequire } from 'module' ;
5
6
import * as os from 'os' ;
6
7
import * as path from 'path' ;
7
8
import * as tar from 'tar' ;
8
- import * as vm from 'vm' ;
9
9
10
10
import * as api from './api' ;
11
11
import { TOKEN_REF } from './api' ;
@@ -27,8 +27,8 @@ export class Kernel {
27
27
private nextid = 20000 ; // incrementing counter for objid, cbid, promiseid
28
28
private syncInProgress ?: string ; // forbids async calls (begin) while processing sync calls (get/set/invoke)
29
29
private installDir ?: string ;
30
-
31
- private readonly sandbox : vm . Context ;
30
+ /** The internal require function, used instead of the global "require" so that webpack does not transform it... */
31
+ private require ?: typeof require ;
32
32
33
33
/**
34
34
* Creates a jsii kernel object.
@@ -37,27 +37,7 @@ export class Kernel {
37
37
* It's responsibility is to execute the callback and return it's
38
38
* result (or throw an error).
39
39
*/
40
- public constructor ( public callbackHandler : ( callback : api . Callback ) => any ) {
41
- // `setImmediate` is required for tests to pass (it is otherwise
42
- // impossible to wait for in-VM promises to complete)
43
-
44
- // `Buffer` is required when using simple-resource-bundler.
45
-
46
- // HACK: when we webpack @jsii /runtime, all "require" statements get transpiled,
47
- // so modules can be resolved within the pack. However, here we actually want to
48
- // let loaded modules to use the native node "require" method.
49
- // I wonder if webpack has some pragma that allows opting-out at certain points
50
- // in the code.
51
- // eslint-disable-next-line @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires
52
- const moduleLoad = require ( 'module' ) . Module . _load ;
53
- const nodeRequire = ( p : string ) => moduleLoad ( p , module , false ) ;
54
-
55
- this . sandbox = vm . createContext ( {
56
- Buffer, // to use simple-resource-bundler
57
- setImmediate, // async tests
58
- require : nodeRequire , // modules need to "require"
59
- } ) ;
60
- }
40
+ public constructor ( public callbackHandler : ( callback : api . Callback ) => any ) { }
61
41
62
42
public load ( req : api . LoadRequest ) : api . LoadResponse {
63
43
this . _debug ( 'load' , req ) ;
@@ -122,11 +102,8 @@ export class Kernel {
122
102
throw new Error ( `Error for package tarball ${ req . tarball } : ${ e . message } ` ) ;
123
103
}
124
104
125
- // load the module and capture it's closure
126
- const closure = this . _execute (
127
- `require(String.raw\`${ packageDir } \`)` ,
128
- packageDir ,
129
- ) ;
105
+ // load the module and capture its closure
106
+ const closure = this . require ! ( packageDir ) ;
130
107
const assm = new Assembly ( assmSpec , closure ) ;
131
108
this . _addAssembly ( assm ) ;
132
109
@@ -205,8 +182,9 @@ export class Kernel {
205
182
206
183
const prototype = this . _findSymbol ( fqn ) ;
207
184
208
- const value = this . _ensureSync ( `property ${ property } ` , ( ) =>
209
- this . _wrapSandboxCode ( ( ) => prototype [ property ] ) ,
185
+ const value = this . _ensureSync (
186
+ `property ${ property } ` ,
187
+ ( ) => prototype [ property ] ,
210
188
) ;
211
189
212
190
this . _debug ( 'value:' , value ) ;
@@ -231,15 +209,14 @@ export class Kernel {
231
209
232
210
const prototype = this . _findSymbol ( fqn ) ;
233
211
234
- this . _ensureSync ( `property ${ property } ` , ( ) =>
235
- this . _wrapSandboxCode (
236
- ( ) =>
237
- ( prototype [ property ] = this . _toSandbox (
238
- value ,
239
- ti ,
240
- `assigned to static property ${ symbol } ` ,
241
- ) ) ,
242
- ) ,
212
+ this . _ensureSync (
213
+ `property ${ property } ` ,
214
+ ( ) =>
215
+ ( prototype [ property ] = this . _toSandbox (
216
+ value ,
217
+ ti ,
218
+ `assigned to static property ${ symbol } ` ,
219
+ ) ) ,
243
220
) ;
244
221
245
222
return { } ;
@@ -262,7 +239,7 @@ export class Kernel {
262
239
// by jsii overrides.
263
240
const value = this . _ensureSync (
264
241
`property '${ objref [ TOKEN_REF ] } .${ propertyToGet } '` ,
265
- ( ) => this . _wrapSandboxCode ( ( ) => instance [ propertyToGet ] ) ,
242
+ ( ) => instance [ propertyToGet ] ,
266
243
) ;
267
244
this . _debug ( 'value:' , value ) ;
268
245
const ret = this . _fromSandbox ( value , ti , `of property ${ fqn } .${ property } ` ) ;
@@ -285,15 +262,14 @@ export class Kernel {
285
262
286
263
const propertyToSet = this . _findPropertyTarget ( instance , property ) ;
287
264
288
- this . _ensureSync ( `property '${ objref [ TOKEN_REF ] } .${ propertyToSet } '` , ( ) =>
289
- this . _wrapSandboxCode (
290
- ( ) =>
291
- ( instance [ propertyToSet ] = this . _toSandbox (
292
- value ,
293
- propInfo ,
294
- `assigned to property ${ fqn } .${ property } ` ,
295
- ) ) ,
296
- ) ,
265
+ this . _ensureSync (
266
+ `property '${ objref [ TOKEN_REF ] } .${ propertyToSet } '` ,
267
+ ( ) =>
268
+ ( instance [ propertyToSet ] = this . _toSandbox (
269
+ value ,
270
+ propInfo ,
271
+ `assigned to property ${ fqn } .${ property } ` ,
272
+ ) ) ,
297
273
) ;
298
274
299
275
return { } ;
@@ -315,14 +291,12 @@ export class Kernel {
315
291
const ret = this . _ensureSync (
316
292
`method '${ objref [ TOKEN_REF ] } .${ method } '` ,
317
293
( ) => {
318
- return this . _wrapSandboxCode ( ( ) =>
319
- fn . apply (
320
- obj ,
321
- this . _toSandboxValues (
322
- args ,
323
- `method ${ fqn ? `${ fqn } #` : '' } ${ method } ` ,
324
- ti . parameters ,
325
- ) ,
294
+ return fn . apply (
295
+ obj ,
296
+ this . _toSandboxValues (
297
+ args ,
298
+ `method ${ fqn ? `${ fqn } #` : '' } ${ method } ` ,
299
+ ti . parameters ,
326
300
) ,
327
301
) ;
328
302
} ,
@@ -359,14 +333,12 @@ export class Kernel {
359
333
const fn = prototype [ method ] as ( ...params : any [ ] ) => any ;
360
334
361
335
const ret = this . _ensureSync ( `method '${ fqn } .${ method } '` , ( ) => {
362
- return this . _wrapSandboxCode ( ( ) =>
363
- fn . apply (
364
- prototype ,
365
- this . _toSandboxValues (
366
- args ,
367
- `static method ${ fqn } .${ method } ` ,
368
- ti . parameters ,
369
- ) ,
336
+ return fn . apply (
337
+ prototype ,
338
+ this . _toSandboxValues (
339
+ args ,
340
+ `static method ${ fqn } .${ method } ` ,
341
+ ti . parameters ,
370
342
) ,
371
343
) ;
372
344
} ) ;
@@ -402,14 +374,12 @@ export class Kernel {
402
374
403
375
const fqn = jsiiTypeFqn ( obj ) ;
404
376
405
- const promise = this . _wrapSandboxCode ( ( ) =>
406
- fn . apply (
407
- obj ,
408
- this . _toSandboxValues (
409
- args ,
410
- `async method ${ fqn ? `${ fqn } #` : '' } ${ method } ` ,
411
- ti . parameters ,
412
- ) ,
377
+ const promise = fn . apply (
378
+ obj ,
379
+ this . _toSandboxValues (
380
+ args ,
381
+ `async method ${ fqn ? `${ fqn } #` : '' } ${ method } ` ,
382
+ ti . parameters ,
413
383
) ,
414
384
) as Promise < any > ;
415
385
@@ -579,6 +549,7 @@ export class Kernel {
579
549
private _getPackageDir ( pkgname : string ) : string {
580
550
if ( ! this . installDir ) {
581
551
this . installDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'jsii-kernel-' ) ) ;
552
+ this . require = createRequire ( this . installDir ) ;
582
553
fs . mkdirpSync ( path . join ( this . installDir , 'node_modules' ) ) ;
583
554
this . _debug ( 'creating jsii-kernel modules workdir:' , this . installDir ) ;
584
555
@@ -597,15 +568,12 @@ export class Kernel {
597
568
598
569
const ctorResult = this . _findCtor ( fqn , requestArgs ) ;
599
570
const ctor = ctorResult . ctor ;
600
- const obj = this . _wrapSandboxCode (
601
- ( ) =>
602
- new ctor (
603
- ...this . _toSandboxValues (
604
- requestArgs ,
605
- `new ${ fqn } ` ,
606
- ctorResult . parameters ,
607
- ) ,
608
- ) ,
571
+ const obj = new ctor (
572
+ ...this . _toSandboxValues (
573
+ requestArgs ,
574
+ `new ${ fqn } ` ,
575
+ ctorResult . parameters ,
576
+ ) ,
609
577
) ;
610
578
const objref = this . objects . registerObject ( obj , fqn , req . interfaces ?? [ ] ) ;
611
579
@@ -1276,23 +1244,6 @@ export class Kernel {
1276
1244
private _makeprid ( ) {
1277
1245
return `jsii::promise::${ this . nextid ++ } ` ;
1278
1246
}
1279
-
1280
- private _wrapSandboxCode < T > ( fn : ( ) => T ) : T {
1281
- return fn ( ) ;
1282
- }
1283
-
1284
- /**
1285
- * Executes arbitrary code in a VM sandbox.
1286
- *
1287
- * @param code JavaScript code to be executed in the VM
1288
- * @param filename the file name to use for the executed code
1289
- *
1290
- * @returns the result of evaluating the code
1291
- */
1292
- private _execute ( code : string , filename : string ) {
1293
- const script = new vm . Script ( code , { filename } ) ;
1294
- return script . runInContext ( this . sandbox , { displayErrors : true } ) ;
1295
- }
1296
1247
}
1297
1248
1298
1249
interface Callback {
0 commit comments