Remove dvm package
This commit is contained in:
@@ -21,7 +21,6 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/throttle-debounce": "^5.0.2",
|
||||
"@welshman/dvm": "workspace:*",
|
||||
"@welshman/feeds": "workspace:*",
|
||||
"@welshman/lib": "workspace:*",
|
||||
"@welshman/relay": "workspace:*",
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"paths": {
|
||||
"@welshman/dvm": ["../dvm/src/index.js"],
|
||||
"@welshman/feeds": ["../feeds/src/index.js"],
|
||||
"@welshman/lib": ["../lib/src/index.js"],
|
||||
"@welshman/relay": ["../relay/src/index.js"],
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
build
|
||||
normalize-url
|
||||
__tests__
|
||||
@@ -1,33 +0,0 @@
|
||||
{
|
||||
"name": "@welshman/dvm",
|
||||
"version": "0.3.4",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of utilities for building nostr DVMs.",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"type": "module",
|
||||
"main": "dist/dvm/src/index.js",
|
||||
"types": "dist/dvm/src/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "pnpm run clean && pnpm run compile --force",
|
||||
"clean": "rimraf ./dist",
|
||||
"compile": "tsc -b tsconfig.build.json",
|
||||
"prepublishOnly": "pnpm run build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@noble/hashes": "^1.6.1",
|
||||
"@welshman/lib": "workspace:*",
|
||||
"@welshman/net": "workspace:*",
|
||||
"@welshman/util": "workspace:*",
|
||||
"@welshman/signer": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "~6.0.0",
|
||||
"typescript": "~5.8.0"
|
||||
}
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
import {now} from "@welshman/lib"
|
||||
import {Nip01Signer} from "@welshman/signer"
|
||||
import {TrustedEvent, StampedEvent, Filter} from "@welshman/util"
|
||||
import {request, publish, AdapterContext} from "@welshman/net"
|
||||
|
||||
export type DVMHandler = {
|
||||
stop?: () => void
|
||||
handleEvent: (e: TrustedEvent) => AsyncGenerator<StampedEvent>
|
||||
}
|
||||
|
||||
export type CreateDVMHandler = (dvm: DVM) => DVMHandler
|
||||
|
||||
export type DVMOpts = {
|
||||
sk: string
|
||||
relays: string[]
|
||||
handlers: Record<string, CreateDVMHandler>
|
||||
expireAfter?: number
|
||||
requireMention?: boolean
|
||||
context?: AdapterContext
|
||||
}
|
||||
|
||||
export class DVM {
|
||||
active = false
|
||||
logEvents = false
|
||||
seen = new Set()
|
||||
handlers = new Map()
|
||||
signer: Nip01Signer
|
||||
|
||||
constructor(readonly opts: DVMOpts) {
|
||||
this.signer = new Nip01Signer(opts.sk)
|
||||
|
||||
for (const [kind, createHandler] of Object.entries(this.opts.handlers)) {
|
||||
this.handlers.set(parseInt(kind), createHandler(this))
|
||||
}
|
||||
}
|
||||
|
||||
async start() {
|
||||
this.active = true
|
||||
|
||||
const {relays, context, requireMention = false} = this.opts
|
||||
const pubkey = await this.signer.getPubkey()
|
||||
|
||||
while (this.active) {
|
||||
await new Promise<void>(resolve => {
|
||||
const since = now()
|
||||
const kinds = Array.from(this.handlers.keys())
|
||||
const filter: Filter = {kinds, since}
|
||||
|
||||
if (requireMention) {
|
||||
filter["#p"] = [pubkey]
|
||||
}
|
||||
|
||||
request({
|
||||
relays,
|
||||
filters: [filter],
|
||||
context,
|
||||
onClose: resolve,
|
||||
onEvent: this.onEvent,
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
stop() {
|
||||
for (const handler of this.handlers.values()) {
|
||||
handler.stop?.()
|
||||
}
|
||||
|
||||
this.active = false
|
||||
}
|
||||
|
||||
async onEvent(request: TrustedEvent) {
|
||||
const {expireAfter = 60 * 60} = this.opts
|
||||
|
||||
if (this.seen.has(request.id)) {
|
||||
return
|
||||
}
|
||||
|
||||
const handler = this.handlers.get(request.kind)
|
||||
|
||||
if (!handler) {
|
||||
return
|
||||
}
|
||||
|
||||
this.seen.add(request.id)
|
||||
|
||||
if (this.logEvents) {
|
||||
console.info("Handling request", request)
|
||||
}
|
||||
|
||||
for await (const event of handler.handleEvent(request)) {
|
||||
if (event.kind !== 7000) {
|
||||
event.tags.push(["request", JSON.stringify(request)])
|
||||
|
||||
const inputTag = request.tags.find((t: string[]) => t[0] === "i")
|
||||
|
||||
if (inputTag) {
|
||||
event.tags.push(inputTag)
|
||||
}
|
||||
}
|
||||
|
||||
event.tags.push(["p", request.pubkey])
|
||||
event.tags.push(["e", request.id])
|
||||
|
||||
if (expireAfter) {
|
||||
event.tags.push(["expiration", String(now() + expireAfter)])
|
||||
}
|
||||
|
||||
if (this.logEvents) {
|
||||
console.info("Publishing event", event)
|
||||
}
|
||||
|
||||
this.publish(event)
|
||||
}
|
||||
}
|
||||
|
||||
async publish(template: StampedEvent) {
|
||||
const {relays, context} = this.opts
|
||||
const event = await this.signer.sign(template)
|
||||
|
||||
await publish({event, relays, context})
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from "./handler.js"
|
||||
export * from "./request.js"
|
||||
@@ -1,43 +0,0 @@
|
||||
import {now} from "@welshman/lib"
|
||||
import {TrustedEvent, SignedEvent, Filter} from "@welshman/util"
|
||||
import {request, publish, AdapterContext} from "@welshman/net"
|
||||
|
||||
export type DVMRequestOptions = {
|
||||
event: SignedEvent
|
||||
relays: string[]
|
||||
timeout?: number
|
||||
autoClose?: boolean
|
||||
context?: AdapterContext
|
||||
onResult?: (event: TrustedEvent, url: string) => void
|
||||
onProgress?: (event: TrustedEvent, url: string) => void
|
||||
}
|
||||
|
||||
export const requestDvmResponse = (options: DVMRequestOptions) => {
|
||||
const {event, relays, context, timeout = 30_000, autoClose = true, onResult, onProgress} = options
|
||||
const kind = event.kind + 1000
|
||||
const kinds = onProgress ? [kind, 7000] : [kind]
|
||||
const filters: Filter[] = [{kinds, since: now() - 60, "#e": [event.id]}]
|
||||
const abortController = new AbortController()
|
||||
const signal = AbortSignal.any([abortController.signal, AbortSignal.timeout(timeout)])
|
||||
|
||||
return request({
|
||||
signal,
|
||||
relays,
|
||||
filters,
|
||||
context,
|
||||
onEvent: (event: TrustedEvent, url: string) => {
|
||||
if (event.kind === 7000) {
|
||||
onProgress?.(event, url)
|
||||
} else {
|
||||
onResult?.(event, url)
|
||||
|
||||
if (autoClose) {
|
||||
abortController.abort()
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const makeDvmRequest = (options: DVMRequestOptions) =>
|
||||
Promise.all([publish(options), requestDvmResponse(options)])
|
||||
@@ -1,18 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.build.json",
|
||||
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"paths": {
|
||||
"@welshman/lib": ["../lib/src/index.js"],
|
||||
"@welshman/signer": ["../signer/src/index.js"],
|
||||
"@welshman/util": ["../util/src/index.js"],
|
||||
"@welshman/relay": ["../relay/src/index.js"],
|
||||
"@welshman/net": ["../net/src/index.js"]
|
||||
}
|
||||
},
|
||||
|
||||
"include": [
|
||||
"src/**/*"
|
||||
]
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json"
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"entryPoints": ["src/index.ts"]
|
||||
}
|
||||
@@ -21,7 +21,6 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@welshman/lib": "workspace:*",
|
||||
"@welshman/dvm": "workspace:*",
|
||||
"@welshman/net": "workspace:*",
|
||||
"@welshman/relay": "workspace:*",
|
||||
"@welshman/router": "workspace:*",
|
||||
|
||||
@@ -12,8 +12,7 @@ import {
|
||||
import {Nip01Signer, ISigner} from "@welshman/signer"
|
||||
import {LOCAL_RELAY_URL} from "@welshman/relay"
|
||||
import {Router, getFilterSelections, addMinimalFallbacks} from "@welshman/router"
|
||||
import {Tracker, AdapterContext, request} from "@welshman/net"
|
||||
import {makeDvmRequest} from "@welshman/dvm"
|
||||
import {Tracker, AdapterContext, request, publish} from "@welshman/net"
|
||||
|
||||
export type RequestPageOptions = {
|
||||
filters: Filter[]
|
||||
@@ -136,6 +135,10 @@ export const requestDVM = async ({
|
||||
}
|
||||
|
||||
const event = await signer.sign(makeEvent(kind, {tags}))
|
||||
const filters = [{kinds: [event.kind + 1000], since: now() - 60, "#e": [event.id]}]
|
||||
|
||||
await makeDvmRequest({relays, event, context, onResult})
|
||||
return Promise.all([
|
||||
publish({event, relays, context}),
|
||||
request({filters, relays, context, autoClose: true, onEvent: onResult}),
|
||||
])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user