Skip to content

Commit

Permalink
Improve error handling on upload (#126)
Browse files Browse the repository at this point in the history
* Improve error handling on upload

* Lint love
  • Loading branch information
trygve-lie authored May 8, 2020
1 parent 6d21a86 commit 8eb52f6
Show file tree
Hide file tree
Showing 24 changed files with 565 additions and 236 deletions.
4 changes: 2 additions & 2 deletions examples/pkg.alias.put.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ const put = async (address) => {
const auth = await authenticate(address);

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

const headers = {'Authorization': `Bearer ${auth.token}`, ...formData.getHeaders()};

const res = await fetch(`${address}/pkg/fuzz/v8`, {
const res = await fetch(`${address}/npm/lit-html/v1`, {
method: 'PUT',
body: formData,
headers,
Expand Down
4 changes: 2 additions & 2 deletions examples/pkg.put.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ const put = async (address) => {
const auth = await authenticate(address);

const formData = new FormData();
formData.append('package', fs.createReadStream('../fixtures/archive.tgz'));
formData.append('package', fs.createReadStream('./lit-html.tar'));

const headers = {'Authorization': `Bearer ${auth.token}`, ...formData.getHeaders()};

const res = await fetch(`${address}/pkg/fuzz/8.4.1`, {
const res = await fetch(`${address}/npm/lit-html/1.2.1`, {
method: 'PUT',
body: formData,
headers,
Expand Down
Binary file added fixtures/package.tar
Binary file not shown.
Binary file added fixtures/package.tar.bz2
Binary file not shown.
Binary file added fixtures/package.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/classes/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Asset = class Asset {
type = '',
org = '',
} = {}) {
this._mimeType = mime.getType(pathname) || '';
this._mimeType = mime.getType(pathname) || 'application/octet-stream';
this._type = type.toLowerCase();
this._size = -1;

Expand Down
20 changes: 10 additions & 10 deletions lib/handlers/alias.delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ const AliasDel = class AliasDel {
async handler(req, user, type, name, alias) {
const end = this._histogram.timer();

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`alias:del - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

try {
validators.alias(alias);
validators.name(name);
Expand All @@ -70,6 +60,16 @@ const AliasDel = class AliasDel {
throw e;
}

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`alias:del - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

const path = createFilePathToAlias({ org, type, name, alias });
const exist = await this._exist(path);
if (!exist) {
Expand Down
20 changes: 10 additions & 10 deletions lib/handlers/alias.get.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ const AliasGet = class AliasGet {
async handler(req, type, name, alias, extra) {
const end = this._histogram.timer();

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`alias:get - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

try {
validators.alias(alias);
validators.extra(extra);
Expand All @@ -64,6 +54,16 @@ const AliasGet = class AliasGet {
throw e;
}

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`alias:get - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

const path = createFilePathToAlias({ org, type, name, alias });

try {
Expand Down
20 changes: 10 additions & 10 deletions lib/handlers/alias.post.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,6 @@ const AliasPost = class AliasPost {
async handler(req, user, type, name, alias) {
const end = this._histogram.timer();

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`alias:post - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

try {
validators.alias(alias);
validators.name(name);
Expand All @@ -160,6 +150,16 @@ const AliasPost = class AliasPost {
throw e;
}

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`alias:post - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

const author = new Author(user);

const incoming = new HttpIncoming(req, {
Expand Down
20 changes: 10 additions & 10 deletions lib/handlers/alias.put.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,6 @@ const AliasPut = class AliasPut {
async handler(req, user, type, name, alias) {
const end = this._histogram.timer();

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`alias:put - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

try {
validators.alias(alias);
validators.name(name);
Expand All @@ -160,6 +150,16 @@ const AliasPut = class AliasPut {
throw e;
}

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`alias:put - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

const author = new Author(user);

const incoming = new HttpIncoming(req, {
Expand Down
20 changes: 10 additions & 10 deletions lib/handlers/map.get.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ const MapGet = class MapGet {
async handler(req, name, version) {
const end = this._histogram.timer();

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`map:get - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

try {
validators.version(version);
validators.name(name);
Expand All @@ -63,6 +53,16 @@ const MapGet = class MapGet {
throw e;
}

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`map:get - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

const path = createFilePathToImportMap({ org, name, version });

try {
Expand Down
20 changes: 10 additions & 10 deletions lib/handlers/map.put.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,6 @@ const MapPut = class MapPut {
async handler(req, user, name, version) {
const end = this._histogram.timer();

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`map:put - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

try {
validators.version(version);
validators.name(name);
Expand All @@ -181,6 +171,16 @@ const MapPut = class MapPut {
throw e;
}

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`map:put - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

const author = new Author(user);

const incoming = new HttpIncoming(req, {
Expand Down
20 changes: 10 additions & 10 deletions lib/handlers/pkg.get.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ const PkgGet = class PkgGet {
async handler(req, type, name, version, extra) {
const end = this._histogram.timer();

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`pkg:get - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

try {
validators.version(version);
validators.extra(extra);
Expand All @@ -66,6 +56,16 @@ const PkgGet = class PkgGet {
throw e;
}

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`pkg:get - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

const asset = new Asset({
pathname: extra,
version,
Expand Down
20 changes: 10 additions & 10 deletions lib/handlers/pkg.log.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ const PkgLog = class PkgLog {
async handler(req, type, name, version) {
const end = this._histogram.timer();

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`pkg:log - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

try {
validators.version(version);
validators.name(name);
Expand All @@ -63,6 +53,16 @@ const PkgLog = class PkgLog {
throw e;
}

const url = originalUrl(req);
const org = this._orgRegistry.get(url.hostname);

if (!org) {
this._log.info(`pkg:log - Hostname does not match a configured organization - ${url.hostname}`);
const e = new HttpError.InternalServerError();
end({ labels: { success: false, status: e.status } });
throw e;
}

const path = createFilePathToPackage({ org, type, name, version });

try {
Expand Down
Loading

0 comments on commit 8eb52f6

Please sign in to comment.