Skip to content

Commit

Permalink
fix: unpkg redirect (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr authored May 29, 2023
1 parent cc01398 commit c395c79
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/port/controller/PackageVersionFileController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ export class PackageVersionFileController extends AbstractController {
}
const { manifest } = await this.packageManagerService.showPackageVersionManifest(scope, name, versionOrTag);
// GET /foo/1.0.0/files => /foo/1.0.0/files/{main}
const indexFile = manifest?.main ?? 'index.js';
// ignore empty entry exp: @types/node@20.2.5/
const indexFile = manifest?.main || 'index.js';
ctx.redirect(join(ctx.path, indexFile));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@ describe('test/port/controller/PackageVersionFileController/listFiles.test.ts',
assert.equal(res.body.error, '[NOT_FOUND] Not Found');
});

it('should 404 when empty entry', async () => {
mock(app.config.cnpmcore, 'allowPublishNonScopePackage', true);
mock(app.config.cnpmcore, 'enableUnpkg', true);
const pkg = await TestUtil.getFullPackage({
name: 'foo',
version: '1.0.0',
versionObject: {
main: '',
description: 'empty main',
},
});
await app.httpRequest()
.put(`/${pkg.name}`)
.set('authorization', publisher.authorization)
.set('user-agent', publisher.ua)
.send(pkg)
.expect(201);
let res = await app.httpRequest()
.get('/foo/1.0.0/files')
.expect(302)
.expect('location', '/foo/1.0.0/files/index.js');

res = await app.httpRequest()
.get('/foo/1.0.0/files/index.js')
.expect(404)
.expect('content-type', 'application/json; charset=utf-8');
assert.equal(res.body.error, '[NOT_FOUND] File foo@1.0.0/index.js not found');
});

it('should list one package version files', async () => {
mock(app.config.cnpmcore, 'allowPublishNonScopePackage', true);
const pkg = await TestUtil.getFullPackage({
Expand Down

0 comments on commit c395c79

Please sign in to comment.