Skip to content

Commit

Permalink
Clean up alias (eik-lib#1)
Browse files Browse the repository at this point in the history
* lines up fs/gcs write method signature

at the expense of adding a blocking operation which we will need to fix

* refactor alias command to match docs as written

* remove console.log

* add metafiles and set up linting

* autofix lint issues

* fix lint issues
  • Loading branch information
digitalsadhu authored and trygve-lie committed Oct 15, 2019
1 parent e31445f commit dfe6612
Show file tree
Hide file tree
Showing 22 changed files with 548 additions and 449 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
10 changes: 10 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": ["airbnb-base", "prettier"],
"plugins": ["prettier"],
"rules": {
"strict": [0, "global"],
"class-methods-use-this": [0],
"no-underscore-dangle": [0],
"no-restricted-syntax": [0]
}
}
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@ package-lock.json
uploads
tmp
gcloud.json
node_modules/**/*
.DS_Store
tmp/**/*
.idea
.idea/**/*
*.iml
*.log
coverage
.vscode
.nyc_output/
19 changes: 19 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 4,
"overrides": [
{
"files": [
".prettierrc",
"*.json",
"*.yml",
".travis.yml",
".eslintrc"
],
"options": {
"tabWidth": 2
}
}
]
}
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: node_js
node_js:
- '12'
script:
- npm test
- npm run lint
cache:
npm: true
directories:
- node_modules
branches:
only:
- master
- '/^greenkeeper/.*$/'
dist: trusty
47 changes: 15 additions & 32 deletions lib/classes/alias.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
'use strict';

const { BASE_ASSETS, BASE_ALIAS, ROOT } = require('../utils/globals');
const semver = require('semver');
const path = require('path');
const { BASE_ASSETS, BASE_ALIAS, ROOT } = require('../utils/globals');

const FILE_NAME = '/alias.json';

class Alias {
constructor({
name = '',
type = '',
org = '',
} = {}) {
constructor({ name = '', type = '', org = '' } = {}) {
this._version = '';
this._alias = '',
this._alias = '';
this._name = name;
this._type = type;
this._org = org;
}

set version(value) {
const obj = semver.parse(value);
this._version = value;
this._alias = obj.major.toString();
}

get version() {
Expand All @@ -43,7 +36,7 @@ class Alias {
this._org,
this._type,
this._name,
this._version,
this._version
);
}

Expand All @@ -53,7 +46,7 @@ class Alias {
this._org,
this._type,
this._name,
this._alias,
this._alias
);
}

Expand All @@ -77,31 +70,21 @@ class Alias {
name: this.name,
type: this.type,
org: this.org,
}
};
}

static buildPathname(org = '', type = '', name = '', version = '', extras = '') {
return path.join(
ROOT,
org,
BASE_ASSETS,
type,
name,
version,
extras,
);
static buildPathname(
org = '',
type = '',
name = '',
version = '',
extras = ''
) {
return path.join(ROOT, org, BASE_ASSETS, type, name, version, extras);
}

static buildPath(org = '', type = '', name = '', alias = '') {
return path.join(
ROOT,
org,
BASE_ALIAS,
type,
name,
alias,
FILE_NAME,
);
return path.join(ROOT, org, BASE_ALIAS, type, name, alias, FILE_NAME);
}
}
module.exports = Alias;
6 changes: 3 additions & 3 deletions lib/classes/asset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { BASE_ASSETS, ROOT } = require('../utils/globals');
const path = require('path');
const { BASE_ASSETS, ROOT } = require('../utils/globals');

const MIME_DEFAULT = 'application/octet-stream';

Expand Down Expand Up @@ -36,8 +36,8 @@ class Asset {
const ext = path.extname(extra);
const mime = MIME_TYPES.get(ext);

this._mimeType = mime ? mime : MIME_DEFAULT;
this._supported = mime ? true : false;
this._mimeType = mime || MIME_DEFAULT;
this._supported = !!mime;
this._errored = false;
// this._type = '';
this._size = -1;
Expand Down
4 changes: 2 additions & 2 deletions lib/classes/import-map.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { BASE_IMPORT_MAPS, ROOT } = require('../utils/globals');
const path = require('path');
const { BASE_IMPORT_MAPS, ROOT } = require('../utils/globals');

const FILE_NAME = '/import-map.json';

Expand All @@ -11,7 +11,7 @@ class ImportMap {
} = {}) {
this._imports = new Map();

for (let [specifier, address] of Object.entries(imports)) {
for (const [specifier, address] of Object.entries(imports)) {
this._imports.set(specifier, address);
}
}
Expand Down
22 changes: 11 additions & 11 deletions lib/handlers/alias.delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const params = {
};
module.exports.params = params;

const handler = (sink, req, org, type, name, alias) => {
const handler = async (sink, req, org, type, name, alias) => {
if (typeof org !== 'string' || org === '') {
throw new TypeError(
':org is a required url parameter and must be a string'
Expand All @@ -44,18 +44,18 @@ const handler = (sink, req, org, type, name, alias) => {
);
}

return new Promise(async (resolve, reject) => {
const path = Alias.buildPath(org, type, name, alias);

const path = Alias.buildPath(org, type, name, alias);

// TODO; try/catch
const result = await sink.delete(path);
// TODO; try/catch
await sink.delete(path);

const outgoing = new HttpOutgoing();
outgoing.mimeType = 'plain/text';
outgoing.statusCode = 204;
outgoing.push(null);
const outgoing = new HttpOutgoing();
outgoing.mimeType = 'plain/text';
outgoing.statusCode = 204;
outgoing.push(null);

resolve(outgoing);
});
return outgoing

};
module.exports.handler = handler;
31 changes: 15 additions & 16 deletions lib/handlers/alias.get.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const params = {
};
module.exports.params = params;

const handler = (sink, req, org, type, name, alias, extra) => {
const handler = async (sink, req, org, type, name, alias, extra) => {
if (typeof org !== 'string' || org === '') {
throw new TypeError(
':org is a required url parameter and must be a string'
Expand All @@ -45,24 +45,23 @@ const handler = (sink, req, org, type, name, alias, extra) => {
);
}

return new Promise(async (resolve, reject) => {
const path = Alias.buildPath(org, type, name, alias);
const path = Alias.buildPath(org, type, name, alias);

let obj = {};
try {
obj = await utils.readJSON(sink, path);
} catch(error) {
// TODO; log error?
}
let obj = {};
try {
obj = await utils.readJSON(sink, path);
} catch(error) {
// TODO; log error?
}

const location = Alias.buildPathname(obj.org, obj.type, obj.name, obj.version, extra);

const location = Alias.buildPathname(obj.org, obj.type, obj.name, obj.version, extra);
const outgoing = new HttpOutgoing();
outgoing.mimeType = 'application/json';
outgoing.statusCode = 302;
outgoing.location = location;

const outgoing = new HttpOutgoing();
outgoing.mimeType = 'application/json';
outgoing.statusCode = 302;
outgoing.location = location;
return outgoing;

resolve(outgoing);
});
};
module.exports.handler = handler;
Loading

0 comments on commit dfe6612

Please sign in to comment.