Skip to content

Commit 904042a

Browse files
committed
Add HEAD request. #104
1 parent edbe9f3 commit 904042a

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lib/spark.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ export default class Spark {
7878
stats.providerAddress = provider.address
7979

8080
await this.fetchCAR(provider.protocol, provider.address, retrieval.cid, stats)
81+
if (stats.protocol === 'http') {
82+
await this.testHeadRequest(provider.address, retrieval.cid, stats)
83+
}
8184
}
8285

8386
async fetchCAR (protocol, address, cid, stats) {
@@ -160,6 +163,22 @@ export default class Spark {
160163
stats.endAt = new Date()
161164
}
162165

166+
async testHeadRequest (address, cid, stats) {
167+
const url = getRetrievalUrl('http', address, cid)
168+
console.log(`Testing HEAD request: ${url}`)
169+
try {
170+
const res = await this.#fetch(url, {
171+
method: 'HEAD',
172+
signal: AbortSignal.timeout(10_000)
173+
})
174+
stats.headStatusCode = res.status
175+
} catch (err) {
176+
console.error(`Failed to make HEAD request to ${address} for ${cid}`)
177+
console.error(err)
178+
stats.headStatusCode = mapErrorToStatusCode(err)
179+
}
180+
}
181+
163182
async submitMeasurement (task, stats) {
164183
console.log('Submitting measurement...')
165184
const payload = {
@@ -211,11 +230,11 @@ export default class Spark {
211230
this.handleRunError(err)
212231
}
213232
const duration = Date.now() - started
214-
const delay = calculateDelayBeforeNextTask({
233+
const delay = /* calculateDelayBeforeNextTask({
215234
roundLengthInMs: APPROX_ROUND_LENGTH_IN_MS,
216235
maxTasksPerRound: this.#tasker.maxTasksPerRound,
217236
lastTaskDurationInMs: duration
218-
})
237+
}) */ 0
219238

220239
if (delay > 0) {
221240
console.log(
@@ -263,7 +282,8 @@ export function newStats () {
263282
carTooLarge: false,
264283
byteLength: 0,
265284
carChecksum: null,
266-
statusCode: null
285+
statusCode: null,
286+
headStatusCode: null
267287
}
268288
}
269289

0 commit comments

Comments
 (0)