Skip to content

Commit

Permalink
access-api service does createStoryProxy with patched fetch function …
Browse files Browse the repository at this point in the history
…that logs unexpected responses with status=530
  • Loading branch information
gobengo committed Jan 18, 2023
1 parent 187428e commit 6aada8e
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion packages/access-api/src/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import * as uploadApi from './upload-api-proxy.js'
*/
export function service(ctx) {
return {
store: uploadApi.createStoreProxy(ctx),
store: uploadApi.createStoreProxy({
...ctx,
fetch: log530Responses(globalThis.fetch.bind(globalThis), ctx),
}),
upload: uploadApi.createUploadProxy(ctx),

voucher: {
Expand Down Expand Up @@ -151,3 +154,43 @@ export function service(ctx) {
},
}
}

/**
* Wrap `fetch` producing a new fetch that will log any responses it encounters with a status code of 530.
* Temporary for debugging https://github.com/web3-storage/w3protocol/issues/363
*
* @param {typeof globalThis.fetch} fetch
* @param {object} ctx
* @param {import('@web3-storage/worker-utils/logging.js').Logging} ctx.log
* @returns {typeof globalThis.fetch}
*/
function log530Responses(fetch, ctx) {
/** @type {typeof globalThis.fetch} */
const fetchWithLog = async (requestInfo, requestInit) => {
const response = await fetch(requestInfo, requestInit)
if (response.status === 530) {
const message = `unexpected 530 response from fetch`
const fetchInvocationDescription = {
request: {
requestInfo,
requestInit,
},
response: {
type: response.type,
ok: response.ok,
redirected: response.redirected,
headers: [...response.headers],
status: response.status,
statusText: response.statusText,
url: response.url,
text: await response.clone().text(),
},
}
ctx.log.error(message, fetchInvocationDescription)
// eslint-disable-next-line no-console
console.warn(message, fetchInvocationDescription)
}
return response
}
return fetchWithLog
}

0 comments on commit 6aada8e

Please sign in to comment.