Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data-handlers): add digest, etag, stable and verified headers #216

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export const headerNames = {
hops: 'X-AR-IO-Hops',
origin: 'X-AR-IO-Origin',
originNodeRelease: 'X-AR-IO-Origin-Node-Release',
digest: 'X-AR-IO-Digest',
stable: 'X-AR-IO-Stable',
verified: 'X-AR-IO-Verified',
cache: 'X-Cache',
arnsTtlSeconds: 'X-ArNS-TTL-Seconds',
arnsResolvedId: 'X-ArNS-Resolved-Id',
Expand Down
2 changes: 2 additions & 0 deletions src/routes/data/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ describe('Data routes', () => {
beforeEach(() => {
app = express();
dataIndex = {
getDataItemAttributes: () => Promise.resolve(undefined),
getDataAttributes: () => Promise.resolve(undefined),
getDataParent: () => Promise.resolve(undefined),
saveDataContentAttributes: () => Promise.resolve(undefined),
getTransactionAttributes: () => Promise.resolve(undefined),
};
dataSource = {
getData: () =>
Expand Down
25 changes: 25 additions & 0 deletions src/routes/data/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ const DEFAULT_CONTENT_TYPE = 'application/octet-stream';

const REQUEST_METHOD_HEAD = 'HEAD';

const setDigestStableVerifiedHeaders = ({
res,
dataAttributes,
}: {
res: Response;
dataAttributes: ContiguousDataAttributes | undefined;
}) => {
if (dataAttributes !== undefined) {
res.setHeader(headerNames.stable, dataAttributes.stable ? 'true' : 'false');
res.setHeader(
headerNames.verified,
dataAttributes.verified ? 'true' : 'false',
);
djwhitt marked this conversation as resolved.
Show resolved Hide resolved

if (dataAttributes.hash !== undefined) {
res.setHeader(headerNames.digest, dataAttributes.hash);
res.setHeader('ETag', dataAttributes.hash);
}
}
};

const setDataHeaders = ({
res,
dataAttributes,
Expand Down Expand Up @@ -99,6 +120,8 @@ const setDataHeaders = ({
if (dataAttributes?.contentEncoding !== undefined) {
res.header('Content-Encoding', dataAttributes.contentEncoding);
}

setDigestStableVerifiedHeaders({ res, dataAttributes });
};

const getRequestAttributes = (req: Request): RequestAttributes => {
Expand Down Expand Up @@ -147,6 +170,8 @@ const handleRangeRequest = (
data.sourceContentType ??
'application/octet-stream';

setDigestStableVerifiedHeaders({ res, dataAttributes });

if (isSingleRange) {
const totalSize = data.size;
const start = ranges[0].start;
Expand Down
20 changes: 20 additions & 0 deletions test/end-to-end/data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ describe('X-AR-IO headers', function () {
resWithHeaders.headers['x-ar-io-origin-node-release'],
'v1.0.0',
);
assert.equal(
resWithHeaders.headers['x-ar-io-digest'],
'gkOH8JBTdKr_wD9SriwYwCM6p7saQAJFU60AREREQLA',
);
assert.equal(
resWithHeaders.headers['etag'],
'gkOH8JBTdKr_wD9SriwYwCM6p7saQAJFU60AREREQLA',
);
assert.equal(resWithHeaders.headers['x-ar-io-stable'], 'false');
assert.equal(resWithHeaders.headers['x-ar-io-verified'], 'false');
});

it('Verifying that /<id> for a manifest with a missing index returns no hops, origin and node release', async function () {
Expand Down Expand Up @@ -454,6 +464,16 @@ describe('X-AR-IO headers', function () {
assert.equal(resWithHeaders.headers['x-ar-io-hops'], '6');
assert.equal(resWithHeaders.headers['x-ar-io-origin'], 'another-host');
assert.equal(resWithHeaders.headers['x-ar-io-origin-node-release'], '10');
assert.equal(
resWithHeaders.headers['x-ar-io-digest'],
'gkOH8JBTdKr_wD9SriwYwCM6p7saQAJFU60AREREQLA',
);
assert.equal(
resWithHeaders.headers['etag'],
'gkOH8JBTdKr_wD9SriwYwCM6p7saQAJFU60AREREQLA',
);
assert.equal(resWithHeaders.headers['x-ar-io-stable'], 'false');
assert.equal(resWithHeaders.headers['x-ar-io-verified'], 'false');
});

it('Verifying that /<id> for a manifest with a missing index returns no hops, origin and node release', async function () {
Expand Down