From 50c243ab7cacfc041d7d4eb421d5175de91327b6 Mon Sep 17 00:00:00 2001 From: b1ek Date: Sun, 28 Jul 2024 14:52:20 +1000 Subject: [PATCH] refactor: yarn format --- src/keys/indexes.ts | 262 +++++++++++++++-------------- src/keys/keys.module.ts | 10 +- src/keys/providers/all.provider.ts | 10 +- src/keys/providers/utils.ts | 18 +- test/app.e2e-spec.ts | 2 +- 5 files changed, 154 insertions(+), 148 deletions(-) diff --git a/src/keys/indexes.ts b/src/keys/indexes.ts index f157125..dbc27b9 100644 --- a/src/keys/indexes.ts +++ b/src/keys/indexes.ts @@ -1,173 +1,177 @@ // as per https://www.ietf.org/archive/id/draft-gallagher-openpgp-hkp-05.html#name-machine-readable-indexes export interface Index { - prefix: 'info' | 'pub' | 'uid'; + prefix: 'info' | 'pub' | 'uid' } -export type Indexes = Index[]; +export type Indexes = Index[] export class InfoLine implements Index { - prefix: 'info'; - version: 1; - count: number; + prefix: 'info' + version: 1 + count: number - constructor(indexLine: string) { - parseIndex(this, indexLine, ['version', 'count']); + constructor(indexLine: string) { + parseIndex(this, indexLine, ['version', 'count']) - this.prefix = 'info'; + this.prefix = 'info' - if (this.version != 1) { - throw new Error("InfoLine's version MUST be 1! Got " + this.version); - } - if (this.count) { - if (typeof this.count !== 'number') { - if (isNaN(this.count)) { - throw new Error("InfoLine's count MUST NOT be NaN!"); + if (this.version != 1) { + throw new Error("InfoLine's version MUST be 1! Got " + this.version) + } + if (this.count) { + if (typeof this.count !== 'number') { + if (isNaN(this.count)) { + throw new Error("InfoLine's count MUST NOT be NaN!") + } + } } - } } - } } export class PubLine implements Index { - prefix: 'pub'; - keyid?: string; - keylen?: number; - algorithm?: string; - creationdate?: number; - expirationdate?: number; - flags?: string; - version?: string; + prefix: 'pub' + keyid?: string + keylen?: number + algorithm?: string + creationdate?: number + expirationdate?: number + flags?: string + version?: string - constructor(indexLine: string) { - parseIndex(this, indexLine, [ - 'keyid', - 'keylen', - 'algorithm', - 'creationdate', - 'expirationdate', - 'flags', - 'version', - ]); + constructor(indexLine: string) { + parseIndex(this, indexLine, [ + 'keyid', + 'keylen', + 'algorithm', + 'creationdate', + 'expirationdate', + 'flags', + 'version', + ]) - this.prefix = 'pub'; + this.prefix = 'pub' - if (this.creationdate && typeof this.creationdate !== 'number') { - this.creationdate = parseFloat(this.creationdate); - if (isNaN(this.creationdate)) { - throw new Error("PubLine's creationdate MUST NOT be NaN!"); - } + if (this.creationdate && typeof this.creationdate !== 'number') { + this.creationdate = parseFloat(this.creationdate) + if (isNaN(this.creationdate)) { + throw new Error("PubLine's creationdate MUST NOT be NaN!") + } + } + if (this.expirationdate && typeof this.expirationdate !== 'number') { + this.expirationdate = parseFloat(this.expirationdate) + if (isNaN(this.expirationdate)) { + throw new Error("PubLine's expirationdate MUST NOT be NaN!") + } + } } - if (this.expirationdate && typeof this.expirationdate !== 'number') { - this.expirationdate = parseFloat(this.expirationdate); - if (isNaN(this.expirationdate)) { - throw new Error("PubLine's expirationdate MUST NOT be NaN!"); - } - } - } } export class UidLine implements Index { - prefix: 'uid'; - uidstring?: string; - creationdate?: string; - expirationdate?: string; - flags?: string; + prefix: 'uid' + uidstring?: string + creationdate?: string + expirationdate?: string + flags?: string - constructor(indexLine: string) { - parseIndex(this, indexLine, [ - 'uidstring', - 'creationdate', - 'expirationdate', - 'flags', - ]); + constructor(indexLine: string) { + parseIndex(this, indexLine, [ + 'uidstring', + 'creationdate', + 'expirationdate', + 'flags', + ]) - this.prefix = 'uid'; - } + this.prefix = 'uid' + } } export function assertValidPrefix(prefix: string, throwError = true): boolean { - if (['info', 'pub', 'uid'].indexOf(prefix) == -1) { - if (!throwError) { - return false; + if (['info', 'pub', 'uid'].indexOf(prefix) == -1) { + if (!throwError) { + return false + } + throw new Error('Prefix must be one of: info, pub, uid') } - throw new Error('Prefix must be one of: info, pub, uid'); - } - return true; + return true } export function parseIndexes(untyped: string[]): Indexes { - return untyped - .filter((x) => x.split(':').length > 1) - .filter((x) => assertValidPrefix(x.split(':')[0], false)) - .map((x) => { - const prefix = x.split(':')[0] as 'info' | 'pub' | 'uid'; - switch (prefix) { - case 'info': return new InfoLine(x) - case 'pub': return new PubLine(x) - case 'uid': return new UidLine(x) - } - }); + return untyped + .filter((x) => x.split(':').length > 1) + .filter((x) => assertValidPrefix(x.split(':')[0], false)) + .map((x) => { + const prefix = x.split(':')[0] as 'info' | 'pub' | 'uid' + switch (prefix) { + case 'info': + return new InfoLine(x) + case 'pub': + return new PubLine(x) + case 'uid': + return new UidLine(x) + } + }) } export function parseIndex( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - self: any, - index: string, - keys: (keyof T)[], + // eslint-disable-next-line @typescript-eslint/no-explicit-any + self: any, + index: string, + keys: (keyof T)[] ): void { - let exploded = index.replaceAll('\r', '').split(':'); + let exploded = index.replaceAll('\r', '').split(':') - if (keys.length > exploded.length) { - throw new Error('keys MUST NOT be longer than index'); - } + if (keys.length > exploded.length) { + throw new Error('keys MUST NOT be longer than index') + } - const prefix = exploded[0] as 'info' | 'pub' | 'uid'; - assertValidPrefix(prefix, true); - self.prefix = prefix; - exploded = exploded.slice(1, exploded.length); + const prefix = exploded[0] as 'info' | 'pub' | 'uid' + assertValidPrefix(prefix, true) + self.prefix = prefix + exploded = exploded.slice(1, exploded.length) - for (let i = 0; i != keys.length; i++) { - self[keys[i]] = decodeURIComponent(exploded[i]); - } + for (let i = 0; i != keys.length; i++) { + self[keys[i]] = decodeURIComponent(exploded[i]) + } } export function serializeIndexes(indexes: Indexes): string { - const out: (string | number | undefined)[][] = []; - for (const index of indexes) { - if (index instanceof InfoLine) { - out.push(['info', index.version, index.count]); + const out: (string | number | undefined)[][] = [] + for (const index of indexes) { + if (index instanceof InfoLine) { + out.push(['info', index.version, index.count]) + } + if (index instanceof PubLine) { + out.push([ + 'pub', + index.keyid, + index.algorithm, + index.keylen, + index.creationdate, + index.expirationdate, + index.flags, + index.version, + ]) + } + if (index instanceof UidLine) { + out.push([ + 'uid', + index.uidstring, + index.creationdate, + index.expirationdate, + index.flags, + ]) + } } - if (index instanceof PubLine) { - out.push([ - 'pub', - index.keyid, - index.algorithm, - index.keylen, - index.creationdate, - index.expirationdate, - index.flags, - index.version, - ]); - } - if (index instanceof UidLine) { - out.push([ - 'uid', - index.uidstring, - index.creationdate, - index.expirationdate, - index.flags, - ]); - } - } - return ( - out - .map((x) => - x.map(x => x ?? '') - .map(encodeURIComponent) - .join(':') - ) - .join('\n') + '\n' - ); + return ( + out + .map((x) => + x + .map((x) => x ?? '') + .map(encodeURIComponent) + .join(':') + ) + .join('\n') + '\n' + ) } diff --git a/src/keys/keys.module.ts b/src/keys/keys.module.ts index 911e0a1..e69ee9b 100644 --- a/src/keys/keys.module.ts +++ b/src/keys/keys.module.ts @@ -1,8 +1,8 @@ -import { Module } from '@nestjs/common'; -import { HKPController } from './controllers/hkp.controller.js'; -import { OpenPGPKeysProvider } from './providers/openpgp.provider.js'; -import { AllKeysProvider } from './providers/all.provider.js'; -import { UbuntuKeysProvider } from './providers/ubuntu.provider.js'; +import { Module } from '@nestjs/common' +import { HKPController } from './controllers/hkp.controller.js' +import { OpenPGPKeysProvider } from './providers/openpgp.provider.js' +import { AllKeysProvider } from './providers/all.provider.js' +import { UbuntuKeysProvider } from './providers/ubuntu.provider.js' @Module({ providers: [OpenPGPKeysProvider, UbuntuKeysProvider, AllKeysProvider], diff --git a/src/keys/providers/all.provider.ts b/src/keys/providers/all.provider.ts index 350a905..815c3e3 100644 --- a/src/keys/providers/all.provider.ts +++ b/src/keys/providers/all.provider.ts @@ -5,17 +5,19 @@ import { } from './abstract.provider.js' import { Indexes, InfoLine } from '../indexes.js' import { OpenPGPKeysProvider } from './openpgp.provider.js' -import { UbuntuKeysProvider } from './ubuntu.provider.js'; +import { UbuntuKeysProvider } from './ubuntu.provider.js' /** * This provider searches all key providers and returns their combined result */ @Injectable() export class AllKeysProvider implements AbstractKeysProvider { + readonly url = Object.freeze('http://none') - readonly url = Object.freeze('http://none'); - - constructor(private openPgpKeysProvider: OpenPGPKeysProvider, private ubuntuKeysProvider: UbuntuKeysProvider) {} + constructor( + private openPgpKeysProvider: OpenPGPKeysProvider, + private ubuntuKeysProvider: UbuntuKeysProvider + ) {} getAll(): AbstractKeysProvider[] { return [this.openPgpKeysProvider, this.ubuntuKeysProvider] diff --git a/src/keys/providers/utils.ts b/src/keys/providers/utils.ts index e07e725..fb98f9e 100644 --- a/src/keys/providers/utils.ts +++ b/src/keys/providers/utils.ts @@ -1,11 +1,11 @@ -import type { KyResponse, ResponsePromise } from 'ky'; +import type { KyResponse, ResponsePromise } from 'ky' import ky from 'ky' import type { AdditionalData, GetOperationReturn, HKPOperation, } from './abstract.provider.js' -import type { Indexes} from '../indexes.js'; +import type { Indexes } from '../indexes.js' import { parseIndexes } from '../indexes.js' const BEGIN_HEADER = '-----BEGIN PGP PUBLIC KEY BLOCK-----' @@ -35,7 +35,7 @@ export function proxyRequest( op, search, }, - }); + }) } export async function proxyGetOp( @@ -43,17 +43,17 @@ export async function proxyGetOp( search: string, data: AdditionalData ): Promise { - let httpRes: KyResponse; + let httpRes: KyResponse try { - httpRes = await proxyRequest(url, 'index', search, data); + httpRes = await proxyRequest(url, 'index', search, data) if (httpRes.status !== 200) { - return 404; + return 404 } } catch { return 404 } - return getKey(await httpRes.text()); + return getKey(await httpRes.text()) } export async function proxyIndexOp( @@ -61,9 +61,9 @@ export async function proxyIndexOp( search: string, data: AdditionalData ): Promise { - let httpRes: KyResponse; + let httpRes: KyResponse try { - httpRes = await proxyRequest(url, 'index', search, data); + httpRes = await proxyRequest(url, 'index', search, data) if (httpRes.status !== 200) { return [] } diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts index dc067ff..c02e3fb 100644 --- a/test/app.e2e-spec.ts +++ b/test/app.e2e-spec.ts @@ -1,4 +1,4 @@ -import type { TestingModule } from '@nestjs/testing'; +import type { TestingModule } from '@nestjs/testing' import { Test } from '@nestjs/testing' import type { INestApplication } from '@nestjs/common' import request from 'supertest'