Add domain package
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
build
|
||||
normalize-url
|
||||
@@ -0,0 +1,3 @@
|
||||
# @welshman/domain [](https://npmjs.com/package/@welshman/domain)
|
||||
|
||||
A collection of utilities for mapping nostr events to useful data structures.
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"name": "@welshman/domain",
|
||||
"version": "0.0.1",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of utilities for mapping nostr events to useful data structures.",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"type": "module",
|
||||
"files": [
|
||||
"build"
|
||||
],
|
||||
"types": "./build/src/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./build/src/index.d.ts",
|
||||
"import": "./build/src/index.mjs",
|
||||
"require": "./build/src/index.cjs"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"pub": "npm run lint && npm run build && npm publish",
|
||||
"build": "gts clean && tsc-multi",
|
||||
"lint": "gts lint",
|
||||
"fix": "gts fix"
|
||||
},
|
||||
"devDependencies": {
|
||||
"gts": "^5.0.1",
|
||||
"tsc-multi": "^1.1.0",
|
||||
"typescript": "~5.1.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"nostr-tools": "^2.3.2",
|
||||
"@welshman/lib": "0.0.14",
|
||||
"@welshman/util": "0.0.25"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import {last} from '@welshman/lib'
|
||||
|
||||
export type Handle = {
|
||||
pubkey: string
|
||||
nip05: string
|
||||
nip46: string[]
|
||||
relays: string[]
|
||||
}
|
||||
|
||||
export const displayHandle = (handle: Handle) =>
|
||||
handle.nip05.startsWith("_@") ? last(handle.nip05.split("@")) : handle.nip05
|
||||
@@ -0,0 +1,50 @@
|
||||
import {fromPairs, parseJson} from "@welshman/lib"
|
||||
import {getAddress, Tags} from "@welshman/util"
|
||||
import type {TrustedEvent} from "@welshman/util"
|
||||
|
||||
export type Handler<E extends TrustedEvent> = {
|
||||
kind: number
|
||||
name: string
|
||||
about: string
|
||||
image: string
|
||||
identifier: string
|
||||
event: E
|
||||
website?: string
|
||||
lud16?: string
|
||||
nip05?: string
|
||||
}
|
||||
|
||||
export const readHandlers = <E extends TrustedEvent>(event: E) => {
|
||||
const {d: identifier} = fromPairs(event.tags)
|
||||
const meta = parseJson(event.content)
|
||||
const normalizedMeta = {
|
||||
name: meta?.name || meta?.display_name || "",
|
||||
image: meta?.image || meta?.picture || "",
|
||||
about: meta?.about || "",
|
||||
website: meta?.website || "",
|
||||
lud16: meta?.lud16 || "",
|
||||
nip05: meta?.nip05 || "",
|
||||
}
|
||||
|
||||
// If our meta is missing important stuff, don't bother showing it
|
||||
if (!normalizedMeta.name || !normalizedMeta.image) {
|
||||
return []
|
||||
}
|
||||
|
||||
return Tags.fromEvent(event)
|
||||
.whereKey("k")
|
||||
.values()
|
||||
.valueOf()
|
||||
.map(kind => ({...normalizedMeta, kind: parseInt(kind), identifier, event})) as Handler<E>[]
|
||||
}
|
||||
|
||||
export const getHandlerKey = <E extends TrustedEvent>(handler: Handler<E>) => `${handler.kind}:${getAddress(handler.event)}`
|
||||
|
||||
export const displayHandler = <E extends TrustedEvent>(handler?: Handler<E>, fallback = "") => handler?.name || fallback
|
||||
|
||||
export const getHandlerAddress = <E extends TrustedEvent>(event: E) => {
|
||||
const tags = Tags.fromEvent(event).whereKey("a")
|
||||
const tag = tags.filter(t => t.last() === "web").first() || tags.first()
|
||||
|
||||
return tag?.value()
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export * from "./handle"
|
||||
export * from "./handler"
|
||||
export * from "./list"
|
||||
export * from "./profile"
|
||||
export * from "./relay"
|
||||
export * from "./util"
|
||||
@@ -0,0 +1,45 @@
|
||||
import {parseJson} from "@welshman/lib"
|
||||
import {Address, isShareableRelayUrl, TrustedEvent} from "@welshman/util"
|
||||
import {Encryptable, DecryptedEvent} from "./util"
|
||||
|
||||
export type ListParams = {
|
||||
kind: number
|
||||
}
|
||||
|
||||
export type List<E extends TrustedEvent> = ListParams & {
|
||||
publicTags: string[][]
|
||||
privateTags: string[][]
|
||||
event?: DecryptedEvent<E>
|
||||
}
|
||||
|
||||
export type PublishedList<E extends TrustedEvent> = Omit<List<E>, "event"> & {
|
||||
event: DecryptedEvent<E>
|
||||
}
|
||||
|
||||
export const makeList = <E extends TrustedEvent>(list: ListParams & Partial<List<E>>): List<E> =>
|
||||
({publicTags: [], privateTags: [], ...list})
|
||||
|
||||
const isValidTag = (tag: string[]) => {
|
||||
if (tag[0] === "p") return tag[1]?.length === 64
|
||||
if (tag[0] === "e") return tag[1]?.length === 64
|
||||
if (tag[0] === "a") return Address.isAddress(tag[1] || "")
|
||||
if (tag[0] === "t") return tag[1]?.length > 0
|
||||
if (tag[0] === "r") return isShareableRelayUrl(tag[1])
|
||||
if (tag[0] === "relay") return isShareableRelayUrl(tag[1])
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
export const readList = <E extends TrustedEvent>(event: DecryptedEvent<E>): PublishedList<E> => {
|
||||
const getTags = (tags: string[][]) => (Array.isArray(tags) ? tags.filter(isValidTag) : [])
|
||||
const privateTags = getTags(parseJson(event.plaintext?.content))
|
||||
const publicTags = getTags(event.tags)
|
||||
|
||||
return {event, kind: event.kind, publicTags, privateTags}
|
||||
}
|
||||
|
||||
export const createList = <E extends TrustedEvent>({kind, publicTags = [], privateTags = []}: List<E>) =>
|
||||
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
|
||||
|
||||
export const editList = <E extends TrustedEvent>({kind, publicTags = [], privateTags = []}: PublishedList<E>) =>
|
||||
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
|
||||
@@ -0,0 +1,71 @@
|
||||
import {nip19} from "nostr-tools"
|
||||
import {ellipsize, parseJson} from "@welshman/lib"
|
||||
import {PROFILE, TrustedEvent} from "@welshman/util"
|
||||
|
||||
export type Profile<E extends TrustedEvent> = {
|
||||
name?: string
|
||||
nip05?: string
|
||||
lud06?: string
|
||||
lud16?: string
|
||||
about?: string
|
||||
banner?: string
|
||||
picture?: string
|
||||
website?: string
|
||||
display_name?: string
|
||||
event?: E
|
||||
}
|
||||
|
||||
export type PublishedProfile<E extends TrustedEvent> = Omit<Profile<E>, "event"> & {
|
||||
event: E
|
||||
}
|
||||
|
||||
export const isPublishedProfile = <E extends TrustedEvent>(profile: Profile<E>): profile is PublishedProfile<E> =>
|
||||
Boolean(profile.event)
|
||||
|
||||
export const makeProfile = <E extends TrustedEvent>(profile: Partial<Profile<E>> = {}): Profile<E> => ({
|
||||
name: "",
|
||||
nip05: "",
|
||||
lud06: "",
|
||||
lud16: "",
|
||||
about: "",
|
||||
banner: "",
|
||||
picture: "",
|
||||
website: "",
|
||||
display_name: "",
|
||||
...profile,
|
||||
})
|
||||
|
||||
export const readProfile = <E extends TrustedEvent>(event: E) => {
|
||||
const profile = parseJson(event.content) || {}
|
||||
|
||||
return {...profile, event} as PublishedProfile<E>
|
||||
}
|
||||
|
||||
export const createProfile = <E extends TrustedEvent>({event, ...profile}: Profile<E>) => ({
|
||||
kind: PROFILE,
|
||||
content: JSON.stringify(profile),
|
||||
})
|
||||
|
||||
export const editProfile = <E extends TrustedEvent>({event, ...profile}: PublishedProfile<E>) => ({
|
||||
kind: PROFILE,
|
||||
content: JSON.stringify(profile),
|
||||
tags: event.tags,
|
||||
})
|
||||
|
||||
export const displayPubkey = (pubkey: string) => {
|
||||
const d = nip19.npubEncode(pubkey)
|
||||
|
||||
return d.slice(0, 8) + "…" + d.slice(-5)
|
||||
}
|
||||
|
||||
export const displayProfile = <E extends TrustedEvent>(profile?: Profile<E>, fallback = "") => {
|
||||
const {display_name, name, event} = profile || {}
|
||||
|
||||
if (name) return ellipsize(name, 60)
|
||||
if (display_name) return ellipsize(display_name, 60)
|
||||
if (event) return displayPubkey(event.pubkey)
|
||||
|
||||
return fallback
|
||||
}
|
||||
|
||||
export const profileHasName = <E extends TrustedEvent>(profile?: Profile<E>) => Boolean(profile?.name || profile?.display_name)
|
||||
@@ -0,0 +1,63 @@
|
||||
import {last} from "@welshman/lib"
|
||||
import {LOCAL_RELAY_URL, normalizeRelayUrl as _normalizeRelayUrl} from "@welshman/util"
|
||||
|
||||
// Utils related to bare urls
|
||||
|
||||
export function normalizeRelayUrl(url: string, opts = {}) {
|
||||
if (url === LOCAL_RELAY_URL) {
|
||||
return url
|
||||
}
|
||||
|
||||
try {
|
||||
return _normalizeRelayUrl(url, opts)
|
||||
} catch (e) {
|
||||
return url
|
||||
}
|
||||
}
|
||||
|
||||
export const displayRelayUrl = (url: string) => last(url.split("://")).replace(/\/$/, "")
|
||||
|
||||
// Relay profiles
|
||||
|
||||
export type RelayProfile = {
|
||||
url: string
|
||||
name?: string
|
||||
contact?: string
|
||||
description?: string
|
||||
supported_nips?: number[]
|
||||
limitation?: {
|
||||
payment_required?: boolean
|
||||
auth_required?: boolean
|
||||
}
|
||||
}
|
||||
|
||||
export const makeRelayProfile = (relayProfile: RelayProfile) => relayProfile
|
||||
|
||||
export const filterRelaysByNip = (nip: number, relays: RelayProfile[]) =>
|
||||
relays.filter(r => r.supported_nips?.includes(nip))
|
||||
|
||||
// Relay policies
|
||||
|
||||
export enum RelayMode {
|
||||
Read = "read",
|
||||
Write = "write",
|
||||
Inbox = "inbox",
|
||||
}
|
||||
|
||||
export type RelayPolicy = {
|
||||
url: string
|
||||
read: boolean
|
||||
write: boolean
|
||||
inbox: boolean
|
||||
}
|
||||
|
||||
export const makeRelayPolicy = ({
|
||||
url,
|
||||
...relayPolicy
|
||||
}: Partial<RelayPolicy> & {url: string}): RelayPolicy => ({
|
||||
url: normalizeRelayUrl(url),
|
||||
read: false,
|
||||
write: false,
|
||||
inbox: false,
|
||||
...relayPolicy,
|
||||
})
|
||||
@@ -0,0 +1,48 @@
|
||||
import type {TrustedEvent} from "@welshman/util"
|
||||
|
||||
export type Encrypt = (x: string) => Promise<string>
|
||||
|
||||
export type EventContent = {
|
||||
content?: string
|
||||
tags?: string[][]
|
||||
}
|
||||
|
||||
export type DecryptedEvent<E extends TrustedEvent> = E & {
|
||||
plaintext: EventContent
|
||||
}
|
||||
|
||||
export const asDecryptedEvent = <E extends TrustedEvent>(event: E, plaintext: EventContent) =>
|
||||
({...event, plaintext}) as DecryptedEvent<E>
|
||||
|
||||
export class Encryptable<E extends TrustedEvent> {
|
||||
constructor(readonly event: Partial<E>, readonly updates: EventContent) {}
|
||||
|
||||
async reconcile(encrypt: Encrypt) {
|
||||
const encryptContent = () => {
|
||||
if (!this.updates.content) return null
|
||||
|
||||
return encrypt(this.updates.content)
|
||||
}
|
||||
|
||||
const encryptTags = () => {
|
||||
if (!this.updates.tags) return null
|
||||
|
||||
return Promise.all(
|
||||
this.updates.tags.map(async tag => {
|
||||
tag[1] = await encrypt(tag[1])
|
||||
|
||||
return tag
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
const [content, tags] = await Promise.all([encryptContent(), encryptTags()])
|
||||
|
||||
// Updates are optional. If not provided, fall back to the event's content and tags.
|
||||
return {
|
||||
...this.event,
|
||||
tags: tags || this.event.tags,
|
||||
content: content || this.event.content,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"targets": [
|
||||
{"extname": ".cjs", "module": "commonjs"},
|
||||
{"extname": ".mjs", "module": "esnext", "moduleResolution": "node"}
|
||||
],
|
||||
"projects": ["tsconfig.json"]
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"extends": "../../node_modules/gts/tsconfig-google.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": ".",
|
||||
"outDir": "build",
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"lib": ["esnext", "dom", "dom.iterable"]
|
||||
},
|
||||
"include": ["**/*.ts"]
|
||||
}
|
||||
@@ -121,6 +121,16 @@ export const toggle = <T>(x: T, xs: T[]) => xs.includes(x) ? remove(x, xs) : app
|
||||
|
||||
export const clamp = ([min, max]: [number, number], n: number) => Math.min(max, Math.max(min, n))
|
||||
|
||||
export const parseJson = (json: string | Nil) => {
|
||||
if (!json) return null
|
||||
|
||||
try {
|
||||
return JSON.parse(json)
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export const tryCatch = <T>(f: () => T, onError?: (e: Error) => void): T | undefined => {
|
||||
try {
|
||||
const r = f()
|
||||
@@ -137,6 +147,18 @@ export const tryCatch = <T>(f: () => T, onError?: (e: Error) => void): T | undef
|
||||
return undefined
|
||||
}
|
||||
|
||||
export const ellipsize = (s: string, l: number, suffix = '...') => {
|
||||
if (s.length < l * 1.1) {
|
||||
return s
|
||||
}
|
||||
|
||||
while (s.length > l && s.includes(' ')) {
|
||||
s = s.split(' ').slice(0, -1).join(' ')
|
||||
}
|
||||
|
||||
return s + suffix
|
||||
}
|
||||
|
||||
export const isPojo = (obj: any) => {
|
||||
const hasOwnProperty = Object.prototype.hasOwnProperty
|
||||
const toString = Object.prototype.toString
|
||||
|
||||
@@ -102,7 +102,7 @@ export const createEventStore = <E extends TrustedEvent>(repository: Repository<
|
||||
}
|
||||
}
|
||||
|
||||
export const deriveEventsMapped = <T, E extends TrustedEvent>({
|
||||
export const deriveEventsMapped = <E extends TrustedEvent, T>({
|
||||
filters,
|
||||
repository,
|
||||
eventToItem,
|
||||
|
||||
Reference in New Issue
Block a user