import vm from 'vm' ; import * as fetch from 'cross-fetch' ; import { FabSpecExports } from '@fab/core' ; import Stream from 'stream' ; import { ReadableStream as WebReadableStream } from 'web-streams-polyfill/ponyfill/es2018' ; export function HybridReadableStream ( ... args ) { const readableStream = new WebReadableStream ( ... args ) ; return new Proxy ( readableStream , { getPrototypeOf ( ) { return Stream . Readable . prototype ; } , get ( target , prop , receiver ) { if ( prop === 'on' ) return ( ) => { } ; return Reflect . get ( target , prop , receiver ) ; } , } ) ; } global . ReadableStream = WebReadableStream ; export default async ( src , enhanced_fetch ) : Promise < FabSpecExports > => { const sandbox = { fetch : enhanced_fetch , Request : fetch . Request , Response : fetch . Response , Headers : fetch . Headers , URL : URL , ReadableStream : HybridReadableStream , console : { log : console . log , error : console . error , warn : console . warn , } , NODE_ENV : 'production' , process : { env : { NODE_ENV : 'production' , } , } , setTimeout , setImmediate , clearTimeout , } ; const script = new vm . Script ( src ) ; const exp = { } ; const ctx = Object . assign ( { } , sandbox , { module : { exports : exp } , exports : exp } ) ; const retval = script . runInNewContext ( ctx ) ; return Object . assign ( { } , exp , retval ) ; } ;