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
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<InfoLine>(this, indexLine, ['version', 'count']);
parseIndex<InfoLine>(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);
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!");
throw new Error("InfoLine's count MUST NOT be NaN!")
}
}
}
@ -30,14 +30,14 @@ export class InfoLine implements Index {
}
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<PubLine>(this, indexLine, [
@ -48,31 +48,31 @@ export class PubLine implements Index {
'expirationdate',
'flags',
'version',
]);
])
this.prefix = 'pub';
this.prefix = 'pub'
if (this.creationdate && typeof this.creationdate !== 'number') {
this.creationdate = parseFloat(this.creationdate);
this.creationdate = parseFloat(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') {
this.expirationdate = parseFloat(this.expirationdate);
this.expirationdate = parseFloat(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 {
prefix: 'uid';
uidstring?: string;
creationdate?: string;
expirationdate?: string;
flags?: string;
prefix: 'uid'
uidstring?: string
creationdate?: string
expirationdate?: string
flags?: string
constructor(indexLine: string) {
parseIndex<UidLine>(this, indexLine, [
@ -80,20 +80,20 @@ export class UidLine implements Index {
'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;
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 {
@ -101,42 +101,45 @@ export function parseIndexes(untyped: string[]): Indexes {
.filter((x) => x.split(':').length > 1)
.filter((x) => assertValidPrefix(x.split(':')[0], false))
.map((x) => {
const prefix = x.split(':')[0] as 'info' | 'pub' | 'uid';
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)
case 'info':
return new InfoLine(x)
case 'pub':
return new PubLine(x)
case 'uid':
return new UidLine(x)
}
});
})
}
export function parseIndex<T extends Index>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
self: any,
index: string,
keys: (keyof T)[],
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');
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]);
self[keys[i]] = decodeURIComponent(exploded[i])
}
}
export function serializeIndexes(indexes: Indexes): string {
const out: (string | number | undefined)[][] = [];
const out: (string | number | undefined)[][] = []
for (const index of indexes) {
if (index instanceof InfoLine) {
out.push(['info', index.version, index.count]);
out.push(['info', index.version, index.count])
}
if (index instanceof PubLine) {
out.push([
@ -148,7 +151,7 @@ export function serializeIndexes(indexes: Indexes): string {
index.expirationdate,
index.flags,
index.version,
]);
])
}
if (index instanceof UidLine) {
out.push([
@ -157,17 +160,18 @@ export function serializeIndexes(indexes: Indexes): string {
index.creationdate,
index.expirationdate,
index.flags,
]);
])
}
}
return (
out
.map((x) =>
x.map(x => x ?? '')
x
.map((x) => x ?? '')
.map(encodeURIComponent)
.join(':')
)
.join('\n') + '\n'
);
)
}

View File

@ -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],

View File

@ -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]

View File

@ -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<GetOperationReturn> {
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<Indexes> {
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 []
}

View File

@ -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'