module: use buffer.toString base64 · nodejs/node@717cfa4 · GitHub
Skip to content

Commit 717cfa4

Browse files
legendecasaduh95
authored andcommitted
module: use buffer.toString base64
`btoa` only supports latin-1 charset and produces invalid source mapping urls. PR-URL: #56315 Refs: #56296 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent ab3e646 commit 717cfa4

3 files changed

Lines changed: 53 additions & 29 deletions

File tree

lib/eslint.config_partial.mjs

Lines changed: 9 additions & 0 deletions

lib/internal/modules/typescript.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
} = require('internal/errors').codes;
1818
const { getOptionValue } = require('internal/options');
1919
const assert = require('internal/assert');
20+
const { Buffer } = require('buffer');
2021

2122
/**
2223
* The TypeScript parsing mode, either 'strip-only' or 'transform'.
@@ -134,9 +135,10 @@ function stripTypeScriptModuleTypes(source, filename) {
134135
* @returns {string} The code with the source map attached.
135136
*/
136137
function addSourceMap(code, sourceMap) {
137-
// TODO(@marco-ippolito) When Buffer.transcode supports utf8 to
138-
// base64 transformation, we should change this line.
139-
const base64SourceMap = internalBinding('buffer').btoa(sourceMap);
138+
// The base64 encoding should be https://datatracker.ietf.org/doc/html/rfc4648#section-4,
139+
// not base64url https://datatracker.ietf.org/doc/html/rfc4648#section-5. See data url
140+
// spec https://tools.ietf.org/html/rfc2397#section-2.
141+
const base64SourceMap = Buffer.from(sourceMap).toString('base64');
140142
return `${code}\n\n//# sourceMappingURL=data:application/json;base64,${base64SourceMap}`;
141143
}
142144

test/parallel/test-module-strip-types.js

Lines changed: 39 additions & 26 deletions

0 commit comments

Comments
 (0)