@powfix/core-js 0.9.22

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.
Files changed (101) hide show
  1. package/browser.ts +73 -0
  2. package/dist/browser.d.ts +32 -0
  3. package/dist/browser.js +58 -0
  4. package/dist/index.d.ts +2 -0
  5. package/dist/index.js +20 -0
  6. package/dist/src/constants/COORDINATE.d.ts +7 -0
  7. package/dist/src/constants/COORDINATE.js +10 -0
  8. package/dist/src/constants/DISTANCE.d.ts +13 -0
  9. package/dist/src/constants/DISTANCE.js +18 -0
  10. package/dist/src/constants/DURATION.d.ts +16 -0
  11. package/dist/src/constants/DURATION.js +21 -0
  12. package/dist/src/interfaces/Coordinate.d.ts +8 -0
  13. package/dist/src/interfaces/Coordinate.js +2 -0
  14. package/dist/src/interfaces/Point2.d.ts +4 -0
  15. package/dist/src/interfaces/Point2.js +2 -0
  16. package/dist/src/interfaces/Point3.d.ts +4 -0
  17. package/dist/src/interfaces/Point3.js +2 -0
  18. package/dist/src/scripts/base64-polyfill.d.ts +1 -0
  19. package/dist/src/scripts/base64-polyfill.js +14 -0
  20. package/dist/src/services/Session.d.ts +27 -0
  21. package/dist/src/services/Session.js +140 -0
  22. package/dist/src/services/browser.d.ts +2 -0
  23. package/dist/src/services/browser.js +20 -0
  24. package/dist/src/services/index.d.ts +2 -0
  25. package/dist/src/services/index.js +19 -0
  26. package/dist/src/services/redis/RedisClient.d.ts +20 -0
  27. package/dist/src/services/redis/RedisClient.js +70 -0
  28. package/dist/src/services/redis/RedisPublisher.d.ts +13 -0
  29. package/dist/src/services/redis/RedisPublisher.js +61 -0
  30. package/dist/src/services/redis/RedisSubscriber.d.ts +11 -0
  31. package/dist/src/services/redis/RedisSubscriber.js +68 -0
  32. package/dist/src/services/redis/index.d.ts +3 -0
  33. package/dist/src/services/redis/index.js +20 -0
  34. package/dist/src/services/time/TimeService.d.ts +64 -0
  35. package/dist/src/services/time/TimeService.js +235 -0
  36. package/dist/src/services/time/index.d.ts +1 -0
  37. package/dist/src/services/time/index.js +17 -0
  38. package/dist/src/types/IntRage.d.ts +3 -0
  39. package/dist/src/types/IntRage.js +2 -0
  40. package/dist/src/utils/ArrayUtils.d.ts +10 -0
  41. package/dist/src/utils/ArrayUtils.js +20 -0
  42. package/dist/src/utils/BooleanUtils.d.ts +1 -0
  43. package/dist/src/utils/BooleanUtils.js +9 -0
  44. package/dist/src/utils/CoordinateUtils.d.ts +8 -0
  45. package/dist/src/utils/CoordinateUtils.js +42 -0
  46. package/dist/src/utils/DateUtils.d.ts +12 -0
  47. package/dist/src/utils/DateUtils.js +212 -0
  48. package/dist/src/utils/JuminNumberUtils.d.ts +4 -0
  49. package/dist/src/utils/JuminNumberUtils.js +50 -0
  50. package/dist/src/utils/NumberUtils.d.ts +4 -0
  51. package/dist/src/utils/NumberUtils.js +25 -0
  52. package/dist/src/utils/Point3Utils.d.ts +4 -0
  53. package/dist/src/utils/Point3Utils.js +12 -0
  54. package/dist/src/utils/RandomUtils.d.ts +8 -0
  55. package/dist/src/utils/RandomUtils.js +64 -0
  56. package/dist/src/utils/Sequencer.d.ts +39 -0
  57. package/dist/src/utils/Sequencer.js +148 -0
  58. package/dist/src/utils/StringUtils.d.ts +5 -0
  59. package/dist/src/utils/StringUtils.js +37 -0
  60. package/dist/src/utils/UuidUtils.d.ts +14 -0
  61. package/dist/src/utils/UuidUtils.js +49 -0
  62. package/dist/src/utils/Validator.d.ts +48 -0
  63. package/dist/src/utils/Validator.js +118 -0
  64. package/dist/src/utils/global/between.d.ts +1 -0
  65. package/dist/src/utils/global/between.js +7 -0
  66. package/dist/src/utils/global/sleep.d.ts +1 -0
  67. package/dist/src/utils/global/sleep.js +21 -0
  68. package/index.ts +5 -0
  69. package/package.json +42 -0
  70. package/src/constants/COORDINATE.ts +9 -0
  71. package/src/constants/DISTANCE.ts +15 -0
  72. package/src/constants/DURATION.ts +18 -0
  73. package/src/interfaces/Coordinate.ts +9 -0
  74. package/src/interfaces/Point2.ts +4 -0
  75. package/src/interfaces/Point3.ts +5 -0
  76. package/src/scripts/base64-polyfill.ts +13 -0
  77. package/src/services/Session.ts +141 -0
  78. package/src/services/browser.ts +5 -0
  79. package/src/services/index.ts +4 -0
  80. package/src/services/redis/RedisClient.ts +79 -0
  81. package/src/services/redis/RedisPublisher.ts +48 -0
  82. package/src/services/redis/RedisSubscriber.ts +49 -0
  83. package/src/services/redis/index.ts +4 -0
  84. package/src/services/time/TimeService.ts +304 -0
  85. package/src/services/time/index.ts +1 -0
  86. package/src/types/IntRage.ts +5 -0
  87. package/src/utils/ArrayUtils.ts +25 -0
  88. package/src/utils/BooleanUtils.ts +4 -0
  89. package/src/utils/CoordinateUtils.ts +50 -0
  90. package/src/utils/DateUtils.ts +213 -0
  91. package/src/utils/JuminNumberUtils.ts +47 -0
  92. package/src/utils/NumberUtils.ts +23 -0
  93. package/src/utils/Point3Utils.ts +11 -0
  94. package/src/utils/RandomUtils.ts +62 -0
  95. package/src/utils/Sequencer.ts +178 -0
  96. package/src/utils/StringUtils.ts +43 -0
  97. package/src/utils/UuidUtils.ts +46 -0
  98. package/src/utils/Validator.ts +162 -0
  99. package/src/utils/global/between.ts +3 -0
  100. package/src/utils/global/sleep.ts +6 -0
  101. package/tsconfig.json +103 -0
