forked from metarhia/metautil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metautil.d.ts
288 lines (236 loc) · 7.86 KB
/
metautil.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { IncomingMessage } from 'node:http';
import { ScryptOptions, X509Certificate } from 'node:crypto';
type Strings = Array<string>;
type Dictionary = Record<string, unknown>;
type Cookies = Record<string, string>;
type Headers = Record<string, string>;
// Submodule: async
export const toBool: [() => boolean, () => boolean];
export function timeout(msec: number, signal?: AbortSignal): Promise<void>;
export function delay(msec: number, signal?: AbortSignal): Promise<void>;
export function timeoutify(
promise: Promise<unknown>,
msec: number,
): Promise<unknown>;
// Submodule: crypto
export function cryptoRandom(min?: number, max?: number): number;
export function random(min?: number, max?: number): number;
export function generateUUID(): string;
export function generateKey(possible: string, length: number): string;
export function crcToken(secret: string, key: string): string;
export function generateToken(
secret: string,
characters: string,
length: number,
): string;
export function validateToken(secret: string, token: string): boolean;
export function serializeHash(hash: Buffer, salt: Buffer): string;
export interface HashInfo {
params: ScryptOptions;
salt: Buffer;
hash: Buffer;
}
export function deserializeHash(phcString: string): HashInfo;
export function hashPassword(password: string): Promise<string>;
export function validatePassword(
password: string,
serHash: string,
): Promise<boolean>;
export function md5(fileName: string): Promise<string>;
export function getX509(cert: X509Certificate): Strings;
// Submodule: datetime
export function duration(s: string | number): number;
export function nowDate(date?: Date): string;
export function nowDateTimeUTC(date?: Date, timeSep?: string): string;
export function parseMonth(s: string): number;
export function parseDay(s: string): number;
export function parseEvery(s: string): Every;
type Every = {
YY: number;
MM: number;
DD: number;
wd: number;
dd: number;
hh: number;
mm: number;
ms: number;
};
export type { Every };
export function nextEvent(every: Every, date?: Date): number;
// Submodule: error
export interface ErrorOptions {
code?: number | string;
cause?: Error;
}
export class Error extends global.Error {
constructor(message: string, options?: number | string | ErrorOptions);
message: string;
stack: string;
code?: number | string;
cause?: Error;
}
type Errors = Record<string, string>;
export class DomainError extends Error {
constructor(code?: string, options?: number | string | ErrorOptions);
message: string;
stack: string;
code?: number | string;
cause?: Error;
toError(errors: Errors): Error;
}
export function isError(instance: object): boolean;
// Submodule: file system
export function directoryExists(path: string): Promise<boolean>;
export function ensureDirectory(path: string): Promise<boolean>;
// Submodule: http
export interface StreamRange {
start?: number;
end?: number;
tail?: number;
}
export function parseHost(host?: string): string;
export function parseParams(params: string): Cookies;
export function parseCookies(cookie: string): Headers;
export function parseRange(range: string): StreamRange;
// Submodule: network
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
type Body = ArrayBuffer | Buffer | string;
export type ApiOptions = {
method?: HttpMethod;
headers?: object;
body?: Body;
};
export function receiveBody(stream: IncomingMessage): Promise<Buffer | null>;
export function ipToInt(ip?: string): number;
export function intToIp(int: number): string;
export function httpApiCall(url: string, options: ApiOptions): Promise<object>;
// Submodule: objects
type Namespaces = Array<Dictionary>;
export function makePrivate(instance: object): object;
export function protect(allowMixins: Strings, ...namespaces: Namespaces): void;
export function jsonParse(buffer: Buffer): Dictionary | null;
export function isHashObject(o: string | number | boolean | object): boolean;
export function flatObject(source: Dictionary, fields: Strings): Dictionary;
export function unflatObject(source: Dictionary, fields: Strings): Dictionary;
export function getSignature(method: Function): Strings;
export function namespaceByPath(
namespace: Dictionary,
path: string,
): Dictionary | null;
export function serializeArguments(fields: Strings, args: Dictionary): string;
// Submodule: pool
export interface QueueElement {
resolve: Function;
timer: NodeJS.Timer;
}
export class Pool {
constructor(options: { timeout?: number });
items: Array<unknown>;
free: Array<boolean>;
queue: Array<unknown>;
current: number;
size: number;
available: number;
timeout: number;
next(): Promise<unknown>;
add(item: unknown): void;
capture(): Promise<unknown>;
release(item: unknown): void;
isFree(item: unknown): boolean;
}
// Submodule: array
export function sample(array: Array<unknown>, random?: Function): unknown;
export function shuffle(
array: Array<unknown>,
random?: Function,
): Array<unknown>;
export function projection(
source: object,
fields: Array<string>,
): Array<unknown>;
// Submodule: semaphore
export interface SemaphoreOptions {
concurrency: number;
size?: number;
timeout?: number;
}
export class Semaphore {
constructor(options: SemaphoreOptions);
concurrency: number;
counter: number;
timeout: number;
size: number;
empty: boolean;
queue: Array<QueueElement>;
enter(): Promise<void>;
leave(): void;
}
// Submodule: strings
export function replace(str: string, substr: string, newstr: string): string;
export function between(s: string, prefix: string, suffix: string): string;
export function split(s: string, separator: string): [string, string];
export function isFirstUpper(s: string): boolean;
export function isFirstLower(s: string): boolean;
export function isFirstLetter(s: string): boolean;
export function toLowerCamel(s: string): string;
export function toUpperCamel(s: string): string;
export function toLower(s: string): string;
export function toCamel(separator: string): (s: string) => string;
export function spinalToCamel(s: string): string;
export function snakeToCamel(s: string): string;
export function isConstant(s: string): boolean;
export function fileExt(fileName: string): string;
export function parsePath(relPath: string): Strings;
export function trimLines(s: string): string;
// Submodule: units
export function bytesToSize(bytes: number): string;
export function sizeToBytes(size: string): number;
// Submodule: collector
export interface CollectorOptions {
exact?: boolean;
defaults?: object;
timeout?: number;
reassign?: boolean;
validate?: (data: Record<string, unknown>) => unknown;
}
type AsyncFunction = (...args: Array<unknown>) => Promise<unknown>;
export class Collector {
done: boolean;
data: Dictionary;
keys: Array<string>;
count: number;
exact: boolean;
timeout: number;
defaults: object;
reassign: boolean;
validate?: (data: Record<string, unknown>) => unknown;
signal: AbortSignal;
constructor(keys: Array<string>, options?: CollectorOptions);
set(key: string, value: unknown): void;
wait(
key: string,
fn: AsyncFunction | Promise<unknown>,
...args: Array<unknown>
): void;
take(key: string, fn: Function, ...args: Array<unknown>): void;
collect(sources: Record<string, Collector>): void;
fail(error: Error): void;
abort(): void;
then(onFulfill: Function, onReject?: Function): Promise<unknown>;
}
export function collect(
keys: Array<string>,
options?: CollectorOptions,
): Collector;
// Submodule: Events
export class EventEmitter {
constructor();
getMaxListeners(): number;
listenerCount(name: string): number;
on(name: string, fn: Function): void;
once(name: string, fn: Function): void;
emit(name: string, ...args: Array<unknown>): void;
remove(name: string, fn: Function): void;
clear(name: string): void;
}
export function once(emitter: EventEmitter, name: string): Promise<unknown>;