addons-scanner-utils 13.0.0 → 13.1.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.
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Configuration for makeJWT.
3
+ *
4
+ * @property env - Environment variables object (default: process.env)
5
+ */
6
+ export type MakeJWTConfig = {
7
+ env?: NodeJS.ProcessEnv;
8
+ };
9
+ /**
10
+ * Create a signed JWT token for AMO API authentication using the
11
+ * `AMO_JWT_ISS_KEY` and `AMO_JWT_SECRET` environment variables.
12
+ *
13
+ * @returns A signed HS256 JWT string
14
+ * @throws Error if the required environment variables are not set
15
+ */
16
+ export declare const makeJWT: ({ env }?: MakeJWTConfig) => string;
@@ -0,0 +1,34 @@
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.makeJWT = void 0;
7
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
+ const JWT_EXPIRY_SECONDS = 60;
9
+ /**
10
+ * Create a signed JWT token for AMO API authentication using the
11
+ * `AMO_JWT_ISS_KEY` and `AMO_JWT_SECRET` environment variables.
12
+ *
13
+ * @returns A signed HS256 JWT string
14
+ * @throws Error if the required environment variables are not set
15
+ */
16
+ const makeJWT = ({ env = process.env } = {}) => {
17
+ const issKey = env.AMO_JWT_ISS_KEY;
18
+ const secret = env.AMO_JWT_SECRET;
19
+ if (!issKey) {
20
+ throw new Error('AMO_JWT_ISS_KEY environment variable is not set');
21
+ }
22
+ if (!secret) {
23
+ throw new Error('AMO_JWT_SECRET environment variable is not set');
24
+ }
25
+ const issuedAt = Math.floor(Date.now() / 1000);
26
+ const payload = {
27
+ iss: issKey,
28
+ jti: Math.random().toString(),
29
+ iat: issuedAt,
30
+ exp: issuedAt + JWT_EXPIRY_SECONDS,
31
+ };
32
+ return jsonwebtoken_1.default.sign(payload, secret, { algorithm: 'HS256' });
33
+ };
34
+ exports.makeJWT = makeJWT;
@@ -1,3 +1,4 @@
1
+ import { MakeJWTConfig } from './auth';
1
2
  export declare const DEFAULT_DOWNLOAD_FILENAME = "input.xpi";
2
3
  /**
3
4
  * Error thrown when file download operations fail.
@@ -14,6 +15,26 @@ export declare class DownloadFileError extends Error {
14
15
  */
15
16
  toApiError(): import("./api").ApiError;
16
17
  }
