Skip to content

Commit

Permalink
refactor(sandbox, arns): switch params to config
Browse files Browse the repository at this point in the history
As both middlewares need the root host's subdomain count,
moving it to the config eliminates duplicates.

This also allows to remove the rootHost constructor param and take it
directly from the config.
  • Loading branch information
kay-is authored and djwhitt committed Sep 26, 2023
1 parent 0053e8a commit 89fa449
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
2 changes: 0 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,12 @@ if (config.ARNS_ROOT_HOST !== undefined) {
app.use(
createArnsMiddleware({
dataHandler,
rootHost: config.ARNS_ROOT_HOST,
nameResolver: system.nameResolver,
}),
);

app.use(
createSandboxMiddleware({
rootHost: config.ARNS_ROOT_HOST,
sandboxProtocol: config.SANDBOX_PROTOCOL,
}),
);
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export const ANS104_INDEX_FILTER = createFilter(
JSON.parse(ANS104_INDEX_FILTER_STRING),
);
export const ARNS_ROOT_HOST = env.varOrUndefined('ARNS_ROOT_HOST');
export const ROOT_HOST_SUBDOMAIN_LENGTH =
ARNS_ROOT_HOST !== undefined ? ARNS_ROOT_HOST.split('.').length - 2 : 0;
export const SANDBOX_PROTOCOL = env.varOrUndefined('SANDBOX_PROTOCOL');
export const START_WRITERS =
env.varOrDefault('START_WRITERS', 'true') === 'true';
8 changes: 3 additions & 5 deletions src/middleware/arns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,29 @@
import { Handler } from 'express';
import { asyncMiddleware } from 'middleware-async';

import * as config from '../config.js';
import { sendNotFound } from '../routes/data.js';
import { NameResolver } from '../types.js';

const EXCLUDED_SUBDOMAINS = new Set('www');

export const createArnsMiddleware = ({
dataHandler,
rootHost,
nameResolver,
}: {
dataHandler: Handler;
rootHost: string;
nameResolver: NameResolver;
}): Handler =>
asyncMiddleware(async (req, res, next) => {
const rootHostSubdomainLength = rootHost.split('.').length - 2;
if (
// Ignore subdomains that are part of the ArNS root hostname.
!Array.isArray(req.subdomains) ||
req.subdomains.length === rootHostSubdomainLength
req.subdomains.length === config.ROOT_HOST_SUBDOMAIN_LENGTH
) {
next();
return;
}
const arnsSubdomain = req.subdomains[req.subdomains.length - 1];
const arnsSubdomain = req.subdomains[config.ROOT_HOST_SUBDOMAIN_LENGTH - 1];
if (
EXCLUDED_SUBDOMAINS.has(arnsSubdomain) ||
// Avoid collisions with sandbox URLs by ensuring the subdomain length
Expand Down
11 changes: 5 additions & 6 deletions src/middleware/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import { Handler, Request } from 'express';
import url from 'node:url';
import { base32 } from 'rfc4648';

import * as config from '../config.js';
import { fromB64Url } from '../lib/encoding.js';

function getRequestSandbox(req: Request): string | undefined {
if (req.subdomains.length === 1) {
return req.subdomains[0];
if (req.subdomains.length > config.ROOT_HOST_SUBDOMAIN_LENGTH) {
return req.subdomains[req.subdomains.length - 1];
}
return undefined;
}
Expand All @@ -37,14 +38,12 @@ function sandboxFromId(id: string): string {
}

export function createSandboxMiddleware({
rootHost,
sandboxProtocol,
}: {
rootHost?: string;
sandboxProtocol?: string;
}): Handler {
return (req, res, next) => {
if (rootHost === undefined) {
if (config.ARNS_ROOT_HOST === undefined) {
next();
return;
}
Expand All @@ -63,7 +62,7 @@ export function createSandboxMiddleware({
const protocol = sandboxProtocol ?? (req.secure ? 'https' : 'http');
return res.redirect(
302,
`${protocol}://${idSandbox}.${rootHost}${path}?${queryString}`,
`${protocol}://${idSandbox}.${config.ARNS_ROOT_HOST}${path}?${queryString}`,
);
}

Expand Down

0 comments on commit 89fa449

Please sign in to comment.