From b610fc7b45dcac9eed7544295fdb3c3c088b1d9e Mon Sep 17 00:00:00 2001 From: benzekrimaha <144012792+benzekrimaha@users.noreply.github.com> Date: Wed, 9 Oct 2024 18:23:30 +0200 Subject: [PATCH] Fixup post review --- index.js | 9 +++++---- lib/hdcontroller.js | 24 ++++++++++++------------ lib/shuffle.js | 11 +++-------- package-lock.json | 38 +++++++++++++++++++++++++++++++------- package.json | 3 ++- scripts/server.js | 8 ++++---- src/hdcontroller.ts | 17 +++++------------ src/shuffle.ts | 6 +----- tsconfig.json | 2 +- 9 files changed, 64 insertions(+), 54 deletions(-) diff --git a/index.js b/index.js index ca7c22c0..ccbe1396 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ -module.exports = { - hdcontroller: require('./lib/hdcontroller'), - shuffle: require('./lib/shuffle'), -}; +import hdcontroller from './lib/hdcontroller'; +import shuffle from './lib/shuffle'; + +export { hdcontroller, shuffle }; + diff --git a/lib/hdcontroller.js b/lib/hdcontroller.js index db10f3a6..cb1d6009 100644 --- a/lib/hdcontroller.js +++ b/lib/hdcontroller.js @@ -1,12 +1,12 @@ '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("werelogs"); -const httpagent_1 = require("httpagent"); -const shuffle_1 = 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() { super(...arguments); @@ -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); @@ -189,7 +189,7 @@ class HDProxydClient { ._failover(method, stream, size, key, counter, log, callback, params); } receivedResponse = true; - log.debug('request received response'); + log.end('request received response'); return callback(err, ret); }, args, payload); } @@ -198,7 +198,7 @@ class HDProxydClient { * creation and its sending. */ _handleRequest(method, stream, size, key, log, callback, params, payload) { - //tslint:disable-next-line:no-any + const headers = (params.headers ? params.headers : {}); const req = this._createRequestHeader(method, headers, key, params, log); const host = this.getCurrentBootstrap(); @@ -352,7 +352,7 @@ class HDProxydClient { done(err); } }, {}, payload); - }, (err) => { + }, err => { if (err) { callback(err); } @@ -374,7 +374,7 @@ class HDProxydClient { hostname: currentBootstrap[0], port: currentBootstrap[1], method: 'GET', - path: '/metrics', // XXX + path: '/metrics', headers: { 'X-Scal-Request-Uids': logger.getSerializedUids(), }, diff --git a/lib/shuffle.js b/lib/shuffle.js index 4e6db36b..b8d0c62f 100644 --- a/lib/shuffle.js +++ b/lib/shuffle.js @@ -1,7 +1,7 @@ 'use strict'; -Object.defineProperty(exports, "__esModule", { value: true }); +Object.defineProperty(exports, '__esModule', { value: true }); exports.shuffle = shuffle; -const cryptoLib = require("crypto"); +const cryptoLib = require('crypto'); const randomBytes = cryptoLib.randomBytes; /* * This set of function allows us to create an efficient shuffle @@ -23,12 +23,7 @@ function nextBytes(numBytes) { return randomBytes(numBytes); } catch (ex) { - if (ex instanceof Error) { - throw new Error(`Insufficient entropy: ${ex.message}`); - } - else { - throw new Error('Insufficient entropy: Unknown error'); - } + throw new Error(`Insufficient entropy: ${ex instanceof Error ? ex.message : 'Unknown error'}`); } } /* diff --git a/package-lock.json b/package-lock.json index ae6f4e2c..7e983bf5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "clinic": "^13.0.0", "eslint": "^9.11.1", "eslint-config-scality": "github:scality/Guidelines#8.3.0", + "globals": "^15.11.0", "jsdoc": "^4.0.3", "mocha": "^10.7.3", "nock": "^13.5.5", @@ -411,6 +412,16 @@ "node": ">=6.9.0" } }, + "node_modules/@babel/traverse/node_modules/globals": { + "version": "11.12.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", + "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=4" + } + }, "node_modules/@babel/types": { "version": "7.24.7", "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.24.7.tgz", @@ -5503,12 +5514,16 @@ } }, "node_modules/globals": { - "version": "11.12.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", - "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", + "version": "15.11.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-15.11.0.tgz", + "integrity": "sha512-yeyNSjdbyVaWurlwCpcA6XNBrHTMIeDdj0/hnvX/OLJ9ekOXYbLsLinH/MucQyGvNnXhidTdNhTtJaffL2sMfw==", "dev": true, + "license": "MIT", "engines": { - "node": ">=4" + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/gopd": { @@ -10928,6 +10943,7 @@ "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz", "integrity": "sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==", "dev": true, + "license": "MIT", "dependencies": { "@cspotcode/source-map-support": "^0.8.0", "@tsconfig/node10": "^1.0.7", @@ -12140,6 +12156,14 @@ "@babel/types": "^7.24.7", "debug": "^4.3.1", "globals": "^11.1.0" + }, + "dependencies": { + "globals": { + "version": "11.12.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", + "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", + "dev": true + } } }, "@babel/types": { @@ -16169,9 +16193,9 @@ } }, "globals": { - "version": "11.12.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", - "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", + "version": "15.11.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-15.11.0.tgz", + "integrity": "sha512-yeyNSjdbyVaWurlwCpcA6XNBrHTMIeDdj0/hnvX/OLJ9ekOXYbLsLinH/MucQyGvNnXhidTdNhTtJaffL2sMfw==", "dev": true }, "gopd": { diff --git a/package.json b/package.json index f589fb49..75cb3ac9 100644 --- a/package.json +++ b/package.json @@ -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 '*.ts')", + "lint": "eslint $(git ls-files '*.ts' '*.js')", "test": "mocha --require ts-node/register --use_strict --check-leaks --recursive tests/**/*.ts", "get-version": "echo $npm_package_version" }, @@ -50,6 +50,7 @@ "clinic": "^13.0.0", "eslint": "^9.11.1", "eslint-config-scality": "github:scality/Guidelines#8.3.0", + "globals": "^15.11.0", "jsdoc": "^4.0.3", "mocha": "^10.7.3", "nock": "^13.5.5", diff --git a/scripts/server.js b/scripts/server.js index 6357c003..cb82206c 100644 --- a/scripts/server.js +++ b/scripts/server.js @@ -11,11 +11,11 @@ * curl -XGET http://host:port/mybucket/myboject */ -const { createServer } = require('http'); -const { readFileSync } = require('fs'); -const werelogs = require('werelogs'); +import { createServer } from 'http'; +import { readFileSync } from 'fs'; +import * as werelogs from 'werelogs'; -const { hdcontroller } = require('../index'); +import { hdcontroller } from '../index'; /* On DELETE, should we remove the object from the * in-memory index? diff --git a/src/hdcontroller.ts b/src/hdcontroller.ts index 4cad761c..05c85fe5 100644 --- a/src/hdcontroller.ts +++ b/src/hdcontroller.ts @@ -1,4 +1,4 @@ -'use strict'; +'use strict'; import * as assert from 'assert'; import * as async from 'async'; @@ -78,11 +78,6 @@ export interface HDProxydOptions { logApi: typeof werelogs; } -// tslint:disable-next-line: interface-name -interface Headers { - [key: string]: string; -} - export class HDProxydClient { private path: string; public bootstrap: string[][]; @@ -186,9 +181,7 @@ export class HDProxydClient { reqHeaders['X-Scal-Request-Uids'] = reqUids; reqHeaders['X-Scal-Trace-Ids'] = reqUids; if (params && params.range) { - reqHeaders.Range = `bytes=${params.range[0]}-${params.range[1]}`; - } let realPath: string; if (key === '/job/delete') { @@ -237,7 +230,7 @@ export class HDProxydClient { callback, params); } receivedResponse = true; - log.debug('request received response'); + log.end('request received response'); return callback(err, ret); }, args, payload); } @@ -250,8 +243,8 @@ export class HDProxydClient { size: number, key: string, log: werelogs.RequestLogger, callback: HDProxydCallback, params: Params, payload: object | undefined): void { - //tslint:disable-next-line:no-any - const headers = ( params.headers ? params.headers : {}) as { 'content-length'?: number; [key: string]: any }; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const headers = (params.headers ? params.headers : {}) as { 'content-length'?: number; [key: string]: any }; const req = this._createRequestHeader(method, headers, key, params, log); const host = this.getCurrentBootstrap(); @@ -430,7 +423,7 @@ export class HDProxydClient { hostname: currentBootstrap[0], port: currentBootstrap[1], method: 'GET', - path: '/metrics', // XXX + path: '/metrics', headers: { 'X-Scal-Request-Uids': logger.getSerializedUids(), }, diff --git a/src/shuffle.ts b/src/shuffle.ts index f16d450a..018898a9 100644 --- a/src/shuffle.ts +++ b/src/shuffle.ts @@ -24,11 +24,7 @@ function nextBytes(numBytes: number): Buffer { try { return randomBytes(numBytes); } catch (ex) { - if (ex instanceof Error) { - throw new Error(`Insufficient entropy: ${ex.message}`); - } else { - throw new Error('Insufficient entropy: Unknown error'); - } + throw new Error(`Insufficient entropy: ${ex instanceof Error ? ex.message : 'Unknown error'}`); } } diff --git a/tsconfig.json b/tsconfig.json index 7f70d32d..7f5e53eb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ "target": "es2016", "alwaysStrict": true, "skipLibCheck": true, - "module": "commonjs" + "module": "CommonJS", }, "include": [ "./src/*"