UNPKG

@azure/cosmos

Version:
1,114 lines (1,100 loc) • 372 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var crypto = require('crypto'); var uuid$3 = require('uuid'); var tslib = require('tslib'); var logger$4 = require('@azure/logger'); var stableStringify = require('fast-json-stable-stringify'); var PriorityQueue = require('priorityqueuejs'); var semaphore = require('semaphore'); var coreRestPipeline = require('@azure/core-rest-pipeline'); var nodeAbortController = require('node-abort-controller'); var universalUserAgent = require('universal-user-agent'); var JSBI = require('jsbi'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var stableStringify__default = /*#__PURE__*/_interopDefaultLegacy(stableStringify); var PriorityQueue__default = /*#__PURE__*/_interopDefaultLegacy(PriorityQueue); var semaphore__default = /*#__PURE__*/_interopDefaultLegacy(semaphore); var JSBI__default = /*#__PURE__*/_interopDefaultLegacy(JSBI); // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. const DEFAULT_PARTITION_KEY_PATH = "/_partitionKey"; // eslint-disable-line @typescript-eslint/prefer-as-const // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. /** * @hidden */ const Constants = { HttpHeaders: { Authorization: "authorization", ETag: "etag", MethodOverride: "X-HTTP-Method", Slug: "Slug", ContentType: "Content-Type", LastModified: "Last-Modified", ContentEncoding: "Content-Encoding", CharacterSet: "CharacterSet", UserAgent: "User-Agent", IfModifiedSince: "If-Modified-Since", IfMatch: "If-Match", IfNoneMatch: "If-None-Match", ContentLength: "Content-Length", AcceptEncoding: "Accept-Encoding", KeepAlive: "Keep-Alive", CacheControl: "Cache-Control", TransferEncoding: "Transfer-Encoding", ContentLanguage: "Content-Language", ContentLocation: "Content-Location", ContentMd5: "Content-Md5", ContentRange: "Content-Range", Accept: "Accept", AcceptCharset: "Accept-Charset", AcceptLanguage: "Accept-Language", IfRange: "If-Range", IfUnmodifiedSince: "If-Unmodified-Since", MaxForwards: "Max-Forwards", ProxyAuthorization: "Proxy-Authorization", AcceptRanges: "Accept-Ranges", ProxyAuthenticate: "Proxy-Authenticate", RetryAfter: "Retry-After", SetCookie: "Set-Cookie", WwwAuthenticate: "Www-Authenticate", Origin: "Origin", Host: "Host", AccessControlAllowOrigin: "Access-Control-Allow-Origin", AccessControlAllowHeaders: "Access-Control-Allow-Headers", KeyValueEncodingFormat: "application/x-www-form-urlencoded", WrapAssertionFormat: "wrap_assertion_format", WrapAssertion: "wrap_assertion", WrapScope: "wrap_scope", SimpleToken: "SWT", HttpDate: "date", Prefer: "Prefer", Location: "Location", Referer: "referer", A_IM: "A-IM", // Query Query: "x-ms-documentdb-query", IsQuery: "x-ms-documentdb-isquery", IsQueryPlan: "x-ms-cosmos-is-query-plan-request", SupportedQueryFeatures: "x-ms-cosmos-supported-query-features", QueryVersion: "x-ms-cosmos-query-version", // Our custom Azure Cosmos DB headers Continuation: "x-ms-continuation", PageSize: "x-ms-max-item-count", ItemCount: "x-ms-item-count", // Request sender generated. Simply echoed by backend. ActivityId: "x-ms-activity-id", PreTriggerInclude: "x-ms-documentdb-pre-trigger-include", PreTriggerExclude: "x-ms-documentdb-pre-trigger-exclude", PostTriggerInclude: "x-ms-documentdb-post-trigger-include", PostTriggerExclude: "x-ms-documentdb-post-trigger-exclude", IndexingDirective: "x-ms-indexing-directive", SessionToken: "x-ms-session-token", ConsistencyLevel: "x-ms-consistency-level", XDate: "x-ms-date", CollectionPartitionInfo: "x-ms-collection-partition-info", CollectionServiceInfo: "x-ms-collection-service-info", // Deprecated, use RetryAfterInMs instead. RetryAfterInMilliseconds: "x-ms-retry-after-ms", RetryAfterInMs: "x-ms-retry-after-ms", IsFeedUnfiltered: "x-ms-is-feed-unfiltered", ResourceTokenExpiry: "x-ms-documentdb-expiry-seconds", EnableScanInQuery: "x-ms-documentdb-query-enable-scan", EmitVerboseTracesInQuery: "x-ms-documentdb-query-emit-traces", EnableCrossPartitionQuery: "x-ms-documentdb-query-enablecrosspartition", ParallelizeCrossPartitionQuery: "x-ms-documentdb-query-parallelizecrosspartitionquery", ResponseContinuationTokenLimitInKB: "x-ms-documentdb-responsecontinuationtokenlimitinkb", // QueryMetrics // Request header to tell backend to give you query metrics. PopulateQueryMetrics: "x-ms-documentdb-populatequerymetrics", // Response header that holds the serialized version of query metrics. QueryMetrics: "x-ms-documentdb-query-metrics", // Version headers and values Version: "x-ms-version", // Owner name OwnerFullName: "x-ms-alt-content-path", // Owner ID used for name based request in session token. OwnerId: "x-ms-content-path", // Partition Key PartitionKey: "x-ms-documentdb-partitionkey", PartitionKeyRangeID: "x-ms-documentdb-partitionkeyrangeid", // Quota Info MaxEntityCount: "x-ms-root-entity-max-count", CurrentEntityCount: "x-ms-root-entity-current-count", CollectionQuotaInMb: "x-ms-collection-quota-mb", CollectionCurrentUsageInMb: "x-ms-collection-usage-mb", MaxMediaStorageUsageInMB: "x-ms-max-media-storage-usage-mb", CurrentMediaStorageUsageInMB: "x-ms-media-storage-usage-mb", RequestCharge: "x-ms-request-charge", PopulateQuotaInfo: "x-ms-documentdb-populatequotainfo", MaxResourceQuota: "x-ms-resource-quota", // Offer header OfferType: "x-ms-offer-type", OfferThroughput: "x-ms-offer-throughput", AutoscaleSettings: "x-ms-cosmos-offer-autopilot-settings", // Custom RUs/minute headers DisableRUPerMinuteUsage: "x-ms-documentdb-disable-ru-per-minute-usage", IsRUPerMinuteUsed: "x-ms-documentdb-is-ru-per-minute-used", OfferIsRUPerMinuteThroughputEnabled: "x-ms-offer-is-ru-per-minute-throughput-enabled", // Index progress headers IndexTransformationProgress: "x-ms-documentdb-collection-index-transformation-progress", LazyIndexingProgress: "x-ms-documentdb-collection-lazy-indexing-progress", // Upsert header IsUpsert: "x-ms-documentdb-is-upsert", // Sub status of the error SubStatus: "x-ms-substatus", // StoredProcedure related headers EnableScriptLogging: "x-ms-documentdb-script-enable-logging", ScriptLogResults: "x-ms-documentdb-script-log-results", // Multi-Region Write ALLOW_MULTIPLE_WRITES: "x-ms-cosmos-allow-tentative-writes", // Bulk/Batch header IsBatchRequest: "x-ms-cosmos-is-batch-request", IsBatchAtomic: "x-ms-cosmos-batch-atomic", BatchContinueOnError: "x-ms-cosmos-batch-continue-on-error", // Dedicated Gateway Headers DedicatedGatewayPerRequestCacheStaleness: "x-ms-dedicatedgateway-max-age", // Cache Refresh header ForceRefresh: "x-ms-force-refresh", }, // GlobalDB related constants WritableLocations: "writableLocations", ReadableLocations: "readableLocations", // ServiceDocument Resource ENABLE_MULTIPLE_WRITABLE_LOCATIONS: "enableMultipleWriteLocations", // Background refresh time DefaultUnavailableLocationExpirationTimeMS: 5 * 60 * 1000, // Client generated retry count response header ThrottleRetryCount: "x-ms-throttle-retry-count", ThrottleRetryWaitTimeInMs: "x-ms-throttle-retry-wait-time-ms", // Platform CurrentVersion: "2020-07-15", AzureNamespace: "Azure.Cosmos", AzurePackageName: "@azure/cosmos", SDKName: "azure-cosmos-js", SDKVersion: "3.17.2", Quota: { CollectionSize: "collectionSize", }, Path: { Root: "/", DatabasesPathSegment: "dbs", CollectionsPathSegment: "colls", UsersPathSegment: "users", DocumentsPathSegment: "docs", PermissionsPathSegment: "permissions", StoredProceduresPathSegment: "sprocs", TriggersPathSegment: "triggers", UserDefinedFunctionsPathSegment: "udfs", ConflictsPathSegment: "conflicts", AttachmentsPathSegment: "attachments", PartitionKeyRangesPathSegment: "pkranges", SchemasPathSegment: "schemas", OffersPathSegment: "offers", TopologyPathSegment: "topology", DatabaseAccountPathSegment: "databaseaccount", }, PartitionKeyRange: { // Partition Key Range Constants MinInclusive: "minInclusive", MaxExclusive: "maxExclusive", Id: "id", }, QueryRangeConstants: { // Partition Key Range Constants MinInclusive: "minInclusive", MaxExclusive: "maxExclusive", min: "min", }, /** * @deprecated Use EffectivePartitionKeyConstants instead */ EffectiveParitionKeyConstants: { MinimumInclusiveEffectivePartitionKey: "", MaximumExclusiveEffectivePartitionKey: "FF", }, EffectivePartitionKeyConstants: { MinimumInclusiveEffectivePartitionKey: "", MaximumExclusiveEffectivePartitionKey: "FF", }, }; /** * @hidden */ exports.ResourceType = void 0; (function (ResourceType) { ResourceType["none"] = ""; ResourceType["database"] = "dbs"; ResourceType["offer"] = "offers"; ResourceType["user"] = "users"; ResourceType["permission"] = "permissions"; ResourceType["container"] = "colls"; ResourceType["conflicts"] = "conflicts"; ResourceType["sproc"] = "sprocs"; ResourceType["udf"] = "udfs"; ResourceType["trigger"] = "triggers"; ResourceType["item"] = "docs"; ResourceType["pkranges"] = "pkranges"; })(exports.ResourceType || (exports.ResourceType = {})); /** * @hidden */ exports.HTTPMethod = void 0; (function (HTTPMethod) { HTTPMethod["get"] = "GET"; HTTPMethod["patch"] = "PATCH"; HTTPMethod["post"] = "POST"; HTTPMethod["put"] = "PUT"; HTTPMethod["delete"] = "DELETE"; })(exports.HTTPMethod || (exports.HTTPMethod = {})); /** * @hidden */ exports.OperationType = void 0; (function (OperationType) { OperationType["Create"] = "create"; OperationType["Replace"] = "replace"; OperationType["Upsert"] = "upsert"; OperationType["Delete"] = "delete"; OperationType["Read"] = "read"; OperationType["Query"] = "query"; OperationType["Execute"] = "execute"; OperationType["Batch"] = "batch"; OperationType["Patch"] = "patch"; })(exports.OperationType || (exports.OperationType = {})); /** * @hidden */ var CosmosKeyType; (function (CosmosKeyType) { CosmosKeyType["PrimaryMaster"] = "PRIMARY_MASTER"; CosmosKeyType["SecondaryMaster"] = "SECONDARY_MASTER"; CosmosKeyType["PrimaryReadOnly"] = "PRIMARY_READONLY"; CosmosKeyType["SecondaryReadOnly"] = "SECONDARY_READONLY"; })(CosmosKeyType || (CosmosKeyType = {})); /** * @hidden */ var CosmosContainerChildResourceKind; (function (CosmosContainerChildResourceKind) { CosmosContainerChildResourceKind["Item"] = "ITEM"; CosmosContainerChildResourceKind["StoredProcedure"] = "STORED_PROCEDURE"; CosmosContainerChildResourceKind["UserDefinedFunction"] = "USER_DEFINED_FUNCTION"; CosmosContainerChildResourceKind["Trigger"] = "TRIGGER"; })(CosmosContainerChildResourceKind || (CosmosContainerChildResourceKind = {})); /** * @hidden */ var PermissionScopeValues; (function (PermissionScopeValues) { /** * Values which set permission Scope applicable to control plane related operations. */ PermissionScopeValues[PermissionScopeValues["ScopeAccountReadValue"] = 1] = "ScopeAccountReadValue"; PermissionScopeValues[PermissionScopeValues["ScopeAccountListDatabasesValue"] = 2] = "ScopeAccountListDatabasesValue"; PermissionScopeValues[PermissionScopeValues["ScopeDatabaseReadValue"] = 4] = "ScopeDatabaseReadValue"; PermissionScopeValues[PermissionScopeValues["ScopeDatabaseReadOfferValue"] = 8] = "ScopeDatabaseReadOfferValue"; PermissionScopeValues[PermissionScopeValues["ScopeDatabaseListContainerValue"] = 16] = "ScopeDatabaseListContainerValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReadValue"] = 32] = "ScopeContainerReadValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReadOfferValue"] = 64] = "ScopeContainerReadOfferValue"; PermissionScopeValues[PermissionScopeValues["ScopeAccountCreateDatabasesValue"] = 1] = "ScopeAccountCreateDatabasesValue"; PermissionScopeValues[PermissionScopeValues["ScopeAccountDeleteDatabasesValue"] = 2] = "ScopeAccountDeleteDatabasesValue"; PermissionScopeValues[PermissionScopeValues["ScopeDatabaseDeleteValue"] = 4] = "ScopeDatabaseDeleteValue"; PermissionScopeValues[PermissionScopeValues["ScopeDatabaseReplaceOfferValue"] = 8] = "ScopeDatabaseReplaceOfferValue"; PermissionScopeValues[PermissionScopeValues["ScopeDatabaseCreateContainerValue"] = 16] = "ScopeDatabaseCreateContainerValue"; PermissionScopeValues[PermissionScopeValues["ScopeDatabaseDeleteContainerValue"] = 32] = "ScopeDatabaseDeleteContainerValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReplaceValue"] = 64] = "ScopeContainerReplaceValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerDeleteValue"] = 128] = "ScopeContainerDeleteValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReplaceOfferValue"] = 256] = "ScopeContainerReplaceOfferValue"; PermissionScopeValues[PermissionScopeValues["ScopeAccountReadAllAccessValue"] = 65535] = "ScopeAccountReadAllAccessValue"; PermissionScopeValues[PermissionScopeValues["ScopeDatabaseReadAllAccessValue"] = 124] = "ScopeDatabaseReadAllAccessValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainersReadAllAccessValue"] = 96] = "ScopeContainersReadAllAccessValue"; PermissionScopeValues[PermissionScopeValues["ScopeAccountWriteAllAccessValue"] = 65535] = "ScopeAccountWriteAllAccessValue"; PermissionScopeValues[PermissionScopeValues["ScopeDatabaseWriteAllAccessValue"] = 508] = "ScopeDatabaseWriteAllAccessValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainersWriteAllAccessValue"] = 448] = "ScopeContainersWriteAllAccessValue"; /** * Values which set permission Scope applicable to data plane related operations. */ PermissionScopeValues[PermissionScopeValues["ScopeContainerExecuteQueriesValue"] = 1] = "ScopeContainerExecuteQueriesValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReadFeedsValue"] = 2] = "ScopeContainerReadFeedsValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReadStoredProceduresValue"] = 4] = "ScopeContainerReadStoredProceduresValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReadUserDefinedFunctionsValue"] = 8] = "ScopeContainerReadUserDefinedFunctionsValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReadTriggersValue"] = 16] = "ScopeContainerReadTriggersValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReadConflictsValue"] = 32] = "ScopeContainerReadConflictsValue"; PermissionScopeValues[PermissionScopeValues["ScopeItemReadValue"] = 64] = "ScopeItemReadValue"; PermissionScopeValues[PermissionScopeValues["ScopeStoredProcedureReadValue"] = 128] = "ScopeStoredProcedureReadValue"; PermissionScopeValues[PermissionScopeValues["ScopeUserDefinedFunctionReadValue"] = 256] = "ScopeUserDefinedFunctionReadValue"; PermissionScopeValues[PermissionScopeValues["ScopeTriggerReadValue"] = 512] = "ScopeTriggerReadValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerCreateItemsValue"] = 1] = "ScopeContainerCreateItemsValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReplaceItemsValue"] = 2] = "ScopeContainerReplaceItemsValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerUpsertItemsValue"] = 4] = "ScopeContainerUpsertItemsValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerDeleteItemsValue"] = 8] = "ScopeContainerDeleteItemsValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerCreateStoredProceduresValue"] = 16] = "ScopeContainerCreateStoredProceduresValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReplaceStoredProceduresValue"] = 32] = "ScopeContainerReplaceStoredProceduresValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerDeleteStoredProceduresValue"] = 64] = "ScopeContainerDeleteStoredProceduresValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerExecuteStoredProceduresValue"] = 128] = "ScopeContainerExecuteStoredProceduresValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerCreateTriggersValue"] = 256] = "ScopeContainerCreateTriggersValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReplaceTriggersValue"] = 512] = "ScopeContainerReplaceTriggersValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerDeleteTriggersValue"] = 1024] = "ScopeContainerDeleteTriggersValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerCreateUserDefinedFunctionsValue"] = 2048] = "ScopeContainerCreateUserDefinedFunctionsValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReplaceUserDefinedFunctionsValue"] = 4096] = "ScopeContainerReplaceUserDefinedFunctionsValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerDeleteUserDefinedFunctionSValue"] = 8192] = "ScopeContainerDeleteUserDefinedFunctionSValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerDeleteCONFLICTSValue"] = 16384] = "ScopeContainerDeleteCONFLICTSValue"; PermissionScopeValues[PermissionScopeValues["ScopeItemReplaceValue"] = 65536] = "ScopeItemReplaceValue"; PermissionScopeValues[PermissionScopeValues["ScopeItemUpsertValue"] = 131072] = "ScopeItemUpsertValue"; PermissionScopeValues[PermissionScopeValues["ScopeItemDeleteValue"] = 262144] = "ScopeItemDeleteValue"; PermissionScopeValues[PermissionScopeValues["ScopeStoredProcedureReplaceValue"] = 1048576] = "ScopeStoredProcedureReplaceValue"; PermissionScopeValues[PermissionScopeValues["ScopeStoredProcedureDeleteValue"] = 2097152] = "ScopeStoredProcedureDeleteValue"; PermissionScopeValues[PermissionScopeValues["ScopeStoredProcedureExecuteValue"] = 4194304] = "ScopeStoredProcedureExecuteValue"; PermissionScopeValues[PermissionScopeValues["ScopeUserDefinedFunctionReplaceValue"] = 8388608] = "ScopeUserDefinedFunctionReplaceValue"; PermissionScopeValues[PermissionScopeValues["ScopeUserDefinedFunctionDeleteValue"] = 16777216] = "ScopeUserDefinedFunctionDeleteValue"; PermissionScopeValues[PermissionScopeValues["ScopeTriggerReplaceValue"] = 33554432] = "ScopeTriggerReplaceValue"; PermissionScopeValues[PermissionScopeValues["ScopeTriggerDeleteValue"] = 67108864] = "ScopeTriggerDeleteValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerReadAllAccessValue"] = 4294967295] = "ScopeContainerReadAllAccessValue"; PermissionScopeValues[PermissionScopeValues["ScopeItemReadAllAccessValue"] = 65] = "ScopeItemReadAllAccessValue"; PermissionScopeValues[PermissionScopeValues["ScopeContainerWriteAllAccessValue"] = 4294967295] = "ScopeContainerWriteAllAccessValue"; PermissionScopeValues[PermissionScopeValues["ScopeItemWriteAllAccessValue"] = 458767] = "ScopeItemWriteAllAccessValue"; PermissionScopeValues[PermissionScopeValues["NoneValue"] = 0] = "NoneValue"; })(PermissionScopeValues || (PermissionScopeValues = {})); /** * @hidden */ exports.SasTokenPermissionKind = void 0; (function (SasTokenPermissionKind) { SasTokenPermissionKind[SasTokenPermissionKind["ContainerCreateItems"] = 1] = "ContainerCreateItems"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerReplaceItems"] = 2] = "ContainerReplaceItems"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerUpsertItems"] = 4] = "ContainerUpsertItems"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerDeleteItems"] = 128] = "ContainerDeleteItems"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerExecuteQueries"] = 1] = "ContainerExecuteQueries"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerReadFeeds"] = 2] = "ContainerReadFeeds"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerCreateStoreProcedure"] = 16] = "ContainerCreateStoreProcedure"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerReadStoreProcedure"] = 4] = "ContainerReadStoreProcedure"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerReplaceStoreProcedure"] = 32] = "ContainerReplaceStoreProcedure"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerDeleteStoreProcedure"] = 64] = "ContainerDeleteStoreProcedure"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerCreateTriggers"] = 256] = "ContainerCreateTriggers"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerReadTriggers"] = 16] = "ContainerReadTriggers"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerReplaceTriggers"] = 512] = "ContainerReplaceTriggers"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerDeleteTriggers"] = 1024] = "ContainerDeleteTriggers"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerCreateUserDefinedFunctions"] = 2048] = "ContainerCreateUserDefinedFunctions"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerReadUserDefinedFunctions"] = 8] = "ContainerReadUserDefinedFunctions"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerReplaceUserDefinedFunctions"] = 4096] = "ContainerReplaceUserDefinedFunctions"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerDeleteUserDefinedFunctions"] = 8192] = "ContainerDeleteUserDefinedFunctions"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerExecuteStoredProcedure"] = 128] = "ContainerExecuteStoredProcedure"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerReadConflicts"] = 32] = "ContainerReadConflicts"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerDeleteConflicts"] = 16384] = "ContainerDeleteConflicts"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerReadAny"] = 64] = "ContainerReadAny"; SasTokenPermissionKind[SasTokenPermissionKind["ContainerFullAccess"] = 4294967295] = "ContainerFullAccess"; SasTokenPermissionKind[SasTokenPermissionKind["ItemReadAny"] = 65536] = "ItemReadAny"; SasTokenPermissionKind[SasTokenPermissionKind["ItemFullAccess"] = 65] = "ItemFullAccess"; SasTokenPermissionKind[SasTokenPermissionKind["ItemRead"] = 64] = "ItemRead"; SasTokenPermissionKind[SasTokenPermissionKind["ItemReplace"] = 65536] = "ItemReplace"; SasTokenPermissionKind[SasTokenPermissionKind["ItemUpsert"] = 131072] = "ItemUpsert"; SasTokenPermissionKind[SasTokenPermissionKind["ItemDelete"] = 262144] = "ItemDelete"; SasTokenPermissionKind[SasTokenPermissionKind["StoreProcedureRead"] = 128] = "StoreProcedureRead"; SasTokenPermissionKind[SasTokenPermissionKind["StoreProcedureReplace"] = 1048576] = "StoreProcedureReplace"; SasTokenPermissionKind[SasTokenPermissionKind["StoreProcedureDelete"] = 2097152] = "StoreProcedureDelete"; SasTokenPermissionKind[SasTokenPermissionKind["StoreProcedureExecute"] = 4194304] = "StoreProcedureExecute"; SasTokenPermissionKind[SasTokenPermissionKind["UserDefinedFuntionRead"] = 256] = "UserDefinedFuntionRead"; SasTokenPermissionKind[SasTokenPermissionKind["UserDefinedFuntionReplace"] = 8388608] = "UserDefinedFuntionReplace"; SasTokenPermissionKind[SasTokenPermissionKind["UserDefinedFuntionDelete"] = 16777216] = "UserDefinedFuntionDelete"; SasTokenPermissionKind[SasTokenPermissionKind["TriggerRead"] = 512] = "TriggerRead"; SasTokenPermissionKind[SasTokenPermissionKind["TriggerReplace"] = 33554432] = "TriggerReplace"; SasTokenPermissionKind[SasTokenPermissionKind["TriggerDelete"] = 67108864] = "TriggerDelete"; })(exports.SasTokenPermissionKind || (exports.SasTokenPermissionKind = {})); const trimLeftSlashes = new RegExp("^[/]+"); const trimRightSlashes = new RegExp("[/]+$"); const illegalResourceIdCharacters = new RegExp("[/\\\\?#]"); const illegalItemResourceIdCharacters = new RegExp("[/\\\\#]"); /** @hidden */ function jsonStringifyAndEscapeNonASCII(arg) { // TODO: better way for this? Not sure. // escapes non-ASCII characters as \uXXXX return JSON.stringify(arg).replace(/[\u007F-\uFFFF]/g, (m) => { return "\\u" + ("0000" + m.charCodeAt(0).toString(16)).slice(-4); }); } /** * @hidden */ function parseLink(resourcePath) { if (resourcePath.length === 0) { /* for DatabaseAccount case, both type and objectBody will be undefined. */ return { type: undefined, objectBody: undefined, }; } if (resourcePath[resourcePath.length - 1] !== "/") { resourcePath = resourcePath + "/"; } if (resourcePath[0] !== "/") { resourcePath = "/" + resourcePath; } /* The path will be in the form of /[resourceType]/[resourceId]/ .... /[resourceType]//[resourceType]/[resourceId]/ .... /[resourceType]/[resourceId]/ or /[resourceType]/[resourceId]/ .... /[resourceType]/[resourceId]/[resourceType]/[resourceId]/ .... /[resourceType]/[resourceId]/ The result of split will be in the form of [[[resourceType], [resourceId] ... ,[resourceType], [resourceId], ""] In the first case, to extract the resourceId it will the element before last ( at length -2 ) and the type will be before it ( at length -3 ) In the second case, to extract the resource type it will the element before last ( at length -2 ) */ const pathParts = resourcePath.split("/"); let id; let type; if (pathParts.length % 2 === 0) { // request in form /[resourceType]/[resourceId]/ .... /[resourceType]/[resourceId]. id = pathParts[pathParts.length - 2]; type = pathParts[pathParts.length - 3]; } else { // request in form /[resourceType]/[resourceId]/ .... /[resourceType]/. id = pathParts[pathParts.length - 3]; type = pathParts[pathParts.length - 2]; } const result = { type, objectBody: { id, self: resourcePath, }, }; return result; } /** * @hidden */ function isReadRequest(operationType) { return operationType === exports.OperationType.Read || operationType === exports.OperationType.Query; } /** * @hidden */ function sleep(time) { return new Promise((resolve) => { setTimeout(() => { resolve(); }, time); }); } /** * @hidden */ function getContainerLink(link) { return link.split("/").slice(0, 4).join("/"); } /** * @hidden */ function trimSlashes(source) { return source.replace(trimLeftSlashes, "").replace(trimRightSlashes, ""); } /** * @hidden */ function parsePath(path) { const pathParts = []; let currentIndex = 0; const throwError = () => { throw new Error("Path " + path + " is invalid at index " + currentIndex); }; const getEscapedToken = () => { const quote = path[currentIndex]; let newIndex = ++currentIndex; for (;;) { newIndex = path.indexOf(quote, newIndex); if (newIndex === -1) { throwError(); } if (path[newIndex - 1] !== "\\") { break; } ++newIndex; } const token = path.substr(currentIndex, newIndex - currentIndex); currentIndex = newIndex + 1; return token; }; const getToken = () => { const newIndex = path.indexOf("/", currentIndex); let token = null; if (newIndex === -1) { token = path.substr(currentIndex); currentIndex = path.length; } else { token = path.substr(currentIndex, newIndex - currentIndex); currentIndex = newIndex; } token = token.trim(); return token; }; while (currentIndex < path.length) { if (path[currentIndex] !== "/") { throwError(); } if (++currentIndex === path.length) { break; } if (path[currentIndex] === '"' || path[currentIndex] === "'") { pathParts.push(getEscapedToken()); } else { pathParts.push(getToken()); } } return pathParts; } /** * @hidden */ function isResourceValid(resource, err) { // TODO: fix strictness issues so that caller contexts respects the types of the functions if (resource.id) { if (typeof resource.id !== "string") { err.message = "Id must be a string."; return false; } if (resource.id.indexOf("/") !== -1 || resource.id.indexOf("\\") !== -1 || resource.id.indexOf("?") !== -1 || resource.id.indexOf("#") !== -1) { err.message = "Id contains illegal chars."; return false; } if (resource.id[resource.id.length - 1] === " ") { err.message = "Id ends with a space."; return false; } } return true; } /** * @hidden */ function isItemResourceValid(resource, err) { // TODO: fix strictness issues so that caller contexts respects the types of the functions if (resource.id) { if (typeof resource.id !== "string") { err.message = "Id must be a string."; return false; } if (resource.id.indexOf("/") !== -1 || resource.id.indexOf("\\") !== -1 || resource.id.indexOf("#") !== -1) { err.message = "Id contains illegal chars."; return false; } } return true; } /** @hidden */ function getIdFromLink(resourceLink) { resourceLink = trimSlashes(resourceLink); return resourceLink; } /** @hidden */ function getPathFromLink(resourceLink, resourceType) { resourceLink = trimSlashes(resourceLink); if (resourceType) { return "/" + encodeURI(resourceLink) + "/" + resourceType; } else { return "/" + encodeURI(resourceLink); } } /** * @hidden */ function isStringNullOrEmpty(inputString) { // checks whether string is null, undefined, empty or only contains space return !inputString || /^\s*$/.test(inputString); } /** * @hidden */ function trimSlashFromLeftAndRight(inputString) { if (typeof inputString !== "string") { throw new Error("invalid input: input is not string"); } return inputString.replace(trimLeftSlashes, "").replace(trimRightSlashes, ""); } /** * @hidden */ function validateResourceId(resourceId) { // if resourceId is not a string or is empty throw an error if (typeof resourceId !== "string" || isStringNullOrEmpty(resourceId)) { throw new Error("Resource ID must be a string and cannot be undefined, null or empty"); } // if resource id contains illegal characters throw an error if (illegalResourceIdCharacters.test(resourceId)) { throw new Error("Illegal characters ['/', '\\', '#', '?'] cannot be used in Resource ID"); } return true; } /** * @hidden */ function validateItemResourceId(resourceId) { // if resourceId is not a string or is empty throw an error if (typeof resourceId !== "string" || isStringNullOrEmpty(resourceId)) { throw new Error("Resource ID must be a string and cannot be undefined, null or empty"); } // if resource id contains illegal characters throw an error if (illegalItemResourceIdCharacters.test(resourceId)) { throw new Error("Illegal characters ['/', '\\', '#'] cannot be used in Resource ID"); } return true; } /** * @hidden */ function getResourceIdFromPath(resourcePath) { if (!resourcePath || typeof resourcePath !== "string") { return null; } const trimmedPath = trimSlashFromLeftAndRight(resourcePath); const pathSegments = trimmedPath.split("/"); // number of segments of a path must always be even if (pathSegments.length % 2 !== 0) { return null; } return pathSegments[pathSegments.length - 1]; } /** * @hidden */ function parseConnectionString(connectionString) { const keyValueStrings = connectionString.split(";"); const { AccountEndpoint, AccountKey } = keyValueStrings.reduce((connectionObject, keyValueString) => { const [key, ...value] = keyValueString.split("="); connectionObject[key] = value.join("="); return connectionObject; }, {}); if (!AccountEndpoint || !AccountKey) { throw new Error("Could not parse the provided connection string"); } return { endpoint: AccountEndpoint, key: AccountKey, }; } // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. /** * @hidden */ const StatusCodes = { // Success Ok: 200, Created: 201, Accepted: 202, NoContent: 204, NotModified: 304, // Client error BadRequest: 400, Unauthorized: 401, Forbidden: 403, NotFound: 404, MethodNotAllowed: 405, RequestTimeout: 408, Conflict: 409, Gone: 410, PreconditionFailed: 412, RequestEntityTooLarge: 413, TooManyRequests: 429, RetryWith: 449, // Server Error InternalServerError: 500, ServiceUnavailable: 503, // System codes ENOTFOUND: "ENOTFOUND", // Operation pause and cancel. These are FAKE status codes for QOS logging purpose only. OperationPaused: 1200, OperationCancelled: 1201, }; /** * @hidden */ const SubStatusCodes = { Unknown: 0, // 400: Bad Request Substatus CrossPartitionQueryNotServable: 1004, // 410: StatusCodeType_Gone: substatus PartitionKeyRangeGone: 1002, // 404: NotFound Substatus ReadSessionNotAvailable: 1002, // 403: Forbidden Substatus WriteForbidden: 3, DatabaseAccountNotFound: 1008, }; // Copyright (c) Microsoft Corporation. /** * Would be used when creating or deleting a DocumentCollection * or a User in Azure Cosmos DB database service * @hidden * Given a database id, this creates a database link. * @param databaseId - The database id * @returns A database link in the format of `dbs/{0}` * with `{0}` being a Uri escaped version of the databaseId */ function createDatabaseUri(databaseId) { databaseId = trimSlashFromLeftAndRight(databaseId); validateResourceId(databaseId); return Constants.Path.DatabasesPathSegment + "/" + databaseId; } /** * Given a database and collection id, this creates a collection link. * Would be used when updating or deleting a DocumentCollection, creating a * Document, a StoredProcedure, a Trigger, a UserDefinedFunction, or when executing a query * with CreateDocumentQuery in Azure Cosmos DB database service. * @param databaseId - The database id * @param collectionId - The collection id * @returns A collection link in the format of `dbs/{0}/colls/{1}` * with `{0}` being a Uri escaped version of the databaseId and `{1}` being collectionId * @hidden */ function createDocumentCollectionUri(databaseId, collectionId) { collectionId = trimSlashFromLeftAndRight(collectionId); validateResourceId(collectionId); return (createDatabaseUri(databaseId) + "/" + Constants.Path.CollectionsPathSegment + "/" + collectionId); } /** * Given a database and user id, this creates a user link. * Would be used when creating a Permission, or when replacing or deleting * a User in Azure Cosmos DB database service * @param databaseId - The database id * @param userId - The user id * @returns A user link in the format of `dbs/{0}/users/{1}` * with `{0}` being a Uri escaped version of the databaseId and `{1}` being userId * @hidden */ function createUserUri(databaseId, userId) { userId = trimSlashFromLeftAndRight(userId); validateResourceId(userId); return createDatabaseUri(databaseId) + "/" + Constants.Path.UsersPathSegment + "/" + userId; } /** * Given a database and collection id, this creates a collection link. * Would be used when creating an Attachment, or when replacing * or deleting a Document in Azure Cosmos DB database service * @param databaseId - The database id * @param collectionId - The collection id * @param documentId - The document id * @returns A document link in the format of * `dbs/{0}/colls/{1}/docs/{2}` with `{0}` being a Uri escaped version of * the databaseId, `{1}` being collectionId and `{2}` being the documentId * @hidden */ function createDocumentUri(databaseId, collectionId, documentId) { documentId = trimSlashFromLeftAndRight(documentId); validateItemResourceId(documentId); return (createDocumentCollectionUri(databaseId, collectionId) + "/" + Constants.Path.DocumentsPathSegment + "/" + documentId); } /** * Given a database, collection and document id, this creates a document link. * Would be used when replacing or deleting a Permission in Azure Cosmos DB database service. * @param databaseId -The database Id * @param userId -The user Id * @param permissionId - The permissionId * @returns A permission link in the format of `dbs/{0}/users/{1}/permissions/{2}` * with `{0}` being a Uri escaped version of the databaseId, `{1}` being userId and `{2}` being permissionId * @hidden */ function createPermissionUri(databaseId, userId, permissionId) { permissionId = trimSlashFromLeftAndRight(permissionId); validateResourceId(permissionId); return (createUserUri(databaseId, userId) + "/" + Constants.Path.PermissionsPathSegment + "/" + permissionId); } /** * Given a database, collection and stored proc id, this creates a stored proc link. * Would be used when replacing, executing, or deleting a StoredProcedure in * Azure Cosmos DB database service. * @param databaseId -The database Id * @param collectionId -The collection Id * @param storedProcedureId -The stored procedure Id * @returns A stored procedure link in the format of * `dbs/{0}/colls/{1}/sprocs/{2}` with `{0}` being a Uri escaped version of the databaseId, * `{1}` being collectionId and `{2}` being the storedProcedureId * @hidden */ function createStoredProcedureUri(databaseId, collectionId, storedProcedureId) { storedProcedureId = trimSlashFromLeftAndRight(storedProcedureId); validateResourceId(storedProcedureId); return (createDocumentCollectionUri(databaseId, collectionId) + "/" + Constants.Path.StoredProceduresPathSegment + "/" + storedProcedureId); } /** * Given a database, collection and trigger id, this creates a trigger link. * Would be used when replacing, executing, or deleting a Trigger in Azure Cosmos DB database service * @param databaseId -The database Id * @param collectionId -The collection Id * @param triggerId -The trigger Id * @returns A trigger link in the format of * `dbs/{0}/colls/{1}/triggers/{2}` with `{0}` being a Uri escaped version of the databaseId, * `{1}` being collectionId and `{2}` being the triggerId * @hidden */ function createTriggerUri(databaseId, collectionId, triggerId) { triggerId = trimSlashFromLeftAndRight(triggerId); validateResourceId(triggerId); return (createDocumentCollectionUri(databaseId, collectionId) + "/" + Constants.Path.TriggersPathSegment + "/" + triggerId); } /** * Given a database, collection and udf id, this creates a udf link. * Would be used when replacing, executing, or deleting a UserDefinedFunction in * Azure Cosmos DB database service * @param databaseId -The database Id * @param collectionId -The collection Id * @param udfId -The User Defined Function Id * @returns A udf link in the format of `dbs/{0}/colls/{1}/udfs/{2}` * with `{0}` being a Uri escaped version of the databaseId, `{1}` being collectionId and `{2}` being the udfId * @hidden */ function createUserDefinedFunctionUri(databaseId, collectionId, udfId) { udfId = trimSlashFromLeftAndRight(udfId); validateResourceId(udfId); return (createDocumentCollectionUri(databaseId, collectionId) + "/" + Constants.Path.UserDefinedFunctionsPathSegment + "/" + udfId); } // Copyright (c) Microsoft Corporation. /** * @hidden */ function extractPartitionKey(document, partitionKeyDefinition) { if (partitionKeyDefinition && partitionKeyDefinition.paths && partitionKeyDefinition.paths.length > 0) { const partitionKey = []; partitionKeyDefinition.paths.forEach((path) => { const pathParts = parsePath(path); let obj = document; for (const part of pathParts) { if (typeof obj === "object" && part in obj) { obj = obj[part]; } else { obj = undefined; break; } } partitionKey.push(obj); }); if (partitionKey.length === 1 && partitionKey[0] === undefined) { return undefinedPartitionKey(partitionKeyDefinition); } return partitionKey; } } /** * @hidden */ function undefinedPartitionKey(partitionKeyDefinition) { if (partitionKeyDefinition.systemKey === true) { return []; } else { return [{}]; } } // Copyright (c) Microsoft Corporation. async function hmac(key, message) { return crypto.createHmac("sha256", Buffer.from(key, "base64")).update(message).digest("base64"); } // Copyright (c) Microsoft Corporation. async function generateHeaders(masterKey, method, resourceType = exports.ResourceType.none, resourceId = "", date = new Date()) { if (masterKey.startsWith("type=sas&")) { return { [Constants.HttpHeaders.Authorization]: encodeURIComponent(masterKey), [Constants.HttpHeaders.XDate]: date.toUTCString(), }; } const sig = await signature(masterKey, method, resourceType, resourceId, date); return { [Constants.HttpHeaders.Authorization]: sig, [Constants.HttpHeaders.XDate]: date.toUTCString(), }; } async function signature(masterKey, method, resourceType, resourceId = "", date = new Date()) { const type = "master"; const version = "1.0"; const text = method.toLowerCase() + "\n" + resourceType.toLowerCase() + "\n" + resourceId + "\n" + date.toUTCString().toLowerCase() + "\n" + "" + "\n"; const signed = await hmac(masterKey, text); return encodeURIComponent("type=" + type + "&ver=" + version + "&sig=" + signed); } // Copyright (c) Microsoft Corporation. /** * @hidden */ async function setAuthorizationHeader(clientOptions, verb, path, resourceId, resourceType, headers) { if (clientOptions.permissionFeed) { clientOptions.resourceTokens = {}; for (const permission of clientOptions.permissionFeed) { const id = getResourceIdFromPath(permission.resource); if (!id) { throw new Error(`authorization error: ${id} \ is an invalid resourceId in permissionFeed`); } clientOptions.resourceTokens[id] = permission._token; // TODO: any } } if (clientOptions.key) { await setAuthorizationTokenHeaderUsingMasterKey(verb, resourceId, resourceType, headers, clientOptions.key); } else if (clientOptions.resourceTokens) { headers[Constants.HttpHeaders.Authorization] = encodeURIComponent(getAuthorizationTokenUsingResourceTokens(clientOptions.resourceTokens, path, resourceId)); } else if (clientOptions.tokenProvider) { headers[Constants.HttpHeaders.Authorization] = encodeURIComponent(await clientOptions.tokenProvider({ verb, path, resourceId, resourceType, headers })); } } /** * The default function for setting header token using the masterKey * @hidden */ async function setAuthorizationTokenHeaderUsingMasterKey(verb, resourceId, resourceType, headers, masterKey) { // TODO This should live in cosmos-sign if (resourceType === exports.ResourceType.offer) { resourceId = resourceId && resourceId.toLowerCase(); } headers = Object.assign(headers, await generateHeaders(masterKey, verb, resourceType, resourceId)); } /** * @hidden */ // TODO: Resource tokens function getAuthorizationTokenUsingResourceTokens(resourceTokens, path, resourceId) { if (resourceTokens && Object.keys(resourceTokens).length > 0) { // For database account access(through getDatabaseAccount API), path and resourceId are "", // so in this case we return the first token to be used for creating the auth header as the // service will accept any token in this case if (!path && !resourceId) { return resourceTokens[Object.keys(resourceTokens)[0]]; } // If we have exact resource token for the path use it if (resourceId && resourceTokens[resourceId]) { return resourceTokens[resourceId]; } // minimum valid path /dbs if (!path || path.length < 4) { // TODO: This should throw an error return null; } path = trimSlashFromLeftAndRight(path); const pathSegments = (path && path.split("/")) || []; // Item path if (pathSegments.length === 6) { // Look for a container token matching the item path const containerPath = pathSegments.slice(0, 4).map(decodeURIComponent).join("/"); if (resourceTokens[containerPath]) { return resourceTokens[containerPath]; } } // TODO remove in v4: This is legacy behavior that lets someone use a resource token pointing ONLY at an ID // It was used when _rid was exposed by the SDK, but now that we are using user provided ids it is not needed // However removing it now would be a breaking change // if it's an incomplete path like /dbs/db1/colls/, start from the parent resource let index = pathSegments.length % 2 === 0 ? pathSegments.length - 1 : pathSegments.length - 2; for (; index > 0; index -= 2) { const id = decodeURI(pathSegments[index]); if (resourceTokens[id]) { return resourceTokens[id]; } } } // TODO: This should throw an error return null; } // Copyright (c) Microsoft Corporation. const uuid$2 = uuid$3.v4; function isKeyInRange(min, max, key) { const isAfterMinInclusive = key.localeCompare(min) >= 0; const isBeforeMax = key.localeCompare(max) < 0; return isAfterMinInclusive && isBeforeMax; } const BulkOperationType = { Create: "Create", Upsert: "Upsert", Read: "Read", Delete: "Delete", Replace: "Replace", Patch: "Patch", }; function hasResource(operation) { return (operation.operationType !== "Patch" && operation.resourceBody !== undefined); } function getPartitionKeyToHash(operation, partitionProperty) { const toHashKey = hasResource(operation) ? deepFind(operation.resourceBody, partitionProperty) : (operation.partitionKey && operation.partitionKey.replace(/[[\]"']/g, "")) || operation.partitionKey; // We check for empty object since replace will stringify the value // The second check avoids cases where the partitionKey value is actually the string '{}' if (toHashKey === "{}" && operation.partitionKey === "[{}]") { return {}; } if (toHashKey === "null" && operation.partitionKey === "[null]") { return null; } if (toHashKey === "0" && operation.partitionKey === "[0]") { return 0; } return toHashKey; } function decorateOperation(operation, definition, options = {}) { if (operation.operationType === BulkOperationType.Create || operation.operationType === BulkOperationType.Upsert) { if ((operation.resourceBody.id === undefined || operation.resourceBody.id === "") && !options.disableAutomaticIdGeneration) { operation.resourceBody.id = uuid$2(); } } if ("partitionKey" in operation) { const extracted = extractPartitionKey(operation, { paths: ["/partitionKey"] }); return Object.assign(Object.assign({}, operation), { partitionKey: JSON.stringify(extracted) }); } else if (operation.operationType === BulkOperationType.Create || operation.operationType === BulkOperationType.Replace || operation.operationType === BulkOperationType.Upsert) { const pk = extractPartitionKey(operation.resourceBody, definition); return Object.assign(Object.assign({}, operation), { partitionKey: JSON.stringify(pk) }); } else if (operation.operationType === BulkOperationType.Read || operation.operationType === BulkOperationType.Delete) { return Object.assign(Object.assign({}, operation), { partitionKey: "[{}]" }); } return operation; } function decorateBatchOperation(oper