@contrail/util 1.1.6-0 → 1.1.6-alpha.1
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.
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function decompressFromBase64<T>(base64: string): Promise<T>;
|
|
4
|
-
export declare function compressObjectIntoBuffer(obj: any): Promise<Buffer>;
|
|
5
|
-
export declare function convertBufferToBase64(buffer: Buffer): string;
|
|
6
|
-
export declare function convertBase64ToBuffer(base64: string): Buffer;
|
|
7
|
-
export declare function decompressBuffer(buffer: Buffer): Promise<any>;
|
|
1
|
+
export declare function compressIntoString(obj: any): string;
|
|
2
|
+
export declare function decompressFromString(str: string): any;
|
|
@@ -1,57 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (err)
|
|
32
|
-
reject(err);
|
|
33
|
-
else
|
|
34
|
-
resolve(buffer);
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
exports.compressObjectIntoBuffer = compressObjectIntoBuffer;
|
|
39
|
-
function convertBufferToBase64(buffer) {
|
|
40
|
-
return buffer.toString('base64');
|
|
41
|
-
}
|
|
42
|
-
exports.convertBufferToBase64 = convertBufferToBase64;
|
|
43
|
-
function convertBase64ToBuffer(base64) {
|
|
44
|
-
return Buffer.from(base64, 'base64');
|
|
3
|
+
exports.decompressFromString = exports.compressIntoString = void 0;
|
|
4
|
+
const fflate_1 = require("fflate");
|
|
5
|
+
const bufferToString = (buffer) => {
|
|
6
|
+
let result = '';
|
|
7
|
+
for (let value of buffer)
|
|
8
|
+
result += String.fromCharCode(value);
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
const stringToBuffer = (str) => {
|
|
12
|
+
let result = new Uint8Array(str.length);
|
|
13
|
+
for (let i = 0; i < str.length; ++i)
|
|
14
|
+
result[i] = str.charCodeAt(i);
|
|
15
|
+
return result;
|
|
16
|
+
};
|
|
17
|
+
function compressIntoString(obj) {
|
|
18
|
+
const json = obj === null || obj === undefined ? null : obj;
|
|
19
|
+
const buffer = stringToBuffer(JSON.stringify(json));
|
|
20
|
+
const compressed = (0, fflate_1.compressSync)(buffer);
|
|
21
|
+
return bufferToString(compressed);
|
|
45
22
|
}
|
|
46
|
-
exports.
|
|
47
|
-
function
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
else
|
|
53
|
-
resolve(JSON.parse(result.toString()));
|
|
54
|
-
});
|
|
55
|
-
});
|
|
23
|
+
exports.compressIntoString = compressIntoString;
|
|
24
|
+
function decompressFromString(str) {
|
|
25
|
+
const buffer = stringToBuffer(str);
|
|
26
|
+
const decompressed = (0, fflate_1.decompressSync)(buffer);
|
|
27
|
+
const originalText = new TextDecoder().decode(decompressed);
|
|
28
|
+
return JSON.parse(originalText);
|
|
56
29
|
}
|
|
57
|
-
exports.
|
|
30
|
+
exports.decompressFromString = decompressFromString;
|
|
@@ -67,16 +67,6 @@ class StringUtil {
|
|
|
67
67
|
static isArray(value) {
|
|
68
68
|
return Array.isArray(value);
|
|
69
69
|
}
|
|
70
|
-
static decodeEncodedString(encodedString) {
|
|
71
|
-
return encodedString.replace(/_COMMA_/g, ',').replace(/%[0-9A-Fa-f]{2}/g, (match) => {
|
|
72
|
-
try {
|
|
73
|
-
return decodeURIComponent(match);
|
|
74
|
-
}
|
|
75
|
-
catch (_a) {
|
|
76
|
-
return match;
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
70
|
static getStringSizeInMB(str) {
|
|
81
71
|
const bytes = new TextEncoder().encode(str).length;
|
|
82
72
|
return bytes / (1024 * 1024);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrail/util",
|
|
3
|
-
"version": "1.1.6-
|
|
3
|
+
"version": "1.1.6-alpha.1",
|
|
4
4
|
"description": "General javascript utilities",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@contrail/types": "^3.0.95",
|
|
42
|
+
"fflate": "^0.8.2",
|
|
42
43
|
"lodash": "^4.17.21"
|
|
43
44
|
}
|
|
44
45
|
}
|