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

ES2015 modernization #349

Closed
wants to merge 12 commits into from
66 changes: 33 additions & 33 deletions lib/api_formats/siren/device.siren.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
var url = require('url');
var rel = require('zetta-rels');
const url = require('url');
const rel = require('zetta-rels');

module.exports = function(context) {
var loader = context.loader;
var env = context.env;
var model = context.model;
var actions = buildActions(model.id, env, loader, model.transitionsAvailable());
var streams = buildStreamLinks(model, loader, env);
var properties = model.properties();
var entity = {
module.exports = context => {
const loader = context.loader;
const env = context.env;
const model = context.model;
const actions = buildActions(model.id, env, loader, model.transitionsAvailable());
const streams = buildStreamLinks(model, loader, env);
const properties = model.properties();
const entity = {
class: ['device', properties.type],
properties: properties,
actions: actions,
links: [{ rel: ['self', 'edit'], href: env.helpers.url.path(loader.path + '/devices/' + model.id) },
properties,
actions,
links: [{ rel: ['self', 'edit'], href: env.helpers.url.path(`${loader.path}/devices/${model.id}`) },
{ title: context.serverName, rel: ['up', rel.server], href: env.helpers.url.path(loader.path) },
{ rel: [rel.type, 'describedby'], href: env.helpers.url.path(loader.path) + '/meta/' + properties.type }]
{ rel: [rel.type, 'describedby'], href: `${env.helpers.url.path(loader.path)}/meta/${properties.type}` }]
};

entity.links = entity.links.concat(streams);
return entity;
};

var buildActions = module.exports.buildActions = function(deviceId, env, loader, transitions) {
var actions = null;
Object.keys(transitions).forEach(function(type) {
var transition = transitions[type];
var fields = transition.fields ? [].concat(transition.fields) : [];
var buildActions = module.exports.buildActions = (deviceId, env, loader, transitions) => {
let actions = null;
Object.keys(transitions).forEach(type => {
const transition = transitions[type];
const fields = transition.fields ? [].concat(transition.fields) : [];
fields.push({ name: 'action', type: 'hidden', value: type });

var action = {
const action = {
class: ['transition'],
name: type,
method: 'POST',
href: env.helpers.url.path(loader.path + '/devices/' + deviceId),
fields: fields
href: env.helpers.url.path(`${loader.path}/devices/${deviceId}`),
fields
};
if (!actions) {
actions = [];
Expand All @@ -45,23 +45,23 @@ var buildActions = module.exports.buildActions = function(deviceId, env, loader,
return actions;
};

var buildStreamLinks = function(model, loader, env) {
var links = [];
var rootPath = env.helpers.url.path(loader.path);
var isForwardedProtocol = env.request && env.request.headers.hasOwnProperty('x-forwarded-proto') &&
var buildStreamLinks = (model, loader, env) => {
const links = [];
const rootPath = env.helpers.url.path(loader.path);
const isForwardedProtocol = env.request && env.request.headers.hasOwnProperty('x-forwarded-proto') &&
['http', 'https'].indexOf(env.request.headers['x-forwarded-proto']) !== -1;
var isSpdy = env.request && !!env.request.isSpdy && !isForwardedProtocol;
var eventPath = isSpdy ? rootPath + '/events' : rootPath.replace(/^http/, 'ws') + '/events';
const isSpdy = env.request && !!env.request.isSpdy && !isForwardedProtocol;
const eventPath = isSpdy ? `${rootPath}/events` : `${rootPath.replace(/^http/, 'ws')}/events`;

var streams = model._streams;
const streams = model._streams;
streams.logs = { enabled: true }; // add logs to links
Object.keys(streams).forEach(function(name) {
var q = { topic: model.type + '/' + model.id + '/' + name };
var streamRel = rel.objectStream;
Object.keys(streams).forEach(name => {
const q = { topic: `${model.type}/${model.id}/${name}` };
let streamRel = rel.objectStream;
if (streams[name]._writableState && !streams[name]._writableState.objectMode) {
streamRel = rel.binaryStream;
}
var stream = {
const stream = {
title: name,
rel: ['monitor', streamRel],
href: eventPath + url.format({ query: q})
Expand Down
26 changes: 13 additions & 13 deletions lib/api_formats/siren/devices.siren.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
var rel = require('zetta-rels');
const rel = require('zetta-rels');

module.exports = function(context) {
module.exports = context => {

var devices = context.devices;
var loader = context.loader;
var env = context.env;
const devices = context.devices;
const loader = context.loader;
const env = context.env;

var entity = {
const entity = {
class: ['devices'],
links: [
{ rel: ['self'], href: env.helpers.url.path(loader.path)}
]
};

entity.entities = [];
Object.keys(devices).forEach(function(device) {
Object.keys(devices).forEach(device => {
entity.entities.push(buildEntity(devices[device], loader, env));
});

return entity;
};

var buildEntity = function(model, loader, env) {
var self = this;
var properties = model.properties();
var entity = {
const self = this;
const properties = model.properties();
const entity = {
class: ['device', properties.type],
rel: [rel.device],
properties: properties,
links: [{ rel: ['self', 'edit'], href: env.helpers.url.path(loader.path + '/devices/' + model.id) },
{ rel: [rel.type, 'describedby'], href: env.helpers.url.path(loader.path) + '/meta/' + encodeURIComponent(properties.type) },
properties,
links: [{ rel: ['self', 'edit'], href: env.helpers.url.path(`${loader.path}/devices/${model.id}`) },
{ rel: [rel.type, 'describedby'], href: `${env.helpers.url.path(loader.path)}/meta/${encodeURIComponent(properties.type)}` },
{ rel: ['up', rel.server], href: env.helpers.url.path(loader.path) }]
};

Expand Down
32 changes: 16 additions & 16 deletions lib/api_formats/siren/metadata.siren.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
var path = require('path');
var rel = require('zetta-rels');
const path = require('path');
const rel = require('zetta-rels');

module.exports = function(context) {
var server = context.server;
var types = context.types;
var loader = context.loader;
var env = context.env;
var isSpdy = !!context.env.request.isSpdy;
var rootPath = env.helpers.url.path(loader.path);
var eventPath = (isSpdy ? rootPath + '/events' : rootPath.replace(/^http/, 'ws')) + '/events';
module.exports = context => {
const server = context.server;
const types = context.types;
const loader = context.loader;
const env = context.env;
const isSpdy = !!context.env.request.isSpdy;
const rootPath = env.helpers.url.path(loader.path);
const eventPath = `${isSpdy ? rootPath + '/events' : rootPath.replace(/^http/, 'ws')}/events`;

var entity = {
const entity = {
class: ['metadata'],
properties: server.getProperties(),
entities: [],
links: [
{ rel: ['self'], href: env.helpers.url.current() },
{ rel: [rel.server], href: env.helpers.url.path(loader.path) },
{ rel: ['monitor'], href: eventPath + '?topic=meta' }
{ rel: ['monitor'], href: `${eventPath}?topic=meta` }
]
};

types.forEach(function(type) {
var e = {
types.forEach(type => {
const e = {
class: ['type'],
rel: [rel.type, 'item'],
properties: {},
links: [
{ rel: ['self'], href: rootPath + '/meta/' + encodeURIComponent(type.type) }
{ rel: ['self'], href: `${rootPath}/meta/${encodeURIComponent(type.type)}` }
]
};

Object.keys(type).forEach(function(key) {
Object.keys(type).forEach(key => {
e.properties[key] = type[key];
});

Expand Down
46 changes: 23 additions & 23 deletions lib/api_formats/siren/server.siren.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
var rel = require('zetta-rels');
var qs = require('querystring');
const rel = require('zetta-rels');
const qs = require('querystring');

module.exports = function(context) {
var server = context.server;
var devices = context.devices;
var loader = context.loader;
var env = context.env;
module.exports = context => {
const server = context.server;
const devices = context.devices;
const loader = context.loader;
const env = context.env;

var isForwardedProtocol = context.env.request.headers.hasOwnProperty('x-forwarded-proto') &&
const isForwardedProtocol = context.env.request.headers.hasOwnProperty('x-forwarded-proto') &&
['http', 'https'].indexOf(context.env.request.headers['x-forwarded-proto']) !== -1;
var isSpdy = !!context.env.request.isSpdy && !isForwardedProtocol;
const isSpdy = !!context.env.request.isSpdy && !isForwardedProtocol;

var rootPath = env.helpers.url.path(loader.path);
var eventPath = isSpdy ? rootPath + '/events' : rootPath.replace(/^http/, 'ws') + '/events';
const rootPath = env.helpers.url.path(loader.path);
const eventPath = isSpdy ? `${rootPath}/events` : `${rootPath.replace(/^http/, 'ws')}/events`;

var entity = {
const entity = {
class: ['server'],
properties: server.getProperties(),
entities: [],
Expand All @@ -34,34 +34,34 @@ module.exports = function(context) {
],
links: [
{ rel: ['self'], href: env.helpers.url.current() },
{ rel: [rel.metadata], href: rootPath + '/meta' },
{ rel: ['monitor'], href: eventPath + '?topic=logs' }
{ rel: [rel.metadata], href: `${rootPath}/meta` },
{ rel: ['monitor'], href: `${eventPath}?topic=logs` }
]
};

Object.keys(devices).forEach(function(device) {
Object.keys(devices).forEach(device => {
entity.entities.push(buildEntity(devices[device], server, loader, env));
});
if(context.query) {
entity.properties.ql = context.query;
entity.class = entity.class.concat(context.classes);
var queryTopic = qs.stringify({topic: 'query/'+context.query, since: new Date().getTime()});
entity.links.push({ rel: [rel.query], href: eventPath + '?' + queryTopic });
const queryTopic = qs.stringify({topic: `query/${context.query}`, since: new Date().getTime()});
entity.links.push({ rel: [rel.query], href: `${eventPath}?${queryTopic}` });
//rerform matching of current devices.
}

return entity;
};

var buildEntity = function(model, server, loader, env) {
var self = this;
var properties = model.properties();
var entity = {
const self = this;
const properties = model.properties();
const entity = {
class: ['device', properties.type],
rel: [rel.device],
properties: properties,
links: [{ rel: ['self', 'edit'], href: env.helpers.url.path(loader.path + '/devices/' + model.id) },
{ rel: [rel.type, 'describedby'], href: env.helpers.url.path(loader.path) + '/meta/' + encodeURIComponent(properties.type) },
properties,
links: [{ rel: ['self', 'edit'], href: env.helpers.url.path(`${loader.path}/devices/${model.id}`) },
{ rel: [rel.type, 'describedby'], href: `${env.helpers.url.path(loader.path)}/meta/${encodeURIComponent(properties.type)}` },
{ title: server._name, rel: ['up', rel.server], href: env.helpers.url.path(loader.path) }]
};

Expand Down
22 changes: 11 additions & 11 deletions lib/api_formats/siren/type.siren.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
var path = require('path');
var rel = require('zetta-rels');
const path = require('path');
const rel = require('zetta-rels');

module.exports = function(context) {
var server = context.server;
var type = context.type;
var loader = context.loader;
var env = context.env;
module.exports = context => {
const server = context.server;
const type = context.type;
const loader = context.loader;
const env = context.env;

var entity = {
const entity = {
class: ['type'],
properties: {},
links: [
{ rel: ['self'], href: env.helpers.url.current() },
{ title: server._name, rel: ['collection', rel.metadata], href: env.helpers.url.path(loader.path) + '/meta' },
{ rel: [rel.instances, 'describes'], href: env.helpers.url.path(loader.path) + '?ql=' + encodeURIComponent('where type="' + type.type + '"') }
{ title: server._name, rel: ['collection', rel.metadata], href: `${env.helpers.url.path(loader.path)}/meta` },
{ rel: [rel.instances, 'describes'], href: `${env.helpers.url.path(loader.path)}?ql=${encodeURIComponent('where type="' + type.type + '"')}` }
]
};

Object.keys(type).forEach(function(key) {
Object.keys(type).forEach(key => {
entity.properties[key] = type[key];
});

Expand Down
52 changes: 28 additions & 24 deletions lib/api_resources/devices.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
var url = require('url');
var MediaType = require('api-media-type');
const url = require('url');
const MediaType = require('api-media-type');

var DevicesResource = module.exports = function(server) {
this.server = server;
this.path = '/devices';
};
class DevicesResource {
constructor(server) {
this.server = server;
this.path = '/devices';
}

DevicesResource.prototype.init = function(config) {
config
.path(this.path)
.produces(MediaType.SIREN)
.consumes(MediaType.FORM_URLENCODED)
.get('/', this.list);
};
init(config) {
config
.path(this.path)
.produces(MediaType.SIREN)
.consumes(MediaType.FORM_URLENCODED)
.get('/', this.list);
}

DevicesResource.prototype.list = function(env, next) {
var parsed = url.parse(env.request.url);
var re = /^\/servers\/([^\/]+)/;
var match = re.exec(parsed.pathname);

var serverId = match && match[1] ? match[1] : this.server.id;
list(env, next) {
const parsed = url.parse(env.request.url);
const re = /^\/servers\/([^\/]+)/;
const match = re.exec(parsed.pathname);
const serverId = match && match[1] ? match[1] : this.server.id;

var localServer = { path: '/servers/'+ encodeURI(serverId) };
var context = { devices: this.server.runtime._jsDevices, loader: localServer, env: env };
env.format.render('devices', context);
next(env);
};
const localServer = { path: `/servers/${encodeURI(serverId)}` };
const context = { devices: this.server.runtime._jsDevices, loader: localServer, env };
env.format.render('devices', context);
next(env);
}
}

module.exports = DevicesResource;
Loading