diff options
Diffstat (limited to 'ext/node')
-rw-r--r-- | ext/node/polyfills/_tls_wrap.ts | 16 | ||||
-rw-r--r-- | ext/node/polyfills/internal_binding/stream_wrap.ts | 17 |
2 files changed, 30 insertions, 3 deletions
diff --git a/ext/node/polyfills/_tls_wrap.ts b/ext/node/polyfills/_tls_wrap.ts index 39df239d0..416bd4136 100644 --- a/ext/node/polyfills/_tls_wrap.ts +++ b/ext/node/polyfills/_tls_wrap.ts @@ -26,6 +26,11 @@ import { import { EventEmitter } from "node:events"; import { kEmptyObject } from "ext:deno_node/internal/util.mjs"; import { nextTick } from "ext:deno_node/_next_tick.ts"; +import { kHandle } from "ext:deno_node/internal/stream_base_commons.ts"; +import { + isAnyArrayBuffer, + isArrayBufferView, +} from "ext:deno_node/internal/util/types.ts"; const kConnectOptions = Symbol("connect-options"); const kIsVerified = Symbol("verified"); @@ -71,7 +76,11 @@ export class TLSSocket extends net.Socket { [kPendingSession]: any; [kConnectOptions]: any; ssl: any; - _start: any; + + _start() { + this[kHandle].afterConnect(); + } + constructor(socket: any, opts: any = kEmptyObject) { const tlsOptions = { ...opts }; @@ -84,6 +93,9 @@ export class TLSSocket extends net.Socket { let caCerts = tlsOptions?.secureContext?.ca; if (typeof caCerts === "string") caCerts = [caCerts]; + else if (isArrayBufferView(caCerts) || isAnyArrayBuffer(caCerts)) { + caCerts = [new TextDecoder().decode(caCerts)]; + } tlsOptions.caCerts = caCerts; super({ @@ -139,9 +151,9 @@ export class TLSSocket extends net.Socket { handle.afterConnect = async (req: any, status: number) => { try { const conn = await Deno.startTls(handle[kStreamBaseField], options); + handle[kStreamBaseField] = conn; tlssock.emit("secure"); tlssock.removeListener("end", onConnectEnd); - handle[kStreamBaseField] = conn; } catch { // TODO(kt3k): Handle this } 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<LibuvStreamWrap>, 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 |