Skip to content

Commit

Permalink
Merge branch 'main' into feat/customer-fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
AmeanAsad committed Nov 3, 2023
2 parents 4e50992 + b809e94 commit 5f2ea66
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@filecoin-saturn/js-client",
"version": "0.3.3",
"version": "0.3.4",
"description": "Filecoin Saturn Client",
"homepage": "https://github.com/filecoin-saturn/js-client",
"main": "src/index.js",
Expand Down Expand Up @@ -61,4 +61,4 @@
"publishConfig": {
"access": "public"
}
}
}
12 changes: 11 additions & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ export class Saturn {
* @returns {Promise<AsyncIterable<Uint8Array>>}
*/
async * fetchContentWithFallback (cidPath, opts = {}) {
const upstreamController = opts.controller
delete opts.controller

let lastError = null
let skipNodes = false
// we use this to checkpoint at which chunk a request failed.
Expand All @@ -253,6 +256,13 @@ export class Saturn {
}

const fetchContent = async function * (options) {
const controller = new AbortController()
opts.controller = controller
if (upstreamController) {
upstreamController.signal.addEventListener('abort', () => {
controller.abort()
})
}
let byteCount = 0
const fetchOptions = Object.assign(opts, { format: 'car' }, options)
const byteChunks = await this.fetchContent(cidPath, fetchOptions)
Expand Down Expand Up @@ -293,7 +303,7 @@ export class Saturn {
let fallbackCount = 0
const nodes = this.nodes
for (let i = 0; i < nodes.length; i++) {
if (fallbackCount > this.config.fallbackLimit || skipNodes) {
if (fallbackCount > this.config.fallbackLimit || skipNodes || upstreamController?.signal.aborted) {
break
}
if (opts.raceNodes) {
Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @property {string} [customerFallbackURL] - Customer Origin that is a fallback.
* @property {number} [connectTimeout=5000] - Connection timeout in milliseconds.
* @property {number} [downloadTimeout=0] - Download timeout in milliseconds.
* @property {AbortController} [controller]
*/

export {}

0 comments on commit 5f2ea66

Please sign in to comment.