Compare commits

..

No commits in common. "28a517323b2b5d28c7e9b6ac67de79262247ee8e" and "dda72bf67d4bb8a88ea018c4f4d0bc86408faf73" have entirely different histories.

15 changed files with 148 additions and 370 deletions

4
.gitignore vendored
View file

@ -1,8 +1,6 @@
# Directories # Directories
docs docs
tmp
temp
# Files # Files
.env .env
*.sqlite* *.sqlite

View file

@ -1,3 +0,0 @@
{
"conventionalCommits.scopes": ["model", "chore", "db"]
}

View file

@ -1,10 +1,5 @@
{ {
"name": "@cohabit/ressources", "name": "@cohabit/ressources",
"version": "0.1.0",
"exports": {
".": "./mod.ts",
"./models": "./src/models/mod.ts"
},
"tasks": { "tasks": {
"start": "deno run --unstable-kv ./mod.ts" "start": "deno run --unstable-kv ./mod.ts"
}, },

2
mod.ts
View file

@ -1,2 +0,0 @@
export * from '@models'
export { Db } from '@db'

View file

@ -1,12 +1,4 @@
import { import { Credential, Group, Machine, Ressource, Service, User } from '@models'
Credential,
Group,
Machine,
type Ressource,
Service,
User,
} from '@models'
import type { CredentialCategory } from '@models/credential.ts'
//!TODO link ressources (get, list) //!TODO link ressources (get, list)
//!TODO Purge unused ressources (delete) //!TODO Purge unused ressources (delete)
@ -34,10 +26,7 @@ export class Db {
get ressource() { get ressource() {
return { return {
credential: this.#ressourceStorage<Credential<CredentialCategory>>( credential: this.#ressourceStorage<Credential>('credential', Credential),
'credential',
Credential,
),
group: this.#ressourceStorage<Group>('group', Group), group: this.#ressourceStorage<Group>('group', Group),
machine: this.#ressourceStorage<Machine>('machine', Machine), machine: this.#ressourceStorage<Machine>('machine', Machine),
service: this.#ressourceStorage<Service>('service', Service), service: this.#ressourceStorage<Service>('service', Service),
@ -55,9 +44,8 @@ export class Db {
set: (ressources: T[]) => this.#set<T>(type, ressources), set: (ressources: T[]) => this.#set<T>(type, ressources),
delete: (ressources: Pick<T, 'uuid'>[]) => delete: (ressources: Pick<T, 'uuid'>[]) =>
this.#delete<T>(type, ressources), this.#delete<T>(type, ressources),
list: ( list: (filter: (ressource: T) => boolean = () => true) =>
filter: (ressource: T) => boolean | Promise<boolean> = () => true, this.#list<T>(type, Builder, filter),
) => this.#list<T>(type, Builder, filter),
} }
} }
@ -106,7 +94,7 @@ export class Db {
async *#list<T extends Ressource>( async *#list<T extends Ressource>(
type: RessourceType<T>, type: RessourceType<T>,
Builder: RessourceBuilder<T>, Builder: RessourceBuilder<T>,
filter: (entry: T) => boolean | Promise<boolean>, filter: (entry: T) => boolean,
): AsyncGenerator<T, void, void> { ): AsyncGenerator<T, void, void> {
const list = this.#kv.list<RessourceJson<T>>({ const list = this.#kv.list<RessourceJson<T>>({
prefix: [this.prefix.ressource, type], prefix: [this.prefix.ressource, type],
@ -115,23 +103,22 @@ export class Db {
const value = entry.value const value = entry.value
//@ts-expect-error Type union of Ressource types for Builder //@ts-expect-error Type union of Ressource types for Builder
const ressource = Builder.fromJSON(value) as T const ressource = Builder.fromJSON(value) as T
if (await filter(ressource)) { if (filter(ressource)) {
yield ressource yield ressource
} }
} }
} }
} }
type RessourceType<T extends Ressource> = T extends type RessourceType<T extends Ressource> = T extends Credential ? 'credential'
Credential<CredentialCategory> ? 'credential'
: T extends Group ? 'group' : T extends Group ? 'group'
: T extends Machine ? 'machine' : T extends Machine ? 'machine'
: T extends Service ? 'service' : T extends Service ? 'service'
: T extends User ? 'user' : T extends User ? 'user'
: never : never
type RessourceBuilder<T extends Ressource> = T extends type RessourceBuilder<T extends Ressource> = T extends Credential
Credential<CredentialCategory> ? typeof Credential ? typeof Credential
: T extends Group ? typeof Group : T extends Group ? typeof Group
: T extends Machine ? typeof Machine : T extends Machine ? typeof Machine
: T extends Service ? typeof Service : T extends Service ? typeof Service

