From bfa02f85f382b033c338e5d0f5c6db46396ae230 Mon Sep 17 00:00:00 2001 From: Max Kurapov Date: Wed, 28 Jun 2023 16:48:21 +0200 Subject: [PATCH] feat: only poll PP in the open payments middleware --- .../payment_pointer/middleware.ts | 2 +- .../payment_pointer/service.test.ts | 50 +++++++++++++------ .../open_payments/payment_pointer/service.ts | 6 ++- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/packages/backend/src/open_payments/payment_pointer/middleware.ts b/packages/backend/src/open_payments/payment_pointer/middleware.ts index e642583db6..35332a9bad 100644 --- a/packages/backend/src/open_payments/payment_pointer/middleware.ts +++ b/packages/backend/src/open_payments/payment_pointer/middleware.ts @@ -11,7 +11,7 @@ export function createPaymentPointerMiddleware() { const paymentPointerService = await ctx.container.use( 'paymentPointerService' ) - const paymentPointer = await paymentPointerService.getByUrl( + const paymentPointer = await paymentPointerService.getOrPollByUrl( ctx.paymentPointerUrl ) diff --git a/packages/backend/src/open_payments/payment_pointer/service.test.ts b/packages/backend/src/open_payments/payment_pointer/service.test.ts index 95e8b5d774..72a9b850a6 100644 --- a/packages/backend/src/open_payments/payment_pointer/service.test.ts +++ b/packages/backend/src/open_payments/payment_pointer/service.test.ts @@ -282,20 +282,42 @@ describe('Open Payments Payment Pointer Service', (): void => { }) describe('Get Payment Pointer By Url', (): void => { + test('can retrieve payment pointer by url', async (): Promise => { + const paymentPointer = await createPaymentPointer(deps) + await expect( + paymentPointerService.getByUrl(paymentPointer.url) + ).resolves.toEqual(paymentPointer) + + await expect( + paymentPointerService.getByUrl(paymentPointer.url + '/path') + ).resolves.toBeUndefined() + + await expect( + paymentPointerService.getByUrl('prefix+' + paymentPointer.url) + ).resolves.toBeUndefined() + }) + + test( + 'returns undefined if no payment pointer exists with url', + withConfigOverride( + () => config, + { paymentPointerLookupTimeoutMs: 1 }, + async (): Promise => { + await expect( + paymentPointerService.getByUrl('test.nope') + ).resolves.toBeUndefined() + } + ) + ) + }) + + describe('Get Or Poll Payment Pointer By Url', (): void => { describe('existing payment pointer', (): void => { test('can retrieve payment pointer by url', async (): Promise => { const paymentPointer = await createPaymentPointer(deps) await expect( - paymentPointerService.getByUrl(paymentPointer.url) + paymentPointerService.getOrPollByUrl(paymentPointer.url) ).resolves.toEqual(paymentPointer) - - await expect( - paymentPointerService.getByUrl(paymentPointer.url + '/path') - ).resolves.toBeUndefined() - - await expect( - paymentPointerService.getByUrl('prefix+' + paymentPointer.url) - ).resolves.toBeUndefined() }) }) @@ -308,7 +330,7 @@ describe('Open Payments Payment Pointer Service', (): void => { async (): Promise => { const paymentPointerUrl = `https://${faker.internet.domainName()}/.well-known/pay` await expect( - paymentPointerService.getByUrl(paymentPointerUrl) + paymentPointerService.getOrPollByUrl(paymentPointerUrl) ).resolves.toBeUndefined() const paymentPointerNotFoundEvents = @@ -331,9 +353,9 @@ describe('Open Payments Payment Pointer Service', (): void => { async (): Promise => { const paymentPointerUrl = `https://${faker.internet.domainName()}/.well-known/pay` - const [getByUrlPaymentPointer, createdPaymentPointer] = + const [getOrPollByUrlPaymentPointer, createdPaymentPointer] = await Promise.all([ - paymentPointerService.getByUrl(paymentPointerUrl), + paymentPointerService.getOrPollByUrl(paymentPointerUrl), (async () => { await sleep(5) return createPaymentPointer(deps, { @@ -342,8 +364,8 @@ describe('Open Payments Payment Pointer Service', (): void => { })() ]) - assert.ok(getByUrlPaymentPointer) - expect(getByUrlPaymentPointer).toEqual(createdPaymentPointer) + assert.ok(getOrPollByUrlPaymentPointer) + expect(getOrPollByUrlPaymentPointer).toEqual(createdPaymentPointer) } ) ) diff --git a/packages/backend/src/open_payments/payment_pointer/service.ts b/packages/backend/src/open_payments/payment_pointer/service.ts index 9bff7c1b4d..1765dd505f 100644 --- a/packages/backend/src/open_payments/payment_pointer/service.ts +++ b/packages/backend/src/open_payments/payment_pointer/service.ts @@ -46,6 +46,7 @@ export interface PaymentPointerService { update(options: UpdateOptions): Promise get(id: string): Promise getByUrl(url: string): Promise + getOrPollByUrl(url: string): Promise getPage(pagination?: Pagination): Promise processNext(): Promise triggerEvents(limit: number): Promise @@ -79,7 +80,8 @@ export async function createPaymentPointerService({ create: (options) => createPaymentPointer(deps, options), update: (options) => updatePaymentPointer(deps, options), get: (id) => getPaymentPointer(deps, id), - getByUrl: (url) => getOrRequestPaymentPointer(deps, url), + getByUrl: (url) => getPaymentPointerByUrl(deps, url), + getOrPollByUrl: (url) => getOrPollByUrl(deps, url), getPage: (pagination?) => getPaymentPointerPage(deps, pagination), processNext: () => processNextPaymentPointer(deps), triggerEvents: (limit) => triggerPaymentPointerEvents(deps, limit) @@ -173,7 +175,7 @@ async function getPaymentPointer( .withGraphFetched('asset') } -async function getOrRequestPaymentPointer( +async function getOrPollByUrl( deps: ServiceDependencies, url: string ): Promise {