UNPKG

@azure/app-configuration

Version:
110 lines (109 loc) 3.95 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var syncTokenPolicy_exports = {}; __export(syncTokenPolicy_exports, { SyncTokenHeaderName: () => SyncTokenHeaderName, SyncTokens: () => SyncTokens, parseSyncToken: () => parseSyncToken, syncTokenPolicy: () => syncTokenPolicy }); module.exports = __toCommonJS(syncTokenPolicy_exports); var import_logger = require("../logger.js"); const SyncTokenHeaderName = "sync-token"; function syncTokenPolicy(syncTokens) { return { name: "Sync Token Policy", async sendRequest(request, next) { const syncTokenHeaderValue = syncTokens.getSyncTokenHeaderValue(); if (syncTokenHeaderValue) { import_logger.logger.info( `[syncTokenPolicy] Setting headers with ${SyncTokenHeaderName} and ${syncTokenHeaderValue}` ); request.headers.set(SyncTokenHeaderName, syncTokenHeaderValue); } const response = await next(request); syncTokens.addSyncTokenFromHeaderValue(response.headers.get(SyncTokenHeaderName)); return response; } }; } class SyncTokens { _currentSyncTokens = /* @__PURE__ */ new Map(); /** * Takes the value from the header named after the constant `SyncTokenHeaderName` * and adds it to our list of accumulated sync tokens. * * If given an empty value (or undefined) it clears the current list of sync tokens. * (indicates the service has properly absorbed values into the cluster). * * @param syncTokenHeaderValue - The full value of the sync token header. */ addSyncTokenFromHeaderValue(syncTokenHeaderValue) { if (syncTokenHeaderValue == null || syncTokenHeaderValue === "") { this._currentSyncTokens.clear(); return; } const newTokens = syncTokenHeaderValue.split(",").map(parseSyncToken); for (const newToken of newTokens) { const existingToken = this._currentSyncTokens.get(newToken.id); if (!existingToken || existingToken.sequenceNumber < newToken.sequenceNumber) { this._currentSyncTokens.set(newToken.id, newToken); continue; } } } /** * Gets a properly formatted SyncToken header value. */ getSyncTokenHeaderValue() { if (this._currentSyncTokens.size === 0) { return void 0; } const syncTokenStrings = []; for (const syncToken of this._currentSyncTokens.values()) { syncTokenStrings.push(`${syncToken.id}=${syncToken.value}`); } return syncTokenStrings.join(","); } } const syncTokenRegex = /^([^=]+)=([^;]+);sn=(\d+)$/; function parseSyncToken(syncToken) { const matches = syncToken.match(syncTokenRegex); if (matches == null) { throw new Error( `Failed to parse sync token '${syncToken}' with regex ${syncTokenRegex.source}` ); } const sequenceNumber = parseInt(matches[3], 10); if (isNaN(sequenceNumber)) { throw new Error(`${syncToken}: The sequence number value '${matches[3]}' wasn't a number`); } return { id: matches[1], value: matches[2], sequenceNumber }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { SyncTokenHeaderName, SyncTokens, parseSyncToken, syncTokenPolicy }); //# sourceMappingURL=syncTokenPolicy.js.map