View file

@ -1,54 +1,27 @@
import type { Base64String, ToJson, UUID } from '@/types.ts' import { ToJson } from '@/types.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
import type { Ref } from '@models'
import { Avatar } from '@/src/models/utils/avatar.ts'
export class Credential<T extends CredentialCategory> extends Ressource { export class Credential extends Ressource {
static fromJSON<T extends CredentialCategory>( static fromJSON(
json: ToJson<Credential<T>>, json: ToJson<Credential>,
): Credential<T> { ): Credential {
return new Credential(json) return new Credential(json)
} }
static create<T extends CredentialCategory>( static create(
{ category, store, name, avatar }: Pick< { category, store, name }: Pick<Credential, 'category' | 'store' | 'name'>,
Credential<T>, ): Credential {
'category' | 'store' | 'name' | 'avatar' const { uuid, createdAt, updatedAt } = super.create({ name })
>, return new Credential({ uuid, createdAt, updatedAt, name, category, store })
): Credential<T> {
const { uuid, createdAt, updatedAt } = super.create({ name, avatar })
return new Credential({
uuid,
createdAt,
updatedAt,
name,
category,
store,
avatar,
})
} }
static load<T extends CredentialCategory>({ #category: 'password' | 'ssh' | 'passkey'
category, #store: CredentialCategory<Credential['category']>
store,
name,
avatar = Avatar.fromEmoji('🔑'),
}: {
name: Credential<T>['name']
category: T
store: Credential<T>['store']
avatar?: Credential<T>['avatar']
}): Credential<T> {
return this.create({ category, store, name, avatar })
}
#category: T
#store: Readonly<CredentialStore<T>>
private constructor( private constructor(
{ category, store, ...props }: { category, store, ...props }:
& Pick<Credential<T>, 'category' | 'store'> & Pick<Credential, 'category' | 'store'>
& Pick<Ressource, 'name' | 'uuid' | 'avatar' | 'createdAt' | 'updatedAt'>, & Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
) { ) {
super(props) super(props)
@ -70,14 +43,14 @@ export class Credential<T extends CredentialCategory> extends Ressource {
} }
get store() { get store() {
return this.#store return Object.freeze(this.#store)
} }
update<T extends CredentialCategory>( update(
props: Partial<Omit<Credential<T>, 'type' | 'uuid' | 'createdAt'>>, props: Partial<Omit<Credential, 'type' | 'uuid' | 'createdAt'>>,
): Credential<T> { ): Credential {
const { updatedAt } = super.update(props) const { updatedAt } = super.update(props)
const credential = new Credential<T>({ ...this, ...props, updatedAt }) const credential = new Credential({ ...this, ...props, updatedAt })
return credential return credential
} }
@ -93,55 +66,40 @@ export class Credential<T extends CredentialCategory> extends Ressource {
toString(): string { toString(): string {
return `Credential (${JSON.stringify(this)})` return `Credential (${JSON.stringify(this)})`
} }
toRef(): Ref<Credential<T>> {
return super.toRef() as Ref<Credential<T>>
}
} }
export interface Credential<T extends CredentialCategory> extends Ressource { export interface Credential extends Ressource {
type: 'credential' type: 'credential'
category: T category: 'password' | 'ssh' | 'passkey'
store: Readonly<CredentialStore<T>> store: Readonly<CredentialCategory<Credential['category']>>
} }
export type CredentialCategory = 'password' | 'ssh' | 'passkey' type CredentialCategory<T extends 'password' | 'ssh' | 'passkey'> = T extends
'password' ? {
export type CredentialStore<T extends CredentialCategory> = T extends 'password'
? {
store: { store: {
hash: Base64String hash: string //hex or b64 of Uint8Array
alg: string alg: string
salt: Base64String salt: string //hex or b64 of Uint8Array
} }
} }
: T extends 'ssh' ? { : T extends 'ssh' ? {
store: { store: {
publicKey: Base64String publicKey: string
} }
} }
: T extends 'passkey' ? { : T extends 'passkey' ? {
store: Passkey store: Record<string, unknown>
} }
: never : never
/** Passkey store */ /*
export type Passkey = { PassKey store:
/** User UUID */ {
user: UUID publicKey: Uint8Array
/** WebAuthn registration key id */ keyId: string
webAuthnUserID: string transport: string
/** Passkey credential unique id */
id: string
/** Passkey user public key */
publicKey: Base64String
/** Number of times the authenticator has been used */
counter: number counter: number
/** Whether the passkey is single-device or multi-device */
deviceType: 'singleDevice' | 'multiDevice'
/** Whether the passkey has been backed up in some way */
backedUp: boolean
/** Passkey physical transport layer */
transports?:
('ble' | 'cable' | 'hybrid' | 'internal' | 'nfc' | 'smart-card' | 'usb')[]
} }
*/
//new Uint8Array(Object.values(JSON.parse(JSON.stringify(new Uint8Array([1, 2, 3])))))

View file

@ -1,68 +1,33 @@
import type { Posix, ToJson, UUID } from '@/types.ts' import { Posix, ToJson, UUID } from '@/types.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
import { Ref } from '@models'
import { Avatar } from '@/src/models/utils/avatar.ts'
export class Group extends Ressource { export class Group extends Ressource {
static fromJSON( static fromJSON(
{ posix, groups, ...json }: ToJson<Group>, { posix, ...json }: ToJson<Group>,
): Group { ): Group {
return new Group({ return new Group({ ...json, posix: posix ?? undefined })
...json,
posix: posix ?? undefined,
groups: groups.map((group) => Ref.fromString<Group>(group)),
})
} }
static create( static create(
{ posix, permissions, name, avatar, groups }: Pick< { posix, permissions, name }: Pick<Group, 'posix' | 'permissions' | 'name'>,
Group,
'posix' | 'permissions' | 'name' | 'avatar' | 'groups'
>,
): Group { ): Group {
const { uuid, createdAt, updatedAt } = super.create({ name, avatar }) const { uuid, createdAt, updatedAt } = super.create({ name })
return new Group({ return new Group({ uuid, createdAt, updatedAt, name, posix, permissions })
uuid,
avatar,
createdAt,
updatedAt,
name,
posix,
permissions,
groups,
})
}
static load({
name,
groups = [],
permissions = {},
posix,
avatar = Avatar.fromEmoji('👥'),
}: {
name: Group['name']
permissions?: Group['permissions']
posix?: Group['posix']
avatar?: Group['avatar']
groups?: Group['groups']
}): Group {
return this.create({ name, groups, permissions, posix, avatar })
} }
#posix?: Posix #posix?: Posix
#permissions: Readonly<{ #permissions: {
[serviceOrMachine: UUID]: { [serviceOrMachine: UUID]: {
read: boolean read: boolean
write: boolean write: boolean
execute: boolean execute: boolean
} }
}> } = {}
#groups: readonly Ref<Group>[]
private constructor( private constructor(
{ posix, permissions, groups, ...props }: { posix, permissions, ...props }:
& Pick<Group, 'posix' | 'permissions' | 'groups'> & Pick<Group, 'posix' | 'permissions'>
& Pick<Ressource, 'name' | 'avatar' | 'uuid' | 'createdAt' | 'updatedAt'>, & Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
) { ) {
super(props) super(props)
@ -85,8 +50,7 @@ export class Group extends Ressource {
this.#posix = posix this.#posix = posix
} }
this.#permissions = Object.freeze(permissions) this.permissions = Object.freeze(permissions)
this.#groups = Object.freeze(groups)
} }
get type(): 'group' { get type(): 'group' {
@ -98,11 +62,7 @@ export class Group extends Ressource {
} }
get permissions() { get permissions() {
return this.#permissions return Object.freeze(this.#permissions)
}
get groups() {
return this.#groups
} }
update(props: Partial<Omit<Group, 'type' | 'uuid' | 'createdAt'>>): Group { update(props: Partial<Omit<Group, 'type' | 'uuid' | 'createdAt'>>): Group {
@ -117,23 +77,17 @@ export class Group extends Ressource {
type: this.type, type: this.type,
posix: this.posix ?? null, posix: this.posix ?? null,
permissions: this.permissions, permissions: this.permissions,
groups: this.groups.map((group) => group.toJSON()),
} as const } as const
} }
toString(): string { toString(): string {
return `Group (${JSON.stringify(this)})` return `Group (${JSON.stringify(this)})`
} }
toRef(): Ref<Group> {
return super.toRef() as Ref<Group>
}
} }
export interface Group extends Ressource { export interface Group extends Ressource {
type: 'group' type: 'group'
posix: Posix | undefined posix: Posix | undefined
groups: readonly Ref<Group>[]
permissions: Readonly<{ permissions: Readonly<{
[serviceOrMachine: UUID]: { [serviceOrMachine: UUID]: {
read: boolean read: boolean

View file

@ -1,58 +1,29 @@
import { Ref } from '@/src/models/utils/ref.ts' import { ToJson } from '@/types.ts'
import type { ToJson, UrlString } from '@/types.ts' import { Group } from '@models/group.ts'
import type { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
import { Avatar } from '@/src/models/utils/avatar.ts' import { Ref } from '@/src/models/utils/ref.ts'
export class Machine extends Ressource { export class Machine extends Ressource {
static fromJSON( static fromJSON(
{ url, ...json }: ToJson<Machine>, json: ToJson<Machine>,
): Machine { ): Machine {
const url = new URL(json.url)
const avatar = new URL(json.avatar)
const groups = json.groups.map((group) => Ref.fromString<Group>(group)) const groups = json.groups.map((group) => Ref.fromString<Group>(group))
return new Machine({ ...json, url, groups }) return new Machine({ ...json, url, avatar, groups })
} }
static create( static create(
{ tags, url, status, groups, avatar }: Pick< { tags, url, status, avatar, groups, ...props }:
Machine, & Pick<Machine, 'tags' | 'url' | 'status' | 'avatar' | 'groups'>
'name' | 'tags' | 'url' | 'status' | 'avatar' | 'groups' & Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
>,
): Machine { ): Machine {
const { uuid, createdAt, updatedAt } = super.create({ name, avatar }) return new Machine({ ...props, tags, url, status, avatar, groups })
return new Machine({
uuid,
createdAt,
updatedAt,
avatar,
name,
tags,
url,
status,
groups,
})
}
static load({
name,
avatar = Avatar.fromEmoji('🖨️'),
tags = [],
url,
status = 'unknown',
groups = [],
}: {
name: Machine['name']
avatar?: Machine['avatar']
tags?: Machine['tags']
url: Machine['url']
status?: Machine['status']
groups?: Machine['groups']
}): Machine {
return this.create({ name, avatar, tags, url, status, groups })
} }
#tags: readonly string[] #tags: readonly string[]
#url: UrlString #url: URL
#status: #status:
| 'ready' | 'ready'
| 'busy' | 'busy'
@ -60,23 +31,25 @@ export class Machine extends Ressource {
| 'discontinued' | 'discontinued'
| 'error' | 'error'
| 'unknown' | 'unknown'
#avatar: URL
#groups: readonly Ref<Group>[] #groups: readonly Ref<Group>[]
private constructor( private constructor(
{ tags, url, status, groups, ...props }: { tags, url, status, avatar, groups, ...props }:
& Pick<Machine, 'tags' | 'url' | 'status' | 'avatar' | 'groups'> & Pick<Machine, 'tags' | 'url' | 'status' | 'avatar' | 'groups'>
& Pick<Ressource, 'name' | 'uuid' | 'avatar' | 'createdAt' | 'updatedAt'>, & Pick<Ressource, 'name' | 'uuid' | 'createdAt' | 'updatedAt'>,
) { ) {
super(props) super(props)
this.#tags = Object.freeze(tags) this.#tags = Object.freeze(tags)
this.#url = new URL(url).href as UrlString this.#url = new URL(url)
if (!['working', 'pending', 'ready', 'unavailable'].includes(status)) { if (!['working', 'pending', 'ready', 'unavailable'].includes(status)) {
throw new TypeError( throw new TypeError(
`status is "${status}" but ('ready' | 'busy' | 'unavailable' | 'discontinued' | 'error' | 'unknown') is required`, `status is "${status}" but ('ready' | 'busy' | 'unavailable' | 'discontinued' | 'error' | 'unknown') is required`,
) )
} }
this.#status = status this.#status = status
this.#avatar = new URL(avatar)
this.#groups = Object.freeze(groups) this.#groups = Object.freeze(groups)
} }
@ -84,14 +57,17 @@ export class Machine extends Ressource {
return 'machine' return 'machine'
} }
get tags() { get tags() {
return this.#tags return this.#tags.slice()
} }
get url() { get url() {
return this.#url return new URL(this.#url)
} }
get status() { get status() {
return this.#status return this.#status
} }
get avatar() {
return new URL(this.#avatar)
}
get groups() { get groups() {
return this.#groups return this.#groups
} }
@ -108,9 +84,10 @@ export class Machine extends Ressource {
return { return {
...super.toJSON(), ...super.toJSON(),
type: this.type, type: this.type,
tags: this.tags, tags: this.tags.slice(),
url: this.url, url: this.url.toJSON(),
status: this.status, status: this.status,
avatar: this.avatar.toJSON(),
groups: Object.freeze(this.groups.map((group) => group.toJSON())), groups: Object.freeze(this.groups.map((group) => group.toJSON())),
} as const } as const
} }
@ -118,16 +95,12 @@ export class Machine extends Ressource {
toString(): string { toString(): string {
return `Machine (${JSON.stringify(this)})` return `Machine (${JSON.stringify(this)})`
} }
toRef(): Ref<Machine> {
return super.toRef() as Ref<Machine>
}
} }
export interface Machine extends Ressource { export interface Machine extends Ressource {
type: 'machine' type: 'machine'
tags: readonly string[] tags: string[]
url: UrlString url: URL
status: status:
| 'ready' | 'ready'
| 'busy' | 'busy'
@ -135,5 +108,6 @@ export interface Machine extends Ressource {
| 'discontinued' | 'discontinued'
| 'error' | 'error'
| 'unknown' | 'unknown'
avatar: URL
groups: readonly Ref<Group>[] groups: readonly Ref<Group>[]
} }

View file

@ -1,5 +1,5 @@
import { Ref } from '@/src/models/utils/ref.ts' import { Ref } from '@/src/models/utils/ref.ts'
import type { DateString, ToJson, UrlString, UUID } from '@/types.ts' import { DateString, ToJson, UUID } from '@/types.ts'
import { regex } from '@/utils.ts' import { regex } from '@/utils.ts'
export class Ressource { export class Ressource {
@ -8,37 +8,29 @@ export class Ressource {
): Ressource { ): Ressource {
return new Ressource({ return new Ressource({
name: json.name, name: json.name,
avatar: json.avatar,
uuid: json.uuid, uuid: json.uuid,
createdAt: json.createdAt, createdAt: json.createdAt,
updatedAt: json.updatedAt, updatedAt: json.updatedAt,
}) })
} }
static create( static create({ name }: Pick<Ressource, 'name'>): Ressource {
{ name, avatar }: Pick<Ressource, 'name' | 'avatar'>,
): Ressource {
const uuid = crypto.randomUUID() as UUID const uuid = crypto.randomUUID() as UUID
const createdAt = new Date().toISOString() as DateString const createdAt = new Date().toISOString() as DateString
const updatedAt = createdAt const updatedAt = createdAt
return new Ressource({ name, avatar, uuid, createdAt, updatedAt }) return new Ressource({ name, uuid, createdAt, updatedAt })
}
static load(_options: never): Ressource {
throw new Error('not implemented')
} }
#name: string #name: string
#uuid: UUID #uuid: UUID
#createdAt: DateString #createdAt: DateString
#updatedAt: DateString #updatedAt: DateString
#avatar: UrlString
protected constructor( protected constructor(
{ name, avatar, uuid, createdAt, updatedAt }: Pick< { name, uuid, createdAt, updatedAt }: Pick<
Ressource, Ressource,
'name' | 'avatar' | 'uuid' | 'createdAt' | 'updatedAt' 'name' | 'uuid' | 'createdAt' | 'updatedAt'
>, >,
) { ) {
this.#name = name this.#name = name
@ -47,7 +39,6 @@ export class Ressource {
`the following uuid "${uuid}" is not a valid UUID V4`, `the following uuid "${uuid}" is not a valid UUID V4`,
) )
} }
this.#avatar = new URL(avatar).href as UrlString
this.#uuid = uuid this.#uuid = uuid
if (!regex.isoDateString.test(createdAt)) { if (!regex.isoDateString.test(createdAt)) {
throw new SyntaxError( throw new SyntaxError(
@ -72,9 +63,6 @@ export class Ressource {
get name(): string { get name(): string {
return this.#name return this.#name
} }
get avatar(): UrlString {
return this.#avatar
}
get createdAt(): DateString { get createdAt(): DateString {
return this.#createdAt return this.#createdAt
} }
@ -95,7 +83,6 @@ export class Ressource {
type: this.type, type: this.type,
uuid: this.uuid, uuid: this.uuid,
name: this.name, name: this.name,
avatar: this.avatar,
createdAt: this.createdAt, createdAt: this.createdAt,
updatedAt: this.updatedAt, updatedAt: this.updatedAt,
} as const } as const
@ -114,7 +101,6 @@ export interface Ressource {
type: 'user' | 'machine' | 'service' | 'group' | 'credential' type: 'user' | 'machine' | 'service' | 'group' | 'credential'
uuid: UUID uuid: UUID
name: string name: string
avatar: UrlString
createdAt: DateString createdAt: DateString
updatedAt: DateString updatedAt: DateString
} }

View file

@ -1,26 +1,27 @@
import { Ref } from '@/src/models/utils/ref.ts' import { ToJson } from '@/types.ts'
import type { ToJson, UrlString } from '@/types.ts' import { Group } from '@models/group.ts'
import type { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
import { Avatar } from '@/src/models/utils/avatar.ts' import { Ref } from '@/src/models/utils/ref.ts'
export class Service extends Ressource { export class Service extends Ressource {
static fromJSON( static fromJSON(
{ groups, ...json }: ToJson<Service>, { groups, url, avatar, ...json }: ToJson<Service>,
): Service { ): Service {
return new Service({ return new Service({
groups: groups.map((group) => Ref.fromString<Group>(group)), groups: groups.map((group) => Ref.fromString<Group>(group)),
url: new URL(url),
avatar: new URL(avatar),
...json, ...json,
}) })
} }
static create( static create(
{ category, tags, url, groups, avatar, name }: Pick< { category, url, groups, avatar, name }: Pick<
Service, Service,
'category' | 'tags' | 'url' | 'groups' | 'avatar' | 'name' 'category' | 'url' | 'groups' | 'avatar' | 'name'
>, >,
): Service { ): Service {
const { uuid, createdAt, updatedAt } = super.create({ name, avatar }) const { uuid, createdAt, updatedAt } = super.create({ name })
return new Service({ return new Service({
uuid, uuid,
createdAt, createdAt,
@ -28,40 +29,21 @@ export class Service extends Ressource {
name, name,
category, category,
url, url,
tags,
groups, groups,
avatar, avatar,
}) })
} }
static load({ #category: 'bin' | 'web' | 'fs' | 'git'
name, #url: URL
avatar = Avatar.fromEmoji('⚙️'),
tags = [],
url,
category,
groups = [],
}: {
name: Service['name']
url: Service['url']
avatar?: Service['avatar']
tags?: Service['tags']
category: Service['category']
groups?: Service['groups']
}): Service {
return this.create({ name, avatar, tags, url, category, groups })
}
#category: 'web' | 'fs' | 'git'
#url: UrlString
#groups: readonly Ref<Group>[] #groups: readonly Ref<Group>[]
#tags: readonly string[] #avatar: URL
private constructor({ private constructor({
category, category,
tags,
url, url,
groups, groups,
avatar,
...ressource ...ressource
}: Pick< }: Pick<
Service, Service,
@ -71,21 +53,20 @@ export class Service extends Ressource {
| 'name' | 'name'
| 'category' | 'category'
| 'url' | 'url'
| 'tags'
| 'groups' | 'groups'
| 'avatar' | 'avatar'
>) { >) {
super(ressource) super(ressource)
if (!['web', 'fs', 'git'].includes(category)) { if (!['bin', 'web', 'fs', 'git'].includes(category)) {
throw new TypeError( throw new TypeError(
`category is "${category}" but ('web' | 'fs' | 'git') is required`, `category is "${category}" but ('bin' | 'web' | 'fs' | 'git') is required`,
) )
} }
this.#category = category this.#category = category
this.#url = new URL(url).href as UrlString this.#url = url
this.#groups = Object.freeze(groups) this.#groups = Object.freeze(groups)
this.#tags = Object.freeze(tags) this.#avatar = avatar
} }
get type(): 'service' { get type(): 'service' {
@ -96,16 +77,16 @@ export class Service extends Ressource {
return this.#category return this.#category
} }
get tags() {
return this.#tags
}
get url() { get url() {
return this.#url return new URL(this.#url)
} }
get groups() { get groups() {
return this.#groups return Object.freeze(this.#groups)
}
get avatar() {
return new URL(this.#avatar)
} }
update( update(
@ -121,25 +102,21 @@ export class Service extends Ressource {
...super.toJSON(), ...super.toJSON(),
type: this.type, type: this.type,
category: this.category, category: this.category,
tags: this.tags, url: this.url.toJSON(),
url: this.url,
groups: this.groups.map((group) => group.toJSON()), groups: this.groups.map((group) => group.toJSON()),
avatar: this.avatar.toJSON(),
} as const } as const
} }
toString(): string { toString(): string {
return `Service (${JSON.stringify(this)})` return `Service (${JSON.stringify(this)})`
} }
toRef(): Ref<Service> {
return super.toRef() as Ref<Service>
}
} }
export interface Service extends Ressource { export interface Service extends Ressource {
type: 'service' type: 'service'
category: 'web' | 'fs' | 'git' category: 'bin' | 'web' | 'fs' | 'git'
tags: readonly string[] url: URL
url: UrlString
groups: readonly Ref<Group>[] groups: readonly Ref<Group>[]
avatar: URL
} }

View file

@ -1,13 +1,14 @@
import { Ref } from '@/src/models/utils/ref.ts' import { Login, MailAddress, Posix, ToJson } from '@/types.ts'
import type { Login, MailAddress, Posix, ToJson } from '@/types.ts'
import { regex, toLogin } from '@/utils.ts' import { regex, toLogin } from '@/utils.ts'
import type { Credential, CredentialCategory } from '@models/credential.ts' import { Credential } from '@models/credential.ts'
import type { Group } from '@models/group.ts' import { Group } from '@models/group.ts'
import { Ressource } from '@models/ressource.ts' import { Ressource } from '@models/ressource.ts'
import { Avatar } from '@/src/models/utils/avatar.ts' import { Ref } from '@/src/models/utils/ref.ts'
export class User extends Ressource { export class User extends Ressource {
static fromJSON(json: ToJson<User>): User { static fromJSON(
json: ToJson<User>,
): User {
if (json.type !== 'user') { if (json.type !== 'user') {
throw new TypeError(`type is "${json.type}" but "user" is required`) throw new TypeError(`type is "${json.type}" but "user" is required`)
} }
@ -21,10 +22,7 @@ export class User extends Ressource {
`firstname type is "${typeof json.firstname}" but "string" is required`, `firstname type is "${typeof json.firstname}" but "string" is required`,
) )
} }
if ( if (typeof json.login !== 'string' && /(\w|_)+\.(\w|_)+/.test(json.login)) {
typeof json.login !== 'string' &&
/(\w|_)+\.(\w|_)+/.test(json.login)
) {
throw new TypeError( throw new TypeError(
`login is "${json.login}" but "\${string}.\${string}" is required`, `login is "${json.login}" but "\${string}.\${string}" is required`,
) )
@ -37,20 +35,20 @@ export class User extends Ressource {
const credentials = Object.freeze( const credentials = Object.freeze(
json.credentials.map((credential) => json.credentials.map((credential) =>
Ref.fromString<Credential<CredentialCategory>>(credential) Ref.fromString<Credential>(credential)
), ),
) )
const groups = Object.freeze( const groups = Object.freeze(
json.groups.map((group) => Ref.fromString<Group>(group)), json.groups.map((group) => Ref.fromString<Group>(group)),
) )
const avatar = new URL(json.avatar)
const posix = json.posix ?? undefined const posix = json.posix ?? undefined
return new User({ ...json, posix, credentials, groups }) return new User({ ...json, posix, credentials, groups, avatar })
} }
static create({ static create({
name, name,
avatar,
...props ...props
}: Pick< }: Pick<
User, User,
@ -63,38 +61,8 @@ export class User extends Ressource {
| 'avatar' | 'avatar'
| 'credentials' | 'credentials'
>): User { >): User {
const { uuid, createdAt, updatedAt } = super.create({ name, avatar }) const { uuid, createdAt, updatedAt } = super.create({ name })
return new User({ name, avatar, uuid, createdAt, updatedAt, ...props }) return new User({ name, uuid, createdAt, updatedAt, ...props })
}
static load({
lastname,
firstname,
mail,
groups = [],
posix,
avatar = Avatar.fromEmoji('👤'),
credentials = [],
}: {
lastname: User['lastname']
firstname: User['firstname']
mail: User['mail']
groups?: User['groups']
posix?: User['posix']
avatar?: User['avatar']
credentials?: User['credentials']
}): User {
const name = `${firstname} ${lastname}`
return this.create({
name,
lastname,
firstname,
mail,
groups,
posix,
avatar,
credentials,
})
} }
#lastname: string #lastname: string
@ -103,7 +71,8 @@ export class User extends Ressource {
#mail: MailAddress #mail: MailAddress
#groups: readonly Ref<Group>[] #groups: readonly Ref<Group>[]
#posix?: Posix #posix?: Posix
#credentials: readonly Ref<Credential<CredentialCategory>>[] #avatar: URL
#credentials: readonly Ref<Credential>[]
private constructor({ private constructor({
lastname, lastname,
@ -111,6 +80,7 @@ export class User extends Ressource {
mail, mail,
groups, groups,
posix, posix,
avatar,
credentials, credentials,
...ressource ...ressource
}: Pick< }: Pick<
@ -143,6 +113,7 @@ export class User extends Ressource {
} }
} }
this.#posix = posix this.#posix = posix
this.#avatar = avatar
this.#login = toLogin({ firstname, lastname }) this.#login = toLogin({ firstname, lastname })
this.#credentials = Object.freeze(credentials) this.#credentials = Object.freeze(credentials)
} }
@ -168,6 +139,9 @@ export class User extends Ressource {
get posix() { get posix() {
return this.#posix return this.#posix
} }
get avatar() {
return this.#avatar
}
get credentials() { get credentials() {
return this.#credentials return this.#credentials
} }
@ -187,7 +161,7 @@ export class User extends Ressource {
mail: this.mail, mail: this.mail,
groups: this.groups.map((group) => group.toJSON()), groups: this.groups.map((group) => group.toJSON()),
posix: this.posix ?? null, posix: this.posix ?? null,
avatar: this.avatar, avatar: this.avatar.toJSON(),
credentials: this.credentials.map((credential) => credential.toJSON()), credentials: this.credentials.map((credential) => credential.toJSON()),
} as const } as const
} }
@ -195,10 +169,6 @@ export class User extends Ressource {
toString(): string { toString(): string {
return `User (${JSON.stringify(this)})` return `User (${JSON.stringify(this)})`
} }
toRef(): Ref<User> {
return super.toRef() as Ref<User>
}
} }
export interface User extends Ressource { export interface User extends Ressource {
@ -209,5 +179,6 @@ export interface User extends Ressource {
mail: MailAddress mail: MailAddress
groups: readonly Ref<Group>[] groups: readonly Ref<Group>[]
posix: Posix | undefined posix: Posix | undefined
credentials: readonly Ref<Credential<CredentialCategory>>[] avatar: URL
credentials: readonly Ref<Credential>[]
} }

View file

@ -1,8 +0,0 @@
import type { UrlString } from '@/types.ts'
export class Avatar {
static fromEmoji(emoji: string): UrlString {
return `data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%2210 0 100 100%22><text y=%22.90em%22 font-size=%2290%22>${emoji}</text></svg>`
}
private constructor() {}
}

View file

@ -1,4 +1,4 @@
import type { Db } from '@/mod.ts' import { Db } from '@/mod.ts'
import type { UUID } from '@/types.ts' import type { UUID } from '@/types.ts'
import { import {
Credential, Credential,
@ -38,9 +38,8 @@ export class Ref<T extends Ressource> extends String {
} }
static dbResolver(db: Db) { static dbResolver(db: Db) {
return <T extends Ressource>(ref: RefString<T>): Promise<T> => { return <T extends Ressource>(ref: RefString<T>) => {
const { type, uuid } = Ref.parse(ref) const { type, uuid } = Ref.parse(ref)
//@ts-expect-error force type casting to fix
return db.ressource[type].get({ uuid }) return db.ressource[type].get({ uuid })
} }
} }

View file

@ -26,11 +26,3 @@ export type Posix = {
home: string home: string
shell: string shell: string
} }
export type Base64String = `${string}`
export type UrlString = `${
| 'data:'
| 'file://'
| 'http://'
| 'https://'
| 'git:'}${string}`

View file

@ -1,4 +1,4 @@
import type { Login } from '@/types.ts' import { Login } from '@/types.ts'
export function toLogin({ export function toLogin({
firstname, firstname,