Skip to content

fix: accept amaro 1.1 and 1.2 stripping of non-ASCII types in --typescript - #70

Open
exo-nikita wants to merge 4 commits into
mainfrom
fix/typescript-loader-amaro
Open

exo-nikita wants to merge 4 commits into
mainfrom
fix/typescript-loader-amaro

Conversation

@exo-nikita

@exo-nikita exo-nikita commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Port of PreventiveMeasures/libraries#472.

With --typescript (Node.js 22.13+ without native type stripping, and the electron-as-node engines), .ts files load through loader/typescript.loader.js, which double-checks Node's stripTypeScriptTypes: the same UTF-8 length as the source, and only spaces or semicolons where it differs. That is what swc used to write, a space for each erased byte, but no Node.js that has stripTypeScriptTypes writes that:

  • amaro 0.2.0 (Node.js 22.13.0) to 1.1.11 (Node.js 26.13.1) write a blank of as many UTF-8 bytes for each erased UTF-16 unit: a space, a no-break space, an en space; a space and a byte order mark for four. So any type with non-ASCII in it, like 'Grüße' | '你好', failed with result mismatch.
  • amaro 1.2.0 (September 2026) and on write a space for each unit, which keeps the length in units but not in bytes. None of the Node.js releases below bundles it yet; once one does, the same files fail with length mismatch.

Changes

  • loader/typescript.loader.js: compare in UTF-16 units, whose length both forms keep, and accept where the source differs the blanks either writes, or a semicolon as before. A stripping that writes other blanks, like tabs, is still rejected. The source bytes must be valid UTF-8 (isUtf8): the byte comparison rejected invalid UTF-8 as a side effect, since decoding replaces it, but a comparison of the decoded strings would not. A leading byte order mark is dropped before stripping, as Node does when it reads a module, and as stripping does: a .ts file starting with one failed on main with length mismatch. Both as in PreventiveMeasures/triage's strip-types-loader.js.
  • tests/typescript.test.ts: a type with 2-, 3- and 4-byte characters. On Node.js 22.13.0, which CI's test:typescript runs through the loader, it fails without the change (result mismatch).

Evidence

  • Bundled amaro, stripping let a: "é一😀" = 1: 22.13.0 (0.2.0), 22.22.0 (1.1.4), 22.23.5 (1.1.8), 24.0.0 (0.5.2), 24.22.3 (1.1.11), 25.9.0 (1.1.8) and 26.13.1 (1.1.11) all write blanks of the bytes. amaro 1.2.0 and 1.3.0 from npm write spaces.
  • load() with the stripping swapped for Node 24.18's, amaro 1.1.11, 1.2.0 and 1.3.0, and Node's with each unit it changed made a space (amaro 1.2's form), over the test files and non-ASCII samples (literal types, JSDoc, type-only imports, generics): before, every non-ASCII file fails (result mismatch with 1.1, length mismatch with 1.2 on); after, all pass. With each changed unit made a tab instead, all are still rejected.
  • Invalid UTF-8 (a byte in a type or in code, an overlong sequence, an encoded lone surrogate) is rejected with invalid utf-8 with each stripping above; main rejected it too, except with amaro 1.2 on, which let one through in a type.
  • A .ts test file starting with a byte order mark, which failed on main with length mismatch, passes on Node.js 22.13.0 through the loader, with the same stack positions as Node.js 24.18.0's own stripping (line 1, column 23, the mark not counted).
  • test:typescript passes on Node.js 22.13.0 (through the loader), 22.22.0, 24.0.0, 25.9.0, 26.13.1 and 24.18.0. CI=true node --run test:all and node --run lint pass on 24.18.0, and quickjs:bundle passes the new test.

The package version isn't bumped.

…cript

The --typescript loader checked Node's stripping byte by byte: the same
UTF-8 length, and only spaces or semicolons where the source differs, as
swc used to write a space for each erased byte. Node's stripping, amaro
0.2.0 (Node.js 22.13.0) to 1.1.11 (26.13.1), writes a blank of as many
UTF-8 bytes for each erased UTF-16 unit instead, so a type with non-ASCII
in it failed with 'result mismatch'. amaro 1.2 writes a space for each
unit, so with it the same types fail with 'length mismatch'.

Compare in UTF-16 units, which both keep, accepting where the source
differs the blanks either writes, or a semicolon as before. A stripping
that writes other blanks is still rejected.

Port of PreventiveMeasures/libraries#472.
Comparing the source to its stripping in bytes rejected invalid UTF-8 as
a side effect: decoding replaces it, so the re-encoded output did not
match the file. Comparing the decoded strings in units does not, and a
file with invalid UTF-8 loaded with replacement characters. Check the
source bytes with isUtf8 first.
Node drops a leading byte order mark when it reads a module, and so does
stripTypeScriptTypes, so a .ts file starting with one failed the length
check with 'length mismatch'. Drop it before stripping and comparing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant