Skip to content

Commit

Permalink
rollup, npm, errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Jan 18, 2023
1 parent bbc6707 commit 4db854a
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 688 deletions.
710 changes: 67 additions & 643 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 8 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,20 @@
"prepare": "npm run build"
},
"dependencies": {
"@hazae41/asn1": "^1.0.15",
"@hazae41/binary": "^1.1.2",
"@hazae41/x509": "^1.0.15",
"tslib": "^2.4.1"
"@hazae41/asn1": "^1.0.17",
"@hazae41/binary": "^1.1.7",
"@hazae41/x509": "^1.0.18"
},
"devDependencies": {
"@hazae41/phobos": "^1.0.7",
"@hazae41/phobos": "^1.0.10",
"@rollup/plugin-inject": "^5.0.3",
"@rollup/plugin-typescript": "^11.0.0",
"@types/node": "^18.11.18",
"rimraf": "^3.0.2",
"rollup": "^3.9.1",
"rimraf": "^4.1.1",
"rollup": "^3.10.0",
"rollup-plugin-dts": "^5.1.1",
"rollup-plugin-node-externals": "^5.0.3",
"ts-node": "^10.9.1",
"ttypescript": "^1.5.15",
"typescript": "^4.9.4",
"typescript-transform-paths": "^3.4.6"
"rollup-plugin-node-externals": "^5.1.0",
"typescript": "^4.9.4"
},
"exports": {
".": {
Expand Down
12 changes: 5 additions & 7 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import inject from '@rollup/plugin-inject';
import inject from "@rollup/plugin-inject";
import ts from "@rollup/plugin-typescript";
import dts from "rollup-plugin-dts";
import externals from "rollup-plugin-node-externals";
import typescript from "ttypescript";

export const config = [
{
Expand All @@ -22,7 +21,7 @@ export const config = [
sourcemap: true,
entryFileNames: "[name].cjs",
}],
plugins: [externals(), ts({ typescript })]
plugins: [externals(), ts()]
},
{
input: "./src/index.ts",
Expand All @@ -34,7 +33,7 @@ export const config = [
sourcemap: false,
entryFileNames: "[name].d.ts",
}],
plugins: [externals(), ts({ typescript }), dts()]
plugins: [externals(), ts(), dts()]
},
{
input: "./src/index.test.ts",
Expand All @@ -44,10 +43,9 @@ export const config = [
exports: "named",
preserveModules: true,
sourcemap: true,
entryFileNames: "[name].mjs"
entryFileNames: "[name].mjs",
}],
plugins: [externals(), ts({ typescript }), inject({ crypto: "node:crypto" })],
external: ["@hazae41/phobos"]
plugins: [externals({ devDeps: true }), ts(), inject({ crypto: "node:crypto" })],
},
]

Expand Down
11 changes: 11 additions & 0 deletions src/libs/events/close.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface CloseEvent extends Event {
readonly code?: number;
readonly reason?: string;
readonly wasClean?: boolean;
}

export class CloseEvent extends Event {
constructor(type: string, eventInitDict: CloseEventInit) {
super(type, eventInitDict)
}
}
9 changes: 9 additions & 0 deletions src/libs/events/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface ErrorEvent extends Event {
readonly error?: any
}

