addons-scanner-utils 15.3.0 → 15.4.0
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/functions/api.d.ts +18 -0
- package/dist/functions/api.js +25 -3
- package/dist/functions/download.js +4 -1
- package/dist/functions/error.d.ts +13 -13
- package/dist/functions/error.js +20 -6
- package/dist/functions/index.d.ts +15 -1
- package/dist/functions/index.js +28 -14
- package/dist/functions/request-context.d.ts +36 -0
- package/dist/functions/request-context.js +45 -0
- package/package.json +2 -2
package/dist/functions/api.d.ts
CHANGED
|
@@ -9,6 +9,16 @@ export type PatchScannerResultParams = MakeJWTConfig & {
|
|
|
9
9
|
url: string;
|
|
10
10
|
payload: Record<string, unknown>;
|
|
11
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* Parameters for posting a scanner result.
|
|
14
|
+
*
|
|
15
|
+
* @property url - URL to POST
|
|
16
|
+
* @property payload - JSON body to send
|
|
17
|
+
*/
|
|
18
|
+
export type PostScannerResultParams = MakeJWTConfig & {
|
|
19
|
+
url: string;
|
|
20
|
+
payload: Record<string, unknown>;
|
|
21
|
+
};
|
|
12
22
|
/**
|
|
13
23
|
* PATCH a scanner result to the AMO API, authenticating with a JWT token
|
|
14
24
|
* computed from the `AMO_JWT_ISS_KEY` and `AMO_JWT_SECRET` environment
|
|
@@ -17,3 +27,11 @@ export type PatchScannerResultParams = MakeJWTConfig & {
|
|
|
17
27
|
* @throws AMOError if the response is not ok
|
|
18
28
|
*/
|
|
19
29
|
export declare const patchScannerResult: ({ url, payload, env, }: PatchScannerResultParams) => Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* POST a scanner result to the AMO API, authenticating with a JWT token
|
|
32
|
+
* computed from the `AMO_JWT_ISS_KEY` and `AMO_JWT_SECRET` environment
|
|
33
|
+
* variables.
|
|
34
|
+
*
|
|
35
|
+
* @throws AMOError if the response is not ok
|
|
36
|
+
*/
|
|
37
|
+
export declare const postScannerResult: ({ url, payload, env, }: PostScannerResultParams) => Promise<void>;
|
package/dist/functions/api.js
CHANGED
|
@@ -9,9 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.patchScannerResult = void 0;
|
|
12
|
+
exports.postScannerResult = exports.patchScannerResult = void 0;
|
|
13
13
|
const error_1 = require("./error");
|
|
14
14
|
const auth_1 = require("./auth");
|
|
15
|
+
const request_context_1 = require("./request-context");
|
|
15
16
|
/**
|
|
16
17
|
* PATCH a scanner result to the AMO API, authenticating with a JWT token
|
|
17
18
|
* computed from the `AMO_JWT_ISS_KEY` and `AMO_JWT_SECRET` environment
|
|
@@ -22,10 +23,10 @@ const auth_1 = require("./auth");
|
|
|
22
23
|
const patchScannerResult = (_a) => __awaiter(void 0, [_a], void 0, function* ({ url, payload, env, }) {
|
|
23
24
|
const response = yield fetch(url, {
|
|
24
25
|
method: 'PATCH',
|
|
25
|
-
headers: {
|
|
26
|
+
headers: (0, request_context_1.withRequestIdHeader)({
|
|
26
27
|
'Content-Type': 'application/json',
|
|
27
28
|
Authorization: `JWT ${(0, auth_1.makeJWT)({ env })}`,
|
|
28
|
-
},
|
|
29
|
+
}),
|
|
29
30
|
body: JSON.stringify(payload),
|
|
30
31
|
});
|
|
31
32
|
if (!response.ok) {
|
|
@@ -33,3 +34,24 @@ const patchScannerResult = (_a) => __awaiter(void 0, [_a], void 0, function* ({
|
|
|
33
34
|
}
|
|
34
35
|
});
|
|
35
36
|
exports.patchScannerResult = patchScannerResult;
|
|
37
|
+
/**
|
|
38
|
+
* POST a scanner result to the AMO API, authenticating with a JWT token
|
|
39
|
+
* computed from the `AMO_JWT_ISS_KEY` and `AMO_JWT_SECRET` environment
|
|
40
|
+
* variables.
|
|
41
|
+
*
|
|
42
|
+
* @throws AMOError if the response is not ok
|
|
43
|
+
*/
|
|
44
|
+
const postScannerResult = (_a) => __awaiter(void 0, [_a], void 0, function* ({ url, payload, env, }) {
|
|
45
|
+
const response = yield fetch(url, {
|
|
46
|
+
method: 'POST',
|
|
47
|
+
headers: (0, request_context_1.withRequestIdHeader)({
|
|
48
|
+
'Content-Type': 'application/json',
|
|
49
|
+
Authorization: `JWT ${(0, auth_1.makeJWT)({ env })}`,
|
|
50
|
+
}),
|
|
51
|
+
body: JSON.stringify(payload),
|
|
52
|
+
});
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
throw new error_1.AMOError(`unexpected response: ${response.statusText}`);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
exports.postScannerResult = postScannerResult;
|
|
@@ -20,6 +20,7 @@ const node_stream_1 = require("node:stream");
|
|
|
20
20
|
const promises_1 = require("node:stream/promises");
|
|
21
21
|
const error_1 = require("./error");
|
|
22
22
|
const auth_1 = require("./auth");
|
|
23
|
+
const request_context_1 = require("./request-context");
|
|
23
24
|
exports.DEFAULT_DOWNLOAD_FILENAME = 'input.xpi';
|
|
24
25
|
/**
|
|
25
26
|
* Error thrown when file download operations fail.
|
|
@@ -50,7 +51,9 @@ class DownloadFileError extends Error {
|
|
|
50
51
|
exports.DownloadFileError = DownloadFileError;
|
|
51
52
|
const fetchToFile = (downloadURL, tmpDir, filename, headers) => __awaiter(void 0, void 0, void 0, function* () {
|
|
52
53
|
try {
|
|
53
|
-
const response = yield fetch(downloadURL, {
|
|
54
|
+
const response = yield fetch(downloadURL, {
|
|
55
|
+
headers: (0, request_context_1.withRequestIdHeader)(headers),
|
|
56
|
+
});
|
|
54
57
|
if (!response.ok || !response.body) {
|
|
55
58
|
throw new DownloadFileError(`unexpected response: ${response.statusText}`);
|
|
56
59
|
}
|
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Error object with HTTP status code and optional extra information intended
|
|
3
|
-
* to be used by the error handler defined in `src/functions/index.ts`.
|
|
4
|
-
*
|
|
5
|
-
* @property message - Error message
|
|
6
|
-
* @property extraInfo - Additional error details
|
|
7
|
-
* @property status - HTTP status code
|
|
8
|
-
*/
|
|
9
|
-
export type AppError = Error & {
|
|
10
|
-
extraInfo?: string;
|
|
11
|
-
status: number;
|
|
12
|
-
};
|
|
13
1
|
/**
|
|
14
2
|
* Parameters for creating an app error.
|
|
15
3
|
*
|
|
@@ -22,10 +10,22 @@ export type CreateAppErrorParams = {
|
|
|
22
10
|
extraInfo?: string;
|
|
23
11
|
status?: number;
|
|
24
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* Error with an HTTP status code and optional extra information intended to be
|
|
15
|
+
* used by the error handler defined in `src/functions/index.ts`.
|
|
16
|
+
*
|
|
17
|
+
* @property extraInfo - Additional error details
|
|
18
|
+
* @property status - HTTP status code
|
|
19
|
+
*/
|
|
20
|
+
export declare class AppError extends Error {
|
|
21
|
+
extraInfo?: string;
|
|
22
|
+
status: number;
|
|
23
|
+
constructor({ message, extraInfo, status }: CreateAppErrorParams);
|
|
24
|
+
}
|
|
25
25
|
/**
|
|
26
26
|
* Create an app error object with status code and extra information.
|
|
27
27
|
*/
|
|
28
|
-
export declare const createAppError: (
|
|
28
|
+
export declare const createAppError: (params: CreateAppErrorParams) => AppError;
|
|
29
29
|
/**
|
|
30
30
|
* Error object used when interactions with the AMO API led to errors.
|
|
31
31
|
*/
|
package/dist/functions/error.js
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* eslint-disable max-classes-per-file */
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AMOError = exports.createAppError = void 0;
|
|
4
|
+
exports.AMOError = exports.createAppError = exports.AppError = void 0;
|
|
5
|
+
/**
|
|
6
|
+
* Error with an HTTP status code and optional extra information intended to be
|
|
7
|
+
* used by the error handler defined in `src/functions/index.ts`.
|
|
8
|
+
*
|
|
9
|
+
* @property extraInfo - Additional error details
|
|
10
|
+
* @property status - HTTP status code
|
|
11
|
+
*/
|
|
12
|
+
class AppError extends Error {
|
|
13
|
+
constructor({ message, extraInfo, status = 500 }) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = 'AppError';
|
|
16
|
+
this.status = status;
|
|
17
|
+
this.extraInfo = extraInfo;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.AppError = AppError;
|
|
4
21
|
/**
|
|
5
22
|
* Create an app error object with status code and extra information.
|
|
6
23
|
*/
|
|
7
|
-
const createAppError = (
|
|
8
|
-
|
|
9
|
-
error.status = status;
|
|
10
|
-
error.extraInfo = extraInfo;
|
|
11
|
-
return error;
|
|
24
|
+
const createAppError = (params) => {
|
|
25
|
+
return new AppError(params);
|
|
12
26
|
};
|
|
13
27
|
exports.createAppError = createAppError;
|
|
14
28
|
/**
|
|
@@ -1,13 +1,26 @@
|
|
|
1
|
-
import { Request, RequestHandler } from 'express';
|
|
1
|
+
import { ErrorRequestHandler, Request, RequestHandler } from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Express error handling middleware that serializes an {@link AppError} to a
|
|
4
|
+
* JSON response and logs it.
|
|
5
|
+
*
|
|
6
|
+
* @param _console - Console object for logging (for testing purposes)
|
|
7
|
+
*/
|
|
8
|
+
export declare const createErrorHandler: ({ _console, }?: {
|
|
9
|
+
_console?: typeof console;
|
|
10
|
+
}) => ErrorRequestHandler;
|
|
2
11
|
/**
|
|
3
12
|
* Extended Express request type with additional properties added by the
|
|
4
13
|
* middleware.
|
|
5
14
|
*
|
|
6
15
|
* @property rawBody - Raw request body buffer used for HMAC signature
|
|
7
16
|
* verification
|
|
17
|
+
* @property amoRequestId - Value of the incoming `X-AMO-Request-ID` header, or
|
|
18
|
+
* a generated UUID when the header is absent. Exposed for convenience (e.g.
|
|
19
|
+
* logging) in addition to being stored in the request context.
|
|
8
20
|
*/
|
|
9
21
|
export type RequestWithExtraProps = Request & {
|
|
10
22
|
rawBody?: Buffer;
|
|
23
|
+
amoRequestId?: string;
|
|
11
24
|
};
|
|
12
25
|
/**
|
|
13
26
|
* Configuration options for creating an Express application.
|
|
@@ -53,3 +66,4 @@ export * from './api';
|
|
|
53
66
|
export * from './auth';
|
|
54
67
|
export * from './download';
|
|
55
68
|
export * from './error';
|
|
69
|
+
export * from './request-context';
|
package/dist/functions/index.js
CHANGED
|
@@ -26,11 +26,33 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.createExpressApp = void 0;
|
|
29
|
+
exports.createExpressApp = exports.createErrorHandler = void 0;
|
|
30
30
|
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
31
31
|
const express_1 = __importDefault(require("express"));
|
|
32
32
|
const safe_compare_1 = __importDefault(require("safe-compare"));
|
|
33
33
|
const error_1 = require("./error");
|
|
34
|
+
const request_context_1 = require("./request-context");
|
|
35
|
+
/**
|
|
36
|
+
* Express error handling middleware that serializes an {@link AppError} to a
|
|
37
|
+
* JSON response and logs it.
|
|
38
|
+
*
|
|
39
|
+
* @param _console - Console object for logging (for testing purposes)
|
|
40
|
+
*/
|
|
41
|
+
const createErrorHandler = ({ _console = console, } = {}) =>
|
|
42
|
+
// Even though we are not using `next`, it must be kept because the Express
|
|
43
|
+
// error handler signature requires 4 arguments.
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
45
|
+
(err, req, res, next) => {
|
|
46
|
+
const error = {
|
|
47
|
+
error: err.message,
|
|
48
|
+
extra_info: err.extraInfo || null,
|
|
49
|
+
};
|
|
50
|
+
res.status(err.status || 500).json(error);
|
|
51
|
+
// Also send the error to the cloud provider, including the request ID for
|
|
52
|
+
// traceability when available.
|
|
53
|
+
_console.error(Object.assign(Object.assign({}, error), { request_id: req.amoRequestId || null }));
|
|
54
|
+
};
|
|
55
|
+
exports.createErrorHandler = createErrorHandler;
|
|
34
56
|
/**
|
|
35
57
|
* Easily create a new scanner application with built-in authentication
|
|
36
58
|
* middleware. The middleware validates that the content-type is
|
|
@@ -72,6 +94,8 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
|
|
|
72
94
|
};
|
|
73
95
|
// Parse JSON body requests.
|
|
74
96
|
app.use(express_1.default.json(jsonOptions));
|
|
97
|
+
// Capture the request ID and make it available for the rest of the chain.
|
|
98
|
+
app.use(request_context_1.requestIdMiddleware);
|
|
75
99
|
// Authentication middleware plus some extra checks.
|
|
76
100
|
app.use((req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
77
101
|
var _a;
|
|
@@ -136,19 +160,8 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
|
|
|
136
160
|
app.use((req, res, next) => {
|
|
137
161
|
next((0, error_1.createAppError)({ message: 'not found', status: 404 }));
|
|
138
162
|
});
|
|
139
|
-
// Error handler.
|
|
140
|
-
|
|
141
|
-
app.use(
|
|
142
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
143
|
-
(err, req, res, next) => {
|
|
144
|
-
const error = {
|
|
145
|
-
error: err.message,
|
|
146
|
-
extra_info: err.extraInfo || null,
|
|
147
|
-
};
|
|
148
|
-
res.status(err.status || 500).json(error);
|
|
149
|
-
// Also send the error to the cloud provider.
|
|
150
|
-
_console.error(error);
|
|
151
|
-
});
|
|
163
|
+
// Error handler.
|
|
164
|
+
app.use((0, exports.createErrorHandler)({ _console }));
|
|
152
165
|
return app;
|
|
153
166
|
};
|
|
154
167
|
exports.createExpressApp = createExpressApp;
|
|
@@ -156,3 +169,4 @@ __exportStar(require("./api"), exports);
|
|
|
156
169
|
__exportStar(require("./auth"), exports);
|
|
157
170
|
__exportStar(require("./download"), exports);
|
|
158
171
|
__exportStar(require("./error"), exports);
|
|
172
|
+
__exportStar(require("./request-context"), exports);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { RequestHandler } from 'express';
|
|
3
|
+
/**
|
|
4
|
+
* Name of the header carrying the unique request ID issued by AMO.
|
|
5
|
+
*/
|
|
6
|
+
export declare const AMO_REQUEST_ID_HEADER = "X-AMO-Request-ID";
|
|
7
|
+
/**
|
|
8
|
+
* Data kept around for the duration of a single request.
|
|
9
|
+
*
|
|
10
|
+
* @property requestId - The value of the incoming `X-AMO-Request-ID` header, or
|
|
11
|
+
* a generated UUID when the header is absent.
|
|
12
|
+
*/
|
|
13
|
+
export type RequestContext = {
|
|
14
|
+
requestId?: string;
|
|
15
|
+
};
|
|
16
|
+
export declare const requestContextStorage: AsyncLocalStorage<RequestContext>;
|
|
17
|
+
/**
|
|
18
|
+
* Return the `X-AMO-Request-ID` of the request currently being handled, or
|
|
19
|
+
* `undefined` when there is no active request context (e.g. outside of the
|
|
20
|
+
* Express middleware chain).
|
|
21
|
+
*/
|
|
22
|
+
export declare const getRequestId: () => string | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Return a copy of `headers` with the `X-AMO-Request-ID` header added when a
|
|
25
|
+
* request ID is available in the current context. When no request ID is in
|
|
26
|
+
* scope, `headers` is returned unchanged.
|
|
27
|
+
*/
|
|
28
|
+
export declare const withRequestIdHeader: (headers?: Record<string, string>) => Record<string, string>;
|
|
29
|
+
/**
|
|
30
|
+
* Express middleware that captures the AMO request ID from the incoming
|
|
31
|
+
* `X-AMO-Request-ID` header (generating a UUID when it is absent), exposes it
|
|
32
|
+
* on `req.amoRequestId`, and runs the rest of the middleware chain inside a
|
|
33
|
+
* request context so the ID stays available for the duration of the request
|
|
34
|
+
* and can be echoed back on every outgoing AMO API call.
|
|
35
|
+
*/
|
|
36
|
+
export declare const requestIdMiddleware: RequestHandler;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.requestIdMiddleware = exports.withRequestIdHeader = exports.getRequestId = exports.requestContextStorage = exports.AMO_REQUEST_ID_HEADER = void 0;
|
|
7
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
8
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
9
|
+
/**
|
|
10
|
+
* Name of the header carrying the unique request ID issued by AMO.
|
|
11
|
+
*/
|
|
12
|
+
exports.AMO_REQUEST_ID_HEADER = 'X-AMO-Request-ID';
|
|
13
|
+
exports.requestContextStorage = new node_async_hooks_1.AsyncLocalStorage();
|
|
14
|
+
/**
|
|
15
|
+
* Return the `X-AMO-Request-ID` of the request currently being handled, or
|
|
16
|
+
* `undefined` when there is no active request context (e.g. outside of the
|
|
17
|
+
* Express middleware chain).
|
|
18
|
+
*/
|
|
19
|
+
const getRequestId = () => { var _a; return (_a = exports.requestContextStorage.getStore()) === null || _a === void 0 ? void 0 : _a.requestId; };
|
|
20
|
+
exports.getRequestId = getRequestId;
|
|
21
|
+
/**
|
|
22
|
+
* Return a copy of `headers` with the `X-AMO-Request-ID` header added when a
|
|
23
|
+
* request ID is available in the current context. When no request ID is in
|
|
24
|
+
* scope, `headers` is returned unchanged.
|
|
25
|
+
*/
|
|
26
|
+
const withRequestIdHeader = (headers = {}) => {
|
|
27
|
+
const requestId = (0, exports.getRequestId)();
|
|
28
|
+
return requestId
|
|
29
|
+
? Object.assign(Object.assign({}, headers), { [exports.AMO_REQUEST_ID_HEADER]: requestId }) : headers;
|
|
30
|
+
};
|
|
31
|
+
exports.withRequestIdHeader = withRequestIdHeader;
|
|
32
|
+
/**
|
|
33
|
+
* Express middleware that captures the AMO request ID from the incoming
|
|
34
|
+
* `X-AMO-Request-ID` header (generating a UUID when it is absent), exposes it
|
|
35
|
+
* on `req.amoRequestId`, and runs the rest of the middleware chain inside a
|
|
36
|
+
* request context so the ID stays available for the duration of the request
|
|
37
|
+
* and can be echoed back on every outgoing AMO API call.
|
|
38
|
+
*/
|
|
39
|
+
const requestIdMiddleware = (req, res, next) => {
|
|
40
|
+
var _a;
|
|
41
|
+
const requestId = (_a = req.get(exports.AMO_REQUEST_ID_HEADER)) !== null && _a !== void 0 ? _a : node_crypto_1.default.randomUUID();
|
|
42
|
+
req.amoRequestId = requestId;
|
|
43
|
+
exports.requestContextStorage.run({ requestId }, () => next());
|
|
44
|
+
};
|
|
45
|
+
exports.requestIdMiddleware = requestIdMiddleware;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "addons-scanner-utils",
|
|
3
|
-
"version": "15.
|
|
3
|
+
"version": "15.4.0",
|
|
4
4
|
"description": "Various addons related helpers to build CLIs.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
"eslint-plugin-amo": "^3.0.0",
|
|
73
73
|
"express": "5.2.1",
|
|
74
74
|
"jest": "^30.2.0",
|
|
75
|
-
"prettier": "3.8.
|
|
75
|
+
"prettier": "3.8.4",
|
|
76
76
|
"pretty-quick": "4.2.2",
|
|
77
77
|
"rimraf": "^6.1.0",
|
|
78
78
|
"safe-compare": "1.1.4",
|