yandex-cloud-functions-router
Version:
Node router for Yandex Cloud Functions
34 lines (33 loc) • 1.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.simpleRequestAllowedContentTypes = exports.simpleRequestAllowedHeaders = exports.simpleRequestAllowedMethods = exports.isRequestSimple = void 0;
const simpleRequestAllowedMethods = ['GET', 'HEAD', 'POST'];
exports.simpleRequestAllowedMethods = simpleRequestAllowedMethods;
const simpleRequestAllowedHeaders = [
'accept',
'accept-language',
'content-language',
'content-type',
'dpr',
'downlink',
'save-data',
'viewport-width',
'width',
'cookie',
'user-agent',
'origin'
];
exports.simpleRequestAllowedHeaders = simpleRequestAllowedHeaders;
const simpleRequestAllowedContentTypes = ['application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain'];
exports.simpleRequestAllowedContentTypes = simpleRequestAllowedContentTypes;
const isRequestSimple = (request) => {
var _a;
const isMethodValid = simpleRequestAllowedMethods.indexOf(request.httpMethod.toString().trim().toUpperCase()) !== -1;
const areHeadersValid = Object.keys(request.headers)
.map((h) => h.trim().toLowerCase())
.every((h) => simpleRequestAllowedHeaders.indexOf(h) !== -1);
const [, requestContentType] = (_a = Object.entries(request.headers).find(([name]) => name.trim().toLowerCase() === 'content-type')) !== null && _a !== void 0 ? _a : [];
const isContentTypeValid = !Boolean(requestContentType) || simpleRequestAllowedContentTypes.indexOf(requestContentType) !== -1;
return isMethodValid && areHeadersValid && isContentTypeValid;
};
exports.isRequestSimple = isRequestSimple;