Skip to content

Commit

Permalink
module: throw ERR_NO_TYPESCRIPT when compiled without amaro
Browse files Browse the repository at this point in the history
PR-URL: #55332
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Jacob Smith <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
marco-ippolito authored and ruyadorno committed Nov 27, 2024
1 parent 907484f commit c81c818
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
12 changes: 12 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2343,6 +2343,17 @@ OpenSSL crypto support.
An attempt was made to use features that require [ICU][], but Node.js was not
compiled with ICU support.

<a id="ERR_NO_TYPESCRIPT"></a>

### `ERR_NO_TYPESCRIPT`

<!-- YAML
added: REPLACEME
-->

An attempt was made to use features that require [Native TypeScript support][], but Node.js was not
compiled with TypeScript support.

<a id="ERR_OPERATION_FAILED"></a>

### `ERR_OPERATION_FAILED`
Expand Down Expand Up @@ -4112,6 +4123,7 @@ An error occurred trying to allocate memory. This should never happen.
[ICU]: intl.md#internationalization-support
[JSON Web Key Elliptic Curve Registry]: https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
[JSON Web Key Types Registry]: https://www.iana.org/assignments/jose/jose.xhtml#web-key-types
[Native TypeScript support]: typescript.md#type-stripping
[Node.js error codes]: #nodejs-error-codes
[Permission Model]: permissions.md#permission-model
[RFC 7230 Section 3]: https://tools.ietf.org/html/rfc7230#section-3
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,8 @@ E('ERR_NO_CRYPTO',
'Node.js is not compiled with OpenSSL crypto support', Error);
E('ERR_NO_ICU',
'%s is not supported on Node.js compiled without ICU', TypeError);
E('ERR_NO_TYPESCRIPT',
'Node.js is not compiled with TypeScript support', Error);
E('ERR_OPERATION_FAILED', 'Operation failed: %s', Error, TypeError);
E('ERR_OUT_OF_RANGE',
(str, range, input, replaceDefaultBoolean = false) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/modules/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const assert = require('internal/assert');

const { Buffer } = require('buffer');
const { getOptionValue } = require('internal/options');
const { setOwnProperty, getLazy } = require('internal/util');
const { assertTypeScript, setOwnProperty, getLazy } = require('internal/util');
const { inspect } = require('internal/util/inspect');

const lazyTmpdir = getLazy(() => require('os').tmpdir());
Expand Down Expand Up @@ -328,6 +328,7 @@ const getTypeScriptParsingMode = getLazy(() =>
* @returns {Function} The TypeScript parser function.
*/
const loadTypeScriptParser = getLazy(() => {
assertTypeScript();
const amaro = require('internal/deps/amaro/dist/index');
return amaro.transformSync;
});
Expand Down
17 changes: 14 additions & 3 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const {
const {
codes: {
ERR_NO_CRYPTO,
ERR_NO_TYPESCRIPT,
ERR_UNKNOWN_SIGNAL,
},
isErrorStackTraceLimitWritable,
Expand All @@ -65,6 +66,7 @@ const { getOptionValue } = require('internal/options');
const { encodings } = internalBinding('string_decoder');

const noCrypto = !process.versions.openssl;
const noTypeScript = !process.versions.amaro;

const isWindows = process.platform === 'win32';
const isMacOS = process.platform === 'darwin';
Expand Down Expand Up @@ -196,9 +198,17 @@ function assertCrypto() {
throw new ERR_NO_CRYPTO();
}

// Return undefined if there is no match.
// Move the "slow cases" to a separate function to make sure this function gets
// inlined properly. That prioritizes the common case.
function assertTypeScript() {
if (noTypeScript)
throw new ERR_NO_TYPESCRIPT();
}

/**
* Move the "slow cases" to a separate function to make sure this function gets
* inlined properly. That prioritizes the common case.
* @param {unknown} enc
* @returns {string | undefined} Returns undefined if there is no match.
*/
function normalizeEncoding(enc) {
if (enc == null || enc === 'utf8' || enc === 'utf-8') return 'utf8';
return slowCases(enc);
Expand Down Expand Up @@ -862,6 +872,7 @@ for (let i = 0; i < encodings.length; ++i)
module.exports = {
getLazy,
assertCrypto,
assertTypeScript,
cachedResult,
convertToValidSignal,
createClassWrapper,
Expand Down

0 comments on commit c81c818

Please sign in to comment.