Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: compatible verdaccio path style #723

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/port/controller/package/DownloadPackageVersionTar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,24 @@ export class DownloadPackageVersionTarController extends AbstractController {
});
return res;
}

// Compatible Verdaccio path style

@HTTPMethod({
// GET /:fullname/-/:scope/:filenameWithVersion.tgz
path: `/:fullname(${FULLNAME_REG_STRING})/-/:scope/:filenameWithVersion.tgz`,
fangzhengjin marked this conversation as resolved.
Show resolved Hide resolved
method: HTTPMethodEnum.OPTIONS,
})
async downloadVerdaccioPathStyleorOptions(@Context() ctx: EggContext) {
return this.downloadForOptions(ctx);
}

@HTTPMethod({
// GET /:fullname/-/:scope/:filenameWithVersion.tgz
path: `/:fullname(${FULLNAME_REG_STRING})/-/:scope/:filenameWithVersion.tgz`,
method: HTTPMethodEnum.GET,
})
async downloadVerdaccioPathStyle(@Context() ctx: EggContext, @HTTPParam() fullname: string, @HTTPParam() filenameWithVersion: string) {
return this.download(ctx, fullname, filenameWithVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ describe('test/port/controller/package/DownloadPackageVersionTarController.test.
nfsClientAdapter = await app.getEggObject(NFSClientAdapter);
});

const scopedName = '@cnpm/testmodule-download-version-tar';
const scope = '@cnpm';
const name = 'testmodule-download-version-tar';
const scopedName = `${scope}/${name}`;
beforeEach(async () => {
mock(app.config.cnpmcore, 'allowPublishNonScopePackage', true);
let pkg = await TestUtil.getFullPackage({ name, version: '1.0.0' });
Expand Down Expand Up @@ -367,4 +368,52 @@ describe('test/port/controller/package/DownloadPackageVersionTarController.test.
assert(res.body.error === `[NOT_FOUND] "${name}-1.0.0.tgz" not found`);
});
});

describe('[GET /:fullname/-/:scope/:name-:version.tgz] download()', () => {
it('should download a version tar redirect to mock cdn success', async () => {
mock(nfsClientAdapter, 'url', async (storeKey: string) => {
// console.log('call url: ', storeKey);
return `https://cdn.mock.com${storeKey}`;
});
let res = await app.httpRequest()
.get(`/${name}/-/${scope}/${name}-1.0.0.tgz`);
assert(res.status === 302);
assert(res.headers.location === `https://cdn.mock.com/packages/${name}/1.0.0/${name}-1.0.0.tgz`);
res = await app.httpRequest()
.get(`/${scopedName}/-/${scope}/${name}-1.0.0.tgz`);
assert(res.status === 302);
assert(res.headers.location === `https://cdn.mock.com/packages/${scopedName}/1.0.0/${name}-1.0.0.tgz`);
});

it('should download a version tar with streaming success', async () => {
mock(nfsClientAdapter, 'url', 'not-function');
const res = await app.httpRequest()
.get(`/${name}/-/${scope}/${name}-1.0.0.tgz`);
assert(res.status === 200);
assert(res.headers['content-type'] === 'application/octet-stream');
assert(res.headers['content-disposition'] === `attachment; filename="${name}-1.0.0.tgz"`);

await app.httpRequest()
.get(`/${scopedName}/-/${scope}/${name}-1.0.0.tgz`);
assert(res.status === 200);
assert(res.headers['content-type'] === 'application/octet-stream');
assert(res.headers['content-disposition'] === `attachment; filename="${name}-1.0.0.tgz"`);
});

it('should mock getDownloadUrlOrStream return undefined', async () => {
mock(nfsClientAdapter, 'createDownloadStream', async () => {
return undefined;
});
if (process.env.CNPMCORE_NFS_TYPE === 'oss') {
mock(nfsClientAdapter, 'url', async () => {
return undefined;
});
}
const res = await app.httpRequest()
.get(`/${name}/-/${scope}/${name}-1.0.0.tgz`);
assert(res.status === 404);
assert(res.headers['content-type'] === 'application/json; charset=utf-8');
assert(res.body.error === `[NOT_FOUND] "${name}-1.0.0.tgz" not found`);
});
});
fangzhengjin marked this conversation as resolved.
Show resolved Hide resolved
});
Loading