Skip to content

Commit

Permalink
chore: remove parts of access-api/src/service.js upload-api-proxy tha…
Browse files Browse the repository at this point in the history
…t were only useful for debugging/testing (#378)

Motivation:
* part of #325 
* remove things that were only useful for debugging/prototyping
  • Loading branch information
gobengo authored Jan 20, 2023
1 parent d4e7722 commit bb2f9be
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 78 deletions.
65 changes: 1 addition & 64 deletions packages/access-api/src/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,7 @@ import * as uploadApi from './upload-api-proxy.js'
*/
export function service(ctx) {
return {
store: uploadApi.createStoreProxy({
...ctx,
fetch: patchedFetch(
globalThis.fetch.bind(globalThis),
async (response, fetchArgs) => {
if (!response.ok) {
try {
ctx.log.warn(
`unexpected non-ok response from fetch in createStoreProxy`,
await describeFetch(response, fetchArgs)
)
} catch (error) {
ctx.log.error(
new Error(`error describing/logging fetch response`, {
cause: error,
})
)
}
}
}
),
}),
store: uploadApi.createStoreProxy(ctx),
upload: uploadApi.createUploadProxy(ctx),

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

/**
* Wrap `fetch` producing a new fetch that will pass any responses it fetches to a cb.
*
* @param {typeof globalThis.fetch} fetch
* @param {(response: Response, fetchArgs: Parameters<typeof globalThis.fetch>) => Promise<void>} onResponse - handles each response
* @returns {typeof globalThis.fetch}
*/
function patchedFetch(fetch, onResponse) {
/** @type {typeof globalThis.fetch} */
const fetchWithLog = async (input, init) => {
const response = await fetch(input, init)
await onResponse(response, [
input instanceof URL ? input.toString() : input,
init,
])
return response
}
return fetchWithLog
}

/**
* Given a fetch invocation and the response it produced,
* return an object that can be logged to describe the request/response fully.
*
* @param {Response} response
* @param {Parameters<typeof globalThis.fetch>} fetchArgs
*/
async function describeFetch(response, fetchArgs) {
return {
request: fetchArgs,
response: {
ok: response.ok,
redirected: response.redirected,
headers: [...response.headers],
status: response.status,
statusText: response.statusText,
url: response.url,
text: await response.clone().text(),
},
}
}
6 changes: 3 additions & 3 deletions packages/access-api/src/service/upload-api-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { createProxyHandler } from '../ucanto/proxy.js'
* @template {string|number|symbol} M
* @template {Ucanto.ConnectionView<any>} [Connection=Ucanto.ConnectionView<any>]
* @param {object} options
* @param {Ucanto.Signer} [options.signer]
* @param {Ucanto.Signer} options.signer
* @param {Array<M>} options.methods
* @param {{ default: Connection } & Record<Ucanto.UCAN.DID, Connection>} options.connections
*/
Expand Down Expand Up @@ -97,7 +97,7 @@ function getDefaultConnections(options) {
/**
* @template {Ucanto.ConnectionView<any>} [Connection=Ucanto.ConnectionView<any>]
* @param {object} options
* @param {Ucanto.Signer} [options.signer]
* @param {Ucanto.Signer} options.signer
* @param {typeof globalThis.fetch} [options.fetch]
* @param {{ default: Connection, [K: Ucanto.UCAN.DID]: Connection }} [options.connections]
* @param {Record<Ucanto.UCAN.DID, URL>} [options.audienceToUrl]
Expand All @@ -116,7 +116,7 @@ export function createUploadProxy(options) {
/**
* @template {Ucanto.ConnectionView<any>} [Connection=Ucanto.ConnectionView<any>]
* @param {object} options
* @param {Ucanto.Signer} [options.signer]
* @param {Ucanto.Signer} options.signer
* @param {typeof globalThis.fetch} [options.fetch]
* @param {{ default: Connection, [K: Ucanto.UCAN.DID]: Connection }} [options.connections]
* @param {Record<Ucanto.UCAN.DID, URL>} [options.audienceToUrl]
Expand Down
13 changes: 2 additions & 11 deletions packages/access-api/src/ucanto/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function defaultCatchInvocationError(error) {
* @param {object} options
* @param {(error: unknown) => Promise<unknown>} [options.catchInvocationError] - catches any error that comes from invoking the proxy invocation on the connection. If it returns a value, that value will be the proxied invocation result.
* @param {{ default: Connection, [K: Ucanto.UCAN.DID]: Connection }} options.connections
* @param {Ucanto.Signer} [options.signer]
* @param {Ucanto.Signer} options.signer
*/
export function createProxyHandler(options) {
/**
Expand All @@ -64,17 +64,8 @@ export function createProxyHandler(options) {
} = options
const { audience, capabilities, expiration, notBefore } = invocationIn
const connection = connections[audience.did()] ?? connections.default
// eslint-disable-next-line unicorn/prefer-logical-operator-over-ternary, no-unneeded-ternary
const proxyInvocationIssuer = signer
? // this results in a forwarded invocation, but the upstream will reject the signature
// created using options.signer unless options.signer signs w/ the same private key as the original issuer
// and it'd be nice to not even have to pass around `options.signer`
signer
: // this works, but involves lying about the issuer type (it wants a Signer but context.id is only a Verifier)
// @todo obviate this type override via https://github.com/web3-storage/ucanto/issues/195
/** @type {Ucanto.Signer} */ (context.id)
const proxyInvocation = Client.invoke({
issuer: proxyInvocationIssuer,
issuer: signer,
capability: capabilities[0],
audience,
proofs: [invocationIn],
Expand Down
2 changes: 2 additions & 0 deletions packages/access-api/test/ucanto-proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('ucanto-proxy', () => {
service: {
test: {
succeed: createProxyHandler({
signer: proxyPrincipal,
connections: {
default: Client.connect({
id: upstreamPrincipal,
Expand Down Expand Up @@ -116,6 +117,7 @@ describe('ucanto-proxy', () => {
service: {
test: {
succeed: createProxyHandler({
signer: upstreamPrincipal,
connections: {
default: Client.connect({
id: upstreamPrincipal,
Expand Down

0 comments on commit bb2f9be

Please sign in to comment.