@@ -5,7 +5,13 @@ namespace ts {
5
5
}
6
6
7
7
declare let TextDecoder : undefined | ( new ( ) => { decode ( buffer : ArrayBuffer | ArrayBufferView ) : string } ) ;
8
- const decoder = new ( typeof TextDecoder !== "undefined" ? TextDecoder : require ( "util" ) . TextDecoder ) ( "ascii" ) ;
8
+ const decoder = typeof Buffer === "undefined" ?
9
+ new ( typeof TextDecoder !== "undefined" ? TextDecoder : require ( "util" ) . TextDecoder ) ( "ascii" ) :
10
+ undefined ;
11
+ const decode : ( buffer : ArrayBuffer ) => string =
12
+ decoder
13
+ ? buffer => decoder . decode ( buffer )
14
+ : buffer => ( buffer as Buffer ) . toString ( "ascii" ) ;
9
15
let mappingsBuffer : Uint8Array ;
10
16
11
17
export function createSourceMapGenerator ( host : EmitHost , file : string , guessedInputLength : number , sourceRoot : string , sourcesDirectoryPath : string , generatorOptions : SourceMapGeneratorOptions ) : SourceMapGenerator {
@@ -21,7 +27,7 @@ namespace ts {
21
27
22
28
const names : string [ ] = [ ] ;
23
29
let nameToNameIndexMap : ESMap < string , number > | undefined ;
24
- mappingsBuffer ||= new Uint8Array ( guessedInputLength + 1 >> 1 ) ;
30
+ mappingsBuffer ||= typeof Buffer === undefined ? new Uint8Array ( guessedInputLength + 1 >> 1 ) : Buffer . alloc ( guessedInputLength + 1 >> 1 ) ;
25
31
let lastMappings : string | undefined ;
26
32
let mappingsPos = 0 ;
27
33
function setMapping ( charCode : number ) {
@@ -306,7 +312,7 @@ namespace ts {
306
312
307
313
function toJSON ( ) : RawSourceMap {
308
314
commitPendingMapping ( ) ;
309
- const mappings = ( lastMappings ??= decoder . decode ( mappingsBuffer . subarray ( 0 , mappingsPos ) ) ) ;
315
+ const mappings = ( lastMappings ??= decode ( mappingsBuffer . subarray ( 0 , mappingsPos ) ) ) ;
310
316
return {
311
317
version : 3 ,
312
318
file,
0 commit comments