@idlebox/common 1.5.18 → 1.5.20
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/autoindex.d.ts +5 -2
- package/lib/autoindex.d.ts.map +1 -1
- package/lib/autoindex.js +7 -5
- package/lib/autoindex.js.map +1 -1
- package/lib/debugging/inspect.d.ts.map +1 -1
- package/lib/debugging/inspect.js +26 -3
- package/lib/debugging/inspect.js.map +1 -1
- package/lib/error/cause.d.ts +3 -0
- package/lib/error/cause.d.ts.map +1 -0
- package/lib/error/cause.js +22 -0
- package/lib/error/cause.js.map +1 -0
- package/lib/error/get-frame.d.ts +1 -1
- package/lib/error/get-frame.d.ts.map +1 -1
- package/lib/error/get-frame.js +11 -4
- package/lib/error/get-frame.js.map +1 -1
- package/lib/function/callback-list.js +2 -2
- package/lib/function/callback-list.js.map +1 -1
- package/lib/lifecycle/dispose/disposable.d.ts +6 -5
- package/lib/lifecycle/dispose/disposable.d.ts.map +1 -1
- package/lib/lifecycle/dispose/disposable.js +39 -33
- package/lib/lifecycle/dispose/disposable.js.map +1 -1
- package/lib/lifecycle/dispose/disposedError.d.ts +6 -6
- package/lib/lifecycle/dispose/disposedError.d.ts.map +1 -1
- package/lib/lifecycle/dispose/disposedError.js +32 -25
- package/lib/lifecycle/dispose/disposedError.js.map +1 -1
- package/lib/lifecycle/dispose/sync-disposable.d.ts.map +1 -1
- package/lib/lifecycle/dispose/sync-disposable.js +3 -2
- package/lib/lifecycle/dispose/sync-disposable.js.map +1 -1
- package/lib/log/logger.js +1 -1
- package/lib/log/logger.js.map +1 -1
- package/lib/platform/os.d.ts +1 -0
- package/lib/platform/os.d.ts.map +1 -1
- package/lib/platform/os.js +1 -0
- package/lib/platform/os.js.map +1 -1
- package/lib/typing-helper/deep.writable.d.ts +3 -0
- package/lib/typing-helper/deep.writable.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/autoindex.ts +9 -6
- package/src/debugging/inspect.ts +25 -3
- package/src/error/cause.ts +21 -0
- package/src/error/get-frame.ts +10 -4
- package/src/function/callback-list.ts +2 -2
- package/src/lifecycle/dispose/disposable.ts +45 -39
- package/src/lifecycle/dispose/disposedError.ts +33 -26
- package/src/lifecycle/dispose/sync-disposable.ts +3 -2
- package/src/log/logger.ts +1 -1
- package/src/platform/os.ts +2 -0
- package/src/typing-helper/deep.writable.ts +10 -0
- package/lib/error/known.d.ts +0 -14
- package/lib/error/known.d.ts.map +0 -1
- package/lib/error/known.js +0 -34
- package/lib/error/known.js.map +0 -1
- package/src/error/known.ts +0 -40
|
@@ -1,17 +1,17 @@
|
|
|
1
|
+
import { ErrorWithCode, ExitCode } from '@idlebox/errors';
|
|
1
2
|
import { tryInspect } from '../../debugging/inspect.js';
|
|
2
|
-
import { getErrorFrame } from '../../error/get-frame.js';
|
|
3
3
|
import { prettyFormatError } from '../../error/pretty.nodejs.js';
|
|
4
4
|
import type { StackTraceHolder } from '../../error/stack-trace.js';
|
|
5
|
-
import {
|
|
5
|
+
import { isV8 } from '../../platform/os.js';
|
|
6
6
|
import { dispose_name } from './debug.js';
|
|
7
7
|
|
|
8
|
-
export class DisposedError extends
|
|
8
|
+
export class DisposedError extends ErrorWithCode {
|
|
9
9
|
constructor(
|
|
10
10
|
message = 'Object has been disposed',
|
|
11
11
|
public readonly previous: StackTraceHolder,
|
|
12
|
+
boundary?: CallableFunction,
|
|
12
13
|
) {
|
|
13
|
-
super(message, { cause: previous });
|
|
14
|
-
this.name = 'DisposedError';
|
|
14
|
+
super(message, ExitCode.DUPLICATE, { cause: previous, boundary });
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
// override get stack() {
|
|
@@ -19,44 +19,51 @@ export class DisposedError extends Error {
|
|
|
19
19
|
// }
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
const lineStart = /^/gm;
|
|
23
|
+
|
|
22
24
|
/**
|
|
23
25
|
* Error when call dispose() twice
|
|
24
26
|
*/
|
|
25
|
-
export class
|
|
26
|
-
public readonly inspectString: string;
|
|
27
|
+
export class DuplicateDisposedError extends DisposedError {
|
|
27
28
|
constructor(
|
|
28
29
|
public readonly object: any,
|
|
29
30
|
previous: StackTraceHolder,
|
|
30
31
|
) {
|
|
31
32
|
const old = Error.stackTraceLimit;
|
|
32
33
|
Error.stackTraceLimit = Number.MAX_SAFE_INTEGER;
|
|
33
|
-
const stacks = getErrorFrame(previous, 2);
|
|
34
34
|
|
|
35
|
-
const
|
|
36
|
-
const name = dispose_name(object, inspectString);
|
|
35
|
+
const name = dispose_name(object, 'UnknownDisposable');
|
|
37
36
|
|
|
38
|
-
super(`Object [${name}] has already disposed
|
|
39
|
-
this.name = 'DuplicateDisposedError';
|
|
40
|
-
this.inspectString = inspectString;
|
|
37
|
+
super(`Object [${name}] has already disposed`, previous);
|
|
41
38
|
|
|
42
39
|
Error.stackTraceLimit = old;
|
|
43
|
-
|
|
44
|
-
this.tryCreateConsoleWarning().catch(() => {});
|
|
45
40
|
}
|
|
46
41
|
|
|
47
|
-
public
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
42
|
+
public consoleWarning() {
|
|
43
|
+
try {
|
|
44
|
+
if (isV8) {
|
|
45
|
+
console.error('\x1B[48;5;1m DisposedWarning \x1B[0m DUPLICATE DISPOSE');
|
|
46
|
+
const colorBlock = `\x1B[48;5;238m \x1B[0m `;
|
|
47
|
+
console.error('\x1B[48;5;14m ● \x1B[0;38;5;4m first dispose\x1B[0m');
|
|
48
|
+
console.error(prettyFormatError(this.previous, false).replace(lineStart, colorBlock));
|
|
49
|
+
console.error('\x1B[48;5;14m ● \x1B[0;38;5;4m current dispose\x1B[0m');
|
|
50
|
+
console.error(prettyFormatError(this, false).replace(lineStart, colorBlock));
|
|
51
|
+
console.error('\x1B[48;5;14m ● \x1B[0;38;5;4m the object\x1B[0m');
|
|
52
|
+
console.error(tryInspect(this.object, { colors: true }).replace(lineStart, colorBlock));
|
|
53
|
+
console.error('');
|
|
54
|
+
} else {
|
|
55
|
+
console.error('[DisposedWarning] DUPLICATE DISPOSE');
|
|
56
|
+
console.error(' * first dispose:%O', this.previous);
|
|
57
|
+
console.error(' * current dispose:%O', this);
|
|
58
|
+
console.error(' * the object: %O', this.object);
|
|
59
|
+
}
|
|
60
|
+
} catch (e) {
|
|
61
|
+
console.error('Failed to create console warning for duplicate dispose', e);
|
|
62
|
+
console.error('');
|
|
55
63
|
}
|
|
56
|
-
console.error(' * Object: %s', this.inspectString);
|
|
57
64
|
}
|
|
58
65
|
|
|
59
|
-
is(other: unknown): other is
|
|
60
|
-
return other instanceof
|
|
66
|
+
is(other: unknown): other is DuplicateDisposedError {
|
|
67
|
+
return other instanceof DuplicateDisposedError;
|
|
61
68
|
}
|
|
62
69
|
}
|
|
@@ -2,7 +2,7 @@ import { convertCaughtError } from '../../error/convert-unknown.js';
|
|
|
2
2
|
import { createStackTraceHolder, type StackTraceHolder } from '../../error/stack-trace.js';
|
|
3
3
|
import { dispose_name } from './debug.js';
|
|
4
4
|
import { AbstractEnhancedDisposable, type IDisposable } from './disposable.js';
|
|
5
|
-
import {
|
|
5
|
+
import { DuplicateDisposedError } from './disposedError.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* 简单版手动disposable
|
|
@@ -15,7 +15,8 @@ export abstract class DisposableOnce implements IDisposable {
|
|
|
15
15
|
}
|
|
16
16
|
public dispose(): void {
|
|
17
17
|
if (this._disposed) {
|
|
18
|
-
|
|
18
|
+
const w = new DuplicateDisposedError(this, this._disposed);
|
|
19
|
+
w.consoleWarning();
|
|
19
20
|
return;
|
|
20
21
|
}
|
|
21
22
|
this._disposed = createStackTraceHolder('disposed', this.dispose);
|
package/src/log/logger.ts
CHANGED
|
@@ -121,7 +121,7 @@ export abstract class WrappedConsole {
|
|
|
121
121
|
return;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
args.splice(pos, 0, (isWeb ? ' %o' : ' %j').repeat(args.length - pos).
|
|
124
|
+
args.splice(pos, 0, (isWeb ? ' %o' : ' %j').repeat(args.length - pos).slice(1));
|
|
125
125
|
} else {
|
|
126
126
|
args[pos] = '';
|
|
127
127
|
}
|
package/src/platform/os.ts
CHANGED
|
@@ -59,5 +59,7 @@ if (hasWindow && !hasProcess) {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
export const isV8 = (isNative && typeof process.versions?.v8 === 'string') || (() => new Error('test').stack?.includes(' at '))() || false;
|
|
63
|
+
|
|
62
64
|
export const sepList = isWindows ? ';' : ':';
|
|
63
65
|
export const is32Bit = !is64Bit;
|
|
@@ -14,3 +14,13 @@ type DeepWriteableArray<T> = Array<DeepWriteable<T>>;
|
|
|
14
14
|
type DeepWriteableMap<K, V> = Map<DeepWriteable<K>, DeepWriteable<V>>;
|
|
15
15
|
type DeepWriteableSet<T> = Set<DeepWriteable<T>>;
|
|
16
16
|
type DeepWriteableObject<T> = { -readonly [K in keyof T]: DeepWriteable<T[K]> };
|
|
17
|
+
|
|
18
|
+
export type Writeable<T> = T extends Primitive
|
|
19
|
+
? T
|
|
20
|
+
: T extends ReadonlyArray<infer U>
|
|
21
|
+
? Array<U>
|
|
22
|
+
: T extends ReadonlyMap<infer K, infer V>
|
|
23
|
+
? Map<K, V>
|
|
24
|
+
: T extends ReadonlySet<infer M>
|
|
25
|
+
? Set<M>
|
|
26
|
+
: { -readonly [K in keyof T]: T[K] };
|
package/lib/error/known.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
interface ErrorOptions {
|
|
2
|
-
cause?: unknown;
|
|
3
|
-
}
|
|
4
|
-
declare abstract class KnownErrorAbstract extends Error {
|
|
5
|
-
protected static debugMode: boolean;
|
|
6
|
-
static debug(enabled?: boolean): void;
|
|
7
|
-
static is(e: any): e is KnownErrorAbstract;
|
|
8
|
-
}
|
|
9
|
-
declare class KnownErrorNode extends KnownErrorAbstract {
|
|
10
|
-
constructor(message: string, consopt?: ErrorOptions);
|
|
11
|
-
}
|
|
12
|
-
export declare const KnownError: typeof KnownErrorNode;
|
|
13
|
-
export {};
|
|
14
|
-
//# sourceMappingURL=known.d.ts.map
|
package/lib/error/known.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"known.d.ts","sourceRoot":"","sources":["../../src/error/known.ts"],"names":[],"mappings":"AAEA,UAAU,YAAY;IACrB,KAAK,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,uBAAe,kBAAmB,SAAQ,KAAK;IAC9C,SAAS,CAAC,MAAM,CAAC,SAAS,UAAS;IACnC,MAAM,CAAC,KAAK,CAAC,OAAO,UAAO;IAG3B,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,IAAI,kBAAkB;CAG1C;AAED,cAAM,cAAe,SAAQ,kBAAkB;gBAClC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAWnD;AAWD,eAAO,MAAM,UAAU,uBAA+C,CAAC"}
|
package/lib/error/known.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { isNative } from '../platform/os.js';
|
|
2
|
-
class KnownErrorAbstract extends Error {
|
|
3
|
-
static debugMode = false;
|
|
4
|
-
static debug(enabled = true) {
|
|
5
|
-
KnownErrorAbstract.debugMode = enabled;
|
|
6
|
-
}
|
|
7
|
-
static is(e) {
|
|
8
|
-
return e instanceof KnownErrorAbstract;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
class KnownErrorNode extends KnownErrorAbstract {
|
|
12
|
-
constructor(message, consopt) {
|
|
13
|
-
if (KnownErrorAbstract.debugMode) {
|
|
14
|
-
super(message, consopt);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
const limit = Error.stackTraceLimit;
|
|
18
|
-
Error.stackTraceLimit = 0;
|
|
19
|
-
super(message, consopt);
|
|
20
|
-
Error.stackTraceLimit = limit;
|
|
21
|
-
this.stack = message;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
class KnownErrorCommon extends KnownErrorAbstract {
|
|
26
|
-
constructor(message, consopt) {
|
|
27
|
-
super(message, consopt);
|
|
28
|
-
if (!KnownErrorAbstract.debugMode) {
|
|
29
|
-
this.stack = message;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
export const KnownError = isNative ? KnownErrorNode : KnownErrorCommon;
|
|
34
|
-
//# sourceMappingURL=known.js.map
|
package/lib/error/known.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"known.js","sourceRoot":"","sources":["../../src/error/known.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAM7C,MAAe,kBAAmB,SAAQ,KAAK;IACpC,MAAM,CAAC,SAAS,GAAG,KAAK,CAAC;IACnC,MAAM,CAAC,KAAK,CAAC,OAAO,GAAG,IAAI;QAC1B,kBAAkB,CAAC,SAAS,GAAG,OAAO,CAAC;IACxC,CAAC;IACD,MAAM,CAAC,EAAE,CAAC,CAAM;QACf,OAAO,CAAC,YAAY,kBAAkB,CAAC;IACxC,CAAC;;AAGF,MAAM,cAAe,SAAQ,kBAAkB;IAC9C,YAAY,OAAe,EAAE,OAAsB;QAClD,IAAI,kBAAkB,CAAC,SAAS,EAAE,CAAC;YAClC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACzB,CAAC;aAAM,CAAC;YACP,MAAM,KAAK,GAAI,KAAa,CAAC,eAAe,CAAC;YAC5C,KAAa,CAAC,eAAe,GAAG,CAAC,CAAC;YACnC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YACvB,KAAa,CAAC,eAAe,GAAG,KAAK,CAAC;YACvC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC;QACtB,CAAC;IACF,CAAC;CACD;AAED,MAAM,gBAAiB,SAAQ,kBAAkB;IAChD,YAAY,OAAe,EAAE,OAAsB;QAClD,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,kBAAkB,CAAC,SAAS,EAAE,CAAC;YACnC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC;QACtB,CAAC;IACF,CAAC;CACD;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,QAAQ,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,gBAAgB,CAAC"}
|
package/src/error/known.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { isNative } from '../platform/os.js';
|
|
2
|
-
|
|
3
|
-
interface ErrorOptions {
|
|
4
|
-
cause?: unknown;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
abstract class KnownErrorAbstract extends Error {
|
|
8
|
-
protected static debugMode = false;
|
|
9
|
-
static debug(enabled = true) {
|
|
10
|
-
KnownErrorAbstract.debugMode = enabled;
|
|
11
|
-
}
|
|
12
|
-
static is(e: any): e is KnownErrorAbstract {
|
|
13
|
-
return e instanceof KnownErrorAbstract;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
class KnownErrorNode extends KnownErrorAbstract {
|
|
18
|
-
constructor(message: string, consopt?: ErrorOptions) {
|
|
19
|
-
if (KnownErrorAbstract.debugMode) {
|
|
20
|
-
super(message, consopt);
|
|
21
|
-
} else {
|
|
22
|
-
const limit = (Error as any).stackTraceLimit;
|
|
23
|
-
(Error as any).stackTraceLimit = 0;
|
|
24
|
-
super(message, consopt);
|
|
25
|
-
(Error as any).stackTraceLimit = limit;
|
|
26
|
-
this.stack = message;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
class KnownErrorCommon extends KnownErrorAbstract {
|
|
32
|
-
constructor(message: string, consopt?: ErrorOptions) {
|
|
33
|
-
super(message, consopt);
|
|
34
|
-
if (!KnownErrorAbstract.debugMode) {
|
|
35
|
-
this.stack = message;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export const KnownError = isNative ? KnownErrorNode : KnownErrorCommon;
|