1
- import { deepStrictEqual , notStrictEqual , rejects , strictEqual , throws } from 'node:assert' ;
1
+ import { deepStrictEqual , equal , notStrictEqual , rejects , strictEqual , throws } from 'node:assert' ;
2
2
import { Readable , Writable } from 'node:stream' ;
3
3
import { setImmediate as setImmediatePromise } from 'node:timers/promises' ;
4
4
import WebSocket from 'isomorphic-ws' ;
@@ -335,19 +335,30 @@ describe('V5 protocol support', () => {
335
335
protocol : 'v5.channel.k8s.io' ,
336
336
} as WebSocket . WebSocket ;
337
337
let uriOut = '' ;
338
- let endCalled = false ;
338
+ let stderrEndCalled = false ;
339
+ let stdoutEndCalled = false ;
340
+ let stdinPauseCalled = false ;
339
341
const handler = new WebSocketHandler (
340
342
kc ,
341
343
( uri : string , protocols : string [ ] , opts : WebSocket . ClientOptions ) : WebSocket . WebSocket => {
342
344
uriOut = uri ;
343
345
return mockWs as WebSocket . WebSocket ;
344
346
} ,
345
347
{
346
- stdin : process . stdin ,
347
- stderr : process . stderr ,
348
+ stdin : {
349
+ pause : ( ) => {
350
+ stdinPauseCalled = true ;
351
+ return { } as Readable ;
352
+ } ,
353
+ } as Readable ,
354
+ stderr : {
355
+ end : ( ) => {
356
+ stderrEndCalled = true ;
357
+ } ,
358
+ } as Writable ,
348
359
stdout : {
349
360
end : ( ) => {
350
- endCalled = true ;
361
+ stdoutEndCalled = true ;
351
362
} ,
352
363
} as Writable ,
353
364
} ,
@@ -364,17 +375,30 @@ describe('V5 protocol support', () => {
364
375
type : 'open' ,
365
376
} ;
366
377
mockWs . onopen ! ( event ) ;
367
- const closeBuff = Buffer . alloc ( 2 ) ;
368
- closeBuff . writeUint8 ( 255 , 0 ) ;
369
- closeBuff . writeUint8 ( WebSocketHandler . StdoutStream , 1 ) ;
370
-
378
+ // Close stdin/stdout with Buffers
379
+ [ WebSocketHandler . StdinStream , WebSocketHandler . StdoutStream ] . forEach ( ( stream ) => {
380
+ const closeBuff = Buffer . alloc ( 2 ) ;
381
+ closeBuff . writeUint8 ( 255 , 0 ) ;
382
+ closeBuff . writeUint8 ( stream , 1 ) ;
383
+
384
+ mockWs . onmessage ! ( {
385
+ data : closeBuff ,
386
+ type : 'type' ,
387
+ target : mockWs ,
388
+ } ) ;
389
+ } ) ;
390
+ // Close stderr with a string \xff is 'close' \x02 is the stderr stream number
391
+ // so that both paths are tested.
392
+ const closeMsg = '\xFF\x02' ;
371
393
mockWs . onmessage ! ( {
372
- data : closeBuff ,
394
+ data : closeMsg ,
373
395
type : 'type' ,
374
396
target : mockWs ,
375
397
} ) ;
376
398
await promise ;
377
- strictEqual ( endCalled , true ) ;
399
+ strictEqual ( stdoutEndCalled , true ) ;
400
+ strictEqual ( stderrEndCalled , true ) ;
401
+ strictEqual ( stdinPauseCalled , true ) ;
378
402
} ) ;
379
403
it ( 'should handle closing stdin < v4 protocol' , ( ) => {
380
404
const ws = {
@@ -436,4 +460,73 @@ describe('Restartable Handle Standard Input', () => {
436
460
strictEqual ( count , retryTimes ) ;
437
461
} ) ;
438
462
} ) ;
463
+
464
+ it ( 'should work correctly' , async ( ) => {
465
+ let sent : Buffer | null = null ;
466
+ const ws = {
467
+ protocol : 'v5.channel.k8s.io' ,
468
+ send : ( data ) => {
469
+ sent = data ;
470
+ } ,
471
+ readyState : WebSocket . OPEN ,
472
+ close : ( ) => {
473
+ throw new Error ( 'should not be called' ) ;
474
+ } ,
475
+ } as unknown as WebSocket ;
476
+ const p = new Promise < WebSocket > ( ( resolve , reject ) => resolve ( ws ) ) ;
477
+ let dataCb : any ;
478
+ let endCb : any ;
479
+ let flushCb : any ;
480
+
481
+ const r = {
482
+ on : ( verb , cb ) => {
483
+ if ( verb === 'data' ) {
484
+ dataCb = cb ;
485
+ }
486
+ if ( verb === 'end' ) {
487
+ endCb = cb ;
488
+ }
489
+ if ( verb == 'flush' ) {
490
+ flushCb = cb ;
491
+ }
492
+ } ,
493
+ } as Readable ;
494
+
495
+ WebSocketHandler . restartableHandleStandardInput ( ( ) => p , r , 0 , 4 , true ) ;
496
+
497
+ dataCb ( 'some test data' ) ;
498
+ endCb ( ) ;
499
+ await flushCb ( ) ;
500
+
501
+ equal ( sent ! . toString ( ) , '\x00some test data' ) ;
502
+ } ) ;
503
+
504
+ it ( 'should work if the web socket exists' , ( ) => {
505
+ let sent : Buffer | null = null ;
506
+ const ws = {
507
+ protocol : 'v5.channel.k8s.io' ,
508
+ send : ( data ) => {
509
+ sent = data ;
510
+ } ,
511
+ readyState : WebSocket . OPEN ,
512
+ close : ( ) => {
513
+ throw new Error ( 'should not be called' ) ;
514
+ } ,
515
+ } as unknown as WebSocket ;
516
+ let count = 0 ;
517
+ WebSocketHandler . processData (
518
+ 'some test data' ,
519
+ ws ,
520
+ ( ) : Promise < WebSocket . WebSocket > => {
521
+ return new Promise < WebSocket . WebSocket > ( ( resolve ) => {
522
+ count ++ ;
523
+ resolve ( ws as WebSocket . WebSocket ) ;
524
+ } ) ;
525
+ } ,
526
+ 0 ,
527
+ 5 ,
528
+ ) ;
529
+ equal ( sent ! . toString ( ) , '\x00some test data' ) ;
530
+ strictEqual ( count , 0 ) ;
531
+ } ) ;
439
532
} ) ;
0 commit comments