@msgpack/msgpack
Advanced tools
Comparing version 1.2.3 to 1.3.0
This is the revision history of @msgpack/msgpack | ||
## v1.3.0 2019/05/29 | ||
https://github.com/msgpack/msgpack-javascript/compare/v1.2.3...v1.3.0 | ||
* Add `decodeArrayStream()` to decode an array and returns `AsyncIterable<unknown>` [#42](https://github.com/msgpack/msgpack-javascript/pull/42) | ||
* Add `decodeStream()` to decode an unlimited data stream [#46](https://github.com/msgpack/msgpack-javascript/pull/46) | ||
* Let `decodeAsync()` and `decodeArrayStream()` to take `ReadalbeStream<Uint8Array | ArrayLike<number>>` (whatwg-streams) [#43](https://github.com/msgpack/msgpack-javascript/pull/46) | ||
## v1.2.3 2019/05/29 | ||
@@ -4,0 +12,0 @@ |
@@ -1,6 +0,6 @@ | ||
import { ExtensionCodecType } from "./ExtensionCodec"; | ||
import { DecodeOptions } from "./decode"; | ||
import { ReadableStreamLike } from "./utils/stream"; | ||
export declare type DecodeAsyncOptions = DecodeOptions; | ||
export declare const defaultDecodeAsyncOptions: Partial<Readonly<{ | ||
extensionCodec: ExtensionCodecType; | ||
extensionCodec: import("./ExtensionCodec").ExtensionCodecType; | ||
maxStrLength: number; | ||
@@ -12,2 +12,4 @@ maxBinLength: number; | ||
}>>; | ||
export declare function decodeAsync(stream: AsyncIterable<Uint8Array | ArrayLike<number>>, options?: DecodeAsyncOptions): Promise<unknown>; | ||
export declare function decodeAsync(streamLike: ReadableStreamLike<Uint8Array | ArrayLike<number>>, options?: DecodeAsyncOptions): Promise<unknown>; | ||
export declare function decodeArrayStream(streamLike: ReadableStreamLike<Uint8Array | ArrayLike<number>>, options?: DecodeAsyncOptions): AsyncIterableIterator<unknown>; | ||
export declare function decodeStream(streamLike: ReadableStreamLike<Uint8Array | ArrayLike<number>>, options?: DecodeAsyncOptions): AsyncIterableIterator<unknown>; |
@@ -5,4 +5,6 @@ "use strict"; | ||
const decode_1 = require("./decode"); | ||
const stream_1 = require("./utils/stream"); | ||
exports.defaultDecodeAsyncOptions = decode_1.defaultDecodeOptions; | ||
async function decodeAsync(stream, options = decode_1.defaultDecodeOptions) { | ||
async function decodeAsync(streamLike, options = decode_1.defaultDecodeOptions) { | ||
const stream = stream_1.ensureAsyncIterabe(streamLike); | ||
const decoder = new Decoder_1.Decoder(options.extensionCodec, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength); | ||
@@ -12,2 +14,14 @@ return decoder.decodeOneAsync(stream); | ||
exports.decodeAsync = decodeAsync; | ||
function decodeArrayStream(streamLike, options = decode_1.defaultDecodeOptions) { | ||
const stream = stream_1.ensureAsyncIterabe(streamLike); | ||
const decoder = new Decoder_1.Decoder(options.extensionCodec, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength); | ||
return decoder.decodeArrayStream(stream); | ||
} | ||
exports.decodeArrayStream = decodeArrayStream; | ||
function decodeStream(streamLike, options = decode_1.defaultDecodeOptions) { | ||
const stream = stream_1.ensureAsyncIterabe(streamLike); | ||
const decoder = new Decoder_1.Decoder(options.extensionCodec, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength); | ||
return decoder.decodeStream(stream); | ||
} | ||
exports.decodeStream = decodeStream; | ||
//# sourceMappingURL=decodeAsync.js.map |
@@ -36,2 +36,3 @@ declare enum State { | ||
setBuffer(buffer: ArrayLike<number> | Uint8Array): void; | ||
appendBuffer(buffer: Uint8Array | ArrayLike<number>): void; | ||
hasRemaining(size?: number): boolean; | ||
@@ -41,5 +42,8 @@ createNoExtraBytesError(posToShow: number): RangeError; | ||
decodeOneAsync(stream: AsyncIterable<ArrayLike<number> | Uint8Array>): Promise<unknown>; | ||
decodeStream(stream: AsyncIterable<ArrayLike<number> | Uint8Array>): AsyncIterableIterator<unknown>; | ||
decodeArrayStream(stream: AsyncIterable<ArrayLike<number> | Uint8Array>): AsyncIterableIterator<unknown>; | ||
decodeSync(): unknown; | ||
readHeadByte(): number; | ||
complete(): void; | ||
readArraySize(): number; | ||
pushMapState(size: number): void; | ||
@@ -46,0 +50,0 @@ pushArrayState(size: number): void; |
@@ -53,2 +53,16 @@ "use strict"; | ||
} | ||
appendBuffer(buffer) { | ||
if (this.headByte === HEAD_BYTE_REQUIRED && !this.hasRemaining()) { | ||
this.setBuffer(buffer); | ||
} | ||
else { | ||
// retried because data is insufficient | ||
const remainingData = this.bytes.subarray(this.pos); | ||
const newData = typedArrays_1.ensureUint8Array(buffer); | ||
const concated = new Uint8Array(remainingData.length + newData.length); | ||
concated.set(remainingData); | ||
concated.set(newData, remainingData.length); | ||
this.setBuffer(concated); | ||
} | ||
} | ||
hasRemaining(size = 1) { | ||
@@ -75,15 +89,3 @@ return this.view.byteLength - this.pos >= size; | ||
} | ||
if (this.headByte === HEAD_BYTE_REQUIRED && !this.hasRemaining()) { | ||
this.setBuffer(buffer); | ||
} | ||
else { | ||
// retried because data is insufficient | ||
const remainingData = this.bytes.subarray(this.pos); | ||
const newData = typedArrays_1.ensureUint8Array(buffer); | ||
const concated = new Uint8Array(remainingData.length + newData.length); | ||
concated.set(remainingData); | ||
concated.set(newData, remainingData.length); | ||
this.setBuffer(concated); | ||
} | ||
//console.log("view", this.view, this.headByte); | ||
this.appendBuffer(buffer); | ||
try { | ||
@@ -110,2 +112,53 @@ object = this.decodeSync(); | ||
} | ||
async *decodeStream(stream) { | ||
for await (const buffer of stream) { | ||
this.appendBuffer(buffer); | ||
try { | ||
while (true) { | ||
const result = this.decodeSync(); | ||
yield result; | ||
} | ||
} | ||
catch (e) { | ||
if (!(e instanceof exports.DataViewIndexOutOfBoundsError)) { | ||
throw e; // rethrow | ||
} | ||
// fallthrough | ||
} | ||
} | ||
} | ||
async *decodeArrayStream(stream) { | ||
let headerParsed = false; | ||
let decoded = false; | ||
let itemsLeft = 0; | ||
for await (const buffer of stream) { | ||
if (decoded) { | ||
throw this.createNoExtraBytesError(this.totalPos); | ||
} | ||
this.appendBuffer(buffer); | ||
if (!headerParsed) { | ||
itemsLeft = this.readArraySize(); | ||
headerParsed = true; | ||
this.complete(); | ||
} | ||
try { | ||
while (true) { | ||
const result = this.decodeSync(); | ||
yield result; | ||
itemsLeft--; | ||
if (itemsLeft === 0) { | ||
decoded = true; | ||
break; | ||
} | ||
} | ||
} | ||
catch (e) { | ||
if (!(e instanceof exports.DataViewIndexOutOfBoundsError)) { | ||
throw e; // rethrow | ||
} | ||
// fallthrough | ||
} | ||
this.totalPos += this.pos; | ||
} | ||
} | ||
decodeSync() { | ||
@@ -374,2 +427,19 @@ DECODE: while (true) { | ||
} | ||
readArraySize() { | ||
const headByte = this.readHeadByte(); | ||
switch (headByte) { | ||
case 0xdc: | ||
return this.readU16(); | ||
case 0xdd: | ||
return this.readU32(); | ||
default: { | ||
if (headByte < 0xa0) { | ||
return headByte - 0x90; | ||
} | ||
else { | ||
throw new Error(`Unrecognized array type byte: ${prettyByte_1.prettyByte(headByte)}`); | ||
} | ||
} | ||
} | ||
} | ||
pushMapState(size) { | ||
@@ -376,0 +446,0 @@ if (size > this.maxMapLength) { |
export { encode } from "./encode"; | ||
export { decode } from "./decode"; | ||
export { decodeAsync } from "./decodeAsync"; | ||
export { decodeAsync, decodeArrayStream } from "./decodeAsync"; | ||
export { Decoder } from "./Decoder"; | ||
export { Encoder } from "./Encoder"; | ||
export { ExtensionCodec, ExtensionCodecType, ExtensionDecoderType, ExtensionEncoderType } from "./ExtensionCodec"; | ||
export { ExtData } from "./ExtData"; | ||
export { EXT_TIMESTAMP, encodeDateToTimeSpec, encodeTimeSpecToTimestamp, encodeTimestampExtension, decodeTimestampExtension, } from "./timestamp"; | ||
export { EXT_TIMESTAMP, encodeDateToTimeSpec, encodeTimeSpecToTimestamp, decodeTimestampToTimeSpec, encodeTimestampExtension, decodeTimestampExtension, } from "./timestamp"; | ||
export { WASM_AVAILABLE as __WASM_AVAILABLE } from "./wasmFunctions"; |
@@ -10,2 +10,7 @@ "use strict"; | ||
exports.decodeAsync = decodeAsync_1.decodeAsync; | ||
exports.decodeArrayStream = decodeAsync_1.decodeArrayStream; | ||
var Decoder_1 = require("./Decoder"); | ||
exports.Decoder = Decoder_1.Decoder; | ||
var Encoder_1 = require("./Encoder"); | ||
exports.Encoder = Encoder_1.Encoder; | ||
// Utilitiies for Extension Types: | ||
@@ -20,2 +25,3 @@ var ExtensionCodec_1 = require("./ExtensionCodec"); | ||
exports.encodeTimeSpecToTimestamp = timestamp_1.encodeTimeSpecToTimestamp; | ||
exports.decodeTimestampToTimeSpec = timestamp_1.decodeTimestampToTimeSpec; | ||
exports.encodeTimestampExtension = timestamp_1.encodeTimestampExtension; | ||
@@ -22,0 +28,0 @@ exports.decodeTimestampExtension = timestamp_1.decodeTimestampExtension; |
@@ -9,2 +9,3 @@ export declare const EXT_TIMESTAMP = -1; | ||
export declare function encodeTimestampExtension(object: unknown): Uint8Array | null; | ||
export declare function decodeTimestampToTimeSpec(data: Uint8Array): TimeSpec; | ||
export declare function decodeTimestampExtension(data: Uint8Array): Date; | ||
@@ -11,0 +12,0 @@ export declare const timestampExtension: { |
@@ -43,4 +43,4 @@ "use strict"; | ||
const msec = date.getTime(); | ||
const sec = Math.floor(msec / 1000); | ||
const nsec = (msec - sec * 1000) * 1e6; | ||
const sec = Math.floor(msec / 1e3); | ||
const nsec = (msec - sec * 1e3) * 1e6; | ||
// Normalizes { sec, nsec } to ensure nsec is unsigned. | ||
@@ -64,3 +64,3 @@ const nsecInSec = Math.floor(nsec / 1e9); | ||
exports.encodeTimestampExtension = encodeTimestampExtension; | ||
function decodeTimestampExtension(data) { | ||
function decodeTimestampToTimeSpec(data) { | ||
// data may be 32, 64, or 96 bits | ||
@@ -72,3 +72,4 @@ switch (data.byteLength) { | ||
const sec = view.getUint32(0); | ||
return new Date(sec * 1000); | ||
const nsec = 0; | ||
return { sec, nsec }; | ||
} | ||
@@ -80,5 +81,5 @@ case 8: { | ||
const secLow32 = view.getUint32(4); | ||
const sec = (nsec30AndSecHigh2 & 0x3) * 0x100000000 + secLow32; | ||
const nsec = nsec30AndSecHigh2 >>> 2; | ||
const sec = (nsec30AndSecHigh2 & 0x3) * 0x100000000 + secLow32; | ||
return new Date(sec * 1000 + nsec / 1e6); | ||
return { sec, nsec }; | ||
} | ||
@@ -88,5 +89,5 @@ case 12: { | ||
const view = new DataView(data.buffer, data.byteOffset, data.byteLength); | ||
const sec = int_1.getInt64(view, 4); | ||
const nsec = view.getUint32(0); | ||
const sec = int_1.getInt64(view, 4); | ||
return new Date(sec * 1000 + nsec / 1e6); | ||
return { sec, nsec }; | ||
} | ||
@@ -97,2 +98,7 @@ default: | ||
} | ||
exports.decodeTimestampToTimeSpec = decodeTimestampToTimeSpec; | ||
function decodeTimestampExtension(data) { | ||
const timeSpec = decodeTimestampToTimeSpec(data); | ||
return new Date(timeSpec.sec * 1e3 + timeSpec.nsec / 1e6); | ||
} | ||
exports.decodeTimestampExtension = decodeTimestampExtension; | ||
@@ -99,0 +105,0 @@ exports.timestampExtension = { |
{ | ||
"name": "@msgpack/msgpack", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "MessagePack for JavaScript/ECMA-262", | ||
@@ -56,2 +56,3 @@ "main": "./dist/index.js", | ||
"@bitjourney/check-es-version-webpack-plugin": "^1.0.2", | ||
"@std-proposal/temporal": "0.0.1", | ||
"@types/base64-js": "^1.2.5", | ||
@@ -58,0 +59,0 @@ "@types/mocha": "^5.2.6", |
# MessagePack for JavaScript/ECMA-262 | ||
[![npm version](https://badge.fury.io/js/%40msgpack%2Fmsgpack.svg)](https://badge.fury.io/js/%40msgpack%2Fmsgpack) [![Build Status](https://travis-ci.org/msgpack/msgpack-javascript.svg?branch=master)](https://travis-ci.org/msgpack/msgpack-javascript) [![codecov](https://codecov.io/gh/msgpack/msgpack-javascript/branch/master/graph/badge.svg)](https://codecov.io/gh/msgpack/msgpack-javascript) [![bundlephobia](https://badgen.net/bundlephobia/minzip/@msgpack/msgpack)](https://bundlephobia.com/result?p=@msgpack/msgpack) | ||
[![npm version](https://img.shields.io/npm/v/@msgpack/msgpack.svg)](https://www.npmjs.com/package/@msgpack/msgpack) [![Build Status](https://travis-ci.org/msgpack/msgpack-javascript.svg?branch=master)](https://travis-ci.org/msgpack/msgpack-javascript) [![codecov](https://codecov.io/gh/msgpack/msgpack-javascript/branch/master/graphs/badge.svg)](https://codecov.io/gh/msgpack/msgpack-javascript) [![bundlephobia](https://badgen.net/bundlephobia/minzip/@msgpack/msgpack)](https://bundlephobia.com/result?p=@msgpack/msgpack) | ||
@@ -66,3 +66,3 @@ [![Browser Matrix powered by Sauce Labs](https://saucelabs.com/browser-matrix/gfx2019.svg)](https://saucelabs.com) | ||
### `decodeAsync(stream: AsyncIterable<ArrayLike<number> | Uint8Array>, options?: DecodeAsyncOptions): Promise<unknown>` | ||
### `decodeAsync(stream: AsyncIterable<Uint8Array | ArrayLike<number>> | ReadableStream<Uint8Array | ArrayLike<number>>, options?: DecodeAsyncOptions): Promise<unknown>` | ||
@@ -73,2 +73,14 @@ It decodes `stream` in an async iterable of byte arrays and returns decoded data as `uknown` wrapped in `Promise`. This function works asyncronously. | ||
### `decodeArrayStream(stream: AsyncIterable<Uint8Array | ArrayLike<number>> | ReadableStream<Uint8Array | ArrayLike<number>>, options?: DecodeAsyncOptions): AsyncIterable<unknown>` | ||
It is alike to `decodeAsync()`, but only accepts an array of items as the input `stream`, and emits the decoded item one by one. | ||
It throws errors when the input is not an array. | ||
### `decodeStream(stream: AsyncIterable<Uint8Array | ArrayLike<number>> | ReadableStream<Uint8Array | ArrayLike<number>>, options?: DecodeAsyncOptions): AsyncIterable<unknown>` | ||
It is like to `decodeAsync()` and `decodeArrayStream()`, but the input `stream` consists of independent MessagePack items. | ||
In other words, it decodes an unlimited stream and emits an item one by one. | ||
### Extension Types | ||
@@ -157,2 +169,46 @@ | ||
#### The temporal module as timestamp extensions | ||
This library maps `Date` to the MessagePack timestamp extension, but you re-map the [temporal module](https://github.com/tc39/proposal-temporal) to the timestamp ext like this: | ||
```typescript | ||
import { Instant } from "@std-proposal/temporal"; | ||
import { deepStrictEqual } from "assert"; | ||
import { | ||
encode, | ||
decode, | ||
ExtensionCodec, | ||
EXT_TIMESTAMP, | ||
encodeTimeSpecToTimestamp, | ||
decodeTimestampToTimeSpec, | ||
} from "@msgpack/msgpack"; | ||
const extensionCodec = new ExtensionCodec(); | ||
extensionCodec.register({ | ||
type: EXT_TIMESTAMP, // override the default behavior! | ||
encode: (input: any) => { | ||
if (input instanceof Instant) { | ||
const sec = input.seconds; | ||
const nsec = Number(input.nanoseconds - BigInt(sec) * BigInt(1e9)); | ||
return encodeTimeSpecToTimestamp({ sec, nsec }); | ||
} else { | ||
return null; | ||
} | ||
}, | ||
decode: (data: Uint8Array) => { | ||
const timeSpec = decodeTimestampToTimeSpec(data); | ||
const sec = BigInt(timeSpec.sec); | ||
const nsec = BigInt(timeSpec.nsec); | ||
return Instant.fromEpochNanoseconds(sec * BigInt(1e9) + nsec); | ||
}, | ||
}); | ||
const instant = Instant.fromEpochMilliseconds(Date.now()); | ||
const encoded = encode(instant, { extensionCodec }); | ||
const decoded = decode(encoded, { extensionCodec }); | ||
deepStrictEqual(decoded, instant); | ||
``` | ||
This will be default after the temporal module is implemented in major browsers, which is not a near-future, though. | ||
## MessagePack Mapping Table | ||
@@ -159,0 +215,0 @@ |
@@ -1,4 +0,4 @@ | ||
import { ExtensionCodecType } from "./ExtensionCodec"; | ||
import { Decoder } from "./Decoder"; | ||
import { defaultDecodeOptions, DecodeOptions } from "./decode"; | ||
import { ensureAsyncIterabe, ReadableStreamLike } from "./utils/stream"; | ||
@@ -9,5 +9,7 @@ export type DecodeAsyncOptions = DecodeOptions; | ||
export async function decodeAsync( | ||
stream: AsyncIterable<Uint8Array | ArrayLike<number>>, | ||
streamLike: ReadableStreamLike<Uint8Array | ArrayLike<number>>, | ||
options: DecodeAsyncOptions = defaultDecodeOptions, | ||
): Promise<unknown> { | ||
const stream = ensureAsyncIterabe(streamLike); | ||
const decoder = new Decoder( | ||
@@ -23,1 +25,37 @@ options.extensionCodec, | ||
} | ||
export function decodeArrayStream( | ||
streamLike: ReadableStreamLike<Uint8Array | ArrayLike<number>>, | ||
options: DecodeAsyncOptions = defaultDecodeOptions, | ||
) { | ||
const stream = ensureAsyncIterabe(streamLike); | ||
const decoder = new Decoder( | ||
options.extensionCodec, | ||
options.maxStrLength, | ||
options.maxBinLength, | ||
options.maxArrayLength, | ||
options.maxMapLength, | ||
options.maxExtLength, | ||
); | ||
return decoder.decodeArrayStream(stream); | ||
} | ||
export function decodeStream( | ||
streamLike: ReadableStreamLike<Uint8Array | ArrayLike<number>>, | ||
options: DecodeAsyncOptions = defaultDecodeOptions, | ||
) { | ||
const stream = ensureAsyncIterabe(streamLike); | ||
const decoder = new Decoder( | ||
options.extensionCodec, | ||
options.maxStrLength, | ||
options.maxBinLength, | ||
options.maxArrayLength, | ||
options.maxMapLength, | ||
options.maxExtLength, | ||
); | ||
return decoder.decodeStream(stream); | ||
} |
@@ -77,2 +77,16 @@ import { prettyByte } from "./utils/prettyByte"; | ||
appendBuffer(buffer: Uint8Array | ArrayLike<number>) { | ||
if (this.headByte === HEAD_BYTE_REQUIRED && !this.hasRemaining()) { | ||
this.setBuffer(buffer); | ||
} else { | ||
// retried because data is insufficient | ||
const remainingData = this.bytes.subarray(this.pos); | ||
const newData = ensureUint8Array(buffer); | ||
const concated = new Uint8Array(remainingData.length + newData.length); | ||
concated.set(remainingData); | ||
concated.set(newData, remainingData.length); | ||
this.setBuffer(concated); | ||
} | ||
} | ||
hasRemaining(size = 1) { | ||
@@ -103,14 +117,3 @@ return this.view.byteLength - this.pos >= size; | ||
if (this.headByte === HEAD_BYTE_REQUIRED && !this.hasRemaining()) { | ||
this.setBuffer(buffer); | ||
} else { | ||
// retried because data is insufficient | ||
const remainingData = this.bytes.subarray(this.pos); | ||
const newData = ensureUint8Array(buffer); | ||
const concated = new Uint8Array(remainingData.length + newData.length); | ||
concated.set(remainingData); | ||
concated.set(newData, remainingData.length); | ||
this.setBuffer(concated); | ||
} | ||
//console.log("view", this.view, this.headByte); | ||
this.appendBuffer(buffer); | ||
@@ -142,2 +145,62 @@ try { | ||
async *decodeStream(stream: AsyncIterable<ArrayLike<number> | Uint8Array>) { | ||
for await (const buffer of stream) { | ||
this.appendBuffer(buffer); | ||
try { | ||
while (true) { | ||
const result = this.decodeSync(); | ||
yield result; | ||
} | ||
} catch (e) { | ||
if (!(e instanceof DataViewIndexOutOfBoundsError)) { | ||
throw e; // rethrow | ||
} | ||
// fallthrough | ||
} | ||
} | ||
} | ||
async *decodeArrayStream(stream: AsyncIterable<ArrayLike<number> | Uint8Array>) { | ||
let headerParsed = false; | ||
let decoded = false; | ||
let itemsLeft = 0; | ||
for await (const buffer of stream) { | ||
if (decoded) { | ||
throw this.createNoExtraBytesError(this.totalPos); | ||
} | ||
this.appendBuffer(buffer); | ||
if (!headerParsed) { | ||
itemsLeft = this.readArraySize(); | ||
headerParsed = true; | ||
this.complete(); | ||
} | ||
try { | ||
while (true) { | ||
const result = this.decodeSync(); | ||
yield result; | ||
itemsLeft--; | ||
if (itemsLeft === 0) { | ||
decoded = true; | ||
break; | ||
} | ||
} | ||
} catch (e) { | ||
if (!(e instanceof DataViewIndexOutOfBoundsError)) { | ||
throw e; // rethrow | ||
} | ||
// fallthrough | ||
} | ||
this.totalPos += this.pos; | ||
} | ||
} | ||
decodeSync(): unknown { | ||
@@ -369,2 +432,20 @@ DECODE: while (true) { | ||
readArraySize(): number { | ||
const headByte = this.readHeadByte(); | ||
switch (headByte) { | ||
case 0xdc: | ||
return this.readU16(); | ||
case 0xdd: | ||
return this.readU32(); | ||
default: { | ||
if (headByte < 0xa0) { | ||
return headByte - 0x90; | ||
} else { | ||
throw new Error(`Unrecognized array type byte: ${prettyByte(headByte)}`); | ||
} | ||
} | ||
} | ||
} | ||
pushMapState(size: number) { | ||
@@ -371,0 +452,0 @@ if (size > this.maxMapLength) { |
@@ -5,4 +5,7 @@ // Main Functions: | ||
export { decode } from "./decode"; | ||
export { decodeAsync } from "./decodeAsync"; | ||
export { decodeAsync, decodeArrayStream } from "./decodeAsync"; | ||
export { Decoder } from "./Decoder"; | ||
export { Encoder } from "./Encoder"; | ||
// Utilitiies for Extension Types: | ||
@@ -16,2 +19,3 @@ | ||
encodeTimeSpecToTimestamp, | ||
decodeTimestampToTimeSpec, | ||
encodeTimestampExtension, | ||
@@ -18,0 +22,0 @@ decodeTimestampExtension, |
@@ -47,4 +47,4 @@ // https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type | ||
const msec = date.getTime(); | ||
const sec = Math.floor(msec / 1000); | ||
const nsec = (msec - sec * 1000) * 1e6; | ||
const sec = Math.floor(msec / 1e3); | ||
const nsec = (msec - sec * 1e3) * 1e6; | ||
@@ -68,3 +68,3 @@ // Normalizes { sec, nsec } to ensure nsec is unsigned. | ||
export function decodeTimestampExtension(data: Uint8Array): Date { | ||
export function decodeTimestampToTimeSpec(data: Uint8Array): TimeSpec { | ||
// data may be 32, 64, or 96 bits | ||
@@ -76,3 +76,4 @@ switch (data.byteLength) { | ||
const sec = view.getUint32(0); | ||
return new Date(sec * 1000); | ||
const nsec = 0; | ||
return { sec, nsec }; | ||
} | ||
@@ -85,5 +86,5 @@ case 8: { | ||
const secLow32 = view.getUint32(4); | ||
const sec = (nsec30AndSecHigh2 & 0x3) * 0x100000000 + secLow32; | ||
const nsec = nsec30AndSecHigh2 >>> 2; | ||
const sec = (nsec30AndSecHigh2 & 0x3) * 0x100000000 + secLow32; | ||
return new Date(sec * 1000 + nsec / 1e6); | ||
return { sec, nsec }; | ||
} | ||
@@ -94,6 +95,5 @@ case 12: { | ||
const sec = getInt64(view, 4); | ||
const nsec = view.getUint32(0); | ||
const sec = getInt64(view, 4); | ||
return new Date(sec * 1000 + nsec / 1e6); | ||
return { sec, nsec }; | ||
} | ||
@@ -105,2 +105,7 @@ default: | ||
export function decodeTimestampExtension(data: Uint8Array): Date { | ||
const timeSpec = decodeTimestampToTimeSpec(data); | ||
return new Date(timeSpec.sec * 1e3 + timeSpec.nsec / 1e6); | ||
} | ||
export const timestampExtension = { | ||
@@ -107,0 +112,0 @@ type: EXT_TIMESTAMP, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
606004
72
7068
318
34