Skip to content

Commit 0ceb083

Browse files
committed
Update code to match scality guidelines
1 parent 35f7692 commit 0ceb083

File tree

10 files changed

+293
-310
lines changed

10 files changed

+293
-310
lines changed

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
module.exports = {
2-
hdcontroller: require('./lib/hdcontroller'),
3-
shuffle: require('./lib/shuffle'),
4-
};
1+
import hdcontroller from './lib/hdcontroller';
2+
import shuffle from './lib/shuffle';
3+
4+
export { hdcontroller, shuffle };
5+

lib/RequestLogger.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

lib/hdcontroller.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
'use strict'; // eslint-disable-line strict
2-
Object.defineProperty(exports, "__esModule", { value: true });
1+
'use strict';
2+
Object.defineProperty(exports, '__esModule', { value: true });
33
exports.HDProxydClient = exports.HDProxydError = void 0;
4-
const assert = require("assert");
5-
const async = require("async");
6-
const http = require("http");
7-
const werelogs = require("werelogs");
8-
const httpagent_1 = require("httpagent");
9-
const shuffle_1 = require("./shuffle");
4+
const assert = require('assert');
5+
const async = require('async');
6+
const http = require('http');
7+
const werelogs = require('werelogs');
8+
const httpagent_1 = require('httpagent');
9+
const shuffle_1 = require('./shuffle');
1010
class HDProxydError extends Error {
1111
constructor() {
1212
super(...arguments);
@@ -19,7 +19,7 @@ exports.HDProxydError = HDProxydError;
1919
*/
2020
function _createRequest(req, log, callback) {
2121
let callbackCalled = false;
22-
const request = http.request(req, (response) => {
22+
const request = http.request(req, response => {
2323
callbackCalled = true;
2424
// Get range returns a 206
2525
// Concurrent deletes on hdproxyd/immutable keys returns 423
@@ -33,14 +33,15 @@ function _createRequest(req, log, callback) {
3333
return callback(error);
3434
}
3535
return callback(undefined, response);
36-
}).on('error', (err) => {
36+
}).on('error', err => {
3737
if (!callbackCalled) {
3838
callbackCalled = true;
3939
return callback(err);
4040
}
4141
if (err.code !== 'ERR_SOCKET_TIMEOUT') {
4242
log.error('got socket error after response', { err });
4343
}
44+
return null;
4445
});
4546
// disable nagle algorithm
4647
request.setNoDelay(true);
@@ -55,7 +56,7 @@ function _createRequest(req, log, callback) {
5556
* maintain.
5657
*/
5758
function _parseBootstrapList(list) {
58-
return list.map((value) => value.split(':'));
59+
return list.map(value => value.split(':'));
5960
}
6061
class HDProxydClient {
6162
/**
@@ -144,9 +145,7 @@ class HDProxydClient {
144145
reqHeaders['X-Scal-Request-Uids'] = reqUids;
145146
reqHeaders['X-Scal-Trace-Ids'] = reqUids;
146147
if (params && params.range) {
147-
/* eslint-disable dot-notation */
148148
reqHeaders.Range = `bytes=${params.range[0]}-${params.range[1]}`;
149-
/* eslint-enable dot-notation */
150149
}
151150
let realPath;
152151
if (key === '/job/delete') {
@@ -199,7 +198,8 @@ class HDProxydClient {
199198
* creation and its sending.
200199
*/
201200
_handleRequest(method, stream, size, key, log, callback, params, payload) {
202-
const headers = params.headers ? params.headers : {};
201+
202+
const headers = (params.headers ? params.headers : {});
203203
const req = this._createRequestHeader(method, headers, key, params, log);
204204
const host = this.getCurrentBootstrap();
205205
const isBatchDelete = key === '/job/delete';
@@ -227,7 +227,7 @@ class HDProxydClient {
227227
contentLength: size,
228228
}));
229229
stream.pipe(request);
230-
stream.on('error', (err) => {
230+
stream.on('error', err => {
231231
log.error('error from readable stream', {
232232
error: err,
233233
method: '_handleRequest',
@@ -278,9 +278,8 @@ class HDProxydClient {
278278
return callback(new HDProxydError('no key returned'));
279279
}
280280
const key = response.headers['scal-key'];
281-
response.on('end', () => {
282-
return callback(undefined, key);
283-
});
281+
response.on('end', () => callback(undefined, key));
282+
return null;
284283
}, params);
285284
}
286285
/**
@@ -332,7 +331,7 @@ class HDProxydClient {
332331
*/
333332
batchDelete(list, reqUids, callback) {
334333
assert.strictEqual(typeof list, 'object');
335-
assert(list.keys.every((k) => typeof k === 'string'));
334+
assert(list.keys.every(k => typeof k === 'string'));
336335
// split the list into batches of 1000 each
337336
const batches = [];
338337
while (list.keys.length > 0) {
@@ -353,7 +352,7 @@ class HDProxydClient {
353352
done(err);
354353
}
355354
}, {}, payload);
356-
}, (err) => {
355+
}, err => {
357356
if (err) {
358357
callback(err);
359358
}

lib/shuffle.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
'use strict'; // eslint-disable-line strict
2-
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.shuffle = void 0;
4-
const crypto = require("crypto");
5-
const randomBytes = crypto.randomBytes;
1+
'use strict';
2+
Object.defineProperty(exports, '__esModule', { value: true });
3+
exports.shuffle = shuffle;
4+
const cryptoLib = require('crypto');
5+
const randomBytes = cryptoLib.randomBytes;
66
/*
77
* This set of function allows us to create an efficient shuffle
88
* of our array, since Math.random() will not be enough (32 bits of
@@ -23,7 +23,7 @@ function nextBytes(numBytes) {
2323
return randomBytes(numBytes);
2424
}
2525
catch (ex) {
26-
throw new Error('Insufficient entropy');
26+
throw new Error(`Insufficient entropy: ${ex instanceof Error ? ex.message : 'Unknown error'}`);
2727
}
2828
}
2929
/*
@@ -68,4 +68,3 @@ function shuffle(array) {
6868
}
6969
return array;
7070
}
71-
exports.shuffle = shuffle;

scripts/server.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/* eslint-disable strict */
2-
/* eslint-disable prefer-destructuring */
3-
/* eslint-disable no-bitwise */
4-
/* eslint-disable no-console */
5-
61
'use strict';
72

83
/**
@@ -18,6 +13,7 @@
1813

1914
import { createServer } from 'http';
2015
import { readFileSync } from 'fs';
16+
import * as werelogs from 'werelogs';
2117

2218
import { hdcontroller } from '../index';
2319

@@ -29,6 +25,8 @@ import { hdcontroller } from '../index';
2925
*/
3026
let removeMemIndexOnDelete = true;
3127

28+
const log = new werelogs.Logger('HDClient:Server');
29+
3230
function getHyperdriveClient(config) {
3331
return new hdcontroller.HDProxydClient(config);
3432
}
@@ -162,7 +160,7 @@ function loadConfig(file) {
162160
function main() {
163161
const args = process.argv;
164162
if (args.length < 4) {
165-
console.error(`Usage: <port> <conf path> <memindexnodel>
163+
log.error(`Usage: <port> <conf path> <memindexnodel>
166164
{Number} port to listen on
167165
{String} path to HyperdriveClient json config
168166
{*} don't remove in-memory keys on DELETE (used to check
@@ -183,7 +181,7 @@ function main() {
183181
const server = createServer(
184182
(req, res) => serverCallback(client, object2rawkey, req, res),
185183
);
186-
server.listen(port, () => console.log('Listening on %d',
184+
server.listen(port, () => log.info('Listening on %d',
187185
server.address().port));
188186
server.on('connection', socket => socket.setNoDelay(true));
189187
}

src/RequestLogger.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)