liquidty provider w nostr transport

This commit is contained in:
boufni95 2024-05-21 21:19:37 +02:00
parent b6259a64a9
commit 779d3204c3
21 changed files with 4870 additions and 4533 deletions

View file

@ -16,9 +16,9 @@ export type NostrOptions = {
logger?: Logger
throwErrors?: true
metricsCallback: (metrics: Types.RequestMetric[]) => void
NostrUserAuthGuard: (appId?:string, identifier?: string) => Promise<Types.UserContext>
NostrUserAuthGuard: (appId?: string, identifier?: string) => Promise<Types.UserContext>
}
const logErrorAndReturnResponse = (error: Error, response: string, res: NostrResponse, logger: Logger, metric: Types.RequestMetric, metricsCallback: (metrics: Types.RequestMetric[]) => void) => {
const logErrorAndReturnResponse = (error: Error, response: string, res: NostrResponse, logger: Logger, metric: Types.RequestMetric, metricsCallback: (metrics: Types.RequestMetric[]) => void) => {
logger.error(error.message || error); metricsCallback([{ ...metric, error: response }]); res({ status: 'ERROR', reason: response })
}
export default (methods: Types.ServerMethods, opts: NostrOptions) => {
@ -39,11 +39,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.LinkNPubThroughTokenRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
await methods.LinkNPubThroughToken({rpcName:'LinkNPubThroughToken', ctx:authContext , req: request})
await methods.LinkNPubThroughToken({ rpcName: 'LinkNPubThroughToken', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK'})
res({ status: 'OK' })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'UserHealth':
try {
@ -52,11 +52,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
await methods.UserHealth({rpcName:'UserHealth', ctx:authContext })
await methods.UserHealth({ rpcName: 'UserHealth', ctx: authContext })
stats.handle = process.hrtime.bigint()
res({status: 'OK'})
res({ status: 'OK' })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetUserInfo':
try {
@ -65,11 +65,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
const response = await methods.GetUserInfo({rpcName:'GetUserInfo', ctx:authContext })
const response = await methods.GetUserInfo({ rpcName: 'GetUserInfo', ctx: authContext })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'AddProduct':
try {
@ -81,11 +81,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.AddProductRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.AddProduct({rpcName:'AddProduct', ctx:authContext , req: request})
const response = await methods.AddProduct({ rpcName: 'AddProduct', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'NewProductInvoice':
try {
@ -94,11 +94,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
const response = await methods.NewProductInvoice({rpcName:'NewProductInvoice', ctx:authContext ,query: req.query||{}})
const response = await methods.NewProductInvoice({ rpcName: 'NewProductInvoice', ctx: authContext, query: req.query || {} })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetUserOperations':
try {
@ -110,11 +110,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.GetUserOperationsRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.GetUserOperations({rpcName:'GetUserOperations', ctx:authContext , req: request})
const response = await methods.GetUserOperations({ rpcName: 'GetUserOperations', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'NewAddress':
try {
@ -126,11 +126,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.NewAddressRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.NewAddress({rpcName:'NewAddress', ctx:authContext , req: request})
const response = await methods.NewAddress({ rpcName: 'NewAddress', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'PayAddress':
try {
@ -142,11 +142,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.PayAddressRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.PayAddress({rpcName:'PayAddress', ctx:authContext , req: request})
const response = await methods.PayAddress({ rpcName: 'PayAddress', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'NewInvoice':
try {
@ -158,11 +158,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.NewInvoiceRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.NewInvoice({rpcName:'NewInvoice', ctx:authContext , req: request})
const response = await methods.NewInvoice({ rpcName: 'NewInvoice', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'DecodeInvoice':
try {
@ -174,11 +174,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.DecodeInvoiceRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.DecodeInvoice({rpcName:'DecodeInvoice', ctx:authContext , req: request})
const response = await methods.DecodeInvoice({ rpcName: 'DecodeInvoice', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'PayInvoice':
try {
@ -190,11 +190,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.PayInvoiceRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.PayInvoice({rpcName:'PayInvoice', ctx:authContext , req: request})
const response = await methods.PayInvoice({ rpcName: 'PayInvoice', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'OpenChannel':
try {
@ -206,11 +206,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.OpenChannelRequestValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.OpenChannel({rpcName:'OpenChannel', ctx:authContext , req: request})
const response = await methods.OpenChannel({ rpcName: 'OpenChannel', ctx: authContext, req: request })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetLnurlWithdrawLink':
try {
@ -219,11 +219,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
const response = await methods.GetLnurlWithdrawLink({rpcName:'GetLnurlWithdrawLink', ctx:authContext })
const response = await methods.GetLnurlWithdrawLink({ rpcName: 'GetLnurlWithdrawLink', ctx: authContext })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetLnurlPayLink':
try {
@ -232,11 +232,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
const response = await methods.GetLnurlPayLink({rpcName:'GetLnurlPayLink', ctx:authContext })
const response = await methods.GetLnurlPayLink({ rpcName: 'GetLnurlPayLink', ctx: authContext })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetLNURLChannelLink':
try {
@ -245,11 +245,11 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
const response = await methods.GetLNURLChannelLink({rpcName:'GetLNURLChannelLink', ctx:authContext })
const response = await methods.GetLNURLChannelLink({ rpcName: 'GetLNURLChannelLink', ctx: authContext })
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
res({ status: 'OK', ...response })
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetLiveUserOperations':
try {
@ -258,11 +258,13 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
methods.GetLiveUserOperations({rpcName:'GetLiveUserOperations', ctx:authContext ,cb: (response, err) => {
stats.handle = process.hrtime.bigint()
if (err) { logErrorAndReturnResponse(err, err.message, res, logger, { ...info, ...stats, ...authContext }, opts.metricsCallback)} else { res({status: 'OK', ...response});opts.metricsCallback([{ ...info, ...stats, ...authContext }])}
}})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
methods.GetLiveUserOperations({
rpcName: 'GetLiveUserOperations', ctx: authContext, cb: (response, err) => {
stats.handle = process.hrtime.bigint()
if (err) { logErrorAndReturnResponse(err, err.message, res, logger, { ...info, ...stats, ...authContext }, opts.metricsCallback) } else { res({ status: 'OK', ...response }); opts.metricsCallback([{ ...info, ...stats, ...authContext }]) }
}
})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetMigrationUpdate':
try {
@ -271,17 +273,34 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
methods.GetMigrationUpdate({rpcName:'GetMigrationUpdate', ctx:authContext ,cb: (response, err) => {
stats.handle = process.hrtime.bigint()
if (err) { logErrorAndReturnResponse(err, err.message, res, logger, { ...info, ...stats, ...authContext }, opts.metricsCallback)} else { res({status: 'OK', ...response});opts.metricsCallback([{ ...info, ...stats, ...authContext }])}
}})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
methods.GetMigrationUpdate({
rpcName: 'GetMigrationUpdate', ctx: authContext, cb: (response, err) => {
stats.handle = process.hrtime.bigint()
if (err) { logErrorAndReturnResponse(err, err.message, res, logger, { ...info, ...stats, ...authContext }, opts.metricsCallback) } else { res({ status: 'OK', ...response }); opts.metricsCallback([{ ...info, ...stats, ...authContext }]) }
}
})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetHttpCreds':
try {
if (!methods.GetHttpCreds) throw new Error('method: GetHttpCreds is not implemented')
const authContext = await opts.NostrUserAuthGuard(req.appId, req.authIdentifier)
stats.guard = process.hrtime.bigint()
authCtx = authContext
stats.validate = stats.guard
methods.GetHttpCreds({
rpcName: 'GetHttpCreds', ctx: authContext, cb: (response, err) => {
stats.handle = process.hrtime.bigint()
if (err) { logErrorAndReturnResponse(err, err.message, res, logger, { ...info, ...stats, ...authContext }, opts.metricsCallback) } else { res({ status: 'OK', ...response }); opts.metricsCallback([{ ...info, ...stats, ...authContext }]) }
}
})
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'BatchUser':
try {
info.batch = true
const requests = req.body.requests as Types.UserMethodInputs[]
if (!Array.isArray(requests))throw new Error('invalid body, is not an array')
if (!Array.isArray(requests)) throw new Error('invalid body, is not an array')
info.batchSize = requests.length
if (requests.length > 10) throw new Error('too many requests in the batch')
const ctx = await opts.NostrUserAuthGuard(req.appId, req.authIdentifier)
@ -295,7 +314,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const opInfo: Types.RequestInfo = { rpcName: operation.rpcName, batch: true, nostr: true, batchSize: 0 }
const opStats: Types.RequestStats = { startMs, start: startTime, parse: stats.parse, guard: stats.guard, validate: 0n, handle: 0n }
try {
switch(operation.rpcName) {
switch (operation.rpcName) {
case 'LinkNPubThroughToken':
if (!methods.LinkNPubThroughToken) {
throw new Error('method not defined: LinkNPubThroughToken')
@ -303,7 +322,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.LinkNPubThroughTokenRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
await methods.LinkNPubThroughToken({...operation, ctx}); responses.push({ status: 'OK' })
await methods.LinkNPubThroughToken({ ...operation, ctx }); responses.push({ status: 'OK' })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -313,7 +332,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
throw new Error('method not defined: UserHealth')
} else {
opStats.validate = opStats.guard
await methods.UserHealth({...operation, ctx}); responses.push({ status: 'OK' })
await methods.UserHealth({ ...operation, ctx }); responses.push({ status: 'OK' })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -323,7 +342,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
throw new Error('method not defined: GetUserInfo')
} else {
opStats.validate = opStats.guard
const res = await methods.GetUserInfo({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.GetUserInfo({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -335,7 +354,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.AddProductRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.AddProduct({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.AddProduct({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -345,7 +364,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
throw new Error('method not defined: NewProductInvoice')
} else {
opStats.validate = opStats.guard
const res = await methods.NewProductInvoice({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.NewProductInvoice({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -357,7 +376,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.GetUserOperationsRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.GetUserOperations({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.GetUserOperations({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -369,7 +388,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.NewAddressRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.NewAddress({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.NewAddress({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -381,7 +400,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.PayAddressRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.PayAddress({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.PayAddress({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -393,7 +412,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.NewInvoiceRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.NewInvoice({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.NewInvoice({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -405,7 +424,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.DecodeInvoiceRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.DecodeInvoice({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.DecodeInvoice({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -417,7 +436,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.PayInvoiceRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.PayInvoice({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.PayInvoice({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -429,7 +448,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const error = Types.OpenChannelRequestValidate(operation.req)
opStats.validate = process.hrtime.bigint()
if (error !== null) throw error
const res = await methods.OpenChannel({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.OpenChannel({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -439,7 +458,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
throw new Error('method not defined: GetLnurlWithdrawLink')
} else {
opStats.validate = opStats.guard
const res = await methods.GetLnurlWithdrawLink({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.GetLnurlWithdrawLink({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -449,7 +468,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
throw new Error('method not defined: GetLnurlPayLink')
} else {
opStats.validate = opStats.guard
const res = await methods.GetLnurlPayLink({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.GetLnurlPayLink({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
@ -459,22 +478,22 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
throw new Error('method not defined: GetLNURLChannelLink')
} else {
opStats.validate = opStats.guard
const res = await methods.GetLNURLChannelLink({...operation, ctx}); responses.push({ status: 'OK', ...res })
const res = await methods.GetLNURLChannelLink({ ...operation, ctx }); responses.push({ status: 'OK', ...res })
opStats.handle = process.hrtime.bigint()
callsMetrics.push({ ...opInfo, ...opStats, ...ctx })
}
break
default:
throw new Error('unkown rpcName')
throw new Error('unkown rpcName')
}
} catch(ex) {const e = ex as any; logger.error(e.message || e); callsMetrics.push({ ...opInfo, ...opStats, ...ctx, error: e.message }); responses.push({ status: 'ERROR', reason: e.message || e })}
} catch (ex) { const e = ex as any; logger.error(e.message || e); callsMetrics.push({ ...opInfo, ...opStats, ...ctx, error: e.message }); responses.push({ status: 'ERROR', reason: e.message || e }) }
}
stats.handle = process.hrtime.bigint()
res({ status: 'OK', responses })
opts.metricsCallback([{ ...info, ...stats, ...ctx }, ...callsMetrics])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
default: logger.error('unknown rpc call name from nostr event:'+req.rpcName)
default: logger.error('unknown rpc call name from nostr event:' + req.rpcName)
}
}
}