-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
module: support Wasm without file extension within module
scope
#49540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
887ee9b
fcb8be9
bfaedc4
500ea04
d78a9da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,12 @@ | |
|
||
const { | ||
RegExpPrototypeExec, | ||
Uint8Array, | ||
} = primordials; | ||
const { getOptionValue } = require('internal/options'); | ||
|
||
const { closeSync, openSync, readSync } = require('fs'); | ||
|
||
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules'); | ||
|
||
const extensionFormatMap = { | ||
|
@@ -35,7 +38,27 @@ function mimeToFormat(mime) { | |
return null; | ||
} | ||
|
||
function guessExtensionlessModule(url) { | ||
if (!experimentalWasmModules) | ||
return 'module'; | ||
|
||
const magic = new Uint8Array(4); | ||
let fd; | ||
try { | ||
fd = openSync(url); | ||
readSync(fd, magic); | ||
if (magic[0] === 0x00 && magic[1] === 0x61 && magic[2] === 0x73 && magic[3] === 0x6d) { | ||
return 'wasm'; | ||
} | ||
Comment on lines
+48
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get that we’re just reading the first four bytes here (I think?) but doesn’t doing this here mean that we open and read the file twice? Once to detect format and again later to actually run it? Especially since this happens for every ESM entry point, even JavaScript ones, this feels like a performance regression that we should avoid. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This happens only for extensionless files, right? If that's the case, that seems absolutely OK as a tradeoff, the workaround is to use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's correct, the check is only for local extensionless files in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need a tradeoff? Can’t we keep the file contents in memory once we have them, so that later on when the |
||
} finally { | ||
if (fd) closeSync(fd); | ||
} | ||
|
||
return 'module'; | ||
} | ||
|
||
module.exports = { | ||
extensionFormatMap, | ||
guessExtensionlessModule, | ||
mimeToFormat, | ||
}; |
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,13 @@ | ||||||||
import { strictEqual } from 'assert'; | ||||||||
|
||||||||
export function jsFn () { | ||||||||
state = 'WASM JS Function Executed'; | ||||||||
return 42; | ||||||||
} | ||||||||
|
||||||||
export let state = 'JS Function Executed'; | ||||||||
|
||||||||
export function jsInitFn () { | ||||||||
strictEqual(state, 'JS Function Executed'); | ||||||||
state = 'WASM Start Executed'; | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as common from '../common/index.mjs'; | ||
import * as fixtures from '../common/fixtures.mjs'; | ||
import { spawn } from 'node:child_process'; | ||
import assert from 'node:assert'; | ||
|
||
const entry = fixtures.path('/es-modules/package-type-module/noext-esm'); | ||
|
||
// Run a module that does not have extension. | ||
// This is to ensure that "type": "module" applies to extensionless files. | ||
|
||
const child = spawn(process.execPath, [entry]); | ||
|
||
let stdout = ''; | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => { | ||
stdout += data; | ||
}); | ||
child.on('close', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 0); | ||
assert.strictEqual(signal, null); | ||
assert.strictEqual(stdout, 'executed\n'); | ||
})); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import * as common from '../common/index.mjs'; | ||
import * as fixtures from '../common/fixtures.mjs'; | ||
import { spawn } from 'node:child_process'; | ||
import assert from 'node:assert'; | ||
|
||
{ | ||
const entry = fixtures.path( | ||
'/es-modules/package-type-module/extension.unknown' | ||
); | ||
const child = spawn(process.execPath, [entry]); | ||
let stdout = ''; | ||
let stderr = ''; | ||
child.stderr.setEncoding('utf8'); | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => { | ||
stdout += data; | ||
}); | ||
child.stderr.on('data', (data) => { | ||
stderr += data; | ||
}); | ||
child.on('close', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 1); | ||
assert.strictEqual(signal, null); | ||
assert.strictEqual(stdout, ''); | ||
assert.ok(stderr.indexOf('ERR_UNKNOWN_FILE_EXTENSION') !== -1); | ||
})); | ||
} | ||
{ | ||
const entry = fixtures.path( | ||
'/es-modules/package-type-module/imports-unknownext.mjs' | ||
); | ||
const child = spawn(process.execPath, [entry]); | ||
let stdout = ''; | ||
let stderr = ''; | ||
child.stderr.setEncoding('utf8'); | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => { | ||
stdout += data; | ||
}); | ||
child.stderr.on('data', (data) => { | ||
stderr += data; | ||
}); | ||
child.on('close', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 1); | ||
assert.strictEqual(signal, null); | ||
assert.strictEqual(stdout, ''); | ||
assert.ok(stderr.indexOf('ERR_UNKNOWN_FILE_EXTENSION') !== -1); | ||
})); | ||
} | ||
{ | ||
const entry = fixtures.path('/es-modules/package-type-module/noext-esm'); | ||
const child = spawn(process.execPath, [entry]); | ||
let stdout = ''; | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => { | ||
stdout += data; | ||
}); | ||
child.on('close', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 0); | ||
assert.strictEqual(signal, null); | ||
assert.strictEqual(stdout, 'executed\n'); | ||
})); | ||
} | ||
{ | ||
const entry = fixtures.path( | ||
'/es-modules/package-type-module/imports-noext.mjs' | ||
); | ||
const child = spawn(process.execPath, [entry]); | ||
let stdout = ''; | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => { | ||
stdout += data; | ||
}); | ||
child.on('close', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 0); | ||
assert.strictEqual(signal, null); | ||
assert.strictEqual(stdout, 'executed\n'); | ||
})); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Flags: --experimental-wasm-modules | ||
import { mustCall } from '../common/index.mjs'; | ||
import { path } from '../common/fixtures.mjs'; | ||
import { strictEqual } from 'assert'; | ||
import { spawn } from 'child_process'; | ||
|
||
{ | ||
const entry = path('/es-modules/package-type-module/noext-wasm'); | ||
|
||
// Run a module that does not have extension. | ||
// This is to ensure that "type": "module" applies to extensionless files. | ||
|
||
const child = spawn(process.execPath, ['--experimental-wasm-modules', entry]); | ||
|
||
let stdout = ''; | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => { | ||
stdout += data; | ||
}); | ||
child.on('close', mustCall((code, signal) => { | ||
strictEqual(code, 0); | ||
strictEqual(signal, null); | ||
strictEqual(stdout, ''); | ||
})); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.