extension-from-store 0.1.1 → 0.2.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 +27 -0
- package/bin.cjs +7 -10
- package/bin.js +7 -10
- package/dist/browser.cjs +355 -0
- package/dist/browser.d.ts +44 -0
- package/dist/browser.js +321 -0
- package/dist/core.cjs +301 -0
- package/dist/core.d.ts +10 -0
- package/dist/core.js +222 -0
- package/dist/crx.d.ts +1 -0
- package/dist/extract.d.ts +1 -1
- package/dist/index.cjs +141 -101
- package/dist/index.js +131 -91
- package/dist/manifest.d.ts +7 -0
- package/dist/meta.d.ts +1 -4
- package/dist/node-platform.d.ts +2 -0
- package/dist/platform.d.ts +8 -0
- package/dist/resolve.d.ts +21 -0
- package/dist/stores/chrome.d.ts +2 -1
- package/dist/stores/firefox.d.ts +5 -0
- package/package.json +14 -2
package/dist/browser.js
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_fflate__ from "fflate";
|
|
2
|
+
function _define_property(obj, key, value) {
|
|
3
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
else obj[key] = value;
|
|
10
|
+
return obj;
|
|
11
|
+
}
|
|
12
|
+
class errors_extensionFromStoreError extends Error {
|
|
13
|
+
constructor(code, message, cause){
|
|
14
|
+
super(message), _define_property(this, "code", void 0), _define_property(this, "cause", void 0);
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.cause = cause;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function readUInt32LE(bytes, offset) {
|
|
20
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset + offset, Uint32Array.BYTES_PER_ELEMENT);
|
|
21
|
+
return view.getUint32(0, true);
|
|
22
|
+
}
|
|
23
|
+
function readMagic(bytes) {
|
|
24
|
+
let out = '';
|
|
25
|
+
const slice = bytes.subarray(0, 4);
|
|
26
|
+
for(let index = 0; index < slice.length; index += 1)out += String.fromCharCode(slice[index]);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
function stripCrxHeader(buffer) {
|
|
30
|
+
if (buffer.length < 16) throw new errors_extensionFromStoreError('ExtractionFailed', 'CRX file too small');
|
|
31
|
+
const magic = readMagic(buffer);
|
|
32
|
+
if ('Cr24' !== magic) throw new errors_extensionFromStoreError('ExtractionFailed', 'Invalid CRX header');
|
|
33
|
+
const version = readUInt32LE(buffer, 4);
|
|
34
|
+
if (2 === version) {
|
|
35
|
+
const publicKeyLength = readUInt32LE(buffer, 8);
|
|
36
|
+
const signatureLength = readUInt32LE(buffer, 12);
|
|
37
|
+
const headerSize = 16 + publicKeyLength + signatureLength;
|
|
38
|
+
return buffer.subarray(headerSize);
|
|
39
|
+
}
|
|
40
|
+
if (3 === version) {
|
|
41
|
+
const headerSize = readUInt32LE(buffer, 8);
|
|
42
|
+
return buffer.subarray(12 + headerSize);
|
|
43
|
+
}
|
|
44
|
+
throw new errors_extensionFromStoreError('ExtractionFailed', `Unsupported CRX version ${version}`);
|
|
45
|
+
}
|
|
46
|
+
function createLogger(logger) {
|
|
47
|
+
return {
|
|
48
|
+
info: (message)=>{
|
|
49
|
+
var _logger_onInfo;
|
|
50
|
+
return null == logger ? void 0 : null == (_logger_onInfo = logger.onInfo) ? void 0 : _logger_onInfo.call(logger, message);
|
|
51
|
+
},
|
|
52
|
+
warn: (message)=>{
|
|
53
|
+
var _logger_onWarn;
|
|
54
|
+
return null == logger ? void 0 : null == (_logger_onWarn = logger.onWarn) ? void 0 : _logger_onWarn.call(logger, message);
|
|
55
|
+
},
|
|
56
|
+
error: (message, error)=>{
|
|
57
|
+
var _logger_onError;
|
|
58
|
+
return null == logger ? void 0 : null == (_logger_onError = logger.onError) ? void 0 : _logger_onError.call(logger, message, error);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function parseManifestInfo(raw) {
|
|
63
|
+
let parsed;
|
|
64
|
+
try {
|
|
65
|
+
parsed = JSON.parse(raw);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
throw new errors_extensionFromStoreError('ExtractionFailed', 'manifest.json is not valid JSON', error);
|
|
68
|
+
}
|
|
69
|
+
if (!parsed || 'object' != typeof parsed || Array.isArray(parsed)) throw new errors_extensionFromStoreError('ExtractionFailed', 'manifest.json must contain an object');
|
|
70
|
+
const manifest = parsed;
|
|
71
|
+
const manifestVersion = manifest.manifest_version;
|
|
72
|
+
const extensionVersion = manifest.version;
|
|
73
|
+
if (2 !== manifestVersion && 3 !== manifestVersion) throw new errors_extensionFromStoreError('ExtractionFailed', 'manifest_version must be 2 or 3');
|
|
74
|
+
if (!extensionVersion || 'string' != typeof extensionVersion) throw new errors_extensionFromStoreError('ExtractionFailed', 'manifest.json is missing a version');
|
|
75
|
+
return {
|
|
76
|
+
manifest,
|
|
77
|
+
manifestVersion,
|
|
78
|
+
extensionVersion
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function normalizeChromePlatformInfo(platform) {
|
|
82
|
+
return {
|
|
83
|
+
os: platform.os,
|
|
84
|
+
arch: platform.arch,
|
|
85
|
+
naclArch: platform.naclArch || platform.arch
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
const DEFAULT_CHROME_PLATFORM = {
|
|
89
|
+
os: 'linux',
|
|
90
|
+
arch: 'x64'
|
|
91
|
+
};
|
|
92
|
+
function getChromeDownloadUrl(id, platformInfo = DEFAULT_CHROME_PLATFORM) {
|
|
93
|
+
const encoded = encodeURIComponent(id);
|
|
94
|
+
const platform = normalizeChromePlatformInfo(platformInfo);
|
|
95
|
+
const productId = 'chromiumcrx';
|
|
96
|
+
const productChannel = 'unknown';
|
|
97
|
+
const productVersion = '9999.0.9999.0';
|
|
98
|
+
return [
|
|
99
|
+
'https://clients2.google.com/service/update2/crx',
|
|
100
|
+
'?response=redirect',
|
|
101
|
+
`&os=${platform.os}`,
|
|
102
|
+
`&arch=${platform.arch}`,
|
|
103
|
+
`&os_arch=${platform.arch}`,
|
|
104
|
+
`&nacl_arch=${platform.naclArch}`,
|
|
105
|
+
`&prod=${productId}`,
|
|
106
|
+
`&prodchannel=${productChannel}`,
|
|
107
|
+
`&prodversion=${productVersion}`,
|
|
108
|
+
'&acceptformat=crx2,crx3',
|
|
109
|
+
`&x=id%3D${encoded}%26uc`
|
|
110
|
+
].join('');
|
|
111
|
+
}
|
|
112
|
+
function getEdgeDownloadUrl(id) {
|
|
113
|
+
const encoded = encodeURIComponent(id);
|
|
114
|
+
return [
|
|
115
|
+
'https://edge.microsoft.com/extensionwebstorebase/v1/crx',
|
|
116
|
+
'?response=redirect',
|
|
117
|
+
'&prodversion=109.0.0.0',
|
|
118
|
+
`&x=id%3D${encoded}%26installsource%3Dondemand%26uc`
|
|
119
|
+
].join('');
|
|
120
|
+
}
|
|
121
|
+
async function resolveFirefoxDownload(idOrSlug, versionHint, options) {
|
|
122
|
+
var _addon_current_version_file, _addon_current_version, _addon_current_version1;
|
|
123
|
+
const baseUrl = `https://addons.mozilla.org/api/v5/addons/addon/${encodeURIComponent(idOrSlug)}/`;
|
|
124
|
+
const addon = await options.requestJson(baseUrl, options);
|
|
125
|
+
const slugOrId = addon.slug || idOrSlug;
|
|
126
|
+
if (versionHint) {
|
|
127
|
+
var _version_file;
|
|
128
|
+
const versionUrl = `${baseUrl}versions/${encodeURIComponent(versionHint)}/`;
|
|
129
|
+
const version = await options.requestJson(versionUrl, options);
|
|
130
|
+
const downloadUrl = null == (_version_file = version.file) ? void 0 : _version_file.url;
|
|
131
|
+
if (!downloadUrl) throw new errors_extensionFromStoreError('NotPublic', `Version ${versionHint} is not publicly downloadable`);
|
|
132
|
+
return {
|
|
133
|
+
downloadUrl,
|
|
134
|
+
version: version.version || versionHint,
|
|
135
|
+
slugOrId
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
const downloadUrl = null == (_addon_current_version = addon.current_version) ? void 0 : null == (_addon_current_version_file = _addon_current_version.file) ? void 0 : _addon_current_version_file.url;
|
|
139
|
+
const version = null == (_addon_current_version1 = addon.current_version) ? void 0 : _addon_current_version1.version;
|
|
140
|
+
if (!downloadUrl || !version) throw new errors_extensionFromStoreError('NotPublic', 'Extension is not publicly downloadable');
|
|
141
|
+
return {
|
|
142
|
+
downloadUrl,
|
|
143
|
+
version,
|
|
144
|
+
slugOrId
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
const chromePattern = /^https?:\/\/(?:chrome\.google\.com\/webstore|chromewebstore\.google\.com)\/.+?\/([a-p]{32})(?=[\/#?]|$)/i;
|
|
148
|
+
const chromeDownloadPattern = /^https?:\/\/clients2\.google\.com\/service\/update2\/crx\b.*?%3D([a-p]{32})%26uc/i;
|
|
149
|
+
const edgePattern = /^https?:\/\/microsoftedge\.microsoft\.com\/addons\/.+?\/([a-z]{32})(?=[\/#?]|$)/i;
|
|
150
|
+
const edgeDownloadPattern = /^https?:\/\/edge\.microsoft\.com\/extensionwebstorebase\/v1\/crx\b.*?%3D([a-z]{32})%26/i;
|
|
151
|
+
const firefoxPattern = /^https?:\/\/((?:reviewers\.)?(?:addons\.mozilla\.org|addons(?:-dev)?\.allizom\.org))\/.*?(?:addon|review)\/([^/<>"'?#]+)/i;
|
|
152
|
+
const firefoxDownloadPattern = /^https?:\/\/(addons\.mozilla\.org|addons(?:-dev)?\.allizom\.org)\/[^?#]*\/downloads\/latest\/([^/?#]+)/i;
|
|
153
|
+
function detectStoreFromUrl(url) {
|
|
154
|
+
if (chromePattern.test(url) || chromeDownloadPattern.test(url)) return 'chrome';
|
|
155
|
+
if (edgePattern.test(url) || edgeDownloadPattern.test(url)) return 'edge';
|
|
156
|
+
if (firefoxPattern.test(url) || firefoxDownloadPattern.test(url)) return 'firefox';
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
function extractChromeIdFromUrl(url) {
|
|
160
|
+
const match = chromePattern.exec(url) || chromeDownloadPattern.exec(url);
|
|
161
|
+
return match ? match[1] : null;
|
|
162
|
+
}
|
|
163
|
+
function extractEdgeIdFromUrl(url) {
|
|
164
|
+
const match = edgePattern.exec(url) || edgeDownloadPattern.exec(url);
|
|
165
|
+
return match ? match[1] : null;
|
|
166
|
+
}
|
|
167
|
+
function extractFirefoxSlugFromUrl(url) {
|
|
168
|
+
const match = firefoxPattern.exec(url) || firefoxDownloadPattern.exec(url);
|
|
169
|
+
return match ? match[2] : null;
|
|
170
|
+
}
|
|
171
|
+
function validateInput(url) {
|
|
172
|
+
if (!url || 'string' != typeof url) throw new errors_extensionFromStoreError('InvalidInput', 'URL is required');
|
|
173
|
+
}
|
|
174
|
+
async function resolveDownload(url, options) {
|
|
175
|
+
const store = detectStoreFromUrl(url);
|
|
176
|
+
if (!store) throw new errors_extensionFromStoreError('UnsupportedStore', 'URL does not match a supported store');
|
|
177
|
+
if ('chrome' === store) {
|
|
178
|
+
const downloadId = extractChromeIdFromUrl(url);
|
|
179
|
+
if (!downloadId) throw new errors_extensionFromStoreError('NotFound', 'Chrome extension id not found in URL');
|
|
180
|
+
return {
|
|
181
|
+
store,
|
|
182
|
+
downloadUrl: getChromeDownloadUrl(downloadId, options.platform),
|
|
183
|
+
archiveType: 'crx',
|
|
184
|
+
downloadId,
|
|
185
|
+
slugOrId: downloadId
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
if ('edge' === store) {
|
|
189
|
+
const downloadId = extractEdgeIdFromUrl(url);
|
|
190
|
+
if (!downloadId) throw new errors_extensionFromStoreError('NotFound', 'Edge extension id not found in URL');
|
|
191
|
+
return {
|
|
192
|
+
store,
|
|
193
|
+
downloadUrl: getEdgeDownloadUrl(downloadId),
|
|
194
|
+
archiveType: 'crx',
|
|
195
|
+
downloadId,
|
|
196
|
+
slugOrId: downloadId
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
const slug = extractFirefoxSlugFromUrl(url);
|
|
200
|
+
if (!slug) throw new errors_extensionFromStoreError('NotFound', 'Firefox extension slug not found in URL');
|
|
201
|
+
const firefox = await resolveFirefoxDownload(slug, options.version, {
|
|
202
|
+
userAgent: options.userAgent,
|
|
203
|
+
logger: options.logger,
|
|
204
|
+
requestJson: options.requestJson
|
|
205
|
+
});
|
|
206
|
+
return {
|
|
207
|
+
store,
|
|
208
|
+
downloadUrl: firefox.downloadUrl,
|
|
209
|
+
archiveType: 'xpi',
|
|
210
|
+
versionHint: firefox.version,
|
|
211
|
+
slugOrId: firefox.slugOrId
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
const TEXT_FILE_PATTERN = /(^|\/)(?:[^/]+\.(?:txt|md|mdx|json|js|jsx|mjs|cjs|ts|tsx|css|scss|sass|less|html|xml|svg|yml|yaml|toml|ini|conf|map)|\.(?:gitignore|npmrc|editorconfig|prettierrc|eslintrc))$/i;
|
|
215
|
+
function getDefaultFetch() {
|
|
216
|
+
if ('function' != typeof globalThis.fetch) throw new errors_extensionFromStoreError('DownloadFailed', 'No fetch implementation was provided');
|
|
217
|
+
return globalThis.fetch.bind(globalThis);
|
|
218
|
+
}
|
|
219
|
+
function inferBrowserChromePlatformInfo() {
|
|
220
|
+
var _navigatorLike_userAgentData;
|
|
221
|
+
const navigatorLike = globalThis.navigator;
|
|
222
|
+
const fingerprint = [
|
|
223
|
+
null == navigatorLike ? void 0 : navigatorLike.platform,
|
|
224
|
+
null == navigatorLike ? void 0 : navigatorLike.userAgent,
|
|
225
|
+
null == navigatorLike ? void 0 : null == (_navigatorLike_userAgentData = navigatorLike.userAgentData) ? void 0 : _navigatorLike_userAgentData.platform
|
|
226
|
+
].filter(Boolean).join(' ').toLowerCase();
|
|
227
|
+
const os = fingerprint.includes('mac') ? 'mac' : fingerprint.includes('win') ? 'win' : 'linux';
|
|
228
|
+
const arch = fingerprint.includes('arm') || fingerprint.includes('aarch64') ? 'arm64' : fingerprint.includes('i686') || fingerprint.includes('i386') || fingerprint.includes('x86') && !fingerprint.includes('x86_64') ? 'x86' : 'x64';
|
|
229
|
+
return {
|
|
230
|
+
os,
|
|
231
|
+
arch
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function isLikelyTextFile(path) {
|
|
235
|
+
return 'manifest.json' === path || TEXT_FILE_PATTERN.test(path);
|
|
236
|
+
}
|
|
237
|
+
function decodeText(bytes) {
|
|
238
|
+
return (0, __WEBPACK_EXTERNAL_MODULE_fflate__.strFromU8)(bytes);
|
|
239
|
+
}
|
|
240
|
+
function mapHttpError(url, status) {
|
|
241
|
+
if (404 === status) return new errors_extensionFromStoreError('NotFound', `Extension not found at ${url}`);
|
|
242
|
+
if (401 === status || 403 === status) return new errors_extensionFromStoreError('NotPublic', 'Extension is not publicly downloadable');
|
|
243
|
+
return new errors_extensionFromStoreError('DownloadFailed', `Failed to request ${url} (HTTP ${status})`);
|
|
244
|
+
}
|
|
245
|
+
async function requestJsonWithFetch(url, options) {
|
|
246
|
+
if (options.userAgent) createLogger(options.logger).warn('Custom user agents are ignored in browser environments.');
|
|
247
|
+
const response = await options.fetchImpl(url);
|
|
248
|
+
if (!response.ok) throw mapHttpError(url, response.status);
|
|
249
|
+
const body = await response.text();
|
|
250
|
+
try {
|
|
251
|
+
return JSON.parse(body);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
throw new errors_extensionFromStoreError('StoreIncompatibility', `Invalid JSON response from ${url}`, error);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
async function downloadBytes(url, options) {
|
|
257
|
+
if (options.userAgent) createLogger(options.logger).warn('Custom user agents are ignored in browser environments.');
|
|
258
|
+
const response = await options.fetchImpl(url);
|
|
259
|
+
if (!response.ok) throw mapHttpError(url, response.status);
|
|
260
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
261
|
+
return {
|
|
262
|
+
finalUrl: response.url || url,
|
|
263
|
+
bytes
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
function buildBrowserFiles(entries) {
|
|
267
|
+
return Object.entries(entries).sort(([left], [right])=>left.localeCompare(right)).map(([path, bytes])=>({
|
|
268
|
+
path,
|
|
269
|
+
bytes,
|
|
270
|
+
text: isLikelyTextFile(path) ? decodeText(bytes) : void 0
|
|
271
|
+
}));
|
|
272
|
+
}
|
|
273
|
+
async function fetchExtensionFromStoreBrowser(url, options = {}) {
|
|
274
|
+
validateInput(url);
|
|
275
|
+
const fetchImpl = options.fetch || getDefaultFetch();
|
|
276
|
+
const resolved = await resolveDownload(url, {
|
|
277
|
+
version: options.version,
|
|
278
|
+
userAgent: options.userAgent,
|
|
279
|
+
logger: options.logger,
|
|
280
|
+
platform: options.platform || inferBrowserChromePlatformInfo(),
|
|
281
|
+
requestJson: (requestUrl, requestOptions)=>requestJsonWithFetch(requestUrl, {
|
|
282
|
+
...requestOptions,
|
|
283
|
+
fetchImpl
|
|
284
|
+
})
|
|
285
|
+
});
|
|
286
|
+
const archive = await downloadBytes(resolved.downloadUrl, {
|
|
287
|
+
fetchImpl,
|
|
288
|
+
userAgent: options.userAgent,
|
|
289
|
+
logger: options.logger
|
|
290
|
+
});
|
|
291
|
+
const zipPayload = 'crx' === resolved.archiveType ? stripCrxHeader(archive.bytes) : archive.bytes;
|
|
292
|
+
let filesByPath;
|
|
293
|
+
try {
|
|
294
|
+
filesByPath = (0, __WEBPACK_EXTERNAL_MODULE_fflate__.unzipSync)(zipPayload);
|
|
295
|
+
} catch (error) {
|
|
296
|
+
throw new errors_extensionFromStoreError('ExtractionFailed', 'Failed to extract extension archive', error);
|
|
297
|
+
}
|
|
298
|
+
const manifestBytes = filesByPath['manifest.json'];
|
|
299
|
+
if (!manifestBytes) throw new errors_extensionFromStoreError('ExtractionFailed', 'manifest.json was not found after extraction');
|
|
300
|
+
const manifestInfo = parseManifestInfo(decodeText(manifestBytes));
|
|
301
|
+
const version = resolved.versionHint || manifestInfo.extensionVersion;
|
|
302
|
+
const files = buildBrowserFiles(filesByPath);
|
|
303
|
+
return {
|
|
304
|
+
store: resolved.store,
|
|
305
|
+
identifier: resolved.slugOrId,
|
|
306
|
+
version,
|
|
307
|
+
manifestVersion: manifestInfo.manifestVersion,
|
|
308
|
+
archiveType: resolved.archiveType,
|
|
309
|
+
downloadUrl: archive.finalUrl,
|
|
310
|
+
archiveBytes: archive.bytes,
|
|
311
|
+
manifest: manifestInfo.manifest,
|
|
312
|
+
files,
|
|
313
|
+
meta: {
|
|
314
|
+
store: resolved.store,
|
|
315
|
+
identifier: resolved.slugOrId,
|
|
316
|
+
version,
|
|
317
|
+
manifestVersion: manifestInfo.manifestVersion
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
export { fetchExtensionFromStoreBrowser };
|
package/dist/core.cjs
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
extractEdgeIdFromUrl: ()=>extractEdgeIdFromUrl,
|
|
28
|
+
detectStoreFromUrl: ()=>detectStoreFromUrl,
|
|
29
|
+
extractChromeIdFromUrl: ()=>extractChromeIdFromUrl,
|
|
30
|
+
createLogger: ()=>createLogger,
|
|
31
|
+
stripCrxHeader: ()=>stripCrxHeader,
|
|
32
|
+
sanitizeSegment: ()=>sanitizeSegment,
|
|
33
|
+
getChromeDownloadUrl: ()=>getChromeDownloadUrl,
|
|
34
|
+
asExtensionFromStoreError: ()=>asExtensionFromStoreError,
|
|
35
|
+
extractFirefoxSlugFromUrl: ()=>extractFirefoxSlugFromUrl,
|
|
36
|
+
resolveFirefoxDownload: ()=>resolveFirefoxDownload,
|
|
37
|
+
validateInput: ()=>validateInput,
|
|
38
|
+
getEdgeDownloadUrl: ()=>getEdgeDownloadUrl,
|
|
39
|
+
resolveDownload: ()=>resolveDownload,
|
|
40
|
+
extensionFromStoreError: ()=>extensionFromStoreError,
|
|
41
|
+
parseManifestInfo: ()=>parseManifestInfo,
|
|
42
|
+
normalizeChromePlatformInfo: ()=>normalizeChromePlatformInfo
|
|
43
|
+
});
|
|
44
|
+
function _define_property(obj, key, value) {
|
|
45
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
46
|
+
value: value,
|
|
47
|
+
enumerable: true,
|
|
48
|
+
configurable: true,
|
|
49
|
+
writable: true
|
|
50
|
+
});
|
|
51
|
+
else obj[key] = value;
|
|
52
|
+
return obj;
|
|
53
|
+
}
|
|
54
|
+
class extensionFromStoreError extends Error {
|
|
55
|
+
constructor(code, message, cause){
|
|
56
|
+
super(message), _define_property(this, "code", void 0), _define_property(this, "cause", void 0);
|
|
57
|
+
this.code = code;
|
|
58
|
+
this.cause = cause;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function asExtensionFromStoreError(error, fallback) {
|
|
62
|
+
if (error instanceof extensionFromStoreError) return error;
|
|
63
|
+
return new extensionFromStoreError(fallback.code, fallback.message, error);
|
|
64
|
+
}
|
|
65
|
+
function createLogger(logger) {
|
|
66
|
+
return {
|
|
67
|
+
info: (message)=>{
|
|
68
|
+
var _logger_onInfo;
|
|
69
|
+
return null == logger ? void 0 : null == (_logger_onInfo = logger.onInfo) ? void 0 : _logger_onInfo.call(logger, message);
|
|
70
|
+
},
|
|
71
|
+
warn: (message)=>{
|
|
72
|
+
var _logger_onWarn;
|
|
73
|
+
return null == logger ? void 0 : null == (_logger_onWarn = logger.onWarn) ? void 0 : _logger_onWarn.call(logger, message);
|
|
74
|
+
},
|
|
75
|
+
error: (message, error)=>{
|
|
76
|
+
var _logger_onError;
|
|
77
|
+
return null == logger ? void 0 : null == (_logger_onError = logger.onError) ? void 0 : _logger_onError.call(logger, message, error);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function parseManifestInfo(raw) {
|
|
82
|
+
let parsed;
|
|
83
|
+
try {
|
|
84
|
+
parsed = JSON.parse(raw);
|
|
85
|
+
} catch (error) {
|
|
86
|
+
throw new extensionFromStoreError('ExtractionFailed', 'manifest.json is not valid JSON', error);
|
|
87
|
+
}
|
|
88
|
+
if (!parsed || 'object' != typeof parsed || Array.isArray(parsed)) throw new extensionFromStoreError('ExtractionFailed', 'manifest.json must contain an object');
|
|
89
|
+
const manifest = parsed;
|
|
90
|
+
const manifestVersion = manifest.manifest_version;
|
|
91
|
+
const extensionVersion = manifest.version;
|
|
92
|
+
if (2 !== manifestVersion && 3 !== manifestVersion) throw new extensionFromStoreError('ExtractionFailed', 'manifest_version must be 2 or 3');
|
|
93
|
+
if (!extensionVersion || 'string' != typeof extensionVersion) throw new extensionFromStoreError('ExtractionFailed', 'manifest.json is missing a version');
|
|
94
|
+
return {
|
|
95
|
+
manifest,
|
|
96
|
+
manifestVersion,
|
|
97
|
+
extensionVersion
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function normalizeChromePlatformInfo(platform) {
|
|
101
|
+
return {
|
|
102
|
+
os: platform.os,
|
|
103
|
+
arch: platform.arch,
|
|
104
|
+
naclArch: platform.naclArch || platform.arch
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
const DEFAULT_CHROME_PLATFORM = {
|
|
108
|
+
os: 'linux',
|
|
109
|
+
arch: 'x64'
|
|
110
|
+
};
|
|
111
|
+
function getChromeDownloadUrl(id, platformInfo = DEFAULT_CHROME_PLATFORM) {
|
|
112
|
+
const encoded = encodeURIComponent(id);
|
|
113
|
+
const platform = normalizeChromePlatformInfo(platformInfo);
|
|
114
|
+
const productId = 'chromiumcrx';
|
|
115
|
+
const productChannel = 'unknown';
|
|
116
|
+
const productVersion = '9999.0.9999.0';
|
|
117
|
+
return [
|
|
118
|
+
'https://clients2.google.com/service/update2/crx',
|
|
119
|
+
'?response=redirect',
|
|
120
|
+
`&os=${platform.os}`,
|
|
121
|
+
`&arch=${platform.arch}`,
|
|
122
|
+
`&os_arch=${platform.arch}`,
|
|
123
|
+
`&nacl_arch=${platform.naclArch}`,
|
|
124
|
+
`&prod=${productId}`,
|
|
125
|
+
`&prodchannel=${productChannel}`,
|
|
126
|
+
`&prodversion=${productVersion}`,
|
|
127
|
+
'&acceptformat=crx2,crx3',
|
|
128
|
+
`&x=id%3D${encoded}%26uc`
|
|
129
|
+
].join('');
|
|
130
|
+
}
|
|
131
|
+
function getEdgeDownloadUrl(id) {
|
|
132
|
+
const encoded = encodeURIComponent(id);
|
|
133
|
+
return [
|
|
134
|
+
'https://edge.microsoft.com/extensionwebstorebase/v1/crx',
|
|
135
|
+
'?response=redirect',
|
|
136
|
+
'&prodversion=109.0.0.0',
|
|
137
|
+
`&x=id%3D${encoded}%26installsource%3Dondemand%26uc`
|
|
138
|
+
].join('');
|
|
139
|
+
}
|
|
140
|
+
async function resolveFirefoxDownload(idOrSlug, versionHint, options) {
|
|
141
|
+
var _addon_current_version_file, _addon_current_version, _addon_current_version1;
|
|
142
|
+
const baseUrl = `https://addons.mozilla.org/api/v5/addons/addon/${encodeURIComponent(idOrSlug)}/`;
|
|
143
|
+
const addon = await options.requestJson(baseUrl, options);
|
|
144
|
+
const slugOrId = addon.slug || idOrSlug;
|
|
145
|
+
if (versionHint) {
|
|
146
|
+
var _version_file;
|
|
147
|
+
const versionUrl = `${baseUrl}versions/${encodeURIComponent(versionHint)}/`;
|
|
148
|
+
const version = await options.requestJson(versionUrl, options);
|
|
149
|
+
const downloadUrl = null == (_version_file = version.file) ? void 0 : _version_file.url;
|
|
150
|
+
if (!downloadUrl) throw new extensionFromStoreError('NotPublic', `Version ${versionHint} is not publicly downloadable`);
|
|
151
|
+
return {
|
|
152
|
+
downloadUrl,
|
|
153
|
+
version: version.version || versionHint,
|
|
154
|
+
slugOrId
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
const downloadUrl = null == (_addon_current_version = addon.current_version) ? void 0 : null == (_addon_current_version_file = _addon_current_version.file) ? void 0 : _addon_current_version_file.url;
|
|
158
|
+
const version = null == (_addon_current_version1 = addon.current_version) ? void 0 : _addon_current_version1.version;
|
|
159
|
+
if (!downloadUrl || !version) throw new extensionFromStoreError('NotPublic', 'Extension is not publicly downloadable');
|
|
160
|
+
return {
|
|
161
|
+
downloadUrl,
|
|
162
|
+
version,
|
|
163
|
+
slugOrId
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
const chromePattern = /^https?:\/\/(?:chrome\.google\.com\/webstore|chromewebstore\.google\.com)\/.+?\/([a-p]{32})(?=[\/#?]|$)/i;
|
|
167
|
+
const chromeDownloadPattern = /^https?:\/\/clients2\.google\.com\/service\/update2\/crx\b.*?%3D([a-p]{32})%26uc/i;
|
|
168
|
+
const edgePattern = /^https?:\/\/microsoftedge\.microsoft\.com\/addons\/.+?\/([a-z]{32})(?=[\/#?]|$)/i;
|
|
169
|
+
const edgeDownloadPattern = /^https?:\/\/edge\.microsoft\.com\/extensionwebstorebase\/v1\/crx\b.*?%3D([a-z]{32})%26/i;
|
|
170
|
+
const firefoxPattern = /^https?:\/\/((?:reviewers\.)?(?:addons\.mozilla\.org|addons(?:-dev)?\.allizom\.org))\/.*?(?:addon|review)\/([^/<>"'?#]+)/i;
|
|
171
|
+
const firefoxDownloadPattern = /^https?:\/\/(addons\.mozilla\.org|addons(?:-dev)?\.allizom\.org)\/[^?#]*\/downloads\/latest\/([^/?#]+)/i;
|
|
172
|
+
function detectStoreFromUrl(url) {
|
|
173
|
+
if (chromePattern.test(url) || chromeDownloadPattern.test(url)) return 'chrome';
|
|
174
|
+
if (edgePattern.test(url) || edgeDownloadPattern.test(url)) return 'edge';
|
|
175
|
+
if (firefoxPattern.test(url) || firefoxDownloadPattern.test(url)) return 'firefox';
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
function extractChromeIdFromUrl(url) {
|
|
179
|
+
const match = chromePattern.exec(url) || chromeDownloadPattern.exec(url);
|
|
180
|
+
return match ? match[1] : null;
|
|
181
|
+
}
|
|
182
|
+
function extractEdgeIdFromUrl(url) {
|
|
183
|
+
const match = edgePattern.exec(url) || edgeDownloadPattern.exec(url);
|
|
184
|
+
return match ? match[1] : null;
|
|
185
|
+
}
|
|
186
|
+
function extractFirefoxSlugFromUrl(url) {
|
|
187
|
+
const match = firefoxPattern.exec(url) || firefoxDownloadPattern.exec(url);
|
|
188
|
+
return match ? match[2] : null;
|
|
189
|
+
}
|
|
190
|
+
function validateInput(url) {
|
|
191
|
+
if (!url || 'string' != typeof url) throw new extensionFromStoreError('InvalidInput', 'URL is required');
|
|
192
|
+
}
|
|
193
|
+
function sanitizeSegment(value, label) {
|
|
194
|
+
const sanitized = value.replace(/[\\/]/g, '-').trim();
|
|
195
|
+
if (!sanitized) throw new extensionFromStoreError('InvalidInput', `${label} is not a valid path segment`);
|
|
196
|
+
return sanitized;
|
|
197
|
+
}
|
|
198
|
+
async function resolveDownload(url, options) {
|
|
199
|
+
const store = detectStoreFromUrl(url);
|
|
200
|
+
if (!store) throw new extensionFromStoreError('UnsupportedStore', 'URL does not match a supported store');
|
|
201
|
+
if ('chrome' === store) {
|
|
202
|
+
const downloadId = extractChromeIdFromUrl(url);
|
|
203
|
+
if (!downloadId) throw new extensionFromStoreError('NotFound', 'Chrome extension id not found in URL');
|
|
204
|
+
return {
|
|
205
|
+
store,
|
|
206
|
+
downloadUrl: getChromeDownloadUrl(downloadId, options.platform),
|
|
207
|
+
archiveType: 'crx',
|
|
208
|
+
downloadId,
|
|
209
|
+
slugOrId: downloadId
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
if ('edge' === store) {
|
|
213
|
+
const downloadId = extractEdgeIdFromUrl(url);
|
|
214
|
+
if (!downloadId) throw new extensionFromStoreError('NotFound', 'Edge extension id not found in URL');
|
|
215
|
+
return {
|
|
216
|
+
store,
|
|
217
|
+
downloadUrl: getEdgeDownloadUrl(downloadId),
|
|
218
|
+
archiveType: 'crx',
|
|
219
|
+
downloadId,
|
|
220
|
+
slugOrId: downloadId
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
const slug = extractFirefoxSlugFromUrl(url);
|
|
224
|
+
if (!slug) throw new extensionFromStoreError('NotFound', 'Firefox extension slug not found in URL');
|
|
225
|
+
const firefox = await resolveFirefoxDownload(slug, options.version, {
|
|
226
|
+
userAgent: options.userAgent,
|
|
227
|
+
logger: options.logger,
|
|
228
|
+
requestJson: options.requestJson
|
|
229
|
+
});
|
|
230
|
+
return {
|
|
231
|
+
store,
|
|
232
|
+
downloadUrl: firefox.downloadUrl,
|
|
233
|
+
archiveType: 'xpi',
|
|
234
|
+
versionHint: firefox.version,
|
|
235
|
+
slugOrId: firefox.slugOrId
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
function readUInt32LE(bytes, offset) {
|
|
239
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset + offset, Uint32Array.BYTES_PER_ELEMENT);
|
|
240
|
+
return view.getUint32(0, true);
|
|
241
|
+
}
|
|
242
|
+
function readMagic(bytes) {
|
|
243
|
+
let out = '';
|
|
244
|
+
const slice = bytes.subarray(0, 4);
|
|
245
|
+
for(let index = 0; index < slice.length; index += 1)out += String.fromCharCode(slice[index]);
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
248
|
+
function stripCrxHeader(buffer) {
|
|
249
|
+
if (buffer.length < 16) throw new extensionFromStoreError('ExtractionFailed', 'CRX file too small');
|
|
250
|
+
const magic = readMagic(buffer);
|
|
251
|
+
if ('Cr24' !== magic) throw new extensionFromStoreError('ExtractionFailed', 'Invalid CRX header');
|
|
252
|
+
const version = readUInt32LE(buffer, 4);
|
|
253
|
+
if (2 === version) {
|
|
254
|
+
const publicKeyLength = readUInt32LE(buffer, 8);
|
|
255
|
+
const signatureLength = readUInt32LE(buffer, 12);
|
|
256
|
+
const headerSize = 16 + publicKeyLength + signatureLength;
|
|
257
|
+
return buffer.subarray(headerSize);
|
|
258
|
+
}
|
|
259
|
+
if (3 === version) {
|
|
260
|
+
const headerSize = readUInt32LE(buffer, 8);
|
|
261
|
+
return buffer.subarray(12 + headerSize);
|
|
262
|
+
}
|
|
263
|
+
throw new extensionFromStoreError('ExtractionFailed', `Unsupported CRX version ${version}`);
|
|
264
|
+
}
|
|
265
|
+
exports.asExtensionFromStoreError = __webpack_exports__.asExtensionFromStoreError;
|
|
266
|
+
exports.createLogger = __webpack_exports__.createLogger;
|
|
267
|
+
exports.detectStoreFromUrl = __webpack_exports__.detectStoreFromUrl;
|
|
268
|
+
exports.extensionFromStoreError = __webpack_exports__.extensionFromStoreError;
|
|
269
|
+
exports.extractChromeIdFromUrl = __webpack_exports__.extractChromeIdFromUrl;
|
|
270
|
+
exports.extractEdgeIdFromUrl = __webpack_exports__.extractEdgeIdFromUrl;
|
|
271
|
+
exports.extractFirefoxSlugFromUrl = __webpack_exports__.extractFirefoxSlugFromUrl;
|
|
272
|
+
exports.getChromeDownloadUrl = __webpack_exports__.getChromeDownloadUrl;
|
|
273
|
+
exports.getEdgeDownloadUrl = __webpack_exports__.getEdgeDownloadUrl;
|
|
274
|
+
exports.normalizeChromePlatformInfo = __webpack_exports__.normalizeChromePlatformInfo;
|
|
275
|
+
exports.parseManifestInfo = __webpack_exports__.parseManifestInfo;
|
|
276
|
+
exports.resolveDownload = __webpack_exports__.resolveDownload;
|
|
277
|
+
exports.resolveFirefoxDownload = __webpack_exports__.resolveFirefoxDownload;
|
|
278
|
+
exports.sanitizeSegment = __webpack_exports__.sanitizeSegment;
|
|
279
|
+
exports.stripCrxHeader = __webpack_exports__.stripCrxHeader;
|
|
280
|
+
exports.validateInput = __webpack_exports__.validateInput;
|
|
281
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
282
|
+
"asExtensionFromStoreError",
|
|
283
|
+
"createLogger",
|
|
284
|
+
"detectStoreFromUrl",
|
|
285
|
+
"extensionFromStoreError",
|
|
286
|
+
"extractChromeIdFromUrl",
|
|
287
|
+
"extractEdgeIdFromUrl",
|
|
288
|
+
"extractFirefoxSlugFromUrl",
|
|
289
|
+
"getChromeDownloadUrl",
|
|
290
|
+
"getEdgeDownloadUrl",
|
|
291
|
+
"normalizeChromePlatformInfo",
|
|
292
|
+
"parseManifestInfo",
|
|
293
|
+
"resolveDownload",
|
|
294
|
+
"resolveFirefoxDownload",
|
|
295
|
+
"sanitizeSegment",
|
|
296
|
+
"stripCrxHeader",
|
|
297
|
+
"validateInput"
|
|
298
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
299
|
+
Object.defineProperty(exports, '__esModule', {
|
|
300
|
+
value: true
|
|
301
|
+
});
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { extensionFromStoreError, asExtensionFromStoreError, type ErrorCode, } from './errors';
|
|
2
|
+
export { createLogger, type Logger } from './logger';
|
|
3
|
+
export { parseManifestInfo, type ExtensionManifest, type ManifestInfo, } from './manifest';
|
|
4
|
+
export { normalizeChromePlatformInfo, type ChromePlatformArch, type ChromePlatformInfo, type ChromePlatformOs, } from './platform';
|
|
5
|
+
export { resolveDownload, sanitizeSegment, validateInput, type ResolveDownloadOptions, type ResolvedDownload, } from './resolve';
|
|
6
|
+
export { stripCrxHeader } from './crx';
|
|
7
|
+
export { getChromeDownloadUrl } from './stores/chrome';
|
|
8
|
+
export { getEdgeDownloadUrl } from './stores/edge';
|
|
9
|
+
export { resolveFirefoxDownload, type JsonRequester } from './stores/firefox';
|
|
10
|
+
export { detectStoreFromUrl, extractChromeIdFromUrl, extractEdgeIdFromUrl, extractFirefoxSlugFromUrl, type StoreFromUrl, } from './stores/resolve-slug';
|