From 48b96b80430c6f297ef18990c08f8f4340c09ac5 Mon Sep 17 00:00:00 2001 From: Maha Benzekri Date: Wed, 23 Oct 2024 14:42:14 +0200 Subject: [PATCH] fixup post reviews --- index.js | 9 +++++---- lib/hdcontroller.js | 26 +++++++++++--------------- lib/shuffle.js | 8 +++----- package-lock.json | 37 ++++++++++++++++++++++++++++++------- package.json | 1 + scripts/server.js | 8 ++++---- src/hdcontroller.ts | 9 ++------- src/shuffle.ts | 2 +- tsconfig.json | 3 ++- 9 files changed, 59 insertions(+), 44 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 e28cf6ba..9c5b5e81 100644 --- a/lib/hdcontroller.js +++ b/lib/hdcontroller.js @@ -1,19 +1,16 @@ 'use strict'; -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"); -class HDProxydError extends Error { +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 { shuffle } from './shuffle'; +export class HDProxydError extends Error { constructor() { super(...arguments); this.isExpected = false; } } -exports.HDProxydError = HDProxydError; /* * This handles the request, and the corresponding response default behaviour */ @@ -58,7 +55,7 @@ function _createRequest(req, log, callback) { function _parseBootstrapList(list) { return list.map(value => value.split(':')); } -class HDProxydClient { +export class HDProxydClient { /** * This represent our interface with the hdproxyd server. * @constructor @@ -74,10 +71,10 @@ class HDProxydClient { const options = opts || {}; this.bootstrap = opts.bootstrap === undefined ? [['localhost', '18888']] : _parseBootstrapList(opts.bootstrap); - this.bootstrap = (0, shuffle_1.shuffle)(this.bootstrap); + this.bootstrap = shuffle(this.bootstrap); this.path = '/store/'; this.setCurrentBootstrap(this.bootstrap[0]); - this.httpAgent = new httpagent_1.http.Agent({ + this.httpAgent = new httpAgent.Agent({ freeSocketTimeout: 60 * 1000, timeout: 2 * 60 * 1000, }); @@ -198,7 +195,7 @@ class HDProxydClient { * creation and its sending. */ _handleRequest(method, stream, size, key, log, callback, params, payload) { - // tslint:disable-next-line:no-any + // eslint-disable-next-line @typescript-eslint/no-explicit-any const headers = (params.headers ? params.headers : {}); const req = this._createRequestHeader(method, headers, key, params, log); const host = this.getCurrentBootstrap(); @@ -384,4 +381,3 @@ class HDProxydClient { request.end(); } } -exports.HDProxydClient = HDProxydClient; diff --git a/lib/shuffle.js b/lib/shuffle.js index b7d98569..5b522d6d 100644 --- a/lib/shuffle.js +++ b/lib/shuffle.js @@ -1,7 +1,5 @@ 'use strict'; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.shuffle = shuffle; -const cryptoLib = require("crypto"); +import * as cryptoLib from 'crypto'; const randomBytes = cryptoLib.randomBytes; /* * This set of function allows us to create an efficient shuffle @@ -23,7 +21,7 @@ function nextBytes(numBytes) { return randomBytes(numBytes); } catch (ex) { - throw new Error(`Insufficient entropy: ${ex instanceof Error ? ex.message : "Unknown error"}`); + throw new Error(`Insufficient entropy: ${ex instanceof Error ? ex.message : 'Unknown error'}`); } } /* @@ -56,7 +54,7 @@ function randomRange(min, max) { * @param {Array} array - Any type of array * @return {Array} - The sorted array */ -function shuffle(array) { +export function shuffle(array) { if (array.length === 1) { return array; } diff --git a/package-lock.json b/package-lock.json index 42253675..a0c1f827 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": { @@ -12140,6 +12155,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 +16192,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 99b4ca26..75cb3ac9 100644 --- a/package.json +++ b/package.json @@ -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 9a99611d..05c85fe5 100644 --- a/src/hdcontroller.ts +++ b/src/hdcontroller.ts @@ -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[][]; @@ -248,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(); diff --git a/src/shuffle.ts b/src/shuffle.ts index e7ace791..018898a9 100644 --- a/src/shuffle.ts +++ b/src/shuffle.ts @@ -24,7 +24,7 @@ function nextBytes(numBytes: number): Buffer { try { return randomBytes(numBytes); } catch (ex) { - throw new Error(`Insufficient entropy: ${ex instanceof Error ? ex.message : "Unknown error"}`); + throw new Error(`Insufficient entropy: ${ex instanceof Error ? ex.message : 'Unknown error'}`); } } diff --git a/tsconfig.json b/tsconfig.json index 7f70d32d..0a5329c0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,8 @@ "target": "es2016", "alwaysStrict": true, "skipLibCheck": true, - "module": "commonjs" + "module": "esnext", + "moduleResolution": "node", }, "include": [ "./src/*"