| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8a3482e commit 67ecb10
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -161,14 +161,14 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE | |||
| 161 | 161 | default: { // The user did not pass `--experimental-default-type`. | |
| 162 | 162 | // `source` is undefined when this is called from `defaultResolve`; | |
| 163 | 163 | // but this gets called again from `defaultLoad`/`defaultLoadSync`. | |
| 164 | - let parsedSource; | ||
| 165 | - if (source) { | ||
| 166 | - const { stripTypeScriptTypes } = require('internal/modules/helpers'); | ||
| 167 | - parsedSource = stripTypeScriptTypes(source, url); | ||
| 168 | - } | ||
| 164 | + // Since experimental-strip-types depends on detect-module, we always return null | ||
| 165 | + // if source is undefined. | ||
| 166 | + if (!source) { return null; } | ||
| 167 | + const { stripTypeScriptTypes, stringify } = require('internal/modules/helpers'); | ||
| 168 | + const stringifiedSource = stringify(source); | ||
| 169 | + const parsedSource = stripTypeScriptTypes(stringifiedSource, fileURLToPath(url)); | ||
| 169 | 170 | const detectedFormat = detectModuleFormat(parsedSource, url); | |
| 170 | - // When source is undefined, default to module-typescript. | ||
| 171 | - const format = detectedFormat ? `${detectedFormat}-typescript` : 'module-typescript'; | ||
| 171 | + const format = `${detectedFormat}-typescript`; | ||
| 172 | 172 | if (format === 'module-typescript' && foundPackageJson) { | |
| 173 | 173 | // This module has a .js extension, a package.json with no `type` field, and ESM syntax. | |
| 174 | 174 | // Warn about the missing `type` field so that the user can avoid the performance penalty of detection. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,16 +18,6 @@ const { | |||
| 18 | 18 | globalThis: { WebAssembly }, | |
| 19 | 19 | } = primordials; | |
| 20 | 20 | ||
| 21 | - /** @type {import('internal/util/types')} */ | ||
| 22 | - let _TYPES = null; | ||
| 23 | - /** | ||
| 24 | - * Lazily loads and returns the internal/util/types module. | ||
| 25 | - */ | ||
| 26 | - function lazyTypes() { | ||
| 27 | - if (_TYPES !== null) { return _TYPES; } | ||
| 28 | - return _TYPES = require('internal/util/types'); | ||
| 29 | - } | ||
| 30 | - | ||
| 31 | 21 | const { | |
| 32 | 22 | compileFunctionForCJSLoader, | |
| 33 | 23 | } = internalBinding('contextify'); | |
@@ -37,7 +27,9 @@ const assert = require('internal/assert'); | |||
| 37 | 27 | const { readFileSync } = require('fs'); | |
| 38 | 28 | const { dirname, extname, isAbsolute } = require('path'); | |
| 39 | 29 | const { | |
| 30 | + assertBufferSource, | ||
| 40 | 31 | loadBuiltinModule, | |
| 32 | + stringify, | ||
| 41 | 33 | stripTypeScriptTypes, | |
| 42 | 34 | stripBOM, | |
| 43 | 35 | urlToFilename, | |
@@ -57,7 +49,6 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => { | |||
| 57 | 49 | const { emitExperimentalWarning, kEmptyObject, setOwnProperty, isWindows } = require('internal/util'); | |
| 58 | 50 | const { | |
| 59 | 51 | ERR_UNKNOWN_BUILTIN_MODULE, | |
| 60 | - ERR_INVALID_RETURN_PROPERTY_VALUE, | ||
| 61 | 52 | } = require('internal/errors').codes; | |
| 62 | 53 | const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache'); | |
| 63 | 54 | const moduleWrap = internalBinding('module_wrap'); | |
@@ -107,44 +98,6 @@ function initCJSParseSync() { | |||
| 107 | 98 | const translators = new SafeMap(); | |
| 108 | 99 | exports.translators = translators; | |
| 109 | 100 | ||
| 110 | - let DECODER = null; | ||
| 111 | - /** | ||
| 112 | - * Asserts that the given body is a buffer source (either a string, array buffer, or typed array). | ||
| 113 | - * Throws an error if the body is not a buffer source. | ||
| 114 | - * @param {string | ArrayBufferView | ArrayBuffer} body - The body to check. | ||
| 115 | - * @param {boolean} allowString - Whether or not to allow a string as a valid buffer source. | ||
| 116 | - * @param {string} hookName - The name of the hook being called. | ||
| 117 | - * @throws {ERR_INVALID_RETURN_PROPERTY_VALUE} If the body is not a buffer source. | ||
| 118 | - */ | ||
| 119 | - function assertBufferSource(body, allowString, hookName) { | ||
| 120 | - if (allowString && typeof body === 'string') { | ||
| 121 | - return; | ||
| 122 | - } | ||
| 123 | - const { isArrayBufferView, isAnyArrayBuffer } = lazyTypes(); | ||
| 124 | - if (isArrayBufferView(body) || isAnyArrayBuffer(body)) { | ||
| 125 | - return; | ||
| 126 | - } | ||
| 127 | - throw new ERR_INVALID_RETURN_PROPERTY_VALUE( | ||
| 128 | - `${allowString ? 'string, ' : ''}array buffer, or typed array`, | ||
| 129 | - hookName, | ||
| 130 | - 'source', | ||
| 131 | - body, | ||
| 132 | - ); | ||
| 133 | - } | ||
| 134 | - | ||
| 135 | - /** | ||
| 136 | - * Converts a buffer or buffer-like object to a string. | ||
| 137 | - * @param {string | ArrayBuffer | ArrayBufferView} body - The buffer or buffer-like object to convert to a string. | ||
| 138 | - * @returns {string} The resulting string. | ||
| 139 | - */ | ||
| 140 | - function stringify(body) { | ||
| 141 | - if (typeof body === 'string') { return body; } | ||
| 142 | - assertBufferSource(body, false, 'load'); | ||
| 143 | - const { TextDecoder } = require('internal/encoding'); | ||
| 144 | - DECODER = DECODER === null ? new TextDecoder() : DECODER; | ||
| 145 | - return DECODER.decode(body); | ||
| 146 | - } | ||
| 147 | - | ||
| 148 | 101 | /** | |
| 149 | 102 | * Converts a URL to a file path if the URL protocol is 'file:'. | |
| 150 | 103 | * @param {string} url - The URL to convert. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,7 @@ const { | |||
| 16 | 16 | } = primordials; | |
| 17 | 17 | const { | |
| 18 | 18 | ERR_INVALID_ARG_TYPE, | |
| 19 | + ERR_INVALID_RETURN_PROPERTY_VALUE, | ||
| 19 | 20 | } = require('internal/errors').codes; | |
| 20 | 21 | const { BuiltinModule } = require('internal/bootstrap/realm'); | |
| 21 | 22 | ||
@@ -429,10 +430,60 @@ function getCompileCacheDir() { | |||
| 429 | 430 | return _getCompileCacheDir() || undefined; | |
| 430 | 431 | } | |
| 431 | 432 | ||
| 433 | + /** @type {import('internal/util/types')} */ | ||
| 434 | + let _TYPES = null; | ||
| 435 | + /** | ||
| 436 | + * Lazily loads and returns the internal/util/types module. | ||
| 437 | + */ | ||
| 438 | + function lazyTypes() { | ||
| 439 | + if (_TYPES !== null) { return _TYPES; } | ||
| 440 | + return _TYPES = require('internal/util/types'); | ||
| 441 | + } | ||
| 442 | + | ||
| 443 | + /** | ||
| 444 | + * Asserts that the given body is a buffer source (either a string, array buffer, or typed array). | ||
| 445 | + * Throws an error if the body is not a buffer source. | ||
| 446 | + * @param {string | ArrayBufferView | ArrayBuffer} body - The body to check. | ||
| 447 | + * @param {boolean} allowString - Whether or not to allow a string as a valid buffer source. | ||
| 448 | + * @param {string} hookName - The name of the hook being called. | ||
| 449 | + * @throws {ERR_INVALID_RETURN_PROPERTY_VALUE} If the body is not a buffer source. | ||
| 450 | + */ | ||
| 451 | + function assertBufferSource(body, allowString, hookName) { | ||
| 452 | + if (allowString && typeof body === 'string') { | ||
| 453 | + return; | ||
| 454 | + } | ||
| 455 | + const { isArrayBufferView, isAnyArrayBuffer } = lazyTypes(); | ||
| 456 | + if (isArrayBufferView(body) || isAnyArrayBuffer(body)) { | ||
| 457 | + return; | ||
| 458 | + } | ||
| 459 | + throw new ERR_INVALID_RETURN_PROPERTY_VALUE( | ||
| 460 | + `${allowString ? 'string, ' : ''}array buffer, or typed array`, | ||
| 461 | + hookName, | ||
| 462 | + 'source', | ||
| 463 | + body, | ||
| 464 | + ); | ||
| 465 | + } | ||
| 466 | + | ||
| 467 | + let DECODER = null; | ||
| 468 | + | ||
| 469 | + /** | ||
| 470 | + * Converts a buffer or buffer-like object to a string. | ||
| 471 | + * @param {string | ArrayBuffer | ArrayBufferView} body - The buffer or buffer-like object to convert to a string. | ||
| 472 | + * @returns {string} The resulting string. | ||
| 473 | + */ | ||
| 474 | + function stringify(body) { | ||
| 475 | + if (typeof body === 'string') { return body; } | ||
| 476 | + assertBufferSource(body, false, 'load'); | ||
| 477 | + const { TextDecoder } = require('internal/encoding'); | ||
| 478 | + DECODER = DECODER === null ? new TextDecoder() : DECODER; | ||
| 479 | + return DECODER.decode(body); | ||
| 480 | + } | ||
| 481 | + | ||
| 432 | 482 | module.exports = { | |
| 433 | 483 | addBuiltinLibsToObject, | |
| 434 | 484 | constants, | |
| 435 | 485 | enableCompileCache, | |
| 486 | + assertBufferSource, | ||
| 436 | 487 | getBuiltinModule, | |
| 437 | 488 | getCjsConditions, | |
| 438 | 489 | getCompileCacheDir, | |
@@ -442,6 +493,7 @@ module.exports = { | |||
| 442 | 493 | makeRequireFunction, | |
| 443 | 494 | normalizeReferrerURL, | |
| 444 | 495 | stripTypeScriptTypes, | |
| 496 | + stringify, | ||
| 445 | 497 | stripBOM, | |
| 446 | 498 | toRealPath, | |
| 447 | 499 | hasStartedUserCJSExecution() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -325,3 +325,14 @@ test('execute a TypeScript file with CommonJS syntax requiring .mts with require | |||
| 325 | 325 | match(result.stdout, /Hello, TypeScript!/); | |
| 326 | 326 | strictEqual(result.code, 0); | |
| 327 | 327 | }); | |
| 328 | + | ||
| 329 | + test('execute a JavaScript file importing a cjs TypeScript file', async () => { | ||
| 330 | + const result = await spawnPromisified(process.execPath, [ | ||
| 331 | + '--experimental-strip-types', | ||
| 332 | + '--no-warnings', | ||
| 333 | + fixtures.path('typescript/ts/issue-54457.mjs'), | ||
| 334 | + ]); | ||
| 335 | + strictEqual(result.stderr, ''); | ||
| 336 | + match(result.stdout, /Hello, TypeScript!/); | ||
| 337 | + strictEqual(result.code, 0); | ||
| 338 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,4 @@ | |||
| 1 | + // https://github.com/nodejs/node/issues/54457 | ||
| 2 | + const { text } = await import('./test-require.ts'); | ||
| 3 | + | ||
| 4 | + console.log(text); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,7 @@ | |||
| 1 | + const util = require('node:util'); | ||
| 2 | + | ||
| 3 | + const text = 'Hello, TypeScript!'; | ||
| 4 | + | ||
| 5 | + module.exports = { | ||
| 6 | + text | ||
| 7 | + }; | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments