Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
benzekrimaha committed Sep 30, 2024
1 parent 0924471 commit 36e5186
Show file tree
Hide file tree
Showing 10 changed files with 454 additions and 306 deletions.
29 changes: 24 additions & 5 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";

import tseslint from 'typescript-eslint';


const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
Expand All @@ -11,9 +14,25 @@ const compat = new FlatCompat({
allConfig: js.configs.all
});

export default [...compat.extends("scality"), {
languageOptions: {
ecmaVersion: 2020,
sourceType: "script",
export default tseslint.config(
...compat.extends('scality'),
...tseslint.configs.recommended,
{
ignores: [
'**/.vscode',
'**/vendor/*.js',
'**/build',
'**/coverage',
'**/dist',
'**/node_modules',
'**/package',
'**/lib',
'**/scripts',
'**/eslint.config.mjs',
],
rules: {
// CucumberJS steps start with an uppercase
'new-cap': 'off',
},
},
}];
);
4 changes: 2 additions & 2 deletions lib/RequestLogger.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
28 changes: 14 additions & 14 deletions lib/hdcontroller.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
Object.defineProperty(exports, "__esModule", { value: true });
exports.HDProxydClient = exports.HDProxydError = void 0;
const assert = require('assert');
const async = require('async');
const http = require('http');
const werelogs = require('werelog');
const httpagent1 = require('httpagent');
const shuffle1 = require('./shuffle');
const assert = require("assert");
const async = require("async");
const http = require("http");
const werelogs = require("werelogs");
const httpagent_1 = require("httpagent");
const shuffle_1 = require("./shuffle");
class HDProxydError extends Error {
constructor(...args) {
super(...args);
constructor() {
super(...arguments);
this.isExpected = false;
}
}
Expand All @@ -33,7 +33,7 @@ function _createRequest(req, log, callback) {
return callback(error);
}
return callback(undefined, response);
}).on('error', err => {
}).on('error', (err) => {
if (!callbackCalled) {
callbackCalled = true;
return callback(err);
Expand Down Expand Up @@ -74,10 +74,10 @@ class HDProxydClient {
const options = opts || {};
this.bootstrap = opts.bootstrap === undefined ?
[['localhost', '18888']] : _parseBootstrapList(opts.bootstrap);
this.bootstrap = (0, shuffle1.shuffle)(this.bootstrap);
this.bootstrap = (0, shuffle_1.shuffle)(this.bootstrap);
this.path = '/store/';
this.setCurrentBootstrap(this.bootstrap[0]);
this.httpAgent = new httpagent1.http.Agent({
this.httpAgent = new httpagent_1.http.Agent({
freeSocketTimeout: 60 * 1000,
timeout: 2 * 60 * 1000,
});
Expand Down Expand Up @@ -351,7 +351,7 @@ class HDProxydClient {
done(err);
}
}, {}, payload);
}, err => {
}, (err) => {
if (err) {
callback(err);
}
Expand All @@ -373,7 +373,7 @@ class HDProxydClient {
hostname: currentBootstrap[0],
port: currentBootstrap[1],
method: 'GET',
path: '/metrics',
path: '/metrics', // XXX
headers: {
'X-Scal-Request-Uids': logger.getSerializedUids(),
},
Expand Down
20 changes: 10 additions & 10 deletions lib/shuffle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.shuffle = void 0;
const cryptoLib = require('crypto');
Object.defineProperty(exports, "__esModule", { value: true });
exports.shuffle = shuffle;
const cryptoLib = require("crypto");
const randomBytes = cryptoLib.randomBytes;
/*
* This set of function allows us to create an efficient shuffle
Expand Down Expand Up @@ -57,14 +57,14 @@ function randomRange(min, max) {
* @return {Array} - The sorted array
*/
function shuffle(array) {
const arrCopy = array.slice(); // Create a shallow copy of the array
if (arrCopy.length === 1) {
return arrCopy;
if (array.length === 1) {
return array;
}
for (let i = arrCopy.length - 1; i > 0; i--) {
for (let i = array.length - 1; i > 0; i--) {
const randIndex = randomRange(0, i);
[arrCopy[randIndex], arrCopy[i]] = [arrCopy[i], arrCopy[randIndex]];
const randIndexVal = array[randIndex];
[array[randIndex], array[i]] = [array[i], array[randIndex]]; // eslint-disable-line no-param-reassign
array[i] = randIndexVal; // eslint-disable-line no-param-reassign
}
return arrCopy;
return array;
}
exports.shuffle = shuffle;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "tsc --strict ",
"coverage": "nyc --check-coverage --exclude 'tests/*' --lines=90 --reporter=text --reporter=html --reporter=lcov npm test tests/",
"jsdoc": "jsdoc src/ tests/ -d docs/jsdoc",
"lint": "eslint $(git ls-files '*.js')",
"lint": "eslint $(git ls-files '*.ts')",
"test": "mocha --require ts-node/register --use_strict --check-leaks --recursive tests/**/*.ts",
"get-version": "echo $npm_package_version"
},
Expand Down Expand Up @@ -55,7 +55,8 @@
"nyc": "^17.1.0",
"sinon": "^19.0.2",
"ts-node": "^10.9.2",
"typescript": "^5.6.2"
"typescript": "^5.6.2",
"typescript-eslint": "^8.7.0"
},
"license": "Apache-2.0",
"bugs": {
Expand Down
12 changes: 6 additions & 6 deletions src/RequestLogger.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// tslint:disable-next-line: interface-name
export declare interface RequestLogger {
new(logger: any, logLevel: string, dumpThreshold: string, endLevel: string, uids?: string[] | string): any;
new(logger , logLevel: string, dumpThreshold: string, endLevel: string, uids?: string[] | string);

Check failure on line 3 in src/RequestLogger.ts

View workflow job for this annotation

GitHub Actions / test

Construct signature, which lacks return-type annotation, implicitly has an 'any' return type.

Check failure on line 3 in src/RequestLogger.ts

View workflow job for this annotation

GitHub Actions / test

Parameter 'logger' implicitly has an 'any' type.
getUids(): string[];
getSerializedUids(): string;
trace(msg: string, data?: any): void;
debug(msg: string, data?: any): void;
info(msg: string, data?: any): void;
warn(msg: string, data?: any): void;
error(msg: string, data?: any): void;
trace(msg: string, data?): void;

Check failure on line 6 in src/RequestLogger.ts

View workflow job for this annotation

GitHub Actions / test

Parameter 'data' implicitly has an 'any' type.
debug(msg: string, data?): void;

Check failure on line 7 in src/RequestLogger.ts

View workflow job for this annotation

GitHub Actions / test

Parameter 'data' implicitly has an 'any' type.
info(msg: string, data?): void;

Check failure on line 8 in src/RequestLogger.ts

View workflow job for this annotation

GitHub Actions / test

Parameter 'data' implicitly has an 'any' type.
warn(msg: string, data?): void;

Check failure on line 9 in src/RequestLogger.ts

View workflow job for this annotation

GitHub Actions / test

Parameter 'data' implicitly has an 'any' type.
error(msg: string, data?): void;

Check failure on line 10 in src/RequestLogger.ts

View workflow job for this annotation

GitHub Actions / test

Parameter 'data' implicitly has an 'any' type.
}
79 changes: 40 additions & 39 deletions src/hdcontroller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict'; // eslint-disable-line strict
'use strict';

import assert = require('assert');
import async = require('async');
import http = require('http');
import werelogs = require('werelogs');
import * as assert from 'assert';
import * as async from 'async';
import * as http from 'http';
import * as werelogs from 'werelogs';

import { http as httpAgent } from 'httpagent';
import { Stream } from 'stream';
import { RequestLogger } from './RequestLogger';
import { shuffle } from './shuffle';
import { shuffle } from './shuffle';


export class HDProxydError extends Error {
public code: number | string | undefined;
Expand All @@ -26,7 +27,7 @@ type HDProxydClientDeleteCallback = (error?: HDProxydError) => void;
*/
function _createRequest(req: http.RequestOptions, log: RequestLogger, callback: HDProxydCallback): http.ClientRequest {
let callbackCalled = false;
const request = http.request(req, (response) => {
const request = http.request(req, response => {
callbackCalled = true;
// Get range returns a 206
// Concurrent deletes on hdproxyd/immutable keys returns 423
Expand All @@ -36,7 +37,7 @@ function _createRequest(req: http.RequestOptions, log: RequestLogger, callback:
error.code = response.statusCode;
error.isExpected = true;
log.debug('got expected response code:',
{ statusCode: response.statusCode });
{ statusCode: response.statusCode });
response.resume(); // Drain the response stream
return callback(error);
}
Expand All @@ -49,6 +50,7 @@ function _createRequest(req: http.RequestOptions, log: RequestLogger, callback:
if (err.code !== 'ERR_SOCKET_TIMEOUT') {
log.error('got socket error after response', { err });
}
return null;
});

// disable nagle algorithm
Expand All @@ -65,20 +67,20 @@ function _createRequest(req: http.RequestOptions, log: RequestLogger, callback:
* maintain.
*/
function _parseBootstrapList(list: string[]): string[][] {
return list.map((value) => value.split(':'));
return list.map(value => value.split(':'));
}

// tslint:disable-next-line: interface-name
export interface HDProxydOptions {
bootstrap: string[];
logApi: any;
logApi;

Check failure on line 76 in src/hdcontroller.ts

View workflow job for this annotation

GitHub Actions / test

Member 'logApi' implicitly has an 'any' type.
}

export class HDProxydClient {
private path: string;
public bootstrap: string[][];
private httpAgent: http.Agent;
private logging: RequestLogger | any;
private logging: RequestLogger;

Check failure on line 83 in src/hdcontroller.ts

View workflow job for this annotation

GitHub Actions / test

Property 'logging' has no initializer and is not definitely assigned in the constructor.
private current: string[] = ['', ''];
/**
* This represent our interface with the hdproxyd server.
Expand Down Expand Up @@ -121,11 +123,11 @@ export class HDProxydClient {
* for the Logger object
* @return {undefined}
*/
private setupLogging(logApi: any): void {
private setupLogging(logApi): void {

Check failure on line 126 in src/hdcontroller.ts

View workflow job for this annotation

GitHub Actions / test

Parameter 'logApi' implicitly has an 'any' type.
this.logging = new (logApi || werelogs).Logger('HDProxydClient');
}

private createLogger(reqUids?: any): RequestLogger {
private createLogger(reqUids?): RequestLogger {
return reqUids ?
this.logging.newRequestLoggerFromSerializedUids(reqUids) :
this.logging.newRequestLogger();
Expand Down Expand Up @@ -167,7 +169,7 @@ export class HDProxydClient {
* This creates a default request for hdproxyd
*/
private _createRequestHeader(method: string, headers: {[key: string]: string}|undefined,
key: string, params: any, log: any): object {
key: string, params, log): object {
const reqHeaders = headers || {};

const currentBootstrap: string[] = this.getCurrentBootstrap();
Expand All @@ -177,9 +179,9 @@ export class HDProxydClient {
reqHeaders['X-Scal-Request-Uids'] = reqUids;
reqHeaders['X-Scal-Trace-Ids'] = reqUids;
if (params && params.range) {
/* eslint-disable dot-notation */
reqHeaders.Range = `bytes=${params.range[0]}-${params.range[1]}`;
/* eslint-enable dot-notation */
}
let realPath: string;
if (key === '/job/delete') {
Expand All @@ -198,8 +200,8 @@ export class HDProxydClient {
}

private _failover(method: string, stream: Stream|null, size: number, key: string,
tries: number, log: any, callback: HDProxydCallback, params?: any,
payload?: any): void {
tries: number, log, callback: HDProxydCallback, params?,
payload?): void {
const args = params === undefined ? {} : params;
let counter = tries;
log.debug('sending request to hdproxyd', { method, key, args, counter });
Expand All @@ -220,12 +222,12 @@ export class HDProxydClient {
}
if (++counter >= this.bootstrap.length) {
log.errorEnd('failover tried too many times, giving up',
{ retries: counter });
{ retries: counter });
return callback(err);
}
return this._shiftCurrentBootstrapToEnd(log)
._failover(method, stream, size, key, counter, log,
callback, params);
callback, params);
}
receivedResponse = true;
log.end().debug('request received response');
Expand All @@ -238,12 +240,12 @@ export class HDProxydClient {
* creation and its sending.
*/
private _handleRequest(method: string, stream: Stream|null,
size: number, key: string, log: any,
callback: HDProxydCallback, params: any,
payload: any): void {
size: number, key: string, log,
callback: HDProxydCallback, params,
payload): void {
const headers = params.headers ? params.headers : {};
const req = this._createRequestHeader(method, headers, key, params,
log);
log);
const host = this.getCurrentBootstrap();
const isBatchDelete = key === '/job/delete';
if (stream) {
Expand Down Expand Up @@ -271,7 +273,7 @@ export class HDProxydClient {
contentLength: size,
}));
stream.pipe(request);
stream.on('error', (err) => {
stream.on('error', err => {
log.error('error from readable stream', {
error: err,
method: '_handleRequest',
Expand Down Expand Up @@ -309,7 +311,7 @@ export class HDProxydClient {
* @param {HDProxydClient~putCallback} callback - callback
* @returns {undefined}
*/
public put(stream: Stream, size: number, params: any, reqUids: string, callback: HDProxydClientPutCallback): void {
public put(stream: Stream, size: number, params, reqUids: string, callback: HDProxydClientPutCallback): void {
const log = this.createLogger(reqUids);
this._failover('POST', stream, size, '', 0, log, (err?: HDProxydError, response?: http.IncomingMessage) => {
if (response) {
Expand All @@ -322,9 +324,8 @@ export class HDProxydClient {
return callback(new HDProxydError('no key returned'));
}
const key = response.headers['scal-key'] as string;
response.on('end', () => {
return callback(undefined, key);
});
response.on('end', () => callback(undefined, key));
return null;
}, params);
}

Expand Down Expand Up @@ -377,7 +378,7 @@ export class HDProxydClient {
*/
public batchDelete(list: {keys: string[]}, reqUids: string, callback: HDProxydClientDeleteCallback): void {
assert.strictEqual(typeof list, 'object');
assert(list.keys.every((k) => typeof k === 'string'));
assert(list.keys.every(k => typeof k === 'string'));
// split the list into batches of 1000 each
const batches = [];
while (list.keys.length > 0) {
Expand Down Expand Up @@ -413,18 +414,18 @@ export class HDProxydClient {
* @param {SproxydClient-healthcheckCallback} callback - callback
* @returns {void}
*/
public healthcheck(log: RequestLogger, callback: HDProxydCallback): void {
public healthcheck(log: RequestLogger, callback: HDProxydCallback): void {
const logger = log || this.createLogger();
const currentBootstrap = this.getCurrentBootstrap();
const req = {
hostname: currentBootstrap[0],
port: currentBootstrap[1],
method: 'GET',
path: '/metrics', // XXX
headers: {
'X-Scal-Request-Uids': logger.getSerializedUids(),
},
agent: this.httpAgent,
hostname: currentBootstrap[0],
port: currentBootstrap[1],
method: 'GET',
path: '/metrics', // XXX
headers: {
'X-Scal-Request-Uids': logger.getSerializedUids(),
},
agent: this.httpAgent,
};
const request = _createRequest(req, logger, callback);
request.end();
Expand Down
Loading

0 comments on commit 36e5186

Please sign in to comment.