Skip to content

Commit

Permalink
fix: find the receipts URL from the connection URL
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fforbeck committed Jan 15, 2025
1 parent e5af74b commit 8ffaacf
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions packages/upload-client/src/receipts.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -77,6 +77,20 @@ 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<Record<string, any>>} 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.
*
Expand All @@ -85,11 +99,14 @@ export async function poll(taskCid, options = {}) {
* @returns {Promise<import('@ucanto/client').Result<import('@ucanto/interface').Receipt, Error>>}
*/
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 */
Expand Down

0 comments on commit 8ffaacf

Please sign in to comment.