addons-scanner-utils 12.1.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.
- package/README.md +11 -11
- package/dist/functions/auth.d.ts +16 -0
- package/dist/functions/auth.js +34 -0
- package/dist/functions/download.d.ts +21 -0
- package/dist/functions/download.js +30 -12
- package/dist/functions/index.d.ts +1 -0
- package/dist/functions/index.js +1 -0
- package/dist/io/directory.js +0 -1
- package/dist/test-helpers.js +0 -1
- package/package.json +12 -10
package/README.md
CHANGED
|
@@ -7,48 +7,48 @@ Various addons related helpers to build scanners.
|
|
|
7
7
|
## Usage
|
|
8
8
|
|
|
9
9
|
```
|
|
10
|
-
|
|
10
|
+
npm install addons-scanner-utils
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
## Requirements
|
|
14
14
|
|
|
15
|
-
- You need [Node](https://nodejs.org/)
|
|
16
|
-
- You need [
|
|
15
|
+
- You need [Node](https://nodejs.org/) 22, which is the current [LTS](https://github.com/nodejs/LTS) (long term support) release.
|
|
16
|
+
- You need [npm](https://www.npmjs.com/) to manage dependencies and run commands.
|
|
17
17
|
|
|
18
18
|
## Development
|
|
19
19
|
|
|
20
20
|
- Read [our contributing guidelines](./CONTRIBUTING.md) to get started on your first patch
|
|
21
21
|
- Clone this repository
|
|
22
|
-
- Type `
|
|
23
|
-
- Run the test suite to make sure everything is set up: `
|
|
22
|
+
- Type `npm install` to install everything
|
|
23
|
+
- Run the test suite to make sure everything is set up: `npm test`
|
|
24
24
|
|
|
25
25
|
### Available development commands
|
|
26
26
|
|
|
27
27
|
In the project directory, you can run the following commands. There are a few commands not mentioned here (see `package.json`) but those are only used by internal processes.
|
|
28
28
|
|
|
29
|
-
#### `
|
|
29
|
+
#### `npm run eslint`
|
|
30
30
|
|
|
31
31
|
This runs [ESLint][] to discover problems within our codebase without executing it. ESLint also enforces some patterns and practices.
|
|
32
32
|
|
|
33
|
-
#### `
|
|
33
|
+
#### `npm run lint`
|
|
34
34
|
|
|
35
35
|
This runs all the _lint_ commands at once.
|
|
36
36
|
|
|
37
|
-
#### `
|
|
37
|
+
#### `npm run prettier`
|
|
38
38
|
|
|
39
39
|
This runs [Prettier][] to automatically format the entire codebase.
|
|
40
40
|
|
|
41
|
-
#### `
|
|
41
|
+
#### `npm run prettier-dev`
|
|
42
42
|
|
|
43
43
|
This runs [Prettier][] on only your changed files. This is intended for development.
|
|
44
44
|
|
|
45
|
-
#### `
|
|
45
|
+
#### `npm test`
|
|
46
46
|
|
|
47
47
|
This launches [Jest][] in the interactive watch mode.
|
|
48
48
|
|
|
49
49
|
### Prettier
|
|
50
50
|
|
|
51
|
-
We use [Prettier][] to automatically format our JavaScript code and stop all the on-going debates over styles. As a developer, you have to run it (with `
|
|
51
|
+
We use [Prettier][] to automatically format our JavaScript code and stop all the on-going debates over styles. As a developer, you have to run it (with `npm run prettier-dev`) before submitting a Pull Request.
|
|
52
52
|
|
|
53
53
|
### Versioning
|
|
54
54
|
|
|
@@ -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';
|
package/dist/functions/index.js
CHANGED
|
@@ -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);
|
package/dist/io/directory.js
CHANGED
package/dist/test-helpers.js
CHANGED
|
@@ -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": "
|
|
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.
|
|
18
|
+
"yauzl": "3.2.1"
|
|
18
19
|
},
|
|
19
20
|
"peerDependencies": {
|
|
20
21
|
"body-parser": "2.2.2",
|
|
@@ -36,17 +37,18 @@
|
|
|
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
|
-
"@types/supertest": "^
|
|
43
|
+
"@types/supertest": "^7.2.0",
|
|
42
44
|
"@types/yauzl": "2.10.3",
|
|
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": "^
|
|
47
|
-
"eslint-config-amo": "^
|
|
48
|
+
"eslint": "^9.0.0",
|
|
49
|
+
"eslint-config-amo": "^8.0.0",
|
|
48
50
|
"eslint-import-resolver-typescript": "^4.4.4",
|
|
49
|
-
"eslint-plugin-amo": "^
|
|
51
|
+
"eslint-plugin-amo": "^3.0.0",
|
|
50
52
|
"express": "5.2.1",
|
|
51
53
|
"jest": "^30.2.0",
|
|
52
54
|
"prettier": "3.8.1",
|
|
@@ -59,14 +61,14 @@
|
|
|
59
61
|
"typescript": "^5.9.3"
|
|
60
62
|
},
|
|
61
63
|
"scripts": {
|
|
62
|
-
"eslint": "eslint
|
|
63
|
-
"lint": "
|
|
64
|
+
"eslint": "eslint src/",
|
|
65
|
+
"lint": "npm run eslint",
|
|
64
66
|
"prepack": "rimraf dist/ && tsc --outDir dist/ && rimraf -g 'dist/**/*.spec.*' 'dist/*.spec.*'",
|
|
65
67
|
"prettier": "prettier --write '**'",
|
|
66
|
-
"prettier-ci": "prettier --list-different '**' || (echo '\n\nThis failure means you did not run `
|
|
68
|
+
"prettier-ci": "prettier --list-different '**' || (echo '\n\nThis failure means you did not run `npm run prettier-dev` before committing\n\n' && exit 1)",
|
|
67
69
|
"prettier-dev": "pretty-quick --branch master",
|
|
68
70
|
"test": "jest",
|
|
69
|
-
"test-ci": "
|
|
71
|
+
"test-ci": "npm test -- --coverage",
|
|
70
72
|
"type-coverage": "type-coverage",
|
|
71
73
|
"typecheck": "tsc --noEmit"
|
|
72
74
|
},
|