@pnpm/fetching.binary-fetcher 1000.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.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-2016 Rico Sta. Cruz and other contributors
4
+ Copyright (c) 2016-2025 Zoltan Kochan and other contributors
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # @pnpm/fetching.binary-fetcher
2
+
3
+ > A fetcher for binary archives
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ pnpm add @pnpm/fetching.binary-fetcher
9
+ ```
10
+
11
+ ## License
12
+
13
+ MIT
package/lib/index.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { type FetchFromRegistry } from '@pnpm/fetching-types';
2
+ import { type BinaryFetcher, type FetchFunction } from '@pnpm/fetcher-base';
3
+ export declare function createBinaryFetcher(ctx: {
4
+ fetch: FetchFromRegistry;
5
+ fetchFromRemoteTarball: FetchFunction;
6
+ rawConfig: Record<string, string>;
7
+ offline?: boolean;
8
+ }): {
9
+ binary: BinaryFetcher;
10
+ };
11
+ export interface AssetInfo {
12
+ url: string;
13
+ integrity: string;
14
+ basename: string;
15
+ }
16
+ /**
17
+ * Downloads and unpacks a zip file containing a binary asset.
18
+ *
19
+ * @param fetchFromRegistry - Function to fetch resources from registry
20
+ * @param assetInfo - Information about the binary asset
21
+ * @param targetDir - Directory where the binary asset should be installed
22
+ * @throws {PnpmError} When integrity verification fails or extraction fails
23
+ */
24
+ export declare function downloadAndUnpackZip(fetchFromRegistry: FetchFromRegistry, assetInfo: AssetInfo, targetDir: string): Promise<void>;
package/lib/index.js ADDED
@@ -0,0 +1,122 @@
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.createBinaryFetcher = createBinaryFetcher;
7
+ exports.downloadAndUnpackZip = downloadAndUnpackZip;
8
+ const path_1 = __importDefault(require("path"));
9
+ const promises_1 = __importDefault(require("fs/promises"));
10
+ const error_1 = require("@pnpm/error");
11
+ const worker_1 = require("@pnpm/worker");
12
+ const adm_zip_1 = __importDefault(require("adm-zip"));
13
+ const rename_overwrite_1 = __importDefault(require("rename-overwrite"));
14
+ const tempy_1 = __importDefault(require("tempy"));
15
+ const ssri_1 = __importDefault(require("ssri"));
16
+ function createBinaryFetcher(ctx) {
17
+ const fetchBinary = async (cafs, resolution, opts) => {
18
+ if (ctx.offline) {
19
+ throw new error_1.PnpmError('CANNOT_DOWNLOAD_BINARY_OFFLINE', `Cannot download binary "${resolution.url}" because offline mode is enabled.`);
20
+ }
21
+ const version = opts.pkg.version;
22
+ const manifest = {
23
+ name: opts.pkg.name,
24
+ version,
25
+ bin: resolution.bin,
26
+ };
27
+ if (resolution.archive === 'tarball') {
28
+ return {
29
+ ...await ctx.fetchFromRemoteTarball(cafs, {
30
+ tarball: resolution.url,
31
+ integrity: resolution.integrity,
32
+ }, opts),
33
+ manifest,
34
+ };
35
+ }
36
+ if (resolution.archive === 'zip') {
37
+ const tempLocation = await cafs.tempDir();
38
+ await downloadAndUnpackZip(ctx.fetch, {
39
+ url: resolution.url,
40
+ integrity: resolution.integrity,
41
+ basename: resolution.prefix ?? '',
42
+ }, tempLocation);
43
+ return {
44
+ ...await (0, worker_1.addFilesFromDir)({
45
+ storeDir: cafs.storeDir,
46
+ dir: tempLocation,
47
+ filesIndexFile: opts.filesIndexFile,
48
+ readManifest: false,
49
+ }),
50
+ manifest,
51
+ };
52
+ }
53
+ throw new error_1.PnpmError('NOT_SUPPORTED_ARCHIVE', `The binary fetcher doesn't support archive type ${resolution.archive}`);
54
+ };
55
+ return {
56
+ binary: fetchBinary,
57
+ };
58
+ }
59
+ /**
60
+ * Downloads and unpacks a zip file containing a binary asset.
61
+ *
62
+ * @param fetchFromRegistry - Function to fetch resources from registry
63
+ * @param assetInfo - Information about the binary asset
64
+ * @param targetDir - Directory where the binary asset should be installed
65
+ * @throws {PnpmError} When integrity verification fails or extraction fails
66
+ */
67
+ async function downloadAndUnpackZip(fetchFromRegistry, assetInfo, targetDir) {
68
+ const tmp = path_1.default.join(tempy_1.default.directory(), 'pnpm.zip');
69
+ try {
70
+ await downloadWithIntegrityCheck(fetchFromRegistry, assetInfo, tmp);
71
+ await extractZipToTarget(tmp, assetInfo.basename, targetDir);
72
+ }
73
+ finally {
74
+ // Clean up temporary file
75
+ try {
76
+ await promises_1.default.unlink(tmp);
77
+ }
78
+ catch {
79
+ // Ignore cleanup errors
80
+ }
81
+ }
82
+ }
83
+ /**
84
+ * Downloads a file with integrity verification.
85
+ */
86
+ async function downloadWithIntegrityCheck(fetchFromRegistry, { url, integrity }, tmpPath) {
87
+ const response = await fetchFromRegistry(url);
88
+ // Collect all chunks from the response
89
+ const chunks = [];
90
+ for await (const chunk of response.body) {
91
+ chunks.push(chunk);
92
+ }
93
+ const data = Buffer.concat(chunks);
94
+ try {
95
+ // Verify integrity if provided
96
+ ssri_1.default.checkData(data, integrity, { error: true });
97
+ }
98
+ catch (err) {
99
+ if (!(err instanceof Error) || !('expected' in err) || !('found' in err)) {
100
+ throw err;
101
+ }
102
+ throw new error_1.PnpmError('TARBALL_INTEGRITY', `Got unexpected checksum for "${url}". Wanted "${err.expected}". Got "${err.found}".`);
103
+ }
104
+ // Write the verified data to file
105
+ await promises_1.default.writeFile(tmpPath, data);
106
+ }
107
+ /**
108
+ * Extracts a zip file to the target directory.
109
+ *
110
+ * @param zipPath - Path to the zip file
111
+ * @param basename - Base name of the file (without extension)
112
+ * @param targetDir - Directory where contents should be extracted
113
+ * @throws {PnpmError} When extraction fails
114
+ */
115
+ async function extractZipToTarget(zipPath, basename, targetDir) {
116
+ const zip = new adm_zip_1.default(zipPath);
117
+ const nodeDir = basename === '' ? targetDir : path_1.default.dirname(targetDir);
118
+ const extractedDir = path_1.default.join(nodeDir, basename);
119
+ zip.extractAllTo(nodeDir, true);
120
+ await (0, rename_overwrite_1.default)(extractedDir, targetDir);
121
+ }
122
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAWA,kDAgDC;AAgBD,oDAkBC;AA7FD,gDAAuB;AACvB,2DAAoC;AACpC,uCAAuC;AAGvC,yCAA8C;AAC9C,sDAA4B;AAC5B,wEAA8C;AAC9C,kDAAyB;AACzB,gDAAuB;AAEvB,SAAgB,mBAAmB,CAAE,GAKpC;IACC,MAAM,WAAW,GAAkB,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,EAAE;QAClE,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;YAChB,MAAM,IAAI,iBAAS,CAAC,gCAAgC,EAAE,2BAA2B,UAAU,CAAC,GAAG,oCA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ACzE,MAAM,GAAG,CAAA;QACX,CAAC;QACD,MAAM,IAAI,iBAAS,CAAC,mBAAmB,EAAE,gCAAgC,GAAG,cAAc,GAAG,CAAC,QAAkB,WAAW,GAAG,CAAC,KAAe,IAAI,CAAC,CAAA;IACrJ,CAAC;IAED,kCAAkC;IAClC,MAAM,kBAAU,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,CAAA;AAC3C,CAAC;AAED;;;;;;;GAOG;AACH,KAAK,UAAU,kBAAkB,CAC/B,OAAe,EACf,QAAgB,EAChB,SAAiB;IAEjB,MAAM,GAAG,GAAG,IAAI,iBAAM,CAAC,OAAO,CAAC,CAAA;IAC/B,MAAM,OAAO,GAAG,QAAQ,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,cAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;IACrE,MAAM,YAAY,GAAG,cAAI,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAA;IAEjD,GAAG,CAAC,YAAY,CAAC,OAAO,EAAE,IAAI,CAAC,CAAA;IAC/B,MAAM,IAAA,0BAAe,EAAC,YAAY,EAAE,SAAS,CAAC,CAAA;AAChD,CAAC"}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@pnpm/fetching.binary-fetcher",
3
+ "version": "1000.0.0",
4
+ "description": "A fetcher for binary archives",
5
+ "keywords": [
6
+ "pnpm",
7
+ "pnpm10"
8
+ ],
9
+ "license": "MIT",
10
+ "funding": "https://opencollective.com/pnpm",
11
+ "repository": "https://github.com/pnpm/pnpm/blob/main/fetching/binary-fetcher",
12
+ "homepage": "https://github.com/pnpm/pnpm/blob/main/fetching/binary-fetcher#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/pnpm/pnpm/issues"
15
+ },
16
+ "type": "commonjs",
17
+ "main": "lib/index.js",
18
+ "types": "lib/index.d.ts",
19
+ "exports": {
20
+ ".": "./lib/index.js"
21
+ },
22
+ "files": [
23
+ "lib",
24
+ "!*.map"
25
+ ],
26
+ "dependencies": {
27
+ "adm-zip": "^0.5.16",
28
+ "rename-overwrite": "^6.0.2",
29
+ "ssri": "10.0.5",
30
+ "tempy": "^1.0.1",
31
+ "@pnpm/error": "1000.0.4",
32
+ "@pnpm/fetching-types": "1000.2.0",
33
+ "@pnpm/worker": "1000.1.11",
34
+ "@pnpm/fetcher-base": "1001.0.0"
35
+ },
36
+ "devDependencies": {
37
+ "@types/adm-zip": "^0.5.7",
38
+ "@types/ssri": "^7.1.5",
39
+ "@pnpm/fetching.binary-fetcher": "1000.0.0"
40
+ },
41
+ "engines": {
42
+ "node": ">=18.12"
43
+ },
44
+ "jest": {
45
+ "preset": "@pnpm/jest-config"
46
+ },
47
+ "scripts": {
48
+ "lint": "eslint \"src/**/*.ts\"",
49
+ "test": "pnpm run compile",
50
+ "compile": "tsc --build && pnpm run lint --fix"
51
+ }
52
+ }