From 33dc5d262288f4efae3077978f37f22338568b35 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 11 Aug 2023 17:27:41 +0530 Subject: fix(node): implement TLSSocket._start (#20120) Closes https://github.com/denoland/deno/issues/19983 Closes https://github.com/denoland/deno/issues/18303 Closes https://github.com/denoland/deno/issues/16681 Closes https://github.com/denoland/deno/issues/19978 --- ext/node/polyfills/internal_binding/stream_wrap.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'ext/node/polyfills/internal_binding') diff --git a/ext/node/polyfills/internal_binding/stream_wrap.ts b/ext/node/polyfills/internal_binding/stream_wrap.ts index 528dd7c3f..66ebbe682 100644 --- a/ext/node/polyfills/internal_binding/stream_wrap.ts +++ b/ext/node/polyfills/internal_binding/stream_wrap.ts @@ -314,9 +314,16 @@ export class LibuvStreamWrap extends HandleWrap { let buf = BUF; let nread: number | null; + const ridBefore = this[kStreamBaseField]!.rid; try { nread = await this[kStreamBaseField]!.read(buf); } catch (e) { + // Try to read again if the underlying stream resource + // changed. This can happen during TLS upgrades (eg. STARTTLS) + if (ridBefore != this[kStreamBaseField]!.rid) { + return this.#read(); + } + if ( e instanceof Deno.errors.Interrupted || e instanceof Deno.errors.BadResource @@ -365,15 +372,23 @@ export class LibuvStreamWrap extends HandleWrap { async #write(req: WriteWrap, data: Uint8Array) { const { byteLength } = data; + const ridBefore = this[kStreamBaseField]!.rid; + + let nwritten = 0; try { // TODO(crowlKats): duplicate from runtime/js/13_buffer.js - let nwritten = 0; while (nwritten < data.length) { nwritten += await this[kStreamBaseField]!.write( data.subarray(nwritten), ); } } catch (e) { + // Try to read again if the underlying stream resource + // changed. This can happen during TLS upgrades (eg. STARTTLS) + if (ridBefore != this[kStreamBaseField]!.rid) { + return this.#write(req, data.subarray(nwritten)); + } + let status: number; // TODO(cmorten): map err to status codes -- cgit v1.2.3