| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2a3cdd3 commit acba5dc
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,15 @@ const noRestrictedSyntax = [ | |||
| 13 | 13 | selector: "CallExpression[callee.object.name='assert']:not([callee.property.name='ok']):not([callee.property.name='fail']):not([callee.property.name='ifError'])", | |
| 14 | 14 | message: 'Only use simple assertions', | |
| 15 | 15 | }, | |
| 16 | + { | ||
| 17 | + // Forbids usages of `btoa` that are not caught by no-restricted-globals, like: | ||
| 18 | + // ``` | ||
| 19 | + // const { btoa } = internalBinding('buffer'); | ||
| 20 | + // btoa('...'); | ||
| 21 | + // ``` | ||
| 22 | + selector: "CallExpression[callee.property.name='btoa'], CallExpression[callee.name='btoa']", | ||
| 23 | + message: "`btoa` supports only latin-1 charset, use Buffer.from(str).toString('base64') instead", | ||
| 24 | + }, | ||
| 16 | 25 | { | |
| 17 | 26 | selector: 'NewExpression[callee.name=/Error$/]:not([callee.name=/^(AssertionError|NghttpError|AbortError|NodeAggregateError)$/])', | |
| 18 | 27 | message: "Use an error exported by 'internal/errors' instead.", | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,6 +17,7 @@ const { | |||
| 17 | 17 | } = require('internal/errors').codes; | |
| 18 | 18 | const { getOptionValue } = require('internal/options'); | |
| 19 | 19 | const assert = require('internal/assert'); | |
| 20 | + const { Buffer } = require('buffer'); | ||
| 20 | 21 | ||
| 21 | 22 | /** | |
| 22 | 23 | * The TypeScript parsing mode, either 'strip-only' or 'transform'. | |
@@ -134,9 +135,10 @@ function stripTypeScriptModuleTypes(source, filename) { | |||
| 134 | 135 | * @returns {string} The code with the source map attached. | |
| 135 | 136 | */ | |
| 136 | 137 | 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'); | ||
| 140 | 142 | return `${code}\n\n//# sourceMappingURL=data:application/json;base64,${base64SourceMap}`; | |
| 141 | 143 | } | |
| 142 | 144 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,12 @@ common.expectWarning( | |||
| 12 | 12 | 'stripTypeScriptTypes is an experimental feature and might change at any time', | |
| 13 | 13 | ); | |
| 14 | 14 | ||
| 15 | + const sourceToBeTransformed = ` | ||
| 16 | + namespace MathUtil { | ||
| 17 | + export const add = (a: number, b: number) => a + b; | ||
| 18 | + }`; | ||
| 19 | + const sourceToBeTransformedMapping = 'UACY;aACK,MAAM,CAAC,GAAW,IAAc,IAAI;AACnD,GAFU,aAAA'; | ||
| 20 | + | ||
| 15 | 21 | test('stripTypeScriptTypes', () => { | |
| 16 | 22 | const source = 'const x: number = 1;'; | |
| 17 | 23 | const result = stripTypeScriptTypes(source); | |
@@ -48,45 +54,52 @@ test('stripTypeScriptTypes sourceUrl throws when mode is strip', () => { | |||
| 48 | 54 | }); | |
| 49 | 55 | ||
| 50 | 56 | test('stripTypeScriptTypes source map when mode is transform', () => { | |
| 51 | - const source = ` | ||
| 52 | - namespace MathUtil { | ||
| 53 | - export const add = (a: number, b: number) => a + b; | ||
| 54 | - }`; | ||
| 55 | - const result = stripTypeScriptTypes(source, { mode: 'transform', sourceMap: true }); | ||
| 57 | + const result = stripTypeScriptTypes(sourceToBeTransformed, { mode: 'transform', sourceMap: true }); | ||
| 56 | 58 | const script = new vm.Script(result); | |
| 57 | 59 | const sourceMap = | |
| 58 | 60 | { | |
| 59 | 61 | version: 3, | |
| 60 | - sources: [ | ||
| 61 | - '<anon>', | ||
| 62 | - ], | ||
| 63 | - sourcesContent: [ | ||
| 64 | - '\n namespace MathUtil {\n export const add = (a: number, b: number) => a + b;\n }', | ||
| 65 | - ], | ||
| 62 | + sources: [''], | ||
| 66 | 63 | names: [], | |
| 67 | - mappings: ';UACY;aACK,MAAM,CAAC,GAAW,IAAc,IAAI;AACnD,GAFU,aAAA' | ||
| 64 | + mappings: sourceToBeTransformedMapping, | ||
| 68 | 65 | }; | |
| 69 | - assert(script.sourceMapURL, `sourceMappingURL=data:application/json;base64,${JSON.stringify(sourceMap)}`); | ||
| 66 | + const inlinedSourceMap = Buffer.from(JSON.stringify(sourceMap)).toString('base64'); | ||
| 67 | + assert.strictEqual(script.sourceMapURL, `data:application/json;base64,${inlinedSourceMap}`); | ||
| 70 | 68 | }); | |
| 71 | 69 | ||
| 72 | 70 | test('stripTypeScriptTypes source map when mode is transform and sourceUrl', () => { | |
| 73 | - const source = ` | ||
| 74 | - namespace MathUtil { | ||
| 75 | - export const add = (a: number, b: number) => a + b; | ||
| 76 | - }`; | ||
| 77 | - const result = stripTypeScriptTypes(source, { mode: 'transform', sourceMap: true, sourceUrl: 'test.ts' }); | ||
| 71 | + const result = stripTypeScriptTypes(sourceToBeTransformed, { | ||
| 72 | + mode: 'transform', | ||
| 73 | + sourceMap: true, | ||
| 74 | + sourceUrl: 'test.ts' | ||
| 75 | + }); | ||
| 76 | + const script = new vm.Script(result); | ||
| 77 | + const sourceMap = | ||
| 78 | + { | ||
| 79 | + version: 3, | ||
| 80 | + sources: ['test.ts'], | ||
| 81 | + names: [], | ||
| 82 | + mappings: sourceToBeTransformedMapping, | ||
| 83 | + }; | ||
| 84 | + const inlinedSourceMap = Buffer.from(JSON.stringify(sourceMap)).toString('base64'); | ||
| 85 | + assert.strictEqual(script.sourceMapURL, `data:application/json;base64,${inlinedSourceMap}`); | ||
| 86 | + }); | ||
| 87 | + | ||
| 88 | + test('stripTypeScriptTypes source map when mode is transform and sourceUrl with non-latin-1 chars', () => { | ||
| 89 | + const sourceUrl = 'dir%20with $unusual"chars?\'åß∂ƒ©∆¬…`.cts'; | ||
| 90 | + const result = stripTypeScriptTypes(sourceToBeTransformed, { | ||
| 91 | + mode: 'transform', | ||
| 92 | + sourceMap: true, | ||
| 93 | + sourceUrl, | ||
| 94 | + }); | ||
| 78 | 95 | const script = new vm.Script(result); | |
| 79 | 96 | const sourceMap = | |
| 80 | 97 | { | |
| 81 | 98 | version: 3, | |
| 82 | - sources: [ | ||
| 83 | - 'test.ts', | ||
| 84 | - ], | ||
| 85 | - sourcesContent: [ | ||
| 86 | - '\n namespace MathUtil {\n export const add = (a: number, b: number) => a + b;\n }', | ||
| 87 | - ], | ||
| 99 | + sources: [sourceUrl], | ||
| 88 | 100 | names: [], | |
| 89 | - mappings: ';UACY;aACK,MAAM,CAAC,GAAW,IAAc,IAAI;AACnD,GAFU,aAAA' | ||
| 101 | + mappings: sourceToBeTransformedMapping, | ||
| 90 | 102 | }; | |
| 91 | - assert(script.sourceMapURL, `sourceMappingURL=data:application/json;base64,${JSON.stringify(sourceMap)}`); | ||
| 103 | + const inlinedSourceMap = Buffer.from(JSON.stringify(sourceMap)).toString('base64'); | ||
| 104 | + assert.strictEqual(script.sourceMapURL, `data:application/json;base64,${inlinedSourceMap}`); | ||
| 92 | 105 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments