diff options
| author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2019-05-30 14:59:30 +0200 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2019-05-30 08:59:30 -0400 |
| commit | 50a79584cb12129b3db1ef3e0eb9d0c8b9f20b62 (patch) | |
| tree | ee9a90a8b8018c03b1e1a6ace07abdaa494ea90d /ws | |
| parent | 80b3c486f6222f65b52eb2eca903b67312e8ce0c (diff) | |
chore: Implement strict mode (denoland/deno_std#453)
Original: https://github.com/denoland/deno_std/commit/be24677d15494e83eea2e99bfc5ccfdde31cb892
Diffstat (limited to 'ws')
| -rw-r--r-- | ws/mod.ts | 16 | ||||
| -rw-r--r-- | ws/sha1.ts | 7 | ||||
| -rw-r--r-- | ws/test.ts | 37 |
3 files changed, 40 insertions, 20 deletions
@@ -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<void> { |
