@powfix/core-js 0.13.38 → 0.14.0
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/src/services/time/TimeService.d.ts +15 -19
- package/dist/src/services/time/TimeService.js +42 -84
- package/dist/src/utils/nodejs/index.d.ts +1 -1
- package/dist/src/utils/nodejs/index.js +3 -1
- package/dist/src/utils/nodejs/sequelize-utils/SequelizeUtils.d.ts +9 -0
- package/dist/src/utils/nodejs/{SequelizeUtils.js → sequelize-utils/SequelizeUtils.js} +6 -12
- package/dist/src/utils/nodejs/sequelize-utils/index.d.ts +1 -0
- package/dist/src/utils/nodejs/sequelize-utils/index.js +5 -0
- package/dist/src/utils/nodejs/sequelize-utils/types.d.ts +16 -0
- package/dist/src/utils/nodejs/sequelize-utils/types.js +2 -0
- package/package.json +3 -2
- package/dist/src/utils/nodejs/SequelizeUtils.d.ts +0 -23
|
@@ -1,39 +1,36 @@
|
|
|
1
|
-
import
|
|
1
|
+
import EventEmitter from 'eventemitter3';
|
|
2
2
|
export declare enum TimeServiceStatus {
|
|
3
3
|
STOPPED = 0,
|
|
4
4
|
RUNNING = 1
|
|
5
5
|
}
|
|
6
|
-
export declare class TimeService {
|
|
6
|
+
export declare class TimeService extends EventEmitter<TimeService.Event> {
|
|
7
7
|
private static readonly DEFAULT_SYNC_INTERVAL;
|
|
8
|
-
|
|
8
|
+
private static DEFAULT_CLIENT_TIME_PROVIDER;
|
|
9
|
+
private syncHandler?;
|
|
10
|
+
private syncInterval?;
|
|
11
|
+
private clientTimeProvider?;
|
|
12
|
+
private serverTimeProvider?;
|
|
9
13
|
private offset?;
|
|
10
|
-
private option;
|
|
11
14
|
private syncedAt?;
|
|
12
|
-
private emitter;
|
|
13
|
-
readonly on: <T extends TimeService.Event>(event: T, fn: (...args: any[]) => void, context?: any) => EventEmitter3<TimeService.Event, any>;
|
|
14
|
-
readonly off: <T extends TimeService.Event>(event: T, fn?: ((...args: any[]) => void) | undefined, context?: any, once?: boolean) => EventEmitter3<TimeService.Event, any>;
|
|
15
|
-
private readonly emit;
|
|
16
15
|
static calculateNTPResultOffset(ntpResult: TimeService.NTPResult): TimeService.Offset;
|
|
17
16
|
constructor(option: TimeService.Option);
|
|
18
|
-
getOption(): TimeService.Option;
|
|
19
|
-
setOption(option: TimeService.Option): TimeService.Option;
|
|
20
17
|
getOffset(defaultValue: TimeService.Offset): TimeService.Offset;
|
|
21
18
|
getOffset(): TimeService.Offset | undefined;
|
|
22
19
|
setOffset(offset: TimeService.Offset): TimeService.Offset;
|
|
23
20
|
setOffset(offset: TimeService.Offset | undefined): TimeService.Offset;
|
|
24
21
|
getSyncedAt(): TimeService.TimeStamp | undefined;
|
|
25
22
|
private setSyncedAt;
|
|
26
|
-
getSyncInterval(): number
|
|
23
|
+
getSyncInterval(): number;
|
|
27
24
|
setSyncInterval(interval: TimeService.Option['syncInterval']): void;
|
|
25
|
+
getClientTimeProvider(): TimeService.ClientTimeProvider;
|
|
26
|
+
setClientTimeProvider(provider: TimeService.ClientTimeProvider): void;
|
|
28
27
|
getClientTime(defaultValue?: TimeService.TimeStamp): TimeService.TimeStamp;
|
|
28
|
+
getServerTimeProvider(): TimeService.ServerTimeProvider | null | undefined;
|
|
29
|
+
setServerTimeProvider(provider: TimeService.ServerTimeProvider): void;
|
|
29
30
|
getServerTime(): TimeService.TimeStamp | null;
|
|
30
31
|
getTime(): number;
|
|
31
32
|
private fetchServerNTPResult;
|
|
32
|
-
getStatus(): TimeServiceStatus;
|
|
33
|
-
start(): void;
|
|
34
|
-
stop(): void;
|
|
35
33
|
sync(): Promise<TimeService.Offset | null>;
|
|
36
|
-
private syncHandler?;
|
|
37
34
|
private startSync;
|
|
38
35
|
private stopSync;
|
|
39
36
|
}
|
|
@@ -52,9 +49,8 @@ export declare namespace TimeService {
|
|
|
52
49
|
type ClientTimeProvider = () => TimeStamp;
|
|
53
50
|
type ServerTimeProvider = (t1: NTPResult['t1']) => (ServerNTPResult | null) | (Promise<ServerNTPResult | null>);
|
|
54
51
|
interface Option {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
serverTimeProvider?: ServerTimeProvider | undefined;
|
|
52
|
+
syncInterval?: number;
|
|
53
|
+
clientTimeProvider?: ClientTimeProvider;
|
|
54
|
+
serverTimeProvider?: ServerTimeProvider;
|
|
59
55
|
}
|
|
60
56
|
}
|
|
@@ -20,34 +20,24 @@ var TimeServiceStatus;
|
|
|
20
20
|
TimeServiceStatus[TimeServiceStatus["STOPPED"] = 0] = "STOPPED";
|
|
21
21
|
TimeServiceStatus[TimeServiceStatus["RUNNING"] = 1] = "RUNNING";
|
|
22
22
|
})(TimeServiceStatus || (exports.TimeServiceStatus = TimeServiceStatus = {}));
|
|
23
|
-
class TimeService {
|
|
23
|
+
class TimeService extends eventemitter3_1.default {
|
|
24
|
+
static DEFAULT_CLIENT_TIME_PROVIDER() {
|
|
25
|
+
return Date.now();
|
|
26
|
+
}
|
|
24
27
|
static calculateNTPResultOffset(ntpResult) {
|
|
25
28
|
const { t1, t2, t3, t4 } = ntpResult;
|
|
26
29
|
return ((t2 - t1) + (t3 - t4)) / 2;
|
|
27
30
|
}
|
|
28
31
|
constructor(option) {
|
|
29
|
-
|
|
30
|
-
//
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
33
|
-
this.
|
|
34
|
-
this.emit = this.emitter.emit.bind(this.emitter);
|
|
35
|
-
this.option = option;
|
|
36
|
-
if (option.autoStart) {
|
|
37
|
-
this.start();
|
|
38
|
-
}
|
|
32
|
+
super();
|
|
33
|
+
// Options
|
|
34
|
+
this.syncInterval = option.syncInterval;
|
|
35
|
+
this.clientTimeProvider = option.clientTimeProvider;
|
|
36
|
+
this.serverTimeProvider = option.serverTimeProvider;
|
|
39
37
|
// Bind
|
|
40
38
|
this.sync = this.sync.bind(this);
|
|
41
|
-
this.start = this.start.bind(this);
|
|
42
|
-
this.stop = this.stop.bind(this);
|
|
43
39
|
this.fetchServerNTPResult = this.fetchServerNTPResult.bind(this);
|
|
44
40
|
}
|
|
45
|
-
getOption() {
|
|
46
|
-
return this.option;
|
|
47
|
-
}
|
|
48
|
-
setOption(option) {
|
|
49
|
-
return this.option = option;
|
|
50
|
-
}
|
|
51
41
|
getOffset(defaultValue) {
|
|
52
42
|
if (this.offset !== undefined) {
|
|
53
43
|
return this.offset;
|
|
@@ -70,41 +60,37 @@ class TimeService {
|
|
|
70
60
|
return syncedAt;
|
|
71
61
|
}
|
|
72
62
|
getSyncInterval() {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return TimeService.DEFAULT_SYNC_INTERVAL;
|
|
76
|
-
}
|
|
77
|
-
if (this.option.syncInterval === null || this.option.syncInterval === -1) {
|
|
78
|
-
// If option is null, do not sync automatically
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
return this.option.syncInterval;
|
|
63
|
+
var _a;
|
|
64
|
+
return (_a = this.syncInterval) !== null && _a !== void 0 ? _a : TimeService.DEFAULT_SYNC_INTERVAL;
|
|
82
65
|
}
|
|
83
66
|
setSyncInterval(interval) {
|
|
84
|
-
this.
|
|
67
|
+
this.syncInterval = interval;
|
|
85
68
|
// Emit
|
|
86
69
|
this.emit('SYNC_INTERVAL_CHANGED', interval);
|
|
87
|
-
if (this.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
this.startSync();
|
|
91
|
-
}
|
|
70
|
+
if (this.syncHandler !== undefined) {
|
|
71
|
+
this.stopSync();
|
|
72
|
+
this.startSync();
|
|
92
73
|
}
|
|
93
74
|
}
|
|
75
|
+
getClientTimeProvider() {
|
|
76
|
+
var _a;
|
|
77
|
+
return (_a = this.clientTimeProvider) !== null && _a !== void 0 ? _a : TimeService.DEFAULT_CLIENT_TIME_PROVIDER;
|
|
78
|
+
}
|
|
79
|
+
setClientTimeProvider(provider) {
|
|
80
|
+
this.clientTimeProvider = provider;
|
|
81
|
+
}
|
|
94
82
|
getClientTime(defaultValue = Date.now()) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
return defaultValue;
|
|
83
|
+
return this.getClientTimeProvider()();
|
|
84
|
+
}
|
|
85
|
+
getServerTimeProvider() {
|
|
86
|
+
return this.serverTimeProvider;
|
|
87
|
+
}
|
|
88
|
+
setServerTimeProvider(provider) {
|
|
89
|
+
this.serverTimeProvider = provider;
|
|
104
90
|
}
|
|
105
91
|
getServerTime() {
|
|
106
92
|
const offset = this.getOffset();
|
|
107
|
-
if (offset
|
|
93
|
+
if (offset == null) {
|
|
108
94
|
return null;
|
|
109
95
|
}
|
|
110
96
|
const clientTime = this.getClientTime();
|
|
@@ -116,8 +102,9 @@ class TimeService {
|
|
|
116
102
|
fetchServerNTPResult(t1) {
|
|
117
103
|
return __awaiter(this, void 0, void 0, function* () {
|
|
118
104
|
try {
|
|
119
|
-
|
|
120
|
-
|
|
105
|
+
const provider = this.getServerTimeProvider();
|
|
106
|
+
if (typeof provider === 'function') {
|
|
107
|
+
return yield provider(t1);
|
|
121
108
|
}
|
|
122
109
|
}
|
|
123
110
|
catch (e) {
|
|
@@ -126,35 +113,6 @@ class TimeService {
|
|
|
126
113
|
return null;
|
|
127
114
|
});
|
|
128
115
|
}
|
|
129
|
-
getStatus() {
|
|
130
|
-
return this.status;
|
|
131
|
-
}
|
|
132
|
-
start() {
|
|
133
|
-
if (this.status !== TimeServiceStatus.STOPPED) {
|
|
134
|
-
console.warn(LOG_TAG, 'service is not stopped');
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
// Change status
|
|
138
|
-
this.status = TimeServiceStatus.RUNNING;
|
|
139
|
-
// Sync immediately
|
|
140
|
-
this.sync().finally(() => { });
|
|
141
|
-
// Start sync
|
|
142
|
-
this.startSync();
|
|
143
|
-
}
|
|
144
|
-
stop() {
|
|
145
|
-
if (this.status !== TimeServiceStatus.RUNNING) {
|
|
146
|
-
console.warn(LOG_TAG, 'service is not running');
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
// Change status
|
|
150
|
-
this.status = TimeServiceStatus.RUNNING;
|
|
151
|
-
// Stop sync
|
|
152
|
-
this.stopSync();
|
|
153
|
-
// Reset offset
|
|
154
|
-
this.setOffset(undefined);
|
|
155
|
-
// Reset synced at
|
|
156
|
-
this.setSyncedAt(undefined);
|
|
157
|
-
}
|
|
158
116
|
sync() {
|
|
159
117
|
return __awaiter(this, void 0, void 0, function* () {
|
|
160
118
|
try {
|
|
@@ -205,25 +163,25 @@ class TimeService {
|
|
|
205
163
|
});
|
|
206
164
|
}
|
|
207
165
|
startSync() {
|
|
208
|
-
if (this.syncHandler
|
|
209
|
-
console.warn('sync
|
|
166
|
+
if (this.syncHandler != null) {
|
|
167
|
+
console.warn('sync is already started');
|
|
210
168
|
return;
|
|
211
169
|
}
|
|
212
170
|
const syncInterval = this.getSyncInterval();
|
|
213
|
-
if (syncInterval
|
|
171
|
+
if (syncInterval == null || syncInterval <= 0) {
|
|
172
|
+
console.warn('sync is not started', 'syncInterval', syncInterval);
|
|
214
173
|
return;
|
|
215
174
|
}
|
|
216
|
-
this.syncHandler = setInterval(this.sync, syncInterval);
|
|
175
|
+
this.syncHandler = setInterval(this.sync.bind(this), syncInterval);
|
|
217
176
|
}
|
|
218
177
|
;
|
|
219
178
|
stopSync() {
|
|
220
|
-
if (this.syncHandler
|
|
221
|
-
|
|
179
|
+
if (this.syncHandler != null) {
|
|
180
|
+
clearInterval(this.syncHandler);
|
|
181
|
+
this.syncHandler = undefined;
|
|
222
182
|
}
|
|
223
|
-
clearInterval(this.syncHandler);
|
|
224
|
-
this.syncHandler = undefined;
|
|
225
183
|
}
|
|
226
184
|
;
|
|
227
185
|
}
|
|
228
186
|
exports.TimeService = TimeService;
|
|
229
|
-
TimeService.DEFAULT_SYNC_INTERVAL =
|
|
187
|
+
TimeService.DEFAULT_SYNC_INTERVAL = 60 * 1000;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from './HookUtils';
|
|
2
|
-
export
|
|
2
|
+
export { SequelizeUtils } from './sequelize-utils';
|
|
@@ -14,5 +14,7 @@ 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
|
+
exports.SequelizeUtils = void 0;
|
|
17
18
|
__exportStar(require("./HookUtils"), exports);
|
|
18
|
-
|
|
19
|
+
var sequelize_utils_1 = require("./sequelize-utils");
|
|
20
|
+
Object.defineProperty(exports, "SequelizeUtils", { enumerable: true, get: function () { return sequelize_utils_1.SequelizeUtils; } });
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ModelAttributeColumnOptions, WhereOptions } from "sequelize";
|
|
2
|
+
import { Model, NonKnownUuidStringKeys, UuidColumnOptionsBase, UuidColumnOptionsForModel } from "./types";
|
|
3
|
+
export declare class SequelizeUtils {
|
|
4
|
+
static decimal2Number(value: any): number | null | undefined;
|
|
5
|
+
static buildPrimaryUuidColumn: () => Partial<ModelAttributeColumnOptions>;
|
|
6
|
+
static buildUuidColumn<T extends Model, AdditionalKeys extends NonKnownUuidStringKeys<T> = never>(options: UuidColumnOptionsForModel<T, AdditionalKeys>): Partial<ModelAttributeColumnOptions>;
|
|
7
|
+
static buildUuidColumn(options: UuidColumnOptionsBase): Partial<ModelAttributeColumnOptions>;
|
|
8
|
+
static getNullableArrayFilter<T = undefined>(arr: (null | any)[]): WhereOptions<T>;
|
|
9
|
+
}
|
|
@@ -13,8 +13,8 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
14
|
exports.SequelizeUtils = void 0;
|
|
15
15
|
const sequelize_1 = require("sequelize");
|
|
16
|
-
const UuidUtils_1 = require("
|
|
17
|
-
const constants_1 = require("
|
|
16
|
+
const UuidUtils_1 = require("../../UuidUtils");
|
|
17
|
+
const constants_1 = require("../../../constants");
|
|
18
18
|
class SequelizeUtils {
|
|
19
19
|
static decimal2Number(value) {
|
|
20
20
|
if (value === null || value === undefined) {
|
|
@@ -26,7 +26,7 @@ class SequelizeUtils {
|
|
|
26
26
|
}
|
|
27
27
|
return parsed;
|
|
28
28
|
}
|
|
29
|
-
static
|
|
29
|
+
static buildUuidColumn(options) {
|
|
30
30
|
const { columnName } = options, overrideOptions = __rest(options, ["columnName"]);
|
|
31
31
|
if (overrideOptions.allowNull) {
|
|
32
32
|
return Object.assign({ type: "binary(16)", get() {
|
|
@@ -66,16 +66,10 @@ class SequelizeUtils {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
exports.SequelizeUtils = SequelizeUtils;
|
|
69
|
-
SequelizeUtils.
|
|
70
|
-
|
|
69
|
+
SequelizeUtils.buildPrimaryUuidColumn = () => (Object.assign({}, SequelizeUtils.buildUuidColumn({
|
|
70
|
+
columnName: "uuid",
|
|
71
71
|
allowNull: false,
|
|
72
72
|
primaryKey: true,
|
|
73
73
|
unique: true,
|
|
74
74
|
defaultValue: () => UuidUtils_1.UuidUtils.toBuffer(UuidUtils_1.UuidUtils.v4()),
|
|
75
|
-
|
|
76
|
-
return UuidUtils_1.UuidUtils.toString(this.getDataValue("uuid"));
|
|
77
|
-
},
|
|
78
|
-
set(uuid) {
|
|
79
|
-
this.setDataValue("uuid", UuidUtils_1.UuidUtils.toBuffer(uuid));
|
|
80
|
-
}
|
|
81
|
-
});
|
|
75
|
+
})));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SequelizeUtils } from './SequelizeUtils';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SequelizeUtils = void 0;
|
|
4
|
+
var SequelizeUtils_1 = require("./SequelizeUtils");
|
|
5
|
+
Object.defineProperty(exports, "SequelizeUtils", { enumerable: true, get: function () { return SequelizeUtils_1.SequelizeUtils; } });
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ModelAttributeColumnOptions } from "sequelize";
|
|
2
|
+
export type Model = Record<string, any> | undefined;
|
|
3
|
+
export type ExtractUuidKeys<T extends Model> = {
|
|
4
|
+
[Property in keyof T]: Property extends `${string}Uuid` | 'uuid' ? Property : never;
|
|
5
|
+
}[keyof T];
|
|
6
|
+
export type ExtractStringKeys<T extends Model> = {
|
|
7
|
+
[Property in keyof T]: string extends T[Property] ? Property : never;
|
|
8
|
+
}[keyof T];
|
|
9
|
+
export type ConcreteUuidKeys<T extends Model> = ExtractUuidKeys<T>;
|
|
10
|
+
export type NonKnownUuidStringKeys<T extends Model> = Exclude<ExtractStringKeys<T>, ConcreteUuidKeys<T>> & string;
|
|
11
|
+
export interface UuidColumnOptionsBase extends Omit<ModelAttributeColumnOptions, 'type'> {
|
|
12
|
+
columnName: string;
|
|
13
|
+
}
|
|
14
|
+
export interface UuidColumnOptionsForModel<T extends Model, AdditionalKeys extends string> extends Omit<ModelAttributeColumnOptions, 'type'> {
|
|
15
|
+
columnName: ConcreteUuidKeys<T> | AdditionalKeys;
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powfix/core-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "core package",
|
|
5
5
|
"author": "Kwon Kyung-Min <powfix@gmail.com>",
|
|
6
6
|
"private": false,
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
"typescript": "5.8.3"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"moment": ">=2.0.0"
|
|
46
|
+
"moment": ">=2.0.0",
|
|
47
|
+
"eventemitter3": ">=5"
|
|
47
48
|
}
|
|
48
49
|
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { ModelAttributeColumnOptions, WhereOptions } from "sequelize";
|
|
2
|
-
type Model = Record<string, any> | undefined;
|
|
3
|
-
type GetIncludeKeyPatterns<T extends Model> = T extends {
|
|
4
|
-
_id: string;
|
|
5
|
-
} ? `${string}Uuid` | 'uuid' : `${string}Uuid`;
|
|
6
|
-
type ExtractUuidKeys<T extends Model> = {
|
|
7
|
-
[Property in keyof T]: Property extends GetIncludeKeyPatterns<T> ? Property : never;
|
|
8
|
-
}[keyof T];
|
|
9
|
-
type ConcreteUuidKeys<T extends Record<string, any>> = ExtractUuidKeys<T>;
|
|
10
|
-
interface UuidColumnOptionsBase extends Omit<ModelAttributeColumnOptions, 'type'> {
|
|
11
|
-
columnName: string;
|
|
12
|
-
}
|
|
13
|
-
interface UuidColumnOptionsForModel<T extends Record<string, any>> extends Omit<ModelAttributeColumnOptions, 'type'> {
|
|
14
|
-
columnName: ConcreteUuidKeys<T>;
|
|
15
|
-
}
|
|
16
|
-
export declare class SequelizeUtils {
|
|
17
|
-
static decimal2Number(value: any): number | null | undefined;
|
|
18
|
-
static getPrimaryUuidColumn: () => Partial<ModelAttributeColumnOptions>;
|
|
19
|
-
static getUuidColumn<T extends Record<string, any>>(options: UuidColumnOptionsForModel<T>): Partial<ModelAttributeColumnOptions>;
|
|
20
|
-
static getUuidColumn(options: UuidColumnOptionsBase): Partial<ModelAttributeColumnOptions>;
|
|
21
|
-
static getNullableArrayFilter<T = undefined>(arr: (null | any)[]): WhereOptions<T>;
|
|
22
|
-
}
|
|
23
|
-
export {};
|