Skip to content

Commit

Permalink
fix: unpkg support non-npm publish tgz file (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored May 31, 2023
1 parent a7258aa commit 5fe883f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/core/service/PackageVersionFileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ export class PackageVersionFileService extends AbstractService {
strip: 1,
onentry: entry => {
if (entry.type !== 'File') return;
if (!entry.path.startsWith('package/')) return;
// ignore hidden dir
if (entry.path.includes('/./')) return;
paths.push(entry.path.replace(/^package\//i, '/'));
// https://github.com/cnpm/cnpmcore/issues/452#issuecomment-1570077310
// strip first dir, e.g.: 'package/', 'lodash-es/'
paths.push('/' + entry.path.split('/').slice(1).join('/'));
},
});
for (const path of paths) {
Expand Down
Binary file added test/fixtures/unpkg.com/lodash-es-4.17.7.tgz
Binary file not shown.
40 changes: 40 additions & 0 deletions test/port/controller/PackageVersionFileController/raw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,46 @@ describe('test/port/controller/PackageVersionFileController/raw.test.ts', () =>
assert.match(res.text, /ToOneFromχ/);
});

it('should support non-npm pack tgz file', async () => {
// https://github.com/cnpm/cnpmcore/issues/452#issuecomment-1570077310
const tarball = await TestUtil.readFixturesFile('unpkg.com/lodash-es-4.17.7.tgz');
const { integrity } = await calculateIntegrity(tarball);
const pkg = await TestUtil.getFullPackage({
name: '@cnpm/lodash-es',
version: '1.0.0',
versionObject: {
description: 'foo latest description',
},
attachment: {
data: tarball.toString('base64'),
length: tarball.length,
},
dist: {
integrity,
},
main: '',
});
let res = await app.httpRequest()
.put(`/${pkg.name}`)
.set('authorization', publisher.authorization)
.set('user-agent', publisher.ua)
.send(pkg);
assert.equal(res.status, 201);
res = await app.httpRequest()
.get(`/${pkg.name}/1.0.0/files/`);
assert.equal(res.status, 200);
assert(res.body.files.find((file: { path: string }) => file.path === '/package.json'));
res = await app.httpRequest()
.get(`/${pkg.name}/1.0.0/files/zipObjectDeep.d.ts`);
assert.equal(res.status, 200);
assert.equal(res.headers['content-type'], 'text/plain; charset=utf-8');
assert.match(res.text, /export default zipObjectDeep/);
res = await app.httpRequest()
.get(`/${pkg.name}/1.0.0/files`);
assert.equal(res.status, 302);
assert.equal(res.header.location, `/${pkg.name}/1.0.0/files/index.js`);
});

it('should ignore "." hidden dir', async () => {
// https://unpkg.com/browse/bovo-ui@0.0.4-36/
const tarball = await TestUtil.readFixturesFile('unpkg.com/bovo-ui-0.0.4-36.tgz');
Expand Down

0 comments on commit 5fe883f

Please sign in to comment.