@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
183 lines (182 loc) • 8.19 kB
JavaScript
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 CompositeQueryContinuationToken_exports = {};
__export(CompositeQueryContinuationToken_exports, {
convertRangeMappingToQueryRange: () => convertRangeMappingToQueryRange,
convertRangeMappingsToQueryRangesWithTokens: () => convertRangeMappingsToQueryRangesWithTokens,
convertSimplifiedRangeToQueryRange: () => convertSimplifiedRangeToQueryRange,
createCompositeQueryContinuationToken: () => createCompositeQueryContinuationToken,
parseBaseContinuationToken: () => parseBaseContinuationToken,
parseCompositeQueryContinuationToken: () => parseCompositeQueryContinuationToken,
serializeCompositeToken: () => serializeCompositeToken
});
module.exports = __toCommonJS(CompositeQueryContinuationToken_exports);
var import_QueryRange = require("../../routing/QueryRange.js");
var import_ErrorResponse = require("../../request/ErrorResponse.js");
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,
offset,
limit
};
}
function serializeCompositeToken(token) {
return JSON.stringify(token);
}
function parseCompositeQueryContinuationToken(tokenString) {
return parseBaseContinuationToken(tokenString);
}
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 !== void 0 && (!Number.isSafeInteger(candidate.offset) || candidate.offset < 0)) {
throw createInvalidContinuationTokenError("Token offset must be a non-negative safe integer.");
}
if (candidate.limit !== void 0 && (!Number.isSafeInteger(candidate.limit) || candidate.limit < 0)) {
throw createInvalidContinuationTokenError("Token limit must be a non-negative safe integer.");
}
const hashedLastResult = candidate.hashedLastResult;
if (hashedLastResult !== void 0 && 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 !== void 0 && mapping.continuationToken !== null && typeof mapping.continuationToken !== "string") {
throw createInvalidContinuationTokenError(
`Token rangeMappings[${index}].continuationToken must be a string when present.`
);
}
if (mapping.isActive !== void 0 && typeof mapping.isActive !== "boolean") {
throw createInvalidContinuationTokenError(
`Token rangeMappings[${index}].isActive must be a boolean when present.`
);
}
if (mapping.isOrderByCheckpoint !== void 0 && 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 import_ErrorResponse.ErrorResponse(`Invalid continuation token. ${message}`);
if (cause !== void 0) {
error.cause = cause;
}
return error;
}
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;
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 : void 0;
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
} : void 0
};
}
function convertRangeMappingsToQueryRangesWithTokens(rangeMappings) {
return rangeMappings.map((mapping) => convertRangeMappingToQueryRange(mapping));
}
function convertSimplifiedRangeToQueryRange(simplifiedRange) {
return new import_QueryRange.QueryRange(
simplifiedRange.min,
simplifiedRange.max,
true,
// minInclusive = true (assumption)
false
// maxInclusive = false (max is exclusive, assumption)
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
convertRangeMappingToQueryRange,
convertRangeMappingsToQueryRangesWithTokens,
convertSimplifiedRangeToQueryRange,
createCompositeQueryContinuationToken,
parseBaseContinuationToken,
parseCompositeQueryContinuationToken,
serializeCompositeToken
});
//# sourceMappingURL=CompositeQueryContinuationToken.js.map