Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 | 3x 3x 3x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 752x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 7x 7x 7x 1x 7x 2x 7x 1x 1x 138x 138x 119x 19x 2x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 1x 17x 17x 17x 6x 6x 6x 17x 17x 17x 17x 17x 229x 37x 37x 6x 6x 3x 34x 192x 192x 138x 54x 54x 54x 54x 18x 36x 425x 425x 2x 423x 279x 6x 12x 126x 34x 34x 34x 34x 34x 71x 34x 755x 430x 430x 430x 229x 229x 226x 3x 94x 94x 94x 422x 422x 422x 94x 2x 2x 2x 2x 7x 77x 10x 10x 9x 2x 2x 2x 2x 13x 8x 11x 1x 10x 7x 7x 7x 7x 7x 3x 3x 7x 7x 7x 7x 7x 8x 8x 6x 6x 8x 8x 8x 6x 6x 6x 6x 6x 7x 7x 7x 5x 7x 7x 7x 7x 7x 7x 7x 3x 7x 7x 7x 49x 7x 49x 7x 7x 3x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x | import { makeShared } from './sharedFactory' import { API, Token, Response, ValueWrapperRecord, ValueWrapperWellKnown, ValueWrapper, ResponseFailed, World, } from './interface.js' import { createProxyFactoryFactory } from './proxyFactoryBuilder' const SES = { makeShared, createProxyFactoryFactory, init, createScript, fastInit, fastInitNode, DEV: false } // this need to be run before any other script to get properly untainted global export function init(configureCallback ?: API.ConfigureCallback) { 'use strict'; const createProxyFactory = SES.createProxyFactoryFactory(SES) // Must not use any global object // And using only the frozen object returns from makeShared const shared = SES.makeShared() // lock down options after this point shared.dropPrototypeRecursive(SES as any) const FArrayIsArray = shared.FArrayIsArray const FError = shared.FError const FMap = shared.FMap const FBMapSet = shared.FBMapSet const FBMapGet = shared.FBMapGet const FBMapHas = shared.FBMapHas const FWeakMap = shared.FWeakMap const FBWeakMapHas = shared.FBWeakMapHas const FBWeakMapSet = shared.FBWeakMapSet const FBWeakMapGet = shared.FBWeakMapGet const FReflect = shared.FReflect const FCreateEmpty = shared.FCreateEmpty const FBArrayMap = shared.FBArrayMap const FBArrayToIterator = shared.FBArrayToIterator const dropPrototypeRecursive = shared.dropPrototypeRecursive const safeGetProp = shared.safeGetProp return function createServer<T>(root: T, options: { fixInternalSlot: boolean} = { fixInternalSlot: false}) { const successVal = <T>(v: T) => ({ success: true as const, value: v }) const failVal = <T>(v: T) => ({ success: false as const, value: v }) // real object in this world to token const realToToken = new FWeakMap<object, Token>() // token to real object in this world const tokenToReal = new FWeakMap<Token, object>() // token from external world to proxy in this world const tokenToProxy = new FWeakMap<Token, object>() // proxy in this world to token from external const proxyToToken = new FWeakMap<object, Token>() // token from external world to proxy in this world const tokenToRedirected = new FWeakMap<Token, object>() // proxy in this world to token from external const redirectedToToken = new FWeakMap<object, Token>() // not freezing these until configure complete const metaAttachCallBacks: API.IMetaAttach<any>[] = [] const proxyInitCallbacks: API.ICustomProxyInit[] = [] const customTraps: Record<string, API.ICustomTrap> = {} const unwrapCallBacks: API.UnwrapCallBack[] = [] const trapHooks: API.TrapHooks[] = [] const wellKnownValues = new FMap<string | symbol, any>() const wellKnownValuesReversed = new FMap<any, string | symbol>() FReflect.setPrototypeOf(customTraps, null) const registerMetaCallback: API.RegisterMetaCallback = (cb) => { metaAttachCallBacks[metaAttachCallBacks.length] = cb } const registerCustomProxyInit: API.RegisterCustomProxyInit = (cb) => { proxyInitCallbacks[proxyInitCallbacks.length] = cb } const registerCustomTrap: API.RegisterCustomTrap = (str, cb) => { if (FReflect.getOwnPropertyDescriptor(customTraps, str)) { throw new FError(`trap ${str} already exists`) } customTraps[str] = (...args) => { try { // enforce safety policy const result = cb(...args) dropPrototypeRecursive(result) return result } catch (err) { // this shouldn't happen if (SES.DEV) debugger return badPayload } } } const registerUnwrapCallback: API.RegisterUnwrapCallback = (cb) => { unwrapCallBacks[unwrapCallBacks.length] = cb } const registerTrapHooks: API.RegisterTrapHooks = (cb) => { trapHooks[trapHooks.length] = cb } const registerWellKnownValue: API.RegisterWellKnownValue = (key, value) => { FBMapSet(wellKnownValues, key, value) FBMapSet(wellKnownValuesReversed, value, key) } /** * get a safe token that represent this object * @param obj */ function toToken<T extends object>(obj: T, world: World, type: 'function' | 'object'): Token { Iif (redirectedToToken.has(obj)) { return redirectedToToken.get(obj)! } if (proxyToToken.has(obj)) { return proxyToToken.get(obj)! } if (realToToken.has(obj)) { return realToToken.get(obj)! } var token: Token = FCreateEmpty({}) token.owner = world token.type = type // wtf? // https://tc39.es/ecma262/#sec-isarray // https://tc39.es/ecma262/#sec-proxy-revocation-functions try { token.isArray = FArrayIsArray(obj) token.isRevoked = false } catch (err) { token.isArray = false token.isRevoked = true } let meta = {} // hooks: attach metadata if (metaAttachCallBacks.length > 0) { for (let i = 0; i < metaAttachCallBacks.length; i++) { let res try { res = metaAttachCallBacks[i](obj) } catch (err) { // mock the error debugger throw 'Token creation error' } meta = { ...meta, ...res } } } // hooks end: attach metadata token.meta = meta token.functionHasNoPrototype = false if (type === 'function') { const desc = FReflect.getOwnPropertyDescriptor(obj, 'prototype') Eif (desc == null) { token.functionHasNoPrototype = true } else if (desc.configurable) { token.functionHasNoPrototype = true } } token.REALLY_DANGEROUS_INVOKE_WITH_RAW_THIS = (self, ...argsWrapped) => { try { for (let i = 0; i < trapHooks.length; i++) { var hook = trapHooks[i] if (hook.dangerousApply) { const res = hook.dangerousApply(self, obj, argsWrapped) if (res != null) { return res } } } } catch (err) { return failVal(toWrapper(err, currentWorld)) } try { const argsRes = FBArrayMap(argsWrapped, i => unwrap(i)) for (let i of FBArrayToIterator(argsRes)) { if (!i.success) { throw new Error('bad input') } } const args = FBArrayMap(argsRes, i => i.value) const res = FReflect.apply(self, obj, args) return successVal(toWrapper(res, currentWorld)) } catch (err) { return failVal(toWrapper(err, currentWorld)) } } dropPrototypeRecursive(token.REALLY_DANGEROUS_INVOKE_WITH_RAW_THIS) FBWeakMapSet(realToToken, obj, token) FBWeakMapSet(tokenToReal, token, obj) return token } function unwrapToken(token: Token): Response<any, any> { if (FBWeakMapHas(tokenToReal, token)) { const real = FBWeakMapGet(tokenToReal, token) for (let i = 0; i < unwrapCallBacks.length; i++) { try { unwrapCallBacks[i](real) } catch (err) { // return it is allowed because user do allowed to do it return { success: false, value: err } } } return { success: true, value: real } } Iif (FBWeakMapHas(tokenToRedirected, token)) { return { success: true, value: FBWeakMapGet(tokenToRedirected, token) } } if (FBWeakMapHas(tokenToProxy, token)) { return { success: true, value: FBWeakMapGet(tokenToProxy, token) } } // to fake const type: string | null = FReflect.get(token, 'type') const world: World = FReflect.get(token, 'owner') Iif (world === currentWorld) { return { success: true, value: new FError('Unexpected owner of current world') } } switch (type) { case 'function': return { success: true, value: createProxy(token) } case 'object': return { success: true, value: createProxy(token) } default: return { success: true, value: new FError('bad type') } } } function toWrapper(obj: any, world: World): ValueWrapper { // skip all following process if the value is `well known` Iif (FBMapHas(wellKnownValuesReversed, obj)) { return { type: 'well-known', value: FBMapGet(wellKnownValuesReversed, obj) } } if (obj === null) { return { type: 'primitive', value: obj } } switch (typeof obj) { case 'bigint': case 'boolean': case 'number': case 'string': case 'symbol': return { type: 'primitive', value: obj } case 'undefined': return { type: 'primitive', value: undefined // thanks to document.all } case 'function': return { type: 'function', value: toToken(obj, world, 'function') } case 'object': return { type: 'object', value: toToken(obj, world, 'object') } default: throw new FError('how is this possible?') } } function toRecord(obj: any, world: World): ValueWrapper { const keys = FReflect.ownKeys(obj) const target: ValueWrapperRecord = FCreateEmpty({}) as any target.type = 'record' target.value = FCreateEmpty({}) for (let key of FBArrayToIterator(keys)) { // symbol bug target.value[key as any] = toWrapper(obj[key], world) } return target } function unwrap(unsafeObj: ValueWrapper): Response<any, any> { switch (safeGetProp(unsafeObj, 'type')) { case 'primitive': const value = safeGetProp(unsafeObj, 'value') Iif (value != null && (typeof value === 'function' || typeof value === 'object')) { return failVal(new FError('bad')) } return successVal(value) case 'function': case 'object': { const result = unwrapToken(safeGetProp(unsafeObj, 'value') as any) if (result.success) { return successVal(result.value) } else { return failVal(result.value) } } case 'record': { const result = FCreateEmpty({}) const value = safeGetProp(unsafeObj, 'value') as { [key: string]: ValueWrapper } for (let key of FBArrayToIterator(FReflect.ownKeys(value))) { // symbol bug const res = unwrap(value[key as any]) Eif (res.success) { result[key] = res.value } else { return failVal(res.value) } } return successVal(result) } case 'well-known': { const value = safeGetProp(unsafeObj as ValueWrapperWellKnown, 'value')! const result = FBMapGet(wellKnownValues, value) for (let i = 0; i < unwrapCallBacks.length; i++) { try { unwrapCallBacks[i](result) } catch (err) { // return it is allowed because user do allowed to do it return { success: false, value: err } } } return successVal(result) } default: return failVal(new FError('bad wrapper')) } } // this need to be initialized outside of service catch, so it can't throw yet another stack overflow const badPayload: ResponseFailed<ValueWrapper> = dropPrototypeRecursive({ success: false, value: { type: 'primitive', value: 'Internal Error' } }) type MapToWrapper<T> = { [Key in keyof T]: ValueWrapper } type BeArray<T> = T extends any[] ? T : never function createHandler< T extends keyof typeof FReflect, U extends (typeof FReflect)[T], V extends BeArray<MapToWrapper<Parameters<U>>>, >(key: T) { return function (...args: V) { try { for (let i = 0; i < trapHooks.length; i++) { if ((trapHooks[i] as any)[key]) { const fn = (trapHooks[i] as any)[key] const res = fn(...args) Eif (res != null) { return dropPrototypeRecursive(res) } } } const unwrappedRes: Response<any, any>[] = FBArrayMap(args, (i: ValueWrapper) => unwrap(i)) for (let item of FBArrayToIterator(unwrappedRes)) { if (!item.success) { return failPayload(item) } } const unwrapped = FBArrayMap(unwrappedRes, (i: Response<any, any>) => i.value) let value: any let success: boolean // start of zone that user mat throw error try { value = FReflect.apply(FReflect[key], null, unwrapped) success = true } catch (err) { // return this is okay success = false value = err } // end of zone return dropPrototypeRecursive({ success, value: toWrapper(value, currentWorld) }) } catch (err) { // just don't touch any function because it may cause yet another stack overflow here if (SES.DEV) debugger return badPayload } } } const failPayload = <T>(arg: ResponseFailed<T>): ResponseFailed<ValueWrapper> => { var wrapped = toWrapper(arg.value, currentWorld) return { success: false, value: wrapped } } // These shouldn't leak refs const currentWorld: World = { create(world: World) { let res try { res = unwrap(world.getRoot().value) } catch (err) { // this shouldn't happen if (SES.DEV) debugger throw new Error('failed create') } Eif (res.success) { return res.value } else { throw res.value } }, getRoot() { try { return dropPrototypeRecursive({ success: true, value: toWrapper(root, currentWorld) }) } catch (err) { // this simply shouldn't happen, so we ate it if (SES.DEV) debugger return badPayload } }, getCustomTrap (name) { return customTraps[name] }, // TODO: redo with custom resolve trap_get: createHandler('get'), // TODO: redo with custom resolve trap_set: createHandler('set'), // trap_getOwnPropertyDescriptor: createHandler('getOwnPropertyDescriptor'), trap_getOwnPropertyDescriptor(tokenW: ValueWrapper, keyW: ValueWrapper) { try { for (let i = 0; i < trapHooks.length; i++) { const fn = trapHooks[i].getOwnPropertyDescriptor Iif (fn) { const res = fn(tokenW, keyW) if (res != null) { return dropPrototypeRecursive(res) } } } const tokenT = unwrap(tokenW) const keyT = unwrap(keyW) if (!tokenT.success) return failPayload(tokenT) Iif (!keyT.success) return failPayload(keyT) let value: any let success: boolean // start of zone that user mat throw error try { value = FReflect.getOwnPropertyDescriptor(tokenT.value, keyT.value) success = true } catch (err) { // forward user error success = false value = err } return dropPrototypeRecursive({ success, value: success && typeof value === 'object' ? toRecord(value, currentWorld) : toWrapper(value, currentWorld) }) } catch (err) { // just don't touch any function because it may cause yet another stack overflow here if (SES.DEV) debugger return badPayload } }, trap_ownKeys (tokenW: ValueWrapper) { try { for (let i = 0; i < trapHooks.length; i++) { const fn = trapHooks[i].ownKeys if (fn) { const res = fn(tokenW) if (res != null) { return dropPrototypeRecursive(res) } } } const token = unwrap(tokenW) if (!token.success) return failPayload(token) let value: any let success: boolean // start of zone that user mat throw error try { value = FReflect.ownKeys(token.value) success = true } catch (err) { // forward user error success = false value = err } return dropPrototypeRecursive({ success, value: success ? toRecord(value, currentWorld) : toWrapper(value, currentWorld) }) } catch (err) { // just don't touch any function because it may cause yet another stack overflow here if (SES.DEV) debugger return badPayload } }, trap_apply: createHandler('apply'), trap_construct: createHandler('construct'), trap_getPrototypeOf: createHandler('getPrototypeOf'), trap_defineProperty: createHandler('defineProperty'), trap_setPrototypeOf: createHandler('setPrototypeOf'), trap_isExtensible: createHandler('isExtensible'), trap_preventExtensions: createHandler('preventExtensions'), trap_has: createHandler('has'), trap_deleteProperty: createHandler('deleteProperty') } dropPrototypeRecursive(currentWorld) const createProxy = createProxyFactory( shared, unwrap, toWrapper, toRecord, currentWorld, proxyToToken, tokenToProxy, redirectedToToken, tokenToRedirected, proxyInitCallbacks ) if (configureCallback != null) { configureCallback({ registerMetaCallback, registerCustomTrap, registerCustomProxyInit, registerUnwrapCallback, registerTrapHooks, registerWellKnownValue, shared, proxyToToken, tokenToProxy, tokenToRedirected, redirectedToToken, realToToken, tokenToReal, unwrap, toWrapper, toRecord, world: currentWorld }) } function wrapMethodAndInvokeThisAsRaw (func: (...any: any[]) => any) { var wrapped = new Proxy(func, { apply (target, self, args) { if (!FBWeakMapHas(proxyToToken, self) && !FBWeakMapHas(redirectedToToken, self)) { return FReflect.apply(func, self, args) } else { let token!: Token if (FBWeakMapHas(proxyToToken, self)) { token = FBWeakMapGet(proxyToToken, self) } else { token = FBWeakMapGet(redirectedToToken, self) } const wrappedArgs = FBArrayMap(args, (i) => toWrapper(i, currentWorld)) try { const res = token.REALLY_DANGEROUS_INVOKE_WITH_RAW_THIS(func, ...FBArrayToIterator(wrappedArgs)) if (res.success) { let { success, value } = unwrap(res.value) if (success) { return value } else { throw value } } else { let { success, value } = unwrap(res.value) if (success) { return value } else { throw value } } } catch (err) { let message = 'internal crash' try { message = err.message } finally { throw new Error(message) } } } } }) return wrapped } Iif (options.fixInternalSlot) { for (let obj of FBArrayToIterator(shared.whitelistedPrototypes)) { for (let key of FBArrayToIterator(FReflect.ownKeys(obj))) { if (key === 'constructor') { continue // we should not patch the constructor for obvious reason } const desc = FReflect.getOwnPropertyDescriptor(obj, key) if (!desc) continue FReflect.setPrototypeOf(desc, null) if (FReflect.getOwnPropertyDescriptor(desc, 'value')) { // has value if (typeof desc.value === 'function') { FReflect.defineProperty(obj, key, { ...desc, value: wrapMethodAndInvokeThisAsRaw(desc.value) }) } } else { // has getter/setter if (typeof desc.set === 'function') { FReflect.defineProperty(obj, key, { ...desc, set: wrapMethodAndInvokeThisAsRaw(desc.set) }) } if (typeof desc.get === 'function') { FReflect.defineProperty(obj, key, { ...desc, get: wrapMethodAndInvokeThisAsRaw(desc.get) }) } } } } } dropPrototypeRecursive(metaAttachCallBacks) dropPrototypeRecursive(proxyInitCallbacks) dropPrototypeRecursive(customTraps) dropPrototypeRecursive(unwrapCallBacks) dropPrototypeRecursive(trapHooks) return currentWorld } } export function createScript(obj: any) { const keys = Object.keys(obj) let text = ` (function (SES) { "use strict"; ` for (let key of keys) { text += ` const ${key} = ${Reflect.apply( typeof obj[key] === 'function' ? Function.prototype.toString : typeof obj[key] === 'boolean' ? Boolean.prototype.toString : Object.prototype.toString , obj[key], [] )} ` } for (let key of keys) { text += ` SES.${key} = ${key} ` } text += ` return SES })({}) ` return text } /* istanbul ignore next */ export async function fastInit( root: any, configureCallback ?: API.ConfigureCallback, remoteConfigureCallback ?: API.ConfigureCallback | string, remoteRootExpr = "globalThis", options: { fixInternalSlot: boolean} = { fixInternalSlot: false} ) { let iframe = document.createElement('iframe') iframe.src = 'about:blank' iframe.setAttribute('sandbox', 'allow-same-origin allow-scripts'); iframe.style.display = 'none' document.body.append(iframe) await Promise.race([ new Promise(resolve => iframe.onload = resolve), new Promise(resolve => setTimeout(resolve, 100)) ]) const createRoot = init(configureCallback) const server = createRoot(root, options) let realm = (iframe.contentWindow as any).eval(` "use strict"; const SES = ${createScript(SES)} const createRoot = SES.init(${remoteConfigureCallback ? remoteConfigureCallback.toString() : ''}) const server = createRoot(${remoteRootExpr}, ${JSON.stringify(options)}) server //# sourceURL=sandbox-service:/SES.js `) delete (iframe.contentWindow as any).opener const remote = server.create(realm) if (SES.DEV) { console.error('iframe is not detached in DEV mode') } else { // say good bye to the iframe, even ourself can't access the `real` sandbox object after this point iframe.remove() } return remote } export function fastInitNode(root: any, configureCallback ?: API.ConfigureCallback, remoteConfigureCallback ?: API.ConfigureCallback | string) { const createRoot = init(configureCallback) const server = createRoot(root) const rawRealGlobalExpr = `(0, eval)("'use strict'; this")` let script = createScript(SES) let fullScript = ` "use strict"; const SES = ${script} const createRoot = SES.init(${remoteConfigureCallback ? remoteConfigureCallback.toString() : ''}) const server = createRoot(${rawRealGlobalExpr}) server ` /* istanbul ignore next */ if (process.env.NODE_ENV === 'test' && /cov_[a-zA-Z0-9]+/.test(fullScript)) { // use prebuilds /* istanbul ignore next */ const preBuild = eval(` "use strict"; const path = require('path') const file = require('fs').readFileSync(path.resolve(__dirname, './test/dist.js'), { encoding: 'utf8' }) file `) fullScript = ` "use strict"; const SES = ${preBuild} const createRoot = SES.init(${remoteConfigureCallback ? remoteConfigureCallback.toString() : ''}) const server = createRoot(${rawRealGlobalExpr}) server ` } const { runInNewContext } = require('vm') let sandboxGlobal = runInNewContext(rawRealGlobalExpr) let realm = sandboxGlobal.eval(fullScript) sandboxGlobal = null // throw the reference away const remote = server.create(realm) return remote } export default SES |