Skip to content

Commit cba2bb5

Browse files
committed
Use local paths for tarballs
The path for the tarball should be local to the project and not some file path. This fixes functionality with latest npm
1 parent 3b7fa2b commit cba2bb5

File tree

6 files changed

+56
-10
lines changed

6 files changed

+56
-10
lines changed

src/registry.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import { URL } from 'url';
21+
import { baseTarballName } from './utils';
2122

2223
export type PkgJsonRW = {
2324
name: string,
@@ -82,7 +83,7 @@ export class Registry {
8283
for (let i = 0; i < this.pkgs.length; i = i + 1) {
8384
if (this.pkgs[i].name === pkg_name) {
8485
const pkg = JSON.parse(JSON.stringify(this.pkgs[i]));
85-
pkg['dist']['tarball'] = this.requestHandler.url + pkg.dist.tarball;
86+
pkg['dist']['tarball'] = this.requestHandler.url + '-/' + baseTarballName(pkg.dist.tarball);
8687

8788
obj.versions[pkg.version] = pkg;
8889
}
@@ -144,7 +145,10 @@ export class Registry {
144145
return backend_processors;
145146
}
146147

147-
public isRegistered(path: string): boolean {
148-
return this.pkgs.filter(json => json.dist.tarball === "-/" + path).length === 1;
148+
public archiveFile(file: string): (string|null) {
149+
const files = this.pkgs.filter(json => (baseTarballName(json.dist.tarball) === file))
150+
if (files.length !== 1)
151+
return null;
152+
return files[0].dist.tarball.substring(2);
149153
}
150154
};

src/service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ export class Service {
4949

5050
if (split[0] === '-') {
5151
split.shift();
52-
// console.log("archive joiner: " + split.join('/'))
52+
console.log("archive joiner: " + split.join('/'))
5353
return {
5454
type: 'archive',
55-
package: '/' + split.join('/'),
55+
package: split.join('/'),
5656
}
5757
}
5858

@@ -117,11 +117,12 @@ export class Service {
117117
break;
118118
}
119119
case 'archive': {
120-
if (registry.isRegistered(<string>req_type.package)) {
120+
const archive_path = registry.archiveFile(<string>req_type.package)
121+
if (archive_path) {
121122
res.writeHead(200, {
122123
'Content-Type': 'application/x-compressed-tar'
123124
});
124-
fs.createReadStream(<string>req_type.package).pipe(res);
125+
fs.createReadStream(archive_path).pipe(res);
125126
}
126127
else {
127128
console.log("request: (archive not found)" + req.url);

src/utils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* npm_install_proxy -- localhost NPM registry to `npm install` without network
3+
*
4+
* Copyright (C) 2020 SUSE LLC
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
function baseTarballName(path:string): string {
21+
const path_components = path.split('/')
22+
return path_components[path_components.length-1]
23+
}
24+
25+
export {
26+
baseTarballName
27+
}

tests/registry.ispec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("directory registry integration tests", function () {
5050
},
5151
"dist": {
5252
"integrity": "sha512-N+aAxov+CKVS3JuhDIQFr24XvZvwE96Wlhk9dytTg/GmwWoghdOvR8dspx8MVz71O+Y0pA3UPqHF68D6iy8UvQ==",
53-
"tarball": "http://localhost:8888/-/tests/pkgs/assert-1.4.1.tgz",
53+
"tarball": "http://localhost:8888/-/assert-1.4.1.tgz",
5454
},
5555
"homepage": "https://github.com/defunctzombie/commonjs-assert",
5656
"keywords": [

tests/service.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ describe("server request processing", function() {
238238
return makeRequest("/nothing/ver", b => base_url=b).then(res => {
239239
expect(res).toStrictEqual({
240240
"dist": {
241-
"tarball": base_url + "test",
241+
"tarball": base_url + "-/test",
242242
},
243243
"name": "nothing",
244244
"version": "ver",
@@ -251,7 +251,7 @@ describe("server request processing", function() {
251251

252252
return makeRequest("/@scope%2fbadthings/ver", b => base_url=b).then(res => {
253253
expect(res).toStrictEqual({
254-
"dist": { "tarball": base_url + "bad2" },
254+
"dist": { "tarball": base_url + "-/bad2" },
255255
"name": "@scope/badthings",
256256
"version": "ver"
257257
});

tests/utils.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as utils from '../src/utils'
2+
3+
describe("utility tests", function() {
4+
it("returns basename of path", function() {
5+
6+
expect(utils.baseTarballName("test")).toBe("test");
7+
expect(utils.baseTarballName("test1/test2")).toBe("test2");
8+
expect(utils.baseTarballName("t1/t2/t3//")).toBe("");
9+
expect(utils.baseTarballName("///test/3")).toBe('3');
10+
expect(utils.baseTarballName('11/22/3/')).toBe('');
11+
expect(utils.baseTarballName('')).toBe('');
12+
expect(utils.baseTarballName('-//home/adamm/work/cockpit/systemsmanagement🚀cockpit/cockpit/cockpit/../t/read-pkg-semver-5.7.2.tgz')).toBe('read-pkg-semver-5.7.2.tgz')
13+
})
14+
})

0 commit comments

Comments
 (0)