0.1.6 • Published 12 months ago
divine-binary-object v0.1.6
An easy way to store objects that contain binary data in JavaScript.
You can use typed objects to make sure that a value gets stored as a certian type.
Otherwise you can just make a normal JavaScript object with TypedArrays.
import { DBO } from "divine-binary-object";
/*
Normal Object mode
*/
(() => {
const buf = DBO.objectToBuffer({
v0: [
{
v0: new Uint8ClampedArray([1, 2, 3, 4]),
},
{
v0: new Uint16Array([1, 2, 3, 4]),
},
],
v1: 12.12,
v2: DBO.nodes._16ui(1212),
v3: "value 3",
v4: [1, 2, 3, 4, 5, 6, 7, 8],
v5: ["hello", "sup"],
v6: {
hello: "sup",
},
});
console.log("[OBJECT MODE]");
console.log(buf);
const obj = DBO.bufferToObject<any>(buf);
console.log(obj);
})();
const TNM = DBO.nodes;
/*
Typed Node Mode
*/
(() => {
const mmdData = TNM.object({
v0: TNM.array([
TNM.object({
v0: TNM.typedArray("8uic", [1, 2, 3, 4]),
}),
TNM.object({
v0: TNM.typedArray("16ui", [1, 2, 3, 4]),
}),
]),
v1: TNM._32f(12.12),
v2: TNM._16ui(1212),
v3: TNM.string("value 3"),
v4: TNM.typedArray("16ui", [1, 2, 3, 4, 5, 6, 7, 8]),
v5: TNM.stringArray(["hello", "sup"]),
v6: TNM.json({
hello: "sup",
}),
});
console.log("[TYPED NODE MODE]");
const buffer = DBO.toBuffer(mmdData);
console.log(buffer);
const object = DBO.bufferToObject(buffer);
console.log(object);
})();
[OBJECT MODE]
ArrayBuffer {
[Uint8Contents]: <00 03 08 02 00 76 00 30 06 03 08 02 00 76 00 30 19 0b 00 00 00 04 01 02 03 04 04 03 08 02 00 76 00 30 19 0d 00 00 00 04
00 01 00 02 00 03 00 04 04 07 08 02 00 76 00 31 11 40 28 3d 70 a3 d7 0a 3d 08 02 00 76 00 32 0d 04 bc 08 02 00 76 00 33 16 00 00 00 07 00 76 00 61 00 6c 00 75 00 65 00 20 00 33 08 ... 146 more bytes>,
byteLength: 246
}
{
v0: [ { v0: [Uint8ClampedArray] }, { v0: [Uint16Array] } ],
v1: 12.12,
v2: 1212,
v3: 'value 3',
v4: [
1, 2, 3, 4,
5, 6, 7, 8
],
v5: [ 'hello', 'sup' ],
v6: { hello: 'sup' }
}
[TYPED NODE MODE]
ArrayBuffer {
[Uint8Contents]: <00 03 08 02 00 76 00 30 06 03 08 02 00 76 00 30 19 0b 00 00 00 04 01 02 03 04 04 03 08 02 00 76 00 30 19 0d 00 00 00 04
00 01 00 02 00 03 00 04 04 07 08 02 00 76 00 31 0e 41 41 eb 85 08 02 00 76 00 32 0d 04 bc 08 02 00 76 00 33 16 00 00 00 07 00 76 00 61 00 6c 00 75 00 65 00 20 00 33 08 02 00 76 00 ... 101 more bytes>,
byteLength: 201
}
{
v0: [ { v0: [Uint8ClampedArray] }, { v0: [Uint16Array] } ],
v1: 12.119999885559082,
v2: 1212,
v3: 'value 3',
v4: Uint16Array(8) [
1, 2, 3, 4,
5, 6, 7, 8
],
v5: [ 'hello', 'sup' ],
v6: { hello: 'sup' }
}