From d44d1e610317c146f4802cf14abd807e026a87b0 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Wed, 4 Jan 2023 15:41:26 +0100 Subject: [PATCH] enable BE prefer-const eslint rule --- .eslintrc.cjs | 2 +- backend/src/access/index.ts | 2 +- backend/src/activitypub/deliver.ts | 2 +- backend/src/utils/http-signing.ts | 2 +- backend/src/utils/httpsigjs/parser.ts | 22 +++++++++++----------- backend/src/utils/httpsigjs/utils.ts | 2 +- backend/test/utils.spec.ts | 4 +--- 7 files changed, 17 insertions(+), 19 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 5c1bf2f..7c5deba 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -12,6 +12,7 @@ module.exports = { plugins: ['@typescript-eslint'], root: true, rules: { + 'prefer-const': 'error', 'no-var': 'error', /* Note: the following rules have been set to off so that linting @@ -25,7 +26,6 @@ module.exports = { '@typescript-eslint/restrict-plus-operands': 'off', 'no-constant-condition': 'off', '@typescript-eslint/await-thenable': 'off', - 'prefer-const': 'off', '@typescript-eslint/require-await': 'off', '@typescript-eslint/restrict-template-expressions': 'off', '@typescript-eslint/no-misused-promises': 'off', diff --git a/backend/src/access/index.ts b/backend/src/access/index.ts index defbbb5..7554109 100644 --- a/backend/src/access/index.ts +++ b/backend/src/access/index.ts @@ -64,7 +64,7 @@ const base64URLDecode = (s: string) => { } const asciiToUint8Array = (s: string) => { - let chars = [] + const chars = [] for (let i = 0; i < s.length; ++i) { chars.push(s.charCodeAt(i)) } diff --git a/backend/src/activitypub/deliver.ts b/backend/src/activitypub/deliver.ts index 3b06f1e..9806f14 100644 --- a/backend/src/activitypub/deliver.ts +++ b/backend/src/activitypub/deliver.ts @@ -14,7 +14,7 @@ const headers = { export async function deliverToActor(signingKey: CryptoKey, from: Actor, to: Actor, activity: Activity) { const body = JSON.stringify(activity) console.log({ body }) - let req = new Request(to.inbox, { + const req = new Request(to.inbox, { method: 'POST', body, headers, diff --git a/backend/src/utils/http-signing.ts b/backend/src/utils/http-signing.ts index b594815..2320d78 100644 --- a/backend/src/utils/http-signing.ts +++ b/backend/src/utils/http-signing.ts @@ -20,7 +20,7 @@ export async function signRequest(request: Request, key: CryptoKey, keyId: URL): request.headers.set('Host', url.host) } - let components = ['@request-target', 'host'] + const components = ['@request-target', 'host'] if (request.method == 'POST') { components.push('digest') } diff --git a/backend/src/utils/httpsigjs/parser.ts b/backend/src/utils/httpsigjs/parser.ts index 7f95ba8..940909a 100644 --- a/backend/src/utils/httpsigjs/parser.ts +++ b/backend/src/utils/httpsigjs/parser.ts @@ -5,12 +5,12 @@ import { HEADER, HttpSignatureError, InvalidAlgorithmError, validateAlgorithm } ///--- Globals -let State = { +const State = { New: 0, Params: 1, } -let ParamsState = { +const ParamsState = { Name: 0, Quote: 1, Value: 2, @@ -125,10 +125,10 @@ export function parseRequest(request: Request, options?: Options): ParsedSignatu headers = options.headers } - let authz = request.headers.get(HEADER.AUTH) || request.headers.get(HEADER.SIG) + const authz = request.headers.get(HEADER.AUTH) || request.headers.get(HEADER.SIG) if (!authz) { - let errHeader = HEADER.AUTH + ' or ' + HEADER.SIG + const errHeader = HEADER.AUTH + ' or ' + HEADER.SIG throw new MissingHeaderError('no ' + errHeader + ' header ' + 'present in the request') } @@ -141,14 +141,14 @@ export function parseRequest(request: Request, options?: Options): ParsedSignatu let tmpName = '' let tmpValue = '' - let parsed = { + const parsed = { scheme: authz === request.headers.get(HEADER.SIG) ? 'Signature' : '', params: {}, signingString: '', } for (i = 0; i < authz.length; i++) { - let c = authz.charAt(i) + const c = authz.charAt(i) let code = c.charCodeAt(0) switch (Number(state)) { @@ -268,7 +268,7 @@ export function parseRequest(request: Request, options?: Options): ParsedSignatu // Build the signingString for (i = 0; i < parsed.params.headers.length; i++) { - let h = parsed.params.headers[i].toLowerCase() + const h = parsed.params.headers[i].toLowerCase() parsed.params.headers[i] = h if (h === 'request-line') { @@ -290,7 +290,7 @@ export function parseRequest(request: Request, options?: Options): ParsedSignatu } else if (h === '(algorithm)') { parsed.signingString += '(algorithm): ' + parsed.params.algorithm } else if (h === '(opaque)') { - let opaque = parsed.params.opaque + const opaque = parsed.params.opaque if (opaque === undefined) { throw new MissingHeaderError('opaque param was not in the ' + authzHeaderName + ' header') } @@ -300,7 +300,7 @@ export function parseRequest(request: Request, options?: Options): ParsedSignatu } else if (h === '(expires)') { parsed.signingString += '(expires): ' + parsed.params.expires } else { - let value = request.headers.get(h) + const value = request.headers.get(h) if (value === null) throw new MissingHeaderError(h + ' was not in the request') parsed.signingString += h + ': ' + value } @@ -317,7 +317,7 @@ export function parseRequest(request: Request, options?: Options): ParsedSignatu } else { date = new Date(request.headers.date) } - let now = new Date() + const now = new Date() skew = Math.abs(now.getTime() - date.getTime()) if (skew > options.clockSkew * 1000) { @@ -341,7 +341,7 @@ export function parseRequest(request: Request, options?: Options): ParsedSignatu } if (parsed.params.expires) { - let expiredSince = Math.floor(Date.now() / 1000) - parsed.params.expires + const expiredSince = Math.floor(Date.now() / 1000) - parsed.params.expires if (expiredSince > options.clockSkew) { throw new ExpiredRequestError( 'Request expired with skew ' + expiredSince + 's greater than allowed ' + options.clockSkew + 's' diff --git a/backend/src/utils/httpsigjs/utils.ts b/backend/src/utils/httpsigjs/utils.ts index 3493a9b..95860eb 100644 --- a/backend/src/utils/httpsigjs/utils.ts +++ b/backend/src/utils/httpsigjs/utils.ts @@ -31,7 +31,7 @@ export class InvalidAlgorithmError extends HttpSignatureError { * @returns {[string, string]} */ export function validateAlgorithm(algorithm: string, publicKeyType?: string): [string, string] { - var alg = algorithm.toLowerCase().split('-') + const alg = algorithm.toLowerCase().split('-') if (alg[0] === 'hs2019') { return publicKeyType !== undefined ? validateAlgorithm(publicKeyType + '-sha256') : ['hs2019', 'sha256'] diff --git a/backend/test/utils.spec.ts b/backend/test/utils.spec.ts index 32bef84..65dab63 100644 --- a/backend/test/utils.spec.ts +++ b/backend/test/utils.spec.ts @@ -66,9 +66,7 @@ describe('utils', () => { }) test('URL to handle', async () => { - let res - - res = urlToHandle(new URL('https://host.org/users/foobar')) + const res = urlToHandle(new URL('https://host.org/users/foobar')) assert.equal(res, 'foobar@host.org') }) })