@@ -0,0 +1,13 @@
1
+ import { RedisClient } from "./RedisClient";
2
+ export declare class RedisPublisher extends RedisClient {
3
+ private logging;
4
+ constructor(options?: RedisClient.RedisClientOptions);
5
+ start(): Promise<RedisClient.Status>;
6
+ stop(): Promise<RedisClient.Status>;
7
+ setLogging(logging: RedisPublisher.LOGGING): void;
8
+ getLogging(): RedisPublisher.LOGGING;
9
+ publish: (channel: string, data: string | object) => Promise<void>;
10
+ }
11
+ export declare namespace RedisPublisher {
12
+ type LOGGING = 'none' | 'length' | 'data';
13
+ }
@@ -0,0 +1,61 @@
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.RedisPublisher = void 0;
13
+ const RedisClient_1 = require("./RedisClient");
14
+ class RedisPublisher extends RedisClient_1.RedisClient {
15
+ constructor(options) {
16
+ super(options);
17
+ this.logging = 'length';
18
+ // Make public method
19
+ this.publish = (channel, data) => __awaiter(this, void 0, void 0, function* () {
20
+ const stringifyData = typeof data !== 'string' ? JSON.stringify(data) : data;
21
+ switch (this.logging) {
22
+ case "none": {
23
+ break;
24
+ }
25
+ case "length": {
26
+ console.log(Date.now(), 'Server ---> Redis', channel, stringifyData.length);
27
+ break;
28
+ }
29
+ case "data": {
30
+ console.log(Date.now(), 'Server ---> Redis', channel, stringifyData);
31
+ break;
32
+ }
33
+ }
34
+ yield this.client.publish(channel, stringifyData);
35
+ });
36
+ console.log(Date.now(), "RedisPublisher", 'initialized');
37
+ }
38
+ start() {
39
+ const _super = Object.create(null, {
40
+ start: { get: () => super.start }
41
+ });
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ return yield _super.start.call(this);
44
+ });
45
+ }
46
+ stop() {
47
+ const _super = Object.create(null, {
48
+ stop: { get: () => super.stop }
49
+ });
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ return yield _super.stop.call(this);
52
+ });
53
+ }
54
+ setLogging(logging) {
55
+ this.logging = logging;
56
+ }
57
+ getLogging() {
58
+ return this.logging;
59
+ }
60
+ }
61
+ exports.RedisPublisher = RedisPublisher;
@@ -0,0 +1,11 @@
1
+ import { RedisClient } from "./RedisClient";
2
+ import { PubSubListener } from "@redis/client/dist/lib/client/pub-sub";
3
+ export declare class RedisSubscriber extends RedisClient {
4
+ constructor(options?: RedisClient.RedisClientOptions);
5
+ subscribe: <T extends boolean = false>(channels: string | string[], listener: PubSubListener<T>, bufferMode?: T | undefined) => Promise<void>;
6
+ unsubscribe: <T extends boolean = false>(channels: string | string[], listener?: PubSubListener<T> | undefined, bufferMode?: T | undefined) => Promise<void>;
7
+ start(): Promise<RedisClient.Status>;
8
+ stop(): Promise<RedisClient.Status>;
9
+ protected registerListeners(): Promise<void>;
10
+ protected unregisterListeners(): Promise<void>;
11
+ }
@@ -0,0 +1,68 @@
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.RedisSubscriber = void 0;
13
+ const RedisClient_1 = require("./RedisClient");
14
+ class RedisSubscriber extends RedisClient_1.RedisClient {
15
+ constructor(options) {
16
+ super(options);
17
+ this.subscribe = (channels, listener, bufferMode) => __awaiter(this, void 0, void 0, function* () {
18
+ for (const channel of Array.isArray(channels) ? channels : [channels]) {
19
+ if ((/\*/g).test(channel)) {
20
+ yield this.client.pSubscribe(channel, listener, bufferMode);
21
+ }
22
+ else {
23
+ yield this.client.subscribe(channel, listener, bufferMode);
24
+ }
25
+ }
26
+ });
27
+ this.unsubscribe = (channels, listener, bufferMode) => __awaiter(this, void 0, void 0, function* () {
28
+ for (const channel of Array.isArray(channels) ? channels : [channels]) {
29
+ if ((/\*/g).test(channel)) {
30
+ yield this.client.pUnsubscribe(channel, listener, bufferMode);
31
+ }
32
+ else {
33
+ yield this.client.unsubscribe(channel, listener, bufferMode);
34
+ }
35
+ }
36
+ });
37
+ console.log(Date.now(), 'Subscriber', 'initialized');
38
+ }
39
+ start() {
40
+ const _super = Object.create(null, {
41
+ start: { get: () => super.start }
42
+ });
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ const status = yield _super.start.call(this);
45
+ yield this.registerListeners();
46
+ return status;
47
+ });
48
+ }
49
+ stop() {
50
+ const _super = Object.create(null, {
51
+ stop: { get: () => super.stop }
52
+ });
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const status = yield _super.stop.call(this);
55
+ yield this.unregisterListeners();
56
+ return status;
57
+ });
58
+ }
59
+ registerListeners() {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ });
62
+ }
63
+ unregisterListeners() {
64
+ return __awaiter(this, void 0, void 0, function* () {
65
+ });
66
+ }
67
+ }
68
+ exports.RedisSubscriber = RedisSubscriber;
@@ -0,0 +1,3 @@
1
+ export * from './RedisClient';
2
+ export * from './RedisPublisher';
3
+ export * from './RedisSubscriber';
@@ -0,0 +1,20 @@
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
+ // Files
18
+ __exportStar(require("./RedisClient"), exports);
19
+ __exportStar(require("./RedisPublisher"), exports);
20
+ __exportStar(require("./RedisSubscriber"), exports);
@@ -0,0 +1,64 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from "events";
3
+ export declare class TimeService {
4
+ protected status: TimeService.Status;
5
+ private offset?;
6
+ private option;
7
+ private syncedAt?;
8
+ private emitter;
9
+ readonly on: (eventName: string | symbol, listener: (...args: any[]) => void) => EventEmitter;
10
+ readonly off: (eventName: string | symbol, listener: (...args: any[]) => void) => EventEmitter;
11
+ private readonly emit;
12
+ static calculateNTPResultOffset(ntpResult: TimeService.NTPResult): TimeService.Offset;
13
+ constructor(option: TimeService.Option);
14
+ getOption(): TimeService.Option;
15
+ setOption(option: TimeService.Option): TimeService.Option;
16
+ getOffset(defaultValue: TimeService.Offset): TimeService.Offset;
17
+ getOffset(): TimeService.Offset | undefined;
18
+ setOffset(offset: TimeService.Offset): TimeService.Offset;
19
+ setOffset(offset: TimeService.Offset | undefined): TimeService.Offset;
20
+ getSyncedAt(): TimeService.TimeStamp | undefined;
21
+ private setSyncedAt;
22
+ getSyncInterval(): number | null;
23
+ setSyncInterval(interval: TimeService.Option['syncInterval']): void;
24
+ getClientTime(defaultValue?: TimeService.TimeStamp): TimeService.TimeStamp;
25
+ getServerTime(): TimeService.TimeStamp | null;
26
+ getTime(): number;
27
+ private readonly fetchServerNTPResult;
28
+ getStatus(): TimeService.Status;
29
+ start(): void;
30
+ stop(): void;
31
+ sync(): Promise<TimeService.Offset | null>;
32
+ private syncHandler?;
33
+ private startSync;
34
+ private stopSync;
35
+ }
36
+ export declare namespace TimeService {
37
+ const DEFAULT_SYNC_INTERVAL: number;
38
+ enum Status {
39
+ STOPPED = 0,
40
+ RUNNING = 1
41
+ }
42
+ type Offset = number;
43
+ type TimeStamp = number;
44
+ interface NTPResult {
45
+ t1: TimeStamp;
46
+ t2: TimeStamp;
47
+ t3: TimeStamp;
48
+ t4: TimeStamp;
49
+ }
50
+ interface ServerNTPResult extends Pick<NTPResult, 't2' | 't3'> {
51
+ }
52
+ type ClientTimeProvider = () => TimeStamp;
53
+ type ServerTimeProvider = (t1: NTPResult['t1']) => (ServerNTPResult | null) | (Promise<ServerNTPResult | null>);
54
+ interface Option {
55
+ autoStart?: boolean;
56
+ syncInterval?: number | null | undefined;
57
+ clientTimeProvider?: ClientTimeProvider | undefined;
58
+ serverTimeProvider?: ServerTimeProvider | undefined;
59
+ }
60
+ enum EVENT {
61
+ SYNCED = "SYNCED",
62
+ SYNC_INTERVAL_CHANGED = "SYNC_INTERVAL_CHANGED"
63
+ }
64
+ }
@@ -0,0 +1,235 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.TimeService = void 0;
16
+ const events_1 = __importDefault(require("events"));
17
+ const LOG_TAG = 'TimeService';
18
+ class TimeService {
19
+ static calculateNTPResultOffset(ntpResult) {
20
+ const { t1, t2, t3, t4 } = ntpResult;
21
+ return ((t2 - t1) + (t3 - t4)) / 2;
22
+ }
23
+ constructor(option) {
24
+ this.status = TimeService.Status.STOPPED;
25
+ // Emitter
26
+ this.emitter = new events_1.default({});
27
+ this.on = this.emitter.on;
28
+ this.off = this.emitter.off;
29
+ this.emit = this.emitter.emit;
30
+ this.fetchServerNTPResult = (t1) => __awaiter(this, void 0, void 0, function* () {
31
+ try {
32
+ if (typeof this.option.serverTimeProvider === 'function') {
33
+ return yield this.option.serverTimeProvider(t1);
34
+ }
35
+ }
36
+ catch (e) {
37
+ console.error(e);
38
+ }
39
+ return null;
40
+ });
41
+ this.option = option;
42
+ if (option.autoStart) {
43
+ this.start();
44
+ }
45
+ // Binding
46
+ this.start = this.start.bind(this);
47
+ this.stop = this.stop.bind(this);
48
+ this.sync = this.sync.bind(this);
49
+ this.getOption = this.getOption.bind(this);
50
+ this.setOption = this.setOption.bind(this);
51
+ }
52
+ getOption() {
53
+ return this.option;
54
+ }
55
+ setOption(option) {
56
+ return this.option = option;
57
+ }
58
+ getOffset(defaultValue) {
59
+ if (this.offset !== undefined) {
60
+ return this.offset;
61
+ }
62
+ if (defaultValue !== undefined) {
63
+ return defaultValue;
64
+ }
65
+ return undefined;
66
+ }
67
+ setOffset(offset) {
68
+ return this.offset = offset;
69
+ }
70
+ getSyncedAt() {
71
+ return this.syncedAt;
72
+ }
73
+ setSyncedAt(syncedAt) {
74
+ this.syncedAt = syncedAt;
75
+ // Emit
76
+ this.emit(TimeService.EVENT.SYNCED, syncedAt);
77
+ return syncedAt;
78
+ }
79
+ getSyncInterval() {
80
+ if (this.option.syncInterval === undefined) {
81
+ // If option is undefined using default value
82
+ return TimeService.DEFAULT_SYNC_INTERVAL;
83
+ }
84
+ if (this.option.syncInterval === null || this.option.syncInterval === -1) {
85
+ // If option is null, do not sync automatically
86
+ return null;
87
+ }
88
+ return this.option.syncInterval;
89
+ }
90
+ setSyncInterval(interval) {
91
+ this.option.syncInterval = interval;
92
+ // Emit
93
+ this.emit(TimeService.EVENT.SYNC_INTERVAL_CHANGED, interval);
94
+ if (this.status === TimeService.Status.RUNNING) {
95
+ if (this.syncHandler !== undefined) {
96
+ this.stopSync();
97
+ this.startSync();
98
+ }
99
+ }
100
+ }
101
+ getClientTime(defaultValue = Date.now()) {
102
+ try {
103
+ if (typeof this.option.clientTimeProvider === 'function') {
104
+ return this.option.clientTimeProvider();
105
+ }
106
+ }
107
+ catch (e) {
108
+ console.error(e);
109
+ }
110
+ return defaultValue;
111
+ }
112
+ getServerTime() {
113
+ const offset = this.getOffset();
114
+ if (offset === undefined) {
115
+ return null;
116
+ }
117
+ const clientTime = this.getClientTime();
118
+ return clientTime + offset;
119
+ }
120
+ getTime() {
121
+ return this.getServerTime() || this.getClientTime();
122
+ }
123
+ getStatus() {
124
+ return this.status;
125
+ }
126
+ start() {
127
+ if (this.status !== TimeService.Status.STOPPED) {
128
+ console.warn(LOG_TAG, 'service is not stopped');
129
+ return;
130
+ }
131
+ // Change status
132
+ this.status = TimeService.Status.RUNNING;
133
+ // Sync immediately
134
+ this.sync().finally(() => { });
135
+ // Start sync
136
+ this.startSync();
137
+ }
138
+ stop() {
139
+ if (this.status !== TimeService.Status.RUNNING) {
140
+ console.warn(LOG_TAG, 'service is not running');
141
+ return;
142
+ }
143
+ // Change status
144
+ this.status = TimeService.Status.RUNNING;
145
+ // Stop sync
146
+ this.stopSync();
147
+ // Reset offset
148
+ this.setOffset(undefined);
149
+ // Reset synced at
150
+ this.setSyncedAt(undefined);
151
+ }
152
+ sync() {
153
+ return __awaiter(this, void 0, void 0, function* () {
154
+ try {
155
+ // T1 (Client Request Time)
156
+ const requestedAt = Date.now();
157
+ // Fetch server time from server
158
+ const serverNtpResult = yield this.fetchServerNTPResult(requestedAt);
159
+ // Check is null
160
+ if (serverNtpResult === null) {
161
+ console.warn(LOG_TAG, 'Failed to get server time');
162
+ return null;
163
+ }
164
+ // T2 (Server Receive Time)
165
+ const { t2 } = serverNtpResult;
166
+ // Check is not a number
167
+ if (isNaN(Number(t2))) {
168
+ // Not a Number
169
+ console.error(LOG_TAG, 'invalid server time(t2), not a number', t2);
170
+ return null;
171
+ }
172
+ // T3 (Server Transmit Time)
173
+ const { t3 } = serverNtpResult;
174
+ // Check is not a number
175
+ if (isNaN(Number(t3))) {
176
+ // Not a Number
177
+ console.error(LOG_TAG, 'invalid server time(t2), not a number', t2);
178
+ return null;
179
+ }
180
+ // T4 (Client Receive Time)
181
+ const receivedAt = Date.now();
182
+ const ntpResult = {
183
+ t1: requestedAt,
184
+ t2: t2,
185
+ t3: t3,
186
+ t4: receivedAt,
187
+ };
188
+ // Calculate offset
189
+ const offset = TimeService.calculateNTPResultOffset(ntpResult);
190
+ // Save calculated offset
191
+ this.setOffset(offset);
192
+ // Mark synced timestamp
193
+ this.setSyncedAt(Date.now());
194
+ }
195
+ catch (e) {
196
+ console.error(e);
197
+ }
198
+ return null;
199
+ });
200
+ }
201
+ startSync() {
202
+ if (this.syncHandler !== undefined) {
203
+ console.warn('sync handler is not undefined', this.syncHandler);
204
+ return;
205
+ }
206
+ const syncInterval = this.getSyncInterval();
207
+ if (syncInterval === null) {
208
+ return;
209
+ }
210
+ this.syncHandler = setInterval(this.sync, syncInterval);
211
+ }
212
+ ;
213
+ stopSync() {
214
+ if (this.syncHandler === undefined) {
215
+ return;
216
+ }
217
+ clearInterval(this.syncHandler);
218
+ this.syncHandler = undefined;
219
+ }
220
+ ;
221
+ }
222
+ exports.TimeService = TimeService;
223
+ (function (TimeService) {
224
+ TimeService.DEFAULT_SYNC_INTERVAL = 60000;
225
+ let Status;
226
+ (function (Status) {
227
+ Status[Status["STOPPED"] = 0] = "STOPPED";
228
+ Status[Status["RUNNING"] = 1] = "RUNNING";
229
+ })(Status = TimeService.Status || (TimeService.Status = {}));
230
+ let EVENT;
231
+ (function (EVENT) {
232
+ EVENT["SYNCED"] = "SYNCED";
233
+ EVENT["SYNC_INTERVAL_CHANGED"] = "SYNC_INTERVAL_CHANGED";
234
+ })(EVENT = TimeService.EVENT || (TimeService.EVENT = {}));
235
+ })(TimeService || (exports.TimeService = TimeService = {}));
@@ -0,0 +1 @@
1
+ export * from "./TimeService";
@@ -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("./TimeService"), exports);
@@ -0,0 +1,3 @@
1
+ type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N ? Acc[number] : Enumerate<N, [...Acc, Acc['length']]>;
2
+ export type IntRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>> | T;
3
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ export declare class ArrayUtils {
2
+ /**
3
+ * 객체를 요소로 가지는 배열에서 가장 큰 값(Property)를 가지는 객체를 반환한다
4
+ * @param e 객체 배열
5
+ * @param key 값을 비교할 Property 키
6
+ */
7
+ static getGreatestObject(e: any[], key: string): any;
8
+ static removeDuplicate<T>(arr: T[]): T[];
9
+ static removeObjectDuplicate(arr: any[], key: string): any[];
10
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayUtils = void 0;
4
+ class ArrayUtils {
5
+ /**
6
+ * 객체를 요소로 가지는 배열에서 가장 큰 값(Property)를 가지는 객체를 반환한다
7
+ * @param e 객체 배열
8
+ * @param key 값을 비교할 Property 키
9
+ */
10
+ static getGreatestObject(e, key) {
11
+ return e.reduce((prev, current) => ((prev[key] > current[key] ? prev : current)));
12
+ }
13
+ static removeDuplicate(arr) {
14
+ return [...new Set(arr)];
15
+ }
16
+ static removeObjectDuplicate(arr, key) {
17
+ return arr.filter((v, i, self) => (i === self.findIndex(e => (e[key] === v[key]))));
18
+ }
19
+ }
20
+ exports.ArrayUtils = ArrayUtils;
@@ -0,0 +1 @@
1
+ export declare function parseBoolean(value: any, defaultValue: boolean): boolean;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseBoolean = void 0;
4
+ function parseBoolean(value, defaultValue) {
5
+ if (value === undefined || value === null)
6
+ return defaultValue;
7
+ return (value === 'true' || value === true) || (value === 1 || value === '1');
8
+ }
9
+ exports.parseBoolean = parseBoolean;
@@ -0,0 +1,8 @@
1
+ import { Coordinate } from "../interfaces/Coordinate";
2
+ export declare class CoordinateUtils {
3
+ static isValidLatitude(latitude: Coordinate['latitude'] | string): boolean;
4
+ static isValidLongitude(longitude: Coordinate['longitude'] | string): boolean;
5
+ static isValidLatitudeLongitude(latitude: Coordinate['latitude'] | string, longitude: Coordinate['longitude'] | string): boolean;
6
+ static isValidCoordinate(coordinate: Coordinate): boolean;
7
+ static crowDistance(...coordinates: Coordinate[]): number;
8
+ }
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CoordinateUtils = void 0;
4
+ class CoordinateUtils {
5
+ static isValidLatitude(latitude) {
6
+ return /^-?([0-8]?[0-9]|90)(\.[0-9]{1,15})$/.test(latitude.toString());
7
+ }
8
+ static isValidLongitude(longitude) {
9
+ return /^-?([0-9]{1,2}|1[0-7][0-9]|180)(\.[0-9]{1,14})$/.test(longitude.toString());
10
+ }
11
+ static isValidLatitudeLongitude(latitude, longitude) {
12
+ return CoordinateUtils.isValidLatitude(latitude) && CoordinateUtils.isValidLongitude(longitude);
13
+ }
14
+ static isValidCoordinate(coordinate) {
15
+ return this.isValidLatitudeLongitude(coordinate.latitude, coordinate.longitude);
16
+ }
17
+ static crowDistance(...coordinates) {
18
+ if (coordinates.length <= 2) {
19
+ const toRad = (value) => value * Math.PI / 180;
20
+ const c1 = coordinates[0];
21
+ const c2 = coordinates[1];
22
+ if (!c1 || !c2) {
23
+ return 0;
24
+ }
25
+ const R = 6371e3;
26
+ const dLat = toRad(c2.latitude - c1.latitude);
27
+ const dLon = toRad(c2.longitude - c1.longitude);
28
+ const lat1 = toRad(c1.latitude);
29
+ const lat2 = toRad(c2.latitude);
30
+ const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
31
+ Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
32
+ const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
33
+ return R * c;
34
+ }
35
+ let totalDistance = 0;
36
+ for (let i = 0; i < coordinates.length - 1; ++i) {
37
+ totalDistance += this.crowDistance(coordinates[i], coordinates[i + 1]);
38
+ }
39
+ return totalDistance;
40
+ }
41
+ }
42
+ exports.CoordinateUtils = CoordinateUtils;
@@ -0,0 +1,12 @@
1
+ import { MomentInput, RelativeTimeSpec } from "moment";
2
+ export declare class DateUtils {
3
+ static relativeDate: (input: MomentInput, from?: MomentInput) => string;
4
+ static setLocale: (language: DateUtils.Locale) => void;
5
+ private static getRelativeTimeSpec;
6
+ }
7
+ export declare namespace DateUtils {
8
+ type Locale = string;
9
+ const relativeTimeSpecs: {
10
+ [key: Locale]: RelativeTimeSpec;
11
+ };
12
+ }