unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
35 lines • 1.14 kB
JavaScript
import cors from 'cors';
export const resolveOrigin = (allowedOrigins) => {
if (allowedOrigins.length === 0) {
return '*';
}
if (allowedOrigins.some((origin) => origin === '*')) {
return '*';
}
else {
return allowedOrigins;
}
};
// Check the request's Origin header against a list of allowed origins.
// The list may include '*', which `cors` does not support natively.
export const corsOriginMiddleware = ({ frontendApiService }, config) => {
const corsFunc = cors(async (_req, callback) => {
try {
const { frontendApiOrigins = [] } = await frontendApiService.getFrontendSettings();
callback(null, {
origin: resolveOrigin(frontendApiOrigins),
maxAge: config.accessControlMaxAge,
exposedHeaders: 'ETag',
credentials: true,
});
}
catch (error) {
callback(error);
}
});
return (req, res, next) => {
res.setHeader('Vary', 'Origin');
corsFunc(req, res, next);
};
};
//# sourceMappingURL=cors-origin-middleware.js.map