@contrail/util 1.1.3 → 1.1.5-alpha-compression-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.
- package/lib/compression-util/compression.d.ts +7 -0
- package/lib/compression-util/compression.js +57 -0
- package/lib/compression-util/index.d.ts +1 -0
- package/lib/compression-util/index.js +17 -0
- package/lib/index.d.ts +6 -5
- package/lib/index.js +6 -5
- package/lib/timer-util/timer-util.d.ts +16 -0
- package/lib/timer-util/timer-util.js +55 -1
- package/package.json +1 -1
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare function compressToBase64(obj: any): Promise<string>;
|
|
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>;
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.decompressBuffer = exports.convertBase64ToBuffer = exports.convertBufferToBase64 = exports.compressObjectIntoBuffer = exports.decompressFromBase64 = exports.compressToBase64 = void 0;
|
|
13
|
+
const zlib_1 = require("zlib");
|
|
14
|
+
function compressToBase64(obj) {
|
|
15
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
+
return compressObjectIntoBuffer(obj).then((buffer) => convertBufferToBase64(buffer));
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
exports.compressToBase64 = compressToBase64;
|
|
20
|
+
function decompressFromBase64(base64) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const buffer = convertBase64ToBuffer(base64);
|
|
23
|
+
return decompressBuffer(buffer);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
exports.decompressFromBase64 = decompressFromBase64;
|
|
27
|
+
function compressObjectIntoBuffer(obj) {
|
|
28
|
+
const json = obj === undefined ? JSON.stringify(null) : JSON.stringify(obj);
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
zlib_1.default.gzip(json, (err, buffer) => {
|
|
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');
|
|
45
|
+
}
|
|
46
|
+
exports.convertBase64ToBuffer = convertBase64ToBuffer;
|
|
47
|
+
function decompressBuffer(buffer) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
zlib_1.default.gunzip(buffer, (err, result) => {
|
|
50
|
+
if (err)
|
|
51
|
+
reject(err);
|
|
52
|
+
else
|
|
53
|
+
resolve(JSON.parse(result.toString()));
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
exports.decompressBuffer = decompressBuffer;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './compression';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./compression"), exports);
|
package/lib/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
1
|
+
export * from './app-util/app-util';
|
|
2
|
+
export * from './compression-util';
|
|
3
3
|
export * from './date-util/date-util';
|
|
4
4
|
export * from './map-util/map-util';
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './timer-util/timer-util';
|
|
7
|
-
export * from './promise-util/promise-util';
|
|
5
|
+
export * from './object-util/object-util';
|
|
8
6
|
export * from './order-util/order-util';
|
|
7
|
+
export * from './promise-util/promise-util';
|
|
9
8
|
export * from './retry-util/retry-util';
|
|
9
|
+
export * from './string-util/string-util';
|
|
10
|
+
export * from './timer-util/timer-util';
|
package/lib/index.js
CHANGED
|
@@ -14,12 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
18
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./app-util/app-util"), exports);
|
|
18
|
+
__exportStar(require("./compression-util"), exports);
|
|
19
19
|
__exportStar(require("./date-util/date-util"), exports);
|
|
20
20
|
__exportStar(require("./map-util/map-util"), exports);
|
|
21
|
-
__exportStar(require("./
|
|
22
|
-
__exportStar(require("./timer-util/timer-util"), exports);
|
|
23
|
-
__exportStar(require("./promise-util/promise-util"), exports);
|
|
21
|
+
__exportStar(require("./object-util/object-util"), exports);
|
|
24
22
|
__exportStar(require("./order-util/order-util"), exports);
|
|
23
|
+
__exportStar(require("./promise-util/promise-util"), exports);
|
|
25
24
|
__exportStar(require("./retry-util/retry-util"), exports);
|
|
25
|
+
__exportStar(require("./string-util/string-util"), exports);
|
|
26
|
+
__exportStar(require("./timer-util/timer-util"), exports);
|
|
@@ -2,6 +2,10 @@ export declare enum TimeUnit {
|
|
|
2
2
|
MILLISECOND = "ms",
|
|
3
3
|
SECOND = "s"
|
|
4
4
|
}
|
|
5
|
+
interface DumpOptions {
|
|
6
|
+
unit?: TimeUnit;
|
|
7
|
+
format?: boolean;
|
|
8
|
+
}
|
|
5
9
|
export declare class Timer {
|
|
6
10
|
private startTime?;
|
|
7
11
|
private endTime?;
|
|
@@ -13,4 +17,16 @@ export declare class Timer {
|
|
|
13
17
|
getElapsedTime(timeUnit?: TimeUnit): number;
|
|
14
18
|
private getCurrentTime;
|
|
15
19
|
}
|
|
20
|
+
export declare class TimerManager {
|
|
21
|
+
private timers;
|
|
22
|
+
private formatDuration;
|
|
23
|
+
start(key: string): void;
|
|
24
|
+
stop(key: string): void;
|
|
25
|
+
reset(key: string): void;
|
|
26
|
+
getElapsedTime(key: string, unit?: TimeUnit): number;
|
|
27
|
+
has(key: string): boolean;
|
|
28
|
+
clearAll(): void;
|
|
29
|
+
dump(options?: DumpOptions): Record<string, number | string>;
|
|
30
|
+
}
|
|
16
31
|
export declare function delay(ms: number): Promise<unknown>;
|
|
32
|
+
export {};
|
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.delay = exports.Timer = exports.TimeUnit = void 0;
|
|
12
|
+
exports.delay = exports.TimerManager = exports.Timer = exports.TimeUnit = void 0;
|
|
13
13
|
var TimeUnit;
|
|
14
14
|
(function (TimeUnit) {
|
|
15
15
|
TimeUnit["MILLISECOND"] = "ms";
|
|
@@ -55,6 +55,60 @@ class Timer {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
exports.Timer = Timer;
|
|
58
|
+
class TimerManager {
|
|
59
|
+
constructor() {
|
|
60
|
+
this.timers = new Map();
|
|
61
|
+
}
|
|
62
|
+
formatDuration(duration) {
|
|
63
|
+
return duration > 1000 ? `${(duration / 1000).toFixed(2)}s` : `${duration}ms`;
|
|
64
|
+
}
|
|
65
|
+
start(key) {
|
|
66
|
+
if (this.timers.has(key)) {
|
|
67
|
+
throw new Error(`Timer with key "${key}" already started`);
|
|
68
|
+
}
|
|
69
|
+
const timer = new Timer();
|
|
70
|
+
timer.start();
|
|
71
|
+
this.timers.set(key, timer);
|
|
72
|
+
}
|
|
73
|
+
stop(key) {
|
|
74
|
+
const timer = this.timers.get(key);
|
|
75
|
+
if (!timer) {
|
|
76
|
+
throw new Error(`Timer with key "${key}" not found`);
|
|
77
|
+
}
|
|
78
|
+
timer.stop();
|
|
79
|
+
}
|
|
80
|
+
reset(key) {
|
|
81
|
+
this.timers.delete(key);
|
|
82
|
+
}
|
|
83
|
+
getElapsedTime(key, unit = TimeUnit.MILLISECOND) {
|
|
84
|
+
const timer = this.timers.get(key);
|
|
85
|
+
if (!timer) {
|
|
86
|
+
throw new Error(`Timer with key "${key}" not found`);
|
|
87
|
+
}
|
|
88
|
+
return timer.getElapsedTime(unit);
|
|
89
|
+
}
|
|
90
|
+
has(key) {
|
|
91
|
+
return this.timers.has(key);
|
|
92
|
+
}
|
|
93
|
+
clearAll() {
|
|
94
|
+
this.timers.clear();
|
|
95
|
+
}
|
|
96
|
+
dump(options = {}) {
|
|
97
|
+
const { unit = TimeUnit.MILLISECOND, format = false } = options;
|
|
98
|
+
const result = {};
|
|
99
|
+
for (const [key, timer] of this.timers.entries()) {
|
|
100
|
+
try {
|
|
101
|
+
const duration = timer.getElapsedTime(unit);
|
|
102
|
+
result[key] = format ? this.formatDuration(duration) : duration;
|
|
103
|
+
}
|
|
104
|
+
catch (_a) {
|
|
105
|
+
result[key] = NaN;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.TimerManager = TimerManager;
|
|
58
112
|
function delay(ms) {
|
|
59
113
|
return __awaiter(this, void 0, void 0, function* () {
|
|
60
114
|
return new Promise((resolve) => {
|