yandex-cloud-functions-router
Version:
Node router for Yandex Cloud Functions
79 lines (78 loc) • 3.77 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.objectStorageRouter = void 0;
const log_1 = require("../helpers/log");
const routerError_1 = require("../models/routerError");
const validateType = (context, types, message) => {
if (types) {
const result = (types.some((type) => type === 'create') && message.event_metadata.event_type === 'yandex.cloud.events.storage.ObjectCreate') ||
(types.some((type) => type === 'update') && message.event_metadata.event_type === 'yandex.cloud.events.storage.ObjectUpdate') ||
(types.some((type) => type === 'delete') && message.event_metadata.event_type === 'yandex.cloud.events.storage.ObjectDelete');
log_1.debug(context.requestId, `Validating type: ${result ? 'valid' : 'invalid'}`, {
request: message.event_metadata.event_type,
route: types
});
return result;
}
else {
return true;
}
};
const validateBucketId = (context, bucketIds, message) => {
if (bucketIds) {
const result = bucketIds.some((bucketId) => bucketId === message.details.bucket_id);
log_1.debug(context.requestId, `Validating bucket id: ${result ? 'valid' : 'invalid'}`, {
request: message.details.bucket_id,
route: bucketIds
});
return result;
}
else {
return true;
}
};
const validateObjectId = (context, objectIds, message) => {
if (objectIds) {
const result = objectIds.some((objectId) => objectId === message.details.object_id);
log_1.debug(context.requestId, `Validating object id: ${result ? 'valid' : 'invalid'}`, {
request: message.details.object_id,
route: objectIds
});
return result;
}
else {
return true;
}
};
const objectStorageRouter = (routes, event, message, context) => __awaiter(void 0, void 0, void 0, function* () {
log_1.debug(context.requestId, 'Object storage processing started', {});
for (const { type, bucketId, objectId, handler } of routes) {
const matched = validateType(context, type, message) &&
validateBucketId(context, bucketId, message) &&
validateObjectId(context, objectId, message);
log_1.debug(context.requestId, 'Object storage matching completed', {
matched,
message,
type: type !== null && type !== void 0 ? type : '',
bucketId: bucketId !== null && bucketId !== void 0 ? bucketId : '',
objectId: objectId !== null && objectId !== void 0 ? objectId : ''
});
if (matched) {
const result = handler(event, context, message);
log_1.debug(context.requestId, 'Object storage processed', {});
return result instanceof Promise ? result : Promise.resolve(result);
}
}
log_1.log('WARN', context.requestId, 'There is no matched route', {});
throw new routerError_1.NoMatchedRouteError('There is no matched route.');
});
exports.objectStorageRouter = objectStorageRouter;