Skip to content

Commit

Permalink
feat: log anchor (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Oct 21, 2022
1 parent 2bf7587 commit 26351cd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
36 changes: 28 additions & 8 deletions packages/edge-gateway/src/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import pSettle from 'p-settle'
import { FilterError } from 'p-some'
import { gatewayFetch } from 'ipfs-gateway-race'

import { getCidFromSubdomainUrl } from './utils/cid.js'
import {
getCidFromSubdomainUrl,
toDenyListAnchor
} from './utils/cid.js'
import { getHeaders } from './utils/headers.js'
import { TimeoutError } from './errors.js'
import {
Expand All @@ -32,6 +35,9 @@ import {
* @property {Response} response
* @property {ResolutionLayer} resolutionLayer
* @property {string} url
*
* @typedef {Object} OptionalCustomHeaders
* @property {string} [anchor]
*/

/**
Expand Down Expand Up @@ -81,7 +87,10 @@ export async function gatewayGet (request, env, ctx) {
return getResponseWithCustomHeaders(
dotstorageRes.response,
RESOLUTION_LAYERS.DOTSTORAGE_RACE,
dotstorageRes.resolutionIdentifier
dotstorageRes.resolutionIdentifier,
{
anchor: await toDenyListAnchor(cid)
}
)
}

Expand All @@ -93,7 +102,7 @@ export async function gatewayGet (request, env, ctx) {
} = await getFromGatewayRacer(cid, pathname, getHeaders(request), env, ctx)

// Validation layer - resource CID
const resourceCid = getCidFromEtag(winnerGwResponse.headers.get('etag') || '')
const resourceCid = pathname !== '/' ? getCidFromEtag(winnerGwResponse.headers.get('etag') || cid) : cid
if (winnerGwResponse && pathname !== '/' && resourceCid) {
const cidResourceDenylistResponse = await env.CID_VERIFIER.fetch(`${env.CID_VERIFIER_URL}/denylist?cid=${resourceCid}`)
// Ignore if CID received from gateway in etag header is invalid by any reason
Expand All @@ -107,10 +116,9 @@ export async function gatewayGet (request, env, ctx) {
env.isCidVerifierEnabled &&
winnerGwResponse && winnerGwResponse.headers.get('content-type')?.includes('text/html')
) {
const verifyCid = pathname !== '/' ? resourceCid : cid
// fire and forget. Let cid-verifier process this cid and url if it needs to
ctx.waitUntil(
env.CID_VERIFIER.fetch(`${env.CID_VERIFIER_URL}/?cid=${verifyCid}`, { method: 'POST' })
env.CID_VERIFIER.fetch(`${env.CID_VERIFIER_URL}/?cid=${resourceCid}`, { method: 'POST' })
)
}

Expand All @@ -122,7 +130,10 @@ export async function gatewayGet (request, env, ctx) {
return getResponseWithCustomHeaders(
winnerGwResponse,
raceResolutionLayer,
winnerUrl
winnerUrl,
{
anchor: await toDenyListAnchor(resourceCid)
}
)
}

Expand Down Expand Up @@ -358,15 +369,16 @@ async function putToCache (request, response, cache) {
}

/**
*
* @param {Response} response
* @param {ResolutionLayer} resolutionLayer
* @param {string} resolutionIdentifier
* @param {OptionalCustomHeaders} [options]
*/
function getResponseWithCustomHeaders (
response,
resolutionLayer,
resolutionIdentifier
resolutionIdentifier,
options = {}
) {
const clonedResponse = new Response(response.body, response)

Expand All @@ -376,6 +388,14 @@ function getResponseWithCustomHeaders (
resolutionIdentifier
)

// Add anchor
if (options.anchor) {
clonedResponse.headers.set(
'x-dotstorage-anchor',
options.anchor
)
}

return clonedResponse
}

Expand Down
13 changes: 13 additions & 0 deletions packages/edge-gateway/src/utils/cid.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Multibases } from 'ipfs-core-utils/multibases'
import { bases } from 'multiformats/basics'
import { CID } from 'multiformats/cid'
import * as uint8arrays from 'uint8arrays'
import { sha256 } from 'multiformats/hashes/sha2'

import { InvalidUrlError } from '../errors.js'

Expand Down Expand Up @@ -55,3 +57,14 @@ async function getMultibaseDecoder (cid) {

return base.decoder
}

/**
* Get denylist anchor with badbits format.
*
* @param {string} cid
*/
export async function toDenyListAnchor (cid) {
const multihash = await sha256.digest(uint8arrays.fromString(`${cid}/`))
const digest = multihash.bytes.subarray(2)
return uint8arrays.toString(digest, 'hex')
}
3 changes: 3 additions & 0 deletions packages/edge-gateway/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ test('Gets content', async (t) => {

// Validate content headers
t.is(response.headers.get('content-length'), '23')
t.is(response.headers.get('x-dotstorage-anchor'), 'd13421337e80609a2aa9412ee646b4f6d6cb088cd9314bca313249896b2d19d0')

// Validate x-dotstorage headers
t.assert(
Expand All @@ -78,6 +79,8 @@ test('Gets content with path', async (t) => {
// Validate content headers
t.is(response.headers.get('content-length'), '35')
t.is(response.headers.get('content-type'), 'text/plain; charset=utf-8')
t.is(response.headers.get('x-dotstorage-anchor'), 'fd7c4bfd340f259e1276d8cbd61649ba02b3754fdba044f3bfdefa1bee680fc1')

// Validate x-dotstorage headers
t.assert(
response.headers.get('x-dotstorage-resolution-layer')
Expand Down

0 comments on commit 26351cd

Please sign in to comment.