Skip to content

Commit

Permalink
events
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Jan 29, 2023
1 parent ec5eb7d commit 3d20925
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/libs/events/close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ export interface CloseEvent extends Event {
}

export class CloseEvent extends Event {
readonly code?: number;
readonly reason?: string;
readonly wasClean?: boolean;

constructor(type: string, eventInitDict: CloseEventInit) {
super(type, eventInitDict)

const { code, reason, wasClean } = eventInitDict
Object.assign(this, { code, reason, wasClean })
}
}
6 changes: 6 additions & 0 deletions src/libs/events/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ export interface ErrorEvent extends Event {
}

export class ErrorEvent extends Event {
readonly error?: any
readonly message?: string

constructor(type: string, eventInitDict: ErrorEventInit) {
super(type, eventInitDict)

const { error, message } = eventInitDict
Object.assign(this, { error, message })
}
}
26 changes: 26 additions & 0 deletions src/libs/events/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { CloseEvent } from "./close.js"
import { ErrorEvent } from "./error.js"

export namespace Events {

export function clone(event: Event) {
if (event instanceof MessageEvent) {
const { data, cancelable } = event
return new MessageEvent(event.type, { data, cancelable })
}

if (event instanceof CloseEvent) {
const { code, reason, wasClean, cancelable } = event
return new CloseEvent(event.type, { code, reason, wasClean, cancelable })
}

if (event instanceof ErrorEvent) {
const { error, message, cancelable } = event
return new ErrorEvent(event.type, { error, message, cancelable })
}

const { cancelable } = event
return new Event(event.type, { cancelable })
}

}
11 changes: 7 additions & 4 deletions src/mods/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BigMath } from "libs/bigmath/index.js"
import { Bytes } from "libs/bytes/bytes.js"
import { CloseEvent } from "libs/events/close.js"
import { ErrorEvent } from "libs/events/error.js"
import { Events } from "libs/events/events.js"
import { Future } from "libs/futures/future.js"
import { PRF } from "mods/algorithms/prf/prf.js"
import { List } from "mods/binary/lists/writable.js"
Expand Down Expand Up @@ -271,12 +272,14 @@ export class TlsStream extends EventTarget {
if (!this.write.dispatchEvent(event)) return
}

private async onError(error?: unknown) {
const event = new ErrorEvent("error", { error })
private async onError(e: Event) {
const event = Events.clone(e) as ErrorEvent
if (!this.dispatchEvent(event)) return

try { this.input.error(error) } catch (e: unknown) { }
try { this.output.error(error) } catch (e: unknown) { }
console.error(event.error)

try { this.input.error(event.error) } catch (e: unknown) { }
try { this.output.error(event.error) } catch (e: unknown) { }
}

private async onReadStart(controller: TransformStreamDefaultController<Uint8Array>) {
Expand Down
5 changes: 3 additions & 2 deletions test/website/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ export default function Home() {

// const headers = { "Content-Type": "application/json" }
// const body = JSON.stringify({ "jsonrpc": "2.0", "method": "web3_clientVersion", "params": [], "id": 67 })
// const res = await fetch("https://cloudflare-eth.com", { stream: tls, method: "POST", headers, body })

const res = await fetch("https://twitter.com/", { stream: tls })
console.log(res)
const res = await fetch("https://twitter.com", { stream: tls })

console.log(res)
const text = await res.text()
console.log(text)
}, [])
Expand Down

0 comments on commit 3d20925

Please sign in to comment.