Skip to content

Commit

Permalink
events
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Jan 15, 2023
1 parent c7c4c7c commit 5fcefcd
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/mods/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ export interface DataState extends DataStateData {

export interface TlsParams {
ciphers: Cipher[]
signal?: AbortSignal
signal?: AbortSignal,
debug?: boolean
}

export class TlsStream extends EventTarget {
Expand Down Expand Up @@ -254,6 +255,14 @@ export class TlsStream extends EventTarget {
try { this.output.error(error) } catch (e: unknown) { }
}

private async onReadStart(controller: TransformStreamDefaultController<Uint8Array>) {
this._input = controller
}

private async onWriteStart(controller: TransformStreamDefaultController<Uint8Array>) {
this._output = controller
}

async handshake() {
if (this.state.type !== "none")
throw new Error(`Invalid state`)
Expand All @@ -273,22 +282,18 @@ export class TlsStream extends EventTarget {
const finished = new Future<Event>()

try {
this.addEventListener("error", finished.err, { passive: true })
this.addEventListener("close", finished.err, { passive: true })
this.addEventListener("finished", finished.ok, { passive: true })

await finished.promise
} finally {
this.removeEventListener("error", finished.err)
this.removeEventListener("close", finished.err)
this.removeEventListener("finished", finished.ok)
}
}

private async onReadStart(controller: TransformStreamDefaultController<Uint8Array>) {
this._input = controller
}

private async onWriteStart(controller: TransformStreamDefaultController<Uint8Array>) {
this._output = controller
}

private async onRead(chunk: Uint8Array) {
this.wbinary.write(chunk)
this.rbinary.view = this.buffer.subarray(0, this.wbinary.offset)
Expand Down

0 comments on commit 5fcefcd

Please sign in to comment.