Skip to content

Commit

Permalink
fix(arns): return right away on arns HEAD requests
Browse files Browse the repository at this point in the history
Avoid fetching data when handling HEAD requests for arns names
  • Loading branch information
dtfiedler committed Sep 12, 2024
1 parent f765a76 commit ccaf35f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const headerNames = {
arnsResolvedId: 'X-ArNS-Resolved-Id',
arnsProcessId: 'X-ArNS-Process-Id',
};

export const REQUEST_METHOD_HEAD = 'HEAD';
export const DATA_PATH_REGEX =
/^\/?([a-zA-Z0-9-_]{43})\/?$|^\/?([a-zA-Z0-9-_]{43})\/(.*)$/i;
export const RAW_DATA_PATH_REGEX = /^\/raw\/([a-zA-Z0-9-_]{43})\/?$/i;
Expand Down
8 changes: 7 additions & 1 deletion src/middleware/arns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Handler } from 'express';
import { asyncMiddleware } from 'middleware-async';

import * as config from '../config.js';
import { headerNames } from '../constants.js';
import { headerNames, REQUEST_METHOD_HEAD } from '../constants.js';
import { sendNotFound } from '../routes/data/handlers.js';
import { DATA_PATH_REGEX } from '../constants.js';
import { NameResolution, NameResolver } from '../types.js';
Expand Down Expand Up @@ -105,5 +105,11 @@ export const createArnsMiddleware = ({
res.header(headerNames.arnsProcessId, processId);
// TODO: add a header for arns cache status
res.header('Cache-Control', `public, max-age=${ttl}`);

if (req.method === REQUEST_METHOD_HEAD) {
res.end();
return;
}

dataHandler(req, res, next);
});
4 changes: 1 addition & 3 deletions src/routes/data/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { default as asyncHandler } from 'express-async-handler';
import { PassThrough, Transform } from 'node:stream';
import rangeParser from 'range-parser';
import { Logger } from 'winston';
import { headerNames } from '../../constants.js';
import { headerNames, REQUEST_METHOD_HEAD } from '../../constants.js';

import { MANIFEST_CONTENT_TYPE } from '../../lib/encoding.js';
import {
Expand All @@ -39,8 +39,6 @@ const NOT_FOUND_MAX_AGE = 60; // 1 minute

const DEFAULT_CONTENT_TYPE = 'application/octet-stream';

const REQUEST_METHOD_HEAD = 'HEAD';

const setDataHeaders = ({
res,
dataAttributes,
Expand Down

0 comments on commit ccaf35f

Please sign in to comment.