diff options
| author | Yusuke Sakurai <kerokerokerop@gmail.com> | 2019-01-07 04:26:18 +0900 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2019-01-06 14:26:18 -0500 |
| commit | 7907bfc4c91f5287237d87571d1933db4ae7a4fa (patch) | |
| tree | 6c14062ed9e08bb7543053b760dacd043acd874d /net/ioutil.ts | |
| parent | c164e696d7f924fe785421058d834934b7014429 (diff) | |
Add web socket module (denoland/deno_std#84)
Original: https://github.com/denoland/deno_std/commit/2606e295c77fb9d5796d527ed15f2dab3de1a696
Diffstat (limited to 'net/ioutil.ts')
| -rw-r--r-- | net/ioutil.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/net/ioutil.ts b/net/ioutil.ts new file mode 100644 index 000000000..68d6e5190 --- /dev/null +++ b/net/ioutil.ts @@ -0,0 +1,36 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import { BufReader } from "./bufio.ts"; + +/* Read big endian 16bit short from BufReader */ +export async function readShort(buf: BufReader): Promise<number> { + const [high, low] = [await buf.readByte(), await buf.readByte()]; + return (high << 8) | low; +} + +/* Read big endian 32bit integer from BufReader */ +export async function readInt(buf: BufReader): Promise<number> { + const [high, low] = [await readShort(buf), await readShort(buf)]; + return (high << 16) | low; +} + +const BIT32 = 0xffffffff; +/* Read big endian 64bit long from BufReader */ +export async function readLong(buf: BufReader): Promise<number> { + const [high, low] = [await readInt(buf), await readInt(buf)]; + // ECMAScript doesn't support 64bit bit ops. + return high ? high * (BIT32 + 1) + low : low; +} + +/* Slice number into 64bit big endian byte array */ +export function sliceLongToBytes(d: number, dest = new Array(8)): number[] { + let mask = 0xff; + let low = (d << 32) >>> 32; + let high = (d - low) / (BIT32 + 1); + let shift = 24; + for (let i = 0; i < 4; i++) { + dest[i] = (high >>> shift) & mask; + dest[i + 4] = (low >>> shift) & mask; + shift -= 8; + } + return dest; +} |
