@@ -51,18 +51,11 @@ export class WebSocketHandler implements WebSocketInterface {
51
51
52
52
public static handleStandardInput (
53
53
ws : WebSocket . WebSocket ,
54
- stdin : stream . Readable | any ,
54
+ stdin : stream . Readable ,
55
55
streamNum : number = 0 ,
56
56
) : boolean {
57
57
stdin . on ( 'data' , ( data ) => {
58
- const buff = Buffer . alloc ( data . length + 1 ) ;
59
- buff . writeInt8 ( streamNum , 0 ) ;
60
- if ( data instanceof Buffer ) {
61
- data . copy ( buff , 1 ) ;
62
- } else {
63
- buff . write ( data , 1 ) ;
64
- }
65
- ws . send ( buff ) ;
58
+ ws . send ( copyChunkForWebSocket ( streamNum , data , stdin . readableEncoding ) ) ;
66
59
} ) ;
67
60
68
61
stdin . on ( 'end' , ( ) => {
@@ -78,16 +71,9 @@ export class WebSocketHandler implements WebSocketInterface {
78
71
createWS : ( ) => Promise < WebSocket . WebSocket > ,
79
72
streamNum : number = 0 ,
80
73
retryCount : number = 3 ,
74
+ encoding ?: BufferEncoding | null ,
81
75
) : Promise < WebSocket . WebSocket | null > {
82
- const buff = Buffer . alloc ( data . length + 1 ) ;
83
-
84
- buff . writeInt8 ( streamNum , 0 ) ;
85
- if ( data instanceof Buffer ) {
86
- data . copy ( buff , 1 ) ;
87
- } else {
88
- buff . write ( data , 1 ) ;
89
- }
90
-
76
+ const buff = copyChunkForWebSocket ( streamNum , data , encoding ) ;
91
77
let i = 0 ;
92
78
for ( ; i < retryCount ; ++ i ) {
93
79
if ( ws !== null && ws . readyState === WebSocket . OPEN ) {
@@ -109,7 +95,7 @@ export class WebSocketHandler implements WebSocketInterface {
109
95
110
96
public static restartableHandleStandardInput (
111
97
createWS : ( ) => Promise < WebSocket . WebSocket > ,
112
- stdin : stream . Readable | any ,
98
+ stdin : stream . Readable ,
113
99
streamNum : number = 0 ,
114
100
retryCount : number = 3 ,
115
101
) : ( ) => WebSocket . WebSocket | null {
@@ -122,7 +108,14 @@ export class WebSocketHandler implements WebSocketInterface {
122
108
123
109
stdin . on ( 'data' , ( data ) => {
124
110
queue = queue . then ( async ( ) => {
125
- ws = await WebSocketHandler . processData ( data , ws , createWS , streamNum , retryCount ) ;
111
+ ws = await WebSocketHandler . processData (
112
+ data ,
113
+ ws ,
114
+ createWS ,
115
+ streamNum ,
116
+ retryCount ,
117
+ stdin . readableEncoding ,
118
+ ) ;
126
119
} ) ;
127
120
} ) ;
128
121
@@ -201,3 +194,24 @@ export class WebSocketHandler implements WebSocketInterface {
201
194
} ) ;
202
195
}
203
196
}
197
+
198
+ function copyChunkForWebSocket (
199
+ streamNum : number ,
200
+ chunk : string | Buffer ,
201
+ encoding ?: BufferEncoding | null ,
202
+ ) : Buffer {
203
+ let buff : Buffer ;
204
+
205
+ if ( chunk instanceof Buffer ) {
206
+ buff = Buffer . alloc ( chunk . length + 1 ) ;
207
+ chunk . copy ( buff , 1 ) ;
208
+ } else {
209
+ encoding ??= 'utf-8' ;
210
+ const size = Buffer . byteLength ( chunk , encoding ) ;
211
+ buff = Buffer . alloc ( size + 1 ) ;
212
+ buff . write ( chunk , 1 , size , encoding ) ;
213
+ }
214
+
215
+ buff . writeInt8 ( streamNum , 0 ) ;
216
+ return buff ;
217
+ }
0 commit comments