botbuilder-core
Version:
Core components for Microsoft Bot Builder. Components in this library can run either in a browser or on the server.
89 lines • 3.26 kB
JavaScript
/**
* @module botbuilder
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
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;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateChangeHash = exports.assertStoreItems = void 0;
const z = __importStar(require("zod"));
const crypto_1 = require("crypto");
const botbuilder_stdlib_1 = require("botbuilder-stdlib");
const storeItems = z.record(z.unknown());
/**
* @internal
*
* @deprecated Use `zod.record(zod.unknown())` instead.
*/
function assertStoreItems(val, ..._args) {
storeItems.parse(val);
}
exports.assertStoreItems = assertStoreItems;
/**
* Utility function to calculate a change hash for a `StoreItem`.
*
* @remarks
* This example calculates a change hash for an object that's been read in and then only writes it
* back out if it's been modified:
*
* ```JavaScript
* // Calculate state objects initial hash
* const hash = calculateChangeHash(state);
*
* // Process the received activity
* await processActivity(context, state);
*
* // Save state if changed
* if (calculateChangeHash(state) !== hash) {
* await storage.write({ 'botState': state });
* }
* ```
* @param item Item to calculate the change hash for.
* @returns change hash string
*/
function calculateChangeHash(item) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { eTag } = item, rest = __rest(item, ["eTag"]);
const result = (0, botbuilder_stdlib_1.stringify)(rest);
const hash = (0, crypto_1.createHash)('sha256', { encoding: 'utf-8' });
const hashed = hash.update(result).digest('hex');
return hashed;
}
exports.calculateChangeHash = calculateChangeHash;
//# sourceMappingURL=storage.js.map
;