addons-scanner-utils 13.1.0 → 14.0.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 +12 -20
- package/dist/functions/api.js +31 -9
- package/dist/functions/download.d.ts +5 -3
- package/dist/functions/download.js +7 -5
- package/dist/functions/error.d.ts +34 -0
- package/dist/functions/error.js +23 -0
- package/dist/functions/index.d.ts +1 -0
- package/dist/functions/index.js +11 -11
- package/package.json +1 -6
package/dist/functions/api.d.ts
CHANGED
|
@@ -1,27 +1,19 @@
|
|
|
1
|
+
import { MakeJWTConfig } from './auth';
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
+
* Parameters for patching a scanner result.
|
|
3
4
|
*
|
|
4
|
-
* @property
|
|
5
|
-
* @property
|
|
6
|
-
* @property status - HTTP status code
|
|
5
|
+
* @property url - URL to PATCH
|
|
6
|
+
* @property payload - JSON body to send
|
|
7
7
|
*/
|
|
8
|
-
export type
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
export type PatchScannerResultParams = MakeJWTConfig & {
|
|
9
|
+
url: string;
|
|
10
|
+
payload: Record<string, unknown>;
|
|
11
11
|
};
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* PATCH a scanner result to the AMO API, authenticating with a JWT token
|
|
14
|
+
* computed from the `AMO_JWT_ISS_KEY` and `AMO_JWT_SECRET` environment
|
|
15
|
+
* variables.
|
|
14
16
|
*
|
|
15
|
-
* @
|
|
16
|
-
* @property extraInfo - Additional error details
|
|
17
|
-
* @property status - HTTP status code (default: 500)
|
|
17
|
+
* @throws AMOError if the response is not ok
|
|
18
18
|
*/
|
|
19
|
-
export
|
|
20
|
-
message: string;
|
|
21
|
-
extraInfo?: string;
|
|
22
|
-
status?: number;
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* Create API error object with status code and extra information.
|
|
26
|
-
*/
|
|
27
|
-
export declare const createApiError: ({ message, extraInfo, status, }: CreateApiErrorParams) => ApiError;
|
|
19
|
+
export declare const patchScannerResult: ({ url, payload, env, }: PatchScannerResultParams) => Promise<void>;
|
package/dist/functions/api.js
CHANGED
|
@@ -1,13 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
12
|
+
exports.patchScannerResult = void 0;
|
|
13
|
+
const error_1 = require("./error");
|
|
14
|
+
const auth_1 = require("./auth");
|
|
4
15
|
/**
|
|
5
|
-
*
|
|
16
|
+
* PATCH a scanner result to the AMO API, authenticating with a JWT token
|
|
17
|
+
* computed from the `AMO_JWT_ISS_KEY` and `AMO_JWT_SECRET` environment
|
|
18
|
+
* variables.
|
|
19
|
+
*
|
|
20
|
+
* @throws AMOError if the response is not ok
|
|
6
21
|
*/
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
22
|
+
const patchScannerResult = (_a) => __awaiter(void 0, [_a], void 0, function* ({ url, payload, env, }) {
|
|
23
|
+
const response = yield fetch(url, {
|
|
24
|
+
method: 'PATCH',
|
|
25
|
+
headers: {
|
|
26
|
+
'Content-Type': 'application/json',
|
|
27
|
+
Authorization: `JWT ${(0, auth_1.makeJWT)({ env })}`,
|
|
28
|
+
},
|
|
29
|
+
body: JSON.stringify(payload),
|
|
30
|
+
});
|
|
31
|
+
if (!response.ok) {
|
|
32
|
+
throw new error_1.AMOError(`unexpected response: ${response.statusText}`);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
exports.patchScannerResult = patchScannerResult;
|
|
@@ -3,17 +3,19 @@ export declare const DEFAULT_DOWNLOAD_FILENAME = "input.xpi";
|
|
|
3
3
|
/**
|
|
4
4
|
* Error thrown when file download operations fail.
|
|
5
5
|
*
|
|
6
|
-
* @property status - HTTP status code
|
|
7
6
|
* @property extraInfo - Additional error details
|
|
7
|
+
* @property status - HTTP status code to be used when returning the error
|
|
8
|
+
* response to the client. This is not the HTTP status code received when trying
|
|
9
|
+
* to download a file.
|
|
8
10
|
*/
|
|
9
11
|
export declare class DownloadFileError extends Error {
|
|
10
12
|
status: number;
|
|
11
13
|
extraInfo?: string;
|
|
12
14
|
constructor(message: string, extraInfo?: string, status?: number);
|
|
13
15
|
/**
|
|
14
|
-
* Convert this error to
|
|
16
|
+
* Convert this error to AppError format.
|
|
15
17
|
*/
|
|
16
|
-
|
|
18
|
+
toAppError(): import("./error").AppError;
|
|
17
19
|
}
|
|
18
20
|
/**
|
|
19
21
|
* Parameters for downloading a file from the AMO API with JWT authentication.
|
|
@@ -18,14 +18,16 @@ const node_path_1 = __importDefault(require("node:path"));
|
|
|
18
18
|
const node_fs_1 = require("node:fs");
|
|
19
19
|
const node_stream_1 = require("node:stream");
|
|
20
20
|
const promises_1 = require("node:stream/promises");
|
|
21
|
-
const
|
|
21
|
+
const error_1 = require("./error");
|
|
22
22
|
const auth_1 = require("./auth");
|
|
23
23
|
exports.DEFAULT_DOWNLOAD_FILENAME = 'input.xpi';
|
|
24
24
|
/**
|
|
25
25
|
* Error thrown when file download operations fail.
|
|
26
26
|
*
|
|
27
|
-
* @property status - HTTP status code
|
|
28
27
|
* @property extraInfo - Additional error details
|
|
28
|
+
* @property status - HTTP status code to be used when returning the error
|
|
29
|
+
* response to the client. This is not the HTTP status code received when trying
|
|
30
|
+
* to download a file.
|
|
29
31
|
*/
|
|
30
32
|
class DownloadFileError extends Error {
|
|
31
33
|
constructor(message, extraInfo, status = 500) {
|
|
@@ -35,10 +37,10 @@ class DownloadFileError extends Error {
|
|
|
35
37
|
this.extraInfo = extraInfo;
|
|
36
38
|
}
|
|
37
39
|
/**
|
|
38
|
-
* Convert this error to
|
|
40
|
+
* Convert this error to AppError format.
|
|
39
41
|
*/
|
|
40
|
-
|
|
41
|
-
return (0,
|
|
42
|
+
toAppError() {
|
|
43
|
+
return (0, error_1.createAppError)({
|
|
42
44
|
message: this.message,
|
|
43
45
|
extraInfo: this.extraInfo,
|
|
44
46
|
status: this.status,
|
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
/**
|
|
14
|
+
* Parameters for creating an app error.
|
|
15
|
+
*
|
|
16
|
+
* @property message - Error message
|
|
17
|
+
* @property extraInfo - Additional error details
|
|
18
|
+
* @property status - HTTP status code (default: 500)
|
|
19
|
+
*/
|
|
20
|
+
export type CreateAppErrorParams = {
|
|
21
|
+
message: string;
|
|
22
|
+
extraInfo?: string;
|
|
23
|
+
status?: number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Create an app error object with status code and extra information.
|
|
27
|
+
*/
|
|
28
|
+
export declare const createAppError: ({ message, extraInfo, status, }: CreateAppErrorParams) => AppError;
|
|
29
|
+
/**
|
|
30
|
+
* Error object used when interactions with the AMO API led to errors.
|
|
31
|
+
*/
|
|
32
|
+
export declare class AMOError extends Error {
|
|
33
|
+
constructor(message: string);
|
|
34
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AMOError = exports.createAppError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Create an app error object with status code and extra information.
|
|
6
|
+
*/
|
|
7
|
+
const createAppError = ({ message, extraInfo, status = 500, }) => {
|
|
8
|
+
const error = new Error(message);
|
|
9
|
+
error.status = status;
|
|
10
|
+
error.extraInfo = extraInfo;
|
|
11
|
+
return error;
|
|
12
|
+
};
|
|
13
|
+
exports.createAppError = createAppError;
|
|
14
|
+
/**
|
|
15
|
+
* Error object used when interactions with the AMO API led to errors.
|
|
16
|
+
*/
|
|
17
|
+
class AMOError extends Error {
|
|
18
|
+
constructor(message) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = 'AMOError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.AMOError = AMOError;
|
package/dist/functions/index.js
CHANGED
|
@@ -29,9 +29,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
exports.createExpressApp = void 0;
|
|
30
30
|
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
31
31
|
const express_1 = __importDefault(require("express"));
|
|
32
|
-
const body_parser_1 = __importDefault(require("body-parser"));
|
|
33
32
|
const safe_compare_1 = __importDefault(require("safe-compare"));
|
|
34
|
-
const
|
|
33
|
+
const error_1 = require("./error");
|
|
35
34
|
/**
|
|
36
35
|
* Easily create a new scanner application with built-in authentication
|
|
37
36
|
* middleware. The middleware validates that the content-type is
|
|
@@ -72,7 +71,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
|
|
|
72
71
|
},
|
|
73
72
|
};
|
|
74
73
|
// Parse JSON body requests.
|
|
75
|
-
app.use(
|
|
74
|
+
app.use(express_1.default.json(jsonOptions));
|
|
76
75
|
// Authentication middleware plus some extra checks.
|
|
77
76
|
app.use((req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
78
77
|
var _a;
|
|
@@ -82,14 +81,14 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
|
|
|
82
81
|
// We do not throw because we are inside a callback, so we pass an error
|
|
83
82
|
// to the next middleware, which will be the error handler.
|
|
84
83
|
// See: https://expressjs.com/en/guide/error-handling.html
|
|
85
|
-
next((0,
|
|
84
|
+
next((0, error_1.createAppError)({
|
|
86
85
|
message: 'unsupported content type',
|
|
87
86
|
status: 415,
|
|
88
87
|
}));
|
|
89
88
|
return;
|
|
90
89
|
}
|
|
91
90
|
if (!apiKey) {
|
|
92
|
-
next((0,
|
|
91
|
+
next((0, error_1.createAppError)({
|
|
93
92
|
message: `api key must be set`,
|
|
94
93
|
status: 500,
|
|
95
94
|
}));
|
|
@@ -97,7 +96,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
|
|
|
97
96
|
}
|
|
98
97
|
const authorization = (_a = req.get('X-Forwarded-Authorization')) !== null && _a !== void 0 ? _a : req.get('Authorization');
|
|
99
98
|
if (!authorization) {
|
|
100
|
-
next((0,
|
|
99
|
+
next((0, error_1.createAppError)({
|
|
101
100
|
message: `missing authorization header`,
|
|
102
101
|
status: 400,
|
|
103
102
|
}));
|
|
@@ -109,7 +108,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
|
|
|
109
108
|
.update(req.rawBody)
|
|
110
109
|
.digest('hex');
|
|
111
110
|
if (!(0, safe_compare_1.default)(`HMAC-SHA256 ${digest}`, authorization)) {
|
|
112
|
-
next((0,
|
|
111
|
+
next((0, error_1.createAppError)({
|
|
113
112
|
message: 'authentication has failed',
|
|
114
113
|
status: 401,
|
|
115
114
|
}));
|
|
@@ -118,7 +117,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
|
|
|
118
117
|
}
|
|
119
118
|
else if (authorization.startsWith('Bearer ')) {
|
|
120
119
|
if (!(0, safe_compare_1.default)(`Bearer ${apiKey}`, authorization)) {
|
|
121
|
-
next((0,
|
|
120
|
+
next((0, error_1.createAppError)({
|
|
122
121
|
message: 'authentication has failed',
|
|
123
122
|
status: 401,
|
|
124
123
|
}));
|
|
@@ -126,7 +125,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
|
|
|
126
125
|
}
|
|
127
126
|
}
|
|
128
127
|
else {
|
|
129
|
-
next((0,
|
|
128
|
+
next((0, error_1.createAppError)({
|
|
130
129
|
message: 'unsupported authorization scheme',
|
|
131
130
|
status: 400,
|
|
132
131
|
}));
|
|
@@ -135,7 +134,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
|
|
|
135
134
|
if (!allowedMethods
|
|
136
135
|
.map((method) => method.toLowerCase())
|
|
137
136
|
.includes(req.method.toLowerCase())) {
|
|
138
|
-
next((0,
|
|
137
|
+
next((0, error_1.createAppError)({ message: 'method not allowed', status: 405 }));
|
|
139
138
|
return;
|
|
140
139
|
}
|
|
141
140
|
next();
|
|
@@ -144,7 +143,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
|
|
|
144
143
|
app.post('/', handler);
|
|
145
144
|
// NotFound handler.
|
|
146
145
|
app.use((req, res, next) => {
|
|
147
|
-
next((0,
|
|
146
|
+
next((0, error_1.createAppError)({ message: 'not found', status: 404 }));
|
|
148
147
|
});
|
|
149
148
|
// Error handler. Even though we are not using `next`, it must be kept
|
|
150
149
|
// because the Express error handler signature requires 4 arguments.
|
|
@@ -165,3 +164,4 @@ exports.createExpressApp = createExpressApp;
|
|
|
165
164
|
__exportStar(require("./api"), exports);
|
|
166
165
|
__exportStar(require("./auth"), exports);
|
|
167
166
|
__exportStar(require("./download"), exports);
|
|
167
|
+
__exportStar(require("./error"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "addons-scanner-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "14.0.0",
|
|
4
4
|
"description": "Various addons related helpers to build CLIs.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,14 +18,10 @@
|
|
|
18
18
|
"yauzl": "3.2.1"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
|
-
"body-parser": "2.2.2",
|
|
22
21
|
"express": "5.2.1",
|
|
23
22
|
"safe-compare": "1.1.4"
|
|
24
23
|
},
|
|
25
24
|
"peerDependenciesMeta": {
|
|
26
|
-
"body-parser": {
|
|
27
|
-
"optional": true
|
|
28
|
-
},
|
|
29
25
|
"express": {
|
|
30
26
|
"optional": true
|
|
31
27
|
},
|
|
@@ -44,7 +40,6 @@
|
|
|
44
40
|
"@types/yauzl": "2.10.3",
|
|
45
41
|
"@typescript-eslint/eslint-plugin": "^8.46.2",
|
|
46
42
|
"@typescript-eslint/parser": "^8.46.2",
|
|
47
|
-
"body-parser": "2.2.2",
|
|
48
43
|
"eslint": "^9.0.0",
|
|
49
44
|
"eslint-config-amo": "^8.0.0",
|
|
50
45
|
"eslint-import-resolver-typescript": "^4.4.4",
|