From ae7b911b16ea29c351acb9f39becbd3d3cd1c082 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Wed, 19 Jun 2024 17:10:00 +0200 Subject: [PATCH] perf: :label: fix most of slow types using deno lint --- deno.json | 2 +- src/db/mod.ts | 6 +++--- src/models/src/credential.ts | 4 ++-- src/models/src/group.ts | 30 +++++++++++++----------------- src/models/src/machine.ts | 32 ++++++++++++++------------------ src/models/src/service.ts | 14 ++++++++------ src/models/src/user.ts | 14 +++++++------- src/models/utils/ref.ts | 36 +++++++++++++++++++++--------------- 8 files changed, 69 insertions(+), 69 deletions(-) diff --git a/deno.json b/deno.json index 3a1fc05..8aa7014 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@cohabit/ressources", - "version": "0.1.1", + "version": "0.1.2", "exports": { ".": "./mod.ts", "./models": "./src/models/mod.ts" diff --git a/src/db/mod.ts b/src/db/mod.ts index 511a71c..b8f92b3 100644 --- a/src/db/mod.ts +++ b/src/db/mod.ts @@ -14,7 +14,7 @@ import type { CredentialCategory } from '../models/src/credential.ts' export class Db { #kv: Deno.Kv - static async init(path?: string) { + static async init(path?: string): Promise { const kv = await Deno.openKv(path ?? Deno.env.get('DB_PATH')) return new Db(kv) } @@ -23,11 +23,11 @@ export class Db { this.#kv = kv } - get storage() { + get storage(): Deno.Kv { return this.#kv } - get prefix() { + get prefix(): { ressource: 'ressource' } { return { ressource: 'ressource', } diff --git a/src/models/src/credential.ts b/src/models/src/credential.ts index 43b0265..1e149e6 100644 --- a/src/models/src/credential.ts +++ b/src/models/src/credential.ts @@ -65,11 +65,11 @@ export class Credential extends Ressource { return 'credential' } - get category() { + get category(): T { return this.#category } - get store() { + get store(): Readonly['store']> { return this.#store } diff --git a/src/models/src/group.ts b/src/models/src/group.ts index 9da55cd..49e9512 100644 --- a/src/models/src/group.ts +++ b/src/models/src/group.ts @@ -50,13 +50,7 @@ export class Group extends Ressource { } #posix?: Posix - #permissions: Readonly<{ - [serviceOrMachine: UUID]: { - read: boolean - write: boolean - execute: boolean - } - }> + #permissions: Readonly #groups: readonly Ref[] private constructor( @@ -93,15 +87,15 @@ export class Group extends Ressource { return 'group' } - get posix() { + get posix(): Posix | undefined { return this.#posix } - get permissions() { + get permissions(): Readonly { return this.#permissions } - get groups() { + get groups(): readonly Ref[] { return this.#groups } @@ -134,11 +128,13 @@ export interface Group extends Ressource { type: 'group' posix: Posix | undefined groups: readonly Ref[] - permissions: Readonly<{ - [serviceOrMachine: UUID]: { - read: boolean - write: boolean - execute: boolean - } - }> + permissions: Readonly +} + +export type GroupPermissions = { + [serviceOrMachine: UUID]: { + read: boolean + write: boolean + execute: boolean + } } diff --git a/src/models/src/machine.ts b/src/models/src/machine.ts index 0ba4990..53232c8 100644 --- a/src/models/src/machine.ts +++ b/src/models/src/machine.ts @@ -53,13 +53,7 @@ export class Machine extends Ressource { #tags: readonly string[] #url: UrlString - #status: - | 'ready' - | 'busy' - | 'unavailable' - | 'discontinued' - | 'error' - | 'unknown' + #status: MachineStatus #groups: readonly Ref[] private constructor( @@ -83,16 +77,16 @@ export class Machine extends Ressource { get type(): 'machine' { return 'machine' } - get tags() { + get tags(): readonly string[] { return this.#tags } - get url() { + get url(): UrlString { return this.#url } - get status() { + get status(): MachineStatus { return this.#status } - get groups() { + get groups(): readonly Ref[] { return this.#groups } @@ -128,12 +122,14 @@ export interface Machine extends Ressource { type: 'machine' tags: readonly string[] url: UrlString - status: - | 'ready' - | 'busy' - | 'unavailable' - | 'discontinued' - | 'error' - | 'unknown' + status: MachineStatus groups: readonly Ref[] } + +export type MachineStatus = + | 'ready' + | 'busy' + | 'unavailable' + | 'discontinued' + | 'error' + | 'unknown' diff --git a/src/models/src/service.ts b/src/models/src/service.ts index d4ec190..f3a2ee7 100644 --- a/src/models/src/service.ts +++ b/src/models/src/service.ts @@ -52,7 +52,7 @@ export class Service extends Ressource { return this.create({ name, avatar, tags, url, category, groups }) } - #category: 'web' | 'fs' | 'git' + #category: ServiceCategory #url: UrlString #groups: readonly Ref[] #tags: readonly string[] @@ -92,19 +92,19 @@ export class Service extends Ressource { return 'service' } - get category() { + get category(): ServiceCategory { return this.#category } - get tags() { + get tags(): readonly string[] { return this.#tags } - get url() { + get url(): UrlString { return this.#url } - get groups() { + get groups(): readonly Ref[] { return this.#groups } @@ -138,8 +138,10 @@ export class Service extends Ressource { export interface Service extends Ressource { type: 'service' - category: 'web' | 'fs' | 'git' + category: ServiceCategory tags: readonly string[] url: UrlString groups: readonly Ref[] } + +export type ServiceCategory = 'web' | 'fs' | 'git' diff --git a/src/models/src/user.ts b/src/models/src/user.ts index 775d180..d3588b6 100644 --- a/src/models/src/user.ts +++ b/src/models/src/user.ts @@ -150,25 +150,25 @@ export class User extends Ressource { get type(): 'user' { return 'user' } - get firstname() { + get firstname(): string { return this.#firstname } - get lastname() { + get lastname(): string { return this.#lastname } - get login() { + get login(): Login { return this.#login } - get mail() { + get mail(): MailAddress { return this.#mail } - get groups() { + get groups(): readonly Ref[] { return this.#groups } - get posix() { + get posix(): Posix | undefined { return this.#posix } - get credentials() { + get credentials(): readonly Ref>[] { return this.#credentials } diff --git a/src/models/utils/ref.ts b/src/models/utils/ref.ts index 8475e0d..847c3d9 100644 --- a/src/models/utils/ref.ts +++ b/src/models/utils/ref.ts @@ -22,7 +22,9 @@ export class Ref extends String { return `@ref/${type}#${uuid}` as const } - static parse(string: RefString) { + static parse( + string: RefString, + ): { type: T['type']; uuid: UUID } { const [_, value] = string.split('/') const [type, uuid] = value.split('#') as [T['type'], UUID] @@ -33,11 +35,13 @@ export class Ref extends String { return new Ref(ressource) } - static fromString(string: RefString) { + static fromString(string: RefString): Ref { return new Ref(Ref.parse(string)) } - static dbResolver(db: Db) { + static dbResolver( + db: Db, + ): (ref: RefString) => Promise { return (ref: RefString): Promise => { const { type, uuid } = Ref.parse(ref) //@ts-expect-error force type casting to fix @@ -45,27 +49,29 @@ export class Ref extends String { } } - static restResolver(endpoint: string | URL) { - return async (ref: RefString) => { + static restResolver( + endpoint: string | URL, + ): (ref: RefString) => Promise { + return async (ref: RefString): Promise => { const { type, uuid } = Ref.parse(ref) const url = new URL(`${type}s/${uuid}`, endpoint) const response = await fetch(url) const json = await response.json() if (type === 'user') { - return User.fromJSON(json) + return User.fromJSON(json) as unknown as T } if (type === 'machine') { - return Machine.fromJSON(json) + return Machine.fromJSON(json) as unknown as T } if (type === 'service') { - return Service.fromJSON(json) + return Service.fromJSON(json) as unknown as T } if (type === 'group') { - return Group.fromJSON(json) + return Group.fromJSON(json) as unknown as T } if (type === 'credential') { - return Credential.fromJSON(json) + return Credential.fromJSON(json) as unknown as T } throw new TypeError(`unknown ref type "${type}"`) @@ -81,23 +87,23 @@ export class Ref extends String { #type: T['type'] #uuid: UUID - get type() { + get type(): T['type'] { return this.#type } - get uuid() { + get uuid(): UUID { return this.#uuid } - ref(resolver: RefResolver) { + ref(resolver: RefResolver): T | Promise { return resolver(this.toString()) } - toString() { + toString(): `@ref/${T['type']}#${UUID}` { return Ref.#toString({ uuid: this.uuid, type: this.type }) } - toJSON() { + toJSON(): `@ref/${T['type']}#${UUID}` { return this.toString() } }