|
| 1 | +// Flags: --experimental-modules --harmony-dynamic-import |
| 2 | +'use strict'; |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const { URL } = require('url'); |
| 6 | +const vm = require('vm'); |
| 7 | + |
| 8 | +common.crashOnUnhandledRejection(); |
| 9 | + |
| 10 | +const relativePath = './test-esm-ok.mjs'; |
| 11 | +const absolutePath = require.resolve('./test-esm-ok.mjs'); |
| 12 | +const targetURL = new URL('file:///'); |
| 13 | +targetURL.pathname = absolutePath; |
| 14 | + |
| 15 | +function expectErrorProperty(result, propertyKey, value) { |
| 16 | + Promise.resolve(result) |
| 17 | + .catch(common.mustCall(error => { |
| 18 | + assert.equal(error[propertyKey], value); |
| 19 | + })); |
| 20 | +} |
| 21 | + |
| 22 | +function expectMissingModuleError(result) { |
| 23 | + expectErrorProperty(result, 'code', 'MODULE_NOT_FOUND'); |
| 24 | +} |
| 25 | + |
| 26 | +function expectInvalidUrlError(result) { |
| 27 | + expectErrorProperty(result, 'code', 'ERR_INVALID_URL'); |
| 28 | +} |
| 29 | + |
| 30 | +function expectInvalidReferrerError(result) { |
| 31 | + expectErrorProperty(result, 'code', 'ERR_INVALID_URL'); |
| 32 | +} |
| 33 | + |
| 34 | +function expectInvalidProtocolError(result) { |
| 35 | + expectErrorProperty(result, 'code', 'ERR_INVALID_PROTOCOL'); |
| 36 | +} |
| 37 | + |
| 38 | +function expectInvalidContextError(result) { |
| 39 | + expectErrorProperty(result, |
| 40 | + 'message', 'import() called outside of main context'); |
| 41 | +} |
| 42 | + |
| 43 | +function expectOkNamespace(result) { |
| 44 | + Promise.resolve(result) |
| 45 | + .then(common.mustCall(ns => { |
| 46 | + // Can't deepStrictEqual because ns isn't a normal object |
| 47 | + assert.deepEqual(ns, { default: true }); |
| 48 | + })); |
| 49 | +} |
| 50 | + |
| 51 | +function expectFsNamespace(result) { |
| 52 | + Promise.resolve(result) |
| 53 | + .then(common.mustCall(ns => { |
| 54 | + assert.equal(typeof ns.default.writeFile, 'function'); |
| 55 | + })); |
| 56 | +} |
| 57 | + |
| 58 | +// For direct use of import expressions inside of CJS or ES modules, including |
| 59 | +// via eval, all kinds of specifiers should work without issue. |
| 60 | +(function testScriptOrModuleImport() { |
| 61 | + // Importing another file, both direct & via eval |
| 62 | + // expectOkNamespace(import(relativePath)); |
| 63 | + expectOkNamespace(eval.call(null, `import("${relativePath}")`)); |
| 64 | + expectOkNamespace(eval(`import("${relativePath}")`)); |
| 65 | + expectOkNamespace(eval.call(null, `import("${targetURL}")`)); |
| 66 | + |
| 67 | + // Importing a built-in, both direct & via eval |
| 68 | + expectFsNamespace(import("fs")); |
| 69 | + expectFsNamespace(eval('import("fs")')); |
| 70 | + expectFsNamespace(eval.call(null, 'import("fs")')); |
| 71 | + |
| 72 | + expectMissingModuleError(import("./not-an-existing-module.mjs")); |
| 73 | + // TODO(jkrems): Right now this doesn't hit a protocol error because the |
| 74 | + // module resolution step already rejects it. These arguably should be |
| 75 | + // protocol errors. |
| 76 | + expectMissingModuleError(import("node:fs")); |
| 77 | + expectMissingModuleError(import('http://example.com/foo.js')); |
| 78 | +})(); |
| 79 | + |
| 80 | +// vm.runInThisContext: |
| 81 | +// * Supports built-ins, always |
| 82 | +// * Supports imports if the script has a known defined origin |
| 83 | +(function testRunInThisContext() { |
| 84 | + // Succeeds because it's got an valid base url |
| 85 | + expectFsNamespace(vm.runInThisContext(`import("fs")`, { |
| 86 | + filename: __filename, |
| 87 | + })); |
| 88 | + expectOkNamespace(vm.runInThisContext(`import("${relativePath}")`, { |
| 89 | + filename: __filename, |
| 90 | + })); |
| 91 | + // Rejects because it's got an invalid referrer URL. |
| 92 | + // TODO(jkrems): Arguably the first two (built-in + absolute URL) could work |
| 93 | + // with some additional effort. |
| 94 | + expectInvalidReferrerError(vm.runInThisContext('import("fs")')); |
| 95 | + expectInvalidReferrerError(vm.runInThisContext(`import("${targetURL}")`)); |
| 96 | + expectInvalidReferrerError(vm.runInThisContext(`import("${relativePath}")`)); |
| 97 | +})(); |
| 98 | + |
| 99 | +// vm.runInNewContext is currently completely unsupported, pending well-defined |
| 100 | +// semantics for per-context/realm module maps in node. |
| 101 | +(function testRunInNewContext() { |
| 102 | + // Rejects because it's running in the wrong context |
| 103 | + expectInvalidContextError( |
| 104 | + vm.runInNewContext(`import("${targetURL}")`, undefined, { |
| 105 | + filename: __filename, |
| 106 | + }) |
| 107 | + ); |
| 108 | + |
| 109 | + // Rejects because it's running in the wrong context |
| 110 | + expectInvalidContextError(vm.runInNewContext(`import("fs")`, undefined, { |
| 111 | + filename: __filename, |
| 112 | + })); |
| 113 | +})(); |
0 commit comments