Skip to content

Commit da6a842

Browse files
committed
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.
1 parent 86e7a46 commit da6a842

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

packages/upload-client/src/receipts.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import retry, { AbortError } from 'p-retry'
22
import { CAR } from '@ucanto/transport'
3-
import { receiptsEndpoint } from './service.js'
3+
import { receiptsEndpoint as defaultReceiptsEndpoint } from './service.js'
44
import { REQUEST_RETRIES } from './constants.js'
55

66
export class ReceiptNotFound extends Error {
@@ -77,6 +77,20 @@ export async function poll(taskCid, options = {}) {
7777
)
7878
}
7979

80+
/**
81+
* Calculate a receipt endpoint from the URL of a channel, if it has one.
82+
*
83+
* @param {import('@ucanto/interface').Channel<Record<string, any>>} channel
84+
*/
85+
function receiptEndpointFromChannel(channel) {
86+
if ('url' in channel && channel.url instanceof URL) {
87+
const url = channel.url
88+
return new URL('/receipt/', url.toString())
89+
} else {
90+
return null
91+
}
92+
}
93+
8094
/**
8195
* Get a receipt for an executed task by its CID.
8296
*
@@ -85,11 +99,13 @@ export async function poll(taskCid, options = {}) {
8599
* @returns {Promise<import('@ucanto/client').Result<import('@ucanto/interface').Receipt, Error>>}
86100
*/
87101
async function get(taskCid, options = {}) {
102+
const channel = options.connection?.channel
103+
const receiptsEndpoint =
104+
options.receiptsEndpoint ??
105+
(channel && receiptEndpointFromChannel(channel)) ??
106+
defaultReceiptsEndpoint
88107
// Fetch receipt from endpoint
89-
const url = new URL(
90-
taskCid.toString(),
91-
options.receiptsEndpoint ?? receiptsEndpoint
92-
)
108+
const url = new URL(taskCid.toString(), receiptsEndpoint)
93109
const fetchReceipt = options.fetch ?? globalThis.fetch.bind(globalThis)
94110
const workflowResponse = await fetchReceipt(url)
95111
/* c8 ignore start */

0 commit comments

Comments
 (0)