-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/blob-get
- Loading branch information
Showing
37 changed files
with
685 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
{ | ||
"packages/access-client": "20.0.0", | ||
"packages/blob-index": "1.0.2", | ||
"packages/blob-index": "1.0.3", | ||
"packages/filecoin-api": "7.1.0", | ||
"packages/filecoin-client": "3.3.3", | ||
"packages/capabilities": "17.1.1", | ||
"packages/upload-api": "17.0.0", | ||
"packages/upload-client": "16.0.1", | ||
"packages/w3up-client": "13.1.1", | ||
"packages/upload-client": "16.0.2", | ||
"packages/w3up-client": "14.0.0", | ||
"packages/did-mailto": "2.0.2" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * as ShardedDAGIndex from './sharded-dag-index.js' | ||
export * from './digest-map.js' | ||
export { indexShardedDAG } from './util.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import retry, { AbortError } from 'p-retry' | ||
import { CAR } from '@ucanto/transport' | ||
import { receiptsEndpoint } from './service.js' | ||
import { REQUEST_RETRIES } from './constants.js' | ||
|
||
export class ReceiptNotFound extends Error { | ||
/** | ||
* @param {import('multiformats').UnknownLink} taskCid | ||
*/ | ||
constructor(taskCid) { | ||
super() | ||
this.taskCid = taskCid | ||
} | ||
|
||
/* c8 ignore start */ | ||
get reason() { | ||
return `receipt not found for task ${this.taskCid} in the indexed workflow` | ||
} | ||
/* c8 ignore end */ | ||
|
||
get name() { | ||
return 'ReceiptNotFound' | ||
} | ||
} | ||
|
||
export class ReceiptMissing extends Error { | ||
/** | ||
* @param {import('multiformats').UnknownLink} taskCid | ||
*/ | ||
constructor(taskCid) { | ||
super() | ||
this.taskCid = taskCid | ||
} | ||
|
||
/* c8 ignore start */ | ||
get reason() { | ||
return `receipt missing for task ${this.taskCid}` | ||
} | ||
/* c8 ignore end */ | ||
|
||
get name() { | ||
return 'ReceiptMissing' | ||
} | ||
} | ||
|
||
/** | ||
* Polls for a receipt for an executed task by its CID. | ||
* | ||
* @param {import('multiformats').UnknownLink} taskCid | ||
* @param {import('./types.js').RequestOptions} [options] | ||
* @returns {Promise<import('@ucanto/interface').Receipt>} | ||
*/ | ||
export async function poll(taskCid, options = {}) { | ||
return await retry( | ||
async () => { | ||
const res = await get(taskCid, options) | ||
if (res.error) { | ||
// @ts-ignore | ||
if (res.error.name === 'ReceiptNotFound') { | ||
// throw an error that will cause `p-retry` to retry with | ||
throw res.error | ||
} else { | ||
throw new AbortError( | ||
new Error('failed to fetch blob/accept receipt', { | ||
cause: res.error, | ||
}) | ||
) | ||
} | ||
} | ||
return res.ok | ||
}, | ||
{ | ||
onFailedAttempt: console.warn, | ||
/* c8 ignore next */ | ||
retries: options.retries ?? REQUEST_RETRIES, | ||
} | ||
) | ||
} | ||
|
||
/** | ||
* Get a receipt for an executed task by its CID. | ||
* | ||
* @param {import('multiformats').UnknownLink} taskCid | ||
* @param {import('./types.js').RequestOptions} [options] | ||
* @returns {Promise<import('@ucanto/client').Result<import('@ucanto/interface').Receipt, Error>>} | ||
*/ | ||
async function get(taskCid, options = {}) { | ||
// Fetch receipt from endpoint | ||
const url = new URL( | ||
taskCid.toString(), | ||
options.receiptsEndpoint ?? receiptsEndpoint | ||
) | ||
const fetchReceipt = options.fetch ?? globalThis.fetch.bind(globalThis) | ||
const workflowResponse = await fetchReceipt(url) | ||
/* c8 ignore start */ | ||
if (workflowResponse.status === 404) { | ||
return { | ||
error: new ReceiptNotFound(taskCid), | ||
} | ||
} | ||
/* c8 ignore stop */ | ||
// Get receipt from Message Archive | ||
const agentMessageBytes = new Uint8Array(await workflowResponse.arrayBuffer()) | ||
// Decode message | ||
const agentMessage = await CAR.request.decode({ | ||
body: agentMessageBytes, | ||
headers: {}, | ||
}) | ||
// Get receipt from the potential multiple receipts in the message | ||
const receipt = agentMessage.receipts.get(taskCid.toString()) | ||
if (!receipt) { | ||
return { | ||
error: new ReceiptMissing(taskCid), | ||
} | ||
} | ||
return { | ||
ok: receipt, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.