UNPKG

@inngest/middleware-encryption

Version:
41 lines (40 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LEGACY_V0Service = exports.LEGACY_DEFAULT_EVENT_ENCRYPTION_FIELD = void 0; const middleware_1 = require("../middleware"); const aes_1 = require("./aes"); /** * The default field used to store encrypted data in events. */ exports.LEGACY_DEFAULT_EVENT_ENCRYPTION_FIELD = "encrypted"; class LEGACY_V0Service { constructor(options) { this.options = options; this.service = new aes_1.AESEncryptionService(this.options.key); } fieldShouldBeEncrypted(field) { if (typeof this.options.eventEncryptionField === "undefined") { return field === exports.LEGACY_DEFAULT_EVENT_ENCRYPTION_FIELD; } if (typeof this.options.eventEncryptionField === "function") { return this.options.eventEncryptionField(field); } if (Array.isArray(this.options.eventEncryptionField)) { return this.options.eventEncryptionField.includes(field); } return this.options.eventEncryptionField === field; } encryptEventData(data) { const encryptedData = Object.keys(data).reduce((acc, key) => { if (this.fieldShouldBeEncrypted(key)) { return Object.assign(Object.assign({}, acc), { [key]: { [middleware_1.EncryptionService.ENCRYPTION_MARKER]: true, data: this.service.encrypt(data[key]), } }); } return Object.assign(Object.assign({}, acc), { [key]: data[key] }); }, {}); return encryptedData; } } exports.LEGACY_V0Service = LEGACY_V0Service;