Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exports.start = function(clients, {rootUrl}={}) {
assert(rootUrl, 'rootUrl option is required');
const authPath = url.parse(libUrls.api(rootUrl, 'auth', 'v1', '/authenticate-hawk')).pathname;
nock(rootUrl, {encodedQueryParams: true, allowUnmocked: true})
.persist()
.filteringRequestBody(/.*/, '*')
.post(authPath, '*')
.reply(200, function(uri, body) {
let scopes = [];
let from = 'client config';
let ext = null;
let clientId = null;
if (body.authorization) {
let authorization = hawk.utils.parseAuthorizationHeader(body.authorization);
clientId = authorization.id;
scopes = clients[clientId];
ext = authorization.ext;
} else {
// The following is a hacky reproduction of the bewit logic in
async connect() {
if (this.isConnected()) {
return Promise.resolve();
}
const socketUrl = api(
this.options.rootUrl,
'events',
'v1',
'/listen/websocket'
).replace('http', 'ws');
this.socket = new WebSocket(socketUrl);
this.socket.addEventListener('message', this.handleMessage);
this.socket.addEventListener('error', this.handleError);
this.socket.addEventListener('close', this.handleClose);
await new Promise((resolve, reject) => {
this.socket.addEventListener('error', reject);
this.socket.addEventListener('close', reject);
this.socket.addEventListener('open', () => {
// Remove event handler for error and close
express(app) {
// generate the appropriate path for this service, based on the rootUrl
const path = url.parse(
libUrls.api(this.options.rootUrl, this.builder.serviceName, this.builder.apiVersion, '')).path;
app.use(path, this.router());
}
}
}
// Substitute parameters into route
let endpoint = entry.route.replace(/<([^<>]+)>/g, function(text, arg) {
let index = entry.args.indexOf(arg);
if (index !== -1) {
let param = args[index];
if (typeof param !== 'string' && typeof param !== 'number') {
throw new Error('URL parameter ' + arg + ' must be a string, but ' +
'we received a: ' + typeof param);
}
return encodeURIComponent(param);
}
return text; // Preserve original
});
// Create url for the request
let url = tcUrl.api(this._options.rootUrl, this._options.serviceName, this._options.serviceVersion, endpoint);
// Add payload if one is given
let payload = undefined;
if (entry.input) {
payload = args[nb_args - 1];
}
// Find query string options (if present)
let query = args[nb_args] || null;
if (query) {
_.keys(query).forEach(function(key) {
if (!_.includes(optKeys, key)) {
throw new Error('Function ' + entry.name + ' takes options: ' +
optKeys.join(', ') + ' but was given ' + key);
}
});
}
let query = args[N] || '';
if (query) {
_.keys(query).forEach(function(key) {
if (!_.includes(optKeys, key)) {
throw new Error('Function ' + entry.name + ' takes options: ' +
optKeys.join(', ') + ' but was given ' + key);
}
});
query = querystring.stringify(query);
if (query.length > 0) {
query = '?' + query;
}
}
return tcUrl.api(this._options.rootUrl, this._options.serviceName, this._options.serviceVersion, endpoint) + query;
};