Skip to content

Commit 3f8edc5

Browse files
juliangruberbajtos
andauthored
Add HEAD request. #104 (#109)
* 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]>
1 parent d16b3a1 commit 3f8edc5

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

lib/spark.js

Lines changed: 24 additions & 1 deletion
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,25 @@ 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+
headers: {
173+
Accept: 'application/vnd.ipld.raw'
174+
},
175+
signal: AbortSignal.timeout(10_000)
176+
})
177+
stats.headStatusCode = res.status
178+
} catch (err) {
179+
console.error(`Failed to make HEAD request to ${address} for ${cid}`)
180+
console.error(err)
181+
stats.headStatusCode = mapErrorToStatusCode(err)
182+
}
183+
}
184+
163185
async submitMeasurement (task, stats) {
164186
console.log('Submitting measurement...')
165187
const payload = {
@@ -263,7 +285,8 @@ export function newStats () {
263285
carTooLarge: false,
264286
byteLength: 0,
265287
carChecksum: null,
266-
statusCode: null
288+
statusCode: null,
289+
headStatusCode: null
267290
}
268291
}
269292

test/integration.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ test('retrieval check for our CID', async () => {
4242
assertProp('protocol', 'http')
4343
assertProp('timeout', false)
4444
assertProp('statusCode', 200)
45+
// Note: frisbii.fly.io doesn't support HEAD requests yet
46+
// https://github.com/CheckerNetwork/frisbii-on-fly/issues/3
47+
assertProp('headStatusCode', 405)
4548
assertProp('byteLength', 200)
4649
assertProp('carTooLarge', false)
4750
// TODO - spark-api does not record this field yet

test/spark.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,52 @@ test('getRetrieval', async () => {
6969
])
7070
})
7171

72+
test('testHeadRequest', async () => {
73+
const requests = []
74+
const spark = new Spark({
75+
fetch: async (url, { method, headers }) => {
76+
requests.push({ url: url.toString(), method, headers })
77+
return {
78+
status: 200
79+
}
80+
}
81+
})
82+
const stats = {}
83+
await spark.testHeadRequest('/dns/frisbii.fly.dev/tcp/443/https', KNOWN_CID, stats)
84+
assertEquals(stats.headStatusCode, 200)
85+
assertEquals(requests, [{ url: `https://frisbii.fly.dev/ipfs/${KNOWN_CID}?dag-scope=block`, method: 'HEAD', headers: { Accept: 'application/vnd.ipld.raw' } }])
86+
})
87+
88+
test('testHeadRequest - with statusCode=500', async () => {
89+
const requests = []
90+
const spark = new Spark({
91+
fetch: async (url, { method }) => {
92+
requests.push({ url: url.toString(), method })
93+
return {
94+
status: 500
95+
}
96+
}
97+
})
98+
const stats = {}
99+
await spark.testHeadRequest('/dns/frisbii.fly.dev/tcp/443/https', KNOWN_CID, stats)
100+
assertEquals(stats.headStatusCode, 500)
101+
assertEquals(requests, [{ url: `https://frisbii.fly.dev/ipfs/${KNOWN_CID}?dag-scope=block`, method: 'HEAD' }])
102+
})
103+
104+
test('testHeadRequest - with network failure', async () => {
105+
const requests = []
106+
const spark = new Spark({
107+
fetch: async (url, { method }) => {
108+
requests.push({ url: url.toString(), method })
109+
throw new Error()
110+
}
111+
})
112+
const stats = {}
113+
await spark.testHeadRequest('/dns/frisbii.fly.dev/tcp/443/https', KNOWN_CID, stats)
114+
assertEquals(stats.headStatusCode, 600)
115+
assertEquals(requests, [{ url: `https://frisbii.fly.dev/ipfs/${KNOWN_CID}?dag-scope=block`, method: 'HEAD' }])
116+
})
117+
72118
test('fetchCAR - http', async () => {
73119
const requests = []
74120
const spark = new Spark({

0 commit comments

Comments
 (0)