1
- import { GitpodError } from '../../core/error ' ;
1
+ import { concatBytes , decodeUTF8 , encodeUTF8 } from '../utils/bytes ' ;
2
2
3
3
export type Bytes = string | ArrayBuffer | Uint8Array | null | undefined ;
4
4
@@ -13,16 +13,11 @@ export class LineDecoder {
13
13
static NEWLINE_CHARS = new Set ( [ '\n' , '\r' ] ) ;
14
14
static NEWLINE_REGEXP = / \r \n | [ \n \r ] / g;
15
15
16
- buffer : Uint8Array ;
16
+ # buffer: Uint8Array ;
17
17
#carriageReturnIndex: number | null ;
18
- textDecoder :
19
- | undefined
20
- | {
21
- decode ( buffer : Uint8Array | ArrayBuffer ) : string ;
22
- } ;
23
18
24
19
constructor ( ) {
25
- this . buffer = new Uint8Array ( ) ;
20
+ this . # buffer = new Uint8Array ( ) ;
26
21
this . #carriageReturnIndex = null ;
27
22
}
28
23
@@ -33,17 +28,14 @@ export class LineDecoder {
33
28
34
29
const binaryChunk =
35
30
chunk instanceof ArrayBuffer ? new Uint8Array ( chunk )
36
- : typeof chunk === 'string' ? new TextEncoder ( ) . encode ( chunk )
31
+ : typeof chunk === 'string' ? encodeUTF8 ( chunk )
37
32
: chunk ;
38
33
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 ] ) ;
43
35
44
36
const lines : string [ ] = [ ] ;
45
37
let patternIndex ;
46
- while ( ( patternIndex = findNewlineIndex ( this . buffer , this . #carriageReturnIndex) ) != null ) {
38
+ while ( ( patternIndex = findNewlineIndex ( this . # buffer, this . #carriageReturnIndex) ) != null ) {
47
39
if ( patternIndex . carriage && this . #carriageReturnIndex == null ) {
48
40
// skip until we either get a corresponding `\n`, a new `\r` or nothing
49
41
this . #carriageReturnIndex = patternIndex . index ;
@@ -55,64 +47,27 @@ export class LineDecoder {
55
47
this . #carriageReturnIndex != null &&
56
48
( patternIndex . index !== this . #carriageReturnIndex + 1 || patternIndex . carriage )
57
49
) {
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) ;
60
52
this . #carriageReturnIndex = null ;
61
53
continue ;
62
54
}
63
55
64
56
const endIndex =
65
57
this . #carriageReturnIndex !== null ? patternIndex . preceding - 1 : patternIndex . preceding ;
66
58
67
- const line = this . decodeText ( this . buffer . slice ( 0 , endIndex ) ) ;
59
+ const line = decodeUTF8 ( this . # buffer. subarray ( 0 , endIndex ) ) ;
68
60
lines . push ( line ) ;
69
61
70
- this . buffer = this . buffer . slice ( patternIndex . index ) ;
62
+ this . # buffer = this . # buffer. subarray ( patternIndex . index ) ;
71
63
this . #carriageReturnIndex = null ;
72
64
}
73
65
74
66
return lines ;
75
67
}
76
68
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
-
114
69
flush ( ) : string [ ] {
115
- if ( ! this . buffer . length ) {
70
+ if ( ! this . # buffer. length ) {
116
71
return [ ] ;
117
72
}
118
73
return this . decode ( '\n' ) ;
0 commit comments