Skip to content

Commit

Permalink
fix: Check for existing import-map and npm package before POSTing og …
Browse files Browse the repository at this point in the history
…PUTing alias (#277)

* Check for existing import-map and npm package before POSTing og PUTing alias

* fix lint
  • Loading branch information
mfolkeseth authored Oct 12, 2021
1 parent 1780774 commit a6c65ef
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/handlers/alias.post.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Metrics = require('@metrics/client');

const {
createFilePathToAlias,
createFilePathToPackage,
createFilePathToAliasOrigin,
} = require('../utils/path-builders-fs');
const { createURIToAlias } = require('../utils/path-builders-uri');
const MultipartParser = require('../multipart/parser');
Expand Down Expand Up @@ -88,7 +88,7 @@ const AliasPost = class AliasPost {
})
.then(async (alias) => {
try {
const path = createFilePathToPackage({
const path = createFilePathToAliasOrigin({
org: alias.org,
type: alias.type,
name: alias.name,
Expand Down
6 changes: 3 additions & 3 deletions lib/handlers/alias.put.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Metrics = require('@metrics/client');

const {
createFilePathToAlias,
createFilePathToPackage,
createFilePathToAliasOrigin
} = require('../utils/path-builders-fs');
const { createURIToAlias } = require('../utils/path-builders-uri');
const MultipartParser = require('../multipart/parser');
Expand Down Expand Up @@ -88,12 +88,12 @@ const AliasPut = class AliasPut {
})
.then(async (alias) => {
try {
const path = createFilePathToPackage({
const path = createFilePathToAliasOrigin({
org: alias.org,
type: alias.type,
name: alias.name,
version: alias.version,
});
})
await this._sink.read(path);
} catch (error) {
this._log.error(
Expand Down
10 changes: 9 additions & 1 deletion lib/utils/path-builders-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ module.exports.createFilePathToAlias = createFilePathToAlias;
// Build file system path to an version file

const createFilePathToVersion = ({ org = '', type = '', name = '' } = {}) => path.join(ROOT, org, type, name, 'versions.json')
module.exports.createFilePathToVersion = createFilePathToVersion;
module.exports.createFilePathToVersion = createFilePathToVersion;

const createFilePathToAliasOrigin = ({org = '', type = '', name = '', version = '',} = {}) => {
if(type === 'map') {
return createFilePathToImportMap({org, name, version})
}
return createFilePathToPackage({org, type, name, version})
};
module.exports.createFilePathToAliasOrigin = createFilePathToAliasOrigin
27 changes: 14 additions & 13 deletions test/handlers/alias.post.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,23 @@ tap.test('alias.post() - URL parameters is URL encoded', async (t) => {
t.end();
});

tap.test('alias.post() - Prevent unexisting alias', async (t) => {
const sink = new Sink();
sink.set('/local/pkg/@foo/bar-lib/8.alias.json', 'payload');
tap.test('alias.post() - Prevent non-existing package to map to alias', async (t) => {
const sink = new Sink();
sink.set('/local/pkg/@foo/bar-lib/8.alias.json', 'payload');

const h = new Handler({ sink });
const h = new Handler({ sink });

const formData = new FormData();
formData.append('version', '8.1.4-1');
const formData = new FormData();
formData.append('version', '8.1.4-1');

const headers = formData.getHeaders();
const req = new Request({ headers });
formData.pipe(req);
const headers = formData.getHeaders();
const req = new Request({ headers });
formData.pipe(req);

const res = h.handler(req, 'anton', 'pkg', '%40foo%2Fbar-lib', '8');
const res = h.handler(req, 'anton', 'pkg', '%40foo%2Fbar-lib', '8');

t.rejects(res, HttpError.NotFound);
t.rejects(res, HttpError.NotFound);

t.end();
});
t.end();
},
);
82 changes: 81 additions & 1 deletion test/handlers/alias.put.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tap.test('alias.put() - URL parameters is URL encoded', async (t) => {
t.end();
});

tap.test('alias.put() - Prevent unexisting alias', async (t) => {
tap.test('alias.put() - Prevent non-existing package to map to alias', async (t) => {
const sink = new Sink();
const h = new Handler({ sink });

Expand All @@ -52,5 +52,85 @@ tap.test('alias.put() - Prevent unexisting alias', async (t) => {

t.rejects(res, HttpError.NotFound)

t.end();
});

tap.test('alias.put() - Map alias to existing npm package', async (t) => {
const sink = new Sink();
sink.set('/local/npm/@bar/baz/17.0.2.package.json', 'payload');

const h = new Handler({ sink });

const formData = new FormData();
formData.append('version', '17.0.2');

const headers = formData.getHeaders();
const req = new Request({ headers });
formData.pipe(req);

const res = await h.handler(req, 'anton', 'npm', '%40bar%2Fbaz', '8');

t.equal(res.statusCode, 303, 'should respond with expected status code');
t.equal(res.location, '/npm/@bar/baz/v8', '.location should be decoded');

t.end();
});

tap.test('alias.put() - Prevent non-existing npm to map to alias', async (t) => {
const sink = new Sink();

const h = new Handler({ sink });

const formData = new FormData();
formData.append('version', '17.0.2');

const headers = formData.getHeaders();
const req = new Request({ headers });
formData.pipe(req);

const res = h.handler(req, 'anton', 'npm', '%40bar%2Fbaz', '8');

t.rejects(res, HttpError.NotFound);

t.end();
});

tap.test('alias.put() - Map alias to existing import-map', async (t) => {
const sink = new Sink();
sink.set('/local/map/@cuz/fuzz/8.1.4.import-map.json', 'payload');

const h = new Handler({ sink });

const formData = new FormData();
formData.append('version', '8.1.4');

const headers = formData.getHeaders();
const req = new Request({ headers });
formData.pipe(req);

const res = await h.handler(req, 'anton', 'map', '%40cuz%2Ffuzz', '8');

t.equal(res.statusCode, 303, 'should respond with expected status code');
t.equal(res.location, '/map/@cuz/fuzz/v8', '.location should be decoded');

t.end();
});

tap.test('alias.put() - Prevent non-existing import-map to map to alias', async (t) => {
const sink = new Sink();

const h = new Handler({ sink });

const formData = new FormData();
formData.append('version', '8.1.4');

const headers = formData.getHeaders();
const req = new Request({ headers });
formData.pipe(req);

const res = h.handler(req, 'anton', 'map', '%40cuz%2Ffuzz', '8');
t.rejects(res, HttpError.NotFound);


t.end();
});

0 comments on commit a6c65ef

Please sign in to comment.