@pamoja/codec 0.1.15 → 0.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +80 -0
- package/dist/index.js +117 -0
- package/package.json +2 -2
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ergonomic facade over the generated codec binding.
|
|
3
|
+
*
|
|
4
|
+
* JavaScript already has JSON, so the facade takes and returns ordinary values
|
|
5
|
+
* and does the encoding itself, leaving callers to think in documents rather than
|
|
6
|
+
* buffers. The conversion and the packing happen in the native core reached
|
|
7
|
+
* through the generated contract.
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Encodes a value as CBOR, which is typically much smaller than its JSON form.
|
|
13
|
+
*
|
|
14
|
+
* @param value - Any JSON-serializable value, or the raw bytes of a JSON
|
|
15
|
+
* document.
|
|
16
|
+
* @returns The CBOR encoding.
|
|
17
|
+
* @throws If the value cannot be encoded.
|
|
18
|
+
*/
|
|
19
|
+
export declare function toCbor(value: unknown): Buffer;
|
|
20
|
+
/**
|
|
21
|
+
* Decodes a CBOR document back into an ordinary JavaScript value.
|
|
22
|
+
*
|
|
23
|
+
* @param cbor - The CBOR document to decode.
|
|
24
|
+
* @returns The decoded value.
|
|
25
|
+
* @throws If the document is malformed, or holds a construct with no JSON
|
|
26
|
+
* equivalent such as a non-string map key.
|
|
27
|
+
*/
|
|
28
|
+
export declare function fromCbor(cbor: Uint8Array): unknown;
|
|
29
|
+
/**
|
|
30
|
+
* Delta-encodes a series of integer samples into a compact buffer.
|
|
31
|
+
*
|
|
32
|
+
* @param samples - The samples, in order.
|
|
33
|
+
* @returns The packed encoding, far smaller than the samples for a slow-moving
|
|
34
|
+
* series.
|
|
35
|
+
*/
|
|
36
|
+
export declare function packSamples(samples: readonly number[]): Buffer;
|
|
37
|
+
/**
|
|
38
|
+
* Unpacks a buffer produced by {@link packSamples}.
|
|
39
|
+
*
|
|
40
|
+
* @param bytes - The packed encoding.
|
|
41
|
+
* @returns The samples, in order.
|
|
42
|
+
* @throws If the buffer is malformed.
|
|
43
|
+
*/
|
|
44
|
+
export declare function unpackSamples(bytes: Uint8Array): number[];
|
|
45
|
+
/**
|
|
46
|
+
* Packs float readings to a fixed precision, for a link that charges per byte.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const quantizer = new Quantizer(100) // keep two decimal places
|
|
51
|
+
* const packed = quantizer.encode([20.0, 20.1, 20.2])
|
|
52
|
+
* quantizer.decode(packed) // [20.0, 20.1, 20.2], to within 0.01
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare class Quantizer {
|
|
56
|
+
#private;
|
|
57
|
+
/**
|
|
58
|
+
* Creates a quantizer at the given precision.
|
|
59
|
+
*
|
|
60
|
+
* @param scale - The multiplier applied before rounding; `100` keeps two
|
|
61
|
+
* decimal places. Must be positive and finite.
|
|
62
|
+
* @throws If the scale is not positive and finite.
|
|
63
|
+
*/
|
|
64
|
+
constructor(scale: number);
|
|
65
|
+
/**
|
|
66
|
+
* Quantizes and packs a batch of readings.
|
|
67
|
+
*
|
|
68
|
+
* @param readings - The readings, in order.
|
|
69
|
+
* @returns The packed encoding.
|
|
70
|
+
*/
|
|
71
|
+
encode(readings: readonly number[]): Buffer;
|
|
72
|
+
/**
|
|
73
|
+
* Unpacks a batch, to within this quantizer's precision.
|
|
74
|
+
*
|
|
75
|
+
* @param bytes - The encoding produced by {@link encode} at the same scale.
|
|
76
|
+
* @returns The readings, in order.
|
|
77
|
+
* @throws If the buffer is malformed.
|
|
78
|
+
*/
|
|
79
|
+
decode(bytes: Uint8Array): number[];
|
|
80
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Ergonomic facade over the generated codec binding.
|
|
4
|
+
*
|
|
5
|
+
* JavaScript already has JSON, so the facade takes and returns ordinary values
|
|
6
|
+
* and does the encoding itself, leaving callers to think in documents rather than
|
|
7
|
+
* buffers. The conversion and the packing happen in the native core reached
|
|
8
|
+
* through the generated contract.
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
*/
|
|
12
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
13
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
14
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
15
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
16
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
17
|
+
};
|
|
18
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
19
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
20
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
21
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
22
|
+
};
|
|
23
|
+
var _Quantizer_native;
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.Quantizer = void 0;
|
|
26
|
+
exports.toCbor = toCbor;
|
|
27
|
+
exports.fromCbor = fromCbor;
|
|
28
|
+
exports.packSamples = packSamples;
|
|
29
|
+
exports.unpackSamples = unpackSamples;
|
|
30
|
+
const native_1 = require("@pamoja/native");
|
|
31
|
+
/**
|
|
32
|
+
* Encodes a value as CBOR, which is typically much smaller than its JSON form.
|
|
33
|
+
*
|
|
34
|
+
* @param value - Any JSON-serializable value, or the raw bytes of a JSON
|
|
35
|
+
* document.
|
|
36
|
+
* @returns The CBOR encoding.
|
|
37
|
+
* @throws If the value cannot be encoded.
|
|
38
|
+
*/
|
|
39
|
+
function toCbor(value) {
|
|
40
|
+
const json = value instanceof Uint8Array ? Buffer.from(value) : Buffer.from(JSON.stringify(value), 'utf8');
|
|
41
|
+
return (0, native_1.jsonToCborBytes)(json);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Decodes a CBOR document back into an ordinary JavaScript value.
|
|
45
|
+
*
|
|
46
|
+
* @param cbor - The CBOR document to decode.
|
|
47
|
+
* @returns The decoded value.
|
|
48
|
+
* @throws If the document is malformed, or holds a construct with no JSON
|
|
49
|
+
* equivalent such as a non-string map key.
|
|
50
|
+
*/
|
|
51
|
+
function fromCbor(cbor) {
|
|
52
|
+
return JSON.parse((0, native_1.cborToJsonBytes)(Buffer.from(cbor)).toString('utf8'));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Delta-encodes a series of integer samples into a compact buffer.
|
|
56
|
+
*
|
|
57
|
+
* @param samples - The samples, in order.
|
|
58
|
+
* @returns The packed encoding, far smaller than the samples for a slow-moving
|
|
59
|
+
* series.
|
|
60
|
+
*/
|
|
61
|
+
function packSamples(samples) {
|
|
62
|
+
return (0, native_1.encodeDeltaSamples)(samples);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Unpacks a buffer produced by {@link packSamples}.
|
|
66
|
+
*
|
|
67
|
+
* @param bytes - The packed encoding.
|
|
68
|
+
* @returns The samples, in order.
|
|
69
|
+
* @throws If the buffer is malformed.
|
|
70
|
+
*/
|
|
71
|
+
function unpackSamples(bytes) {
|
|
72
|
+
return (0, native_1.decodeDeltaSamples)(Buffer.from(bytes));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Packs float readings to a fixed precision, for a link that charges per byte.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const quantizer = new Quantizer(100) // keep two decimal places
|
|
80
|
+
* const packed = quantizer.encode([20.0, 20.1, 20.2])
|
|
81
|
+
* quantizer.decode(packed) // [20.0, 20.1, 20.2], to within 0.01
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
class Quantizer {
|
|
85
|
+
/**
|
|
86
|
+
* Creates a quantizer at the given precision.
|
|
87
|
+
*
|
|
88
|
+
* @param scale - The multiplier applied before rounding; `100` keeps two
|
|
89
|
+
* decimal places. Must be positive and finite.
|
|
90
|
+
* @throws If the scale is not positive and finite.
|
|
91
|
+
*/
|
|
92
|
+
constructor(scale) {
|
|
93
|
+
_Quantizer_native.set(this, void 0);
|
|
94
|
+
__classPrivateFieldSet(this, _Quantizer_native, new native_1.Quantizer(scale), "f");
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Quantizes and packs a batch of readings.
|
|
98
|
+
*
|
|
99
|
+
* @param readings - The readings, in order.
|
|
100
|
+
* @returns The packed encoding.
|
|
101
|
+
*/
|
|
102
|
+
encode(readings) {
|
|
103
|
+
return __classPrivateFieldGet(this, _Quantizer_native, "f").encode(readings);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Unpacks a batch, to within this quantizer's precision.
|
|
107
|
+
*
|
|
108
|
+
* @param bytes - The encoding produced by {@link encode} at the same scale.
|
|
109
|
+
* @returns The readings, in order.
|
|
110
|
+
* @throws If the buffer is malformed.
|
|
111
|
+
*/
|
|
112
|
+
decode(bytes) {
|
|
113
|
+
return __classPrivateFieldGet(this, _Quantizer_native, "f").decode(Buffer.from(bytes));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.Quantizer = Quantizer;
|
|
117
|
+
_Quantizer_native = new WeakMap();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pamoja/codec",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.16",
|
|
4
4
|
"description": "CBOR, JSON, and raw codecs behind one trait, delta and varint batch packing, and an f32 quantizer for metered links.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -34,6 +34,6 @@
|
|
|
34
34
|
"node": ">= 16"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@pamoja/native": "0.1.
|
|
37
|
+
"@pamoja/native": "0.1.16"
|
|
38
38
|
}
|
|
39
39
|
}
|