18
+ /**
19
+ * Parameters for downloading a file from the AMO API with JWT authentication.
20
+ *
21
+ * @property downloadURL - URL to download from
22
+ * @property tmpDir - Temporary directory for download (default: os.tmpdir())
23
+ * @property filename - Downloaded file name (default: 'input.xpi')
24
+ */
25
+ export type DownloadFileParams = MakeJWTConfig & {
26
+ downloadURL: string;
27
+ tmpDir?: string;
28
+ filename?: string;
29
+ };
30
+ /**
31
+ * Download a file from the AMO API, authenticating with a JWT token computed
32
+ * from the `AMO_JWT_ISS_KEY` and `AMO_JWT_SECRET` environment variables.
33
+ *
34
+ * @returns Promise resolving to the file path of the downloaded file
35
+ * @throws DownloadFileError if the download fails
36
+ */
37
+ export declare const downloadFile: ({ downloadURL, tmpDir, filename, env, }: DownloadFileParams) => Promise<string>;
17
38
  /**
18
39
  * Parameters for downloading a file upload.
19
40
  *
@@ -12,13 +12,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.downloadFileUpload = exports.DownloadFileError = exports.DEFAULT_DOWNLOAD_FILENAME = void 0;
15
+ exports.downloadFileUpload = exports.downloadFile = exports.DownloadFileError = exports.DEFAULT_DOWNLOAD_FILENAME = void 0;
16
16
  const node_os_1 = __importDefault(require("node:os"));
17
17
  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
21
  const api_1 = require("./api");
22
+ const auth_1 = require("./auth");
22
23
  exports.DEFAULT_DOWNLOAD_FILENAME = 'input.xpi';
23
24
  /**
24
25
  * Error thrown when file download operations fail.
@@ -45,18 +46,9 @@ class DownloadFileError extends Error {
45
46
  }
46
47
  }
47
48
  exports.DownloadFileError = DownloadFileError;
48
- /**
49
- * Download a file from a URL to a temporary location.
50
- *
51
- * @returns Promise resolving to the file path of the downloaded file
52
- * @throws DownloadFileError if the download fails or URL origin doesn't match
53
- */
54
- const downloadFileUpload = (_a) => __awaiter(void 0, [_a], void 0, function* ({ downloadURL, allowedOrigin, tmpDir = node_os_1.default.tmpdir(), filename = exports.DEFAULT_DOWNLOAD_FILENAME, }) {
55
- if (!downloadURL.startsWith(`${allowedOrigin}/`)) {
56
- throw new DownloadFileError(`download URL does not match allowed origin: ${allowedOrigin}`, undefined, 400);
57
- }
49
+ const fetchToFile = (downloadURL, tmpDir, filename, headers) => __awaiter(void 0, void 0, void 0, function* () {
58
50
  try {
59
- const response = yield fetch(downloadURL);
51
+ const response = yield fetch(downloadURL, { headers });
60
52
  if (!response.ok || !response.body) {
61
53
  throw new DownloadFileError(`unexpected response: ${response.statusText}`);
62
54
  }
@@ -72,4 +64,30 @@ const downloadFileUpload = (_a) => __awaiter(void 0, [_a], void 0, function* ({
72
64
  throw new DownloadFileError('failed to download file', message);
73
65
  }
74
66
  });
67
+ /**
68
+ * Download a file from the AMO API, authenticating with a JWT token computed
69
+ * from the `AMO_JWT_ISS_KEY` and `AMO_JWT_SECRET` environment variables.
70
+ *
71
+ * @returns Promise resolving to the file path of the downloaded file
72
+ * @throws DownloadFileError if the download fails
73
+ */
74
+ const downloadFile = (_a) => __awaiter(void 0, [_a], void 0, function* ({ downloadURL, tmpDir = node_os_1.default.tmpdir(), filename = exports.DEFAULT_DOWNLOAD_FILENAME, env, }) {
75
+ const token = (0, auth_1.makeJWT)({ env });
76
+ return fetchToFile(downloadURL, tmpDir, filename, {
77
+ Authorization: `JWT ${token}`,
78
+ });
79
+ });
80
+ exports.downloadFile = downloadFile;
81
+ /**
82
+ * Download a file from a URL to a temporary location.
83
+ *
84
+ * @returns Promise resolving to the file path of the downloaded file
85
+ * @throws DownloadFileError if the download fails or URL origin doesn't match
86
+ */
87
+ const downloadFileUpload = (_a) => __awaiter(void 0, [_a], void 0, function* ({ downloadURL, allowedOrigin, tmpDir = node_os_1.default.tmpdir(), filename = exports.DEFAULT_DOWNLOAD_FILENAME, }) {
88
+ if (!downloadURL.startsWith(`${allowedOrigin}/`)) {
89
+ throw new DownloadFileError(`download URL does not match allowed origin: ${allowedOrigin}`, undefined, 400);
90
+ }
91
+ return fetchToFile(downloadURL, tmpDir, filename);
92
+ });
75
93
  exports.downloadFileUpload = downloadFileUpload;
@@ -47,4 +47,5 @@ export type FunctionConfig = {
47
47
  */
48
48
  export declare const createExpressApp: ({ _console, _process, apiKeyEnvVarName, }?: FunctionConfig) => (handler: RequestHandler) => import("express-serve-static-core").Express;
49
49
  export * from './api';
50
+ export * from './auth';
50
51
  export * from './download';
@@ -163,4 +163,5 @@ const createExpressApp = ({ _console = console, _process = process, apiKeyEnvVar
163
163
  };
164
164
  exports.createExpressApp = createExpressApp;
165
165
  __exportStar(require("./api"), exports);
166
+ __exportStar(require("./auth"), exports);
166
167
  __exportStar(require("./download"), exports);
@@ -81,7 +81,6 @@ class Directory extends base_1.IOBase {
81
81
  let content = '';
82
82
  readStream.on('readable', () => {
83
83
  let chunk;
84
- // eslint-disable-next-line no-cond-assign
85
84
  while ((chunk = readStream.read()) !== null) {
86
85
  content += chunk.toString();
87
86
  }
@@ -21,7 +21,6 @@ const readStringFromStream = (readStream, encoding) => {
21
21
  let content = '';
22
22
  readStream.on('readable', () => {
23
23
  let chunk;
24
- // eslint-disable-next-line no-cond-assign
25
24
  while ((chunk = readStream.read()) !== null) {
26
25
  content += chunk.toString(encoding);
27
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "addons-scanner-utils",
3
- "version": "13.0.0",
3
+ "version": "13.1.0",
4
4
  "description": "Various addons related helpers to build CLIs.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -12,9 +12,10 @@
12
12
  "dependencies": {
13
13
  "common-tags": "1.8.2",
14
14
  "first-chunk-stream": "3.0.0",
15
+ "jsonwebtoken": "^9.0.3",
15
16
  "strip-bom-stream": "4.0.0",
16
17
  "upath": "2.0.1",
17
- "yauzl": "3.2.0"
18
+ "yauzl": "3.2.1"
18
19
  },
19
20
  "peerDependencies": {
20
21
  "body-parser": "2.2.2",
@@ -36,6 +37,7 @@
36
37
  "@types/common-tags": "^1.8.0",
37
38
  "@types/express": "5.0.6",
38
39
  "@types/jest": "^30.0.0",
40
+ "@types/jsonwebtoken": "^9.0.10",
39
41
  "@types/node": "^25.0.1",
40
42
  "@types/safe-compare": "^1.1.0",
41
43
  "@types/supertest": "^7.2.0",
@@ -43,8 +45,8 @@
43
45
  "@typescript-eslint/eslint-plugin": "^8.46.2",
44
46
  "@typescript-eslint/parser": "^8.46.2",
45
47
  "body-parser": "2.2.2",
46
- "eslint": "^8.1.0",
47
- "eslint-config-amo": "^6.1.0",
48
+ "eslint": "^9.0.0",
49
+ "eslint-config-amo": "^8.0.0",
48
50
  "eslint-import-resolver-typescript": "^4.4.4",
49
51
  "eslint-plugin-amo": "^3.0.0",
50
52
  "express": "5.2.1",
@@ -59,7 +61,7 @@
59
61
  "typescript": "^5.9.3"
60
62
  },
61
63
  "scripts": {
62
- "eslint": "eslint --ext ts --ext js src/",
64
+ "eslint": "eslint src/",
63
65
  "lint": "npm run eslint",
64
66
  "prepack": "rimraf dist/ && tsc --outDir dist/ && rimraf -g 'dist/**/*.spec.*' 'dist/*.spec.*'",
65
67
  "prettier": "prettier --write '**'",