Skip to content

Commit

Permalink
feat: allow OPTIONS request on tgz downlaod url (#605)
Browse files Browse the repository at this point in the history
make webcontainer can run npm install on cnpmcore registry
  • Loading branch information
fengmk2 authored Nov 3, 2023
1 parent 31946ba commit 5bedb25
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/port/controller/package/DownloadPackageVersionTar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ export class DownloadPackageVersionTarController extends AbstractController {
@Inject()
private nfsAdapter: NFSAdapter;

// Support OPTIONS Request on tgz download
@HTTPMethod({
// GET /:fullname/-/:filenameWithVersion.tgz
path: `/:fullname(${FULLNAME_REG_STRING})/-/:filenameWithVersion.tgz`,
method: HTTPMethodEnum.OPTIONS,
})
async downloadForOptions(@Context() ctx: EggContext) {
ctx.status = 204;
}

@HTTPMethod({
// GET /:fullname/-/:filenameWithVersion.tgz
path: `/:fullname(${FULLNAME_REG_STRING})/-/:filenameWithVersion.tgz`,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"eggjs-elasticsearch": "^0.0.6",
"fs-cnpm": "^2.4.0",
"ioredis": "^5.3.1",
"leoric": "^2.6.2",
"leoric": "^2.11.5",
"lodash": "^4.17.21",
"mime-types": "^2.1.35",
"mysql": "^2.18.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ describe('test/port/controller/package/DownloadPackageVersionTarController.test.
assert(res.headers.location === `https://cdn.mock.com/packages/${scopedName}/1.0.0/${name}-1.0.0.tgz`);
});

it('should support cors OPTIONS Request', async () => {
mock(nfsClientAdapter, 'url', async (storeKey: string) => {
return `https://cdn.mock.com${storeKey}`;
});

let res = await app.httpRequest()
.options(`/${name}/-/testmodule-download-version-tar-1.0.0.tgz`);
assert.equal(res.status, 204);
res = await app.httpRequest()
.options(`/${scopedName}/-/testmodule-download-version-tar-1.0.0.tgz`);
assert.equal(res.status, 204);
});

if (process.env.CNPMCORE_NFS_TYPE !== 'oss') {
it('should download a version tar redirect to mock cdn success with url function is not async function', async () => {
mock(nfsClientAdapter, 'url', (storeKey: string) => {
Expand Down

0 comments on commit 5bedb25

Please sign in to comment.