extension-from-store 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Cezar Augusto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,129 @@
1
+ [npm-version-image]: https://img.shields.io/npm/v/extension-from-store.svg?color=0078D7
2
+ [npm-version-url]: https://www.npmjs.com/package/extension-from-store
3
+ [npm-downloads-image]: https://img.shields.io/npm/dm/extension-from-store.svg?color=2ecc40
4
+ [npm-downloads-url]: https://www.npmjs.com/package/extension-from-store
5
+ [action-image]: https://github.com/cezaraugusto/extension-from-store/actions/workflows/ci.yml/badge.svg?branch=main
6
+ [action-url]: https://github.com/cezaraugusto/extension-from-store/actions
7
+
8
+ > Download public browser extensions from official stores
9
+
10
+ # extension-from-store [![Version][npm-version-image]][npm-version-url] [![Downloads][npm-downloads-image]][npm-downloads-url] [![workflow][action-image]][action-url]
11
+
12
+ - Chrome Web Store, Microsoft Edge Add-ons, Firefox AMO
13
+ - Easy-to-use API
14
+ - Node.js + CLI support
15
+
16
+ ## Install
17
+
18
+ ```bash
19
+ npm i extension-from-store
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ Designed for quiet reliability, extension-from-store keeps the interface simple and the output predictable.
25
+
26
+ **Via Node.js:**
27
+
28
+ ```ts
29
+ import {fetchExtensionFromStore} from 'extension-from-store'
30
+
31
+ const url =
32
+ 'https://chromewebstore.google.com/detail/adblock-plus-free-ad-bloc/cfhdojbkjhnklbpkdaibdccddilifddb'
33
+
34
+ const options = {
35
+ outDir: './extensions',
36
+ userAgent: 'my-tool/1.0.0',
37
+ extract: true,
38
+ logger: {
39
+ onInfo: (message) => console.log(message),
40
+ onWarn: (message) => console.warn(message),
41
+ onError: (message, error) => console.error(message, error)
42
+ }
43
+ }
44
+
45
+ await fetchExtensionFromStore(url, options)
46
+ ```
47
+
48
+ **Via CLI (default command is `fetch`):**
49
+
50
+ ```bash
51
+ npx extension-from-store --url "<store-item-url>"
52
+ npx extension-from-store --url "<store-item-url>" --out ./my-exts
53
+ npx extension-from-store --url "<store-item-url>" --version 2.1.0 --extract
54
+ npx extension-from-store --url "<store-item-url>" --extract
55
+ ```
56
+
57
+ The store is detected from the URL.
58
+
59
+ ## Output
60
+
61
+ ```
62
+ <out>/
63
+ <identifier>@<resolved-version>/
64
+ extension.meta.json
65
+ ...
66
+ ```
67
+
68
+ If the target folder already exists, the operation fails.
69
+
70
+ By default, extension-from-store downloads the archive without extraction. When you pass `--extract`, it unpacks the archive and writes metadata.
71
+
72
+ When extraction is disabled, the archive is saved as:
73
+
74
+ ```
75
+ <out>/<identifier>[@<version>].crx
76
+ <out>/<identifier>[@<version>].xpi
77
+ ```
78
+
79
+ ## Extraction Rules
80
+
81
+ - `.crx`: strip CRX header, extract ZIP payload
82
+ - `.xpi`: treat as ZIP
83
+ - No normalization, no rewriting, no formatting
84
+
85
+ ## `extension.meta.json`
86
+
87
+ ```json
88
+ {
89
+ "store": "chrome | edge | firefox",
90
+ "identifier": "<identifier derived from the URL>",
91
+ "version": "<resolved version>",
92
+ "manifestVersion": 2 | 3
93
+ }
94
+ ```
95
+
96
+ This file is written by extension-from-store to the same folder as the extracted files.
97
+
98
+ ## CLI Flags
99
+
100
+ | Flag | Required | Description |
101
+ | --- | --- | --- |
102
+ | `--url <string>` | Yes | Extension URL |
103
+ | `--out <path>` | No | Output directory (default: `./extensions`) |
104
+ | `--version <string>` | No | Version hint |
105
+ | `--user-agent <string>` | No | Override user agent |
106
+ | `--extract` | No | Extract after download (default: download only) |
107
+ | `--quiet` | No | Suppress info logs |
108
+ | `--verbose` | No | Emit verbose info logs |
109
+ | `--json` | No | JSON lines output to stdout |
110
+
111
+
112
+ ## Logging
113
+
114
+ Library logging is opt-in via the `logger` hooks. The library never writes directly to stdout/stderr.
115
+
116
+ ## Exit Codes
117
+
118
+ - `0` success
119
+ - `1` invalid input
120
+ - `2` unsupported store
121
+ - `3` not found / not public
122
+ - `4` download failed
123
+ - `5` extraction failed
124
+ - `6` filesystem conflict
125
+ - `7` store incompatibility
126
+
127
+ ## License
128
+
129
+ MIT (c) Cezar Augusto.
package/bin.cjs ADDED
@@ -0,0 +1,156 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const mod = require('./dist/index.cjs');
5
+ const fetchExtensionFromStore = mod.fetchExtensionFromStore;
6
+ const extensionFromStoreError = mod.extensionFromStoreError;
7
+
8
+ const EXIT_CODES = {
9
+ InvalidInput: 1,
10
+ UnsupportedStore: 2,
11
+ NotFound: 3,
12
+ NotPublic: 3,
13
+ DownloadFailed: 4,
14
+ ExtractionFailed: 5,
15
+ FilesystemConflict: 6,
16
+ StoreIncompatibility: 7,
17
+ };
18
+
19
+ function parseArgs(argv) {
20
+ const args = [...argv];
21
+ if (args[0] === 'fetch') args.shift();
22
+
23
+ const result = {
24
+ url: '',
25
+ out: '',
26
+ version: '',
27
+ userAgent: '',
28
+ extract: false,
29
+ quiet: false,
30
+ verbose: false,
31
+ json: false,
32
+ };
33
+
34
+ for (let i = 0; i < args.length; i += 1) {
35
+ const arg = args[i];
36
+
37
+ if (arg === '--url') {
38
+ result.url = args[++i] || '';
39
+ continue;
40
+ }
41
+
42
+ if (arg === '--out') {
43
+ result.out = args[++i] || '';
44
+ continue;
45
+ }
46
+
47
+ if (arg === '--version') {
48
+ result.version = args[++i] || '';
49
+ continue;
50
+ }
51
+ if (arg === '--extract') {
52
+ result.extract = true;
53
+ continue;
54
+ }
55
+
56
+ if (arg === '--user-agent') {
57
+ result.userAgent = args[++i] || '';
58
+ continue;
59
+ }
60
+
61
+ if (arg === '--quiet') {
62
+ result.quiet = true;
63
+ continue;
64
+ }
65
+
66
+ if (arg === '--verbose') {
67
+ result.verbose = true;
68
+ continue;
69
+ }
70
+
71
+ if (arg === '--json') {
72
+ result.json = true;
73
+ continue;
74
+ }
75
+
76
+ throw new Error(`Unknown flag: ${arg}`);
77
+ }
78
+
79
+ if (!result.url) {
80
+ throw new Error('Missing required flag: --url');
81
+ }
82
+
83
+ return result;
84
+ }
85
+
86
+ function createCliLogger(opts) {
87
+ const emit = (level, message, error) => {
88
+ const payload = { level, message };
89
+
90
+ if (error) payload.error = String(error);
91
+
92
+ console.log(JSON.stringify(payload));
93
+ };
94
+
95
+ if (opts.json) {
96
+ return {
97
+ onInfo: (message) => emit('info', message),
98
+ onWarn: (message) => emit('warn', message),
99
+ onError: (message, error) => emit('error', message, error),
100
+ };
101
+ }
102
+
103
+ return {
104
+ onInfo: opts.quiet ? undefined : (message) => console.log(message),
105
+ onWarn: opts.quiet ? undefined : (message) => console.error(message),
106
+ onError: (message, error) => {
107
+ const text = error ? `${message}\n${String(error)}` : message;
108
+ console.error(text);
109
+ },
110
+ };
111
+ }
112
+
113
+ async function main() {
114
+ let args = null;
115
+
116
+ try {
117
+ args = parseArgs(process.argv.slice(2));
118
+ const logger = createCliLogger(args);
119
+ await fetchExtensionFromStore(
120
+ args.url,
121
+ {
122
+ outDir: args.out || undefined,
123
+ userAgent: args.userAgent || undefined,
124
+ version: args.version || undefined,
125
+ extract: args.extract,
126
+ logger,
127
+ },
128
+ );
129
+
130
+ process.exit(0);
131
+ } catch (error) {
132
+ if (error instanceof extensionFromStoreError) {
133
+ const code = EXIT_CODES[error.code] || 1;
134
+ const message = error.message || 'Extension fetch failed';
135
+
136
+ if (args?.json) {
137
+ console.log(JSON.stringify({ level: 'error', message }));
138
+ } else if (code !== 0) {
139
+ console.error(message);
140
+ }
141
+
142
+ process.exit(code);
143
+ }
144
+ const message = String(error?.message || error);
145
+
146
+ if (args?.json) {
147
+ console.log(JSON.stringify({ level: 'error', message }));
148
+ } else {
149
+ console.error(message);
150
+ }
151
+
152
+ process.exit(1);
153
+ }
154
+ }
155
+
156
+ main();
package/bin.js ADDED
@@ -0,0 +1,153 @@
1
+ #!/usr/bin/env node
2
+
3
+ const mod = require('./dist/index.cjs');
4
+ const fetchExtensionFromStore = mod.fetchExtensionFromStore;
5
+ const extensionFromStoreError = mod.extensionFromStoreError;
6
+
7
+ const EXIT_CODES = {
8
+ InvalidInput: 1,
9
+ UnsupportedStore: 2,
10
+ NotFound: 3,
11
+ NotPublic: 3,
12
+ DownloadFailed: 4,
13
+ ExtractionFailed: 5,
14
+ FilesystemConflict: 6,
15
+ StoreIncompatibility: 7,
16
+ };
17
+
18
+ function parseArgs(argv) {
19
+ const args = [...argv];
20
+
21
+ if (args[0] === 'fetch') args.shift();
22
+
23
+ const result = {
24
+ url: '',
25
+ out: '',
26
+ version: '',
27
+ userAgent: '',
28
+ extract: false,
29
+ quiet: false,
30
+ verbose: false,
31
+ json: false,
32
+ };
33
+
34
+ for (let i = 0; i < args.length; i += 1) {
35
+ const arg = args[i];
36
+
37
+ if (arg === '--url') {
38
+ result.url = args[++i] || '';
39
+ continue;
40
+ }
41
+
42
+ if (arg === '--out') {
43
+ result.out = args[++i] || '';
44
+ continue;
45
+ }
46
+
47
+ if (arg === '--version') {
48
+ result.version = args[++i] || '';
49
+ continue;
50
+ }
51
+ if (arg === '--extract') {
52
+ result.extract = true;
53
+ continue;
54
+ }
55
+
56
+ if (arg === '--user-agent') {
57
+ result.userAgent = args[++i] || '';
58
+ continue;
59
+ }
60
+
61
+ if (arg === '--quiet') {
62
+ result.quiet = true;
63
+ continue;
64
+ }
65
+
66
+ if (arg === '--verbose') {
67
+ result.verbose = true;
68
+ continue;
69
+ }
70
+
71
+ if (arg === '--json') {
72
+ result.json = true;
73
+ continue;
74
+ }
75
+
76
+ throw new Error(`Unknown flag: ${arg}`);
77
+ }
78
+
79
+ if (!result.url) {
80
+ throw new Error('Missing required flag: --url');
81
+ }
82
+
83
+ return result;
84
+ }
85
+
86
+ function createCliLogger(opts) {
87
+ const emit = (level, message, error) => {
88
+ const payload = { level, message };
89
+ if (error) payload.error = String(error);
90
+ console.log(JSON.stringify(payload));
91
+ };
92
+
93
+ if (opts.json) {
94
+ return {
95
+ onInfo: (message) => emit('info', message),
96
+ onWarn: (message) => emit('warn', message),
97
+ onError: (message, error) => emit('error', message, error),
98
+ };
99
+ }
100
+
101
+ return {
102
+ onInfo: opts.quiet ? undefined : (message) => console.log(message),
103
+ onWarn: opts.quiet ? undefined : (message) => console.error(message),
104
+ onError: (message, error) => {
105
+ const text = error ? `${message}\n${String(error)}` : message;
106
+ console.error(text);
107
+ },
108
+ };
109
+ }
110
+
111
+ async function main() {
112
+ let args = null;
113
+
114
+ try {
115
+ args = parseArgs(process.argv.slice(2));
116
+ const logger = createCliLogger(args);
117
+ await fetchExtensionFromStore(
118
+ args.url,
119
+ {
120
+ outDir: args.out || undefined,
121
+ userAgent: args.userAgent || undefined,
122
+ version: args.version || undefined,
123
+ extract: args.extract,
124
+ logger,
125
+ },
126
+ );
127
+ process.exit(0);
128
+ } catch (error) {
129
+ if (error instanceof extensionFromStoreError) {
130
+ const code = EXIT_CODES[error.code] || 1;
131
+ const message = error.message || 'Extension fetch failed';
132
+
133
+ if (args?.json) {
134
+ console.log(JSON.stringify({ level: 'error', message }));
135
+ } else if (code !== 0) {
136
+ console.error(message);
137
+ }
138
+
139
+ process.exit(code);
140
+ }
141
+ const message = String(error?.message || error);
142
+
143
+ if (args?.json) {
144
+ console.log(JSON.stringify({ level: 'error', message }));
145
+ } else {
146
+ console.error(message);
147
+ }
148
+
149
+ process.exit(1);
150
+ }
151
+ }
152
+
153
+ main();
@@ -0,0 +1,7 @@
1
+ export type ErrorCode = 'InvalidInput' | 'UnsupportedStore' | 'NotFound' | 'NotPublic' | 'DownloadFailed' | 'ExtractionFailed' | 'FilesystemConflict' | 'StoreIncompatibility';
2
+ export declare class extensionFromStoreError extends Error {
3
+ readonly code: ErrorCode;
4
+ readonly cause?: unknown;
5
+ constructor(code: ErrorCode, message: string, cause?: unknown);
6
+ }
7
+ export declare function asExtensionFromStoreError(error: unknown, fallback: extensionFromStoreError): extensionFromStoreError;
@@ -0,0 +1,3 @@
1
+ export declare function stripCrxHeader(buffer: Buffer): Buffer;
2
+ export declare function extractCrx(crxPath: string, extractDir: string, workDir: string): Promise<void>;
3
+ export declare function extractZipArchive(zipPath: string, extractDir: string): Promise<void>;
package/dist/http.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { type Logger } from './logger';
2
+ type HttpOptions = {
3
+ userAgent?: string;
4
+ logger?: Logger;
5
+ };
6
+ export declare function resolveFinalUrl(url: string, options: HttpOptions, maxRedirects?: number): Promise<string>;
7
+ export declare function downloadToFile(url: string, filePath: string, options: HttpOptions, maxRedirects?: number): Promise<void>;
8
+ export declare function requestJson<T>(url: string, options: HttpOptions, maxRedirects?: number): Promise<T>;
9
+ export declare function requestText(url: string, options: HttpOptions, maxRedirects?: number): Promise<string>;
10
+ export {};