1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.encodeIntoCompressedBase64 = exports.decodeFromCompressedBase64 = exports.decompress = void 0;
|
---|
4 | /*
|
---|
5 | * This is a TypeScript port of the original Java version, which was written by
|
---|
6 | * Gil Tene as described in
|
---|
7 | * https://github.com/HdrHistogram/HdrHistogram
|
---|
8 | * and released to the public domain, as explained at
|
---|
9 | * http://creativecommons.org/publicdomain/zero/1.0/
|
---|
10 | */
|
---|
11 | const JsHistogram_1 = require("./JsHistogram");
|
---|
12 | const ByteBuffer_1 = require("./ByteBuffer");
|
---|
13 | const wasm_1 = require("./wasm");
|
---|
14 | // @ts-ignore
|
---|
15 | const base64 = require("base64-js");
|
---|
16 | const JsHistogram_encoding_1 = require("./JsHistogram.encoding");
|
---|
17 | const V2CompressedEncodingCookieBase = 0x1c849304;
|
---|
18 | const compressedEncodingCookie = V2CompressedEncodingCookieBase | 0x10; // LSBit of wordsize byte indicates TLZE Encoding
|
---|
19 | function decompress(data) {
|
---|
20 | const buffer = new ByteBuffer_1.default(data);
|
---|
21 | const initialTargetPosition = buffer.position;
|
---|
22 | const cookie = buffer.getInt32();
|
---|
23 | if ((cookie & ~0xf0) !== V2CompressedEncodingCookieBase) {
|
---|
24 | throw new Error("Encoding not supported, only V2 is supported");
|
---|
25 | }
|
---|
26 | const lengthOfCompressedContents = buffer.getInt32();
|
---|
27 | const uncompressedBuffer = JsHistogram_encoding_1.inflate(buffer.data.slice(initialTargetPosition + 8, initialTargetPosition + 8 + lengthOfCompressedContents));
|
---|
28 | return uncompressedBuffer;
|
---|
29 | }
|
---|
30 | exports.decompress = decompress;
|
---|
31 | exports.decodeFromCompressedBase64 = (base64String, bitBucketSize = 32, useWebAssembly = false, minBarForHighestTrackableValue = 0) => {
|
---|
32 | const data = base64.toByteArray(base64String.trim());
|
---|
33 | const uncompressedData = decompress(data);
|
---|
34 | if (useWebAssembly) {
|
---|
35 | return wasm_1.WasmHistogram.decode(uncompressedData, bitBucketSize, minBarForHighestTrackableValue);
|
---|
36 | }
|
---|
37 | return JsHistogram_1.JsHistogram.decode(uncompressedData, bitBucketSize, minBarForHighestTrackableValue);
|
---|
38 | };
|
---|
39 | function encodeWasmIntoCompressedBase64(compressionLevel) {
|
---|
40 | const compressionOptions = compressionLevel
|
---|
41 | ? { level: compressionLevel }
|
---|
42 | : {};
|
---|
43 | const self = this;
|
---|
44 | const targetBuffer = ByteBuffer_1.default.allocate();
|
---|
45 | targetBuffer.putInt32(compressedEncodingCookie);
|
---|
46 | const uncompressedData = self.encode();
|
---|
47 | const compressedData = JsHistogram_encoding_1.deflate(uncompressedData, compressionOptions);
|
---|
48 | targetBuffer.putInt32(compressedData.byteLength);
|
---|
49 | targetBuffer.putArray(compressedData);
|
---|
50 | return base64.fromByteArray(targetBuffer.data);
|
---|
51 | }
|
---|
52 | wasm_1.WasmHistogram.prototype.encodeIntoCompressedBase64 = encodeWasmIntoCompressedBase64;
|
---|
53 | exports.encodeIntoCompressedBase64 = (histogram, compressionLevel) => {
|
---|
54 | if (histogram instanceof wasm_1.WasmHistogram) {
|
---|
55 | return histogram.encodeIntoCompressedBase64(compressionLevel);
|
---|
56 | }
|
---|
57 | if (histogram instanceof JsHistogram_1.JsHistogram) {
|
---|
58 | return histogram.encodeIntoCompressedBase64(compressionLevel);
|
---|
59 | }
|
---|
60 | throw new Error("Unsupported Histogram implementation");
|
---|
61 | };
|
---|
62 | //# sourceMappingURL=encoding.js.map |
---|