Repository URL to install this package:
Version:
0.6.0 ▾
|
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorHandler = exports.routerClosure = exports.requestFilter = exports.RedirectCidError = exports.NotEnabledError = void 0;
const swarmCid = __importStar(require("@ethersphere/swarm-cid"));
const logger_1 = require("./logger");
class NotEnabledError extends Error {
}
exports.NotEnabledError = NotEnabledError;
class RedirectCidError extends Error {
constructor(message, cid) {
super(message);
this.newUrl = cid;
}
}
exports.RedirectCidError = RedirectCidError;
/**
* Function that evaluates if the request was made with subdomain.
*
* @param pathname
* @param req
*/
function requestFilter(pathname, req) {
return req.subdomains.length >= 1;
}
exports.requestFilter = requestFilter;
/**
* Closure that routes subdomain CID/ENS to /bzz endpoint.
*
* @param target
* @param isCidEnabled
* @param isEnsEnabled
*/
function routerClosure(target, isCidEnabled, isEnsEnabled) {
return (req) => {
const bzzResource = subdomainToBzz(req, isCidEnabled, isEnsEnabled);
logger_1.logger.debug(`bzz link proxy`, { hostname: req.hostname, bzzResource });
return `${target}/bzz/${bzzResource}`;
};
}
exports.routerClosure = routerClosure;
/**
* Express error handler that handles BZZ link error cases.
*
* @param err
* @param req
* @param res
* @param next
*/
function errorHandler(err, req, res, next) {
if (res.headersSent) {
next(err);
return;
}
if (err instanceof NotEnabledError) {
res.writeHead(500).end(`Error 500: ${err.message}`);
return;
}
if (err instanceof RedirectCidError) {
// Using Permanently Moved HTTP code for redirection
res.redirect(301, err.newUrl);
return;
}
next(err);
}
exports.errorHandler = errorHandler;
/**
* Helper function that translates subdomain (CID/ENS) into Bzz resource
*
* @param req
* @param isCidEnabled
* @param isEnsEnabled
*/
function subdomainToBzz(req, isCidEnabled, isEnsEnabled) {
const host = req.hostname.split('.');
const subdomain = [...req.subdomains].reverse().join('.');
try {
const result = swarmCid.decodeCid(subdomain);
if (!isCidEnabled) {
logger_1.logger.warn('cid subdomain support disabled, but got cid', { subdomain });
throw new NotEnabledError('CID subdomain support is disabled, but got a CID!');
}
// We got old CID redirect to new one with proper multicodec
if (result.type === undefined) {
host[0] = swarmCid.encodeManifestReference(result.reference).toString();
const newUrl = `${req.protocol}://${host.join('.')}${req.originalUrl}`;
logger_1.logger.info(`redirecting to new cid`, newUrl);
throw new RedirectCidError('old CID format, redirect to new one', newUrl);
}
return result.reference;
}
catch (e) {
if (e instanceof NotEnabledError || e instanceof RedirectCidError) {
throw e;
}
if (!isEnsEnabled) {
logger_1.logger.warn('ens subdomain support disabled, but got ens', { subdomain });
throw new NotEnabledError('ENS subdomain support is disabled, but got an ENS domain!');
}
return `${subdomain}.eth`;
}
}