Skip to content

Commit

Permalink
Add HEAD request. #104 (#109)
Browse files Browse the repository at this point in the history
* Add HEAD request. #104

* clean up

* todo

* style

* add passing tests

* Update test/integration.js

Co-authored-by: Miroslav Bajtoš <[email protected]>

* add Accept header

* fix lint

---------

Co-authored-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
juliangruber and bajtos authored Feb 21, 2025
1 parent d16b3a1 commit 3f8edc5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export default class Spark {
stats.providerAddress = provider.address

await this.fetchCAR(provider.protocol, provider.address, retrieval.cid, stats)
if (stats.protocol === 'http') {
await this.testHeadRequest(provider.address, retrieval.cid, stats)
}
}

async fetchCAR (protocol, address, cid, stats) {
Expand Down Expand Up @@ -160,6 +163,25 @@ export default class Spark {
stats.endAt = new Date()
}

async testHeadRequest (address, cid, stats) {
const url = getRetrievalUrl('http', address, cid)
console.log(`Testing HEAD request: ${url}`)
try {
const res = await this.#fetch(url, {
method: 'HEAD',
headers: {
Accept: 'application/vnd.ipld.raw'
},
signal: AbortSignal.timeout(10_000)
})
stats.headStatusCode = res.status
} catch (err) {
console.error(`Failed to make HEAD request to ${address} for ${cid}`)
console.error(err)
stats.headStatusCode = mapErrorToStatusCode(err)
}
}

async submitMeasurement (task, stats) {
console.log('Submitting measurement...')
const payload = {
Expand Down Expand Up @@ -263,7 +285,8 @@ export function newStats () {
carTooLarge: false,
byteLength: 0,
carChecksum: null,
statusCode: null
statusCode: null,
headStatusCode: null
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ test('retrieval check for our CID', async () => {
assertProp('protocol', 'http')
assertProp('timeout', false)
assertProp('statusCode', 200)
// Note: frisbii.fly.io doesn't support HEAD requests yet
// https://github.com/CheckerNetwork/frisbii-on-fly/issues/3
assertProp('headStatusCode', 405)
assertProp('byteLength', 200)
assertProp('carTooLarge', false)
// TODO - spark-api does not record this field yet
Expand Down
46 changes: 46 additions & 0 deletions test/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,52 @@ test('getRetrieval', async () => {
])
})

test('testHeadRequest', async () => {
const requests = []
const spark = new Spark({
fetch: async (url, { method, headers }) => {
requests.push({ url: url.toString(), method, headers })
return {
status: 200
}
}
})
const stats = {}
await spark.testHeadRequest('/dns/frisbii.fly.dev/tcp/443/https', KNOWN_CID, stats)
assertEquals(stats.headStatusCode, 200)
assertEquals(requests, [{ url: `https://frisbii.fly.dev/ipfs/${KNOWN_CID}?dag-scope=block`, method: 'HEAD', headers: { Accept: 'application/vnd.ipld.raw' } }])
})

test('testHeadRequest - with statusCode=500', async () => {
const requests = []
const spark = new Spark({
fetch: async (url, { method }) => {
requests.push({ url: url.toString(), method })
return {
status: 500
}
}
})
const stats = {}
await spark.testHeadRequest('/dns/frisbii.fly.dev/tcp/443/https', KNOWN_CID, stats)
assertEquals(stats.headStatusCode, 500)
assertEquals(requests, [{ url: `https://frisbii.fly.dev/ipfs/${KNOWN_CID}?dag-scope=block`, method: 'HEAD' }])
})

test('testHeadRequest - with network failure', async () => {
const requests = []
const spark = new Spark({
fetch: async (url, { method }) => {
requests.push({ url: url.toString(), method })
throw new Error()
}
})
const stats = {}
await spark.testHeadRequest('/dns/frisbii.fly.dev/tcp/443/https', KNOWN_CID, stats)
assertEquals(stats.headStatusCode, 600)
assertEquals(requests, [{ url: `https://frisbii.fly.dev/ipfs/${KNOWN_CID}?dag-scope=block`, method: 'HEAD' }])
})

test('fetchCAR - http', async () => {
const requests = []
const spark = new Spark({
Expand Down

0 comments on commit 3f8edc5

Please sign in to comment.