addons-scanner-utils 13.1.0 → 15.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.
@@ -1,27 +1,19 @@
1
+ import { MakeJWTConfig } from './auth';
1
2
  /**
2
- * Error object with HTTP status code and optional extra information.
3
+ * Parameters for patching a scanner result.
3
4
  *
4
- * @property message - Error message
5
- * @property extraInfo - Additional error details
6
- * @property status - HTTP status code
5
+ * @property url - URL to PATCH
6
+ * @property payload - JSON body to send
7
7
  */
8
- export type ApiError = Error & {
9
- extraInfo?: string;
10
- status: number;
8
+ export type PatchScannerResultParams = MakeJWTConfig & {
9
+ url: string;
10
+ payload: Record<string, unknown>;
11
11
  };
12
12
  /**
13
- * Parameters for creating an API error.
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
- * @property message - Error message
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 type CreateApiErrorParams = {
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>;
@@ -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.createApiError = void 0;
12
+ exports.patchScannerResult = void 0;
13
+ const error_1 = require("./error");
14
+ const auth_1 = require("./auth");
4
15
  /**
5
- * Create API error object with status code and extra information.
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 createApiError = ({ message, extraInfo, status = 500, }) => {
8
- const error = new Error(message);
9
- error.status = status;
10
- error.extraInfo = extraInfo;
11
- return error;
12
- };
13
- exports.createApiError = createApiError;
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 ApiError format.
16
+ * Convert this error to AppError format.
15
17
  */
16
- toApiError(): import("./api").ApiError;
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 api_1 = require("./api");
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 ApiError format.
40
+ * Convert this error to AppError format.
39
41
  */
40
- toApiError() {
41
- return (0, api_1.createApiError)({
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;
@@ -41,7 +41,6 @@ export type FunctionConfig = {
41
41
  * via `apiKeyEnvVarName`)
42
42
  *
43
43
  * **Authentication:**
44
- * - Bearer token: `Authorization: Bearer <api_key>` (deprecated)
45
44
  * - HMAC-SHA256: `Authorization: HMAC-SHA256 <digest>` (digest of request
46
45
  * body)
47
46
  */
@@ -49,3 +48,4 @@ export declare const createExpressApp: ({ _console, _process, apiKeyEnvVarName,
49
48
  export * from './api';
50
49
  export * from './auth';
51
50
  export * from './download';
51
+ export * from './error';
@@ -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 api_1 = require("./api");
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
@@ -51,7 +50,6 @@ const api_1 = require("./api");
51
50
  * via `apiKeyEnvVarName`)
52
51
  *
53
52
  * **Authentication:**
54
- * - Bearer token: `Authorization: Bearer <api_key>` (deprecated)
55
53
  * - HMAC-SHA256: `Authorization: HMAC-SHA256 <digest>` (digest of request
56
54
  * body)
57
55
  */
@@ -72,7 +70,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
72
70
  },
73
71
  };
74
72
  // Parse JSON body requests.
75
- app.use(body_parser_1.default.json(jsonOptions));
73
+ app.use(express_1.default.json(jsonOptions));
76
74
  // Authentication middleware plus some extra checks.
77
75
  app.use((req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
78
76
  var _a;
@@ -82,14 +80,14 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
82
80
  // We do not throw because we are inside a callback, so we pass an error
83
81
  // to the next middleware, which will be the error handler.
84
82
  // See: https://expressjs.com/en/guide/error-handling.html
85
- next((0, api_1.createApiError)({
83
+ next((0, error_1.createAppError)({
86
84
  message: 'unsupported content type',
87
85
  status: 415,
88
86
  }));
89
87
  return;
90
88
  }
91
89
  if (!apiKey) {
92
- next((0, api_1.createApiError)({
90
+ next((0, error_1.createAppError)({
93
91
  message: `api key must be set`,
94
92
  status: 500,
95
93
  }));
@@ -97,7 +95,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
97
95
  }
98
96
  const authorization = (_a = req.get('X-Forwarded-Authorization')) !== null && _a !== void 0 ? _a : req.get('Authorization');
99
97
  if (!authorization) {
100
- next((0, api_1.createApiError)({
98
+ next((0, error_1.createAppError)({
101
99
  message: `missing authorization header`,
102
100
  status: 400,
103
101
  }));
@@ -109,16 +107,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
109
107
  .update(req.rawBody)
110
108
  .digest('hex');
111
109
  if (!(0, safe_compare_1.default)(`HMAC-SHA256 ${digest}`, authorization)) {
112
- next((0, api_1.createApiError)({
113
- message: 'authentication has failed',
114
- status: 401,
115
- }));
116
- return;
117
- }
118
- }
119
- else if (authorization.startsWith('Bearer ')) {
120
- if (!(0, safe_compare_1.default)(`Bearer ${apiKey}`, authorization)) {
121
- next((0, api_1.createApiError)({
110
+ next((0, error_1.createAppError)({
122
111
  message: 'authentication has failed',
123
112
  status: 401,
124
113
  }));
@@ -126,7 +115,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
126
115
  }
127
116
  }
128
117
  else {
129
- next((0, api_1.createApiError)({
118
+ next((0, error_1.createAppError)({
130
119
  message: 'unsupported authorization scheme',
131
120
  status: 400,
132
121
  }));
@@ -135,7 +124,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
135
124
  if (!allowedMethods
136
125
  .map((method) => method.toLowerCase())
137
126
  .includes(req.method.toLowerCase())) {
138
- next((0, api_1.createApiError)({ message: 'method not allowed', status: 405 }));
127
+ next((0, error_1.createAppError)({ message: 'method not allowed', status: 405 }));
139
128
  return;
140
129
  }
141
130
  next();
@@ -144,7 +133,7 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
144
133
  app.post('/', handler);
145
134
  // NotFound handler.
146
135
  app.use((req, res, next) => {
147
- next((0, api_1.createApiError)({ message: 'not found', status: 404 }));
136
+ next((0, error_1.createAppError)({ message: 'not found', status: 404 }));
148
137
  });
149
138
  // Error handler. Even though we are not using `next`, it must be kept
150
139
  // because the Express error handler signature requires 4 arguments.
@@ -165,3 +154,4 @@ exports.createExpressApp = createExpressApp;
165
154
  __exportStar(require("./api"), exports);
166
155
  __exportStar(require("./auth"), exports);
167
156
  __exportStar(require("./download"), exports);
157
+ __exportStar(require("./error"), exports);
package/package.json CHANGED
@@ -1,9 +1,35 @@
1
1
  {
2
2
  "name": "addons-scanner-utils",
3
- "version": "13.1.0",
3
+ "version": "15.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",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ },
12
+ "./functions": {
13
+ "types": "./dist/functions/index.d.ts",
14
+ "default": "./dist/functions/index.js"
15
+ },
16
+ "./functions/*": {
17
+ "types": "./dist/functions/*.d.ts",
18
+ "default": "./dist/functions/*.js"
19
+ },
20
+ "./io": {
21
+ "types": "./dist/io/index.d.ts",
22
+ "default": "./dist/io/index.js"
23
+ },
24
+ "./io/*": {
25
+ "types": "./dist/io/*.d.ts",
26
+ "default": "./dist/io/*.js"
27
+ },
28
+ "./*": {
29
+ "types": "./dist/*.d.ts",
30
+ "default": "./dist/*.js"
31
+ }
32
+ },
7
33
  "files": [
8
34
  "dist/**/*"
9
35
  ],
@@ -18,14 +44,10 @@
18
44
  "yauzl": "3.2.1"
19
45
  },
20
46
  "peerDependencies": {
21
- "body-parser": "2.2.2",
22
47
  "express": "5.2.1",
23
48
  "safe-compare": "1.1.4"
24
49
  },
25
50
  "peerDependenciesMeta": {
26
- "body-parser": {
27
- "optional": true
28
- },
29
51
  "express": {
30
52
  "optional": true
31
53
  },
@@ -44,7 +66,6 @@
44
66
  "@types/yauzl": "2.10.3",
45
67
  "@typescript-eslint/eslint-plugin": "^8.46.2",
46
68
  "@typescript-eslint/parser": "^8.46.2",
47
- "body-parser": "2.2.2",
48
69
  "eslint": "^9.0.0",
49
70
  "eslint-config-amo": "^8.0.0",
50
71
  "eslint-import-resolver-typescript": "^4.4.4",