refactor: yarn format

This commit is contained in:
b1ek 2024-07-28 14:52:20 +10:00
parent 75751f7a17
commit 50c243ab7c
Signed by: blek
GPG Key ID: 14546221E3595D0C
5 changed files with 154 additions and 148 deletions

View File

@ -1,28 +1,28 @@
// as per https://www.ietf.org/archive/id/draft-gallagher-openpgp-hkp-05.html#name-machine-readable-indexes // as per https://www.ietf.org/archive/id/draft-gallagher-openpgp-hkp-05.html#name-machine-readable-indexes
export interface Index { export interface Index {
prefix: 'info' | 'pub' | 'uid'; prefix: 'info' | 'pub' | 'uid'
} }
export type Indexes = Index[]; export type Indexes = Index[]
export class InfoLine implements Index { export class InfoLine implements Index {
prefix: 'info'; prefix: 'info'
version: 1; version: 1
count: number; count: number
constructor(indexLine: string) { constructor(indexLine: string) {
parseIndex<InfoLine>(this, indexLine, ['version', 'count']); parseIndex<InfoLine>(this, indexLine, ['version', 'count'])
this.prefix = 'info'; this.prefix = 'info'
if (this.version != 1) { if (this.version != 1) {
throw new Error("InfoLine's version MUST be 1! Got " + this.version); throw new Error("InfoLine's version MUST be 1! Got " + this.version)
} }
if (this.count) { if (this.count) {
if (typeof this.count !== 'number') { if (typeof this.count !== 'number') {
if (isNaN(this.count)) { if (isNaN(this.count)) {
throw new Error("InfoLine's count MUST NOT be NaN!"); throw new Error("InfoLine's count MUST NOT be NaN!")
} }
} }
} }
@ -30,14 +30,14 @@ export class InfoLine implements Index {
} }
export class PubLine implements Index { export class PubLine implements Index {
prefix: 'pub'; prefix: 'pub'
keyid?: string; keyid?: string
keylen?: number; keylen?: number
algorithm?: string; algorithm?: string
creationdate?: number; creationdate?: number
expirationdate?: number; expirationdate?: number
flags?: string; flags?: string
version?: string; version?: string
constructor(indexLine: string) { constructor(indexLine: string) {
parseIndex<PubLine>(this, indexLine, [ parseIndex<PubLine>(this, indexLine, [
@ -48,31 +48,31 @@ export class PubLine implements Index {
'expirationdate', 'expirationdate',
'flags', 'flags',
'version', 'version',
]); ])
this.prefix = 'pub'; this.prefix = 'pub'
if (this.creationdate && typeof this.creationdate !== 'number') { if (this.creationdate && typeof this.creationdate !== 'number') {
this.creationdate = parseFloat(this.creationdate); this.creationdate = parseFloat(this.creationdate)
if (isNaN(this.creationdate)) { if (isNaN(this.creationdate)) {
throw new Error("PubLine's creationdate MUST NOT be NaN!"); throw new Error("PubLine's creationdate MUST NOT be NaN!")
} }
} }
if (this.expirationdate && typeof this.expirationdate !== 'number') { if (this.expirationdate && typeof this.expirationdate !== 'number') {
this.expirationdate = parseFloat(this.expirationdate); this.expirationdate = parseFloat(this.expirationdate)
if (isNaN(this.expirationdate)) { if (isNaN(this.expirationdate)) {
throw new Error("PubLine's expirationdate MUST NOT be NaN!"); throw new Error("PubLine's expirationdate MUST NOT be NaN!")
} }
} }
} }
} }
export class UidLine implements Index { export class UidLine implements Index {
prefix: 'uid'; prefix: 'uid'
uidstring?: string; uidstring?: string
creationdate?: string; creationdate?: string
expirationdate?: string; expirationdate?: string
flags?: string; flags?: string
constructor(indexLine: string) { constructor(indexLine: string) {
parseIndex<UidLine>(this, indexLine, [ parseIndex<UidLine>(this, indexLine, [
@ -80,20 +80,20 @@ export class UidLine implements Index {
'creationdate', 'creationdate',
'expirationdate', 'expirationdate',
'flags', 'flags',
]); ])
this.prefix = 'uid'; this.prefix = 'uid'
} }
} }
export function assertValidPrefix(prefix: string, throwError = true): boolean { export function assertValidPrefix(prefix: string, throwError = true): boolean {
if (['info', 'pub', 'uid'].indexOf(prefix) == -1) { if (['info', 'pub', 'uid'].indexOf(prefix) == -1) {
if (!throwError) { if (!throwError) {
return false; 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 { export function parseIndexes(untyped: string[]): Indexes {
@ -101,42 +101,45 @@ export function parseIndexes(untyped: string[]): Indexes {
.filter((x) => x.split(':').length > 1) .filter((x) => x.split(':').length > 1)
.filter((x) => assertValidPrefix(x.split(':')[0], false)) .filter((x) => assertValidPrefix(x.split(':')[0], false))
.map((x) => { .map((x) => {
const prefix = x.split(':')[0] as 'info' | 'pub' | 'uid'; const prefix = x.split(':')[0] as 'info' | 'pub' | 'uid'
switch (prefix) { switch (prefix) {
case 'info': return new InfoLine(x) case 'info':
case 'pub': return new PubLine(x) return new InfoLine(x)
case 'uid': return new UidLine(x) case 'pub':
return new PubLine(x)
case 'uid':
return new UidLine(x)
} }
}); })
} }
export function parseIndex<T extends Index>( export function parseIndex<T extends Index>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
self: any, self: any,
index: string, index: string,
keys: (keyof T)[], keys: (keyof T)[]
): void { ): void {
let exploded = index.replaceAll('\r', '').split(':'); let exploded = index.replaceAll('\r', '').split(':')
if (keys.length > exploded.length) { if (keys.length > exploded.length) {
throw new Error('keys MUST NOT be longer than index'); throw new Error('keys MUST NOT be longer than index')
} }
const prefix = exploded[0] as 'info' | 'pub' | 'uid'; const prefix = exploded[0] as 'info' | 'pub' | 'uid'
assertValidPrefix(prefix, true); assertValidPrefix(prefix, true)
self.prefix = prefix; self.prefix = prefix
exploded = exploded.slice(1, exploded.length); exploded = exploded.slice(1, exploded.length)
for (let i = 0; i != keys.length; i++) { for (let i = 0; i != keys.length; i++) {
self[keys[i]] = decodeURIComponent(exploded[i]); self[keys[i]] = decodeURIComponent(exploded[i])
} }
} }
export function serializeIndexes(indexes: Indexes): string { export function serializeIndexes(indexes: Indexes): string {
const out: (string | number | undefined)[][] = []; const out: (string | number | undefined)[][] = []
for (const index of indexes) { for (const index of indexes) {
if (index instanceof InfoLine) { if (index instanceof InfoLine) {
out.push(['info', index.version, index.count]); out.push(['info', index.version, index.count])
} }
if (index instanceof PubLine) { if (index instanceof PubLine) {
out.push([ out.push([
@ -148,7 +151,7 @@ export function serializeIndexes(indexes: Indexes): string {
index.expirationdate, index.expirationdate,
index.flags, index.flags,
index.version, index.version,
]); ])
} }
if (index instanceof UidLine) { if (index instanceof UidLine) {
out.push([ out.push([
@ -157,17 +160,18 @@ export function serializeIndexes(indexes: Indexes): string {
index.creationdate, index.creationdate,
index.expirationdate, index.expirationdate,
index.flags, index.flags,
]); ])
} }
} }
return ( return (
out out
.map((x) => .map((x) =>
x.map(x => x ?? '') x
.map((x) => x ?? '')
.map(encodeURIComponent) .map(encodeURIComponent)
.join(':') .join(':')
) )
.join('\n') + '\n' .join('\n') + '\n'
); )
} }

View File

@ -1,8 +1,8 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common'
import { HKPController } from './controllers/hkp.controller.js'; import { HKPController } from './controllers/hkp.controller.js'
import { OpenPGPKeysProvider } from './providers/openpgp.provider.js'; import { OpenPGPKeysProvider } from './providers/openpgp.provider.js'
import { AllKeysProvider } from './providers/all.provider.js'; import { AllKeysProvider } from './providers/all.provider.js'
import { UbuntuKeysProvider } from './providers/ubuntu.provider.js'; import { UbuntuKeysProvider } from './providers/ubuntu.provider.js'
@Module({ @Module({
providers: [OpenPGPKeysProvider, UbuntuKeysProvider, AllKeysProvider], providers: [OpenPGPKeysProvider, UbuntuKeysProvider, AllKeysProvider],

View File

@ -5,17 +5,19 @@ import {
} from './abstract.provider.js' } from './abstract.provider.js'
import { Indexes, InfoLine } from '../indexes.js' import { Indexes, InfoLine } from '../indexes.js'
import { OpenPGPKeysProvider } from './openpgp.provider.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 * This provider searches all key providers and returns their combined result
*/ */
@Injectable() @Injectable()
export class AllKeysProvider implements AbstractKeysProvider { export class AllKeysProvider implements AbstractKeysProvider {
readonly url = Object.freeze('http://none')
readonly url = Object.freeze('http://none'); constructor(
private openPgpKeysProvider: OpenPGPKeysProvider,
constructor(private openPgpKeysProvider: OpenPGPKeysProvider, private ubuntuKeysProvider: UbuntuKeysProvider) {} private ubuntuKeysProvider: UbuntuKeysProvider
) {}
getAll(): AbstractKeysProvider[] { getAll(): AbstractKeysProvider[] {
return [this.openPgpKeysProvider, this.ubuntuKeysProvider] return [this.openPgpKeysProvider, this.ubuntuKeysProvider]

View File

@ -1,11 +1,11 @@
import type { KyResponse, ResponsePromise } from 'ky'; import type { KyResponse, ResponsePromise } from 'ky'
import ky from 'ky' import ky from 'ky'
import type { import type {
AdditionalData, AdditionalData,
GetOperationReturn, GetOperationReturn,
HKPOperation, HKPOperation,
} from './abstract.provider.js' } from './abstract.provider.js'
import type { Indexes} from '../indexes.js'; import type { Indexes } from '../indexes.js'
import { parseIndexes } from '../indexes.js' import { parseIndexes } from '../indexes.js'
const BEGIN_HEADER = '-----BEGIN PGP PUBLIC KEY BLOCK-----' const BEGIN_HEADER = '-----BEGIN PGP PUBLIC KEY BLOCK-----'
@ -35,7 +35,7 @@ export function proxyRequest(
op, op,
search, search,
}, },
}); })
} }
export async function proxyGetOp( export async function proxyGetOp(
@ -43,17 +43,17 @@ export async function proxyGetOp(
search: string, search: string,
data: AdditionalData data: AdditionalData
): Promise<GetOperationReturn> { ): Promise<GetOperationReturn> {
let httpRes: KyResponse; let httpRes: KyResponse
try { try {
httpRes = await proxyRequest(url, 'index', search, data); httpRes = await proxyRequest(url, 'index', search, data)
if (httpRes.status !== 200) { if (httpRes.status !== 200) {
return 404; return 404
} }
} catch { } catch {
return 404 return 404
} }
return getKey(await httpRes.text()); return getKey(await httpRes.text())
} }
export async function proxyIndexOp( export async function proxyIndexOp(
@ -61,9 +61,9 @@ export async function proxyIndexOp(
search: string, search: string,
data: AdditionalData data: AdditionalData
): Promise<Indexes> { ): Promise<Indexes> {
let httpRes: KyResponse; let httpRes: KyResponse
try { try {
httpRes = await proxyRequest(url, 'index', search, data); httpRes = await proxyRequest(url, 'index', search, data)
if (httpRes.status !== 200) { if (httpRes.status !== 200) {
return [] return []
} }

View File

@ -1,4 +1,4 @@
import type { TestingModule } from '@nestjs/testing'; import type { TestingModule } from '@nestjs/testing'
import { Test } from '@nestjs/testing' import { Test } from '@nestjs/testing'
import type { INestApplication } from '@nestjs/common' import type { INestApplication } from '@nestjs/common'
import request from 'supertest' import request from 'supertest'