2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2023-07-10 03:09:49 +00:00
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { concat } from "@std/bytes/concat";
|
2023-07-10 03:09:49 +00:00
|
|
|
|
2024-01-18 05:50:35 +00:00
|
|
|
/**
|
|
|
|
* Value types that can be encoded to MessagePack.
|
|
|
|
*/
|
2023-07-10 03:09:49 +00:00
|
|
|
export type ValueType =
|
|
|
|
| number
|
|
|
|
| bigint
|
|
|
|
| string
|
|
|
|
| boolean
|
|
|
|
| null
|
|
|
|
| Uint8Array
|
2024-08-27 05:16:03 +00:00
|
|
|
| readonly ValueType[]
|
2023-07-10 03:09:49 +00:00
|
|
|
| ValueMap;
|
|
|
|
|
2024-01-18 05:50:35 +00:00
|
|
|
/**
|
|
|
|
* Value map that can be encoded to MessagePack.
|
|
|
|
*/
|
|
|
|
export interface ValueMap {
|
2024-06-12 03:49:23 +00:00
|
|
|
/** Value map entry */
|
2023-07-10 03:09:49 +00:00
|
|
|
[index: string | number]: ValueType;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FOUR_BITS = 16;
|
|
|
|
const FIVE_BITS = 32;
|
|
|
|
const SEVEN_BITS = 128;
|
|
|
|
const EIGHT_BITS = 256;
|
|
|
|
const FIFTEEN_BITS = 32768;
|
|
|
|
const SIXTEEN_BITS = 65536;
|
|
|
|
const THIRTY_ONE_BITS = 2147483648;
|
|
|
|
const THIRTY_TWO_BITS = 4294967296;
|
|
|
|
const SIXTY_THREE_BITS = 9223372036854775808n;
|
|
|
|
const SIXTY_FOUR_BITS = 18446744073709551616n;
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
|
|
|
/**
|
2024-06-12 03:49:23 +00:00
|
|
|
* Encode a value to {@link https://msgpack.org/ | MessagePack} binary format.
|
2023-07-10 03:09:49 +00:00
|
|
|
*
|
2024-06-12 03:49:23 +00:00
|
|
|
* @example Usage
|
2023-07-10 03:09:49 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { encode } from "@std/msgpack/encode";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-07-10 03:09:49 +00:00
|
|
|
*
|
|
|
|
* const obj = {
|
|
|
|
* str: "deno",
|
|
|
|
* arr: [1, 2, 3],
|
|
|
|
* map: {
|
|
|
|
* foo: "bar"
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2024-06-12 03:49:23 +00:00
|
|
|
* const encoded = encode(obj);
|
|
|
|
*
|
|
|
|
* assertEquals(encoded.length, 31);
|
2023-07-10 03:09:49 +00:00
|
|
|
* ```
|
2024-05-22 12:20:52 +00:00
|
|
|
*
|
|
|
|
* @param object Value to encode to MessagePack binary format.
|
|
|
|
* @returns Encoded MessagePack binary data.
|
2023-07-10 03:09:49 +00:00
|
|
|
*/
|
2023-12-19 00:26:13 +00:00
|
|
|
export function encode(object: ValueType): Uint8Array {
|
2023-08-31 10:40:32 +00:00
|
|
|
const byteParts: Uint8Array[] = [];
|
|
|
|
encodeSlice(object, byteParts);
|
2023-11-08 08:30:22 +00:00
|
|
|
return concat(byteParts);
|
2023-07-10 03:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function encodeFloat64(num: number) {
|
|
|
|
const dataView = new DataView(new ArrayBuffer(9));
|
|
|
|
dataView.setFloat64(1, num);
|
|
|
|
dataView.setUint8(0, 0xcb);
|
|
|
|
return new Uint8Array(dataView.buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
function encodeNumber(num: number) {
|
|
|
|
if (!Number.isInteger(num)) { // float 64
|
|
|
|
return encodeFloat64(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num < 0) {
|
|
|
|
if (num >= -FIVE_BITS) { // negative fixint
|
|
|
|
return new Uint8Array([num]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num >= -SEVEN_BITS) { // int 8
|
|
|
|
return new Uint8Array([0xd0, num]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num >= -FIFTEEN_BITS) { // int 16
|
|
|
|
const dataView = new DataView(new ArrayBuffer(3));
|
|
|
|
dataView.setInt16(1, num);
|
|
|
|
dataView.setUint8(0, 0xd1);
|
|
|
|
return new Uint8Array(dataView.buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num >= -THIRTY_ONE_BITS) { // int 32
|
|
|
|
const dataView = new DataView(new ArrayBuffer(5));
|
|
|
|
dataView.setInt32(1, num);
|
|
|
|
dataView.setUint8(0, 0xd2);
|
|
|
|
return new Uint8Array(dataView.buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// float 64
|
|
|
|
return encodeFloat64(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the number fits within a positive fixint, use it
|
|
|
|
if (num <= 0x7f) {
|
|
|
|
return new Uint8Array([num]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num < EIGHT_BITS) { // uint8
|
|
|
|
return new Uint8Array([0xcc, num]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num < SIXTEEN_BITS) { // uint16
|
|
|
|
const dataView = new DataView(new ArrayBuffer(3));
|
|
|
|
dataView.setUint16(1, num);
|
|
|
|
dataView.setUint8(0, 0xcd);
|
|
|
|
return new Uint8Array(dataView.buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num < THIRTY_TWO_BITS) { // uint32
|
|
|
|
const dataView = new DataView(new ArrayBuffer(5));
|
|
|
|
dataView.setUint32(1, num);
|
|
|
|
dataView.setUint8(0, 0xce);
|
|
|
|
return new Uint8Array(dataView.buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// float 64
|
|
|
|
return encodeFloat64(num);
|
|
|
|
}
|
|
|
|
|
2023-08-31 10:40:32 +00:00
|
|
|
function encodeSlice(object: ValueType, byteParts: Uint8Array[]) {
|
2023-07-10 03:09:49 +00:00
|
|
|
if (object === null) {
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array([0xc0]));
|
2023-07-10 03:09:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object === false) {
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array([0xc2]));
|
2023-07-10 03:09:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object === true) {
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array([0xc3]));
|
2023-07-10 03:09:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof object === "number") {
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(encodeNumber(object));
|
2023-07-10 03:09:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof object === "bigint") {
|
|
|
|
if (object < 0) {
|
|
|
|
if (object < -SIXTY_THREE_BITS) {
|
|
|
|
throw new Error("Cannot safely encode bigint larger than 64 bits");
|
|
|
|
}
|
|
|
|
|
|
|
|
const dataView = new DataView(new ArrayBuffer(9));
|
|
|
|
dataView.setBigInt64(1, object);
|
|
|
|
dataView.setUint8(0, 0xd3);
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array(dataView.buffer));
|
2023-07-10 03:09:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object >= SIXTY_FOUR_BITS) {
|
|
|
|
throw new Error("Cannot safely encode bigint larger than 64 bits");
|
|
|
|
}
|
|
|
|
|
|
|
|
const dataView = new DataView(new ArrayBuffer(9));
|
|
|
|
dataView.setBigUint64(1, object);
|
|
|
|
dataView.setUint8(0, 0xcf);
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array(dataView.buffer));
|
2023-07-10 03:09:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof object === "string") {
|
|
|
|
const encoded = encoder.encode(object);
|
|
|
|
const len = encoded.length;
|
|
|
|
|
|
|
|
if (len < FIVE_BITS) { // fixstr
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array([0xa0 | len]));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else if (len < EIGHT_BITS) { // str 8
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array([0xd9, len]));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else if (len < SIXTEEN_BITS) { // str 16
|
|
|
|
const dataView = new DataView(new ArrayBuffer(3));
|
|
|
|
dataView.setUint16(1, len);
|
|
|
|
dataView.setUint8(0, 0xda);
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array(dataView.buffer));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else if (len < THIRTY_TWO_BITS) { // str 32
|
|
|
|
const dataView = new DataView(new ArrayBuffer(5));
|
|
|
|
dataView.setUint32(1, len);
|
|
|
|
dataView.setUint8(0, 0xdb);
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array(dataView.buffer));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
"Cannot safely encode string with size larger than 32 bits",
|
|
|
|
);
|
|
|
|
}
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(encoded);
|
2023-07-10 03:09:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object instanceof Uint8Array) {
|
|
|
|
if (object.length < EIGHT_BITS) { // bin 8
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array([0xc4, object.length]));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else if (object.length < SIXTEEN_BITS) { // bin 16
|
|
|
|
const dataView = new DataView(new ArrayBuffer(3));
|
|
|
|
dataView.setUint16(1, object.length);
|
|
|
|
dataView.setUint8(0, 0xc5);
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array(dataView.buffer));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else if (object.length < THIRTY_TWO_BITS) { // bin 32
|
|
|
|
const dataView = new DataView(new ArrayBuffer(5));
|
|
|
|
dataView.setUint32(1, object.length);
|
|
|
|
dataView.setUint8(0, 0xc6);
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array(dataView.buffer));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
"Cannot safely encode Uint8Array with size larger than 32 bits",
|
|
|
|
);
|
|
|
|
}
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(object);
|
2023-07-10 03:09:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(object)) {
|
|
|
|
if (object.length < FOUR_BITS) { // fixarray
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array([0x90 | object.length]));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else if (object.length < SIXTEEN_BITS) { // array 16
|
|
|
|
const dataView = new DataView(new ArrayBuffer(3));
|
|
|
|
dataView.setUint16(1, object.length);
|
|
|
|
dataView.setUint8(0, 0xdc);
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array(dataView.buffer));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else if (object.length < THIRTY_TWO_BITS) { // array 32
|
|
|
|
const dataView = new DataView(new ArrayBuffer(5));
|
|
|
|
dataView.setUint32(1, object.length);
|
|
|
|
dataView.setUint8(0, 0xdd);
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array(dataView.buffer));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
"Cannot safely encode array with size larger than 32 bits",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const obj of object) {
|
2023-08-31 10:40:32 +00:00
|
|
|
encodeSlice(obj, byteParts);
|
2023-07-10 03:09:49 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If object is a plain object
|
2024-05-20 22:06:56 +00:00
|
|
|
const prototype = Object.getPrototypeOf(object);
|
|
|
|
|
|
|
|
if (prototype === null || prototype === Object.prototype) {
|
2023-07-10 03:09:49 +00:00
|
|
|
const numKeys = Object.keys(object).length;
|
|
|
|
|
|
|
|
if (numKeys < FOUR_BITS) { // fixarray
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array([0x80 | numKeys]));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else if (numKeys < SIXTEEN_BITS) { // map 16
|
|
|
|
const dataView = new DataView(new ArrayBuffer(3));
|
|
|
|
dataView.setUint16(1, numKeys);
|
|
|
|
dataView.setUint8(0, 0xde);
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array(dataView.buffer));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else if (numKeys < THIRTY_TWO_BITS) { // map 32
|
|
|
|
const dataView = new DataView(new ArrayBuffer(5));
|
|
|
|
dataView.setUint32(1, numKeys);
|
|
|
|
dataView.setUint8(0, 0xdf);
|
2023-08-31 10:40:32 +00:00
|
|
|
byteParts.push(new Uint8Array(dataView.buffer));
|
2023-07-10 03:09:49 +00:00
|
|
|
} else {
|
|
|
|
throw new Error("Cannot safely encode map with size larger than 32 bits");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(object)) {
|
2023-08-31 10:40:32 +00:00
|
|
|
encodeSlice(key, byteParts);
|
|
|
|
encodeSlice(value, byteParts);
|
2023-07-10 03:09:49 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("Cannot safely encode value into messagepack");
|
|
|
|
}
|