@vltpkg/output 1.0.0-rc.22 → 1.0.0-rc.24
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/error.d.ts +27 -0
- package/dist/error.js +45 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +23 -0
- package/package.json +5 -5
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type ErrorCause = Record<string, unknown> & {
|
|
2
|
+
cause?: ErrorCause;
|
|
3
|
+
};
|
|
4
|
+
export type ParsedError = {
|
|
5
|
+
name: string;
|
|
6
|
+
message: string;
|
|
7
|
+
stack?: string;
|
|
8
|
+
cause?: ErrorCause | ParsedError;
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Parse an unknown value into an error. Will also take enumerable properties
|
|
13
|
+
* from legacy style Node errors and treat them as the cause.
|
|
14
|
+
*/
|
|
15
|
+
export declare const parseError: (err: unknown) => null | ParsedError;
|
|
16
|
+
/**
|
|
17
|
+
* Find the root error from an unknown value, optionally matching a code.
|
|
18
|
+
*/
|
|
19
|
+
export declare const findRootError: (v: unknown, match?: {
|
|
20
|
+
code?: string;
|
|
21
|
+
}) => ParsedError | null;
|
|
22
|
+
/**
|
|
23
|
+
* Find the root error from an unknown value and throw if one is not found.
|
|
24
|
+
*/
|
|
25
|
+
export declare const asRootError: (v: unknown, match?: {
|
|
26
|
+
code?: string;
|
|
27
|
+
}) => Omit<ParsedError, "cause"> & Required<Pick<ParsedError, "cause">>;
|
package/dist/error.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { isError, isObject } from '@vltpkg/types';
|
|
2
|
+
/**
|
|
3
|
+
* Parse an unknown value into an error. Will also take enumerable properties
|
|
4
|
+
* from legacy style Node errors and treat them as the cause.
|
|
5
|
+
*/
|
|
6
|
+
export const parseError = (err) => {
|
|
7
|
+
if (!isError(err)) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
// Assume that if the error has enumerable properties, then
|
|
11
|
+
// it is a legacy error and does not also have a cause.
|
|
12
|
+
const enumerable = Object.fromEntries(Object.entries(err));
|
|
13
|
+
if (Object.keys(enumerable).length) {
|
|
14
|
+
err.cause = enumerable;
|
|
15
|
+
return err;
|
|
16
|
+
}
|
|
17
|
+
if (isObject(err.cause)) {
|
|
18
|
+
if (isError(err.cause.cause)) {
|
|
19
|
+
err.cause.cause = parseError(err.cause.cause);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
else if (isError(err.cause)) {
|
|
23
|
+
err.cause = parseError(err.cause);
|
|
24
|
+
}
|
|
25
|
+
return err;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Find the root error from an unknown value, optionally matching a code.
|
|
29
|
+
*/
|
|
30
|
+
export const findRootError = (v, match) => {
|
|
31
|
+
const err = parseError(v);
|
|
32
|
+
return !err || (match?.code && err.cause?.code !== match.code) ?
|
|
33
|
+
null
|
|
34
|
+
: err;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Find the root error from an unknown value and throw if one is not found.
|
|
38
|
+
*/
|
|
39
|
+
export const asRootError = (v, match) => {
|
|
40
|
+
const err = findRootError(v, match);
|
|
41
|
+
if (!err)
|
|
42
|
+
throw v;
|
|
43
|
+
err.cause = err.cause ?? {};
|
|
44
|
+
return err;
|
|
45
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type Events = {
|
|
2
|
+
request: {
|
|
3
|
+
url: URL | string;
|
|
4
|
+
state: 'start' | 'complete' | '304' | 'cache' | 'stale';
|
|
5
|
+
};
|
|
6
|
+
graphStep: {
|
|
7
|
+
step: 'build' | 'actual' | 'reify';
|
|
8
|
+
state: 'start' | 'stop';
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export declare class OutputEmitter {
|
|
12
|
+
private emitter;
|
|
13
|
+
emit<T extends keyof Events>(eventName: T, payload: Events[T]): void;
|
|
14
|
+
on<T extends keyof Events>(eventName: T, handler: (payload: Events[T]) => void): void;
|
|
15
|
+
off<T extends keyof Events>(eventName: T, handler: (payload: Events[T]) => void): void;
|
|
16
|
+
}
|
|
17
|
+
export declare const emitter: OutputEmitter;
|
|
18
|
+
export declare const logRequest: (url: URL | string, state: Events["request"]["state"]) => void;
|
|
19
|
+
export declare const graphStep: (step: Events["graphStep"]["step"]) => () => void;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import EventEmitter from 'node:events';
|
|
2
|
+
export class OutputEmitter {
|
|
3
|
+
emitter = new EventEmitter();
|
|
4
|
+
emit(eventName, payload) {
|
|
5
|
+
this.emitter.emit(eventName, payload);
|
|
6
|
+
}
|
|
7
|
+
on(eventName, handler) {
|
|
8
|
+
this.emitter.on(eventName, handler);
|
|
9
|
+
}
|
|
10
|
+
off(eventName, handler) {
|
|
11
|
+
this.emitter.off(eventName, handler);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
// Exported event emitter to coordinate events.
|
|
15
|
+
// There must be one version of output across the entire CLI.
|
|
16
|
+
export const emitter = new OutputEmitter();
|
|
17
|
+
export const logRequest = (url, state) => {
|
|
18
|
+
emitter.emit('request', { url, state });
|
|
19
|
+
};
|
|
20
|
+
export const graphStep = (step) => {
|
|
21
|
+
emitter.emit('graphStep', { step, state: 'start' });
|
|
22
|
+
return () => emitter.emit('graphStep', { step, state: 'stop' });
|
|
23
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vltpkg/output",
|
|
3
3
|
"description": "Generate output from the vlt CLI",
|
|
4
|
-
"version": "1.0.0-rc.
|
|
4
|
+
"version": "1.0.0-rc.24",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/vltpkg/vltpkg.git",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"email": "support@vlt.sh"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@vltpkg/types": "1.0.0-rc.
|
|
15
|
+
"@vltpkg/types": "1.0.0-rc.24"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@eslint/js": "^9.39.1",
|
|
@@ -43,18 +43,18 @@
|
|
|
43
43
|
"extends": "../../tap-config.yaml"
|
|
44
44
|
},
|
|
45
45
|
"prettier": "../../.prettierrc.js",
|
|
46
|
-
"module": "./
|
|
46
|
+
"module": "./dist/index.js",
|
|
47
47
|
"type": "module",
|
|
48
48
|
"exports": {
|
|
49
49
|
"./package.json": "./package.json",
|
|
50
50
|
".": {
|
|
51
51
|
"import": {
|
|
52
|
-
"default": "./
|
|
52
|
+
"default": "./dist/index.js"
|
|
53
53
|
}
|
|
54
54
|
},
|
|
55
55
|
"./error": {
|
|
56
56
|
"import": {
|
|
57
|
-
"default": "./
|
|
57
|
+
"default": "./dist/error.js"
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
},
|