Skip to content

Commit 2da51d1

Browse files
committed
loader: remove bytes implementation
1 parent a392f74 commit 2da51d1

10 files changed

+22
-101
lines changed

lib/internal/modules/esm/assert.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ const formatTypeMap = {
3333
'wasm': kImplicitTypeAttribute, // It's unclear whether the HTML spec will require an type attribute or not for Wasm; see https://github.com/WebAssembly/esm-integration/issues/42
3434
};
3535

36-
const experimentalFormatTypeMap = {
37-
'__proto__': null,
38-
'bytes': 'bytes',
39-
'text': 'text',
40-
};
36+
function getFormatType(format) {
37+
if (format === 'text' && getOptionValue('--experimental-import-text')) {
38+
return 'text';
39+
}
40+
41+
return formatTypeMap[format];
42+
}
4143

4244
/**
4345
* The HTML spec disallows the default type to be explicitly specified
@@ -49,18 +51,6 @@ const supportedTypeAttributes = ArrayPrototypeFilter(
4951
ObjectValues(formatTypeMap),
5052
(type) => type !== kImplicitTypeAttribute);
5153

52-
function getExperimentalFormatType(format) {
53-
const type = experimentalFormatTypeMap[format];
54-
if (type === undefined) {
55-
return undefined;
56-
}
57-
if (!getOptionValue('--experimental-raw-imports')) {
58-
return undefined;
59-
}
60-
return type;
61-
}
62-
63-
6454
/**
6555
* Test a module's import attributes.
6656
* @param {string} url The URL of the imported module, for error reporting.
@@ -78,7 +68,7 @@ function validateAttributes(url, format,
7868
throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED(keys[i], importAttributes[keys[i]], url);
7969
}
8070
}
81-
const validType = formatTypeMap[format] ?? getExperimentalFormatType(format);
71+
const validType = getFormatType(format);
8272

8373
switch (validType) {
8474
case undefined:
@@ -120,8 +110,7 @@ function handleInvalidType(url, type) {
120110

121111
// `type` might not have been one of the types we understand.
122112
if (!ArrayPrototypeIncludes(supportedTypeAttributes, type) &&
123-
!(ObjectPrototypeHasOwnProperty(experimentalFormatTypeMap, type) &&
124-
getOptionValue('--experimental-raw-imports'))) {
113+
!(type === 'text' && getOptionValue('--experimental-import-text'))) {
125114
throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED('type', type, url);
126115
}
127116

lib/internal/modules/esm/get_format.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,21 @@ const protocolHandlers = {
8080

8181
function getFormatFromImportAttributes(importAttributes) {
8282
if (
83-
!getOptionValue('--experimental-raw-imports') ||
8483
!importAttributes ||
8584
!ObjectPrototypeHasOwnProperty(importAttributes, 'type') ||
8685
typeof importAttributes.type !== 'string'
8786
) {
8887
return undefined;
8988
}
9089

91-
switch (importAttributes.type) {
92-
case 'text':
93-
case 'bytes':
94-
return importAttributes.type;
95-
default:
96-
return undefined;
90+
if (
91+
getOptionValue('--experimental-import-text') &&
92+
importAttributes.type === 'text'
93+
) {
94+
return 'text';
9795
}
96+
97+
return undefined;
9898
}
9999

100100
/**

lib/internal/modules/esm/translators.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -652,13 +652,3 @@ translators.set('text', function textStrategy(url, translateContext) {
652652
this.setExport('default', source);
653653
});
654654
});
655-
656-
// Strategy for loading source as raw bytes.
657-
translators.set('bytes', function bytesStrategy(url, translateContext) {
658-
let { source } = translateContext;
659-
assertBufferSource(source, false, 'load');
660-
source = new Uint8Array(Buffer.from(source));
661-
return new ModuleWrap(url, undefined, ['default'], function() {
662-
this.setExport('default', source);
663-
});
664-
});

src/node_options.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
632632
AddAlias("--loader", "--experimental-loader");
633633
AddOption("--experimental-modules", "", NoOp{}, kAllowedInEnvvar);
634634
AddOption("--experimental-wasm-modules", "", NoOp{}, kAllowedInEnvvar);
635-
AddOption("--experimental-raw-imports",
636-
"experimental support for raw source imports with import "
635+
AddOption("--experimental-import-text",
636+
"experimental support for importing source as text with import "
637637
"attributes",
638-
&EnvironmentOptions::experimental_raw_imports,
638+
&EnvironmentOptions::experimental_import_text,
639639
kAllowedInEnvvar);
640640
AddOption("--experimental-import-meta-resolve",
641641
"experimental ES Module import.meta.resolve() parentURL support",

src/node_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class EnvironmentOptions : public Options {
134134
std::string localstorage_file;
135135
bool experimental_global_navigator = true;
136136
bool experimental_global_web_crypto = true;
137-
bool experimental_raw_imports = false;
137+
bool experimental_import_text = false;
138138
bool experimental_import_meta_resolve = false;
139139
std::string input_type; // Value of --input-type
140140
bool entry_is_url = false;

test/es-module/test-esm-import-attributes-errors.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ async function test() {
3131
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
3232
);
3333

34-
await assert.rejects(
35-
import(jsModuleDataUrl, { with: { type: 'bytes' } }),
36-
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
37-
);
38-
3934
await assert.rejects(
4035
import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }),
4136
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }

test/es-module/test-esm-import-attributes-errors.mjs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ await assert.rejects(
2626
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
2727
);
2828

29-
await assert.rejects(
30-
import(jsModuleDataUrl, { with: { type: 'bytes' } }),
31-
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
32-
);
33-
3429
await assert.rejects(
3530
import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }),
3631
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }

test/es-module/test-esm-import-experimental-raw-imports-attributes-validation.js renamed to test/es-module/test-esm-import-attributes-validation-text.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Flags: --expose-internals --experimental-raw-imports
1+
// Flags: --expose-internals --experimental-import-text
22
'use strict';
33
require('../common');
44

@@ -8,7 +8,6 @@ const { validateAttributes } = require('internal/modules/esm/assert');
88

99
const url = 'test://';
1010

11-
// ... {type = 'text'}
1211
assert.ok(validateAttributes(url, 'text', { type: 'text' }));
1312

1413
assert.throws(() => validateAttributes(url, 'text', {}), {
@@ -18,14 +17,3 @@ assert.throws(() => validateAttributes(url, 'text', {}), {
1817
assert.throws(() => validateAttributes(url, 'module', { type: 'text' }), {
1918
code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE',
2019
});
21-
22-
// ... {type = 'bytes'}
23-
assert.ok(validateAttributes(url, 'bytes', { type: 'bytes' }));
24-
25-
assert.throws(() => validateAttributes(url, 'bytes', {}), {
26-
code: 'ERR_IMPORT_ATTRIBUTE_MISSING',
27-
});
28-
29-
assert.throws(() => validateAttributes(url, 'module', { type: 'bytes' }), {
30-
code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE',
31-
});

test/es-module/test-esm-import-experimental-raw-imports-disabled.mjs renamed to test/es-module/test-esm-import-text-disabled.mjs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,3 @@ await assert.rejects(
1010
import('data:text/plain,hello%20world', { with: { type: 'text' } }),
1111
{ code: 'ERR_UNKNOWN_MODULE_FORMAT' },
1212
);
13-
14-
await assert.rejects(
15-
import('../fixtures/file-to-read-without-bom.txt', { with: { type: 'bytes' } }),
16-
{ code: 'ERR_UNKNOWN_FILE_EXTENSION' },
17-
);
18-
19-
await assert.rejects(
20-
import('data:text/plain,hello%20world', { with: { type: 'bytes' } }),
21-
{ code: 'ERR_UNKNOWN_MODULE_FORMAT' },
22-
);

test/es-module/test-esm-import-experimental-raw-imports.mjs renamed to test/es-module/test-esm-import-text.mjs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,30 @@
1-
// Flags: --experimental-raw-imports
1+
// Flags: --experimental-import-text
22
import '../common/index.mjs';
33
import assert from 'assert';
4-
import { Buffer } from 'buffer';
54

65
import staticText from '../fixtures/file-to-read-without-bom.txt' with { type: 'text' };
76
import staticTextWithBOM from '../fixtures/file-to-read-with-bom.txt' with { type: 'text' };
8-
import staticBytes from '../fixtures/file-to-read-without-bom.txt' with { type: 'bytes' };
97

108
const expectedText = 'abc\ndef\nghi\n';
11-
const expectedBytes = Buffer.from(expectedText);
129

1310
assert.strictEqual(staticText, expectedText);
1411
assert.strictEqual(staticTextWithBOM, expectedText);
1512

16-
assert.ok(staticBytes instanceof Uint8Array);
17-
assert.deepStrictEqual(Buffer.from(staticBytes), expectedBytes);
18-
1913
const dynamicText = await import('../fixtures/file-to-read-without-bom.txt', {
2014
with: { type: 'text' },
2115
});
2216
assert.strictEqual(dynamicText.default, expectedText);
2317

24-
const dynamicBytes = await import('../fixtures/file-to-read-without-bom.txt', {
25-
with: { type: 'bytes' },
26-
});
27-
assert.deepStrictEqual(Buffer.from(dynamicBytes.default), expectedBytes);
28-
2918
const dataText = await import('data:text/plain,hello%20world', {
3019
with: { type: 'text' },
3120
});
3221
assert.strictEqual(dataText.default, 'hello world');
3322

34-
const dataBytes = await import('data:text/plain,hello%20world', {
35-
with: { type: 'bytes' },
36-
});
37-
assert.strictEqual(Buffer.from(dataBytes.default).toString(), 'hello world');
38-
3923
const dataJsAsText = await import('data:text/javascript,export{}', {
4024
with: { type: 'text' },
4125
});
4226
assert.strictEqual(dataJsAsText.default, 'export{}');
4327

44-
const dataJsAsBytes = await import('data:text/javascript,export{}', {
45-
with: { type: 'bytes' },
46-
});
47-
assert.strictEqual(Buffer.from(dataJsAsBytes.default).toString(), 'export{}');
48-
4928
const dataInvalidUtf8 = await import('data:text/plain,%66%6f%80%6f', {
5029
with: { type: 'text' },
5130
});
@@ -56,11 +35,6 @@ const jsAsText = await import('../fixtures/syntax/bad_syntax.js', {
5635
});
5736
assert.match(jsAsText.default, /^var foo bar;/);
5837

59-
const jsAsBytes = await import('../fixtures/syntax/bad_syntax.js', {
60-
with: { type: 'bytes' },
61-
});
62-
assert.match(Buffer.from(jsAsBytes.default).toString(), /^var foo bar;/);
63-
6438
const jsonAsText = await import('../fixtures/invalid.json', {
6539
with: { type: 'text' },
6640
});

0 commit comments

Comments
 (0)