UNPKG

@azure/cosmos

Version:
177 lines • 8 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. import { QueryRange } from "../../routing/QueryRange.js"; import { ErrorResponse } from "../../request/ErrorResponse.js"; /** * Creates a new CompositeQueryContinuationToken * @hidden */ export function createCompositeQueryContinuationToken(rid, rangeMappings, offset, limit) { if (!rangeMappings || rangeMappings.length === 0) { throw new Error("Failed to create composite continuation token: No partition range mappings provided. " + "This typically indicates an issue with query execution context initialization or partition key range resolution. " + "Ensure the query is properly configured and the container has valid partition ranges."); } return { rid, rangeMappings: rangeMappings, offset, limit, }; } /** * Serializes the composite continuation token to a JSON string * @hidden */ export function serializeCompositeToken(token) { return JSON.stringify(token); } /** * Deserializes a JSON string to a CompositeQueryContinuationToken * @hidden */ export function parseCompositeQueryContinuationToken(tokenString) { return parseBaseContinuationToken(tokenString); } /** * Deserializes a JSON string to a CompositeQueryContinuationToken * @hidden */ export function parseBaseContinuationToken(tokenString) { let token; try { token = JSON.parse(tokenString); } catch (error) { throw createInvalidContinuationTokenError("Token is not valid JSON.", error); } validateBaseContinuationToken(token); return token; } function validateBaseContinuationToken(token) { if (!token || typeof token !== "object" || Array.isArray(token)) { throw createInvalidContinuationTokenError("Token must be a JSON object."); } const candidate = token; if (typeof candidate.rid !== "string" || candidate.rid.length === 0) { throw createInvalidContinuationTokenError("Token rid must be a non-empty string."); } if (!Array.isArray(candidate.rangeMappings) || candidate.rangeMappings.length === 0) { throw createInvalidContinuationTokenError("Token rangeMappings must be a non-empty array."); } if (candidate.offset !== undefined && (!Number.isSafeInteger(candidate.offset) || candidate.offset < 0)) { throw createInvalidContinuationTokenError("Token offset must be a non-negative safe integer."); } if (candidate.limit !== undefined && (!Number.isSafeInteger(candidate.limit) || candidate.limit < 0)) { throw createInvalidContinuationTokenError("Token limit must be a non-negative safe integer."); } const hashedLastResult = candidate.hashedLastResult; if (hashedLastResult !== undefined && typeof hashedLastResult !== "string") { throw createInvalidContinuationTokenError("Token hashedLastResult must be a string when present."); } const ranges = candidate.rangeMappings.map((mapping, index) => { if (!mapping || typeof mapping !== "object" || Array.isArray(mapping)) { throw createInvalidContinuationTokenError(`Token rangeMappings[${index}] must be an object.`); } if (!mapping.queryRange || typeof mapping.queryRange !== "object" || Array.isArray(mapping.queryRange)) { throw createInvalidContinuationTokenError(`Token rangeMappings[${index}].queryRange is required.`); } const { min, max } = mapping.queryRange; if (typeof min !== "string" || typeof max !== "string") { throw createInvalidContinuationTokenError(`Token rangeMappings[${index}] boundaries must be strings.`); } if (!(min < max)) { throw createInvalidContinuationTokenError(`Token rangeMappings[${index}] must have min less than max.`); } if (mapping.continuationToken !== undefined && mapping.continuationToken !== null && typeof mapping.continuationToken !== "string") { throw createInvalidContinuationTokenError(`Token rangeMappings[${index}].continuationToken must be a string when present.`); } if (mapping.isActive !== undefined && typeof mapping.isActive !== "boolean") { throw createInvalidContinuationTokenError(`Token rangeMappings[${index}].isActive must be a boolean when present.`); } if (mapping.isOrderByCheckpoint !== undefined && typeof mapping.isOrderByCheckpoint !== "boolean") { throw createInvalidContinuationTokenError(`Token rangeMappings[${index}].isOrderByCheckpoint must be a boolean when present.`); } return { min, max }; }); ranges.sort((left, right) => left.min.localeCompare(right.min)); for (let index = 1; index < ranges.length; index++) { if (ranges[index].min < ranges[index - 1].max) { throw createInvalidContinuationTokenError("Token rangeMappings must not overlap."); } } } function createInvalidContinuationTokenError(message, cause) { const error = new ErrorResponse(`Invalid continuation token. ${message}`); if (cause !== undefined) { error.cause = cause; } return error; } /** * Converts QueryRangeMapping to QueryRangeWithContinuationToken using simplified range format * @param rangeMapping - The QueryRangeMapping to convert * @returns QueryRangeWithContinuationToken with simplified boundaries and continuation token * @hidden */ export function convertRangeMappingToQueryRange(rangeMapping) { if (!rangeMapping.partitionKeyRange) { throw new Error("Failed to convert range mapping: Missing partition key range information. " + "The QueryRangeMapping object must contain a valid partitionKeyRange with min and max boundaries. " + "This may indicate an incomplete partition key range resolution during query setup."); } const pkRange = rangeMapping.partitionKeyRange; // Create simplified range assuming min is inclusive and max is exclusive const simplifiedRange = { min: pkRange.minInclusive, max: pkRange.maxExclusive, }; const inferredActive = !!rangeMapping.continuationToken && rangeMapping.continuationToken.toLowerCase() !== "null"; const isActive = rangeMapping.isActive === false ? false : rangeMapping.isActive === true && (rangeMapping.continuationToken === rangeMapping.previousContinuationToken || !inferredActive) ? true : undefined; return { queryRange: simplifiedRange, continuationToken: rangeMapping.continuationToken, isActive, isOrderByCheckpoint: rangeMapping.isOrderByCheckpoint, orderByFilter: rangeMapping.orderByFilter ? { orderByItems: [...rangeMapping.orderByFilter.orderByItems], rid: rangeMapping.orderByFilter.rid, skipCount: rangeMapping.orderByFilter.skipCount, } : undefined, }; } /** * Converts an array of QueryRangeMapping to an array of QueryRangeWithContinuationToken * @param rangeMappings - Array of QueryRangeMapping to convert * @returns Array of QueryRangeWithContinuationToken with simplified boundaries and continuation tokens * @hidden */ export function convertRangeMappingsToQueryRangesWithTokens(rangeMappings) { return rangeMappings.map((mapping) => convertRangeMappingToQueryRange(mapping)); } /** * Converts a SimplifiedQueryRange back to a QueryRange for internal use * @param simplifiedRange - The simplified range to convert * @returns QueryRange with standard assumptions (min inclusive, max exclusive) * @hidden */ export function convertSimplifiedRangeToQueryRange(simplifiedRange) { return new QueryRange(simplifiedRange.min, simplifiedRange.max, true, // minInclusive = true (assumption) false); } //# sourceMappingURL=CompositeQueryContinuationToken.js.map