From 6aada8eff35470e19c4301eec0e4c0514ca11e98 Mon Sep 17 00:00:00 2001 From: Benjamin Goering <171782+gobengo@users.noreply.github.com> Date: Wed, 18 Jan 2023 14:19:25 -0800 Subject: [PATCH] access-api service does createStoryProxy with patched fetch function that logs unexpected responses with status=530 --- packages/access-api/src/service/index.js | 45 +++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/packages/access-api/src/service/index.js b/packages/access-api/src/service/index.js index 06ea54814..726539fcc 100644 --- a/packages/access-api/src/service/index.js +++ b/packages/access-api/src/service/index.js @@ -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: { @@ -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 +}