From 50a79584cb12129b3db1ef3e0eb9d0c8b9f20b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 30 May 2019 14:59:30 +0200 Subject: chore: Implement strict mode (denoland/deno_std#453) Original: https://github.com/denoland/deno_std/commit/be24677d15494e83eea2e99bfc5ccfdde31cb892 --- ws/mod.ts | 16 ++++++++++++---- ws/sha1.ts | 7 ++++--- ws/test.ts | 37 ++++++++++++++++++++++++------------- 3 files changed, 40 insertions(+), 20 deletions(-) (limited to 'ws') diff --git a/ws/mod.ts b/ws/mod.ts index 7d8200dfc..2ae6cabda 100644 --- a/ws/mod.ts +++ b/ws/mod.ts @@ -31,19 +31,25 @@ export interface WebSocketCloseEvent { reason?: string; } -export function isWebSocketCloseEvent(a): a is WebSocketCloseEvent { - return a && typeof a["code"] === "number"; +export function isWebSocketCloseEvent( + a: WebSocketEvent +): a is WebSocketCloseEvent { + return typeof a === "object" && a.hasOwnProperty("code"); } export type WebSocketPingEvent = ["ping", Uint8Array]; -export function isWebSocketPingEvent(a): a is WebSocketPingEvent { +export function isWebSocketPingEvent( + a: WebSocketEvent +): a is WebSocketPingEvent { return Array.isArray(a) && a[0] === "ping" && a[1] instanceof Uint8Array; } export type WebSocketPongEvent = ["pong", Uint8Array]; -export function isWebSocketPongEvent(a): a is WebSocketPongEvent { +export function isWebSocketPongEvent( + a: WebSocketEvent +): a is WebSocketPongEvent { return Array.isArray(a) && a[0] === "pong" && a[1] instanceof Uint8Array; } @@ -436,6 +442,8 @@ async function handshake( if (!m) { throw new Error("ws: invalid status line: " + statusLine); } + + // @ts-ignore const { version, statusCode } = m.groups; if (version !== "HTTP/1.1" || statusCode !== "101") { throw new Error( diff --git a/ws/sha1.ts b/ws/sha1.ts index d01df21ad..dc8ba680c 100644 --- a/ws/sha1.ts +++ b/ws/sha1.ts @@ -77,11 +77,11 @@ export class Sha1 { if (notString) { for (i = start; index < length && i < 64; ++index) { - blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; + blocks[i >> 2] |= (message[index] as number) << SHIFT[i++ & 3]; } } else { for (i = start; index < length && i < 64; ++index) { - code = message.charCodeAt(index); + code = (message as string).charCodeAt(index); if (code < 0x80) { blocks[i >> 2] |= code << SHIFT[i++ & 3]; } else if (code < 0x800) { @@ -94,7 +94,8 @@ export class Sha1 { } else { code = 0x10000 + - (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); + (((code & 0x3ff) << 10) | + ((message as string).charCodeAt(++index) & 0x3ff)); blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; diff --git a/ws/test.ts b/ws/test.ts index bac82453d..e14af1d55 100644 --- a/ws/test.ts +++ b/ws/test.ts @@ -166,20 +166,31 @@ test(function wsAcceptable(): void { ); }); -const invalidHeaders = [ - { "sec-websocket-key": "aaa" }, - { upgrade: "websocket" }, - { upgrade: "invalid", "sec-websocket-key": "aaa" }, - { upgrade: "websocket", "sec-websocket-ky": "" } -]; - test(function wsAcceptableInvalid(): void { - for (const pat of invalidHeaders) { - const ret = acceptable({ - headers: new Headers(pat) - }); - assertEquals(ret, false); - } + assertEquals( + acceptable({ + headers: new Headers({ "sec-websocket-key": "aaa" }) + }), + false + ); + assertEquals( + acceptable({ + headers: new Headers({ upgrade: "websocket" }) + }), + false + ); + assertEquals( + acceptable({ + headers: new Headers({ upgrade: "invalid", "sec-websocket-key": "aaa" }) + }), + false + ); + assertEquals( + acceptable({ + headers: new Headers({ upgrade: "websocket", "sec-websocket-ky": "" }) + }), + false + ); }); test(async function wsWriteReadMaskedFrame(): Promise { -- cgit v1.2.3