1- import { GitpodError } from '../../core/error ' ;
1+ import { concatBytes , decodeUTF8 , encodeUTF8 } from '../utils/bytes ' ;
22
33export type Bytes = string | ArrayBuffer | Uint8Array | null | undefined ;
44
@@ -13,16 +13,11 @@ export class LineDecoder {
1313 static NEWLINE_CHARS = new Set ( [ '\n' , '\r' ] ) ;
1414 static NEWLINE_REGEXP = / \r \n | [ \n \r ] / g;
1515
16- buffer : Uint8Array ;
16+ # buffer: Uint8Array ;
1717 #carriageReturnIndex: number | null ;
18- textDecoder :
19- | undefined
20- | {
21- decode ( buffer : Uint8Array | ArrayBuffer ) : string ;
22- } ;
2318
2419 constructor ( ) {
25- this . buffer = new Uint8Array ( ) ;
20+ this . # buffer = new Uint8Array ( ) ;
2621 this . #carriageReturnIndex = null ;
2722 }
2823
@@ -33,17 +28,14 @@ export class LineDecoder {
3328
3429 const binaryChunk =
3530 chunk instanceof ArrayBuffer ? new Uint8Array ( chunk )
36- : typeof chunk === 'string' ? new TextEncoder ( ) . encode ( chunk )
31+ : typeof chunk === 'string' ? encodeUTF8 ( chunk )
3732 : chunk ;
3833
39- let newData = new Uint8Array ( this . buffer . length + binaryChunk . length ) ;
40- newData . set ( this . buffer ) ;
41- newData . set ( binaryChunk , this . buffer . length ) ;
42- this . buffer = newData ;
34+ this . #buffer = concatBytes ( [ this . #buffer, binaryChunk ] ) ;
4335
4436 const lines : string [ ] = [ ] ;
4537 let patternIndex ;
46- while ( ( patternIndex = findNewlineIndex ( this . buffer , this . #carriageReturnIndex) ) != null ) {
38+ while ( ( patternIndex = findNewlineIndex ( this . # buffer, this . #carriageReturnIndex) ) != null ) {
4739 if ( patternIndex . carriage && this . #carriageReturnIndex == null ) {
4840 // skip until we either get a corresponding `\n`, a new `\r` or nothing
4941 this . #carriageReturnIndex = patternIndex . index ;
@@ -55,64 +47,27 @@ export class LineDecoder {
5547 this . #carriageReturnIndex != null &&
5648 ( patternIndex . index !== this . #carriageReturnIndex + 1 || patternIndex . carriage )
5749 ) {
58- lines . push ( this . decodeText ( this . buffer . slice ( 0 , this . #carriageReturnIndex - 1 ) ) ) ;
59- this . buffer = this . buffer . slice ( this . #carriageReturnIndex) ;
50+ lines . push ( decodeUTF8 ( this . # buffer. subarray ( 0 , this . #carriageReturnIndex - 1 ) ) ) ;
51+ this . # buffer = this . # buffer. subarray ( this . #carriageReturnIndex) ;
6052 this . #carriageReturnIndex = null ;
6153 continue ;
6254 }
6355
6456 const endIndex =
6557 this . #carriageReturnIndex !== null ? patternIndex . preceding - 1 : patternIndex . preceding ;
6658
67- const line = this . decodeText ( this . buffer . slice ( 0 , endIndex ) ) ;
59+ const line = decodeUTF8 ( this . # buffer. subarray ( 0 , endIndex ) ) ;
6860 lines . push ( line ) ;
6961
70- this . buffer = this . buffer . slice ( patternIndex . index ) ;
62+ this . # buffer = this . # buffer. subarray ( patternIndex . index ) ;
7163 this . #carriageReturnIndex = null ;
7264 }
7365
7466 return lines ;
7567 }
7668
77- decodeText ( bytes : Bytes ) : string {
78- if ( bytes == null ) return '' ;
79- if ( typeof bytes === 'string' ) return bytes ;
80-
81- // Node:
82- if ( typeof ( globalThis as any ) . Buffer !== 'undefined' ) {
83- if ( bytes instanceof ( globalThis as any ) . Buffer ) {
84- return bytes . toString ( ) ;
85- }
86- if ( bytes instanceof Uint8Array ) {
87- return ( globalThis as any ) . Buffer . from ( bytes ) . toString ( ) ;
88- }
89-
90- throw new GitpodError (
91- `Unexpected: received non-Uint8Array (${ bytes . constructor . name } ) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.` ,
92- ) ;
93- }
94-
95- // Browser
96- if ( typeof ( globalThis as any ) . TextDecoder !== 'undefined' ) {
97- if ( bytes instanceof Uint8Array || bytes instanceof ArrayBuffer ) {
98- this . textDecoder ??= new ( globalThis as any ) . TextDecoder ( 'utf8' ) ;
99- return this . textDecoder ! . decode ( bytes ) ;
100- }
101-
102- throw new GitpodError (
103- `Unexpected: received non-Uint8Array/ArrayBuffer (${
104- ( bytes as any ) . constructor . name
105- } ) in a web platform. Please report this error.`,
106- ) ;
107- }
108-
109- throw new GitpodError (
110- `Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.` ,
111- ) ;
112- }
113-
11469 flush ( ) : string [ ] {
115- if ( ! this . buffer . length ) {
70+ if ( ! this . # buffer. length ) {
11671 return [ ] ;
11772 }
11873 return this . decode ( '\n' ) ;
0 commit comments