@idlebox/common 1.3.24 → 1.3.27
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/cjs/__create_index.generated.cjs +77 -57
- package/lib/cjs/__create_index.generated.cjs.map +1 -1
- package/lib/cjs/__create_index.generated.d.cts +69 -5
- package/lib/cjs/debugging/tryInspect.cjs +4 -1
- package/lib/cjs/debugging/tryInspect.cjs.map +1 -1
- package/lib/cjs/error/known.cjs +26 -0
- package/lib/cjs/error/known.cjs.map +10 -0
- package/lib/cjs/error/pretty.cjs +238 -0
- package/lib/cjs/error/pretty.cjs.map +10 -0
- package/lib/cjs/lifecycle/dispose/disposedError.cjs +15 -2
- package/lib/cjs/lifecycle/dispose/disposedError.cjs.map +1 -1
- package/lib/cjs/lifecycle/dispose/lifecycle.async.cjs +9 -4
- package/lib/cjs/lifecycle/dispose/lifecycle.async.cjs.map +1 -1
- package/lib/cjs/lifecycle/dispose/lifecycle.sync.cjs +10 -2
- package/lib/cjs/lifecycle/dispose/lifecycle.sync.cjs.map +1 -1
- package/lib/cjs/path/normalizePath.cjs +116 -4
- package/lib/cjs/path/normalizePath.cjs.map +1 -1
- package/lib/cjs/promise/{promisePool.cjs → promiseCollection.cjs} +4 -4
- package/lib/cjs/promise/promiseCollection.cjs.map +10 -0
- package/lib/cjs/promise/{timeoutPromisePool.cjs → timeoutPromiseCollection.cjs} +5 -5
- package/lib/cjs/promise/timeoutPromiseCollection.cjs.map +10 -0
- package/lib/cjs/state/StateMachine.cjs +34 -0
- package/lib/cjs/state/StateMachine.cjs.map +10 -0
- package/lib/esm/__create_index.generated.d.mts +69 -5
- package/lib/esm/__create_index.generated.mjs +44 -32
- package/lib/esm/__create_index.generated.mjs.map +1 -1
- package/lib/esm/debugging/tryInspect.mjs +4 -1
- package/lib/esm/debugging/tryInspect.mjs.map +1 -1
- package/lib/esm/error/known.mjs +23 -0
- package/lib/esm/error/known.mjs.map +10 -0
- package/lib/esm/error/pretty.mjs +232 -0
- package/lib/esm/error/pretty.mjs.map +10 -0
- package/lib/esm/lifecycle/dispose/disposedError.mjs +15 -2
- package/lib/esm/lifecycle/dispose/disposedError.mjs.map +1 -1
- package/lib/esm/lifecycle/dispose/lifecycle.async.mjs +9 -4
- package/lib/esm/lifecycle/dispose/lifecycle.async.mjs.map +1 -1
- package/lib/esm/lifecycle/dispose/lifecycle.sync.mjs +10 -2
- package/lib/esm/lifecycle/dispose/lifecycle.sync.mjs.map +1 -1
- package/lib/esm/path/normalizePath.mjs +113 -3
- package/lib/esm/path/normalizePath.mjs.map +1 -1
- package/lib/esm/promise/{promisePool.mjs → promiseCollection.mjs} +2 -2
- package/lib/esm/promise/promiseCollection.mjs.map +10 -0
- package/lib/esm/promise/{timeoutPromisePool.mjs → timeoutPromiseCollection.mjs} +3 -3
- package/lib/esm/promise/timeoutPromiseCollection.mjs.map +10 -0
- package/lib/esm/state/StateMachine.mjs +30 -0
- package/lib/esm/state/StateMachine.mjs.map +10 -0
- package/package.json +4 -4
- package/src/debugging/tryInspect.ts +3 -1
- package/src/error/known.ts +27 -0
- package/src/error/pretty.ts +253 -0
- package/src/lifecycle/dispose/disposedError.ts +20 -3
- package/src/lifecycle/dispose/lifecycle.async.ts +14 -3
- package/src/lifecycle/dispose/lifecycle.sync.ts +18 -4
- package/src/lifecycle/dispose/lifecycle.ts +1 -1
- package/src/path/normalizePath.ts +126 -3
- package/src/promise/{promisePool.ts → promiseCollection.ts} +1 -1
- package/src/promise/{timeoutPromisePool.ts → timeoutPromiseCollection.ts} +2 -2
- package/src/state/StateMachine.ts +52 -0
- package/lib/cjs/promise/promisePool.cjs.map +0 -10
- package/lib/cjs/promise/timeoutPromisePool.cjs.map +0 -10
- package/lib/esm/promise/promisePool.mjs.map +0 -10
- package/lib/esm/promise/timeoutPromisePool.mjs.map +0 -10
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { isAbsolute } from '../path/isAbsolute';
|
|
2
|
+
import { relativePath } from '../path/normalizePath';
|
|
3
|
+
import { globalObject } from '../platform/globalObject';
|
|
4
|
+
import { isNative } from '../platform/os';
|
|
5
|
+
|
|
6
|
+
const regNormal = /^\s+at ([^\/\s]+)(?: \[as ([^\]]+)])? \(((?:node|file):\/*)?([^:]+)(:\d+)?(:\d+)\)$/;
|
|
7
|
+
const process = globalObject.process;
|
|
8
|
+
const window = globalObject.window;
|
|
9
|
+
|
|
10
|
+
const enum regNormalMatch {
|
|
11
|
+
fn = 1,
|
|
12
|
+
fnAlias,
|
|
13
|
+
fileSchema,
|
|
14
|
+
file,
|
|
15
|
+
row,
|
|
16
|
+
column,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const regFunctionOnly = /^\s+at ([^\/\\\s]+(?: \[as ([^\]]+)])?)$/;
|
|
20
|
+
|
|
21
|
+
const enum regFunctionMatch {
|
|
22
|
+
fn = 1,
|
|
23
|
+
fnAlias,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const regFileOnly = /^\s+at ([^:]+)(:\d+)?(:\d+)$/;
|
|
27
|
+
|
|
28
|
+
const enum regFileOnlyMatch {
|
|
29
|
+
file = 1,
|
|
30
|
+
row,
|
|
31
|
+
column,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const regSimpleFrame = /^\s+at ([^(]+) \(<anonymous>\)$/;
|
|
35
|
+
|
|
36
|
+
const enum regSimpleFrameMatch {
|
|
37
|
+
fn = 1,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let root = process?.cwd() ?? window?.location?.domain ?? '/';
|
|
41
|
+
|
|
42
|
+
export function setErrorLogRoot(_root: string) {
|
|
43
|
+
root = _root;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface IInternalData {
|
|
47
|
+
raw?: string;
|
|
48
|
+
fn?: string;
|
|
49
|
+
as?: string;
|
|
50
|
+
file?: string;
|
|
51
|
+
fileSchema?: string; // 'node' | 'file';
|
|
52
|
+
line?: number;
|
|
53
|
+
col?: number;
|
|
54
|
+
abs?: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let notify_printed = false;
|
|
58
|
+
export function prettyPrintError(type: string, e: Error) {
|
|
59
|
+
if (!e.stack || e.stack === e.message) {
|
|
60
|
+
return console.error(e.message);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (globalObject.process?.env?.DISABLE_PRETTY_ERROR) {
|
|
64
|
+
console.error('[%s] %s', type, e.stack || e.message);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
console.error(`------------------------------------------
|
|
68
|
+
[${type}] ${prettyFormatError(e)}`);
|
|
69
|
+
|
|
70
|
+
if (!notify_printed) {
|
|
71
|
+
notify_printed = true;
|
|
72
|
+
console.error('\x1B[2muse env.DISABLE_PRETTY_ERROR=yes to see original error stack\x1B[0m');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function red(s: string) {
|
|
77
|
+
return isNative ? `\x1B[38;5;9m${s}\x1B[0m` : s;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function prettyFormatError(e: Error, withMessage = true) {
|
|
81
|
+
if (!e || typeof e.message !== 'string') {
|
|
82
|
+
console.error(e);
|
|
83
|
+
return red('Unknown Error') + '\n' + new Error().stack?.split('\n').slice(3).join('\n');
|
|
84
|
+
}
|
|
85
|
+
if (!e.stack) {
|
|
86
|
+
return red(e.message + '\n No stack trace');
|
|
87
|
+
}
|
|
88
|
+
const stackStr = e.stack.replace(/file:\/\//, '').split(/\n/g);
|
|
89
|
+
|
|
90
|
+
let first = stackStr.shift()!;
|
|
91
|
+
const stack: IInternalData[] = stackStr.map((line) => {
|
|
92
|
+
if (regNormal.test(line)) {
|
|
93
|
+
const m = regNormal.exec(line)!;
|
|
94
|
+
return {
|
|
95
|
+
fn: m[regNormalMatch.fn],
|
|
96
|
+
as: m[regNormalMatch.fnAlias],
|
|
97
|
+
fileSchema: m[regNormalMatch.fileSchema],
|
|
98
|
+
file: m[regNormalMatch.file],
|
|
99
|
+
line: parseInt(m[regNormalMatch.row].slice(1)),
|
|
100
|
+
col: parseInt(m[regNormalMatch.column].slice(1)),
|
|
101
|
+
abs: isAbsolute(m[regNormalMatch.file]),
|
|
102
|
+
};
|
|
103
|
+
} else if (regFunctionOnly.test(line)) {
|
|
104
|
+
const m = regFunctionOnly.exec(line)!;
|
|
105
|
+
return {
|
|
106
|
+
fn: m[regFunctionMatch.fn],
|
|
107
|
+
as: m[regFunctionMatch.fnAlias],
|
|
108
|
+
file: undefined,
|
|
109
|
+
line: undefined,
|
|
110
|
+
col: undefined,
|
|
111
|
+
abs: false,
|
|
112
|
+
};
|
|
113
|
+
} else if (regFileOnly.test(line)) {
|
|
114
|
+
const m = regFileOnly.exec(line)!;
|
|
115
|
+
return {
|
|
116
|
+
fn: undefined,
|
|
117
|
+
as: undefined,
|
|
118
|
+
file: m[regFileOnlyMatch.file],
|
|
119
|
+
line: parseInt(m[regFileOnlyMatch.row].slice(1)),
|
|
120
|
+
col: parseInt(m[regFileOnlyMatch.column].slice(1)),
|
|
121
|
+
abs: isAbsolute(m[regFileOnlyMatch.file]),
|
|
122
|
+
};
|
|
123
|
+
} else if (regSimpleFrame.test(line)) {
|
|
124
|
+
const m = regSimpleFrame.exec(line)!;
|
|
125
|
+
return {
|
|
126
|
+
fn: m[regSimpleFrameMatch.fn],
|
|
127
|
+
as: undefined,
|
|
128
|
+
file: '<anonymous>',
|
|
129
|
+
line: undefined,
|
|
130
|
+
col: undefined,
|
|
131
|
+
abs: false,
|
|
132
|
+
};
|
|
133
|
+
} else {
|
|
134
|
+
return { raw: line.replace(/^ /, '') };
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
const stackOutput = stack
|
|
138
|
+
.filter(ignoreSomeFiles)
|
|
139
|
+
.map(translateFunction)
|
|
140
|
+
.map(({ raw, fn, file, as, abs, line, col, fileSchema }) => {
|
|
141
|
+
let ret;
|
|
142
|
+
if (raw) {
|
|
143
|
+
return raw;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (abs) {
|
|
147
|
+
const isNodeModule = file?.includes('/node_modules/');
|
|
148
|
+
const color = fn ? (isNodeModule ? '4' : '14') : '8';
|
|
149
|
+
ret = ` at \x1b[38;5;${color}m`;
|
|
150
|
+
if (as && fn && fn.startsWith('Object.')) {
|
|
151
|
+
ret += as + ' [export]';
|
|
152
|
+
} else {
|
|
153
|
+
ret += formatFunctionName(fn, as);
|
|
154
|
+
}
|
|
155
|
+
ret += '\x1b[0m';
|
|
156
|
+
if (file) {
|
|
157
|
+
ret += ' (';
|
|
158
|
+
if (!isNodeModule) {
|
|
159
|
+
ret += '\x1b[38;5;2m';
|
|
160
|
+
}
|
|
161
|
+
ret += formatFileLine(fileSchema, file, line, col);
|
|
162
|
+
ret += '\x1B[0m)';
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
165
|
+
ret = '\x1B[2m at ';
|
|
166
|
+
if (fn) {
|
|
167
|
+
ret += fn;
|
|
168
|
+
}
|
|
169
|
+
if (file) {
|
|
170
|
+
if (fn) {
|
|
171
|
+
ret += ' (';
|
|
172
|
+
}
|
|
173
|
+
ret += formatFileLine(fileSchema, file, line, col);
|
|
174
|
+
if (fn) {
|
|
175
|
+
ret += ')';
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
ret += '\x1B[0m';
|
|
179
|
+
}
|
|
180
|
+
return ret;
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
if (withMessage) {
|
|
184
|
+
if ((e as any).code) {
|
|
185
|
+
first = first.replace(/^(\S+):/, (_, name) => {
|
|
186
|
+
return `${name}(code ${(e as any).code}):`;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return first + '\n' + stackOutput.join('\n');
|
|
191
|
+
} else {
|
|
192
|
+
return stackOutput.join('\n');
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function formatFileLine(schema: string | undefined, file: string, row?: number, col?: number) {
|
|
197
|
+
let rel = file;
|
|
198
|
+
|
|
199
|
+
if (schema !== 'node:') {
|
|
200
|
+
rel = relativePath(root, file);
|
|
201
|
+
if (rel.startsWith('..')) {
|
|
202
|
+
rel = file;
|
|
203
|
+
} else if (!rel.startsWith('.')) {
|
|
204
|
+
rel = './' + rel;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return `${schema || ''}${rel}${row ? `:${row}` : ''}${col ? `:${col}` : ''}`;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function formatFunctionName(fn?: string, as?: string) {
|
|
212
|
+
if (fn) {
|
|
213
|
+
if (as) {
|
|
214
|
+
return `${fn} [as ${as}]`;
|
|
215
|
+
} else {
|
|
216
|
+
return fn;
|
|
217
|
+
}
|
|
218
|
+
} else {
|
|
219
|
+
return '[anonymous]';
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function translateFunction(data: IInternalData): IInternalData {
|
|
224
|
+
if (!data.fn) {
|
|
225
|
+
return data;
|
|
226
|
+
}
|
|
227
|
+
if (data.fn === 'Timeout._onTimeout') {
|
|
228
|
+
data.fn = 'setTimeout';
|
|
229
|
+
}
|
|
230
|
+
if (data.fn.startsWith('Timeout.') && data.as === '_onTimeout') {
|
|
231
|
+
data.fn = 'setTimeout->' + data.fn.slice(8);
|
|
232
|
+
delete data.as;
|
|
233
|
+
}
|
|
234
|
+
return data;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function ignoreSomeFiles({ file }: IInternalData): boolean {
|
|
238
|
+
if (!file) {
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
if (file === 'internal/timers.js') {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
if (file.startsWith('internal/modules/cjs/loader')) {
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
if (!file.includes('/')) {
|
|
248
|
+
if (file.startsWith('_stream_')) {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
@@ -1,13 +1,30 @@
|
|
|
1
|
-
import { getErrorFrame } from '../../error/getFrame';
|
|
2
1
|
import { tryInspect } from '../../debugging/tryInspect';
|
|
2
|
+
import { getErrorFrame } from '../../error/getFrame';
|
|
3
|
+
import { prettyFormatError } from '../../error/pretty';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Error when call dispose() twice
|
|
6
7
|
*/
|
|
7
8
|
export class DisposedError extends Error {
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
public readonly inspectString: string;
|
|
10
|
+
constructor(
|
|
11
|
+
object: any,
|
|
12
|
+
public readonly previous: Error,
|
|
13
|
+
) {
|
|
14
|
+
const insp = tryInspect(object);
|
|
15
|
+
super(`Object [${insp}] has already disposed ${getErrorFrame(previous, 2)}.`);
|
|
16
|
+
|
|
17
|
+
this.inspectString = insp;
|
|
10
18
|
this.name = 'Warning';
|
|
19
|
+
|
|
20
|
+
this.tryCreateConsoleWarning().catch(() => {});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public async tryCreateConsoleWarning() {
|
|
24
|
+
console.error('DisposedWarning: duplicate dispose.');
|
|
25
|
+
console.error(' * first dispose:\n%s', prettyFormatError(this.previous, false));
|
|
26
|
+
console.error(' * current dispose:\n%s', prettyFormatError(this, false));
|
|
27
|
+
console.error(' * Object: %s', this.inspectString);
|
|
11
28
|
}
|
|
12
29
|
}
|
|
13
30
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { convertCatchedError } from '../../error/convertUnknown';
|
|
2
2
|
import { Emitter, EventRegister } from '../event/event';
|
|
3
3
|
import { DisposedError } from './disposedError';
|
|
4
|
-
import { IAsyncDisposable,
|
|
4
|
+
import { IAsyncDisposable, IDisposableEvents } from './lifecycle';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Async version of Disposable
|
|
8
8
|
* @public
|
|
9
9
|
*/
|
|
10
|
-
export class AsyncDisposable implements IAsyncDisposable,
|
|
10
|
+
export class AsyncDisposable implements IAsyncDisposable, IDisposableEvents {
|
|
11
11
|
private readonly _disposables: IAsyncDisposable[] = [];
|
|
12
12
|
|
|
13
13
|
protected readonly _onDisposeError = new Emitter<Error>();
|
|
@@ -34,12 +34,23 @@ export class AsyncDisposable implements IAsyncDisposable, IDisposableBaseInterna
|
|
|
34
34
|
/**
|
|
35
35
|
* register a disposable object
|
|
36
36
|
*/
|
|
37
|
-
public _register<T extends IAsyncDisposable>(d: T): T
|
|
37
|
+
public _register<T extends IAsyncDisposable>(d: T): T;
|
|
38
|
+
public _register<T extends IAsyncDisposable & IDisposableEvents>(d: T, autoDereference?: boolean): T;
|
|
39
|
+
public _register<T extends IAsyncDisposable | (IAsyncDisposable & IDisposableEvents)>(d: T, deref?: boolean): T {
|
|
38
40
|
this.assertNotDisposed();
|
|
39
41
|
this._disposables.unshift(d);
|
|
42
|
+
if (deref) {
|
|
43
|
+
(d as IDisposableEvents).onBeforeDispose(() => {
|
|
44
|
+
this._unregister(d);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
40
47
|
return d;
|
|
41
48
|
}
|
|
42
49
|
|
|
50
|
+
public _unregister(d: IAsyncDisposable) {
|
|
51
|
+
return this._disposables.splice(this._disposables.indexOf(d), 1).length > 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
43
54
|
public async dispose(): Promise<void> {
|
|
44
55
|
if (this._disposed) {
|
|
45
56
|
console.warn(new DisposedError(this, this._disposed).message);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DisposedError } from './disposedError';
|
|
2
1
|
import { Emitter, EventRegister } from '../event/event';
|
|
3
|
-
import {
|
|
2
|
+
import { DisposedError } from './disposedError';
|
|
3
|
+
import { IDisposable, IDisposableEvents } from './lifecycle';
|
|
4
4
|
|
|
5
5
|
export abstract class DisposableOnce implements IDisposable {
|
|
6
6
|
private _disposed?: Error;
|
|
@@ -23,7 +23,7 @@ export abstract class DisposableOnce implements IDisposable {
|
|
|
23
23
|
/**
|
|
24
24
|
* Standalone disposable class, can use as instance or base class.
|
|
25
25
|
*/
|
|
26
|
-
export class Disposable implements IDisposable,
|
|
26
|
+
export class Disposable implements IDisposable, IDisposableEvents {
|
|
27
27
|
private readonly _disposables: IDisposable[] = [];
|
|
28
28
|
|
|
29
29
|
protected readonly _onDisposeError = new Emitter<Error>();
|
|
@@ -47,12 +47,26 @@ export class Disposable implements IDisposable, IDisposableBaseInternal {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
/**
|
|
51
|
+
* register a disposable object
|
|
52
|
+
*/
|
|
53
|
+
public _register<T extends IDisposable>(d: T): T;
|
|
54
|
+
public _register<T extends IDisposable & IDisposableEvents>(d: T, autoDereference?: boolean): T;
|
|
55
|
+
public _register<T extends IDisposable | (IDisposable & IDisposableEvents)>(d: T, autoDereference?: boolean): T {
|
|
51
56
|
this.assertNotDisposed();
|
|
52
57
|
this._disposables.unshift(d);
|
|
58
|
+
if (autoDereference) {
|
|
59
|
+
(d as IDisposableEvents).onBeforeDispose(() => {
|
|
60
|
+
this._unregister(d);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
53
63
|
return d;
|
|
54
64
|
}
|
|
55
65
|
|
|
66
|
+
public _unregister(d: IDisposable) {
|
|
67
|
+
return this._disposables.splice(this._disposables.indexOf(d), 1).length > 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
56
70
|
public dispose(): void {
|
|
57
71
|
if (this._disposed) {
|
|
58
72
|
console.warn(new DisposedError(this, this._disposed).message);
|
|
@@ -3,7 +3,7 @@ import { EventRegister } from '../event/event';
|
|
|
3
3
|
/**
|
|
4
4
|
* @private
|
|
5
5
|
*/
|
|
6
|
-
export interface
|
|
6
|
+
export interface IDisposableEvents {
|
|
7
7
|
onDisposeError: EventRegister<Error>;
|
|
8
8
|
onBeforeDispose: EventRegister<void>;
|
|
9
9
|
readonly hasDisposed: boolean;
|
|
@@ -1,11 +1,134 @@
|
|
|
1
|
+
const schema = /^[a-z]{2,}:\/\//i;
|
|
2
|
+
const unc = /^[\/\\]{1,2}\?[\/\\]UNC[\/\\]/i;
|
|
3
|
+
const winSp = /^[\/\\]{1,2}\?[\/\\]([a-z]:[\/\\])/i;
|
|
4
|
+
const winLetter = /^[a-z]:\//i;
|
|
5
|
+
const doubleSlash = /^[\/\\]{2}[^\/\\]/i;
|
|
6
|
+
|
|
7
|
+
export enum PathKind {
|
|
8
|
+
url,
|
|
9
|
+
unc,
|
|
10
|
+
win,
|
|
11
|
+
cifs,
|
|
12
|
+
unix,
|
|
13
|
+
relative,
|
|
14
|
+
}
|
|
15
|
+
export interface IPathInfo {
|
|
16
|
+
kind: PathKind;
|
|
17
|
+
prefix?: string;
|
|
18
|
+
path: string;
|
|
19
|
+
url?: URL;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function analyzePath(p: string) {
|
|
23
|
+
const inp = p;
|
|
24
|
+
let r: IPathInfo;
|
|
25
|
+
if (schema.test(p)) {
|
|
26
|
+
const u = new URL(p);
|
|
27
|
+
r = {
|
|
28
|
+
kind: PathKind.url,
|
|
29
|
+
prefix: u.protocol + '//' + u.host,
|
|
30
|
+
path: u.pathname.slice(1),
|
|
31
|
+
url: u,
|
|
32
|
+
};
|
|
33
|
+
} else if (unc.test(p)) {
|
|
34
|
+
p = p.replace(unc, '').replace(/^[\/\\]+/, '');
|
|
35
|
+
const i = /[\///]/.exec(p)?.index ?? -1;
|
|
36
|
+
if (i <= 0) throw new Error('invalid unc path: ' + inp);
|
|
37
|
+
|
|
38
|
+
r = {
|
|
39
|
+
kind: PathKind.unc,
|
|
40
|
+
prefix: '//?/UNC/' + p.slice(0, i),
|
|
41
|
+
path: p.slice(i + 1),
|
|
42
|
+
};
|
|
43
|
+
} else if (winSp.test(p) || winLetter.test(p)) {
|
|
44
|
+
p = p.replace(winSp, '$1');
|
|
45
|
+
|
|
46
|
+
r = {
|
|
47
|
+
kind: PathKind.win,
|
|
48
|
+
prefix: p.slice(0, 2),
|
|
49
|
+
path: p.slice(4),
|
|
50
|
+
};
|
|
51
|
+
} else if (doubleSlash.test(p)) {
|
|
52
|
+
p = p.replace(/^[\/\\]+/, '');
|
|
53
|
+
const i = /[\///]/.exec(p)?.index ?? -1;
|
|
54
|
+
if (i <= 0) throw new Error('invalid cifs url: ' + inp);
|
|
55
|
+
|
|
56
|
+
r = {
|
|
57
|
+
kind: PathKind.cifs,
|
|
58
|
+
prefix: '//' + p.slice(0, i),
|
|
59
|
+
path: p.slice(i + 1),
|
|
60
|
+
};
|
|
61
|
+
} else if (p.startsWith('/')) {
|
|
62
|
+
r = {
|
|
63
|
+
kind: PathKind.unix,
|
|
64
|
+
prefix: '',
|
|
65
|
+
path: p,
|
|
66
|
+
};
|
|
67
|
+
} else {
|
|
68
|
+
const st = p.startsWith('..') ? '..' : '.';
|
|
69
|
+
r = {
|
|
70
|
+
kind: PathKind.relative,
|
|
71
|
+
prefix: st,
|
|
72
|
+
path: p,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const path = r.path.replace(/\\/g, '/');
|
|
77
|
+
const st = [];
|
|
78
|
+
for (const item of path.split('/')) {
|
|
79
|
+
if (!item || item === '.') continue;
|
|
80
|
+
if (item === '..') {
|
|
81
|
+
st.pop();
|
|
82
|
+
} else {
|
|
83
|
+
st.push(item);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
r.path = st.join('/');
|
|
87
|
+
|
|
88
|
+
return r;
|
|
89
|
+
}
|
|
90
|
+
|
|
1
91
|
/**
|
|
2
92
|
* replace // to /
|
|
3
93
|
* replace \ to /
|
|
4
94
|
* remove ending /
|
|
5
95
|
*/
|
|
6
96
|
export function normalizePath(p: string) {
|
|
7
|
-
|
|
8
|
-
|
|
97
|
+
const r = analyzePath(p);
|
|
98
|
+
|
|
99
|
+
if (r.prefix === undefined) {
|
|
100
|
+
return r.path;
|
|
101
|
+
} else {
|
|
102
|
+
return `${r.prefix}/${r.path}`;
|
|
9
103
|
}
|
|
10
|
-
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function relativePath(from: string, to: string) {
|
|
107
|
+
const r1 = analyzePath(from);
|
|
108
|
+
const r2 = analyzePath(to);
|
|
109
|
+
if (r1.kind !== r2.kind)
|
|
110
|
+
throw new Error(
|
|
111
|
+
`cannot relative path between different kind: "${PathKind[r1.kind]}::${r1.prefix}" * "${
|
|
112
|
+
PathKind[r2.kind]
|
|
113
|
+
}::${r2.prefix}"`,
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
if (r1.prefix !== r2.prefix) return to;
|
|
117
|
+
|
|
118
|
+
const p1arr = r1.path.split('/');
|
|
119
|
+
const p2arr = r2.path.split('/');
|
|
120
|
+
|
|
121
|
+
// find same prefix
|
|
122
|
+
let i = 0;
|
|
123
|
+
while (i < p1arr.length && i < p2arr.length && p1arr[i] === p2arr[i]) i++;
|
|
124
|
+
|
|
125
|
+
// remove same prefix
|
|
126
|
+
p1arr.splice(0, i);
|
|
127
|
+
p2arr.splice(0, i);
|
|
128
|
+
|
|
129
|
+
// add ..
|
|
130
|
+
const p1 = p1arr.length > 0 ? p1arr.map(() => '..').join('/') + '/' : '';
|
|
131
|
+
const p2 = p2arr.join('/');
|
|
132
|
+
|
|
133
|
+
return p1 + p2;
|
|
11
134
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CanceledError } from '../lifecycle/promise/cancel';
|
|
2
2
|
import { DeferredPromise } from '../lifecycle/promise/deferredPromise';
|
|
3
3
|
|
|
4
|
-
export class
|
|
4
|
+
export class PromiseCollection {
|
|
5
5
|
protected readonly promiseList: Record<string, DeferredPromise<any>> = {};
|
|
6
6
|
|
|
7
7
|
size() {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TimeoutError } from '../lifecycle/timeout/timeoutError';
|
|
2
|
-
import {
|
|
2
|
+
import { PromiseCollection } from './promiseCollection';
|
|
3
3
|
|
|
4
|
-
export class
|
|
4
|
+
export class TimeoutPromiseCollection extends PromiseCollection {
|
|
5
5
|
constructor(private readonly defaultTimeoutMs = 50000) {
|
|
6
6
|
super();
|
|
7
7
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Emitter } from '../lifecycle/event/event';
|
|
2
|
+
|
|
3
|
+
// type StateType = string | number;
|
|
4
|
+
// type EventType = string | number;
|
|
5
|
+
|
|
6
|
+
export type IFsmRuleMap<StateType, EventType> = MapLike<StateType, MapLike<EventType, StateType>>;
|
|
7
|
+
|
|
8
|
+
type MapLike<K, V> = Pick<Map<K, V>, 'get' | 'has' | 'keys'>;
|
|
9
|
+
|
|
10
|
+
export interface IStateChangeEvent<StateType, EventType> {
|
|
11
|
+
from: StateType;
|
|
12
|
+
to: StateType;
|
|
13
|
+
reason: EventType;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class SimpleStateMachine<StateType, EventType> {
|
|
17
|
+
protected declare currentState: StateType;
|
|
18
|
+
protected readonly rules: IFsmRuleMap<StateType, EventType>;
|
|
19
|
+
|
|
20
|
+
private readonly _onStateChange = new Emitter<IStateChangeEvent<StateType, EventType>>();
|
|
21
|
+
public readonly onStateChange = this._onStateChange.register;
|
|
22
|
+
|
|
23
|
+
constructor(rules: IFsmRuleMap<StateType, EventType>, init_state: StateType) {
|
|
24
|
+
this.rules = rules;
|
|
25
|
+
this.moveTo(init_state);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
protected moveTo(state: StateType) {
|
|
29
|
+
if (!this.rules.has(state)) throw new Error(`missing state "${state}"`);
|
|
30
|
+
|
|
31
|
+
this.currentState = state;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
getName() {
|
|
35
|
+
return this.currentState;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
change(event: EventType) {
|
|
39
|
+
const state = this.rules.get(this.currentState)!;
|
|
40
|
+
const next = state.get(event);
|
|
41
|
+
if (typeof next === 'undefined') {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`no event "${event} (${typeof event})" on state "${this.currentState} (${typeof this
|
|
44
|
+
.currentState})" (has ${[...state.keys()].map((v) => `"${v} (${typeof v})"`).join(', ')})`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const last = this.currentState;
|
|
49
|
+
this.moveTo(next);
|
|
50
|
+
this._onStateChange.fireNoError({ from: last, to: next, reason: event });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"file": "promisePool.cjs",
|
|
4
|
-
"sourceRoot": "",
|
|
5
|
-
"sources": [
|
|
6
|
-
"../../../src/promise/promisePool.ts"
|
|
7
|
-
],
|
|
8
|
-
"names": [],
|
|
9
|
-
"mappings": ";;;AAAA,4DAA4D;AAC5D,8EAAuE;AAEvE,MAAa,WAAW;IACJ,WAAW,GAAyC,EAAE,CAAC;IAE1E,IAAI;QACH,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC;IAC7C,CAAC;IAED,MAAM,CAAC,EAAU;QAChB,IAAI,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE;YACzB,MAAM,IAAI,KAAK,CAAC,4BAA4B,GAAG,EAAE,CAAC,CAAC;SACnD;QACD,MAAM,GAAG,GAAG,IAAI,iCAAe,EAAO,CAAC;QACvC,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC;QAC3B,OAAO,GAAG,CAAC,CAAC,CAAC;IACd,CAAC;IAED,GAAG,CAAC,EAAU;QACb,OAAO,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,IAAI,CAAC,EAAU,EAAE,IAAS;QACzB,IAAI,CAAC,WAAW,CAAC,EAAE,CAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,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