export class ErrorEvent extends Event {
constructor(type: string, eventInitDict: ErrorEventInit) {
super(type, eventInitDict)
}
}
3 changes: 2 additions & 1 deletion src/mods/binary/records/generic_ciphers/block/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class GenericBlockCipher {
// console.log("-> plaintext", plaintext.length, Bytes.toHex(plaintext))
// console.log("-> content", content.length, Bytes.toHex(content))
// console.log("-> mac", mac.length, Bytes.toHex(mac))
// console.log("-> ciphertext", ciphertext.length, ciphertext)

return new this(iv, ciphertext)
}
Expand All @@ -82,7 +83,7 @@ export class GenericBlockCipher {
const content = plaintext.subarray(0, -20)
const mac = plaintext.subarray(-20)

// console.log("<- content", raw.length, Bytes.toHex(raw))
// console.log("<- content", content.length, Bytes.toHex(content))
// console.log("<- mac", mac.length, Bytes.toHex(mac))

return new Opaque(content)
Expand Down
29 changes: 22 additions & 7 deletions src/mods/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Binary } from "@hazae41/binary"
import { Certificate, X509 } from "@hazae41/x509"
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 { Future } from "libs/futures/future.js"
import { PRF } from "mods/algorithms/prf/prf.js"
import { Number16, Number24 } from "mods/binary/number.js"
Expand Down Expand Up @@ -182,6 +184,9 @@ export class TlsStream extends EventTarget {
readonly readable: ReadableStream<Uint8Array>
readonly writable: WritableStream<Uint8Array>

readonly read = new EventTarget()
readonly write = new EventTarget()

private state: State = { type: "none", client_encrypted: false, server_encrypted: false }

private _input?: TransformStreamDefaultController<Uint8Array>
Expand Down Expand Up @@ -229,6 +234,11 @@ export class TlsStream extends EventTarget {
rtrashable
.pipeTo(trash, { signal })
.catch(this.onReadError.bind(this))

const onError = this.onError.bind(this)

this.read.addEventListener("error", onError, { passive: true })
this.write.addEventListener("error", onError, { passive: true })
}

get input() {
Expand All @@ -241,30 +251,35 @@ export class TlsStream extends EventTarget {

private async onReadClose() {
const event = new CloseEvent("close", {})
if (!this.dispatchEvent(event)) return
if (!this.read.dispatchEvent(event)) return
}

private async onWriteClose() {
const event = new CloseEvent("close", {})
if (!this.dispatchEvent(event)) return
if (!this.write.dispatchEvent(event)) return
}

private async onReadError(error?: unknown) {
const event = new ErrorEvent("error", { error })
if (!this.dispatchEvent(event)) return
if (!this.read.dispatchEvent(event)) return

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

private async onWriteError(error?: unknown) {
const event = new ErrorEvent("error", { error })
if (!this.dispatchEvent(event)) return
if (!this.write.dispatchEvent(event)) return

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

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

private async onReadStart(controller: TransformStreamDefaultController<Uint8Array>) {
this._input = controller
}
Expand Down Expand Up @@ -292,14 +307,14 @@ export class TlsStream extends EventTarget {
const finished = new Future<Event>()

try {
this.read.addEventListener("close", finished.err, { passive: true })
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.read.removeEventListener("close", finished.err)
this.removeEventListener("error", finished.err)
this.removeEventListener("close", finished.err)
this.removeEventListener("finished", finished.ok)
}
}
Expand Down Expand Up @@ -436,7 +451,7 @@ export class TlsStream extends EventTarget {
const handshake = Handshake.from(header, fragment)

if (handshake.subtype !== Handshake.types.hello_request)
this.state.messages.push(record.fragment.bytes)
this.state.messages.push(new Uint8Array(record.fragment.bytes))

if (handshake.subtype === ServerHello2.type)
return this.onServerHello(handshake, this.state)
Expand Down
2 changes: 1 addition & 1 deletion src/mods/transports/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class WebSocketSource implements UnderlyingSource<Uint8Array> {
async start(controller: ReadableStreamController<Uint8Array>) {
const onMessage = (e: MessageEvent) => {
const chunk = new Uint8Array(e.data satisfies ArrayBuffer)
controller.enqueue(chunk)
try { controller.enqueue(chunk) } catch (e: unknown) { }
}

const onError = (e: Event) => {
Expand Down
2 changes: 1 addition & 1 deletion test/website/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function Home() {
const onClick = useCallback(async () => {
const ws = await createWebSocketStream()

const ciphers = [Ciphers.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256]
const ciphers = [Ciphers.TLS_DHE_RSA_WITH_AES_256_CBC_SHA]

const tls = new TlsStream(ws, { ciphers })

Expand Down
2 changes: 1 addition & 1 deletion tools/tcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function onconn(conn: Deno.Conn) {
async function onsocket(socket: WebSocket) {
socket.binaryType = "arraybuffer"

const target = await Deno.connect({ hostname: "127.0.0.1", port: 44330, transport: "tcp" })
const target = await Deno.connect({ hostname: "127.0.0.1", port: 9001, transport: "tcp" })

socket.addEventListener("message", async e => {
try {
Expand Down
21 changes: 6 additions & 15 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"compilerOptions": {
"target": "ES2016",
"target": "ESNext",
"baseUrl": "./src",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"downlevelIteration": true,
"lib": [
"dom",
"DOM",
"ESNext"
],
"paths": {
Expand All @@ -17,20 +17,11 @@
"libs/*": [
"libs/*"
],
},
"plugins": [
{
"transform": "typescript-transform-paths"
},
{
"transform": "typescript-transform-paths",
"afterDeclarations": true
}
]
"tests/*": [
"tests/*"
],
}
},
"exclude": [
"node_modules"
],
"include": [
"./src/index.ts",
"./src/index.test.ts"
Expand Down

0 comments on commit 4db854a

Please sign in to comment.