Skip to content

Commit

Permalink
add submit .timeout (#6)
Browse files Browse the repository at this point in the history
* add submit .timeout

* fix lint
  • Loading branch information
juliangruber authored Jun 21, 2023
1 parent c200504 commit 7d05dfc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ export default class Spark {
async fetchCAR (url, stats) {
console.log(`Fetching ${url}...`)

// Abort if no progress was made for 10 seconds
// Abort if no progress was made for 60 seconds
const controller = new AbortController()
const { signal } = controller
let timeout
const resetTimeout = () => {
if (timeout) {
clearTimeout(timeout)
}
timeout = setTimeout(() => controller.abort(), 60_000)
timeout = setTimeout(() => {
stats.timeout = true
controller.abort()
}, 60_000)
}

try {
Expand Down Expand Up @@ -90,6 +93,7 @@ export default class Spark {

let success = false
const stats = {
timeout: false,
startAt: new Date(),
firstByteAt: null,
endAt: null,
Expand Down
2 changes: 2 additions & 0 deletions test/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ test('fetchCAR', async () => {
}
const spark = new Spark({ fetch })
const stats = {
timeout: false,
startAt: new Date(),
firstByteAt: null,
endAt: null,
byteLength: 0,
statusCode: null
}
await spark.fetchCAR(URL, stats)
assertEquals(stats.timeout, false)
assertInstanceOf(stats.startAt, Date)
assertInstanceOf(stats.firstByteAt, Date)
assertInstanceOf(stats.endAt, Date)
Expand Down

0 comments on commit 7d05dfc

Please sign in to comment.