From 2228d769b0e590d2b239839f10a596991352ca19 Mon Sep 17 00:00:00 2001 From: Petra Jaros Date: Fri, 18 Oct 2024 12:54:47 -0400 Subject: [PATCH] fix: Find the receipts URL from the connection URL We were defaulting to the production URL unless a `receiptsEndpoint` was given in the options, and then never providing one. Passing it along would probably be the better solution, but there are a lot of places to get that wrong as things stand right now. It's not clear to me where the receipt URL really should live--eg., whether it should properly be part of the connection config. --- packages/upload-client/src/receipts.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/upload-client/src/receipts.js b/packages/upload-client/src/receipts.js index 3851a7cc2..443737651 100644 --- a/packages/upload-client/src/receipts.js +++ b/packages/upload-client/src/receipts.js @@ -1,6 +1,6 @@ import retry, { AbortError } from 'p-retry' import { CAR } from '@ucanto/transport' -import { receiptsEndpoint } from './service.js' +import { receiptsEndpoint as defaultReceiptsEndpoint } from './service.js' import { REQUEST_RETRIES } from './constants.js' export class ReceiptNotFound extends Error { @@ -77,6 +77,19 @@ export async function poll(taskCid, options = {}) { ) } +/** + * Calculate a receipt endpoint from the URL of a channel, if it has one. + * @param {import('@ucanto/interface').Channel>} channel + */ +function receiptEndpointFromChannel(channel) { + if ('url' in channel && channel.url instanceof URL) { + const url = channel.url + return new URL('/receipt/', url.toString()) + } else { + return null + } +} + /** * Get a receipt for an executed task by its CID. * @@ -85,11 +98,13 @@ export async function poll(taskCid, options = {}) { * @returns {Promise>} */ async function get(taskCid, options = {}) { + const channel = options.connection?.channel + const receiptsEndpoint = + options.receiptsEndpoint ?? + (channel && receiptEndpointFromChannel(channel)) ?? + defaultReceiptsEndpoint // Fetch receipt from endpoint - const url = new URL( - taskCid.toString(), - options.receiptsEndpoint ?? receiptsEndpoint - ) + const url = new URL(taskCid.toString(), receiptsEndpoint) const fetchReceipt = options.fetch ?? globalThis.fetch.bind(globalThis) const workflowResponse = await fetchReceipt(url) /* c8 ignore start */