diff --git a/Dockerfile b/Dockerfile index 64b6093f3890761d0297eec97d24ef555fa6174e..0e60399a9feab0771b36696adbdf78ab8393e910 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,31 +5,49 @@ FROM python:3.11-slim ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ HF_HOME=/code/.cache/huggingface \ - PORT=7860 + PORT=7860 \ + NODE_ENV=production # Set the working directory WORKDIR /code -# Install system dependencies (git is sometimes used by HF libraries) +# Install system dependencies & Node.js (LTS v18) RUN apt-get update && apt-get install -y --no-install-recommends \ git \ build-essential \ + curl \ + && curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \ + && apt-get install -y nodejs \ && rm -rf /var/lib/apt/lists/* -# Copy requirements file first to leverage Docker layer caching +# 1. Install Python RAG Backend dependencies COPY requirements.txt . - -# Install python dependencies RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt -# Create a writable cache directory for Hugging Face models -# Hugging Face Spaces runs as user 1000, so we must make /code writable -RUN mkdir -p /code/.cache/huggingface && chmod -R 777 /code +# 2. Build React Frontend static files +COPY frontend/package*.json ./frontend/ +RUN cd frontend && npm install + +COPY frontend/ ./frontend/ +RUN cd frontend && npm run build + +# 3. Setup Node.js API Gateway +COPY gateway/package*.json ./gateway/ +RUN cd gateway && npm install --production -# Copy the rest of the application files +# Copy build output from frontend to gateway's public directory +RUN mkdir -p gateway/public && cp -r frontend/build/* gateway/public/ + +# Copy the rest of the application files (including backends) COPY . . +# Ensure start.sh has execution permissions +RUN chmod +x start.sh + +# Create a writable cache directory for Hugging Face models (user 1000 compatibility) +RUN mkdir -p /code/.cache/huggingface && chmod -R 777 /code + # Pre-download and cache models in the container image during build time RUN python download_models.py @@ -39,5 +57,5 @@ RUN python scratch/ingest_large_samples.py # Expose port 7860 for Hugging Face Space / external access EXPOSE 7860 -# Run the FastAPI application on host 0.0.0.0 and port 7860 -CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"] +# Run the startup script to concurrently run FastAPI and Node.js +CMD ["bash", "start.sh"] diff --git a/frontend/src/App.js b/frontend/src/App.js index c0577d83e006462d37ff436c0284123d6f4e3647..7390661493cc8b2e6e1a6bf4399e86e65f09699e 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -6,6 +6,8 @@ import { } from 'lucide-react'; import './App.css'; +const API_URL = process.env.REACT_APP_API_URL || 'http://localhost:5000'; + function App() { const [token, setToken] = useState(localStorage.getItem('token') || ''); const [email, setEmail] = useState(''); @@ -238,7 +240,7 @@ function App() { const targetEmail = devEmail || email; const targetPassword = devEmail ? 'password123' : password; try { - const res = await fetch('http://localhost:5000/api/login', { + const res = await fetch(`${API_URL}/api/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: targetEmail, password: targetPassword }) @@ -283,7 +285,7 @@ function App() { } try { - const res = await fetch('http://localhost:5000/api/register', { + const res = await fetch(`${API_URL}/api/register`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ @@ -322,7 +324,7 @@ function App() { const fetchUserStatus = async () => { try { - const res = await fetch('http://localhost:5000/api/user-status', { + const res = await fetch(`${API_URL}/api/user-status`, { headers: { 'Authorization': `Bearer ${token}` } }); const data = await res.json(); @@ -333,7 +335,7 @@ function App() { const fetchDocuments = async () => { try { - const res = await fetch('http://localhost:5000/api/documents', { + const res = await fetch(`${API_URL}/api/documents`, { headers: { 'Authorization': `Bearer ${token}` } }); const data = await res.json(); @@ -351,7 +353,7 @@ function App() { const fetchChatThreads = async (projId) => { try { - const res = await fetch(`http://localhost:5000/api/projects/${projId}/chats`, { + const res = await fetch(`${API_URL}/api/projects/${projId}/chats`, { headers: { 'Authorization': `Bearer ${token}` } }); const data = await res.json(); @@ -363,7 +365,7 @@ function App() { const handleCreateChatThread = async (projId, threadTitle) => { try { - const res = await fetch(`http://localhost:5000/api/projects/${projId}/chats`, { + const res = await fetch(`${API_URL}/api/projects/${projId}/chats`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -380,7 +382,7 @@ function App() { const handleDeleteChatThread = async (e, threadId) => { e.stopPropagation(); try { - await fetch(`http://localhost:5000/api/projects/${selectedProjectId}/chats/${threadId}`, { + await fetch(`${API_URL}/api/projects/${selectedProjectId}/chats/${threadId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` } }); @@ -397,7 +399,7 @@ function App() { if (newTitle === currentTitle) return; try { - const res = await fetch(`http://localhost:5000/api/projects/${selectedProjectId}/chats/${threadId}`, { + const res = await fetch(`${API_URL}/api/projects/${selectedProjectId}/chats/${threadId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', @@ -419,7 +421,7 @@ function App() { if (!window.confirm("Are you sure you want to delete this document from the database? This will permanently wipe its text vectors and conversation history.")) return; try { - const res = await fetch(`http://localhost:5000/api/documents/${docId}`, { + const res = await fetch(`${API_URL}/api/documents/${docId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` } }); @@ -479,7 +481,7 @@ function App() { setQuery(''); setLoading(true); try { - const response = await fetch('http://localhost:5000/api/query', { + const response = await fetch(`${API_URL}/api/query`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -518,7 +520,7 @@ function App() { formData.append('doc_id', docId); formData.append('title', uploadTitle); try { - const res = await fetch('http://localhost:5000/api/ingest-file', { + const res = await fetch(`${API_URL}/api/ingest-file`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}` }, body: formData @@ -543,7 +545,7 @@ function App() { setUploadStatus({ type: '', text: '' }); const docId = `doc_${uploadTitle.toLowerCase().replace(/[^a-z0-9]/g, '_')}`; try { - const res = await fetch('http://localhost:5000/api/ingest', { + const res = await fetch(`${API_URL}/api/ingest`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -568,7 +570,7 @@ function App() { const handleViewDocContent = async (docId) => { try { - const res = await fetch(`http://localhost:5000/api/documents/${docId}`, { + const res = await fetch(`${API_URL}/api/documents/${docId}`, { headers: { 'Authorization': `Bearer ${token}` } }); const data = await res.json(); @@ -578,7 +580,7 @@ function App() { const handleUpgrade = async () => { try { - const res = await fetch('http://localhost:5000/api/create-razorpay-order', { + const res = await fetch(`${API_URL}/api/create-razorpay-order`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -600,7 +602,7 @@ function App() { order_id: order.id, handler: async function (response) { try { - const verifyRes = await fetch("http://localhost:5000/api/verify-razorpay-payment", { + const verifyRes = await fetch(`${API_URL}/api/verify-razorpay-payment`, { method: "POST", headers: { "Content-Type": "application/json", @@ -659,7 +661,7 @@ function App() { setProfileSaveStatus({ type: '', text: '' }); try { - const res = await fetch('http://localhost:5000/api/user-profile', { + const res = await fetch(`${API_URL}/api/user-profile`, { method: 'PUT', headers: { 'Content-Type': 'application/json', @@ -691,7 +693,7 @@ function App() { if (!confirmCancel) return; try { - const res = await fetch('http://localhost:5000/api/cancel-subscription', { + const res = await fetch(`${API_URL}/api/cancel-subscription`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}` diff --git a/gateway/migrate.js b/gateway/migrate.js new file mode 100644 index 0000000000000000000000000000000000000000..27795d55460a18035f71c3debd4fe43a1f7afadb --- /dev/null +++ b/gateway/migrate.js @@ -0,0 +1,83 @@ +const fs = require('fs'); +const path = require('path'); +const mongoose = require('mongoose'); +const dotenv = require('dotenv'); + +// Load environment variables +dotenv.config(); + +const User = require('./models/User'); +const ProjectChat = require('./models/Chat'); + +const USERS_FILE = path.join(__dirname, 'users.json'); +const CHATS_FILE = path.join(__dirname, 'chats.json'); + +async function migrate() { + if (!process.env.MONGODB_URI) { + console.error("❌ MONGODB_URI not found in environment configurations."); + process.exit(1); + } + + console.log("🌿 Connecting to MongoDB..."); + await mongoose.connect(process.env.MONGODB_URI); + console.log("🌿 Connected successfully."); + + // 1. Migrate Users + if (fs.existsSync(USERS_FILE)) { + try { + const rawUsers = JSON.parse(fs.readFileSync(USERS_FILE, 'utf8')); + console.log(`💼 Found ${rawUsers.length} users in users.json to migrate.`); + + for (const user of rawUsers) { + const exists = await User.findOne({ userId: user.userId }); + if (!exists) { + // Create new user model (preserving old plain-text passwords or bcrypt hashes as-is) + await User.create(user); + console.log(` ✅ Migrated user profile: ${user.name} (${user.email})`); + } else { + console.log(` ⏭️ User already exists in MongoDB: ${user.name} (${user.email})`); + } + } + } catch (err) { + console.error("❌ Failed migrating users:", err.message); + } + } else { + console.log("⏭️ No users.json file found to migrate."); + } + + // 2. Migrate Chats + if (fs.existsSync(CHATS_FILE)) { + try { + const chatsDb = JSON.parse(fs.readFileSync(CHATS_FILE, 'utf8')); + let threadCount = 0; + + for (const userId of Object.keys(chatsDb)) { + for (const projectId of Object.keys(chatsDb[userId])) { + const exists = await ProjectChat.findOne({ userId, projectId }); + if (!exists) { + const projectData = chatsDb[userId][projectId]; + await ProjectChat.create({ + userId, + projectId, + chats: projectData.chats || [] + }); + threadCount += (projectData.chats || []).length; + console.log(` ✅ Migrated project workspace chats for User: ${userId}, Project: ${projectId}`); + } else { + console.log(` ⏭️ Project chat already exists in MongoDB for User: ${userId}, Project: ${projectId}`); + } + } + } + console.log(`💼 Migrated total of ${threadCount} chat threads to MongoDB.`); + } catch (err) { + console.error("❌ Failed migrating chats:", err.message); + } + } else { + console.log("⏭️ No chats.json file found to migrate."); + } + + console.log("\n🎉 Migration completed successfully!"); + mongoose.connection.close(); +} + +migrate(); diff --git a/gateway/models/Chat.js b/gateway/models/Chat.js new file mode 100644 index 0000000000000000000000000000000000000000..2b707b415997c4c028ab607cc4dc1cc0b34395c6 --- /dev/null +++ b/gateway/models/Chat.js @@ -0,0 +1,25 @@ +const mongoose = require('mongoose'); + +const messageSchema = new mongoose.Schema({ + sender: { type: String, enum: ['user', 'ai'], required: true }, + text: { type: String, required: true }, + timestamp: { type: Date, default: Date.now }, + telemetry: { type: mongoose.Schema.Types.Mixed } // Multi-source metadata storage +}); + +const threadSchema = new mongoose.Schema({ + chatId: { type: String, required: true }, + chatTitle: { type: String, required: true }, + messages: [messageSchema] +}); + +const projectChatSchema = new mongoose.Schema({ + userId: { type: String, required: true }, + projectId: { type: String, required: true }, // active document workspace ID + chats: [threadSchema] +}, { timestamps: true }); + +// Dynamic indexes for fast lookups +projectChatSchema.index({ userId: 1, projectId: 1 }, { unique: true }); + +module.exports = mongoose.model('ProjectChat', projectChatSchema); \ No newline at end of file diff --git a/gateway/models/User.js b/gateway/models/User.js new file mode 100644 index 0000000000000000000000000000000000000000..fc6d8d766e83b5874d2426cbf4e4e70208aa2be8 --- /dev/null +++ b/gateway/models/User.js @@ -0,0 +1,25 @@ +const mongoose = require('mongoose'); + +const documentSchema = new mongoose.Schema({ + id: { type: String, required: true }, + name: { type: String, required: true }, + sizeBytes: { type: Number, required: true }, + uploadedAt: { type: Date, default: Date.now } +}); + +const userSchema = new mongoose.Schema({ + userId: { type: String, required: true, unique: true }, + name: { type: String, required: true }, + email: { type: String, required: true, unique: true, lowercase: true, trim: true }, + password: { type: String, required: true }, // bcrypt hashed passwords + isPremium: { type: Boolean, default: false }, + queryCount: { type: Number, default: 0 }, + totalStorageBytes: { type: Number, default: 0 }, + uploadedDocuments: [documentSchema], + profilePhoto: { type: String, default: '👨‍💻' }, + phoneNumber: { type: String, default: '' }, + razorpayPaymentId: { type: String }, + stripeCustomerId: { type: String } +}, { timestamps: true }); // Auto adds createdAt & updatedAt fields + +module.exports = mongoose.model('User', userSchema); \ No newline at end of file diff --git a/gateway/node_modules/.package-lock.json b/gateway/node_modules/.package-lock.json index 65622a319ef45da5d1d82e8b14f63016aeb9c985..99a9c259e52943cffcfaa98eec9d4b49a844f007 100644 --- a/gateway/node_modules/.package-lock.json +++ b/gateway/node_modules/.package-lock.json @@ -4,6 +4,36 @@ "lockfileVersion": 3, "requires": true, "packages": { + "node_modules/@mongodb-js/saslprep": { + "version": "1.4.13", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.4.13.tgz", + "integrity": "sha512-E3Sv4eCYAlKYUTx8S3ioQcDUscOif+8zZ5OnW1IzJ+Tt+EO+ke8mn+Y3FX6N1H79picwbdOavVOb1jPi2EOyrg==", + "license": "MIT", + "dependencies": { + "sparse-bitfield": "^3.0.3" + } + }, + "node_modules/@standard-schema/spec": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz", + "integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==", + "license": "MIT" + }, + "node_modules/@types/webidl-conversions": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz", + "integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA==", + "license": "MIT" + }, + "node_modules/@types/whatwg-url": { + "version": "13.0.0", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-13.0.0.tgz", + "integrity": "sha512-N8WXpbE6Wgri7KUSvrmQcqrMllKZ9uxkYWMt+mCSGwNc0Hsw9VQTW7ApqI4XNrx6/SaM2QQJCzMPDEXE058s+Q==", + "license": "MIT", + "dependencies": { + "@types/webidl-conversions": "*" + } + }, "node_modules/accepts": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz", @@ -99,6 +129,15 @@ "url": "https://opencollective.com/express" } }, + "node_modules/bson": { + "version": "7.3.2", + "resolved": "https://registry.npmjs.org/bson/-/bson-7.3.2.tgz", + "integrity": "sha512-1w0ra+ho1cuE+w8jzwgzTFIimFtCfZeCoOsvIPQg6uyFCsp8M29U7bNNf5GrFh88TXbYg1g3TyKUGpd6O7q6zA==", + "license": "Apache-2.0", + "engines": { + "node": ">=20.19.0" + } + }, "node_modules/buffer-equal-constant-time": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", @@ -766,6 +805,15 @@ "safe-buffer": "^5.0.1" } }, + "node_modules/kareem": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-3.3.0.tgz", + "integrity": "sha512-kpSuLD3/7RenBnjnJdOHXCKC8dTd1JzeOiJhN0necWWci6cC+qX+VuwPnMVgb+a4+KNJSfgqahpnfWaeDXCimw==", + "license": "Apache-2.0", + "engines": { + "node": ">=18.0.0" + } + }, "node_modules/lodash.includes": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", @@ -830,6 +878,12 @@ "url": "https://opencollective.com/express" } }, + "node_modules/memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "license": "MIT" + }, "node_modules/merge-descriptors": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-2.0.0.tgz", @@ -867,6 +921,105 @@ "url": "https://opencollective.com/express" } }, + "node_modules/mongodb": { + "version": "7.5.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-7.5.0.tgz", + "integrity": "sha512-5FnrEDLnvp6ycUOGLNLLU33BfCx2qmp2mJjGPDwKLruYsVzXVSK5fsGpoDXvsXJwBfBsD7ebMRdawbDxC2814g==", + "license": "Apache-2.0", + "dependencies": { + "@mongodb-js/saslprep": "^1.4.11", + "bson": "^7.2.0", + "mongodb-connection-string-url": "^7.0.1" + }, + "engines": { + "node": ">=20.19.0" + }, + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.806.0", + "@mongodb-js/zstd": "^7.0.0", + "gcp-metadata": "^7.0.1", + "kerberos": "^7.0.0", + "mongodb-client-encryption": "^7.2.0", + "snappy": "^7.3.2", + "socks": "^2.8.6" + }, + "peerDependenciesMeta": { + "@aws-sdk/credential-providers": { + "optional": true + }, + "@mongodb-js/zstd": { + "optional": true + }, + "gcp-metadata": { + "optional": true + }, + "kerberos": { + "optional": true + }, + "mongodb-client-encryption": { + "optional": true + }, + "snappy": { + "optional": true + }, + "socks": { + "optional": true + } + } + }, + "node_modules/mongodb-connection-string-url": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-7.0.2.tgz", + "integrity": "sha512-ZoS07RoFqpKYQwAk59qmrx8+jJHNHU30UjlU96QktiGn1ltvDr+vCznLX5DiUBLEpMAHatHNWV1nM/74ul66kA==", + "license": "Apache-2.0", + "dependencies": { + "@types/whatwg-url": "^13.0.0", + "whatwg-url": "^14.1.0" + }, + "engines": { + "node": ">=20.19.0" + } + }, + "node_modules/mongoose": { + "version": "9.9.3", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-9.9.3.tgz", + "integrity": "sha512-9dQaKct05LNgVkunDzFPZsGGBhinobl3Qc5B5KYJkLNG5lBLgqESUEItl1EUIkVaCwZJGBgTuvJiFAjjIATCiw==", + "license": "MIT", + "dependencies": { + "@standard-schema/spec": "^1.1.0", + "kareem": "3.3.0", + "mongodb": "~7.5", + "mpath": "0.9.0", + "mquery": "6.0.0", + "ms": "2.1.3", + "sift": "17.1.3" + }, + "engines": { + "node": ">=20.19.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/mongoose" + } + }, + "node_modules/mpath": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz", + "integrity": "sha512-ikJRQTk8hw5DEoFVxHG1Gn9T/xcjtdnOKIU1JTmGjZZlg9LST2mBLmcX3/ICIbgJydT2GOc15RnNy5mHmzfSew==", + "license": "MIT", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/mquery": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-6.0.0.tgz", + "integrity": "sha512-b2KQNsmgtkscfeDgkYMcWGn9vZI9YoXh802VDEwE6qc50zxBFQ0Oo8ROkawbPAsXCY1/Z1yp0MagqsZStPWJjw==", + "license": "MIT", + "engines": { + "node": ">=20.19.0" + } + }, "node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", @@ -1027,6 +1180,15 @@ "node": ">=10" } }, + "node_modules/punycode": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "license": "MIT", + "engines": { + "node": ">=6" + } + }, "node_modules/qs": { "version": "6.15.3", "resolved": "https://registry.npmjs.org/qs/-/qs-6.15.3.tgz", @@ -1271,6 +1433,21 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/sift": { + "version": "17.1.3", + "resolved": "https://registry.npmjs.org/sift/-/sift-17.1.3.tgz", + "integrity": "sha512-Rtlj66/b0ICeFzYTuNvX/EF1igRbbnGSvEyT79McoZa/DeGhMyC5pWKOEsZKnpkqtSeovd5FL/bjHWC3CIIvCQ==", + "license": "MIT" + }, + "node_modules/sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", + "license": "MIT", + "dependencies": { + "memory-pager": "^1.0.2" + } + }, "node_modules/statuses": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz", @@ -1323,6 +1500,18 @@ "node": ">=0.6" } }, + "node_modules/tr46": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-5.1.1.tgz", + "integrity": "sha512-hdF5ZgjTqgAntKkklYw0R03MG2x/bSzTtkxmIRw/sTNV8YXsCJ1tfLAX23lhxhHJlEf3CRCOCGGWw3vI3GaSPw==", + "license": "MIT", + "dependencies": { + "punycode": "^2.3.1" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/type-is": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/type-is/-/type-is-2.1.0.tgz", @@ -1384,6 +1573,28 @@ "node": ">= 0.8" } }, + "node_modules/webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + } + }, + "node_modules/whatwg-url": { + "version": "14.2.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-14.2.0.tgz", + "integrity": "sha512-De72GdQZzNTUBBChsXueQUnPKDkg/5A5zp7pFDuQAj5UFoENpiACU0wlCvzpAGnTkj++ihpKwKyYewn/XNUbKw==", + "license": "MIT", + "dependencies": { + "tr46": "^5.1.0", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/gateway/node_modules/@mongodb-js/saslprep/LICENSE b/gateway/node_modules/@mongodb-js/saslprep/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..481c7a50f96cf594ff078ba43267185b9c5cccc7 --- /dev/null +++ b/gateway/node_modules/@mongodb-js/saslprep/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2014 Dmitry Tsvettsikh + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/gateway/node_modules/@mongodb-js/saslprep/package.json b/gateway/node_modules/@mongodb-js/saslprep/package.json new file mode 100644 index 0000000000000000000000000000000000000000..d271c759e5721daf90094494f0ae47f2289fa4f6 --- /dev/null +++ b/gateway/node_modules/@mongodb-js/saslprep/package.json @@ -0,0 +1,87 @@ +{ + "name": "@mongodb-js/saslprep", + "description": "SASLprep: Stringprep Profile for User Names and Passwords, rfc4013", + "keywords": [ + "sasl", + "saslprep", + "stringprep", + "rfc4013", + "4013" + ], + "author": "Dmitry Tsvettsikh ", + "publishConfig": { + "access": "public" + }, + "main": "dist/node.js", + "bugs": { + "url": "https://jira.mongodb.org/projects/COMPASS/issues", + "email": "compass@mongodb.com" + }, + "homepage": "https://github.com/mongodb-js/devtools-shared/tree/main/packages/saslprep", + "version": "1.4.13", + "repository": { + "type": "git", + "url": "git+https://github.com/mongodb-js/devtools-shared.git" + }, + "files": [ + "dist" + ], + "license": "MIT", + "exports": { + "browser": { + "types": "./dist/browser.d.ts", + "default": "./dist/browser.js" + }, + "import": { + "types": "./dist/node.d.ts", + "default": "./dist/.esm-wrapper.mjs" + }, + "require": { + "types": "./dist/node.d.ts", + "default": "./dist/node.js" + } + }, + "types": "./dist/node.d.ts", + "scripts": { + "gen-code-points": "ts-node src/generate-code-points.ts src/code-points-data.ts src/code-points-data-browser.ts", + "bootstrap": "npm run compile", + "prepublishOnly": "npm run compile", + "compile": "npm run gen-code-points && tsc -p tsconfig.json && gen-esm-wrapper . ./dist/.esm-wrapper.mjs", + "typecheck": "tsc --noEmit", + "eslint": "eslint", + "prettier": "prettier", + "lint": "npm run eslint . && npm run prettier -- --check .", + "depcheck": "depcheck", + "check": "npm run typecheck && npm run lint && npm run depcheck", + "check-ci": "npm run check", + "test": "mocha", + "test-cov": "nyc -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test", + "test-watch": "npm run test -- --watch", + "test-ci": "npm run test-cov", + "reformat": "npm run prettier -- --write ." + }, + "dependencies": { + "sparse-bitfield": "^3.0.3" + }, + "devDependencies": { + "@mongodb-js/eslint-config-devtools": "^0.11.8", + "@mongodb-js/mocha-config-devtools": "^1.1.3", + "@mongodb-js/prettier-config-devtools": "^1.0.4", + "@mongodb-js/tsconfig-devtools": "^1.1.3", + "@types/chai": "^4.2.21", + "@types/mocha": "^9.1.1", + "@types/node": "^25.5.2", + "@types/sinon-chai": "^4.0.0", + "@types/sparse-bitfield": "^3.0.4", + "chai": "^4.5.0", + "depcheck": "^1.4.7", + "eslint": "^7.25.0 || ^8.0.0", + "gen-esm-wrapper": "^1.1.3", + "mocha": "^8.4.0", + "nyc": "^15.1.0", + "prettier": "^3.8.1", + "sinon": "^9.2.3", + "typescript": "^5.9.3" + }, + "gitHead": "58acee6dbcadb316e3fd3d52bd97c2bd31d621cc" +} diff --git a/gateway/node_modules/@mongodb-js/saslprep/readme.md b/gateway/node_modules/@mongodb-js/saslprep/readme.md new file mode 100644 index 0000000000000000000000000000000000000000..28539edaa32ae2c361edd9b9f18b0430847d2f21 --- /dev/null +++ b/gateway/node_modules/@mongodb-js/saslprep/readme.md @@ -0,0 +1,29 @@ +# saslprep + +_Note: This is a fork of the original [`saslprep`](https://www.npmjs.com/package/saslprep) npm package +and provides equivalent functionality._ + +Stringprep Profile for User Names and Passwords, [rfc4013](https://tools.ietf.org/html/rfc4013) + +### Usage + +```js +const saslprep = require('@mongodb-js/saslprep'); + +saslprep('password\u00AD'); // password +saslprep('password\u0007'); // Error: prohibited character +``` + +### API + +##### `saslprep(input: String, opts: Options): String` + +Normalize user name or password. + +##### `Options.allowUnassigned: bool` + +A special behavior for unassigned code points, see https://tools.ietf.org/html/rfc4013#section-2.5. Disabled by default. + +## License + +MIT, 2017-2019 (c) Dmitriy Tsvettsikh diff --git a/gateway/node_modules/@standard-schema/spec/LICENSE b/gateway/node_modules/@standard-schema/spec/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..ea54e0ddad405b2ee635d687a876613214160ba2 --- /dev/null +++ b/gateway/node_modules/@standard-schema/spec/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Colin McDonnell + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/gateway/node_modules/@standard-schema/spec/README.md b/gateway/node_modules/@standard-schema/spec/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f9813ffa878e7914dc2928ab13db856850e8d83e --- /dev/null +++ b/gateway/node_modules/@standard-schema/spec/README.md @@ -0,0 +1,198 @@ +

+ Standard Schema fire logo +
+ Standard Schema

+

+ A family of specs for interoperable TypeScript +
+ standardschema.dev +

+
+ + + +The Standard Schema project is a set of interfaces that standardize the provision and consumption of shared functionality in the TypeScript ecosystem. + +Its goal is to allow tools to accept a single input that includes all the types and capabilities they need— no library-specific adapters, no extra dependencies. The result is an ecosystem that's fair for implementers, friendly for consumers, and open for end users. + +## The specifications + +The specifications can be found below in their entirety. Libraries wishing to implement a spec can copy/paste the code block below into their codebase. They're also available at `@standard-schema/spec` on [npm](https://www.npmjs.com/package/@standard-schema/spec) and [JSR](https://jsr.io/@standard-schema/spec). + +```ts +// ######################### +// ### Standard Typed ### +// ######################### + +/** The Standard Typed interface. This is a base type extended by other specs. */ +export interface StandardTypedV1 { + /** The Standard properties. */ + readonly "~standard": StandardTypedV1.Props; +} + +export declare namespace StandardTypedV1 { + /** The Standard Typed properties interface. */ + export interface Props { + /** The version number of the standard. */ + readonly version: 1; + /** The vendor name of the schema library. */ + readonly vendor: string; + /** Inferred types associated with the schema. */ + readonly types?: Types | undefined; + } + + /** The Standard Typed types interface. */ + export interface Types { + /** The input type of the schema. */ + readonly input: Input; + /** The output type of the schema. */ + readonly output: Output; + } + + /** Infers the input type of a Standard Typed. */ + export type InferInput = NonNullable< + Schema["~standard"]["types"] + >["input"]; + + /** Infers the output type of a Standard Typed. */ + export type InferOutput = NonNullable< + Schema["~standard"]["types"] + >["output"]; +} + +// ########################## +// ### Standard Schema ### +// ########################## + +/** The Standard Schema interface. */ +export interface StandardSchemaV1 { + /** The Standard Schema properties. */ + readonly "~standard": StandardSchemaV1.Props; +} + +export declare namespace StandardSchemaV1 { + /** The Standard Schema properties interface. */ + export interface Props + extends StandardTypedV1.Props { + /** Validates unknown input values. */ + readonly validate: ( + value: unknown, + options?: StandardSchemaV1.Options | undefined + ) => Result | Promise>; + } + + /** The result interface of the validate function. */ + export type Result = SuccessResult | FailureResult; + + /** The result interface if validation succeeds. */ + export interface SuccessResult { + /** The typed output value. */ + readonly value: Output; + /** A falsy value for `issues` indicates success. */ + readonly issues?: undefined; + } + + export interface Options { + /** Explicit support for additional vendor-specific parameters, if needed. */ + readonly libraryOptions?: Record | undefined; + } + + /** The result interface if validation fails. */ + export interface FailureResult { + /** The issues of failed validation. */ + readonly issues: ReadonlyArray; + } + + /** The issue interface of the failure output. */ + export interface Issue { + /** The error message of the issue. */ + readonly message: string; + /** The path of the issue, if any. */ + readonly path?: ReadonlyArray | undefined; + } + + /** The path segment interface of the issue. */ + export interface PathSegment { + /** The key representing a path segment. */ + readonly key: PropertyKey; + } + + /** The Standard types interface. */ + export interface Types + extends StandardTypedV1.Types {} + + /** Infers the input type of a Standard. */ + export type InferInput = + StandardTypedV1.InferInput; + + /** Infers the output type of a Standard. */ + export type InferOutput = + StandardTypedV1.InferOutput; +} + +// ############################### +// ### Standard JSON Schema ### +// ############################### + +/** The Standard JSON Schema interface. */ +export interface StandardJSONSchemaV1 { + /** The Standard JSON Schema properties. */ + readonly "~standard": StandardJSONSchemaV1.Props; +} + +export declare namespace StandardJSONSchemaV1 { + /** The Standard JSON Schema properties interface. */ + export interface Props + extends StandardTypedV1.Props { + /** Methods for generating the input/output JSON Schema. */ + readonly jsonSchema: StandardJSONSchemaV1.Converter; + } + + /** The Standard JSON Schema converter interface. */ + export interface Converter { + /** Converts the input type to JSON Schema. May throw if conversion is not supported. */ + readonly input: ( + options: StandardJSONSchemaV1.Options + ) => Record; + /** Converts the output type to JSON Schema. May throw if conversion is not supported. */ + readonly output: ( + options: StandardJSONSchemaV1.Options + ) => Record; + } + + /** + * The target version of the generated JSON Schema. + * + * It is *strongly recommended* that implementers support `"draft-2020-12"` and `"draft-07"`, as they are both in wide use. All other targets can be implemented on a best-effort basis. Libraries should throw if they don't support a specified target. + * + * The `"openapi-3.0"` target is intended as a standardized specifier for OpenAPI 3.0 which is a superset of JSON Schema `"draft-04"`. + */ + export type Target = + | "draft-2020-12" + | "draft-07" + | "openapi-3.0" + // Accepts any string for future targets while preserving autocomplete + | ({} & string); + + /** The options for the input/output methods. */ + export interface Options { + /** Specifies the target version of the generated JSON Schema. Support for all versions is on a best-effort basis. If a given version is not supported, the library should throw. */ + readonly target: Target; + + /** Explicit support for additional vendor-specific parameters, if needed. */ + readonly libraryOptions?: Record | undefined; + } + + /** The Standard types interface. */ + export interface Types + extends StandardTypedV1.Types {} + + /** Infers the input type of a Standard. */ + export type InferInput = + StandardTypedV1.InferInput; + + /** Infers the output type of a Standard. */ + export type InferOutput = + StandardTypedV1.InferOutput; +} +``` diff --git a/gateway/node_modules/@standard-schema/spec/package.json b/gateway/node_modules/@standard-schema/spec/package.json new file mode 100644 index 0000000000000000000000000000000000000000..62bb5518706bf743a2140f03aa5271507ae9bcdf --- /dev/null +++ b/gateway/node_modules/@standard-schema/spec/package.json @@ -0,0 +1,52 @@ +{ + "name": "@standard-schema/spec", + "description": "A family of specs for interoperable TypeScript", + "version": "1.1.0", + "license": "MIT", + "author": "Colin McDonnell", + "homepage": "https://standardschema.dev", + "repository": { + "type": "git", + "url": "https://github.com/standard-schema/standard-schema" + }, + "keywords": [ + "typescript", + "schema", + "validation", + "standard", + "interface" + ], + "type": "module", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "exports": { + ".": { + "standard-schema-spec": "./src/index.ts", + "import": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + }, + "require": { + "types": "./dist/index.d.cts", + "default": "./dist/index.cjs" + } + } + }, + "sideEffects": false, + "files": [ + "dist" + ], + "publishConfig": { + "access": "public" + }, + "devDependencies": { + "tsup": "^8.3.0", + "typescript": "^5.6.2" + }, + "scripts": { + "lint": "pnpm biome lint ./src", + "format": "pnpm biome format --write ./src", + "check": "pnpm biome check ./src", + "build": "tsup" + } +} \ No newline at end of file diff --git a/gateway/node_modules/@types/webidl-conversions/LICENSE b/gateway/node_modules/@types/webidl-conversions/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..9e841e7a26e4eb057b24511e7b92d42b257a80e5 --- /dev/null +++ b/gateway/node_modules/@types/webidl-conversions/LICENSE @@ -0,0 +1,21 @@ + MIT License + + Copyright (c) Microsoft Corporation. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE diff --git a/gateway/node_modules/@types/webidl-conversions/README.md b/gateway/node_modules/@types/webidl-conversions/README.md new file mode 100644 index 0000000000000000000000000000000000000000..a0f3f9def93b2a0c16b850d1a31b2d11196e820a --- /dev/null +++ b/gateway/node_modules/@types/webidl-conversions/README.md @@ -0,0 +1,15 @@ +# Installation +> `npm install --save @types/webidl-conversions` + +# Summary +This package contains type definitions for webidl-conversions (https://github.com/jsdom/webidl-conversions#readme). + +# Details +Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/webidl-conversions. + +### Additional Details + * Last updated: Tue, 07 Nov 2023 15:11:36 GMT + * Dependencies: none + +# Credits +These definitions were written by [ExE Boss](https://github.com/ExE-Boss), and [BendingBender](https://github.com/BendingBender). diff --git a/gateway/node_modules/@types/webidl-conversions/index.d.ts b/gateway/node_modules/@types/webidl-conversions/index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..bcf395ab201288f1bd5c194757820d2b43311b7f --- /dev/null +++ b/gateway/node_modules/@types/webidl-conversions/index.d.ts @@ -0,0 +1,91 @@ +declare namespace WebIDLConversions { + interface Globals { + [key: string]: unknown; + + Number: (value?: unknown) => number; + String: (value?: unknown) => string; + TypeError: new(message?: string) => TypeError; + } + + interface Options { + context?: string | undefined; + globals?: Globals | undefined; + } + + interface IntegerOptions extends Options { + enforceRange?: boolean | undefined; + clamp?: boolean | undefined; + } + + interface StringOptions extends Options { + treatNullAsEmptyString?: boolean | undefined; + } + + interface BufferSourceOptions extends Options { + allowShared?: boolean | undefined; + } + + type IntegerConversion = (V: unknown, opts?: IntegerOptions) => number; + type StringConversion = (V: unknown, opts?: StringOptions) => string; + type NumberConversion = (V: unknown, opts?: Options) => number; +} + +declare const WebIDLConversions: { + any(V: V, opts?: WebIDLConversions.Options): V; + undefined(V?: unknown, opts?: WebIDLConversions.Options): void; + boolean(V: unknown, opts?: WebIDLConversions.Options): boolean; + + byte(V: unknown, opts?: WebIDLConversions.IntegerOptions): number; + octet(V: unknown, opts?: WebIDLConversions.IntegerOptions): number; + + short(V: unknown, opts?: WebIDLConversions.IntegerOptions): number; + ["unsigned short"](V: unknown, opts?: WebIDLConversions.IntegerOptions): number; + + long(V: unknown, opts?: WebIDLConversions.IntegerOptions): number; + ["unsigned long"](V: unknown, opts?: WebIDLConversions.IntegerOptions): number; + + ["long long"](V: unknown, opts?: WebIDLConversions.IntegerOptions): number; + ["unsigned long long"](V: unknown, opts?: WebIDLConversions.IntegerOptions): number; + + double(V: unknown, opts?: WebIDLConversions.Options): number; + ["unrestricted double"](V: unknown, opts?: WebIDLConversions.Options): number; + + float(V: unknown, opts?: WebIDLConversions.Options): number; + ["unrestricted float"](V: unknown, opts?: WebIDLConversions.Options): number; + + DOMString(V: unknown, opts?: WebIDLConversions.StringOptions): string; + ByteString(V: unknown, opts?: WebIDLConversions.StringOptions): string; + USVString(V: unknown, opts?: WebIDLConversions.StringOptions): string; + + object(V: V, opts?: WebIDLConversions.Options): V extends object ? V : V & object; + ArrayBuffer( + V: unknown, + opts?: WebIDLConversions.BufferSourceOptions & { allowShared?: false | undefined }, + ): ArrayBuffer; + ArrayBuffer(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): ArrayBufferLike; + DataView(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): DataView; + + Int8Array(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): Int8Array; + Int16Array(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): Int16Array; + Int32Array(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): Int32Array; + + Uint8Array(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): Uint8Array; + Uint16Array(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): Uint16Array; + Uint32Array(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): Uint32Array; + Uint8ClampedArray(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): Uint8ClampedArray; + + Float32Array(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): Float32Array; + Float64Array(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): Float64Array; + + ArrayBufferView(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): ArrayBufferView; + BufferSource( + V: unknown, + opts?: WebIDLConversions.BufferSourceOptions & { allowShared?: false | undefined }, + ): ArrayBuffer | ArrayBufferView; + BufferSource(V: unknown, opts?: WebIDLConversions.BufferSourceOptions): ArrayBufferLike | ArrayBufferView; + + DOMTimeStamp(V: unknown, opts?: WebIDLConversions.Options): number; +}; + +// This can't use ES6 style exports, as those can't have spaces in export names. +export = WebIDLConversions; diff --git a/gateway/node_modules/@types/webidl-conversions/package.json b/gateway/node_modules/@types/webidl-conversions/package.json new file mode 100644 index 0000000000000000000000000000000000000000..21fdb9586483712346233bbe2f4776ad1baf6f8a --- /dev/null +++ b/gateway/node_modules/@types/webidl-conversions/package.json @@ -0,0 +1,30 @@ +{ + "name": "@types/webidl-conversions", + "version": "7.0.3", + "description": "TypeScript definitions for webidl-conversions", + "homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/webidl-conversions", + "license": "MIT", + "contributors": [ + { + "name": "ExE Boss", + "githubUsername": "ExE-Boss", + "url": "https://github.com/ExE-Boss" + }, + { + "name": "BendingBender", + "githubUsername": "BendingBender", + "url": "https://github.com/BendingBender" + } + ], + "main": "", + "types": "index.d.ts", + "repository": { + "type": "git", + "url": "https://github.com/DefinitelyTyped/DefinitelyTyped.git", + "directory": "types/webidl-conversions" + }, + "scripts": {}, + "dependencies": {}, + "typesPublisherContentHash": "ff1514e10869784e8b7cca9c4099a4213d3f14b48c198b1bf116300df94bf608", + "typeScriptVersion": "4.5" +} \ No newline at end of file diff --git a/gateway/node_modules/@types/whatwg-url/LICENSE b/gateway/node_modules/@types/whatwg-url/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..9e841e7a26e4eb057b24511e7b92d42b257a80e5 --- /dev/null +++ b/gateway/node_modules/@types/whatwg-url/LICENSE @@ -0,0 +1,21 @@ + MIT License + + Copyright (c) Microsoft Corporation. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE diff --git a/gateway/node_modules/@types/whatwg-url/README.md b/gateway/node_modules/@types/whatwg-url/README.md new file mode 100644 index 0000000000000000000000000000000000000000..1092540db3e47fd0fdfba6ffaa42cfb4eaacf90c --- /dev/null +++ b/gateway/node_modules/@types/whatwg-url/README.md @@ -0,0 +1,15 @@ +# Installation +> `npm install --save @types/whatwg-url` + +# Summary +This package contains type definitions for whatwg-url (https://github.com/jsdom/whatwg-url#readme). + +# Details +Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/whatwg-url. + +### Additional Details + * Last updated: Tue, 12 Nov 2024 00:46:36 GMT + * Dependencies: [@types/webidl-conversions](https://npmjs.com/package/@types/webidl-conversions) + +# Credits +These definitions were written by [Alexander Marks](https://github.com/aomarks), [ExE Boss](https://github.com/ExE-Boss), and [BendingBender](https://github.com/BendingBender). diff --git a/gateway/node_modules/@types/whatwg-url/index.d.ts b/gateway/node_modules/@types/whatwg-url/index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..e8921f847e0e5d4c4707b12c648770b052d387e1 --- /dev/null +++ b/gateway/node_modules/@types/whatwg-url/index.d.ts @@ -0,0 +1,172 @@ +/// +/** https://url.spec.whatwg.org/#url-representation */ +export interface URLRecord { + scheme: string; + username: string; + password: string; + host: string | number | IPv6Address | null; + port: number | null; + path: string | string[]; + query: string | null; + fragment: string | null; +} + +/** https://url.spec.whatwg.org/#concept-ipv6 */ +export type IPv6Address = [number, number, number, number, number, number, number, number]; + +/** https://url.spec.whatwg.org/#url-class */ +export class URL { + constructor(url: string, base?: string | URL); + + static canParse(url: string, base?: string): boolean; + + get href(): string; + set href(V: string); + + get origin(): string; + + get protocol(): string; + set protocol(V: string); + + get username(): string; + set username(V: string); + + get password(): string; + set password(V: string); + + get host(): string; + set host(V: string); + + get hostname(): string; + set hostname(V: string); + + get port(): string; + set port(V: string); + + get pathname(): string; + set pathname(V: string); + + get search(): string; + set search(V: string); + + get searchParams(): URLSearchParams; + + get hash(): string; + set hash(V: string); + + toJSON(): string; + + readonly [Symbol.toStringTag]: "URL"; +} + +/** https://url.spec.whatwg.org/#interface-urlsearchparams */ +export class URLSearchParams { + constructor( + init?: + | ReadonlyArray + | Iterable + | { readonly [name: string]: string } + | string, + ); + + get size(): number; + append(name: string, value: string): void; + delete(name: string, value?: string): void; + get(name: string): string | null; + getAll(name: string): string[]; + has(name: string, value?: string): boolean; + set(name: string, value: string): void; + sort(): void; + + keys(): IterableIterator; + values(): IterableIterator; + entries(): IterableIterator<[name: string, value: string]>; + forEach( + callback: (this: THIS_ARG, value: string, name: string, searchParams: this) => void, + thisArg?: THIS_ARG, + ): void; + + readonly [Symbol.toStringTag]: "URLSearchParams"; + [Symbol.iterator](): IterableIterator<[name: string, value: string]>; +} + +/** https://url.spec.whatwg.org/#concept-url-parser */ +export function parseURL(input: string, options?: { readonly baseURL?: URLRecord | undefined }): URLRecord | null; + +/** https://url.spec.whatwg.org/#concept-basic-url-parser */ +export function basicURLParse( + input: string, + options?: { + baseURL?: URLRecord | undefined; + url?: URLRecord | undefined; + stateOverride?: StateOverride | undefined; + }, +): URLRecord | null; + +/** https://url.spec.whatwg.org/#scheme-start-state */ +export type StateOverride = + | "scheme start" + | "scheme" + | "no scheme" + | "special relative or authority" + | "path or authority" + | "relative" + | "relative slash" + | "special authority slashes" + | "special authority ignore slashes" + | "authority" + | "host" + | "hostname" + | "port" + | "file" + | "file slash" + | "file host" + | "path start" + | "path" + | "opaque path" + | "query" + | "fragment"; + +/** https://url.spec.whatwg.org/#concept-url-serializer */ +export function serializeURL(urlRecord: URLRecord, excludeFragment?: boolean): string; + +/** https://url.spec.whatwg.org/#concept-host-serializer */ +export function serializeHost(host: string | number | IPv6Address): string; + +/** https://url.spec.whatwg.org/#url-path-serializer */ +export function serializePath(urlRecord: URLRecord): string; + +/** https://url.spec.whatwg.org/#serialize-an-integer */ +export function serializeInteger(number: number): string; + +/** https://html.spec.whatwg.org#ascii-serialisation-of-an-origin */ +export function serializeURLOrigin(urlRecord: URLRecord): string; + +/** https://url.spec.whatwg.org/#set-the-username */ +export function setTheUsername(urlRecord: URLRecord, username: string): void; + +/** https://url.spec.whatwg.org/#set-the-password */ +export function setThePassword(urlRecord: URLRecord, password: string): void; + +/** https://url.spec.whatwg.org/#url-opaque-path */ +export function hasAnOpaquePath(urlRecord: URLRecord): boolean; + +/** https://url.spec.whatwg.org/#cannot-have-a-username-password-port */ +export function cannotHaveAUsernamePasswordPort(urlRecord: URLRecord): boolean; + +/** https://url.spec.whatwg.org/#percent-decode */ +export function percentDecodeBytes(buffer: TypedArray): Uint8Array; + +/** https://url.spec.whatwg.org/#string-percent-decode */ +export function percentDecodeString(string: string): Uint8Array; + +export type TypedArray = + | Uint8Array + | Uint8ClampedArray + | Uint16Array + | Uint32Array + | Int8Array + | Int16Array + | Int32Array + | Float32Array + | Float64Array; diff --git a/gateway/node_modules/@types/whatwg-url/package.json b/gateway/node_modules/@types/whatwg-url/package.json new file mode 100644 index 0000000000000000000000000000000000000000..0f1f317d88d328bce5f383df524226ec1905e531 --- /dev/null +++ b/gateway/node_modules/@types/whatwg-url/package.json @@ -0,0 +1,38 @@ +{ + "name": "@types/whatwg-url", + "version": "13.0.0", + "description": "TypeScript definitions for whatwg-url", + "homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/whatwg-url", + "license": "MIT", + "contributors": [ + { + "name": "Alexander Marks", + "githubUsername": "aomarks", + "url": "https://github.com/aomarks" + }, + { + "name": "ExE Boss", + "githubUsername": "ExE-Boss", + "url": "https://github.com/ExE-Boss" + }, + { + "name": "BendingBender", + "githubUsername": "BendingBender", + "url": "https://github.com/BendingBender" + } + ], + "main": "", + "types": "index.d.ts", + "repository": { + "type": "git", + "url": "https://github.com/DefinitelyTyped/DefinitelyTyped.git", + "directory": "types/whatwg-url" + }, + "scripts": {}, + "dependencies": { + "@types/webidl-conversions": "*" + }, + "peerDependencies": {}, + "typesPublisherContentHash": "fd4818c1b74d8ef43c58e984d60d82658280822821b6ea5d4978f4007f29c39c", + "typeScriptVersion": "4.9" +} \ No newline at end of file diff --git a/gateway/node_modules/@types/whatwg-url/webidl2js-wrapper.d.ts b/gateway/node_modules/@types/whatwg-url/webidl2js-wrapper.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..96029b766d880a4b6414a82a8ce64f7ea92609ba --- /dev/null +++ b/gateway/node_modules/@types/whatwg-url/webidl2js-wrapper.d.ts @@ -0,0 +1,4 @@ +import * as URL from "./lib/URL"; +import * as URLSearchParams from "./lib/URLSearchParams"; + +export { URL, URLSearchParams }; diff --git a/gateway/node_modules/bson/LICENSE.md b/gateway/node_modules/bson/LICENSE.md new file mode 100644 index 0000000000000000000000000000000000000000..261eeb9e9f8b2b4b0d119366dda99c6fd7d35c64 --- /dev/null +++ b/gateway/node_modules/bson/LICENSE.md @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/gateway/node_modules/bson/README.md b/gateway/node_modules/bson/README.md new file mode 100644 index 0000000000000000000000000000000000000000..ededa816bbca325895367c9e3bb1e42dcfa0418d --- /dev/null +++ b/gateway/node_modules/bson/README.md @@ -0,0 +1,297 @@ +# BSON parser + +BSON is short for "Binary JSON," and is the binary-encoded serialization of JSON-like documents. +You can learn more about it in [the specification](http://bsonspec.org). + +### Table of Contents + +- [Usage](#usage) +- [Bugs/Feature Requests](#bugs--feature-requests) +- [Installation](#installation) +- [Documentation](#documentation) +- [FAQ](#faq) + + +### Release Integrity + +Releases are created automatically and signed using the [Node team's GPG key](https://pgp.mongodb.com/node-driver.asc). All release packages provided as part of a GitHub release are signed. To verify the provided packages, download the key and import it using gpg: + +```shell +gpg --import node-driver.asc +``` + +The GitHub release contains a detached signature file for the NPM package (named +`bson-X.Y.Z.tgz.sig`). + +The following command returns the link npm package. +```shell +npm view bson@vX.Y.Z dist.tarball +``` + +Using the result of the above command, a `curl` command can return the official npm package for the release. + +To verify the integrity of the downloaded package, run the following command: +```shell +gpg --verify bson-X.Y.Z.tgz.sig bson-X.Y.Z.tgz +``` + +>[!Note] +No GPG verification is done when using npm to install the package. The contents of the GitHub tarball and npm's tarball are identical. + +Releases published to the npm registry also include a [provenance attestation](https://docs.npmjs.com/generating-provenance-statements), which cryptographically links the package to its source repository and build workflow. To verify provenance: + +```shell +npm audit signatures +``` + +## Bugs / Feature Requests + +Think you've found a bug? Want to see a new feature in `bson`? Please open a case in our issue management tool, JIRA: + +1. Create an account and login: [jira.mongodb.org](https://jira.mongodb.org) +2. Navigate to the NODE project: [jira.mongodb.org/browse/NODE](https://jira.mongodb.org/browse/NODE) +3. Click **Create Issue** - Please provide as much information as possible about the issue and how to reproduce it. + +Bug reports in JIRA for the NODE driver project are **public**. + +## Usage + +To build a new version perform the following operations: + +``` +npm install +npm run build +``` + +### Node.js or Bundling Usage + +When using a bundler or Node.js you can import bson using the package name: + +```js +import { BSON, EJSON, ObjectId } from 'bson'; +// or: +// const { BSON, EJSON, ObjectId } = require('bson'); + +const bytes = BSON.serialize({ _id: new ObjectId() }); +console.log(bytes); +const doc = BSON.deserialize(bytes); +console.log(EJSON.stringify(doc)); +// {"_id":{"$oid":"..."}} +``` + +### Browser Usage + +If you are working directly in the browser without a bundler please use the `.mjs` bundle like so: + +```html + +``` + +## Installation + +```sh +npm install bson +``` + +### MongoDB Node.js Driver Version Compatibility + +Only the following version combinations with the [MongoDB Node.js Driver](https://github.com/mongodb/node-mongodb-native) are considered stable. + +| | `bson@1.x` | `bson@4.x` | `bson@5.x` | `bson@6.x` | `bson@7.x` | +| ------------- | ---------- | ---------- | ---------- | ---------- | ---------- | +| `mongodb@7.x` | N/A | N/A | N/A | N/A | ✓ | +| `mongodb@6.x` | N/A | N/A | N/A | ✓ | N/A | +| `mongodb@5.x` | N/A | N/A | ✓ | N/A | N/A | +| `mongodb@4.x` | N/A | ✓ | N/A | N/A | N/A | +| `mongodb@3.x` | ✓ | N/A | N/A | N/A | N/A | + +## Documentation + +### BSON + +[API documentation](https://mongodb.github.io/node-mongodb-native/Next/modules/BSON.html) + + + +### EJSON + +- [EJSON](#EJSON) + + - [.parse(text, [options])](#EJSON.parse) + + - [.stringify(value, [replacer], [space], [options])](#EJSON.stringify) + + - [.serialize(bson, [options])](#EJSON.serialize) + + - [.deserialize(ejson, [options])](#EJSON.deserialize) + + + +#### _EJSON_.parse(text, [options]) + +| Param | Type | Default | Description | +| ----------------- | -------------------- | ----------------- | ---------------------------------------------------------------------------------- | +| text | string | | | +| [options] | object | | Optional settings | +| [options.relaxed] | boolean | true | Attempt to return native JS types where possible, rather than BSON types (if true) | + +Parse an Extended JSON string, constructing the JavaScript value or object described by that +string. + +**Example** + +```js +const { EJSON } = require('bson'); +const text = '{ "int32": { "$numberInt": "10" } }'; + +// prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } } +console.log(EJSON.parse(text, { relaxed: false })); + +// prints { int32: 10 } +console.log(EJSON.parse(text)); +``` + + + +#### _EJSON_.stringify(value, [replacer], [space], [options]) + +| Param | Type | Default | Description | +| ----------------- | ------------------------------------------- | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| value | object | | The value to convert to extended JSON | +| [replacer] | function \| array | | A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string | +| [space] | string \| number | | A String or Number object that's used to insert white space into the output JSON string for readability purposes. | +| [options] | object | | Optional settings | +| [options.relaxed] | boolean | true | Enabled Extended JSON's `relaxed` mode | +| [options.legacy] | boolean | true | Output in Extended JSON v1 | + +Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer +function is specified or optionally including only the specified properties if a replacer array +is specified. + +**Example** + +```js +const { EJSON } = require('bson'); +const Int32 = require('mongodb').Int32; +const doc = { int32: new Int32(10) }; + +// prints '{"int32":{"$numberInt":"10"}}' +console.log(EJSON.stringify(doc, { relaxed: false })); + +// prints '{"int32":10}' +console.log(EJSON.stringify(doc)); +``` + + + +#### _EJSON_.serialize(bson, [options]) + +| Param | Type | Description | +| --------- | ------------------- | ---------------------------------------------------- | +| bson | object | The object to serialize | +| [options] | object | Optional settings passed to the `stringify` function | + +Serializes an object to an Extended JSON string, and reparse it as a JavaScript object. + + + +#### _EJSON_.deserialize(ejson, [options]) + +| Param | Type | Description | +| --------- | ------------------- | -------------------------------------------- | +| ejson | object | The Extended JSON object to deserialize | +| [options] | object | Optional settings passed to the parse method | + +Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types + +## Error Handling + +It is our recommendation to use `BSONError.isBSONError()` checks on errors and to avoid relying on parsing `error.message` and `error.name` strings in your code. We guarantee `BSONError.isBSONError()` checks will pass according to semver guidelines, but errors may be sub-classed or their messages may change at any time, even patch releases, as we see fit to increase the helpfulness of the errors. + +Any new errors we add to the driver will directly extend an existing error class and no existing error will be moved to a different parent class outside of a major release. +This means `BSONError.isBSONError()` will always be able to accurately capture the errors that our BSON library throws. + +Hypothetical example: A collection in our Db has an issue with UTF-8 data: + +```ts +let documentCount = 0; +const cursor = collection.find({}, { utf8Validation: true }); +try { + for await (const doc of cursor) documentCount += 1; +} catch (error) { + if (BSONError.isBSONError(error)) { + console.log(`Found the troublemaker UTF-8!: ${documentCount} ${error.message}`); + return documentCount; + } + throw error; +} +``` + +## React Native + +js-bson requires the `atob`, `btoa` and `TextEncoder` globals. Older versions of React Native did not support these global objects, and so +[js-bson v5.4.0](https://github.com/mongodb/js-bson/releases/tag/v5.4.0) added support for bundled polyfills for these globals. Newer versions +of Hermes includes these globals, and so the polyfills for are no longer needed in the js-bson package. + +If you find yourself on a version of React Native that does not have these globals, either: + +1. polyfill them yourself +2. upgrade to a later version of hermes +3. use a version of js-bson `>=5.4.0` and `<7.0.0` + +One additional polyfill, `crypto.getRandomValues` is recommended and can be installed with the following command: + +```sh +npm install --save react-native-get-random-values +``` + +The following snippet should be placed at the top of the entrypoint (by default this is the root `index.js` file) for React Native projects using the BSON library. These lines must be placed for any code that imports `BSON`. + +```typescript +// Required Polyfills For ReactNative +import 'react-native-get-random-values'; +``` + +Finally, import the `BSON` library like so: + +```typescript +import { BSON, EJSON } from 'bson'; +``` + +This will cause React Native to import the `node_modules/bson/lib/bson.rn.cjs` bundle (see the `"react-native"` setting we have in the `"exports"` section of our [package.json](./package.json).) + +### Technical Note about React Native module import + +The `"exports"` definition in our `package.json` will result in BSON's CommonJS bundle being imported in a React Native project instead of the ES module bundle. Importing the CommonJS bundle is necessary because BSON's ES module bundle of BSON uses top-level await, which is not supported syntax in [React Native's runtime hermes](https://hermesengine.dev/). + +## FAQ + +#### Why does `undefined` get converted to `null`? + +The `undefined` BSON type has been [deprecated for many years](http://bsonspec.org/spec.html), so this library has dropped support for it. Use the `ignoreUndefined` option (for example, from the [driver](http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect) ) to instead remove `undefined` keys. + +#### How do I add custom serialization logic? + +This library looks for `toBSON()` functions on every path, and calls the `toBSON()` function to get the value to serialize. + +```javascript +const BSON = require('bson'); + +class CustomSerialize { + toBSON() { + return 42; + } +} + +const obj = { answer: new CustomSerialize() }; +// "{ answer: 42 }" +console.log(BSON.deserialize(BSON.serialize(obj))); +``` diff --git a/gateway/node_modules/bson/bson.d.ts b/gateway/node_modules/bson/bson.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..2f57468a734209acd17020234c18bf5e6cfd829f --- /dev/null +++ b/gateway/node_modules/bson/bson.d.ts @@ -0,0 +1,1901 @@ +/** + * A class representation of the BSON Binary type. + * @public + * @category BSONType + */ +export declare class Binary extends BSONValue { + get _bsontype(): 'Binary'; + /* Excluded from this release type: BSON_BINARY_SUBTYPE_DEFAULT */ + /** Initial buffer default size */ + static readonly BUFFER_SIZE = 256; + /** Default BSON type */ + static readonly SUBTYPE_DEFAULT = 0; + /** Function BSON type */ + static readonly SUBTYPE_FUNCTION = 1; + /** + * Legacy default BSON Binary type + * @deprecated BSON Binary subtype 2 is deprecated in the BSON specification + */ + static readonly SUBTYPE_BYTE_ARRAY = 2; + /** Deprecated UUID BSON type @deprecated Please use SUBTYPE_UUID */ + static readonly SUBTYPE_UUID_OLD = 3; + /** UUID BSON type */ + static readonly SUBTYPE_UUID = 4; + /** MD5 BSON type */ + static readonly SUBTYPE_MD5 = 5; + /** Encrypted BSON type */ + static readonly SUBTYPE_ENCRYPTED = 6; + /** Column BSON type */ + static readonly SUBTYPE_COLUMN = 7; + /** Sensitive BSON type */ + static readonly SUBTYPE_SENSITIVE = 8; + /** Vector BSON type */ + static readonly SUBTYPE_VECTOR = 9; + /** User BSON type */ + static readonly SUBTYPE_USER_DEFINED = 128; + /** datatype of a Binary Vector (subtype: 9) */ + static readonly VECTOR_TYPE: Readonly<{ + readonly Int8: 3; + readonly Float32: 39; + readonly PackedBit: 16; + }>; + /** + * The bytes of the Binary value. + * + * The format of a Binary value in BSON is defined as: + * ```txt + * binary ::= int32 subtype (byte*) + * ``` + * + * This `buffer` is the "(byte*)" segment. + * + * Unless the value is subtype 2, then deserialize will read the first 4 bytes as an int32 and set this to the remaining bytes. + * + * ```txt + * binary ::= int32 unsigned_byte(2) int32 (byte*) + * ``` + * + * @see https://bsonspec.org/spec.html + */ + buffer: Uint8Array; + /** + * The binary subtype. + * + * Current defined values are: + * + * - `unsigned_byte(0)` Generic binary subtype + * - `unsigned_byte(1)` Function + * - `unsigned_byte(2)` Binary (Deprecated) + * - `unsigned_byte(3)` UUID (Deprecated) + * - `unsigned_byte(4)` UUID + * - `unsigned_byte(5)` MD5 + * - `unsigned_byte(6)` Encrypted BSON value + * - `unsigned_byte(7)` Compressed BSON column + * - `unsigned_byte(8)` Sensitive + * - `unsigned_byte(9)` Vector + * - `unsigned_byte(128)` - `unsigned_byte(255)` User defined + */ + sub_type: number; + /** + * The Binary's `buffer` can be larger than the Binary's content. + * This property is used to determine where the content ends in the buffer. + */ + position: number; + /** + * Create a new Binary instance. + * @param buffer - a buffer object containing the binary data. + * @param subType - the option binary type. + */ + constructor(buffer?: BinarySequence, subType?: number); + /** + * Updates this binary with byte_value. + * + * @param byteValue - a single byte we wish to write. + */ + put(byteValue: string | number | Uint8Array | number[]): void; + /** + * Writes a buffer to the binary. + * + * @param sequence - a string or buffer to be written to the Binary BSON object. + * @param offset - specify the binary of where to write the content. + */ + write(sequence: BinarySequence, offset: number): void; + /** + * Returns a view of **length** bytes starting at **position**. + * + * @param position - read from the given position in the Binary. + * @param length - the number of bytes to read. + */ + read(position: number, length: number): Uint8Array; + /** returns a view of the binary value as a Uint8Array */ + value(): Uint8Array; + /** the length of the binary sequence */ + length(): number; + toJSON(): string; + toString(encoding?: 'hex' | 'base64' | 'utf8' | 'utf-8'): string; + /* Excluded from this release type: toExtendedJSON */ + toUUID(): UUID; + /** Creates an Binary instance from a hex digit string */ + static createFromHexString(hex: string, subType?: number): Binary; + /** Creates an Binary instance from a base64 string */ + static createFromBase64(base64: string, subType?: number): Binary; + /* Excluded from this release type: fromExtendedJSON */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; + /** + * If this Binary represents a Int8 Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.Int8`), + * returns a copy of the bytes in a new Int8Array. + * + * If the Binary is not a Vector, or the datatype is not Int8, an error is thrown. + */ + toInt8Array(): Int8Array; + /** + * If this Binary represents a Float32 Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.Float32`), + * returns a copy of the bytes in a new Float32Array. + * + * If the Binary is not a Vector, or the datatype is not Float32, an error is thrown. + */ + toFloat32Array(): Float32Array; + /** + * If this Binary represents packed bit Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.PackedBit`), + * returns a copy of the bytes that are packed bits. + * + * Use `toBits` to get the unpacked bits. + * + * If the Binary is not a Vector, or the datatype is not PackedBit, an error is thrown. + */ + toPackedBits(): Uint8Array; + /** + * If this Binary represents a Packed bit Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.PackedBit`), + * returns a copy of the bit unpacked into a new Int8Array. + * + * Use `toPackedBits` to get the bits still in packed form. + * + * If the Binary is not a Vector, or the datatype is not PackedBit, an error is thrown. + */ + toBits(): Int8Array; + /** + * Constructs a Binary representing an Int8 Vector. + * @param array - The array to store as a view on the Binary class + */ + static fromInt8Array(array: Int8Array): Binary; + /** Constructs a Binary representing an Float32 Vector. */ + static fromFloat32Array(array: Float32Array): Binary; + /** + * Constructs a Binary representing a packed bit Vector. + * + * Use `fromBits` to pack an array of 1s and 0s. + */ + static fromPackedBits(array: Uint8Array, padding?: number): Binary; + /** + * Constructs a Binary representing an Packed Bit Vector. + * @param array - The array of 1s and 0s to pack into the Binary instance + */ + static fromBits(bits: ArrayLike): Binary; +} + +/** @public */ +export declare interface BinaryExtended { + $binary: { + subType: string; + base64: string; + }; +} + +/** @public */ +export declare interface BinaryExtendedLegacy { + $type: string; + $binary: string; +} + +/** @public */ +export declare type BinarySequence = Uint8Array | number[]; + +declare namespace BSON { + export { + setInternalBufferSize, + serialize, + serializeWithBufferAndIndex, + deserialize, + calculateObjectSize, + deserializeStream, + UUIDExtended, + BinaryExtended, + BinaryExtendedLegacy, + BinarySequence, + CodeExtended, + DBRefLike, + Decimal128Extended, + DoubleExtended, + EJSONOptions, + EJSONOptionsBase, + EJSONSerializeOptions, + EJSONParseOptions, + Int32Extended, + LongExtended, + MaxKeyExtended, + MinKeyExtended, + ObjectIdExtended, + ObjectIdLike, + BSONRegExpExtended, + BSONRegExpExtendedLegacy, + BSONSymbolExtended, + LongWithoutOverrides, + TimestampExtended, + TimestampOverrides, + LongWithoutOverridesClass, + SerializeOptions, + DeserializeOptions, + Code, + BSONSymbol, + DBRef, + Binary, + ObjectId, + UUID, + Long, + Timestamp, + Double, + Int32, + MinKey, + MaxKey, + BSONRegExp, + Decimal128, + NumberUtils, + ByteUtils, + BSONValue, + bsonType, + BSONTypeTag, + BSONError, + BSONVersionError, + BSONRuntimeError, + BSONOffsetError, + BSONType, + EJSON, + onDemand, + OnDemand, + Document, + CalculateObjectSizeOptions + } +} +export { BSON } + +/* Excluded from this release type: BSON_MAJOR_VERSION */ + +/* Excluded from this release type: BSON_VERSION_SYMBOL */ + +/** + * @public + * @experimental + */ +declare type BSONElement = [ +type: number, +nameOffset: number, +nameLength: number, +offset: number, +length: number +]; + +/** + * @public + * @category Error + * + * `BSONError` objects are thrown when BSON encounters an error. + * + * This is the parent class for all the other errors thrown by this library. + */ +export declare class BSONError extends Error { + /* Excluded from this release type: bsonError */ + get name(): string; + constructor(message: string, options?: { + cause?: unknown; + }); + /** + * @public + * + * All errors thrown from the BSON library inherit from `BSONError`. + * This method can assist with determining if an error originates from the BSON library + * even if it does not pass an `instanceof` check against this class' constructor. + * + * @param value - any javascript value that needs type checking + */ + static isBSONError(value: unknown): value is BSONError; +} + +/** + * @public + * @category Error + * + * @experimental + * + * An error generated when BSON bytes are invalid. + * Reports the offset the parser was able to reach before encountering the error. + */ +export declare class BSONOffsetError extends BSONError { + get name(): 'BSONOffsetError'; + offset: number; + constructor(message: string, offset: number, options?: { + cause?: unknown; + }); +} + +/** + * A class representation of the BSON RegExp type. + * @public + * @category BSONType + */ +export declare class BSONRegExp extends BSONValue { + get _bsontype(): 'BSONRegExp'; + pattern: string; + options: string; + /** + * @param pattern - The regular expression pattern to match + * @param options - The regular expression options + */ + constructor(pattern: string, options?: string); + static parseOptions(options?: string): string; + /* Excluded from this release type: toExtendedJSON */ + /* Excluded from this release type: fromExtendedJSON */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; +} + +/** @public */ +export declare interface BSONRegExpExtended { + $regularExpression: { + pattern: string; + options: string; + }; +} + +/** @public */ +export declare interface BSONRegExpExtendedLegacy { + $regex: string | BSONRegExp; + $options: string; +} + +/** + * @public + * @category Error + * + * An error generated when BSON functions encounter an unexpected input + * or reaches an unexpected/invalid internal state + * + */ +export declare class BSONRuntimeError extends BSONError { + get name(): 'BSONRuntimeError'; + constructor(message: string); +} + +/** + * A class representation of the BSON Symbol type. + * @public + * @category BSONType + */ +export declare class BSONSymbol extends BSONValue { + get _bsontype(): 'BSONSymbol'; + value: string; + /** + * @param value - the string representing the symbol. + */ + constructor(value: string); + /** Access the wrapped string value. */ + valueOf(): string; + toString(): string; + toJSON(): string; + /* Excluded from this release type: toExtendedJSON */ + /* Excluded from this release type: fromExtendedJSON */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; +} + +/** @public */ +export declare interface BSONSymbolExtended { + $symbol: string; +} + +/** @public */ +export declare const BSONType: Readonly<{ + readonly double: 1; + readonly string: 2; + readonly object: 3; + readonly array: 4; + readonly binData: 5; + readonly undefined: 6; + readonly objectId: 7; + readonly bool: 8; + readonly date: 9; + readonly null: 10; + readonly regex: 11; + readonly dbPointer: 12; + readonly javascript: 13; + readonly symbol: 14; + readonly javascriptWithScope: 15; + readonly int: 16; + readonly timestamp: 17; + readonly long: 18; + readonly decimal: 19; + readonly minKey: -1; + readonly maxKey: 127; +}>; + +/** @public */ +export declare type BSONType = (typeof BSONType)[keyof typeof BSONType]; + +/** @public */ +export declare const bsonType: unique symbol; + +/** @public */ +export declare type BSONTypeTag = 'BSONRegExp' | 'BSONSymbol' | 'ObjectId' | 'Binary' | 'Decimal128' | 'Double' | 'Int32' | 'Long' | 'MaxKey' | 'MinKey' | 'Timestamp' | 'Code' | 'DBRef'; + +/** @public */ +export declare abstract class BSONValue { + /** @public */ + abstract get _bsontype(): BSONTypeTag; + get [bsonType](): this['_bsontype']; + /* Excluded from this release type: [BSON_VERSION_SYMBOL] */ + /** + * @public + * Prints a human-readable string of BSON value information + * If invoked manually without node.js.inspect function, this will default to a modified JSON.stringify + */ + abstract inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; + /* Excluded from this release type: toExtendedJSON */ +} + +/** + * @public + * @category Error + */ +export declare class BSONVersionError extends BSONError { + get name(): 'BSONVersionError'; + constructor(); +} + +/** + * @public + * @experimental + * + * A collection of functions that help work with data in a Uint8Array. + * ByteUtils is configured at load time to use Node.js or Web based APIs for the internal implementations. + */ +export declare type ByteUtils = { + /** Checks if the given value is a Uint8Array. */ + isUint8Array: (value: unknown) => value is Uint8Array; + /** Transforms the input to an instance of Buffer if running on node, otherwise Uint8Array */ + toLocalBufferType: (buffer: Uint8Array | ArrayBufferView | ArrayBuffer) => Uint8Array; + /** Create empty space of size */ + allocate: (size: number) => Uint8Array; + /** Create empty space of size, use pooled memory when available */ + allocateUnsafe: (size: number) => Uint8Array; + /** Compare 2 Uint8Arrays lexicographically */ + compare: (buffer1: Uint8Array, buffer2: Uint8Array) => -1 | 0 | 1; + /** Concatenating all the Uint8Arrays in new Uint8Array. */ + concat: (list: Uint8Array[]) => Uint8Array; + /** Copy bytes from source Uint8Array to target Uint8Array */ + copy: (source: Uint8Array, target: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number) => number; + /** Check if two Uint8Arrays are deep equal */ + equals: (a: Uint8Array, b: Uint8Array) => boolean; + /** Create a Uint8Array from an array of numbers */ + fromNumberArray: (array: number[]) => Uint8Array; + /** Create a Uint8Array from a base64 string */ + fromBase64: (base64: string) => Uint8Array; + /** Create a Uint8Array from a UTF8 string */ + fromUTF8: (utf8: string) => Uint8Array; + /** Create a base64 string from bytes */ + toBase64: (buffer: Uint8Array) => string; + /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ + fromISO88591: (codePoints: string) => Uint8Array; + /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ + toISO88591: (buffer: Uint8Array) => string; + /** Create a Uint8Array from a hex string */ + fromHex: (hex: string) => Uint8Array; + /** Create a lowercase hex string from bytes */ + toHex: (buffer: Uint8Array) => string; + /** Create a string from utf8 code units, fatal=true will throw an error if UTF-8 bytes are invalid, fatal=false will insert replacement characters */ + toUTF8: (buffer: Uint8Array, start: number, end: number, fatal: boolean) => string; + /** Get the utf8 code unit count from a string if it were to be transformed to utf8 */ + utf8ByteLength: (input: string) => number; + /** Encode UTF8 bytes generated from `source` string into `destination` at byteOffset. Returns the number of bytes encoded. */ + encodeUTF8Into: (destination: Uint8Array, source: string, byteOffset: number) => number; + /** Generate a Uint8Array filled with random bytes with byteLength */ + randomBytes: (byteLength: number) => Uint8Array; + /** Interprets `buffer` as an array of 32-bit values and swaps the byte order in-place. */ + swap32: (buffer: Uint8Array) => Uint8Array; +}; + +/** + * This is the only ByteUtils that should be used across the rest of the BSON library. + * + * The type annotation is important here, it asserts that each of the platform specific + * utils implementations are compatible with the common one. + * + * @public + * @experimental + */ +export declare const ByteUtils: ByteUtils; + +/** + * Calculate the bson size for a passed in Javascript object. + * + * @param object - the Javascript object to calculate the BSON byte size for + * @returns size of BSON object in bytes + * @public + */ +export declare function calculateObjectSize(object: Document, options?: CalculateObjectSizeOptions): number; + +/** @public */ +export declare type CalculateObjectSizeOptions = Pick; + +/** + * A class representation of the BSON Code type. + * @public + * @category BSONType + */ +export declare class Code extends BSONValue { + get _bsontype(): 'Code'; + code: string; + scope: Document | null; + /** + * @param code - a string or function. + * @param scope - an optional scope for the function. + */ + constructor(code: string | Function, scope?: Document | null); + toJSON(): { + code: string; + scope?: Document; + }; + /* Excluded from this release type: toExtendedJSON */ + /* Excluded from this release type: fromExtendedJSON */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; +} + +/** @public */ +export declare interface CodeExtended { + $code: string; + $scope?: Document; +} + +/** + * A class representation of the BSON DBRef type. + * @public + * @category BSONType + */ +export declare class DBRef extends BSONValue { + get _bsontype(): 'DBRef'; + collection: string; + oid: ObjectId; + db?: string; + fields: Document; + /** + * @param collection - the collection name. + * @param oid - the reference ObjectId. + * @param db - optional db name, if omitted the reference is local to the current db. + */ + constructor(collection: string, oid: ObjectId, db?: string, fields?: Document); + /* Excluded from this release type: namespace */ + /* Excluded from this release type: namespace */ + toJSON(): DBRefLike & Document; + /* Excluded from this release type: toExtendedJSON */ + /* Excluded from this release type: fromExtendedJSON */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; +} + +/** @public */ +export declare interface DBRefLike { + $ref: string; + $id: ObjectId; + $db?: string; +} + +/** + * A class representation of the BSON Decimal128 type. + * @public + * @category BSONType + */ +export declare class Decimal128 extends BSONValue { + get _bsontype(): 'Decimal128'; + readonly bytes: Uint8Array; + /** + * @param bytes - a buffer containing the raw Decimal128 bytes in little endian order, + * or a string representation as returned by .toString() + */ + constructor(bytes: Uint8Array | string); + /** + * Create a Decimal128 instance from a string representation + * + * @param representation - a numeric string representation. + */ + static fromString(representation: string): Decimal128; + /** + * Create a Decimal128 instance from a string representation, allowing for rounding to 34 + * significant digits + * + * @example Example of a number that will be rounded + * ```ts + * > let d = Decimal128.fromString('37.499999999999999196428571428571375') + * Uncaught: + * BSONError: "37.499999999999999196428571428571375" is not a valid Decimal128 string - inexact rounding + * at invalidErr (/home/wajames/js-bson/lib/bson.cjs:1402:11) + * at Decimal128.fromStringInternal (/home/wajames/js-bson/lib/bson.cjs:1633:25) + * at Decimal128.fromString (/home/wajames/js-bson/lib/bson.cjs:1424:27) + * + * > d = Decimal128.fromStringWithRounding('37.499999999999999196428571428571375') + * new Decimal128("37.49999999999999919642857142857138") + * ``` + * @param representation - a numeric string representation. + */ + static fromStringWithRounding(representation: string): Decimal128; + private static _fromString; + /** Create a string representation of the raw Decimal128 value */ + toString(): string; + toJSON(): Decimal128Extended; + /* Excluded from this release type: toExtendedJSON */ + /* Excluded from this release type: fromExtendedJSON */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; +} + +/** @public */ +export declare interface Decimal128Extended { + $numberDecimal: string; +} + +/** + * Deserialize data as BSON. + * + * @param buffer - the buffer containing the serialized set of BSON documents. + * @returns returns the deserialized Javascript Object. + * @public + */ +export declare function deserialize(buffer: Uint8Array, options?: DeserializeOptions): Document; + +/** @public */ +export declare interface DeserializeOptions { + /** + * when deserializing a Long return as a BigInt. + * @defaultValue `false` + */ + useBigInt64?: boolean; + /** + * when deserializing a Long will fit it into a Number if it's smaller than 53 bits. + * @defaultValue `true` + */ + promoteLongs?: boolean; + /** + * when deserializing a Binary will return it as a node.js Buffer instance. + * @defaultValue `false` + */ + promoteBuffers?: boolean; + /** + * when deserializing will promote BSON values to their Node.js closest equivalent types. + * @defaultValue `true` + */ + promoteValues?: boolean; + /** + * allow to specify if there what fields we wish to return as unserialized raw buffer. + * @defaultValue `null` + */ + fieldsAsRaw?: Document; + /** + * return BSON regular expressions as BSONRegExp instances. + * @defaultValue `false` + */ + bsonRegExp?: boolean; + /** + * allows the buffer to be larger than the parsed BSON object. + * @defaultValue `false` + */ + allowObjectSmallerThanBufferSize?: boolean; + /** + * Offset into buffer to begin reading document from + * @defaultValue `0` + */ + index?: number; + raw?: boolean; + /** Allows for opt-out utf-8 validation for all keys or + * specified keys. Must be all true or all false. + * + * @example + * ```js + * // disables validation on all keys + * validation: { utf8: false } + * + * // enables validation only on specified keys a, b, and c + * validation: { utf8: { a: true, b: true, c: true } } + * + * // disables validation only on specified keys a, b + * validation: { utf8: { a: false, b: false } } + * ``` + */ + validation?: { + utf8: boolean | Record | Record; + }; +} + +/** + * Deserialize stream data as BSON documents. + * + * @param data - the buffer containing the serialized set of BSON documents. + * @param startIndex - the start index in the data Buffer where the deserialization is to start. + * @param numberOfDocuments - number of documents to deserialize. + * @param documents - an array where to store the deserialized documents. + * @param docStartIndex - the index in the documents array from where to start inserting documents. + * @param options - additional options used for the deserialization. + * @returns next index in the buffer after deserialization **x** numbers of documents. + * @public + */ +export declare function deserializeStream(data: Uint8Array | ArrayBuffer, startIndex: number, numberOfDocuments: number, documents: Document[], docStartIndex: number, options: DeserializeOptions): number; + +/** @public */ +export declare interface Document { + [key: string]: any; +} + +/** + * A class representation of the BSON Double type. + * @public + * @category BSONType + */ +export declare class Double extends BSONValue { + get _bsontype(): 'Double'; + value: number; + /** + * Create a Double type + * + * @param value - the number we want to represent as a double. + */ + constructor(value: number); + /** + * Attempt to create an double type from string. + * + * This method will throw a BSONError on any string input that is not representable as a IEEE-754 64-bit double. + * Notably, this method will also throw on the following string formats: + * - Strings in non-decimal and non-exponential formats (binary, hex, or octal digits) + * - Strings with characters other than numeric, floating point, or leading sign characters (Note: 'Infinity', '-Infinity', and 'NaN' input strings are still allowed) + * - Strings with leading and/or trailing whitespace + * + * Strings with leading zeros, however, are also allowed + * + * @param value - the string we want to represent as a double. + */ + static fromString(value: string): Double; + /** + * Access the number value. + * + * @returns returns the wrapped double number. + */ + valueOf(): number; + toJSON(): number; + toString(radix?: number): string; + /* Excluded from this release type: toExtendedJSON */ + /* Excluded from this release type: fromExtendedJSON */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; +} + +/** @public */ +export declare interface DoubleExtended { + $numberDouble: string; +} + +/** @public */ +export declare const EJSON: { + parse: typeof parse; + stringify: typeof stringify; + serialize: typeof EJSONserialize; + deserialize: typeof EJSONdeserialize; +}; + +/** + * Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types + * + * @param ejson - The Extended JSON object to deserialize + * @param options - Optional settings passed to the parse method + */ +declare function EJSONdeserialize(ejson: Document, options?: EJSONParseOptions): any; + +/** @public */ +export declare type EJSONOptions = EJSONSerializeOptions & EJSONParseOptions; + +/** @public */ +export declare type EJSONOptionsBase = { + /** + * Output using the Extended JSON v1 spec + * @defaultValue `false` + */ + legacy?: boolean; + /** + * Enable Extended JSON's `relaxed` mode, which attempts to return native JS types where possible, rather than BSON types + * @defaultValue `false` + */ + relaxed?: boolean; +}; + +/** @public */ +export declare type EJSONParseOptions = EJSONOptionsBase & { + /** + * Enable native bigint support + * @defaultValue `false` + */ + useBigInt64?: boolean; +}; + +/** + * Serializes an object to an Extended JSON string, and reparse it as a JavaScript object. + * + * @param value - The object to serialize + * @param options - Optional settings passed to the `stringify` function + */ +declare function EJSONserialize(value: any, options?: EJSONSerializeOptions): Document; + +/** @public */ +export declare type EJSONSerializeOptions = EJSONOptionsBase & { + /** + * Omits undefined values from the output instead of converting them to null + * @defaultValue `false` + */ + ignoreUndefined?: boolean; +}; + +declare type InspectFn = (x: unknown, options?: unknown) => string; + +/** + * A class representation of a BSON Int32 type. + * @public + * @category BSONType + */ +export declare class Int32 extends BSONValue { + get _bsontype(): 'Int32'; + value: number; + /** + * Create an Int32 type + * + * @param value - the number we want to represent as an int32. + */ + constructor(value: number | string); + /** + * Attempt to create an Int32 type from string. + * + * This method will throw a BSONError on any string input that is not representable as an Int32. + * Notably, this method will also throw on the following string formats: + * - Strings in non-decimal formats (exponent notation, binary, hex, or octal digits) + * - Strings non-numeric and non-leading sign characters (ex: '2.0', '24,000') + * - Strings with leading and/or trailing whitespace + * + * Strings with leading zeros, however, are allowed. + * + * @param value - the string we want to represent as an int32. + */ + static fromString(value: string): Int32; + /** + * Access the number value. + * + * @returns returns the wrapped int32 number. + */ + valueOf(): number; + toString(radix?: number): string; + toJSON(): number; + /* Excluded from this release type: toExtendedJSON */ + /* Excluded from this release type: fromExtendedJSON */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; +} + +/** @public */ +export declare interface Int32Extended { + $numberInt: string; +} + +/** + * A class representing a 64-bit integer + * @public + * @category BSONType + * @remarks + * The internal representation of a long is the two given signed, 32-bit values. + * We use 32-bit pieces because these are the size of integers on which + * Javascript performs bit-operations. For operations like addition and + * multiplication, we split each number into 16 bit pieces, which can easily be + * multiplied within Javascript's floating-point representation without overflow + * or change in sign. + * In the algorithms below, we frequently reduce the negative case to the + * positive case by negating the input(s) and then post-processing the result. + * Note that we must ALWAYS check specially whether those values are MIN_VALUE + * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as + * a positive number, it overflows back into a negative). Not handling this + * case would often result in infinite recursion. + * Common constant values ZERO, ONE, NEG_ONE, etc. are found as static properties on this class. + */ +export declare class Long extends BSONValue { + get _bsontype(): 'Long'; + /** An indicator used to reliably determine if an object is a Long or not. */ + get __isLong__(): boolean; + /** + * The high 32 bits as a signed value. + */ + high: number; + /** + * The low 32 bits as a signed value. + */ + low: number; + /** + * Whether unsigned or not. + */ + unsigned: boolean; + /** + * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers. + * + * @param low - The low (signed) 32 bits of the long + * @param high - The high (signed) 32 bits of the long + * @param unsigned - Whether unsigned or not, defaults to signed + */ + constructor(low: number, high?: number, unsigned?: boolean); + /** + * Constructs a 64 bit two's-complement integer, given a bigint representation. + * + * @param value - BigInt representation of the long value + * @param unsigned - Whether unsigned or not, defaults to signed + */ + constructor(value: bigint, unsigned?: boolean); + /** + * Constructs a 64 bit two's-complement integer, given a string representation. + * + * @param value - String representation of the long value + * @param unsigned - Whether unsigned or not, defaults to signed + */ + constructor(value: string, unsigned?: boolean); + static TWO_PWR_24: Long; + /** Maximum unsigned value. */ + static MAX_UNSIGNED_VALUE: Long; + /** Signed zero */ + static ZERO: Long; + /** Unsigned zero. */ + static UZERO: Long; + /** Signed one. */ + static ONE: Long; + /** Unsigned one. */ + static UONE: Long; + /** Signed negative one. */ + static NEG_ONE: Long; + /** Maximum signed value. */ + static MAX_VALUE: Long; + /** Minimum signed value. */ + static MIN_VALUE: Long; + /** + * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. + * Each is assumed to use 32 bits. + * @param lowBits - The low 32 bits + * @param highBits - The high 32 bits + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromBits(lowBits: number, highBits: number, unsigned?: boolean): Long; + /** + * Returns a Long representing the given 32 bit integer value. + * @param value - The 32 bit integer in question + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromInt(value: number, unsigned?: boolean): Long; + /** + * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. + * @param value - The number in question + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromNumber(value: number, unsigned?: boolean): Long; + /** + * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. + * @param value - The number in question + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromBigInt(value: bigint, unsigned?: boolean): Long; + /* Excluded from this release type: _fromString */ + /** + * Returns a signed Long representation of the given string, written using radix 10. + * Will throw an error if the given text is not exactly representable as a Long. + * Throws an error if any of the following conditions are true: + * - the string contains invalid characters for the radix 10 + * - the string contains whitespace + * - the value the string represents is too large or too small to be a Long + * Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero + * @param str - The textual representation of the Long + * @returns The corresponding Long value + */ + static fromStringStrict(str: string): Long; + /** + * Returns a Long representation of the given string, written using the radix 10. + * Will throw an error if the given parameters are not exactly representable as a Long. + * Throws an error if any of the following conditions are true: + * - the string contains invalid characters for the given radix + * - the string contains whitespace + * - the value the string represents is too large or too small to be a Long + * Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero + * @param str - The textual representation of the Long + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromStringStrict(str: string, unsigned?: boolean): Long; + /** + * Returns a signed Long representation of the given string, written using the specified radix. + * Will throw an error if the given parameters are not exactly representable as a Long. + * Throws an error if any of the following conditions are true: + * - the string contains invalid characters for the given radix + * - the string contains whitespace + * - the value the string represents is too large or too small to be a Long + * Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero + * @param str - The textual representation of the Long + * @param radix - The radix in which the text is written (2-36), defaults to 10 + * @returns The corresponding Long value + */ + static fromStringStrict(str: string, radix?: boolean): Long; + /** + * Returns a Long representation of the given string, written using the specified radix. + * Will throw an error if the given parameters are not exactly representable as a Long. + * Throws an error if any of the following conditions are true: + * - the string contains invalid characters for the given radix + * - the string contains whitespace + * - the value the string represents is too large or too small to be a Long + * Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero + * @param str - The textual representation of the Long + * @param unsigned - Whether unsigned or not, defaults to signed + * @param radix - The radix in which the text is written (2-36), defaults to 10 + * @returns The corresponding Long value + */ + static fromStringStrict(str: string, unsigned?: boolean, radix?: number): Long; + /** + * Returns a signed Long representation of the given string, written using radix 10. + * + * If the input string is empty, this function will throw a BSONError. + * + * If input string does not have valid signed 64-bit Long representation, this method will return a coerced value: + * - inputs that overflow 64-bit signed long will be coerced to Long.MAX_VALUE and Long.MIN_VALUE respectively + * - 'NaN' or '+/-Infinity' are coerced to Long.ZERO + * - other invalid characters sequences have variable behavior + * + * @param str - The textual representation of the Long + * @returns The corresponding Long value + */ + static fromString(str: string): Long; + /** + * Returns a signed Long representation of the given string, written using the provided radix. + * + * If the input string is empty or a provided radix is not within (2-36), this function will throw a BSONError. + * + * If input parameters do not have valid signed 64-bit Long representation, this method will return a coerced value: + * - inputs that overflow 64-bit signed long will be coerced to Long.MAX_VALUE and Long.MIN_VALUE respectively + * - if the radix is less than 24, 'NaN' is coerced to Long.ZERO + * - if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO + * - other invalid characters sequences have variable behavior + * @param str - The textual representation of the Long + * @param radix - The radix in which the text is written (2-36), defaults to 10 + * @returns The corresponding Long value + */ + static fromString(str: string, radix?: number): Long; + /** + * Returns a Long representation of the given string, written using radix 10. + * + * If the input string is empty, this function will throw a BSONError. + * + * If input parameters do not have a valid 64-bit Long representation, this method will return a coerced value: + * - inputs that overflow 64-bit long will be coerced to max or min (if signed) values + * - if the radix is less than 24, 'NaN' is coerced to Long.ZERO + * - if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO + * - other invalid characters sequences have variable behavior + * @param str - The textual representation of the Long + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromString(str: string, unsigned?: boolean): Long; + /** + * Returns a Long representation of the given string, written using the specified radix. + * + * If the input string is empty or a provided radix is not within (2-36), this function will throw a BSONError. + * + * If input parameters do not have a valid 64-bit Long representation, this method will return a coerced value: + * - inputs that overflow 64-bit long will be coerced to max or min (if signed) values + * - if the radix is less than 24, 'NaN' is coerced to Long.ZERO + * - if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO + * - other invalid characters sequences have variable behavior + * @param str - The textual representation of the Long + * @param unsigned - Whether unsigned or not, defaults to signed + * @param radix - The radix in which the text is written (2-36), defaults to 10 + * @returns The corresponding Long value + */ + static fromString(str: string, unsigned?: boolean, radix?: number): Long; + /** + * Creates a Long from its byte representation. + * @param bytes - Byte representation + * @param unsigned - Whether unsigned or not, defaults to signed + * @param le - Whether little or big endian, defaults to big endian + * @returns The corresponding Long value + */ + static fromBytes(bytes: number[], unsigned?: boolean, le?: boolean): Long; + /** + * Creates a Long from its little endian byte representation. + * @param bytes - Little endian byte representation + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromBytesLE(bytes: number[], unsigned?: boolean): Long; + /** + * Creates a Long from its big endian byte representation. + * @param bytes - Big endian byte representation + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromBytesBE(bytes: number[], unsigned?: boolean): Long; + /** + * Tests if the specified object is a Long. + */ + static isLong(value: unknown): value is Long; + /** + * Converts the specified value to a Long. + * @param unsigned - Whether unsigned or not, defaults to signed + */ + static fromValue(val: number | string | { + low: number; + high: number; + unsigned?: boolean; + }, unsigned?: boolean): Long; + /** Returns the sum of this and the specified Long. */ + add(addend: string | number | Long | Timestamp): Long; + /** + * Returns the sum of this and the specified Long. + * @returns Sum + */ + and(other: string | number | Long | Timestamp): Long; + /** + * Compares this Long's value with the specified's. + * @returns 0 if they are the same, 1 if the this is greater and -1 if the given one is greater + */ + compare(other: string | number | Long | Timestamp): 0 | 1 | -1; + /** This is an alias of {@link Long.compare} */ + comp(other: string | number | Long | Timestamp): 0 | 1 | -1; + /** + * Returns this Long divided by the specified. The result is signed if this Long is signed or unsigned if this Long is unsigned. + * @returns Quotient + */ + divide(divisor: string | number | Long | Timestamp): Long; + /**This is an alias of {@link Long.divide} */ + div(divisor: string | number | Long | Timestamp): Long; + /** + * Tests if this Long's value equals the specified's. + * @param other - Other value + */ + equals(other: string | number | Long | Timestamp): boolean; + /** This is an alias of {@link Long.equals} */ + eq(other: string | number | Long | Timestamp): boolean; + /** Gets the high 32 bits as a signed integer. */ + getHighBits(): number; + /** Gets the high 32 bits as an unsigned integer. */ + getHighBitsUnsigned(): number; + /** Gets the low 32 bits as a signed integer. */ + getLowBits(): number; + /** Gets the low 32 bits as an unsigned integer. */ + getLowBitsUnsigned(): number; + /** Gets the number of bits needed to represent the absolute value of this Long. */ + getNumBitsAbs(): number; + /** Tests if this Long's value is greater than the specified's. */ + greaterThan(other: string | number | Long | Timestamp): boolean; + /** This is an alias of {@link Long.greaterThan} */ + gt(other: string | number | Long | Timestamp): boolean; + /** Tests if this Long's value is greater than or equal the specified's. */ + greaterThanOrEqual(other: string | number | Long | Timestamp): boolean; + /** This is an alias of {@link Long.greaterThanOrEqual} */ + gte(other: string | number | Long | Timestamp): boolean; + /** This is an alias of {@link Long.greaterThanOrEqual} */ + ge(other: string | number | Long | Timestamp): boolean; + /** Tests if this Long's value is even. */ + isEven(): boolean; + /** Tests if this Long's value is negative. */ + isNegative(): boolean; + /** Tests if this Long's value is odd. */ + isOdd(): boolean; + /** Tests if this Long's value is positive. */ + isPositive(): boolean; + /** Tests if this Long's value equals zero. */ + isZero(): boolean; + /** Tests if this Long's value is less than the specified's. */ + lessThan(other: string | number | Long | Timestamp): boolean; + /** This is an alias of {@link Long#lessThan}. */ + lt(other: string | number | Long | Timestamp): boolean; + /** Tests if this Long's value is less than or equal the specified's. */ + lessThanOrEqual(other: string | number | Long | Timestamp): boolean; + /** This is an alias of {@link Long.lessThanOrEqual} */ + lte(other: string | number | Long | Timestamp): boolean; + /** Returns this Long modulo the specified. */ + modulo(divisor: string | number | Long | Timestamp): Long; + /** This is an alias of {@link Long.modulo} */ + mod(divisor: string | number | Long | Timestamp): Long; + /** This is an alias of {@link Long.modulo} */ + rem(divisor: string | number | Long | Timestamp): Long; + /** + * Returns the product of this and the specified Long. + * @param multiplier - Multiplier + * @returns Product + */ + multiply(multiplier: string | number | Long | Timestamp): Long; + /** This is an alias of {@link Long.multiply} */ + mul(multiplier: string | number | Long | Timestamp): Long; + /** Returns the Negation of this Long's value. */ + negate(): Long; + /** This is an alias of {@link Long.negate} */ + neg(): Long; + /** Returns the bitwise NOT of this Long. */ + not(): Long; + /** Tests if this Long's value differs from the specified's. */ + notEquals(other: string | number | Long | Timestamp): boolean; + /** This is an alias of {@link Long.notEquals} */ + neq(other: string | number | Long | Timestamp): boolean; + /** This is an alias of {@link Long.notEquals} */ + ne(other: string | number | Long | Timestamp): boolean; + /** + * Returns the bitwise OR of this Long and the specified. + */ + or(other: number | string | Long): Long; + /** + * Returns this Long with bits shifted to the left by the given amount. + * @param numBits - Number of bits + * @returns Shifted Long + */ + shiftLeft(numBits: number | Long): Long; + /** This is an alias of {@link Long.shiftLeft} */ + shl(numBits: number | Long): Long; + /** + * Returns this Long with bits arithmetically shifted to the right by the given amount. + * @param numBits - Number of bits + * @returns Shifted Long + */ + shiftRight(numBits: number | Long): Long; + /** This is an alias of {@link Long.shiftRight} */ + shr(numBits: number | Long): Long; + /** + * Returns this Long with bits logically shifted to the right by the given amount. + * @param numBits - Number of bits + * @returns Shifted Long + */ + shiftRightUnsigned(numBits: Long | number): Long; + /** This is an alias of {@link Long.shiftRightUnsigned} */ + shr_u(numBits: number | Long): Long; + /** This is an alias of {@link Long.shiftRightUnsigned} */ + shru(numBits: number | Long): Long; + /** + * Returns the difference of this and the specified Long. + * @param subtrahend - Subtrahend + * @returns Difference + */ + subtract(subtrahend: string | number | Long | Timestamp): Long; + /** This is an alias of {@link Long.subtract} */ + sub(subtrahend: string | number | Long | Timestamp): Long; + /** Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. */ + toInt(): number; + /** Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). */ + toNumber(): number; + /** Converts the Long to a BigInt (arbitrary precision). */ + toBigInt(): bigint; + /** + * Converts this Long to its byte representation. + * @param le - Whether little or big endian, defaults to big endian + * @returns Byte representation + */ + toBytes(le?: boolean): number[]; + /** + * Converts this Long to its little endian byte representation. + * @returns Little endian byte representation + */ + toBytesLE(): number[]; + /** + * Converts this Long to its big endian byte representation. + * @returns Big endian byte representation + */ + toBytesBE(): number[]; + /** + * Converts this Long to signed. + */ + toSigned(): Long; + /** + * Converts the Long to a string written in the specified radix. + * @param radix - Radix (2-36), defaults to 10 + * @throws RangeError If `radix` is out of range + */ + toString(radix?: number): string; + /** Converts this Long to unsigned. */ + toUnsigned(): Long; + /** Returns the bitwise XOR of this Long and the given one. */ + xor(other: Long | number | string): Long; + /** This is an alias of {@link Long.isZero} */ + eqz(): boolean; + /** This is an alias of {@link Long.lessThanOrEqual} */ + le(other: string | number | Long | Timestamp): boolean; + toExtendedJSON(options?: EJSONOptions): number | LongExtended; + static fromExtendedJSON(doc: { + $numberLong: string; + }, options?: EJSONOptions): number | Long | bigint; + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; +} + +/** @public */ +export declare interface LongExtended { + $numberLong: string; +} + +/** @public */ +export declare type LongWithoutOverrides = new (low: unknown, high?: number | boolean, unsigned?: boolean) => { + [P in TimestampKept]: Long[P]; +}; + +/** @public */ +export declare const LongWithoutOverridesClass: LongWithoutOverrides; + +/** + * A class representation of the BSON MaxKey type. + * @public + * @category BSONType + */ +export declare class MaxKey extends BSONValue { + get _bsontype(): 'MaxKey'; + /* Excluded from this release type: toExtendedJSON */ + /* Excluded from this release type: fromExtendedJSON */ + inspect(): string; +} + +/** @public */ +export declare interface MaxKeyExtended { + $maxKey: 1; +} + +/** + * A class representation of the BSON MinKey type. + * @public + * @category BSONType + */ +export declare class MinKey extends BSONValue { + get _bsontype(): 'MinKey'; + /* Excluded from this release type: toExtendedJSON */ + /* Excluded from this release type: fromExtendedJSON */ + inspect(): string; +} + +/** @public */ +export declare interface MinKeyExtended { + $minKey: 1; +} + +/** + * @experimental + * @public + * + * A collection of functions that get or set various numeric types and bit widths from a Uint8Array. + */ +export declare type NumberUtils = { + /** Is true if the current system is big endian. */ + isBigEndian: boolean; + /** + * Parses a signed int32 at offset. Throws a `RangeError` if value is negative. + */ + getNonnegativeInt32LE: (source: Uint8Array, offset: number) => number; + getInt32LE: (source: Uint8Array, offset: number) => number; + getUint32LE: (source: Uint8Array, offset: number) => number; + getUint32BE: (source: Uint8Array, offset: number) => number; + getBigInt64LE: (source: Uint8Array, offset: number) => bigint; + getFloat64LE: (source: Uint8Array, offset: number) => number; + setInt32BE: (destination: Uint8Array, offset: number, value: number) => 4; + setInt32LE: (destination: Uint8Array, offset: number, value: number) => 4; + setBigInt64LE: (destination: Uint8Array, offset: number, value: bigint) => 8; + setFloat64LE: (destination: Uint8Array, offset: number, value: number) => 8; +}; + +/** + * Number parsing and serializing utilities. + * + * @experimental + * @public + */ +export declare const NumberUtils: NumberUtils; + +/** + * A class representation of the BSON ObjectId type. + * @public + * @category BSONType + */ +export declare class ObjectId extends BSONValue { + get _bsontype(): 'ObjectId'; + /* Excluded from this release type: index */ + /* Excluded from this release type: PROCESS_UNIQUE */ + /* Excluded from this release type: resetState */ + static cacheHexString: boolean; + /* Excluded from this release type: i0 */ + /* Excluded from this release type: i1 */ + /* Excluded from this release type: i2 */ + /* Excluded from this release type: i3 */ + /* Excluded from this release type: setFromBytes */ + /* Excluded from this release type: setFromHex */ + /** To generate a new ObjectId, use ObjectId() with no argument. */ + constructor(); + /** + * Create ObjectId from a 24 character hex string. + * + * @param inputId - A 24 character hex string. + */ + constructor(inputId: string); + /** + * Create ObjectId from the BSON ObjectId type. + * + * @param inputId - The BSON ObjectId type. + */ + constructor(inputId: ObjectId); + /** + * Create ObjectId from the object type that has the toHexString method. + * + * @param inputId - The ObjectIdLike type. + */ + constructor(inputId: ObjectIdLike); + /** + * Create ObjectId from a 12 byte binary Buffer. + * + * @param inputId - A 12 byte binary Buffer. + */ + constructor(inputId: Uint8Array); + /** + * Implementation overload. + * + * @param inputId - All input types that are used in the constructor implementation. + */ + constructor(inputId?: string | ObjectId | ObjectIdLike | Uint8Array); + /* Excluded from this release type: __constructor */ + /** + * The ObjectId bytes, rebuilt from the packed integer fields on each access. Every read + * returns a freshly allocated 12-byte buffer. + * @readonly + */ + get id(): Uint8Array; + set id(value: Uint8Array); + /* Excluded from this release type: validateHexString */ + /** Returns the ObjectId id as a 24 lowercase character hex string representation */ + toHexString(): string; + /* Excluded from this release type: getInc */ + /** + * Generate a 12 byte id buffer used in ObjectId's + * + * @param time - pass in a second based timestamp. + */ + static generate(time?: number): Uint8Array; + /** + * Converts the id into a 24 character hex string for printing, unless encoding is provided. + * @param encoding - hex or base64 + */ + toString(encoding?: 'hex' | 'base64'): string; + /** Converts to its JSON the 24 character hex string representation. */ + toJSON(): string; + /* Excluded from this release type: is */ + /** + * Compares the equality of this ObjectId with `otherID`. + * + * @param otherId - ObjectId instance to compare against. + */ + equals(otherId: string | ObjectId | ObjectIdLike | undefined | null): boolean; + /** Returns the generation date (accurate up to the second) that this ID was generated. */ + getTimestamp(): Date; + /* Excluded from this release type: createPk */ + /* Excluded from this release type: serializeInto */ + /** + * Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId. + * + * @param time - an integer number representing a number of seconds. + */ + static createFromTime(time: number): ObjectId; + /** + * Creates an ObjectId from a hex string representation of an ObjectId. + * + * @param hexString - create a ObjectId from a passed in 24 character hexstring. + */ + static createFromHexString(hexString: string): ObjectId; + /** Creates an ObjectId instance from a base64 string */ + static createFromBase64(base64: string): ObjectId; + /** + * Checks if a value can be used to create a valid bson ObjectId + * @param id - any JS value + */ + static isValid(id: string | ObjectId | ObjectIdLike | Uint8Array): boolean; + /* Excluded from this release type: toExtendedJSON */ + /* Excluded from this release type: fromExtendedJSON */ + /* Excluded from this release type: isCached */ + /** + * Converts to a string representation of this Id. + * + * @returns return the 24 character hex string representation. + */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; +} + +/** @public */ +export declare interface ObjectIdExtended { + $oid: string; +} + +/** @public */ +export declare interface ObjectIdLike { + id: string | Uint8Array; + __id?: string; + toHexString(): string; +} + +/** + * @experimental + * @public + * + * A new set of BSON APIs that are currently experimental and not intended for production use. + */ +export declare type OnDemand = { + parseToElements: (this: void, bytes: Uint8Array, startOffset?: number) => Iterable; + BSONElement: BSONElement; + ByteUtils: ByteUtils; + NumberUtils: NumberUtils; +}; + +/** + * @experimental + * @public + */ +export declare const onDemand: OnDemand; + +/** + * Parse an Extended JSON string, constructing the JavaScript value or object described by that + * string. + * + * @example + * ```js + * const { EJSON } = require('bson'); + * const text = '{ "int32": { "$numberInt": "10" } }'; + * + * // prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } } + * console.log(EJSON.parse(text, { relaxed: false })); + * + * // prints { int32: 10 } + * console.log(EJSON.parse(text)); + * ``` + */ +declare function parse(text: string, options?: EJSONParseOptions): any; + +/** + * Serialize a Javascript object. + * + * @param object - the Javascript object to serialize. + * @returns Buffer object containing the serialized object. + * @public + */ +export declare function serialize(object: Document, options?: SerializeOptions): Uint8Array; + +/** @public */ +export declare interface SerializeOptions { + /** + * the serializer will check if keys are valid. + * @defaultValue `false` + */ + checkKeys?: boolean; + /** + * serialize the javascript functions + * @defaultValue `false` + */ + serializeFunctions?: boolean; + /** + * serialize will not emit undefined fields + * note that the driver sets this to `false` + * @defaultValue `true` + */ + ignoreUndefined?: boolean; + /* Excluded from this release type: minInternalBufferSize */ + /** + * the index in the buffer where we wish to start serializing into + * @defaultValue `0` + */ + index?: number; +} + +/** + * Serialize a Javascript object using a predefined Buffer and index into the buffer, + * useful when pre-allocating the space for serialization. + * + * @param object - the Javascript object to serialize. + * @param finalBuffer - the Buffer you pre-allocated to store the serialized BSON object. + * @returns the index pointing to the last written byte in the buffer. + * @public + */ +export declare function serializeWithBufferAndIndex(object: Document, finalBuffer: Uint8Array, options?: SerializeOptions): number; + +/** + * Sets the size of the internal serialization buffer. + * + * @param size - The desired size for the internal serialization buffer in bytes + * @public + */ +export declare function setInternalBufferSize(size: number): void; + +/** + * Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer + * function is specified or optionally including only the specified properties if a replacer array + * is specified. + * + * @param value - The value to convert to extended JSON + * @param replacer - A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string + * @param space - A String or Number object that's used to insert white space into the output JSON string for readability purposes. + * @param options - Optional settings + * + * @example + * ```js + * const { EJSON } = require('bson'); + * const Int32 = require('mongodb').Int32; + * const doc = { int32: new Int32(10) }; + * + * // prints '{"int32":{"$numberInt":"10"}}' + * console.log(EJSON.stringify(doc, { relaxed: false })); + * + * // prints '{"int32":10}' + * console.log(EJSON.stringify(doc)); + * + * // prints '{"int32":{"$numberInt":"10"}}' with 2 space indentation + * console.log(EJSON.stringify(doc, { relaxed: false }, 2)); + * ``` + */ +declare function stringify(value: any, replacer?: (number | string)[] | ((this: any, key: string, value: any) => any) | null, space?: string | number, options?: EJSONSerializeOptions): string; + +declare function stringify(value: any, replacer?: (number | string)[] | ((this: any, key: string, value: any) => any) | null, options?: EJSONSerializeOptions): string; + +declare function stringify(value: any, options?: EJSONSerializeOptions, space?: string | number): string; + +/** + * @public + * @category BSONType + * + * A special type for _internal_ MongoDB use and is **not** associated with the regular Date type. + */ +export declare class Timestamp extends LongWithoutOverridesClass { + get _bsontype(): 'Timestamp'; + get [bsonType](): 'Timestamp'; + /** + * @deprecated Use `Long.MAX_UNSIGNED_VALUE` instead. + */ + static readonly MAX_VALUE: Long; + /** + * An incrementing ordinal for operations within a given second. + */ + get i(): number; + /** + * A `time_t` value measuring seconds since the Unix epoch + */ + get t(): number; + /** + * @param int - A 64-bit bigint representing the Timestamp. + */ + constructor(int: bigint); + /** + * @param long - A 64-bit Long representing the Timestamp. + */ + constructor(long: Long); + /** + * @param value - A pair of two values indicating timestamp and increment. + */ + constructor(value: { + t: number; + i: number; + }); + toJSON(): { + $timestamp: string; + }; + /** + * Returns a Timestamp represented by the given (32-bit) integer value. + * @deprecated Stores `value` in the low 32 bits (increment), leaving t = 0. Use `new Timestamp({ t, i })` or `Timestamp.fromBits(lowBits, highBits)` for explicit construction. + */ + static fromInt(value: number): Timestamp; + /** + * Returns a Timestamp representing the given number value, provided that it is a finite number. Otherwise, zero is returned. + * @deprecated Splits `value` across (t, i) as a uint64; the result rarely matches user intent. Use `new Timestamp({ t, i })` or `Timestamp.fromBits(lowBits, highBits)` for explicit construction. + */ + static fromNumber(value: number): Timestamp; + /** + * Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits. + * + * @param lowBits - the low 32-bits. + * @param highBits - the high 32-bits. + */ + static fromBits(lowBits: number, highBits: number): Timestamp; + /** + * Returns a Timestamp from the given string, optionally using the given radix. + * + * @param str - the textual representation of the Timestamp. + * @param optRadix - the radix in which the text is written. + */ + static fromString(str: string, optRadix: number): Timestamp; + /* Excluded from this release type: toExtendedJSON */ + /* Excluded from this release type: fromExtendedJSON */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + add: Long['add']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + subtract: Long['subtract']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + sub: Long['sub']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + multiply: Long['multiply']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + mul: Long['mul']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + divide: Long['divide']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + div: Long['div']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + modulo: Long['modulo']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + mod: Long['mod']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + rem: Long['rem']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned. */ + negate: Long['negate']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned. */ + neg: Long['neg']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + and: Long['and']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + or: Long['or']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + xor: Long['xor']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + not: Long['not']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + shiftLeft: Long['shiftLeft']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + shl: Long['shl']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + shiftRight: Long['shiftRight']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + shr: Long['shr']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + shiftRightUnsigned: Long['shiftRightUnsigned']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + shr_u: Long['shr_u']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + shru: Long['shru']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned. */ + toSigned: Long['toSigned']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned (this is a no-op). */ + toUnsigned: Long['toUnsigned']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned (is always false). */ + isNegative: Long['isNegative']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned (is always true). */ + isPositive: Long['isPositive']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned (is always true). */ + unsigned: Long['unsigned']; + /** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */ + toInt: Long['toInt']; + /** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */ + toNumber: Long['toNumber']; + /** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */ + toBytes: Long['toBytes']; + /** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */ + toBytesLE: Long['toBytesLE']; + /** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */ + toBytesBE: Long['toBytesBE']; + /** @deprecated Incompatible with Timestamp: returns a signed integer, but Timestamp is always unsigned. Use `.t` for seconds since the Unix epoch. */ + getHighBits: Long['getHighBits']; + /** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch. */ + getHighBitsUnsigned: Long['getHighBitsUnsigned']; + /** @deprecated Incompatible with Timestamp: returns a signed integer, but Timestamp is always unsigned. Use `.i` for the increment ordinal. */ + getLowBits: Long['getLowBits']; + /** @deprecated Not applicable to Timestamp; use `.i` for the increment ordinal. */ + getLowBitsUnsigned: Long['getLowBitsUnsigned']; + /** @deprecated Not applicable to Timestamp; use `.i` instead. */ + low: Long['low']; + /** @deprecated Not applicable to Timestamp; use `.t` instead. */ + high: Long['high']; + /** @deprecated Use .compare() for general comparison, or .equals(), .lessThan(), .greaterThan() for specific cases. */ + comp: Long['comp']; + /** @deprecated Compare `.t` and `.i` against `0` explicitly. */ + isZero: Long['isZero']; + /** @deprecated Compare `.t` and `.i` against `0` explicitly. */ + eqz: Long['eqz']; + /** @deprecated Not applicable to Timestamp. Use the bsonType symbol or _bsontype === 'Timestamp' to identify a Timestamp. */ + __isLong__: Long['__isLong__']; + /** @deprecated Not applicable to Timestamp. */ + getNumBitsAbs: Long['getNumBitsAbs']; + /** @deprecated Not applicable to Timestamp; tests parity of the increment only. */ + isEven: Long['isEven']; + /** @deprecated Not applicable to Timestamp; tests parity of the increment only. */ + isOdd: Long['isOdd']; +} + +/** @public */ +export declare interface TimestampExtended { + $timestamp: { + t: number; + i: number; + }; +} + +/** + * @public + * + * Inherited `Long` members surfaced on `Timestamp`. + * When `Long` gains a new member: add it here if it should pass through, or + * add a `@deprecated declare` line on the `Timestamp` class if it should be + * surfaced as deprecated. Anything left unclassified is silently dropped from + * the public type — preventing accidental behavioral bleed-through. + */ +declare type TimestampKept = 'toBigInt' | 'toString' | 'compare' | 'equals' | 'eq' | 'notEquals' | 'neq' | 'ne' | 'lessThan' | 'lt' | 'lessThanOrEqual' | 'lte' | 'le' | 'greaterThan' | 'gt' | 'greaterThanOrEqual' | 'gte' | 'ge'; + +/** + * @public + * @deprecated This type is no longer used internally and will be removed in a future version. + */ +export declare type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect' | typeof bsonType; + +/** + * A class representation of the BSON UUID type. + * @public + */ +export declare class UUID extends Binary { + /** + * Create a UUID type + * + * When the argument to the constructor is omitted a random v4 UUID will be generated. + * + * @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer. + */ + constructor(input?: string | Uint8Array | UUID); + /** + * The UUID bytes + * @readonly + */ + get id(): Uint8Array; + set id(value: Uint8Array); + /** + * Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated) + * @param includeDashes - should the string exclude dash-separators. + */ + toHexString(includeDashes?: boolean): string; + /** + * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified. + */ + toString(encoding?: 'hex' | 'base64'): string; + /** + * Converts the id into its JSON string representation. + * A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + */ + toJSON(): string; + /** + * Compares the equality of this UUID with `otherID`. + * + * @param otherId - UUID instance to compare against. + */ + equals(otherId: string | Uint8Array | UUID): boolean; + /** + * Creates a Binary instance from the current UUID. + */ + toBinary(): Binary; + /** + * Generates a populated buffer containing a v4 uuid + */ + static generate(): Uint8Array; + /** + * Checks if a value is a valid bson UUID + * @param input - UUID, string or Buffer to validate. + */ + static isValid(input: string | Uint8Array | UUID | Binary): boolean; + /** + * Creates an UUID from a hex string representation of an UUID. + * @param hexString - 32 or 36 character hex string (dashes excluded/included). + */ + static createFromHexString(hexString: string): UUID; + /** Creates an UUID from a base64 string representation of an UUID. */ + static createFromBase64(base64: string): UUID; + /* Excluded from this release type: bytesFromString */ + /* Excluded from this release type: isValidUUIDString */ + /** + * Converts to a string representation of this Id. + * + * @returns return the 36 character hex string representation. + * + */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; +} + +/** @public */ +export declare type UUIDExtended = { + $uuid: string; +}; + +export { } diff --git a/gateway/node_modules/bson/etc/prepare.js b/gateway/node_modules/bson/etc/prepare.js new file mode 100644 index 0000000000000000000000000000000000000000..91e6f3a976f076f9e97003662bb82ad70de1ccb9 --- /dev/null +++ b/gateway/node_modules/bson/etc/prepare.js @@ -0,0 +1,19 @@ +#! /usr/bin/env node +var cp = require('child_process'); +var fs = require('fs'); + +var nodeMajorVersion = +process.version.match(/^v(\d+)\.\d+/)[1]; + +if (fs.existsSync('src') && nodeMajorVersion >= 10) { + cp.spawnSync('npm', ['run', 'build'], { stdio: 'inherit', shell: true }); +} else { + if (!fs.existsSync('lib')) { + console.warn('BSON: No compiled javascript present, the library is not installed correctly.'); + if (nodeMajorVersion < 10) { + console.warn( + 'This library can only be compiled in nodejs version 10 or later, currently running: ' + + nodeMajorVersion + ); + } + } +} diff --git a/gateway/node_modules/bson/package.json b/gateway/node_modules/bson/package.json new file mode 100644 index 0000000000000000000000000000000000000000..ef1f1db90656ac01775afad7699bb6f59f1e1a10 --- /dev/null +++ b/gateway/node_modules/bson/package.json @@ -0,0 +1,118 @@ +{ + "name": "bson", + "description": "A bson parser for node.js and the browser", + "keywords": [ + "mongodb", + "bson", + "parser" + ], + "files": [ + "lib", + "src", + "bson.d.ts", + "etc/prepare.js", + "vendor" + ], + "types": "bson.d.ts", + "version": "7.3.2", + "author": { + "name": "The MongoDB NodeJS Team", + "email": "dbx-node@mongodb.com" + }, + "license": "Apache-2.0", + "contributors": [], + "repository": "mongodb/js-bson", + "bugs": { + "url": "https://jira.mongodb.org/projects/NODE/issues/" + }, + "devDependencies": { + "@istanbuljs/nyc-config-typescript": "^1.0.2", + "@microsoft/api-extractor": "^7.52.5", + "@rollup/plugin-node-resolve": "^16.0.1", + "@rollup/plugin-typescript": "^12.1.2", + "@types/chai": "^4.3.17", + "@types/mocha": "^10.0.7", + "@types/node": "^24.2.1", + "@types/sinon": "^21.0.0", + "@types/sinon-chai": "^3.2.12", + "@typescript-eslint/eslint-plugin": "^8.31.1", + "@typescript-eslint/parser": "^8.31.1", + "benchmark": "^2.1.4", + "chai": "^4.4.1", + "chalk": "^5.3.0", + "dbx-js-tools": "github:mongodb-js/dbx-js-tools#main", + "eslint": "^9.33.0", + "eslint-config-prettier": "^10.1.2", + "eslint-plugin-prettier": "^5.2.6", + "eslint-plugin-tsdoc": "^0.5.2", + "magic-string": "^0.30.11", + "mocha": "^11.7.1", + "node-fetch": "^3.3.2", + "nyc": "^17.1.0", + "prettier": "^3.5.3", + "rollup": "^4.40.1", + "sinon": "^21.0.0", + "sinon-chai": "^3.7.0", + "source-map-support": "^0.5.21", + "tar": "^7.4.3", + "ts-node": "^10.9.2", + "tsd": "^0.33.0", + "typescript": "^5.8.3", + "typescript-cached-transpile": "0.0.6", + "uuid": "^13.0.0" + }, + "tsd": { + "directory": "test/types", + "compilerOptions": { + "strict": true, + "target": "esnext", + "module": "commonjs", + "moduleResolution": "node" + } + }, + "config": { + "native": false + }, + "main": "./lib/bson.cjs", + "module": "./lib/bson.node.mjs", + "exports": { + "browser": { + "types": "./bson.d.ts", + "default": "./lib/bson.mjs" + }, + "react-native": "./lib/bson.rn.cjs", + "default": { + "types": "./bson.d.ts", + "import": "./lib/bson.node.mjs", + "require": "./lib/bson.cjs" + } + }, + "compass:exports": { + "import": "./lib/bson.cjs", + "require": "./lib/bson.cjs" + }, + "engines": { + "node": ">=20.19.0" + }, + "scripts": { + "pretest": "npm run build", + "test": "npm run check:node && npm run check:web", + "check:node": "WEB=false mocha test/node", + "check:tsd": "npm run build:dts && tsd", + "check:web": "WEB=true mocha test/node", + "check:granular-bench": "npm run build:bench && npm run check:baseline-bench && node ./test/bench/etc/run_granular_benchmarks.js", + "check:spec-bench": "npm run build:bench && npm run check:baseline-bench && node ./test/bench/lib/spec/bsonBench.js", + "check:custom-bench": "npm run build && npm run check:baseline-bench && node ./test/bench/custom/main.mjs", + "check:baseline-bench": "node ./test/bench/etc/cpuBaseline.js", + "build:bench": "cd test/bench && npx tsc", + "build:ts": "node ./node_modules/typescript/bin/tsc", + "build:dts": "npm run build:ts && api-extractor run --typescript-compiler-folder node_modules/typescript --local && node etc/clean_definition_files.cjs", + "build:bundle": "rollup -c rollup.config.mjs", + "build": "npm run build:dts && npm run build:bundle", + "check:lint": "ESLINT_USE_FLAT_CONFIG=false eslint -v && ESLINT_USE_FLAT_CONFIG=false eslint --ext '.js,.ts' --max-warnings=0 src test && npm run build:dts && npm run check:tsd", + "format": "ESLINT_USE_FLAT_CONFIG=false eslint --ext '.js,.ts' src test --fix", + "check:coverage": "nyc --check-coverage npm run check:node", + "prepare": "node etc/prepare.js", + "release": "standard-version -i HISTORY.md" + } +} diff --git a/gateway/node_modules/bson/src/binary.ts b/gateway/node_modules/bson/src/binary.ts new file mode 100644 index 0000000000000000000000000000000000000000..06470c403ab2d8b182939f4f6f404384ad81521c --- /dev/null +++ b/gateway/node_modules/bson/src/binary.ts @@ -0,0 +1,751 @@ +import { type InspectFn, defaultInspect, isAnyArrayBuffer, isUint8Array } from './parser/utils'; +import type { EJSONOptions } from './extended_json'; +import { BSONError } from './error'; +import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants'; +import { ByteUtils } from './utils/byte_utils'; +import { BSONValue } from './bson_value'; +import { NumberUtils } from './utils/number_utils'; + +/** @public */ +export type BinarySequence = Uint8Array | number[]; + +/** @public */ +export interface BinaryExtendedLegacy { + $type: string; + $binary: string; +} + +/** @public */ +export interface BinaryExtended { + $binary: { + subType: string; + base64: string; + }; +} + +/** + * A class representation of the BSON Binary type. + * @public + * @category BSONType + */ +export class Binary extends BSONValue { + get _bsontype(): 'Binary' { + return 'Binary'; + } + + /** + * Binary default subtype + * @internal + */ + private static readonly BSON_BINARY_SUBTYPE_DEFAULT = 0; + + /** Initial buffer default size */ + static readonly BUFFER_SIZE = 256; + /** Default BSON type */ + static readonly SUBTYPE_DEFAULT = 0; + /** Function BSON type */ + static readonly SUBTYPE_FUNCTION = 1; + /** + * Legacy default BSON Binary type + * @deprecated BSON Binary subtype 2 is deprecated in the BSON specification + */ + static readonly SUBTYPE_BYTE_ARRAY = 2; + /** Deprecated UUID BSON type @deprecated Please use SUBTYPE_UUID */ + static readonly SUBTYPE_UUID_OLD = 3; + /** UUID BSON type */ + static readonly SUBTYPE_UUID = 4; + /** MD5 BSON type */ + static readonly SUBTYPE_MD5 = 5; + /** Encrypted BSON type */ + static readonly SUBTYPE_ENCRYPTED = 6; + /** Column BSON type */ + static readonly SUBTYPE_COLUMN = 7; + /** Sensitive BSON type */ + static readonly SUBTYPE_SENSITIVE = 8; + /** Vector BSON type */ + static readonly SUBTYPE_VECTOR = 9; + /** User BSON type */ + static readonly SUBTYPE_USER_DEFINED = 128; + + /** datatype of a Binary Vector (subtype: 9) */ + static readonly VECTOR_TYPE = Object.freeze({ + Int8: 0x03, + Float32: 0x27, + PackedBit: 0x10 + } as const); + + /** + * The bytes of the Binary value. + * + * The format of a Binary value in BSON is defined as: + * ```txt + * binary ::= int32 subtype (byte*) + * ``` + * + * This `buffer` is the "(byte*)" segment. + * + * Unless the value is subtype 2, then deserialize will read the first 4 bytes as an int32 and set this to the remaining bytes. + * + * ```txt + * binary ::= int32 unsigned_byte(2) int32 (byte*) + * ``` + * + * @see https://bsonspec.org/spec.html + */ + public buffer: Uint8Array; + /** + * The binary subtype. + * + * Current defined values are: + * + * - `unsigned_byte(0)` Generic binary subtype + * - `unsigned_byte(1)` Function + * - `unsigned_byte(2)` Binary (Deprecated) + * - `unsigned_byte(3)` UUID (Deprecated) + * - `unsigned_byte(4)` UUID + * - `unsigned_byte(5)` MD5 + * - `unsigned_byte(6)` Encrypted BSON value + * - `unsigned_byte(7)` Compressed BSON column + * - `unsigned_byte(8)` Sensitive + * - `unsigned_byte(9)` Vector + * - `unsigned_byte(128)` - `unsigned_byte(255)` User defined + */ + public sub_type: number; + /** + * The Binary's `buffer` can be larger than the Binary's content. + * This property is used to determine where the content ends in the buffer. + */ + public position: number; + + /** + * Create a new Binary instance. + * @param buffer - a buffer object containing the binary data. + * @param subType - the option binary type. + */ + constructor(buffer?: BinarySequence, subType?: number) { + super(); + if ( + !(buffer == null) && + typeof buffer === 'string' && + !ArrayBuffer.isView(buffer) && + !isAnyArrayBuffer(buffer) && + !Array.isArray(buffer) + ) { + throw new BSONError('Binary can only be constructed from Uint8Array or number[]'); + } + + this.sub_type = (subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT) & 0xff; + + if (buffer == null) { + // create an empty binary buffer + this.buffer = ByteUtils.allocate(Binary.BUFFER_SIZE); + this.position = 0; + } else { + this.buffer = Array.isArray(buffer) + ? ByteUtils.fromNumberArray(buffer) + : ByteUtils.toLocalBufferType(buffer); + this.position = this.buffer.byteLength; + } + } + + /** + * Updates this binary with byte_value. + * + * @param byteValue - a single byte we wish to write. + */ + put(byteValue: string | number | Uint8Array | number[]): void { + // If it's a string and a has more than one character throw an error + if (typeof byteValue === 'string' && byteValue.length !== 1) { + throw new BSONError('only accepts single character String'); + } else if (typeof byteValue !== 'number' && byteValue.length !== 1) + throw new BSONError('only accepts single character Uint8Array or Array'); + + // Decode the byte value once + let decodedByte: number; + if (typeof byteValue === 'string') { + decodedByte = byteValue.charCodeAt(0); + } else if (typeof byteValue === 'number') { + decodedByte = byteValue; + } else { + decodedByte = byteValue[0]; + } + + if (decodedByte < 0 || decodedByte > 255) { + throw new BSONError('only accepts number in a valid unsigned byte range 0-255'); + } + + if (this.buffer.byteLength > this.position) { + this.buffer[this.position++] = decodedByte; + } else { + const newSpace = ByteUtils.allocate(Binary.BUFFER_SIZE + this.buffer.length); + newSpace.set(this.buffer, 0); + this.buffer = newSpace; + this.buffer[this.position++] = decodedByte; + } + } + + /** + * Writes a buffer to the binary. + * + * @param sequence - a string or buffer to be written to the Binary BSON object. + * @param offset - specify the binary of where to write the content. + */ + write(sequence: BinarySequence, offset: number): void { + offset = typeof offset === 'number' ? offset : this.position; + + // If the buffer is to small let's extend the buffer + if (this.buffer.byteLength < offset + sequence.length) { + const newSpace = ByteUtils.allocate(this.buffer.byteLength + sequence.length); + newSpace.set(this.buffer, 0); + + // Assign the new buffer + this.buffer = newSpace; + } + + if (ArrayBuffer.isView(sequence)) { + this.buffer.set(ByteUtils.toLocalBufferType(sequence), offset); + this.position = + offset + sequence.byteLength > this.position ? offset + sequence.length : this.position; + } else if (typeof sequence === 'string') { + throw new BSONError('input cannot be string'); + } + } + + /** + * Returns a view of **length** bytes starting at **position**. + * + * @param position - read from the given position in the Binary. + * @param length - the number of bytes to read. + */ + read(position: number, length: number): Uint8Array { + length = length && length > 0 ? length : this.position; + const end = position + length; + return this.buffer.subarray(position, end > this.position ? this.position : end); + } + + /** returns a view of the binary value as a Uint8Array */ + value(): Uint8Array { + // Optimize to serialize for the situation where the data == size of buffer + return this.buffer.length === this.position + ? this.buffer + : this.buffer.subarray(0, this.position); + } + + /** the length of the binary sequence */ + length(): number { + return this.position; + } + + toJSON(): string { + return ByteUtils.toBase64(this.buffer.subarray(0, this.position)); + } + + toString(encoding?: 'hex' | 'base64' | 'utf8' | 'utf-8'): string { + if (encoding === 'hex') return ByteUtils.toHex(this.buffer.subarray(0, this.position)); + if (encoding === 'base64') return ByteUtils.toBase64(this.buffer.subarray(0, this.position)); + if (encoding === 'utf8' || encoding === 'utf-8') + return ByteUtils.toUTF8(this.buffer, 0, this.position, false); + return ByteUtils.toUTF8(this.buffer, 0, this.position, false); + } + + /** @internal */ + toExtendedJSON(options?: EJSONOptions): BinaryExtendedLegacy | BinaryExtended { + options = options || {}; + + if (this.sub_type === Binary.SUBTYPE_VECTOR) { + validateBinaryVector(this); + } + + const base64String = ByteUtils.toBase64(this.buffer); + + const subType = Number(this.sub_type).toString(16); + if (options.legacy) { + return { + $binary: base64String, + $type: subType.length === 1 ? '0' + subType : subType + }; + } + return { + $binary: { + base64: base64String, + subType: subType.length === 1 ? '0' + subType : subType + } + }; + } + + toUUID(): UUID { + if (this.sub_type === Binary.SUBTYPE_UUID) { + return new UUID(this.buffer.subarray(0, this.position)); + } + + throw new BSONError( + `Binary sub_type "${this.sub_type}" (${typeof this.sub_type}) is not supported for converting to UUID. Only 0x${Binary.SUBTYPE_UUID.toString(16).padStart(2, '0')} is currently supported.` + ); + } + + /** Creates an Binary instance from a hex digit string */ + static createFromHexString(hex: string, subType?: number): Binary { + return new Binary(ByteUtils.fromHex(hex), subType); + } + + /** Creates an Binary instance from a base64 string */ + static createFromBase64(base64: string, subType?: number): Binary { + return new Binary(ByteUtils.fromBase64(base64), subType); + } + + /** @internal */ + static fromExtendedJSON( + doc: BinaryExtendedLegacy | BinaryExtended | UUIDExtended, + options?: EJSONOptions + ): Binary { + options = options || {}; + let data: Uint8Array | undefined; + let type; + if ('$binary' in doc) { + if (options.legacy && typeof doc.$binary === 'string' && '$type' in doc) { + type = doc.$type ? parseInt(doc.$type, 16) : 0; + data = ByteUtils.fromBase64(doc.$binary); + } else { + if (typeof doc.$binary !== 'string') { + type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0; + data = ByteUtils.fromBase64(doc.$binary.base64); + } + } + } else if ('$uuid' in doc) { + type = 4; + data = UUID.bytesFromString(doc.$uuid); + } + if (!data) { + throw new BSONError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`); + } + return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type); + } + + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + inspect ??= defaultInspect; + const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position)); + const base64Arg = inspect(base64, options); + const subTypeArg = inspect(this.sub_type, options); + return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`; + } + + /** + * If this Binary represents a Int8 Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.Int8`), + * returns a copy of the bytes in a new Int8Array. + * + * If the Binary is not a Vector, or the datatype is not Int8, an error is thrown. + */ + public toInt8Array(): Int8Array { + if (this.sub_type !== Binary.SUBTYPE_VECTOR) { + throw new BSONError('Binary sub_type is not Vector'); + } + + if (this.buffer[0] !== Binary.VECTOR_TYPE.Int8) { + throw new BSONError('Binary datatype field is not Int8'); + } + + validateBinaryVector(this); + + return new Int8Array( + this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position) + ); + } + + /** + * If this Binary represents a Float32 Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.Float32`), + * returns a copy of the bytes in a new Float32Array. + * + * If the Binary is not a Vector, or the datatype is not Float32, an error is thrown. + */ + public toFloat32Array(): Float32Array { + if (this.sub_type !== Binary.SUBTYPE_VECTOR) { + throw new BSONError('Binary sub_type is not Vector'); + } + + if (this.buffer[0] !== Binary.VECTOR_TYPE.Float32) { + throw new BSONError('Binary datatype field is not Float32'); + } + + validateBinaryVector(this); + + const floatBytes = new Uint8Array( + this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position) + ); + + if (NumberUtils.isBigEndian) ByteUtils.swap32(floatBytes); + + return new Float32Array(floatBytes.buffer); + } + + /** + * If this Binary represents packed bit Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.PackedBit`), + * returns a copy of the bytes that are packed bits. + * + * Use `toBits` to get the unpacked bits. + * + * If the Binary is not a Vector, or the datatype is not PackedBit, an error is thrown. + */ + public toPackedBits(): Uint8Array { + if (this.sub_type !== Binary.SUBTYPE_VECTOR) { + throw new BSONError('Binary sub_type is not Vector'); + } + + if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) { + throw new BSONError('Binary datatype field is not packed bit'); + } + + validateBinaryVector(this); + + return new Uint8Array( + this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position) + ); + } + + /** + * If this Binary represents a Packed bit Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.PackedBit`), + * returns a copy of the bit unpacked into a new Int8Array. + * + * Use `toPackedBits` to get the bits still in packed form. + * + * If the Binary is not a Vector, or the datatype is not PackedBit, an error is thrown. + */ + public toBits(): Int8Array { + if (this.sub_type !== Binary.SUBTYPE_VECTOR) { + throw new BSONError('Binary sub_type is not Vector'); + } + + if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) { + throw new BSONError('Binary datatype field is not packed bit'); + } + + validateBinaryVector(this); + + const byteCount = this.length() - 2; + const bitCount = byteCount * 8 - this.buffer[1]; + const bits = new Int8Array(bitCount); + + for (let bitOffset = 0; bitOffset < bits.length; bitOffset++) { + const byteOffset = (bitOffset / 8) | 0; + const byte = this.buffer[byteOffset + 2]; + const shift = 7 - (bitOffset % 8); + const bit = (byte >> shift) & 1; + bits[bitOffset] = bit; + } + + return bits; + } + + /** + * Constructs a Binary representing an Int8 Vector. + * @param array - The array to store as a view on the Binary class + */ + public static fromInt8Array(array: Int8Array): Binary { + const buffer = ByteUtils.allocate(array.byteLength + 2); + buffer[0] = Binary.VECTOR_TYPE.Int8; + buffer[1] = 0; + const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength); + buffer.set(intBytes, 2); + const bin = new this(buffer, this.SUBTYPE_VECTOR); + validateBinaryVector(bin); + return bin; + } + + /** Constructs a Binary representing an Float32 Vector. */ + public static fromFloat32Array(array: Float32Array): Binary { + const binaryBytes = ByteUtils.allocate(array.byteLength + 2); + binaryBytes[0] = Binary.VECTOR_TYPE.Float32; + binaryBytes[1] = 0; + + const floatBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength); + binaryBytes.set(floatBytes, 2); + + if (NumberUtils.isBigEndian) ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2)); + + const bin = new this(binaryBytes, this.SUBTYPE_VECTOR); + validateBinaryVector(bin); + return bin; + } + + /** + * Constructs a Binary representing a packed bit Vector. + * + * Use `fromBits` to pack an array of 1s and 0s. + */ + public static fromPackedBits(array: Uint8Array, padding = 0): Binary { + const buffer = ByteUtils.allocate(array.byteLength + 2); + buffer[0] = Binary.VECTOR_TYPE.PackedBit; + buffer[1] = padding; + buffer.set(array, 2); + const bin = new this(buffer, this.SUBTYPE_VECTOR); + validateBinaryVector(bin); + return bin; + } + + /** + * Constructs a Binary representing an Packed Bit Vector. + * @param array - The array of 1s and 0s to pack into the Binary instance + */ + public static fromBits(bits: ArrayLike): Binary { + const byteLength = (bits.length + 7) >>> 3; // ceil(bits.length / 8) + const bytes = new Uint8Array(byteLength + 2); + bytes[0] = Binary.VECTOR_TYPE.PackedBit; + + const remainder = bits.length % 8; + bytes[1] = remainder === 0 ? 0 : 8 - remainder; + + for (let bitOffset = 0; bitOffset < bits.length; bitOffset++) { + const byteOffset = bitOffset >>> 3; // floor(bitOffset / 8) + const bit = bits[bitOffset]; + + if (bit !== 0 && bit !== 1) { + throw new BSONError( + `Invalid bit value at ${bitOffset}: must be 0 or 1, found ${bits[bitOffset]}` + ); + } + + if (bit === 0) continue; + + const shift = 7 - (bitOffset % 8); + bytes[byteOffset + 2] |= bit << shift; + } + + return new this(bytes, Binary.SUBTYPE_VECTOR); + } +} + +export function validateBinaryVector(vector: Binary): void { + if (vector.sub_type !== Binary.SUBTYPE_VECTOR) return; + + const size = vector.position; + + // NOTE: Validation is only applied to **KNOWN** vector types + // If a new datatype is introduced, a future version of the library will need to add validation + const datatype = vector.buffer[0]; + + // NOTE: We do not enable noUncheckedIndexedAccess so TS believes this is always number + // a Binary vector may be empty, in which case the padding is undefined + // this possible value is tolerable for our validation checks + const padding: number | undefined = vector.buffer[1]; + + if ( + (datatype === Binary.VECTOR_TYPE.Float32 || datatype === Binary.VECTOR_TYPE.Int8) && + padding !== 0 + ) { + throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors'); + } + + if (datatype === Binary.VECTOR_TYPE.Float32) { + if (size !== 0 && size - 2 !== 0 && (size - 2) % 4 !== 0) { + throw new BSONError('Invalid Vector: Float32 vector must contain a multiple of 4 bytes'); + } + } + + if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) { + throw new BSONError( + 'Invalid Vector: padding must be zero for packed bit vectors that are empty' + ); + } + + if (datatype === Binary.VECTOR_TYPE.PackedBit && padding > 7) { + throw new BSONError( + `Invalid Vector: padding must be a value between 0 and 7. found: ${padding}` + ); + } +} + +/** @public */ +export type UUIDExtended = { + $uuid: string; +}; + +const UUID_BYTE_LENGTH = 16; +const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i; +const UUID_WITH_DASHES = /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i; + +/** + * A class representation of the BSON UUID type. + * @public + */ +export class UUID extends Binary { + /** + * Create a UUID type + * + * When the argument to the constructor is omitted a random v4 UUID will be generated. + * + * @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer. + */ + constructor(input?: string | Uint8Array | UUID) { + let bytes: Uint8Array; + if (input == null) { + bytes = UUID.generate(); + } else if (input instanceof UUID) { + bytes = ByteUtils.toLocalBufferType(new Uint8Array(input.buffer)); + } else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) { + bytes = ByteUtils.toLocalBufferType(input); + } else if (typeof input === 'string') { + bytes = UUID.bytesFromString(input); + } else { + throw new BSONError( + 'Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).' + ); + } + super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW); + } + + /** + * The UUID bytes + * @readonly + */ + get id(): Uint8Array { + return this.buffer; + } + + set id(value: Uint8Array) { + this.buffer = value; + } + + /** + * Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated) + * @param includeDashes - should the string exclude dash-separators. + */ + toHexString(includeDashes = true): string { + if (includeDashes) { + return [ + ByteUtils.toHex(this.buffer.subarray(0, 4)), + ByteUtils.toHex(this.buffer.subarray(4, 6)), + ByteUtils.toHex(this.buffer.subarray(6, 8)), + ByteUtils.toHex(this.buffer.subarray(8, 10)), + ByteUtils.toHex(this.buffer.subarray(10, 16)) + ].join('-'); + } + return ByteUtils.toHex(this.buffer); + } + + /** + * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified. + */ + toString(encoding?: 'hex' | 'base64'): string { + if (encoding === 'hex') return ByteUtils.toHex(this.id); + if (encoding === 'base64') return ByteUtils.toBase64(this.id); + return this.toHexString(); + } + + /** + * Converts the id into its JSON string representation. + * A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + */ + toJSON(): string { + return this.toHexString(); + } + + /** + * Compares the equality of this UUID with `otherID`. + * + * @param otherId - UUID instance to compare against. + */ + equals(otherId: string | Uint8Array | UUID): boolean { + if (!otherId) { + return false; + } + + if (otherId instanceof UUID) { + return ByteUtils.equals(otherId.id, this.id); + } + + try { + return ByteUtils.equals(new UUID(otherId).id, this.id); + } catch { + return false; + } + } + + /** + * Creates a Binary instance from the current UUID. + */ + toBinary(): Binary { + return new Binary(this.id, Binary.SUBTYPE_UUID); + } + + /** + * Generates a populated buffer containing a v4 uuid + */ + static generate(): Uint8Array { + const bytes = ByteUtils.randomBytes(UUID_BYTE_LENGTH); + + // Per 4.4, set bits for version and `clock_seq_hi_and_reserved` + // Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js + bytes[6] = (bytes[6] & 0x0f) | 0x40; + bytes[8] = (bytes[8] & 0x3f) | 0x80; + + return bytes; + } + + /** + * Checks if a value is a valid bson UUID + * @param input - UUID, string or Buffer to validate. + */ + static isValid(input: string | Uint8Array | UUID | Binary): boolean { + if (!input) { + return false; + } + + if (typeof input === 'string') { + return UUID.isValidUUIDString(input); + } + + if (isUint8Array(input)) { + return input.byteLength === UUID_BYTE_LENGTH; + } + + return ( + input._bsontype === 'Binary' && + input.sub_type === this.SUBTYPE_UUID && + input.buffer.byteLength === 16 + ); + } + + /** + * Creates an UUID from a hex string representation of an UUID. + * @param hexString - 32 or 36 character hex string (dashes excluded/included). + */ + static override createFromHexString(hexString: string): UUID { + const buffer = UUID.bytesFromString(hexString); + return new UUID(buffer); + } + + /** Creates an UUID from a base64 string representation of an UUID. */ + static override createFromBase64(base64: string): UUID { + return new UUID(ByteUtils.fromBase64(base64)); + } + + /** @internal */ + static bytesFromString(representation: string) { + if (!UUID.isValidUUIDString(representation)) { + throw new BSONError( + 'UUID string representation must be 32 hex digits or canonical hyphenated representation' + ); + } + return ByteUtils.fromHex(representation.replace(/-/g, '')); + } + + /** + * @internal + * + * Validates a string to be a hex digit sequence with or without dashes. + * The canonical hyphenated representation of a uuid is hex in 8-4-4-4-12 groups. + */ + static isValidUUIDString(representation: string) { + return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation); + } + + /** + * Converts to a string representation of this Id. + * + * @returns return the 36 character hex string representation. + * + */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + inspect ??= defaultInspect; + return `new UUID(${inspect(this.toHexString(), options)})`; + } +} diff --git a/gateway/node_modules/bson/src/bson.ts b/gateway/node_modules/bson/src/bson.ts new file mode 100644 index 0000000000000000000000000000000000000000..20fd7940d4366f0cbb27c096b8c6715dfa605d2d --- /dev/null +++ b/gateway/node_modules/bson/src/bson.ts @@ -0,0 +1,253 @@ +import { Binary, UUID } from './binary'; +import { Code } from './code'; +import { DBRef } from './db_ref'; +import { Decimal128 } from './decimal128'; +import { Double } from './double'; +import { Int32 } from './int_32'; +import { Long } from './long'; +import { MaxKey } from './max_key'; +import { MinKey } from './min_key'; +import { ObjectId } from './objectid'; +import { internalCalculateObjectSize } from './parser/calculate_size'; +// Parts of the parser +import { internalDeserialize, type DeserializeOptions } from './parser/deserializer'; +import { serializeInto, type SerializeOptions } from './parser/serializer'; +import { BSONRegExp } from './regexp'; +import { BSONSymbol } from './symbol'; +import { Timestamp } from './timestamp'; +import { ByteUtils } from './utils/byte_utils'; +import { NumberUtils } from './utils/number_utils'; +export type { UUIDExtended, BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary'; +export type { CodeExtended } from './code'; +export type { DBRefLike } from './db_ref'; +export type { Decimal128Extended } from './decimal128'; +export type { DoubleExtended } from './double'; +export type { + EJSONOptions, + EJSONOptionsBase, + EJSONSerializeOptions, + EJSONParseOptions +} from './extended_json'; +export type { Int32Extended } from './int_32'; +export type { LongExtended } from './long'; +export type { MaxKeyExtended } from './max_key'; +export type { MinKeyExtended } from './min_key'; +export type { ObjectIdExtended, ObjectIdLike } from './objectid'; +export type { BSONRegExpExtended, BSONRegExpExtendedLegacy } from './regexp'; +export type { BSONSymbolExtended } from './symbol'; +export type { LongWithoutOverrides, TimestampExtended, TimestampOverrides } from './timestamp'; +export type { LongWithoutOverridesClass } from './timestamp'; +export type { SerializeOptions, DeserializeOptions }; + +export { + Code, + BSONSymbol, + DBRef, + Binary, + ObjectId, + UUID, + Long, + Timestamp, + Double, + Int32, + MinKey, + MaxKey, + BSONRegExp, + Decimal128, + NumberUtils, + ByteUtils +}; +export { BSONValue, bsonType, type BSONTypeTag } from './bson_value'; +export { BSONError, BSONVersionError, BSONRuntimeError, BSONOffsetError } from './error'; +export { BSONType } from './constants'; +export { EJSON } from './extended_json'; +export { onDemand, type OnDemand } from './parser/on_demand/index'; + +/** @public */ +export interface Document { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + [key: string]: any; +} + +/** @internal */ +// Default Max Size +const MAXSIZE = 1024 * 1024 * 17; + +// Current Internal Temporary Serialization Buffer +let buffer = ByteUtils.allocate(MAXSIZE); + +/** + * Sets the size of the internal serialization buffer. + * + * @param size - The desired size for the internal serialization buffer in bytes + * @public + */ +export function setInternalBufferSize(size: number): void { + // Resize the internal serialization buffer if needed + if (buffer.length < size) { + buffer = ByteUtils.allocate(size); + } +} + +/** + * Serialize a Javascript object. + * + * @param object - the Javascript object to serialize. + * @returns Buffer object containing the serialized object. + * @public + */ +export function serialize(object: Document, options: SerializeOptions = {}): Uint8Array { + // Unpack the options + const checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false; + const serializeFunctions = + typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false; + const ignoreUndefined = + typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true; + const minInternalBufferSize = + typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE; + + // Resize the internal serialization buffer if needed + if (buffer.length < minInternalBufferSize) { + buffer = ByteUtils.allocate(minInternalBufferSize); + } + + // Attempt to serialize + const serializationIndex = serializeInto( + buffer, + object, + checkKeys, + 0, + serializeFunctions, + ignoreUndefined, + null + ); + + // Create the final buffer + const finishedBuffer = ByteUtils.allocateUnsafe(serializationIndex); + + // Copy into the finished buffer + finishedBuffer.set(buffer.subarray(0, serializationIndex), 0); + + // Return the buffer + return finishedBuffer; +} + +/** + * Serialize a Javascript object using a predefined Buffer and index into the buffer, + * useful when pre-allocating the space for serialization. + * + * @param object - the Javascript object to serialize. + * @param finalBuffer - the Buffer you pre-allocated to store the serialized BSON object. + * @returns the index pointing to the last written byte in the buffer. + * @public + */ +export function serializeWithBufferAndIndex( + object: Document, + finalBuffer: Uint8Array, + options: SerializeOptions = {} +): number { + // Unpack the options + const checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false; + const serializeFunctions = + typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false; + const ignoreUndefined = + typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true; + const startIndex = typeof options.index === 'number' ? options.index : 0; + + // Attempt to serialize + const serializationIndex = serializeInto( + buffer, + object, + checkKeys, + 0, + serializeFunctions, + ignoreUndefined, + null + ); + + finalBuffer.set(buffer.subarray(0, serializationIndex), startIndex); + + // Return the index + return startIndex + serializationIndex - 1; +} + +/** + * Deserialize data as BSON. + * + * @param buffer - the buffer containing the serialized set of BSON documents. + * @returns returns the deserialized Javascript Object. + * @public + */ +export function deserialize(buffer: Uint8Array, options: DeserializeOptions = {}): Document { + return internalDeserialize(ByteUtils.toLocalBufferType(buffer), options); +} + +/** @public */ +export type CalculateObjectSizeOptions = Pick< + SerializeOptions, + 'serializeFunctions' | 'ignoreUndefined' +>; + +/** + * Calculate the bson size for a passed in Javascript object. + * + * @param object - the Javascript object to calculate the BSON byte size for + * @returns size of BSON object in bytes + * @public + */ +export function calculateObjectSize( + object: Document, + options: CalculateObjectSizeOptions = {} +): number { + options = options || {}; + + const serializeFunctions = + typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false; + const ignoreUndefined = + typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true; + + return internalCalculateObjectSize(object, serializeFunctions, ignoreUndefined); +} + +/** + * Deserialize stream data as BSON documents. + * + * @param data - the buffer containing the serialized set of BSON documents. + * @param startIndex - the start index in the data Buffer where the deserialization is to start. + * @param numberOfDocuments - number of documents to deserialize. + * @param documents - an array where to store the deserialized documents. + * @param docStartIndex - the index in the documents array from where to start inserting documents. + * @param options - additional options used for the deserialization. + * @returns next index in the buffer after deserialization **x** numbers of documents. + * @public + */ +export function deserializeStream( + data: Uint8Array | ArrayBuffer, + startIndex: number, + numberOfDocuments: number, + documents: Document[], + docStartIndex: number, + options: DeserializeOptions +): number { + const internalOptions = Object.assign( + { allowObjectSmallerThanBufferSize: true, index: 0 }, + options + ); + const bufferData = ByteUtils.toLocalBufferType(data); + + let index = startIndex; + // Loop over all documents + for (let i = 0; i < numberOfDocuments; i++) { + // Find size of the document + const size = NumberUtils.getInt32LE(bufferData, index); + // Update options with index + internalOptions.index = index; + // Parse the document at this point + documents[docStartIndex + i] = internalDeserialize(bufferData, internalOptions); + // Adjust index by the document size + index = index + size; + } + + // Return object containing end index of parsing and list of documents + return index; +} diff --git a/gateway/node_modules/bson/src/bson_value.ts b/gateway/node_modules/bson/src/bson_value.ts new file mode 100644 index 0000000000000000000000000000000000000000..40432fca22523c12c2bbfad290630d12d1ed8951 --- /dev/null +++ b/gateway/node_modules/bson/src/bson_value.ts @@ -0,0 +1,55 @@ +import { BSON_MAJOR_VERSION } from './constants'; +import { type InspectFn } from './parser/utils'; +import { BSON_VERSION_SYMBOL } from './constants'; + +/** @public */ +export type BSONTypeTag = + | 'BSONRegExp' + | 'BSONSymbol' + | 'ObjectId' + | 'Binary' + | 'Decimal128' + | 'Double' + | 'Int32' + | 'Long' + | 'MaxKey' + | 'MinKey' + | 'Timestamp' + | 'Code' + | 'DBRef'; + +/** @public */ +export const bsonType = Symbol.for('@@mdb.bson.type'); + +/** @public */ +export abstract class BSONValue { + /** @public */ + public abstract get _bsontype(): BSONTypeTag; + + public get [bsonType](): this['_bsontype'] { + return this._bsontype; + } + + /** @internal */ + get [BSON_VERSION_SYMBOL](): typeof BSON_MAJOR_VERSION { + return BSON_MAJOR_VERSION; + } + + [Symbol.for('nodejs.util.inspect.custom')]( + depth?: number, + options?: unknown, + inspect?: InspectFn + ): string { + return this.inspect(depth, options, inspect); + } + + /** + * @public + * Prints a human-readable string of BSON value information + * If invoked manually without node.js.inspect function, this will default to a modified JSON.stringify + */ + public abstract inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; + + /** @internal */ + abstract toExtendedJSON(): unknown; +} diff --git a/gateway/node_modules/bson/src/code.ts b/gateway/node_modules/bson/src/code.ts new file mode 100644 index 0000000000000000000000000000000000000000..98b1ede9a66bc9b3f494b2921f1a712b71b55341 --- /dev/null +++ b/gateway/node_modules/bson/src/code.ts @@ -0,0 +1,69 @@ +import type { Document } from './bson'; +import { BSONValue } from './bson_value'; +import { type InspectFn, defaultInspect } from './parser/utils'; + +/** @public */ +export interface CodeExtended { + $code: string; + $scope?: Document; +} + +/** + * A class representation of the BSON Code type. + * @public + * @category BSONType + */ +export class Code extends BSONValue { + get _bsontype(): 'Code' { + return 'Code'; + } + + code: string; + + // a code instance having a null scope is what determines whether + // it is BSONType 0x0D (just code) / 0x0F (code with scope) + scope: Document | null; + + /** + * @param code - a string or function. + * @param scope - an optional scope for the function. + */ + constructor(code: string | Function, scope?: Document | null) { + super(); + this.code = code.toString(); + this.scope = scope ?? null; + } + + toJSON(): { code: string; scope?: Document } { + if (this.scope != null) { + return { code: this.code, scope: this.scope }; + } + + return { code: this.code }; + } + + /** @internal */ + toExtendedJSON(): CodeExtended { + if (this.scope) { + return { $code: this.code, $scope: this.scope }; + } + + return { $code: this.code }; + } + + /** @internal */ + static fromExtendedJSON(doc: CodeExtended): Code { + return new Code(doc.$code, doc.$scope); + } + + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + inspect ??= defaultInspect; + let parametersString = inspect(this.code, options); + const multiLineFn = parametersString.includes('\n'); + if (this.scope != null) { + parametersString += `,${multiLineFn ? '\n' : ' '}${inspect(this.scope, options)}`; + } + const endingNewline = multiLineFn && this.scope === null; + return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${endingNewline ? '\n' : ''})`; + } +} diff --git a/gateway/node_modules/bson/src/constants.ts b/gateway/node_modules/bson/src/constants.ts new file mode 100644 index 0000000000000000000000000000000000000000..5751c0efee4dc598c8d51005c0f683ce5443a4a2 --- /dev/null +++ b/gateway/node_modules/bson/src/constants.ts @@ -0,0 +1,147 @@ +/** @internal */ +export const BSON_MAJOR_VERSION = 7; + +/** @internal */ +export const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version'); + +/** @internal */ +export const BSON_INT32_MAX = 0x7fffffff; +/** @internal */ +export const BSON_INT32_MIN = -0x80000000; +/** @internal */ +export const BSON_INT64_MAX = Math.pow(2, 63) - 1; +/** @internal */ +export const BSON_INT64_MIN = -Math.pow(2, 63); + +/** + * Any integer up to 2^53 can be precisely represented by a double. + * @internal + */ +export const JS_INT_MAX = Math.pow(2, 53); + +/** + * Any integer down to -2^53 can be precisely represented by a double. + * @internal + */ +export const JS_INT_MIN = -Math.pow(2, 53); + +/** Number BSON Type @internal */ +export const BSON_DATA_NUMBER = 1; + +/** String BSON Type @internal */ +export const BSON_DATA_STRING = 2; + +/** Object BSON Type @internal */ +export const BSON_DATA_OBJECT = 3; + +/** Array BSON Type @internal */ +export const BSON_DATA_ARRAY = 4; + +/** Binary BSON Type @internal */ +export const BSON_DATA_BINARY = 5; + +/** Binary BSON Type @internal */ +export const BSON_DATA_UNDEFINED = 6; + +/** ObjectId BSON Type @internal */ +export const BSON_DATA_OID = 7; + +/** Boolean BSON Type @internal */ +export const BSON_DATA_BOOLEAN = 8; + +/** Date BSON Type @internal */ +export const BSON_DATA_DATE = 9; + +/** null BSON Type @internal */ +export const BSON_DATA_NULL = 10; + +/** RegExp BSON Type @internal */ +export const BSON_DATA_REGEXP = 11; + +/** Code BSON Type @internal */ +export const BSON_DATA_DBPOINTER = 12; + +/** Code BSON Type @internal */ +export const BSON_DATA_CODE = 13; + +/** Symbol BSON Type @internal */ +export const BSON_DATA_SYMBOL = 14; + +/** Code with Scope BSON Type @internal */ +export const BSON_DATA_CODE_W_SCOPE = 15; + +/** 32 bit Integer BSON Type @internal */ +export const BSON_DATA_INT = 16; + +/** Timestamp BSON Type @internal */ +export const BSON_DATA_TIMESTAMP = 17; + +/** Long BSON Type @internal */ +export const BSON_DATA_LONG = 18; + +/** Decimal128 BSON Type @internal */ +export const BSON_DATA_DECIMAL128 = 19; + +/** MinKey BSON Type @internal */ +export const BSON_DATA_MIN_KEY = 0xff; + +/** MaxKey BSON Type @internal */ +export const BSON_DATA_MAX_KEY = 0x7f; + +/** Binary Default Type @internal */ +export const BSON_BINARY_SUBTYPE_DEFAULT = 0; + +/** Binary Function Type @internal */ +export const BSON_BINARY_SUBTYPE_FUNCTION = 1; + +/** Binary Byte Array Type @internal */ +export const BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; + +/** Binary Deprecated UUID Type @deprecated Please use BSON_BINARY_SUBTYPE_UUID_NEW @internal */ +export const BSON_BINARY_SUBTYPE_UUID = 3; + +/** Binary UUID Type @internal */ +export const BSON_BINARY_SUBTYPE_UUID_NEW = 4; + +/** Binary MD5 Type @internal */ +export const BSON_BINARY_SUBTYPE_MD5 = 5; + +/** Encrypted BSON type @internal */ +export const BSON_BINARY_SUBTYPE_ENCRYPTED = 6; + +/** Column BSON type @internal */ +export const BSON_BINARY_SUBTYPE_COLUMN = 7; + +/** Sensitive BSON type @internal */ +export const BSON_BINARY_SUBTYPE_SENSITIVE = 8; + +/** Binary User Defined Type @internal */ +export const BSON_BINARY_SUBTYPE_USER_DEFINED = 128; + +/** @public */ +export const BSONType = Object.freeze({ + double: 1, + string: 2, + object: 3, + array: 4, + binData: 5, + undefined: 6, + objectId: 7, + bool: 8, + date: 9, + null: 10, + regex: 11, + dbPointer: 12, + javascript: 13, + symbol: 14, + javascriptWithScope: 15, + int: 16, + timestamp: 17, + long: 18, + decimal: 19, + minKey: -1, + maxKey: 127 +} as const); + +/** @public */ +export type BSONType = (typeof BSONType)[keyof typeof BSONType]; diff --git a/gateway/node_modules/bson/src/db_ref.ts b/gateway/node_modules/bson/src/db_ref.ts new file mode 100644 index 0000000000000000000000000000000000000000..fbb751f8aa13807001e2b051ab411add006d88f2 --- /dev/null +++ b/gateway/node_modules/bson/src/db_ref.ts @@ -0,0 +1,128 @@ +import type { Document } from './bson'; +import { BSONValue } from './bson_value'; +import type { EJSONOptions } from './extended_json'; +import type { ObjectId } from './objectid'; +import { type InspectFn, defaultInspect } from './parser/utils'; + +/** @public */ +export interface DBRefLike { + $ref: string; + $id: ObjectId; + $db?: string; +} + +/** @internal */ +export function isDBRefLike(value: unknown): value is DBRefLike { + return ( + value != null && + typeof value === 'object' && + '$id' in value && + value.$id != null && + '$ref' in value && + typeof value.$ref === 'string' && + // If '$db' is defined it MUST be a string, otherwise it should be absent + (!('$db' in value) || ('$db' in value && typeof value.$db === 'string')) + ); +} + +/** + * A class representation of the BSON DBRef type. + * @public + * @category BSONType + */ +export class DBRef extends BSONValue { + get _bsontype(): 'DBRef' { + return 'DBRef'; + } + + collection!: string; + oid!: ObjectId; + db?: string; + fields!: Document; + + /** + * @param collection - the collection name. + * @param oid - the reference ObjectId. + * @param db - optional db name, if omitted the reference is local to the current db. + */ + constructor(collection: string, oid: ObjectId, db?: string, fields?: Document) { + super(); + // check if namespace has been provided + const parts = collection.split('.'); + if (parts.length === 2) { + db = parts.shift(); + collection = parts.shift()!; + } + + this.collection = collection; + this.oid = oid; + this.db = db; + this.fields = fields || {}; + } + + // Property provided for compatibility with the 1.x parser + // the 1.x parser used a "namespace" property, while 4.x uses "collection" + + /** @internal */ + get namespace(): string { + return this.collection; + } + + set namespace(value: string) { + this.collection = value; + } + + toJSON(): DBRefLike & Document { + const o = Object.assign( + { + $ref: this.collection, + $id: this.oid + }, + this.fields + ); + + if (this.db != null) o.$db = this.db; + return o; + } + + /** @internal */ + toExtendedJSON(options?: EJSONOptions): DBRefLike { + options = options || {}; + let o: DBRefLike = { + $ref: this.collection, + $id: this.oid + }; + + if (options.legacy) { + return o; + } + + if (this.db) o.$db = this.db; + o = Object.assign(o, this.fields); + return o; + } + + /** @internal */ + static fromExtendedJSON(doc: DBRefLike): DBRef { + const copy = Object.assign({}, doc) as Partial; + delete copy.$ref; + delete copy.$id; + delete copy.$db; + return new DBRef(doc.$ref, doc.$id, doc.$db, copy); + } + + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + inspect ??= defaultInspect; + + const args = [ + inspect(this.namespace, options), + inspect(this.oid, options), + ...(this.db ? [inspect(this.db, options)] : []), + ...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : []) + ]; + + args[1] = inspect === defaultInspect ? `new ObjectId(${args[1]})` : args[1]; + + return `new DBRef(${args.join(', ')})`; + } +} diff --git a/gateway/node_modules/bson/src/decimal128.ts b/gateway/node_modules/bson/src/decimal128.ts new file mode 100644 index 0000000000000000000000000000000000000000..8f491b3ce975444863e607d77032591d6f4b7a02 --- /dev/null +++ b/gateway/node_modules/bson/src/decimal128.ts @@ -0,0 +1,855 @@ +import { BSONValue } from './bson_value'; +import { BSONError } from './error'; +import { Long } from './long'; +import { type InspectFn, defaultInspect, isUint8Array } from './parser/utils'; +import { ByteUtils } from './utils/byte_utils'; + +const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/; +const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i; +const PARSE_NAN_REGEXP = /^(\+|-)?NaN$/i; + +const EXPONENT_MAX = 6111; +const EXPONENT_MIN = -6176; +const EXPONENT_BIAS = 6176; +const MAX_DIGITS = 34; + +// Nan value bits as 32 bit values (due to lack of longs) +const NAN_BUFFER = ByteUtils.fromNumberArray( + [ + 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + ].reverse() +); +// Infinity value bits 32 bit values (due to lack of longs) +const INF_NEGATIVE_BUFFER = ByteUtils.fromNumberArray( + [ + 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + ].reverse() +); +const INF_POSITIVE_BUFFER = ByteUtils.fromNumberArray( + [ + 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + ].reverse() +); + +const EXPONENT_REGEX = /^([-+])?(\d+)?$/; + +// Extract least significant 5 bits +const COMBINATION_MASK = 0x1f; +// Extract least significant 14 bits +const EXPONENT_MASK = 0x3fff; +// Value of combination field for Inf +const COMBINATION_INFINITY = 30; +// Value of combination field for NaN +const COMBINATION_NAN = 31; + +// Detect if the value is a digit +function isDigit(value: string): boolean { + return !isNaN(parseInt(value, 10)); +} + +// Divide two uint128 values +function divideu128(value: { parts: [number, number, number, number] }) { + const DIVISOR = Long.fromNumber(1000 * 1000 * 1000); + let _rem = Long.fromNumber(0); + + if (!value.parts[0] && !value.parts[1] && !value.parts[2] && !value.parts[3]) { + return { quotient: value, rem: _rem }; + } + + for (let i = 0; i <= 3; i++) { + // Adjust remainder to match value of next dividend + _rem = _rem.shiftLeft(32); + // Add the divided to _rem + _rem = _rem.add(new Long(value.parts[i], 0)); + value.parts[i] = _rem.div(DIVISOR).low; + _rem = _rem.modulo(DIVISOR); + } + + return { quotient: value, rem: _rem }; +} + +// Multiply two Long values and return the 128 bit value +function multiply64x2(left: Long, right: Long): { high: Long; low: Long } { + if (!left && !right) { + return { high: Long.fromNumber(0), low: Long.fromNumber(0) }; + } + + const leftHigh = left.shiftRightUnsigned(32); + const leftLow = new Long(left.getLowBits(), 0); + const rightHigh = right.shiftRightUnsigned(32); + const rightLow = new Long(right.getLowBits(), 0); + + let productHigh = leftHigh.multiply(rightHigh); + let productMid = leftHigh.multiply(rightLow); + const productMid2 = leftLow.multiply(rightHigh); + let productLow = leftLow.multiply(rightLow); + + productHigh = productHigh.add(productMid.shiftRightUnsigned(32)); + productMid = new Long(productMid.getLowBits(), 0) + .add(productMid2) + .add(productLow.shiftRightUnsigned(32)); + + productHigh = productHigh.add(productMid.shiftRightUnsigned(32)); + productLow = productMid.shiftLeft(32).add(new Long(productLow.getLowBits(), 0)); + + // Return the 128 bit result + return { high: productHigh, low: productLow }; +} + +function lessThan(left: Long, right: Long): boolean { + // Make values unsigned + const uhleft = left.high >>> 0; + const uhright = right.high >>> 0; + + // Compare high bits first + if (uhleft < uhright) { + return true; + } else if (uhleft === uhright) { + const ulleft = left.low >>> 0; + const ulright = right.low >>> 0; + if (ulleft < ulright) return true; + } + + return false; +} + +function invalidErr(string: string, message: string) { + throw new BSONError(`"${string}" is not a valid Decimal128 string - ${message}`); +} + +/** @public */ +export interface Decimal128Extended { + $numberDecimal: string; +} + +/** + * A class representation of the BSON Decimal128 type. + * @public + * @category BSONType + */ +export class Decimal128 extends BSONValue { + get _bsontype(): 'Decimal128' { + return 'Decimal128'; + } + + readonly bytes!: Uint8Array; + + /** + * @param bytes - a buffer containing the raw Decimal128 bytes in little endian order, + * or a string representation as returned by .toString() + */ + constructor(bytes: Uint8Array | string) { + super(); + if (typeof bytes === 'string') { + this.bytes = Decimal128.fromString(bytes).bytes; + } else if (bytes instanceof Uint8Array || isUint8Array(bytes)) { + if (bytes.byteLength !== 16) { + throw new BSONError('Decimal128 must take a Buffer of 16 bytes'); + } + this.bytes = bytes; + } else { + throw new BSONError('Decimal128 must take a Buffer or string'); + } + } + + /** + * Create a Decimal128 instance from a string representation + * + * @param representation - a numeric string representation. + */ + static fromString(representation: string): Decimal128 { + return Decimal128._fromString(representation, { allowRounding: false }); + } + + /** + * Create a Decimal128 instance from a string representation, allowing for rounding to 34 + * significant digits + * + * @example Example of a number that will be rounded + * ```ts + * > let d = Decimal128.fromString('37.499999999999999196428571428571375') + * Uncaught: + * BSONError: "37.499999999999999196428571428571375" is not a valid Decimal128 string - inexact rounding + * at invalidErr (/home/wajames/js-bson/lib/bson.cjs:1402:11) + * at Decimal128.fromStringInternal (/home/wajames/js-bson/lib/bson.cjs:1633:25) + * at Decimal128.fromString (/home/wajames/js-bson/lib/bson.cjs:1424:27) + * + * > d = Decimal128.fromStringWithRounding('37.499999999999999196428571428571375') + * new Decimal128("37.49999999999999919642857142857138") + * ``` + * @param representation - a numeric string representation. + */ + static fromStringWithRounding(representation: string): Decimal128 { + return Decimal128._fromString(representation, { allowRounding: true }); + } + + private static _fromString(representation: string, options: { allowRounding: boolean }) { + // Parse state tracking + let isNegative = false; + let sawSign = false; + let sawRadix = false; + let foundNonZero = false; + + // Total number of significant digits (no leading or trailing zero) + let significantDigits = 0; + // Total number of significand digits read + let nDigitsRead = 0; + // Total number of digits (no leading zeros) + let nDigits = 0; + // The number of the digits after radix + let radixPosition = 0; + // The index of the first non-zero in *str* + let firstNonZero = 0; + + // Digits Array + const digits = [0]; + // The number of digits in digits + let nDigitsStored = 0; + // Insertion pointer for digits + let digitsInsert = 0; + // The index of the last digit + let lastDigit = 0; + + // Exponent + let exponent = 0; + // The high 17 digits of the significand + let significandHigh = new Long(0, 0); + // The low 17 digits of the significand + let significandLow = new Long(0, 0); + // The biased exponent + let biasedExponent = 0; + + // Read index + let index = 0; + + // Naively prevent against REDOS attacks. + // TODO: implementing a custom parsing for this, or refactoring the regex would yield + // further gains. + if (representation.length >= 7000) { + throw new BSONError('' + representation + ' not a valid Decimal128 string'); + } + + // Results + const stringMatch = representation.match(PARSE_STRING_REGEXP); + const infMatch = representation.match(PARSE_INF_REGEXP); + const nanMatch = representation.match(PARSE_NAN_REGEXP); + + // Validate the string + if ((!stringMatch && !infMatch && !nanMatch) || representation.length === 0) { + throw new BSONError('' + representation + ' not a valid Decimal128 string'); + } + + if (stringMatch) { + // full_match = stringMatch[0] + // sign = stringMatch[1] + + const unsignedNumber = stringMatch[2]; + // stringMatch[3] is undefined if a whole number (ex "1", 12") + // but defined if a number w/ decimal in it (ex "1.0, 12.2") + + const e = stringMatch[4]; + const expSign = stringMatch[5]; + const expNumber = stringMatch[6]; + + // they provided e, but didn't give an exponent number. for ex "1e" + if (e && expNumber === undefined) invalidErr(representation, 'missing exponent power'); + + // they provided e, but didn't give a number before it. for ex "e1" + if (e && unsignedNumber === undefined) invalidErr(representation, 'missing exponent base'); + + if (e === undefined && (expSign || expNumber)) { + invalidErr(representation, 'missing e before exponent'); + } + } + + // Get the negative or positive sign + if (representation[index] === '+' || representation[index] === '-') { + sawSign = true; + isNegative = representation[index++] === '-'; + } + + // Check if user passed Infinity or NaN + if (!isDigit(representation[index]) && representation[index] !== '.') { + if (representation[index] === 'i' || representation[index] === 'I') { + return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER); + } else if (representation[index] === 'N') { + return new Decimal128(NAN_BUFFER); + } + } + + // Read all the digits + while (isDigit(representation[index]) || representation[index] === '.') { + if (representation[index] === '.') { + if (sawRadix) invalidErr(representation, 'contains multiple periods'); + + sawRadix = true; + index = index + 1; + continue; + } + + if (nDigitsStored < MAX_DIGITS) { + if (representation[index] !== '0' || foundNonZero) { + if (!foundNonZero) { + firstNonZero = nDigitsRead; + } + + foundNonZero = true; + + // Only store 34 digits + digits[digitsInsert++] = parseInt(representation[index], 10); + nDigitsStored = nDigitsStored + 1; + } + } + + if (foundNonZero) nDigits = nDigits + 1; + if (sawRadix) radixPosition = radixPosition + 1; + + nDigitsRead = nDigitsRead + 1; + index = index + 1; + } + + if (sawRadix && !nDigitsRead) + throw new BSONError('' + representation + ' not a valid Decimal128 string'); + + // Read exponent if exists + if (representation[index] === 'e' || representation[index] === 'E') { + // Read exponent digits + const match = representation.substr(++index).match(EXPONENT_REGEX); + + // No digits read + if (!match || !match[2]) return new Decimal128(NAN_BUFFER); + + // Get exponent + exponent = parseInt(match[0], 10); + + // Adjust the index + index = index + match[0].length; + } + + // Return not a number + if (representation[index]) return new Decimal128(NAN_BUFFER); + + // Done reading input + // Find first non-zero digit in digits + if (!nDigitsStored) { + digits[0] = 0; + nDigits = 1; + nDigitsStored = 1; + significantDigits = 0; + } else { + lastDigit = nDigitsStored - 1; + significantDigits = nDigits; + if (significantDigits !== 1) { + while ( + representation[ + firstNonZero + significantDigits - 1 + Number(sawSign) + Number(sawRadix) + ] === '0' + ) { + significantDigits = significantDigits - 1; + } + } + } + + // Normalization of exponent + // Correct exponent based on radix position, and shift significand as needed + // to represent user input + + // Overflow prevention + if (exponent <= radixPosition && radixPosition > exponent + (1 << 14)) { + exponent = EXPONENT_MIN; + } else { + exponent = exponent - radixPosition; + } + + // Attempt to normalize the exponent + while (exponent > EXPONENT_MAX) { + // Shift exponent to significand and decrease + lastDigit = lastDigit + 1; + if (lastDigit >= MAX_DIGITS) { + // Check if we have a zero then just hard clamp, otherwise fail + if (significantDigits === 0) { + exponent = EXPONENT_MAX; + break; + } + + invalidErr(representation, 'overflow'); + } + exponent = exponent - 1; + } + + if (options.allowRounding) { + while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) { + // Shift last digit. can only do this if < significant digits than # stored. + if (lastDigit === 0 && significantDigits < nDigitsStored) { + exponent = EXPONENT_MIN; + significantDigits = 0; + break; + } + + if (nDigitsStored < nDigits) { + // adjust to match digits not stored + nDigits = nDigits - 1; + } else { + // adjust to round + lastDigit = lastDigit - 1; + } + + if (exponent < EXPONENT_MAX) { + exponent = exponent + 1; + } else { + // Check if we have a zero then just hard clamp, otherwise fail + const digitsString = digits.join(''); + if (digitsString.match(/^0+$/)) { + exponent = EXPONENT_MAX; + break; + } + invalidErr(representation, 'overflow'); + } + } + + // Round + // We've normalized the exponent, but might still need to round. + if (lastDigit + 1 < significantDigits) { + let endOfString = nDigitsRead; + + // If we have seen a radix point, 'string' is 1 longer than we have + // documented with ndigits_read, so inc the position of the first nonzero + // digit and the position that digits are read to. + if (sawRadix) { + firstNonZero = firstNonZero + 1; + endOfString = endOfString + 1; + } + // if negative, we need to increment again to account for - sign at start. + if (sawSign) { + firstNonZero = firstNonZero + 1; + endOfString = endOfString + 1; + } + + const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10); + let roundBit = 0; + + if (roundDigit >= 5) { + roundBit = 1; + if (roundDigit === 5) { + roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0; + for (let i = firstNonZero + lastDigit + 2; i < endOfString; i++) { + if (parseInt(representation[i], 10)) { + roundBit = 1; + break; + } + } + } + } + + if (roundBit) { + let dIdx = lastDigit; + + for (; dIdx >= 0; dIdx--) { + if (++digits[dIdx] > 9) { + digits[dIdx] = 0; + + // overflowed most significant digit + if (dIdx === 0) { + if (exponent < EXPONENT_MAX) { + exponent = exponent + 1; + digits[dIdx] = 1; + } else { + return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER); + } + } + } else { + break; + } + } + } + } + } else { + while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) { + // Shift last digit. can only do this if < significant digits than # stored. + if (lastDigit === 0) { + if (significantDigits === 0) { + exponent = EXPONENT_MIN; + break; + } + + invalidErr(representation, 'exponent underflow'); + } + + if (nDigitsStored < nDigits) { + if ( + representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' && + significantDigits !== 0 + ) { + invalidErr(representation, 'inexact rounding'); + } + // adjust to match digits not stored + nDigits = nDigits - 1; + } else { + if (digits[lastDigit] !== 0) { + invalidErr(representation, 'inexact rounding'); + } + // adjust to round + lastDigit = lastDigit - 1; + } + + if (exponent < EXPONENT_MAX) { + exponent = exponent + 1; + } else { + invalidErr(representation, 'overflow'); + } + } + + // Round + // We've normalized the exponent, but might still need to round. + if (lastDigit + 1 < significantDigits) { + // If we have seen a radix point, 'string' is 1 longer than we have + // documented with ndigits_read, so inc the position of the first nonzero + // digit and the position that digits are read to. + if (sawRadix) { + firstNonZero = firstNonZero + 1; + } + // if saw sign, we need to increment again to account for - or + sign at start. + if (sawSign) { + firstNonZero = firstNonZero + 1; + } + + const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10); + + if (roundDigit !== 0) { + invalidErr(representation, 'inexact rounding'); + } + } + } + + // Encode significand + // The high 17 digits of the significand + significandHigh = Long.fromNumber(0); + // The low 17 digits of the significand + significandLow = Long.fromNumber(0); + + // read a zero + if (significantDigits === 0) { + significandHigh = Long.fromNumber(0); + significandLow = Long.fromNumber(0); + } else if (lastDigit < 17) { + let dIdx = 0; + significandLow = Long.fromNumber(digits[dIdx++]); + significandHigh = new Long(0, 0); + + for (; dIdx <= lastDigit; dIdx++) { + significandLow = significandLow.multiply(Long.fromNumber(10)); + significandLow = significandLow.add(Long.fromNumber(digits[dIdx])); + } + } else { + let dIdx = 0; + significandHigh = Long.fromNumber(digits[dIdx++]); + + for (; dIdx <= lastDigit - 17; dIdx++) { + significandHigh = significandHigh.multiply(Long.fromNumber(10)); + significandHigh = significandHigh.add(Long.fromNumber(digits[dIdx])); + } + + significandLow = Long.fromNumber(digits[dIdx++]); + + for (; dIdx <= lastDigit; dIdx++) { + significandLow = significandLow.multiply(Long.fromNumber(10)); + significandLow = significandLow.add(Long.fromNumber(digits[dIdx])); + } + } + + const significand = multiply64x2(significandHigh, Long.fromString('100000000000000000')); + significand.low = significand.low.add(significandLow); + + if (lessThan(significand.low, significandLow)) { + significand.high = significand.high.add(Long.fromNumber(1)); + } + + // Biased exponent + biasedExponent = exponent + EXPONENT_BIAS; + const dec = { low: Long.fromNumber(0), high: Long.fromNumber(0) }; + + // Encode combination, exponent, and significand. + if ( + significand.high.shiftRightUnsigned(49).and(Long.fromNumber(1)).equals(Long.fromNumber(1)) + ) { + // Encode '11' into bits 1 to 3 + dec.high = dec.high.or(Long.fromNumber(0x3).shiftLeft(61)); + dec.high = dec.high.or( + Long.fromNumber(biasedExponent).and(Long.fromNumber(0x3fff).shiftLeft(47)) + ); + dec.high = dec.high.or(significand.high.and(Long.fromNumber(0x7fffffffffff))); + } else { + dec.high = dec.high.or(Long.fromNumber(biasedExponent & 0x3fff).shiftLeft(49)); + dec.high = dec.high.or(significand.high.and(Long.fromNumber(0x1ffffffffffff))); + } + + dec.low = significand.low; + + // Encode sign + if (isNegative) { + dec.high = dec.high.or(Long.fromString('9223372036854775808')); + } + + // Encode into a buffer + const buffer = ByteUtils.allocateUnsafe(16); + index = 0; + + // Encode the low 64 bits of the decimal + // Encode low bits + buffer[index++] = dec.low.low & 0xff; + buffer[index++] = (dec.low.low >> 8) & 0xff; + buffer[index++] = (dec.low.low >> 16) & 0xff; + buffer[index++] = (dec.low.low >> 24) & 0xff; + // Encode high bits + buffer[index++] = dec.low.high & 0xff; + buffer[index++] = (dec.low.high >> 8) & 0xff; + buffer[index++] = (dec.low.high >> 16) & 0xff; + buffer[index++] = (dec.low.high >> 24) & 0xff; + + // Encode the high 64 bits of the decimal + // Encode low bits + buffer[index++] = dec.high.low & 0xff; + buffer[index++] = (dec.high.low >> 8) & 0xff; + buffer[index++] = (dec.high.low >> 16) & 0xff; + buffer[index++] = (dec.high.low >> 24) & 0xff; + // Encode high bits + buffer[index++] = dec.high.high & 0xff; + buffer[index++] = (dec.high.high >> 8) & 0xff; + buffer[index++] = (dec.high.high >> 16) & 0xff; + buffer[index++] = (dec.high.high >> 24) & 0xff; + + // Return the new Decimal128 + return new Decimal128(buffer); + } + /** Create a string representation of the raw Decimal128 value */ + toString(): string { + // Note: bits in this routine are referred to starting at 0, + // from the sign bit, towards the coefficient. + + // decoded biased exponent (14 bits) + let biased_exponent; + // the number of significand digits + let significand_digits = 0; + // the base-10 digits in the significand + const significand = new Array(36); + for (let i = 0; i < significand.length; i++) significand[i] = 0; + // read pointer into significand + let index = 0; + + // true if the number is zero + let is_zero = false; + + // the most significant significand bits (50-46) + let significand_msb; + // temporary storage for significand decoding + let significand128: { parts: [number, number, number, number] } = { parts: [0, 0, 0, 0] }; + // indexing variables + let j, k; + + // Output string + const string: string[] = []; + + // Unpack index + index = 0; + + // Buffer reference + const buffer = this.bytes; + + // Unpack the low 64bits into a long + // bits 96 - 127 + const low = + buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24); + // bits 64 - 95 + const midl = + buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24); + + // Unpack the high 64bits into a long + // bits 32 - 63 + const midh = + buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24); + // bits 0 - 31 + const high = + buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24); + + // Unpack index + index = 0; + + // Create the state of the decimal + const dec = { + low: new Long(low, midl), + high: new Long(midh, high) + }; + + if (dec.high.lessThan(Long.ZERO)) { + string.push('-'); + } + + // Decode combination field and exponent + // bits 1 - 5 + const combination = (high >> 26) & COMBINATION_MASK; + + if (combination >> 3 === 3) { + // Check for 'special' values + if (combination === COMBINATION_INFINITY) { + return string.join('') + 'Infinity'; + } else if (combination === COMBINATION_NAN) { + return 'NaN'; + } else { + biased_exponent = (high >> 15) & EXPONENT_MASK; + significand_msb = 0x08 + ((high >> 14) & 0x01); + } + } else { + significand_msb = (high >> 14) & 0x07; + biased_exponent = (high >> 17) & EXPONENT_MASK; + } + + // unbiased exponent + const exponent = biased_exponent - EXPONENT_BIAS; + + // Create string of significand digits + + // Convert the 114-bit binary number represented by + // (significand_high, significand_low) to at most 34 decimal + // digits through modulo and division. + significand128.parts[0] = (high & 0x3fff) + ((significand_msb & 0xf) << 14); + significand128.parts[1] = midh; + significand128.parts[2] = midl; + significand128.parts[3] = low; + + if ( + significand128.parts[0] === 0 && + significand128.parts[1] === 0 && + significand128.parts[2] === 0 && + significand128.parts[3] === 0 + ) { + is_zero = true; + } else { + for (k = 3; k >= 0; k--) { + let least_digits = 0; + // Perform the divide + const result = divideu128(significand128); + significand128 = result.quotient; + least_digits = result.rem.low; + + // We now have the 9 least significant digits (in base 2). + // Convert and output to string. + if (!least_digits) continue; + + for (j = 8; j >= 0; j--) { + // significand[k * 9 + j] = Math.round(least_digits % 10); + significand[k * 9 + j] = least_digits % 10; + // least_digits = Math.round(least_digits / 10); + least_digits = Math.floor(least_digits / 10); + } + } + } + + // Output format options: + // Scientific - [-]d.dddE(+/-)dd or [-]dE(+/-)dd + // Regular - ddd.ddd + + if (is_zero) { + significand_digits = 1; + significand[index] = 0; + } else { + significand_digits = 36; + while (!significand[index]) { + significand_digits = significand_digits - 1; + index = index + 1; + } + } + + // the exponent if scientific notation is used + const scientific_exponent = significand_digits - 1 + exponent; + + // The scientific exponent checks are dictated by the string conversion + // specification and are somewhat arbitrary cutoffs. + // + // We must check exponent > 0, because if this is the case, the number + // has trailing zeros. However, we *cannot* output these trailing zeros, + // because doing so would change the precision of the value, and would + // change stored data if the string converted number is round tripped. + if (scientific_exponent >= 34 || scientific_exponent <= -7 || exponent > 0) { + // Scientific format + + // if there are too many significant digits, we should just be treating numbers + // as + or - 0 and using the non-scientific exponent (this is for the "invalid + // representation should be treated as 0/-0" spec cases in decimal128-1.json) + if (significand_digits > 34) { + string.push(`${0}`); + if (exponent > 0) string.push(`E+${exponent}`); + else if (exponent < 0) string.push(`E${exponent}`); + return string.join(''); + } + + string.push(`${significand[index++]}`); + significand_digits = significand_digits - 1; + + if (significand_digits) { + string.push('.'); + } + + for (let i = 0; i < significand_digits; i++) { + string.push(`${significand[index++]}`); + } + + // Exponent + string.push('E'); + if (scientific_exponent > 0) { + string.push(`+${scientific_exponent}`); + } else { + string.push(`${scientific_exponent}`); + } + } else { + // Regular format with no decimal place + if (exponent >= 0) { + for (let i = 0; i < significand_digits; i++) { + string.push(`${significand[index++]}`); + } + } else { + let radix_position = significand_digits + exponent; + + // non-zero digits before radix + if (radix_position > 0) { + for (let i = 0; i < radix_position; i++) { + string.push(`${significand[index++]}`); + } + } else { + string.push('0'); + } + + string.push('.'); + // add leading zeros after radix + while (radix_position++ < 0) { + string.push('0'); + } + + for (let i = 0; i < significand_digits - Math.max(radix_position - 1, 0); i++) { + string.push(`${significand[index++]}`); + } + } + } + + return string.join(''); + } + + toJSON(): Decimal128Extended { + return { $numberDecimal: this.toString() }; + } + + /** @internal */ + toExtendedJSON(): Decimal128Extended { + return { $numberDecimal: this.toString() }; + } + + /** @internal */ + static fromExtendedJSON(doc: Decimal128Extended): Decimal128 { + return Decimal128.fromString(doc.$numberDecimal); + } + + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + inspect ??= defaultInspect; + const d128string = inspect(this.toString(), options); + return `new Decimal128(${d128string})`; + } +} diff --git a/gateway/node_modules/bson/src/double.ts b/gateway/node_modules/bson/src/double.ts new file mode 100644 index 0000000000000000000000000000000000000000..4a3d12982818489718dcc1de36f5bac10e171c1d --- /dev/null +++ b/gateway/node_modules/bson/src/double.ts @@ -0,0 +1,115 @@ +import { BSONValue } from './bson_value'; +import { BSONError } from './error'; +import type { EJSONOptions } from './extended_json'; +import { type InspectFn, defaultInspect } from './parser/utils'; + +/** @public */ +export interface DoubleExtended { + $numberDouble: string; +} + +/** + * A class representation of the BSON Double type. + * @public + * @category BSONType + */ +export class Double extends BSONValue { + get _bsontype(): 'Double' { + return 'Double'; + } + + value!: number; + /** + * Create a Double type + * + * @param value - the number we want to represent as a double. + */ + constructor(value: number) { + super(); + if ((value as unknown) instanceof Number) { + value = value.valueOf(); + } + + this.value = +value; + } + + /** + * Attempt to create an double type from string. + * + * This method will throw a BSONError on any string input that is not representable as a IEEE-754 64-bit double. + * Notably, this method will also throw on the following string formats: + * - Strings in non-decimal and non-exponential formats (binary, hex, or octal digits) + * - Strings with characters other than numeric, floating point, or leading sign characters (Note: 'Infinity', '-Infinity', and 'NaN' input strings are still allowed) + * - Strings with leading and/or trailing whitespace + * + * Strings with leading zeros, however, are also allowed + * + * @param value - the string we want to represent as a double. + */ + static fromString(value: string): Double { + const coercedValue = Number(value); + + if (value === 'NaN') return new Double(NaN); + if (value === 'Infinity') return new Double(Infinity); + if (value === '-Infinity') return new Double(-Infinity); + + if (!Number.isFinite(coercedValue)) { + throw new BSONError(`Input: ${value} is not representable as a Double`); + } + if (value.trim() !== value) { + throw new BSONError(`Input: '${value}' contains whitespace`); + } + if (value === '') { + throw new BSONError(`Input is an empty string`); + } + if (/[^-0-9.+eE]/.test(value)) { + throw new BSONError(`Input: '${value}' is not in decimal or exponential notation`); + } + return new Double(coercedValue); + } + + /** + * Access the number value. + * + * @returns returns the wrapped double number. + */ + valueOf(): number { + return this.value; + } + + toJSON(): number { + return this.value; + } + + toString(radix?: number): string { + return this.value.toString(radix); + } + + /** @internal */ + toExtendedJSON(options?: EJSONOptions): number | DoubleExtended { + if (options && (options.legacy || (options.relaxed && isFinite(this.value)))) { + return this.value; + } + + if (Object.is(Math.sign(this.value), -0)) { + // NOTE: JavaScript has +0 and -0, apparently to model limit calculations. If a user + // explicitly provided `-0` then we need to ensure the sign makes it into the output + return { $numberDouble: '-0.0' }; + } + + return { + $numberDouble: Number.isInteger(this.value) ? this.value.toFixed(1) : this.value.toString() + }; + } + + /** @internal */ + static fromExtendedJSON(doc: DoubleExtended, options?: EJSONOptions): number | Double { + const doubleValue = parseFloat(doc.$numberDouble); + return options && options.relaxed ? doubleValue : new Double(doubleValue); + } + + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + inspect ??= defaultInspect; + return `new Double(${inspect(this.value, options)})`; + } +} diff --git a/gateway/node_modules/bson/src/error.ts b/gateway/node_modules/bson/src/error.ts new file mode 100644 index 0000000000000000000000000000000000000000..ef5184a4acd126de683850bdfd4689fdeac8ace6 --- /dev/null +++ b/gateway/node_modules/bson/src/error.ts @@ -0,0 +1,105 @@ +import { BSON_MAJOR_VERSION } from './constants'; + +/** + * @public + * @category Error + * + * `BSONError` objects are thrown when BSON encounters an error. + * + * This is the parent class for all the other errors thrown by this library. + */ +export class BSONError extends Error { + /** + * @internal + * The underlying algorithm for isBSONError may change to improve how strict it is + * about determining if an input is a BSONError. But it must remain backwards compatible + * with previous minors & patches of the current major version. + */ + protected get bsonError(): true { + return true; + } + + override get name(): string { + return 'BSONError'; + } + + constructor(message: string, options?: { cause?: unknown }) { + super(message, options); + } + + /** + * @public + * + * All errors thrown from the BSON library inherit from `BSONError`. + * This method can assist with determining if an error originates from the BSON library + * even if it does not pass an `instanceof` check against this class' constructor. + * + * @param value - any javascript value that needs type checking + */ + public static isBSONError(value: unknown): value is BSONError { + return ( + value != null && + typeof value === 'object' && + 'bsonError' in value && + value.bsonError === true && + // Do not access the following properties, just check existence + 'name' in value && + 'message' in value && + 'stack' in value + ); + } +} + +/** + * @public + * @category Error + */ +export class BSONVersionError extends BSONError { + get name(): 'BSONVersionError' { + return 'BSONVersionError'; + } + + constructor() { + super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.x.x`); + } +} + +/** + * @public + * @category Error + * + * An error generated when BSON functions encounter an unexpected input + * or reaches an unexpected/invalid internal state + * + */ +export class BSONRuntimeError extends BSONError { + get name(): 'BSONRuntimeError' { + return 'BSONRuntimeError'; + } + + constructor(message: string) { + super(message); + } +} + +/** + * @public + * @category Error + * + * @experimental + * + * An error generated when BSON bytes are invalid. + * Reports the offset the parser was able to reach before encountering the error. + */ +export class BSONOffsetError extends BSONError { + public get name(): 'BSONOffsetError' { + return 'BSONOffsetError'; + } + + public offset: number; + + constructor(message: string, offset: number, options?: { cause?: unknown }) { + super(`${message}. offset: ${offset}`, options); + this.offset = offset; + } +} diff --git a/gateway/node_modules/bson/src/extended_json.ts b/gateway/node_modules/bson/src/extended_json.ts new file mode 100644 index 0000000000000000000000000000000000000000..8dce14f480efb2ebbaa6187c62bb88bb2e387663 --- /dev/null +++ b/gateway/node_modules/bson/src/extended_json.ts @@ -0,0 +1,557 @@ +import { Binary } from './binary'; +import type { Document } from './bson'; +import { Code } from './code'; +import { + BSON_INT32_MAX, + BSON_INT32_MIN, + BSON_INT64_MAX, + BSON_INT64_MIN, + BSON_MAJOR_VERSION, + BSON_VERSION_SYMBOL +} from './constants'; +import { DBRef, isDBRefLike } from './db_ref'; +import { Decimal128 } from './decimal128'; +import { Double } from './double'; +import { BSONError, BSONRuntimeError, BSONVersionError } from './error'; +import { Int32 } from './int_32'; +import { Long } from './long'; +import { MaxKey } from './max_key'; +import { MinKey } from './min_key'; +import { ObjectId } from './objectid'; +import { isDate, isRegExp, isMap } from './parser/utils'; +import { BSONRegExp } from './regexp'; +import { BSONSymbol } from './symbol'; +import { Timestamp } from './timestamp'; + +/** @public */ +export type EJSONOptionsBase = { + /** + * Output using the Extended JSON v1 spec + * @defaultValue `false` + */ + legacy?: boolean; + /** + * Enable Extended JSON's `relaxed` mode, which attempts to return native JS types where possible, rather than BSON types + * @defaultValue `false` + */ + relaxed?: boolean; +}; + +/** @public */ +export type EJSONSerializeOptions = EJSONOptionsBase & { + /** + * Omits undefined values from the output instead of converting them to null + * @defaultValue `false` + */ + ignoreUndefined?: boolean; +}; + +/** @public */ +export type EJSONParseOptions = EJSONOptionsBase & { + /** + * Enable native bigint support + * @defaultValue `false` + */ + useBigInt64?: boolean; +}; + +/** @public */ +export type EJSONOptions = EJSONSerializeOptions & EJSONParseOptions; + +/** @internal */ +type BSONType = + | Binary + | Code + | DBRef + | Decimal128 + | Double + | Int32 + | Long + | MaxKey + | MinKey + | ObjectId + | BSONRegExp + | BSONSymbol + | Timestamp; + +function isBSONType(value: unknown): value is BSONType { + return ( + value != null && + typeof value === 'object' && + '_bsontype' in value && + typeof value._bsontype === 'string' + ); +} + +// all the types where we don't need to do any special processing and can just pass the EJSON +//straight to type.fromExtendedJSON +const keysToCodecs = { + $oid: ObjectId, + $binary: Binary, + $uuid: Binary, + $symbol: BSONSymbol, + $numberInt: Int32, + $numberDecimal: Decimal128, + $numberDouble: Double, + $numberLong: Long, + $minKey: MinKey, + $maxKey: MaxKey, + $regex: BSONRegExp, + $regularExpression: BSONRegExp, + $timestamp: Timestamp +} as const; + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function deserializeValue(value: any, options: EJSONOptions = {}) { + if (typeof value === 'number') { + // TODO(NODE-4377): EJSON js number handling diverges from BSON + const in32BitRange = value <= BSON_INT32_MAX && value >= BSON_INT32_MIN; + const in64BitRange = value <= BSON_INT64_MAX && value >= BSON_INT64_MIN; + + if (options.relaxed || options.legacy) { + return value; + } + + if (Number.isInteger(value) && !Object.is(value, -0)) { + // interpret as being of the smallest BSON integer type that can represent the number exactly + if (in32BitRange) { + return new Int32(value); + } + if (in64BitRange) { + if (options.useBigInt64) { + return BigInt(value); + } + return Long.fromNumber(value); + } + } + + // If the number is a non-integer or out of integer range, should interpret as BSON Double. + return new Double(value); + } + + // from here on out we're looking for bson types, so bail if its not an object + if (value == null || typeof value !== 'object') return value; + + // upgrade deprecated undefined to null + if (value.$undefined) return null; + + const keys = Object.keys(value).filter( + k => k.startsWith('$') && value[k] != null + ) as (keyof typeof keysToCodecs)[]; + for (let i = 0; i < keys.length; i++) { + const c = keysToCodecs[keys[i]]; + if (c) return c.fromExtendedJSON(value, options); + } + + if (value.$date != null) { + const d = value.$date; + const date = new Date(); + + if (options.legacy) { + if (typeof d === 'number') date.setTime(d); + else if (typeof d === 'string') date.setTime(Date.parse(d)); + else if (typeof d === 'bigint') date.setTime(Number(d)); + else throw new BSONRuntimeError(`Unrecognized type for EJSON date: ${typeof d}`); + } else { + if (typeof d === 'string') date.setTime(Date.parse(d)); + else if (Long.isLong(d)) date.setTime(d.toNumber()); + else if (typeof d === 'number' && options.relaxed) date.setTime(d); + else if (typeof d === 'bigint') date.setTime(Number(d)); + else throw new BSONRuntimeError(`Unrecognized type for EJSON date: ${typeof d}`); + } + return date; + } + + if (value.$code != null) { + const copy = Object.assign({}, value); + if (value.$scope) { + copy.$scope = deserializeValue(value.$scope); + } + + return Code.fromExtendedJSON(value); + } + + if (isDBRefLike(value) || value.$dbPointer) { + const v = value.$ref ? value : value.$dbPointer; + + // we run into this in a "degenerate EJSON" case (with $id and $ref order flipped) + // because of the order JSON.parse goes through the document + if (v instanceof DBRef) return v; + + const dollarKeys = Object.keys(v).filter(k => k.startsWith('$')); + let valid = true; + dollarKeys.forEach(k => { + if (['$ref', '$id', '$db'].indexOf(k) === -1) valid = false; + }); + + // only make DBRef if $ keys are all valid + if (valid) return DBRef.fromExtendedJSON(v); + } + + return value; +} + +type EJSONSerializeInternalOptions = EJSONSerializeOptions & { + seenObjects: { obj: unknown; propertyName: string }[]; +}; + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function serializeArray(array: any[], options: EJSONSerializeInternalOptions): any[] { + return array.map((v: unknown, index: number) => { + options.seenObjects.push({ propertyName: `index ${index}`, obj: null }); + try { + return serializeValue(v, options); + } finally { + options.seenObjects.pop(); + } + }); +} + +function getISOString(date: Date) { + const isoStr = date.toISOString(); + // we should only show milliseconds in timestamp if they're non-zero + return date.getUTCMilliseconds() !== 0 ? isoStr : isoStr.slice(0, -5) + 'Z'; +} + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function serializeValue(value: any, options: EJSONSerializeInternalOptions): any { + if (value instanceof Map || isMap(value)) { + const obj: Record = Object.create(null); + for (const [k, v] of value) { + if (typeof k !== 'string') { + throw new BSONError('Can only serialize maps with string keys'); + } + obj[k] = v; + } + + return serializeValue(obj, options); + } + + if ((typeof value === 'object' || typeof value === 'function') && value !== null) { + const index = options.seenObjects.findIndex(entry => entry.obj === value); + if (index !== -1) { + const props = options.seenObjects.map(entry => entry.propertyName); + const leadingPart = props + .slice(0, index) + .map(prop => `${prop} -> `) + .join(''); + const alreadySeen = props[index]; + const circularPart = + ' -> ' + + props + .slice(index + 1, props.length - 1) + .map(prop => `${prop} -> `) + .join(''); + const current = props[props.length - 1]; + const leadingSpace = ' '.repeat(leadingPart.length + alreadySeen.length / 2); + const dashes = '-'.repeat( + circularPart.length + (alreadySeen.length + current.length) / 2 - 1 + ); + + throw new BSONError( + 'Converting circular structure to EJSON:\n' + + ` ${leadingPart}${alreadySeen}${circularPart}${current}\n` + + ` ${leadingSpace}\\${dashes}/` + ); + } + options.seenObjects[options.seenObjects.length - 1].obj = value; + } + + if (Array.isArray(value)) return serializeArray(value, options); + + if (value === undefined) return options.ignoreUndefined ? undefined : null; + + if (value instanceof Date || isDate(value)) { + const dateNum = value.getTime(), + // is it in year range 1970-9999? + // 253402300800000 is the first instant of year 10000 (+010000-01-01T00:00:00Z). + // It is the exclusive upper bound: a relaxed date is emitted as an ISO 8601 string, + // which only has a 4-digit year, so year 10000 and later are kept as a numeric timestamp. + inRange = dateNum > -1 && dateNum < 253402300800000; + + if (options.legacy) { + return options.relaxed && inRange + ? { $date: value.getTime() } + : { $date: getISOString(value) }; + } + return options.relaxed && inRange + ? { $date: getISOString(value) } + : { $date: { $numberLong: value.getTime().toString() } }; + } + + if (typeof value === 'number' && (!options.relaxed || !isFinite(value))) { + if (Number.isInteger(value) && !Object.is(value, -0)) { + // interpret as being of the smallest BSON integer type that can represent the number exactly + if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX) { + return { $numberInt: value.toString() }; + } + if (value >= BSON_INT64_MIN && value <= BSON_INT64_MAX) { + // TODO(NODE-4377): EJSON js number handling diverges from BSON + return { $numberLong: value.toString() }; + } + } + return { $numberDouble: Object.is(value, -0) ? '-0.0' : value.toString() }; + } + + if (typeof value === 'bigint') { + if (!options.relaxed) { + return { $numberLong: BigInt.asIntN(64, value).toString() }; + } + return Number(BigInt.asIntN(64, value)); + } + + if (value instanceof RegExp || isRegExp(value)) { + let flags = value.flags; + if (flags === undefined) { + const match = value.toString().match(/[gimuy]*$/); + if (match) { + flags = match[0]; + } + } + + const rx = new BSONRegExp(value.source, flags); + return rx.toExtendedJSON(options); + } + + if (value != null && typeof value === 'object') return serializeDocument(value, options); + return value; +} + +const BSON_TYPE_MAPPINGS = { + Binary: (o: Binary) => new Binary(o.value(), o.sub_type), + Code: (o: Code) => new Code(o.code, o.scope), + DBRef: (o: DBRef) => new DBRef(o.collection || o.namespace, o.oid, o.db, o.fields), // "namespace" for 1.x library backwards compat + Decimal128: (o: Decimal128) => new Decimal128(o.bytes), + Double: (o: Double) => new Double(o.value), + Int32: (o: Int32) => new Int32(o.value), + Long: ( + o: Long & { + low_: number; + high_: number; + unsigned_: boolean | undefined; + } + ) => + Long.fromBits( + // underscore variants for 1.x backwards compatibility + o.low != null ? o.low : o.low_, + o.low != null ? o.high : o.high_, + o.low != null ? o.unsigned : o.unsigned_ + ), + MaxKey: () => new MaxKey(), + MinKey: () => new MinKey(), + ObjectId: (o: ObjectId) => new ObjectId(o), + BSONRegExp: (o: BSONRegExp) => new BSONRegExp(o.pattern, o.options), + BSONSymbol: (o: BSONSymbol) => new BSONSymbol(o.value), + Timestamp: (o: Timestamp) => Timestamp.fromBits(o.low, o.high) +} as const; + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function serializeDocument(doc: any, options: EJSONSerializeInternalOptions) { + if (doc == null || typeof doc !== 'object') throw new BSONError('not an object instance'); + + const bsontype: BSONType['_bsontype'] = doc._bsontype; + if (typeof bsontype === 'undefined') { + // It's a regular object. Recursively serialize its property values. + const _doc: Document = {}; + for (const name of Object.keys(doc)) { + options.seenObjects.push({ propertyName: name, obj: null }); + try { + const value = serializeValue(doc[name], options); + if (name === '__proto__') { + Object.defineProperty(_doc, name, { + value, + writable: true, + enumerable: true, + configurable: true + }); + } else { + _doc[name] = value; + } + } finally { + options.seenObjects.pop(); + } + } + return _doc; + } else if ( + doc != null && + typeof doc === 'object' && + typeof doc._bsontype === 'string' && + doc[BSON_VERSION_SYMBOL] !== BSON_MAJOR_VERSION + ) { + throw new BSONVersionError(); + } else if (isBSONType(doc)) { + // the "document" is really just a BSON type object + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let outDoc: any = doc; + if (typeof outDoc.toExtendedJSON !== 'function') { + // There's no EJSON serialization function on the object. It's probably an + // object created by a previous version of this library (or another library) + // that's duck-typing objects to look like they were generated by this library). + // Copy the object into this library's version of that type. + const mapper = BSON_TYPE_MAPPINGS[doc._bsontype]; + if (!mapper) { + throw new BSONError('Unrecognized or invalid _bsontype: ' + doc._bsontype); + } + outDoc = mapper(outDoc); + } + + // Two BSON types may have nested objects that may need to be serialized too + if (bsontype === 'Code' && outDoc.scope) { + outDoc = new Code(outDoc.code, serializeValue(outDoc.scope, options)); + } else if (bsontype === 'DBRef' && outDoc.oid) { + outDoc = new DBRef( + serializeValue(outDoc.collection, options), + serializeValue(outDoc.oid, options), + serializeValue(outDoc.db, options), + serializeValue(outDoc.fields, options) + ); + } + + return outDoc.toExtendedJSON(options); + } else { + throw new BSONError('_bsontype must be a string, but was: ' + typeof bsontype); + } +} + +/** + * Parse an Extended JSON string, constructing the JavaScript value or object described by that + * string. + * + * @example + * ```js + * const { EJSON } = require('bson'); + * const text = '{ "int32": { "$numberInt": "10" } }'; + * + * // prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } } + * console.log(EJSON.parse(text, { relaxed: false })); + * + * // prints { int32: 10 } + * console.log(EJSON.parse(text)); + * ``` + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function parse(text: string, options?: EJSONParseOptions): any { + const ejsonOptions = { + useBigInt64: options?.useBigInt64 ?? false, + relaxed: options?.relaxed ?? true, + legacy: options?.legacy ?? false + }; + return JSON.parse(text, (key, value) => { + if (key.indexOf('\x00') !== -1) { + throw new BSONError( + `BSON Document field names cannot contain null bytes, found: ${JSON.stringify(key)}` + ); + } + return deserializeValue(value, ejsonOptions); + }); +} + +/* eslint-disable @typescript-eslint/no-explicit-any */ +/** + * Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer + * function is specified or optionally including only the specified properties if a replacer array + * is specified. + * + * @param value - The value to convert to extended JSON + * @param replacer - A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string + * @param space - A String or Number object that's used to insert white space into the output JSON string for readability purposes. + * @param options - Optional settings + * + * @example + * ```js + * const { EJSON } = require('bson'); + * const Int32 = require('mongodb').Int32; + * const doc = { int32: new Int32(10) }; + * + * // prints '{"int32":{"$numberInt":"10"}}' + * console.log(EJSON.stringify(doc, { relaxed: false })); + * + * // prints '{"int32":10}' + * console.log(EJSON.stringify(doc)); + * + * // prints '{"int32":{"$numberInt":"10"}}' with 2 space indentation + * console.log(EJSON.stringify(doc, { relaxed: false }, 2)); + * ``` + */ +function stringify( + value: any, + replacer?: (number | string)[] | ((this: any, key: string, value: any) => any) | null, + space?: string | number, + options?: EJSONSerializeOptions +): string; +function stringify( + value: any, + replacer?: (number | string)[] | ((this: any, key: string, value: any) => any) | null, + options?: EJSONSerializeOptions +): string; +function stringify(value: any, options?: EJSONSerializeOptions, space?: string | number): string; +function stringify( + value: any, + replacerOrOptions?: + | (number | string)[] + | ((this: any, key: string, value: any) => any) + | null + | EJSONSerializeOptions, + spaceOrOptions?: string | number | EJSONSerializeOptions, + options?: EJSONSerializeOptions +): string { + /* eslint-enable @typescript-eslint/no-explicit-any */ + + if (spaceOrOptions != null && typeof spaceOrOptions === 'object') { + options = spaceOrOptions; + spaceOrOptions = undefined; + } + if ( + replacerOrOptions != null && + typeof replacerOrOptions === 'object' && + !Array.isArray(replacerOrOptions) + ) { + options = replacerOrOptions; + replacerOrOptions = undefined; + } + + const serializeOptions = Object.assign({ relaxed: true, legacy: false }, options, { + seenObjects: [{ propertyName: '(root)', obj: null }] + }); + + const doc = serializeValue(value, serializeOptions); + return JSON.stringify(doc, replacerOrOptions as Parameters[1], spaceOrOptions); +} + +/** + * Serializes an object to an Extended JSON string, and reparse it as a JavaScript object. + * + * @param value - The object to serialize + * @param options - Optional settings passed to the `stringify` function + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function EJSONserialize(value: any, options?: EJSONSerializeOptions): Document { + options = options || {}; + return JSON.parse(stringify(value, options)); +} + +/** + * Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types + * + * @param ejson - The Extended JSON object to deserialize + * @param options - Optional settings passed to the parse method + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function EJSONdeserialize(ejson: Document, options?: EJSONParseOptions): any { + options = options || {}; + return parse(JSON.stringify(ejson), options); +} + +/** @public */ +const EJSON: { + parse: typeof parse; + stringify: typeof stringify; + serialize: typeof EJSONserialize; + deserialize: typeof EJSONdeserialize; +} = Object.create(null); +EJSON.parse = parse; +EJSON.stringify = stringify; +EJSON.serialize = EJSONserialize; +EJSON.deserialize = EJSONdeserialize; +Object.freeze(EJSON); +export { EJSON }; diff --git a/gateway/node_modules/bson/src/index.ts b/gateway/node_modules/bson/src/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..5ef415756a33b0f21dd9a5cb3540358978502ad0 --- /dev/null +++ b/gateway/node_modules/bson/src/index.ts @@ -0,0 +1,19 @@ +import * as BSON from './bson'; + +// Export all named properties from BSON to support +// import { ObjectId, serialize } from 'bson'; +// const { ObjectId, serialize } = require('bson'); +export * from './bson'; + +// Export BSON as a namespace to support: +// import { BSON } from 'bson'; +// const { BSON } = require('bson'); +export { BSON }; + +// BSON does **NOT** have a default export + +// The following will crash in es module environments +// import BSON from 'bson'; + +// The following will work as expected, BSON as a namespace of all the APIs (BSON.ObjectId, BSON.serialize) +// const BSON = require('bson'); diff --git a/gateway/node_modules/bson/src/int_32.ts b/gateway/node_modules/bson/src/int_32.ts new file mode 100644 index 0000000000000000000000000000000000000000..7c95027ce534c3b96cfd6a0d226c686668d0ae18 --- /dev/null +++ b/gateway/node_modules/bson/src/int_32.ts @@ -0,0 +1,101 @@ +import { BSONValue } from './bson_value'; +import { BSON_INT32_MAX, BSON_INT32_MIN } from './constants'; +import { BSONError } from './error'; +import type { EJSONOptions } from './extended_json'; +import { type InspectFn, defaultInspect } from './parser/utils'; +import { removeLeadingZerosAndExplicitPlus } from './utils/string_utils'; + +/** @public */ +export interface Int32Extended { + $numberInt: string; +} + +/** + * A class representation of a BSON Int32 type. + * @public + * @category BSONType + */ +export class Int32 extends BSONValue { + get _bsontype(): 'Int32' { + return 'Int32'; + } + + value!: number; + /** + * Create an Int32 type + * + * @param value - the number we want to represent as an int32. + */ + constructor(value: number | string) { + super(); + if ((value as unknown) instanceof Number) { + value = value.valueOf(); + } + + this.value = +value | 0; + } + + /** + * Attempt to create an Int32 type from string. + * + * This method will throw a BSONError on any string input that is not representable as an Int32. + * Notably, this method will also throw on the following string formats: + * - Strings in non-decimal formats (exponent notation, binary, hex, or octal digits) + * - Strings non-numeric and non-leading sign characters (ex: '2.0', '24,000') + * - Strings with leading and/or trailing whitespace + * + * Strings with leading zeros, however, are allowed. + * + * @param value - the string we want to represent as an int32. + */ + static fromString(value: string): Int32 { + const cleanedValue = removeLeadingZerosAndExplicitPlus(value); + + const coercedValue = Number(value); + + if (BSON_INT32_MAX < coercedValue) { + throw new BSONError(`Input: '${value}' is larger than the maximum value for Int32`); + } else if (BSON_INT32_MIN > coercedValue) { + throw new BSONError(`Input: '${value}' is smaller than the minimum value for Int32`); + } else if (!Number.isSafeInteger(coercedValue)) { + throw new BSONError(`Input: '${value}' is not a safe integer`); + } else if (coercedValue.toString() !== cleanedValue) { + // catch all case + throw new BSONError(`Input: '${value}' is not a valid Int32 string`); + } + return new Int32(coercedValue); + } + + /** + * Access the number value. + * + * @returns returns the wrapped int32 number. + */ + valueOf(): number { + return this.value; + } + + toString(radix?: number): string { + return this.value.toString(radix); + } + + toJSON(): number { + return this.value; + } + + /** @internal */ + toExtendedJSON(options?: EJSONOptions): number | Int32Extended { + if (options && (options.relaxed || options.legacy)) return this.value; + return { $numberInt: this.value.toString() }; + } + + /** @internal */ + static fromExtendedJSON(doc: Int32Extended, options?: EJSONOptions): number | Int32 { + return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt); + } + + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + inspect ??= defaultInspect; + return `new Int32(${inspect(this.value, options)})`; + } +} diff --git a/gateway/node_modules/bson/src/long.ts b/gateway/node_modules/bson/src/long.ts new file mode 100644 index 0000000000000000000000000000000000000000..9a6cc339b9fbd92425d8d0645f212fbf13a79be2 --- /dev/null +++ b/gateway/node_modules/bson/src/long.ts @@ -0,0 +1,1240 @@ +import { BSONValue } from './bson_value'; +import { BSONError } from './error'; +import type { EJSONOptions } from './extended_json'; +import { type InspectFn, defaultInspect } from './parser/utils'; +import type { Timestamp } from './timestamp'; +import * as StringUtils from './utils/string_utils'; + +interface LongWASMHelpers { + /** Gets the high bits of the last operation performed */ + get_high(this: void): number; + div_u( + this: void, + lowBits: number, + highBits: number, + lowBitsDivisor: number, + highBitsDivisor: number + ): number; + div_s( + this: void, + lowBits: number, + highBits: number, + lowBitsDivisor: number, + highBitsDivisor: number + ): number; + rem_u( + this: void, + lowBits: number, + highBits: number, + lowBitsDivisor: number, + highBitsDivisor: number + ): number; + rem_s( + this: void, + lowBits: number, + highBits: number, + lowBitsDivisor: number, + highBitsDivisor: number + ): number; + mul( + this: void, + lowBits: number, + highBits: number, + lowBitsMultiplier: number, + highBitsMultiplier: number + ): number; +} + +/** + * wasm optimizations, to do native i64 multiplication and divide + */ +let wasm: LongWASMHelpers | undefined = undefined; + +/* We do not want to have to include DOM types just for this check */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +declare const WebAssembly: any; + +try { + wasm = new WebAssembly.Instance( + new WebAssembly.Module( + // prettier-ignore + new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11]) + ), + {} + ).exports as unknown as LongWASMHelpers; +} catch { + // no wasm support +} + +const TWO_PWR_16_DBL = 1 << 16; +const TWO_PWR_24_DBL = 1 << 24; +const TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL; +const TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL; +const TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2; + +/** A cache of the Long representations of small integer values. */ +const INT_CACHE: { [key: number]: Long } = {}; + +/** A cache of the Long representations of small unsigned integer values. */ +const UINT_CACHE: { [key: number]: Long } = {}; + +const MAX_INT64_STRING_LENGTH = 20; + +const DECIMAL_REG_EX = /^(\+?0|(\+|-)?[1-9][0-9]*)$/; + +/** @public */ +export interface LongExtended { + $numberLong: string; +} + +/** + * A class representing a 64-bit integer + * @public + * @category BSONType + * @remarks + * The internal representation of a long is the two given signed, 32-bit values. + * We use 32-bit pieces because these are the size of integers on which + * Javascript performs bit-operations. For operations like addition and + * multiplication, we split each number into 16 bit pieces, which can easily be + * multiplied within Javascript's floating-point representation without overflow + * or change in sign. + * In the algorithms below, we frequently reduce the negative case to the + * positive case by negating the input(s) and then post-processing the result. + * Note that we must ALWAYS check specially whether those values are MIN_VALUE + * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as + * a positive number, it overflows back into a negative). Not handling this + * case would often result in infinite recursion. + * Common constant values ZERO, ONE, NEG_ONE, etc. are found as static properties on this class. + */ +export class Long extends BSONValue { + get _bsontype(): 'Long' { + return 'Long'; + } + + /** An indicator used to reliably determine if an object is a Long or not. */ + get __isLong__(): boolean { + return true; + } + + /** + * The high 32 bits as a signed value. + */ + high: number; + + /** + * The low 32 bits as a signed value. + */ + low: number; + + /** + * Whether unsigned or not. + */ + unsigned: boolean; + + /** + * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers. + * + * @param low - The low (signed) 32 bits of the long + * @param high - The high (signed) 32 bits of the long + * @param unsigned - Whether unsigned or not, defaults to signed + */ + constructor(low: number, high?: number, unsigned?: boolean); + /** + * Constructs a 64 bit two's-complement integer, given a bigint representation. + * + * @param value - BigInt representation of the long value + * @param unsigned - Whether unsigned or not, defaults to signed + */ + constructor(value: bigint, unsigned?: boolean); + /** + * Constructs a 64 bit two's-complement integer, given a string representation. + * + * @param value - String representation of the long value + * @param unsigned - Whether unsigned or not, defaults to signed + */ + constructor(value: string, unsigned?: boolean); + constructor( + lowOrValue: number | bigint | string = 0, + highOrUnsigned?: number | boolean, + unsigned?: boolean + ) { + super(); + const unsignedBool = typeof highOrUnsigned === 'boolean' ? highOrUnsigned : Boolean(unsigned); + const high = typeof highOrUnsigned === 'number' ? highOrUnsigned : 0; + const res = + typeof lowOrValue === 'string' + ? Long.fromString(lowOrValue, unsignedBool) + : typeof lowOrValue === 'bigint' + ? Long.fromBigInt(lowOrValue, unsignedBool) + : { low: lowOrValue | 0, high: high | 0, unsigned: unsignedBool }; + this.low = res.low; + this.high = res.high; + this.unsigned = res.unsigned; + } + + static TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL); + + /** Maximum unsigned value. */ + static MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true); + /** Signed zero */ + static ZERO = Long.fromInt(0); + /** Unsigned zero. */ + static UZERO = Long.fromInt(0, true); + /** Signed one. */ + static ONE = Long.fromInt(1); + /** Unsigned one. */ + static UONE = Long.fromInt(1, true); + /** Signed negative one. */ + static NEG_ONE = Long.fromInt(-1); + /** Maximum signed value. */ + static MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false); + /** Minimum signed value. */ + static MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false); + + /** + * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. + * Each is assumed to use 32 bits. + * @param lowBits - The low 32 bits + * @param highBits - The high 32 bits + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromBits(lowBits: number, highBits: number, unsigned?: boolean): Long { + return new Long(lowBits, highBits, unsigned); + } + + /** + * Returns a Long representing the given 32 bit integer value. + * @param value - The 32 bit integer in question + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromInt(value: number, unsigned?: boolean): Long { + let obj, cachedObj, cache; + if (unsigned) { + value >>>= 0; + if ((cache = 0 <= value && value < 256)) { + cachedObj = UINT_CACHE[value]; + if (cachedObj) return cachedObj; + } + obj = Long.fromBits(value, (value | 0) < 0 ? -1 : 0, true); + if (cache) UINT_CACHE[value] = obj; + return obj; + } else { + value |= 0; + if ((cache = -128 <= value && value < 128)) { + cachedObj = INT_CACHE[value]; + if (cachedObj) return cachedObj; + } + obj = Long.fromBits(value, value < 0 ? -1 : 0, false); + if (cache) INT_CACHE[value] = obj; + return obj; + } + } + + /** + * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. + * @param value - The number in question + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromNumber(value: number, unsigned?: boolean): Long { + if (isNaN(value)) return unsigned ? Long.UZERO : Long.ZERO; + if (unsigned) { + if (value < 0) return Long.UZERO; + if (value >= TWO_PWR_64_DBL) return Long.MAX_UNSIGNED_VALUE; + } else { + if (value <= -TWO_PWR_63_DBL) return Long.MIN_VALUE; + if (value + 1 >= TWO_PWR_63_DBL) return Long.MAX_VALUE; + } + if (value < 0) return Long.fromNumber(-value, unsigned).neg(); + return Long.fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned); + } + + /** + * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. + * @param value - The number in question + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromBigInt(value: bigint, unsigned?: boolean): Long { + const FROM_BIGINT_BIT_MASK = 0xffffffffn; + const FROM_BIGINT_BIT_SHIFT = 32n; + return new Long( + Number(value & FROM_BIGINT_BIT_MASK), + Number((value >> FROM_BIGINT_BIT_SHIFT) & FROM_BIGINT_BIT_MASK), + unsigned + ); + } + + /** + * @internal + * Returns a Long representation of the given string, written using the specified radix. + * Throws an error if `throwsError` is set to true and any of the following conditions are true: + * - the string contains invalid characters for the given radix + * - the string contains whitespace + * @param str - The textual representation of the Long + * @param unsigned - Whether unsigned or not, defaults to signed + * @param radix - The radix in which the text is written (2-36), defaults to 10 + * @returns The corresponding Long value + */ + private static _fromString(str: string, unsigned: boolean, radix: number): Long { + if (str.length === 0) throw new BSONError('empty string'); + if (radix < 2 || 36 < radix) throw new BSONError('radix'); + + let p; + if ((p = str.indexOf('-')) > 0) throw new BSONError('interior hyphen'); + else if (p === 0) { + return Long._fromString(str.substring(1), unsigned, radix).neg(); + } + + // Do several (8) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + const radixToPower = Long.fromNumber(Math.pow(radix, 8)); + + let result = Long.ZERO; + for (let i = 0; i < str.length; i += 8) { + const size = Math.min(8, str.length - i), + value = parseInt(str.substring(i, i + size), radix); + if (size < 8) { + const power = Long.fromNumber(Math.pow(radix, size)); + result = result.mul(power).add(Long.fromNumber(value)); + } else { + result = result.mul(radixToPower); + result = result.add(Long.fromNumber(value)); + } + } + result.unsigned = unsigned; + return result; + } + + /** + * Returns a signed Long representation of the given string, written using radix 10. + * Will throw an error if the given text is not exactly representable as a Long. + * Throws an error if any of the following conditions are true: + * - the string contains invalid characters for the radix 10 + * - the string contains whitespace + * - the value the string represents is too large or too small to be a Long + * Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero + * @param str - The textual representation of the Long + * @returns The corresponding Long value + */ + static fromStringStrict(str: string): Long; + /** + * Returns a Long representation of the given string, written using the radix 10. + * Will throw an error if the given parameters are not exactly representable as a Long. + * Throws an error if any of the following conditions are true: + * - the string contains invalid characters for the given radix + * - the string contains whitespace + * - the value the string represents is too large or too small to be a Long + * Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero + * @param str - The textual representation of the Long + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromStringStrict(str: string, unsigned?: boolean): Long; + /** + * Returns a signed Long representation of the given string, written using the specified radix. + * Will throw an error if the given parameters are not exactly representable as a Long. + * Throws an error if any of the following conditions are true: + * - the string contains invalid characters for the given radix + * - the string contains whitespace + * - the value the string represents is too large or too small to be a Long + * Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero + * @param str - The textual representation of the Long + * @param radix - The radix in which the text is written (2-36), defaults to 10 + * @returns The corresponding Long value + */ + static fromStringStrict(str: string, radix?: boolean): Long; + /** + * Returns a Long representation of the given string, written using the specified radix. + * Will throw an error if the given parameters are not exactly representable as a Long. + * Throws an error if any of the following conditions are true: + * - the string contains invalid characters for the given radix + * - the string contains whitespace + * - the value the string represents is too large or too small to be a Long + * Unlike Long.fromString, this method does not coerce '+/-Infinity' and 'NaN' to Long.Zero + * @param str - The textual representation of the Long + * @param unsigned - Whether unsigned or not, defaults to signed + * @param radix - The radix in which the text is written (2-36), defaults to 10 + * @returns The corresponding Long value + */ + static fromStringStrict(str: string, unsigned?: boolean, radix?: number): Long; + static fromStringStrict(str: string, unsignedOrRadix?: boolean | number, radix?: number): Long { + let unsigned = false; + if (typeof unsignedOrRadix === 'number') { + // For goog.math.long compatibility + ((radix = unsignedOrRadix), (unsignedOrRadix = false)); + } else { + unsigned = !!unsignedOrRadix; + } + radix ??= 10; + + if (str.trim() !== str) { + throw new BSONError(`Input: '${str}' contains leading and/or trailing whitespace`); + } + if (!StringUtils.validateStringCharacters(str, radix)) { + throw new BSONError(`Input: '${str}' contains invalid characters for radix: ${radix}`); + } + + // remove leading zeros (for later string comparison and to make math faster) + const cleanedStr = StringUtils.removeLeadingZerosAndExplicitPlus(str); + + // check roundtrip result + const result = Long._fromString(cleanedStr, unsigned, radix); + if (result.toString(radix).toLowerCase() !== cleanedStr.toLowerCase()) { + throw new BSONError( + `Input: ${str} is not representable as ${result.unsigned ? 'an unsigned' : 'a signed'} 64-bit Long ${radix != null ? `with radix: ${radix}` : ''}` + ); + } + return result; + } + + /** + * Returns a signed Long representation of the given string, written using radix 10. + * + * If the input string is empty, this function will throw a BSONError. + * + * If input string does not have valid signed 64-bit Long representation, this method will return a coerced value: + * - inputs that overflow 64-bit signed long will be coerced to Long.MAX_VALUE and Long.MIN_VALUE respectively + * - 'NaN' or '+/-Infinity' are coerced to Long.ZERO + * - other invalid characters sequences have variable behavior + * + * @param str - The textual representation of the Long + * @returns The corresponding Long value + */ + static fromString(str: string): Long; + /** + * Returns a signed Long representation of the given string, written using the provided radix. + * + * If the input string is empty or a provided radix is not within (2-36), this function will throw a BSONError. + * + * If input parameters do not have valid signed 64-bit Long representation, this method will return a coerced value: + * - inputs that overflow 64-bit signed long will be coerced to Long.MAX_VALUE and Long.MIN_VALUE respectively + * - if the radix is less than 24, 'NaN' is coerced to Long.ZERO + * - if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO + * - other invalid characters sequences have variable behavior + * @param str - The textual representation of the Long + * @param radix - The radix in which the text is written (2-36), defaults to 10 + * @returns The corresponding Long value + */ + static fromString(str: string, radix?: number): Long; + /** + * Returns a Long representation of the given string, written using radix 10. + * + * If the input string is empty, this function will throw a BSONError. + * + * If input parameters do not have a valid 64-bit Long representation, this method will return a coerced value: + * - inputs that overflow 64-bit long will be coerced to max or min (if signed) values + * - if the radix is less than 24, 'NaN' is coerced to Long.ZERO + * - if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO + * - other invalid characters sequences have variable behavior + * @param str - The textual representation of the Long + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromString(str: string, unsigned?: boolean): Long; + /** + * Returns a Long representation of the given string, written using the specified radix. + * + * If the input string is empty or a provided radix is not within (2-36), this function will throw a BSONError. + * + * If input parameters do not have a valid 64-bit Long representation, this method will return a coerced value: + * - inputs that overflow 64-bit long will be coerced to max or min (if signed) values + * - if the radix is less than 24, 'NaN' is coerced to Long.ZERO + * - if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO + * - other invalid characters sequences have variable behavior + * @param str - The textual representation of the Long + * @param unsigned - Whether unsigned or not, defaults to signed + * @param radix - The radix in which the text is written (2-36), defaults to 10 + * @returns The corresponding Long value + */ + static fromString(str: string, unsigned?: boolean, radix?: number): Long; + static fromString(str: string, unsignedOrRadix?: boolean | number, radix?: number): Long { + let unsigned = false; + if (typeof unsignedOrRadix === 'number') { + // For goog.math.long compatibility + ((radix = unsignedOrRadix), (unsignedOrRadix = false)); + } else { + unsigned = !!unsignedOrRadix; + } + radix ??= 10; + if (str === 'NaN' && radix < 24) { + // radix does not support n, so coerce to zero + return Long.ZERO; + } else if ((str === 'Infinity' || str === '+Infinity' || str === '-Infinity') && radix < 35) { + // radix does not support y, so coerce to zero + return Long.ZERO; + } + return Long._fromString(str, unsigned, radix); + } + + /** + * Creates a Long from its byte representation. + * @param bytes - Byte representation + * @param unsigned - Whether unsigned or not, defaults to signed + * @param le - Whether little or big endian, defaults to big endian + * @returns The corresponding Long value + */ + static fromBytes(bytes: number[], unsigned?: boolean, le?: boolean): Long { + return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned); + } + + /** + * Creates a Long from its little endian byte representation. + * @param bytes - Little endian byte representation + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromBytesLE(bytes: number[], unsigned?: boolean): Long { + return new Long( + bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24), + bytes[4] | (bytes[5] << 8) | (bytes[6] << 16) | (bytes[7] << 24), + unsigned + ); + } + + /** + * Creates a Long from its big endian byte representation. + * @param bytes - Big endian byte representation + * @param unsigned - Whether unsigned or not, defaults to signed + * @returns The corresponding Long value + */ + static fromBytesBE(bytes: number[], unsigned?: boolean): Long { + return new Long( + (bytes[4] << 24) | (bytes[5] << 16) | (bytes[6] << 8) | bytes[7], + (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3], + unsigned + ); + } + + /** + * Tests if the specified object is a Long. + */ + static isLong(value: unknown): value is Long { + return ( + value != null && + typeof value === 'object' && + '__isLong__' in value && + value.__isLong__ === true + ); + } + + /** + * Converts the specified value to a Long. + * @param unsigned - Whether unsigned or not, defaults to signed + */ + static fromValue( + val: number | string | { low: number; high: number; unsigned?: boolean }, + unsigned?: boolean + ): Long { + if (typeof val === 'number') return Long.fromNumber(val, unsigned); + if (typeof val === 'string') return Long.fromString(val, unsigned); + // Throws for non-objects, converts non-instanceof Long: + return Long.fromBits( + val.low, + val.high, + typeof unsigned === 'boolean' ? unsigned : val.unsigned + ); + } + + /** Returns the sum of this and the specified Long. */ + add(addend: string | number | Long | Timestamp): Long { + if (!Long.isLong(addend)) addend = Long.fromValue(addend); + + // Divide each number into 4 chunks of 16 bits, and then sum the chunks. + + const a48 = this.high >>> 16; + const a32 = this.high & 0xffff; + const a16 = this.low >>> 16; + const a00 = this.low & 0xffff; + + const b48 = addend.high >>> 16; + const b32 = addend.high & 0xffff; + const b16 = addend.low >>> 16; + const b00 = addend.low & 0xffff; + + let c48 = 0, + c32 = 0, + c16 = 0, + c00 = 0; + c00 += a00 + b00; + c16 += c00 >>> 16; + c00 &= 0xffff; + c16 += a16 + b16; + c32 += c16 >>> 16; + c16 &= 0xffff; + c32 += a32 + b32; + c48 += c32 >>> 16; + c32 &= 0xffff; + c48 += a48 + b48; + c48 &= 0xffff; + return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); + } + + /** + * Returns the sum of this and the specified Long. + * @returns Sum + */ + and(other: string | number | Long | Timestamp): Long { + if (!Long.isLong(other)) other = Long.fromValue(other); + return Long.fromBits(this.low & other.low, this.high & other.high, this.unsigned); + } + + /** + * Compares this Long's value with the specified's. + * @returns 0 if they are the same, 1 if the this is greater and -1 if the given one is greater + */ + compare(other: string | number | Long | Timestamp): 0 | 1 | -1 { + if (!Long.isLong(other)) other = Long.fromValue(other); + if (this.eq(other)) return 0; + const thisNeg = this.isNegative(), + otherNeg = other.isNegative(); + if (thisNeg && !otherNeg) return -1; + if (!thisNeg && otherNeg) return 1; + // At this point the sign bits are the same + if (!this.unsigned) return this.sub(other).isNegative() ? -1 : 1; + // Both are positive if at least one is unsigned + return other.high >>> 0 > this.high >>> 0 || + (other.high === this.high && other.low >>> 0 > this.low >>> 0) + ? -1 + : 1; + } + + /** This is an alias of {@link Long.compare} */ + comp(other: string | number | Long | Timestamp): 0 | 1 | -1 { + return this.compare(other); + } + + /** + * Returns this Long divided by the specified. The result is signed if this Long is signed or unsigned if this Long is unsigned. + * @returns Quotient + */ + divide(divisor: string | number | Long | Timestamp): Long { + if (!Long.isLong(divisor)) divisor = Long.fromValue(divisor); + if (divisor.isZero()) throw new BSONError('division by zero'); + + // use wasm support if present + if (wasm) { + // guard against signed division overflow: the largest + // negative number / -1 would be 1 larger than the largest + // positive number, due to two's complement. + if ( + !this.unsigned && + this.high === -0x80000000 && + divisor.low === -1 && + divisor.high === -1 + ) { + // be consistent with non-wasm code path + return this; + } + const low = (this.unsigned ? wasm.div_u : wasm.div_s)( + this.low, + this.high, + divisor.low, + divisor.high + ); + return Long.fromBits(low, wasm.get_high(), this.unsigned); + } + + if (this.isZero()) return this.unsigned ? Long.UZERO : Long.ZERO; + let approx, rem, res; + if (!this.unsigned) { + // This section is only relevant for signed longs and is derived from the + // closure library as a whole. + if (this.eq(Long.MIN_VALUE)) { + if (divisor.eq(Long.ONE) || divisor.eq(Long.NEG_ONE)) return Long.MIN_VALUE; + // recall that -MIN_VALUE == MIN_VALUE + else if (divisor.eq(Long.MIN_VALUE)) return Long.ONE; + else { + // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. + const halfThis = this.shr(1); + approx = halfThis.div(divisor).shl(1); + if (approx.eq(Long.ZERO)) { + return divisor.isNegative() ? Long.ONE : Long.NEG_ONE; + } else { + rem = this.sub(divisor.mul(approx)); + res = approx.add(rem.div(divisor)); + return res; + } + } + } else if (divisor.eq(Long.MIN_VALUE)) return this.unsigned ? Long.UZERO : Long.ZERO; + if (this.isNegative()) { + if (divisor.isNegative()) return this.neg().div(divisor.neg()); + return this.neg().div(divisor).neg(); + } else if (divisor.isNegative()) return this.div(divisor.neg()).neg(); + res = Long.ZERO; + } else { + // The algorithm below has not been made for unsigned longs. It's therefore + // required to take special care of the MSB prior to running it. + if (!divisor.unsigned) divisor = divisor.toUnsigned(); + if (divisor.gt(this)) return Long.UZERO; + if (divisor.gt(this.shru(1))) + // 15 >>> 1 = 7 ; with divisor = 8 ; true + return Long.UONE; + res = Long.UZERO; + } + + // Repeat the following until the remainder is less than other: find a + // floating-point that approximates remainder / other *from below*, add this + // into the result, and subtract it from the remainder. It is critical that + // the approximate value is less than or equal to the real value so that the + // remainder never becomes negative. + // eslint-disable-next-line @typescript-eslint/no-this-alias + rem = this; + while (rem.gte(divisor)) { + // Approximate the result of division. This may be a little greater or + // smaller than the actual value. + approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber())); + + // We will tweak the approximate result by changing it in the 48-th digit or + // the smallest non-fractional digit, whichever is larger. + const log2 = Math.ceil(Math.log(approx) / Math.LN2); + const delta = log2 <= 48 ? 1 : Math.pow(2, log2 - 48); + // Decrease the approximation until it is smaller than the remainder. Note + // that if it is too large, the product overflows and is negative. + let approxRes = Long.fromNumber(approx); + let approxRem = approxRes.mul(divisor); + while (approxRem.isNegative() || approxRem.gt(rem)) { + approx -= delta; + approxRes = Long.fromNumber(approx, this.unsigned); + approxRem = approxRes.mul(divisor); + } + + // We know the answer can't be zero... and actually, zero would cause + // infinite recursion since we would make no progress. + if (approxRes.isZero()) approxRes = Long.ONE; + + res = res.add(approxRes); + rem = rem.sub(approxRem); + } + return res; + } + + /**This is an alias of {@link Long.divide} */ + div(divisor: string | number | Long | Timestamp): Long { + return this.divide(divisor); + } + + /** + * Tests if this Long's value equals the specified's. + * @param other - Other value + */ + equals(other: string | number | Long | Timestamp): boolean { + if (!Long.isLong(other)) other = Long.fromValue(other); + if (this.unsigned !== other.unsigned && this.high >>> 31 === 1 && other.high >>> 31 === 1) + return false; + return this.high === other.high && this.low === other.low; + } + + /** This is an alias of {@link Long.equals} */ + eq(other: string | number | Long | Timestamp): boolean { + return this.equals(other); + } + + /** Gets the high 32 bits as a signed integer. */ + getHighBits(): number { + return this.high; + } + + /** Gets the high 32 bits as an unsigned integer. */ + getHighBitsUnsigned(): number { + return this.high >>> 0; + } + + /** Gets the low 32 bits as a signed integer. */ + getLowBits(): number { + return this.low; + } + + /** Gets the low 32 bits as an unsigned integer. */ + getLowBitsUnsigned(): number { + return this.low >>> 0; + } + + /** Gets the number of bits needed to represent the absolute value of this Long. */ + getNumBitsAbs(): number { + if (this.isNegative()) { + // Unsigned Longs are never negative + return this.eq(Long.MIN_VALUE) ? 64 : this.neg().getNumBitsAbs(); + } + const val = this.high !== 0 ? this.high : this.low; + let bit: number; + for (bit = 31; bit > 0; bit--) if ((val & (1 << bit)) !== 0) break; + return this.high !== 0 ? bit + 33 : bit + 1; + } + + /** Tests if this Long's value is greater than the specified's. */ + greaterThan(other: string | number | Long | Timestamp): boolean { + return this.comp(other) > 0; + } + + /** This is an alias of {@link Long.greaterThan} */ + gt(other: string | number | Long | Timestamp): boolean { + return this.greaterThan(other); + } + + /** Tests if this Long's value is greater than or equal the specified's. */ + greaterThanOrEqual(other: string | number | Long | Timestamp): boolean { + return this.comp(other) >= 0; + } + + /** This is an alias of {@link Long.greaterThanOrEqual} */ + gte(other: string | number | Long | Timestamp): boolean { + return this.greaterThanOrEqual(other); + } + /** This is an alias of {@link Long.greaterThanOrEqual} */ + ge(other: string | number | Long | Timestamp): boolean { + return this.greaterThanOrEqual(other); + } + + /** Tests if this Long's value is even. */ + isEven(): boolean { + return (this.low & 1) === 0; + } + + /** Tests if this Long's value is negative. */ + isNegative(): boolean { + return !this.unsigned && this.high < 0; + } + + /** Tests if this Long's value is odd. */ + isOdd(): boolean { + return (this.low & 1) === 1; + } + + /** Tests if this Long's value is positive. */ + isPositive(): boolean { + return this.unsigned || this.high >= 0; + } + + /** Tests if this Long's value equals zero. */ + isZero(): boolean { + return this.high === 0 && this.low === 0; + } + + /** Tests if this Long's value is less than the specified's. */ + lessThan(other: string | number | Long | Timestamp): boolean { + return this.comp(other) < 0; + } + + /** This is an alias of {@link Long#lessThan}. */ + lt(other: string | number | Long | Timestamp): boolean { + return this.lessThan(other); + } + + /** Tests if this Long's value is less than or equal the specified's. */ + lessThanOrEqual(other: string | number | Long | Timestamp): boolean { + return this.comp(other) <= 0; + } + + /** This is an alias of {@link Long.lessThanOrEqual} */ + lte(other: string | number | Long | Timestamp): boolean { + return this.lessThanOrEqual(other); + } + + /** Returns this Long modulo the specified. */ + modulo(divisor: string | number | Long | Timestamp): Long { + if (!Long.isLong(divisor)) divisor = Long.fromValue(divisor); + + // use wasm support if present + if (wasm) { + const low = (this.unsigned ? wasm.rem_u : wasm.rem_s)( + this.low, + this.high, + divisor.low, + divisor.high + ); + return Long.fromBits(low, wasm.get_high(), this.unsigned); + } + + return this.sub(this.div(divisor).mul(divisor)); + } + + /** This is an alias of {@link Long.modulo} */ + mod(divisor: string | number | Long | Timestamp): Long { + return this.modulo(divisor); + } + /** This is an alias of {@link Long.modulo} */ + rem(divisor: string | number | Long | Timestamp): Long { + return this.modulo(divisor); + } + + /** + * Returns the product of this and the specified Long. + * @param multiplier - Multiplier + * @returns Product + */ + multiply(multiplier: string | number | Long | Timestamp): Long { + if (this.isZero()) return Long.ZERO; + if (!Long.isLong(multiplier)) multiplier = Long.fromValue(multiplier); + + // use wasm support if present + if (wasm) { + const low = wasm.mul(this.low, this.high, multiplier.low, multiplier.high); + return Long.fromBits(low, wasm.get_high(), this.unsigned); + } + + if (multiplier.isZero()) return Long.ZERO; + if (this.eq(Long.MIN_VALUE)) return multiplier.isOdd() ? Long.MIN_VALUE : Long.ZERO; + if (multiplier.eq(Long.MIN_VALUE)) return this.isOdd() ? Long.MIN_VALUE : Long.ZERO; + + if (this.isNegative()) { + if (multiplier.isNegative()) return this.neg().mul(multiplier.neg()); + else return this.neg().mul(multiplier).neg(); + } else if (multiplier.isNegative()) return this.mul(multiplier.neg()).neg(); + + // If both longs are small, use float multiplication + if (this.lt(Long.TWO_PWR_24) && multiplier.lt(Long.TWO_PWR_24)) + return Long.fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned); + + // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products. + // We can skip products that would overflow. + + const a48 = this.high >>> 16; + const a32 = this.high & 0xffff; + const a16 = this.low >>> 16; + const a00 = this.low & 0xffff; + + const b48 = multiplier.high >>> 16; + const b32 = multiplier.high & 0xffff; + const b16 = multiplier.low >>> 16; + const b00 = multiplier.low & 0xffff; + + let c48 = 0, + c32 = 0, + c16 = 0, + c00 = 0; + c00 += a00 * b00; + c16 += c00 >>> 16; + c00 &= 0xffff; + c16 += a16 * b00; + c32 += c16 >>> 16; + c16 &= 0xffff; + c16 += a00 * b16; + c32 += c16 >>> 16; + c16 &= 0xffff; + c32 += a32 * b00; + c48 += c32 >>> 16; + c32 &= 0xffff; + c32 += a16 * b16; + c48 += c32 >>> 16; + c32 &= 0xffff; + c32 += a00 * b32; + c48 += c32 >>> 16; + c32 &= 0xffff; + c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; + c48 &= 0xffff; + return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); + } + + /** This is an alias of {@link Long.multiply} */ + mul(multiplier: string | number | Long | Timestamp): Long { + return this.multiply(multiplier); + } + + /** Returns the Negation of this Long's value. */ + negate(): Long { + if (!this.unsigned && this.eq(Long.MIN_VALUE)) return Long.MIN_VALUE; + return this.not().add(Long.ONE); + } + + /** This is an alias of {@link Long.negate} */ + neg(): Long { + return this.negate(); + } + + /** Returns the bitwise NOT of this Long. */ + not(): Long { + return Long.fromBits(~this.low, ~this.high, this.unsigned); + } + + /** Tests if this Long's value differs from the specified's. */ + notEquals(other: string | number | Long | Timestamp): boolean { + return !this.equals(other); + } + + /** This is an alias of {@link Long.notEquals} */ + neq(other: string | number | Long | Timestamp): boolean { + return this.notEquals(other); + } + /** This is an alias of {@link Long.notEquals} */ + ne(other: string | number | Long | Timestamp): boolean { + return this.notEquals(other); + } + + /** + * Returns the bitwise OR of this Long and the specified. + */ + or(other: number | string | Long): Long { + if (!Long.isLong(other)) other = Long.fromValue(other); + return Long.fromBits(this.low | other.low, this.high | other.high, this.unsigned); + } + + /** + * Returns this Long with bits shifted to the left by the given amount. + * @param numBits - Number of bits + * @returns Shifted Long + */ + shiftLeft(numBits: number | Long): Long { + if (Long.isLong(numBits)) numBits = numBits.toInt(); + if ((numBits &= 63) === 0) return this; + else if (numBits < 32) + return Long.fromBits( + this.low << numBits, + (this.high << numBits) | (this.low >>> (32 - numBits)), + this.unsigned + ); + else return Long.fromBits(0, this.low << (numBits - 32), this.unsigned); + } + + /** This is an alias of {@link Long.shiftLeft} */ + shl(numBits: number | Long): Long { + return this.shiftLeft(numBits); + } + + /** + * Returns this Long with bits arithmetically shifted to the right by the given amount. + * @param numBits - Number of bits + * @returns Shifted Long + */ + shiftRight(numBits: number | Long): Long { + if (Long.isLong(numBits)) numBits = numBits.toInt(); + if ((numBits &= 63) === 0) return this; + else if (numBits < 32) + return Long.fromBits( + (this.low >>> numBits) | (this.high << (32 - numBits)), + this.high >> numBits, + this.unsigned + ); + else return Long.fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned); + } + + /** This is an alias of {@link Long.shiftRight} */ + shr(numBits: number | Long): Long { + return this.shiftRight(numBits); + } + + /** + * Returns this Long with bits logically shifted to the right by the given amount. + * @param numBits - Number of bits + * @returns Shifted Long + */ + shiftRightUnsigned(numBits: Long | number): Long { + if (Long.isLong(numBits)) numBits = numBits.toInt(); + numBits &= 63; + if (numBits === 0) return this; + else { + const high = this.high; + if (numBits < 32) { + const low = this.low; + return Long.fromBits( + (low >>> numBits) | (high << (32 - numBits)), + high >>> numBits, + this.unsigned + ); + } else if (numBits === 32) return Long.fromBits(high, 0, this.unsigned); + else return Long.fromBits(high >>> (numBits - 32), 0, this.unsigned); + } + } + + /** This is an alias of {@link Long.shiftRightUnsigned} */ + shr_u(numBits: number | Long): Long { + return this.shiftRightUnsigned(numBits); + } + /** This is an alias of {@link Long.shiftRightUnsigned} */ + shru(numBits: number | Long): Long { + return this.shiftRightUnsigned(numBits); + } + + /** + * Returns the difference of this and the specified Long. + * @param subtrahend - Subtrahend + * @returns Difference + */ + subtract(subtrahend: string | number | Long | Timestamp): Long { + if (!Long.isLong(subtrahend)) subtrahend = Long.fromValue(subtrahend); + return this.add(subtrahend.neg()); + } + + /** This is an alias of {@link Long.subtract} */ + sub(subtrahend: string | number | Long | Timestamp): Long { + return this.subtract(subtrahend); + } + + /** Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. */ + toInt(): number { + return this.unsigned ? this.low >>> 0 : this.low; + } + + /** Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). */ + toNumber(): number { + if (this.unsigned) return (this.high >>> 0) * TWO_PWR_32_DBL + (this.low >>> 0); + return this.high * TWO_PWR_32_DBL + (this.low >>> 0); + } + + /** Converts the Long to a BigInt (arbitrary precision). */ + toBigInt(): bigint { + return BigInt(this.toString()); + } + + /** + * Converts this Long to its byte representation. + * @param le - Whether little or big endian, defaults to big endian + * @returns Byte representation + */ + toBytes(le?: boolean): number[] { + return le ? this.toBytesLE() : this.toBytesBE(); + } + + /** + * Converts this Long to its little endian byte representation. + * @returns Little endian byte representation + */ + toBytesLE(): number[] { + const hi = this.high, + lo = this.low; + return [ + lo & 0xff, + (lo >>> 8) & 0xff, + (lo >>> 16) & 0xff, + lo >>> 24, + hi & 0xff, + (hi >>> 8) & 0xff, + (hi >>> 16) & 0xff, + hi >>> 24 + ]; + } + + /** + * Converts this Long to its big endian byte representation. + * @returns Big endian byte representation + */ + toBytesBE(): number[] { + const hi = this.high, + lo = this.low; + return [ + hi >>> 24, + (hi >>> 16) & 0xff, + (hi >>> 8) & 0xff, + hi & 0xff, + lo >>> 24, + (lo >>> 16) & 0xff, + (lo >>> 8) & 0xff, + lo & 0xff + ]; + } + + /** + * Converts this Long to signed. + */ + toSigned(): Long { + if (!this.unsigned) return this; + return Long.fromBits(this.low, this.high, false); + } + + /** + * Converts the Long to a string written in the specified radix. + * @param radix - Radix (2-36), defaults to 10 + * @throws RangeError If `radix` is out of range + */ + toString(radix?: number): string { + radix = radix || 10; + if (radix < 2 || 36 < radix) throw new BSONError('radix'); + if (this.isZero()) return '0'; + if (this.isNegative()) { + // Unsigned Longs are never negative + if (this.eq(Long.MIN_VALUE)) { + // We need to change the Long value before it can be negated, so we remove + // the bottom-most digit in this base and then recurse to do the rest. + const radixLong = Long.fromNumber(radix), + div = this.div(radixLong), + rem1 = div.mul(radixLong).sub(this); + return div.toString(radix) + rem1.toInt().toString(radix); + } else return '-' + this.neg().toString(radix); + } + + // Do several (6) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + const radixToPower = Long.fromNumber(Math.pow(radix, 6), this.unsigned); + // eslint-disable-next-line @typescript-eslint/no-this-alias + let rem: Long = this; + let result = ''; + while (true) { + const remDiv = rem.div(radixToPower); + const intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0; + let digits = intval.toString(radix); + rem = remDiv; + if (rem.isZero()) { + return digits + result; + } else { + while (digits.length < 6) digits = '0' + digits; + result = '' + digits + result; + } + } + } + + /** Converts this Long to unsigned. */ + toUnsigned(): Long { + if (this.unsigned) return this; + return Long.fromBits(this.low, this.high, true); + } + + /** Returns the bitwise XOR of this Long and the given one. */ + xor(other: Long | number | string): Long { + if (!Long.isLong(other)) other = Long.fromValue(other); + return Long.fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned); + } + + /** This is an alias of {@link Long.isZero} */ + eqz(): boolean { + return this.isZero(); + } + + /** This is an alias of {@link Long.lessThanOrEqual} */ + le(other: string | number | Long | Timestamp): boolean { + return this.lessThanOrEqual(other); + } + + /* + **************************************************************** + * BSON SPECIFIC ADDITIONS * + **************************************************************** + */ + toExtendedJSON(options?: EJSONOptions): number | LongExtended { + if (options && options.relaxed) return this.toNumber(); + return { $numberLong: this.toString() }; + } + static fromExtendedJSON( + doc: { $numberLong: string }, + options?: EJSONOptions + ): number | Long | bigint { + const { useBigInt64 = false, relaxed = true } = { ...options }; + + if (doc.$numberLong.length > MAX_INT64_STRING_LENGTH) { + throw new BSONError('$numberLong string is too long'); + } + + if (!DECIMAL_REG_EX.test(doc.$numberLong)) { + throw new BSONError(`$numberLong string "${doc.$numberLong}" is in an invalid format`); + } + + if (useBigInt64) { + const bigIntResult = BigInt(doc.$numberLong); + return BigInt.asIntN(64, bigIntResult); + } + + const longResult = Long.fromString(doc.$numberLong); + if (relaxed) { + return longResult.toNumber(); + } + return longResult; + } + + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + inspect ??= defaultInspect; + const longVal = inspect(this.toString(), options); + const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : ''; + return `new Long(${longVal}${unsignedVal})`; + } +} diff --git a/gateway/node_modules/bson/src/max_key.ts b/gateway/node_modules/bson/src/max_key.ts new file mode 100644 index 0000000000000000000000000000000000000000..903f1d1656e46e959f6b7bc2def290272b780f7d --- /dev/null +++ b/gateway/node_modules/bson/src/max_key.ts @@ -0,0 +1,31 @@ +import { BSONValue } from './bson_value'; + +/** @public */ +export interface MaxKeyExtended { + $maxKey: 1; +} + +/** + * A class representation of the BSON MaxKey type. + * @public + * @category BSONType + */ +export class MaxKey extends BSONValue { + get _bsontype(): 'MaxKey' { + return 'MaxKey'; + } + + /** @internal */ + toExtendedJSON(): MaxKeyExtended { + return { $maxKey: 1 }; + } + + /** @internal */ + static fromExtendedJSON(): MaxKey { + return new MaxKey(); + } + + inspect(): string { + return 'new MaxKey()'; + } +} diff --git a/gateway/node_modules/bson/src/min_key.ts b/gateway/node_modules/bson/src/min_key.ts new file mode 100644 index 0000000000000000000000000000000000000000..244e645ab50baf1fb55305d995206e1a055742e7 --- /dev/null +++ b/gateway/node_modules/bson/src/min_key.ts @@ -0,0 +1,31 @@ +import { BSONValue } from './bson_value'; + +/** @public */ +export interface MinKeyExtended { + $minKey: 1; +} + +/** + * A class representation of the BSON MinKey type. + * @public + * @category BSONType + */ +export class MinKey extends BSONValue { + get _bsontype(): 'MinKey' { + return 'MinKey'; + } + + /** @internal */ + toExtendedJSON(): MinKeyExtended { + return { $minKey: 1 }; + } + + /** @internal */ + static fromExtendedJSON(): MinKey { + return new MinKey(); + } + + inspect(): string { + return 'new MinKey()'; + } +} diff --git a/gateway/node_modules/bson/src/objectid.ts b/gateway/node_modules/bson/src/objectid.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a3cd140841e24c523f33ad48f321a8029ca2c79 --- /dev/null +++ b/gateway/node_modules/bson/src/objectid.ts @@ -0,0 +1,546 @@ +import { BSONValue } from './bson_value'; +import { BSONError } from './error'; +import { type InspectFn, defaultInspect } from './parser/utils'; +import { ByteUtils } from './utils/byte_utils'; +import { NumberUtils } from './utils/number_utils'; + +/** ObjectId hexString cache @internal */ +const __idCache = new WeakMap(); // TODO(NODE-6549): convert this to #__id private field when target updated to ES2022 + +/** byte (0-255): its 2-character lowercase hex pair @internal */ +const byteToHex: string[] = []; +for (let n = 0; n < 256; n++) byteToHex.push(n.toString(16).padStart(2, '0')); + +/** hex char code: nibble (0-15); indices are the codes of 0-9, a-f, A-F @internal */ +const hexCharCodeToNibble = new Int8Array(103); +for (let c = 48; c <= 57; c++) hexCharCodeToNibble[c] = c - 48; // '0'-'9' +for (let c = 65; c <= 70; c++) hexCharCodeToNibble[c] = c - 55; // 'A'-'F' +for (let c = 97; c <= 102; c++) hexCharCodeToNibble[c] = c - 87; // 'a'-'f' + +/** @public */ +export interface ObjectIdLike { + id: string | Uint8Array; + __id?: string; + toHexString(): string; +} + +/** @public */ +export interface ObjectIdExtended { + $oid: string; +} + +/** + * A class representation of the BSON ObjectId type. + * @public + * @category BSONType + */ +export class ObjectId extends BSONValue { + get _bsontype(): 'ObjectId' { + return 'ObjectId'; + } + + /** @internal */ + private static index = 0; + + /** Unique sequence for the current process (initialized on first use) + * @internal + */ + private static PROCESS_UNIQUE: Uint8Array | null = null; + + /** @internal */ + private static resetState = (): void => { + this.index = Math.floor(Math.random() * 0x1000000); + // PROCESS_UNIQUE is (re)generated lazily on first use rather than here, so that merely loading + // BSON never calls secure random at module-evaluation time. Runtimes such as Cloudflare Workers + // forbid generating random values in the global scope. + this.PROCESS_UNIQUE = null; + }; + + static { + this.resetState(); + // https://nodejs.org/api/v8.html#startup-snapshot-api + // @ts-expect-error Node.js types not present since this is an optional API + const { startupSnapshot } = globalThis?.process?.getBuiltinModule?.('v8') ?? {}; + if (startupSnapshot?.isBuildingSnapshot?.()) { + startupSnapshot?.addDeserializeCallback?.(this.resetState); + } + } + + static cacheHexString: boolean; + + /** + * The 12 ObjectId bytes packed as four 24-bit integers (bytes 0-2, 3-5, 6-8, 9-11). + * The 24-bit width keeps each value inside V8's small-integer (Smi) range so it stays stored + * inline, which keeps the object small. The fields are enumerable own properties, so two + * ObjectIds with the same bytes stay equal under structural comparison such as deepStrictEqual. + * @internal + */ + private i0!: number; + /** @internal */ + private i1!: number; + /** @internal */ + private i2!: number; + /** @internal */ + private i3!: number; + + /** Pack 12 bytes into the four integer fields. @internal */ + private setFromBytes(b: Uint8Array, offset = 0): void { + this.i0 = (b[offset] << 16) | (b[offset + 1] << 8) | b[offset + 2]; + this.i1 = (b[offset + 3] << 16) | (b[offset + 4] << 8) | b[offset + 5]; + this.i2 = (b[offset + 6] << 16) | (b[offset + 7] << 8) | b[offset + 8]; + this.i3 = (b[offset + 9] << 16) | (b[offset + 10] << 8) | b[offset + 11]; + } + + /** + * Pack a validated 24-character hex string into the four integer fields. The caller must have + * already validated the string; every character is assumed to be a hex digit. + * @internal + */ + private setFromHex(s: string): void { + const t = hexCharCodeToNibble; + this.i0 = + (t[s.charCodeAt(0)] << 20) | + (t[s.charCodeAt(1)] << 16) | + (t[s.charCodeAt(2)] << 12) | + (t[s.charCodeAt(3)] << 8) | + (t[s.charCodeAt(4)] << 4) | + t[s.charCodeAt(5)]; + this.i1 = + (t[s.charCodeAt(6)] << 20) | + (t[s.charCodeAt(7)] << 16) | + (t[s.charCodeAt(8)] << 12) | + (t[s.charCodeAt(9)] << 8) | + (t[s.charCodeAt(10)] << 4) | + t[s.charCodeAt(11)]; + this.i2 = + (t[s.charCodeAt(12)] << 20) | + (t[s.charCodeAt(13)] << 16) | + (t[s.charCodeAt(14)] << 12) | + (t[s.charCodeAt(15)] << 8) | + (t[s.charCodeAt(16)] << 4) | + t[s.charCodeAt(17)]; + this.i3 = + (t[s.charCodeAt(18)] << 20) | + (t[s.charCodeAt(19)] << 16) | + (t[s.charCodeAt(20)] << 12) | + (t[s.charCodeAt(21)] << 8) | + (t[s.charCodeAt(22)] << 4) | + t[s.charCodeAt(23)]; + } + + /** To generate a new ObjectId, use ObjectId() with no argument. */ + constructor(); + /** + * Create ObjectId from a 24 character hex string. + * + * @param inputId - A 24 character hex string. + */ + constructor(inputId: string); + /** + * Create ObjectId from the BSON ObjectId type. + * + * @param inputId - The BSON ObjectId type. + */ + constructor(inputId: ObjectId); + /** + * Create ObjectId from the object type that has the toHexString method. + * + * @param inputId - The ObjectIdLike type. + */ + constructor(inputId: ObjectIdLike); + /** + * Create ObjectId from a 12 byte binary Buffer. + * + * @param inputId - A 12 byte binary Buffer. + */ + constructor(inputId: Uint8Array); + /** + * Implementation overload. + * + * @param inputId - All input types that are used in the constructor implementation. + */ + constructor(inputId?: string | ObjectId | ObjectIdLike | Uint8Array); + /** + * Read 12 bytes from `source` starting at `offset` directly into the packed fields. + * Used by the deserializer on a hot path. + * @internal + */ + constructor(source: Uint8Array, offset: number); + /** + * Create a new ObjectId. + * + * @param inputId - An input value to create a new ObjectId from. + */ + constructor(inputId?: string | ObjectId | ObjectIdLike | Uint8Array, offset?: number) { + super(); + if (typeof offset === 'number') { + // Fast path used by the deserializer: read the 12 bytes directly from source at offset. + this.setFromBytes(inputId as Uint8Array, offset); + return; + } + // workingId is set based on type of input and whether valid id exists for the input + let workingId; + if (typeof inputId === 'object' && inputId && 'id' in inputId) { + if ( + ObjectId.is(inputId) && + typeof inputId.i0 === 'number' && + typeof inputId.i1 === 'number' && + typeof inputId.i2 === 'number' && + typeof inputId.i3 === 'number' + ) { + // Same-build ObjectId: copy the packed fields directly. The general path below would + // hit the id getter (which allocates) and then re-decode a hex round trip. + this.i0 = inputId.i0; + this.i1 = inputId.i1; + this.i2 = inputId.i2; + this.i3 = inputId.i3; + return; + } + if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) { + throw new BSONError('Argument passed in must have an id that is of type string or Buffer'); + } + if ('toHexString' in inputId && typeof inputId.toHexString === 'function') { + workingId = ByteUtils.fromHex(inputId.toHexString()); + } else { + workingId = inputId.id; + } + } else { + workingId = inputId; + } + + // The following cases use workingId to construct an ObjectId + if (workingId == null) { + // The most common use case (blank id, new objectId instance): + // generate a new id directly into the packed fields. + const time = Math.floor(Date.now() / 1000); + const inc = ObjectId.getInc(); + const pu = (ObjectId.PROCESS_UNIQUE ??= ByteUtils.randomBytes(5)); + this.i0 = (time >>> 8) & 0xffffff; + this.i1 = ((time & 0xff) << 16) | (pu[0] << 8) | pu[1]; + this.i2 = (pu[2] << 16) | (pu[3] << 8) | pu[4]; + this.i3 = inc & 0xffffff; + } else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) { + // Normalize a non-Uint8Array view (DataView, Int8Array, ...) to a byte buffer so its + // bytes are read correctly. + this.setFromBytes( + workingId instanceof Uint8Array ? workingId : ByteUtils.toLocalBufferType(workingId) + ); + } else if (typeof workingId === 'string') { + if (ObjectId.validateHexString(workingId)) { + this.setFromHex(workingId); + // If we are caching the hex string + if (ObjectId.cacheHexString) { + __idCache.set(this, workingId); + } + } else { + throw new BSONError( + 'input must be a 24 character hex string, 12 byte Uint8Array, or an integer' + ); + } + } else { + throw new BSONError('Argument passed in does not match the accepted types'); + } + } + + /** + * The ObjectId bytes, rebuilt from the packed integer fields on each access. Every read + * returns a freshly allocated 12-byte buffer. + * @readonly + */ + get id(): Uint8Array { + const b = ByteUtils.allocateUnsafe(12); + b[0] = (this.i0 >>> 16) & 0xff; + b[1] = (this.i0 >>> 8) & 0xff; + b[2] = this.i0 & 0xff; + b[3] = (this.i1 >>> 16) & 0xff; + b[4] = (this.i1 >>> 8) & 0xff; + b[5] = this.i1 & 0xff; + b[6] = (this.i2 >>> 16) & 0xff; + b[7] = (this.i2 >>> 8) & 0xff; + b[8] = this.i2 & 0xff; + b[9] = (this.i3 >>> 16) & 0xff; + b[10] = (this.i3 >>> 8) & 0xff; + b[11] = this.i3 & 0xff; + return b; + } + + set id(value: Uint8Array) { + const bytes = value instanceof Uint8Array ? value : ByteUtils.toLocalBufferType(value); + this.setFromBytes(bytes); + if (ObjectId.cacheHexString) { + __idCache.set(this, ByteUtils.toHex(bytes)); + } + } + + /** + * @internal + * Validates the input string is a valid hex representation of an ObjectId. + */ + private static validateHexString(string: string): boolean { + if (string?.length !== 24) return false; + for (let i = 0; i < 24; i++) { + const char = string.charCodeAt(i); + if ( + // Check for ASCII 0-9 + (char >= 48 && char <= 57) || + // Check for ASCII a-f + (char >= 97 && char <= 102) || + // Check for ASCII A-F + (char >= 65 && char <= 70) + ) { + continue; + } + return false; + } + return true; + } + + /** Returns the ObjectId id as a 24 lowercase character hex string representation */ + toHexString(): string { + if (ObjectId.cacheHexString) { + const __id = __idCache.get(this); + if (__id) return __id; + } + + // Encode the four packed integers to hex via a byte->pair lookup table. + const i0 = this.i0; + const i1 = this.i1; + const i2 = this.i2; + const i3 = this.i3; + const hexString = + byteToHex[(i0 >>> 16) & 0xff] + + byteToHex[(i0 >>> 8) & 0xff] + + byteToHex[i0 & 0xff] + + byteToHex[(i1 >>> 16) & 0xff] + + byteToHex[(i1 >>> 8) & 0xff] + + byteToHex[i1 & 0xff] + + byteToHex[(i2 >>> 16) & 0xff] + + byteToHex[(i2 >>> 8) & 0xff] + + byteToHex[i2 & 0xff] + + byteToHex[(i3 >>> 16) & 0xff] + + byteToHex[(i3 >>> 8) & 0xff] + + byteToHex[i3 & 0xff]; + + if (ObjectId.cacheHexString) { + __idCache.set(this, hexString); + } + + return hexString; + } + + /** + * Update the ObjectId index + * @internal + */ + private static getInc(): number { + return (ObjectId.index = (ObjectId.index + 1) % 0x1000000); + } + + /** + * Generate a 12 byte id buffer used in ObjectId's + * + * @param time - pass in a second based timestamp. + */ + static generate(time?: number): Uint8Array { + if ('number' !== typeof time) { + time = Math.floor(Date.now() / 1000); + } + + const inc = ObjectId.getInc(); + const buffer = ByteUtils.allocateUnsafe(12); + + // 4-byte timestamp + NumberUtils.setInt32BE(buffer, 0, time); + + // 5-byte process unique (generated lazily on first use, see NODE-7667) + const PROCESS_UNIQUE = (this.PROCESS_UNIQUE ??= ByteUtils.randomBytes(5)); + buffer[4] = PROCESS_UNIQUE[0]; + buffer[5] = PROCESS_UNIQUE[1]; + buffer[6] = PROCESS_UNIQUE[2]; + buffer[7] = PROCESS_UNIQUE[3]; + buffer[8] = PROCESS_UNIQUE[4]; + + // 3-byte counter + buffer[11] = inc & 0xff; + buffer[10] = (inc >>> 8) & 0xff; + buffer[9] = (inc >>> 16) & 0xff; + + return buffer; + } + + /** + * Converts the id into a 24 character hex string for printing, unless encoding is provided. + * @param encoding - hex or base64 + */ + toString(encoding?: 'hex' | 'base64'): string { + if (encoding === 'base64') return ByteUtils.toBase64(this.id); + if (encoding === 'hex') return this.toHexString(); + return this.toHexString(); + } + + /** Converts to its JSON the 24 character hex string representation. */ + toJSON(): string { + return this.toHexString(); + } + + /** @internal */ + private static is(variable: unknown): variable is ObjectId { + return ( + variable != null && + typeof variable === 'object' && + '_bsontype' in variable && + variable._bsontype === 'ObjectId' + ); + } + + /** + * Compares the equality of this ObjectId with `otherID`. + * + * @param otherId - ObjectId instance to compare against. + */ + equals(otherId: string | ObjectId | ObjectIdLike | undefined | null): boolean { + if (otherId === undefined || otherId === null) { + return false; + } + + if ( + ObjectId.is(otherId) && + typeof otherId.i0 === 'number' && + typeof otherId.i1 === 'number' && + typeof otherId.i2 === 'number' && + typeof otherId.i3 === 'number' + ) { + // Same-build ObjectId (all four packed fields present): compare them directly. i3 (the + // counter / low bytes) differs most often between two ids, so checking it first fails fast. + return ( + this.i3 === otherId.i3 && + this.i0 === otherId.i0 && + this.i1 === otherId.i1 && + this.i2 === otherId.i2 + ); + } + + if (typeof otherId === 'string') { + return otherId.toLowerCase() === this.toHexString(); + } + + if (typeof otherId === 'object' && typeof otherId.toHexString === 'function') { + // ObjectId-like values, and ObjectIds from a different bson build, compare by their + // public hex representation. + const otherIdString = otherId.toHexString(); + const thisIdString = this.toHexString(); + return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString; + } + + return false; + } + + /** Returns the generation date (accurate up to the second) that this ID was generated. */ + getTimestamp(): Date { + const timestamp = new Date(); + // Bytes 0-3 (the big-endian timestamp) are i0's three bytes followed by i1's top byte. + const time = this.i0 * 0x100 + (this.i1 >>> 16); + timestamp.setTime(time * 1000); + return timestamp; + } + + /** @internal */ + static createPk(): ObjectId { + return new ObjectId(); + } + + /** @internal */ + serializeInto(uint8array: Uint8Array, index: number): 12 { + uint8array[index] = (this.i0 >>> 16) & 0xff; + uint8array[index + 1] = (this.i0 >>> 8) & 0xff; + uint8array[index + 2] = this.i0 & 0xff; + uint8array[index + 3] = (this.i1 >>> 16) & 0xff; + uint8array[index + 4] = (this.i1 >>> 8) & 0xff; + uint8array[index + 5] = this.i1 & 0xff; + uint8array[index + 6] = (this.i2 >>> 16) & 0xff; + uint8array[index + 7] = (this.i2 >>> 8) & 0xff; + uint8array[index + 8] = this.i2 & 0xff; + uint8array[index + 9] = (this.i3 >>> 16) & 0xff; + uint8array[index + 10] = (this.i3 >>> 8) & 0xff; + uint8array[index + 11] = this.i3 & 0xff; + return 12; + } + + /** + * Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId. + * + * @param time - an integer number representing a number of seconds. + */ + static createFromTime(time: number): ObjectId { + const buffer = ByteUtils.allocate(12); + for (let i = 11; i >= 4; i--) buffer[i] = 0; + // Encode time into first 4 bytes + NumberUtils.setInt32BE(buffer, 0, time); + // Return the new objectId + return new ObjectId(buffer); + } + + /** + * Creates an ObjectId from a hex string representation of an ObjectId. + * + * @param hexString - create a ObjectId from a passed in 24 character hexstring. + */ + static createFromHexString(hexString: string): ObjectId { + if (hexString?.length !== 24) { + throw new BSONError('hex string must be 24 characters'); + } + + return new ObjectId(ByteUtils.fromHex(hexString)); + } + + /** Creates an ObjectId instance from a base64 string */ + static createFromBase64(base64: string): ObjectId { + if (base64?.length !== 16) { + throw new BSONError('base64 string must be 16 characters'); + } + + return new ObjectId(ByteUtils.fromBase64(base64)); + } + + /** + * Checks if a value can be used to create a valid bson ObjectId + * @param id - any JS value + */ + static isValid(id: string | ObjectId | ObjectIdLike | Uint8Array): boolean { + if (id == null) return false; + if (typeof id === 'string') return ObjectId.validateHexString(id); + + try { + new ObjectId(id); + return true; + } catch { + return false; + } + } + + /** @internal */ + toExtendedJSON(): ObjectIdExtended { + if (this.toHexString) return { $oid: this.toHexString() }; + return { $oid: this.toString('hex') }; + } + + /** @internal */ + static fromExtendedJSON(doc: ObjectIdExtended): ObjectId { + return new ObjectId(doc.$oid); + } + + /** @internal */ + private isCached(): boolean { + return ObjectId.cacheHexString && __idCache.has(this); + } + + /** + * Converts to a string representation of this Id. + * + * @returns return the 24 character hex string representation. + */ + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + inspect ??= defaultInspect; + return `new ObjectId(${inspect(this.toHexString(), options)})`; + } +} diff --git a/gateway/node_modules/bson/src/parse_utf8.ts b/gateway/node_modules/bson/src/parse_utf8.ts new file mode 100644 index 0000000000000000000000000000000000000000..045a9080be943a8c0d17b5d169fac6ef902c3014 --- /dev/null +++ b/gateway/node_modules/bson/src/parse_utf8.ts @@ -0,0 +1,35 @@ +import { BSONError } from './error'; + +type TextDecoder = { + readonly encoding: string; + readonly fatal: boolean; + readonly ignoreBOM: boolean; + decode(input?: Uint8Array): string; +}; +type TextDecoderConstructor = { + new (label: 'utf8', options: { fatal: boolean; ignoreBOM?: boolean }): TextDecoder; +}; + +// parse utf8 globals +declare const TextDecoder: TextDecoderConstructor; +let TextDecoderFatal: TextDecoder; +let TextDecoderNonFatal: TextDecoder; + +/** + * Determines if the passed in bytes are valid utf8 + * @param bytes - An array of 8-bit bytes. Must be indexable and have length property + * @param start - The index to start validating + * @param end - The index to end validating + */ +export function parseUtf8(buffer: Uint8Array, start: number, end: number, fatal: boolean): string { + if (fatal) { + TextDecoderFatal ??= new TextDecoder('utf8', { fatal: true }); + try { + return TextDecoderFatal.decode(buffer.subarray(start, end)); + } catch (cause) { + throw new BSONError('Invalid UTF-8 string in BSON document', { cause }); + } + } + TextDecoderNonFatal ??= new TextDecoder('utf8', { fatal: false }); + return TextDecoderNonFatal.decode(buffer.subarray(start, end)); +} diff --git a/gateway/node_modules/bson/src/parser/calculate_size.ts b/gateway/node_modules/bson/src/parser/calculate_size.ts new file mode 100644 index 0000000000000000000000000000000000000000..5a34551000d21177c1a4a154f41da9a8f76cc1ad --- /dev/null +++ b/gateway/node_modules/bson/src/parser/calculate_size.ts @@ -0,0 +1,239 @@ +import { Binary } from '../binary'; +import type { Document } from '../bson'; +import { BSONError, BSONVersionError } from '../error'; +import * as constants from '../constants'; +import { ByteUtils } from '../utils/byte_utils'; +import { isAnyArrayBuffer, isDate, isMap, isRegExp } from './utils'; + +export function internalCalculateObjectSize( + object: Document, + serializeFunctions?: boolean, + ignoreUndefined?: boolean +): number { + // Each stack entry carries its own ignoreUndefined so DBRef fields can force ignoreUndefined=true + // regardless of the caller's setting, matching the behavior of serializeInto. + const objectStack: Array<{ obj: Document; ignoreUndefined: boolean }> = [ + { obj: object, ignoreUndefined: ignoreUndefined ?? false } + ]; + let total = 0; + + while (objectStack.length > 0) { + const { obj, ignoreUndefined: frameIgnoreUndefined } = objectStack.pop()!; + total += 5; // 4-byte size field + null terminator + + const isObjArray = Array.isArray(obj); + const isObjMap = !isObjArray && (obj instanceof Map || isMap(obj)); + let target = obj; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + if (!isObjArray && !isObjMap && typeof (obj as any)?.toBSON === 'function') { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + target = (obj as any).toBSON(); + } + + if (isObjArray) { + const array = target as unknown[]; + for (let i = 0; i < array.length; i++) { + total += calculateElementSize( + i.toString(), + array[i], + serializeFunctions, + true, + frameIgnoreUndefined, + objectStack + ); + } + } else if (isObjMap) { + for (const [key, value] of target as Map) { + total += calculateElementSize( + key, + value, + serializeFunctions, + false, + frameIgnoreUndefined, + objectStack + ); + } + } else { + for (const key of Object.keys(target)) { + total += calculateElementSize( + key, + target[key], + serializeFunctions, + false, + frameIgnoreUndefined, + objectStack + ); + } + } + } + + return total; +} + +/** @internal */ +function calculateElementSize( + name: string, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + value: any, + serializeFunctions = false, + isArray = false, + ignoreUndefined = false, + objectStack: Array<{ obj: Document; ignoreUndefined: boolean }> +): number { + // If we have toBSON defined, override the current object + if (typeof value?.toBSON === 'function') { + value = value.toBSON(); + } + + switch (typeof value) { + case 'string': + return 1 + ByteUtils.utf8ByteLength(name) + 1 + 4 + ByteUtils.utf8ByteLength(value) + 1; + case 'number': + if ( + Math.floor(value) === value && + value >= constants.JS_INT_MIN && + value <= constants.JS_INT_MAX + ) { + if (value >= constants.BSON_INT32_MIN && value <= constants.BSON_INT32_MAX) { + // 32 bit + return ByteUtils.utf8ByteLength(name) + 1 + (4 + 1); + } else { + return ByteUtils.utf8ByteLength(name) + 1 + (8 + 1); + } + } else { + // 64 bit + return ByteUtils.utf8ByteLength(name) + 1 + (8 + 1); + } + case 'undefined': + if (isArray || !ignoreUndefined) return ByteUtils.utf8ByteLength(name) + 1 + 1; + return 0; + case 'boolean': + return ByteUtils.utf8ByteLength(name) + 1 + (1 + 1); + case 'object': + if ( + value != null && + typeof value._bsontype === 'string' && + value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION + ) { + throw new BSONVersionError(); + } else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') { + return ByteUtils.utf8ByteLength(name) + 1 + 1; + } else if (value._bsontype === 'ObjectId') { + return ByteUtils.utf8ByteLength(name) + 1 + (12 + 1); + } else if (value instanceof Date || isDate(value)) { + return ByteUtils.utf8ByteLength(name) + 1 + (8 + 1); + } else if ( + ArrayBuffer.isView(value) || + value instanceof ArrayBuffer || + isAnyArrayBuffer(value) + ) { + return ByteUtils.utf8ByteLength(name) + 1 + (1 + 4 + 1) + value.byteLength; + } else if ( + value._bsontype === 'Long' || + value._bsontype === 'Double' || + value._bsontype === 'Timestamp' + ) { + return ByteUtils.utf8ByteLength(name) + 1 + (8 + 1); + } else if (value._bsontype === 'Decimal128') { + return ByteUtils.utf8ByteLength(name) + 1 + (16 + 1); + } else if (value._bsontype === 'Int32') { + return ByteUtils.utf8ByteLength(name) + 1 + (4 + 1); + } else if (value._bsontype === 'Code') { + // Calculate size depending on the availability of a scope + if (value.scope != null && Object.keys(value.scope).length > 0) { + objectStack.push({ obj: value.scope, ignoreUndefined }); + return ( + ByteUtils.utf8ByteLength(name) + + 1 + + 1 + + 4 + + 4 + + ByteUtils.utf8ByteLength(value.code.toString()) + + 1 + ); + } else { + return ( + ByteUtils.utf8ByteLength(name) + + 1 + + 1 + + 4 + + ByteUtils.utf8ByteLength(value.code.toString()) + + 1 + ); + } + } else if (value._bsontype === 'Binary') { + const binary: Binary = value; + // Check what kind of subtype we have + if (binary.sub_type === Binary.SUBTYPE_BYTE_ARRAY) { + return ByteUtils.utf8ByteLength(name) + 1 + (binary.position + 1 + 4 + 1 + 4); + } else { + return ByteUtils.utf8ByteLength(name) + 1 + (binary.position + 1 + 4 + 1); + } + } else if (value._bsontype === 'BSONSymbol') { + return ( + ByteUtils.utf8ByteLength(name) + 1 + ByteUtils.utf8ByteLength(value.value) + 4 + 1 + 1 + ); + } else if (value._bsontype === 'DBRef') { + // Set up correct object for serialization + const ordered_values = Object.assign( + { + $ref: value.collection, + $id: value.oid + }, + value.fields + ); + + // Add db reference if it exists + if (value.db != null) { + ordered_values['$db'] = value.db; + } + + // DBRef fields always use ignoreUndefined=true to match serializeInto behavior. + objectStack.push({ obj: ordered_values, ignoreUndefined: true }); + return ByteUtils.utf8ByteLength(name) + 1 + 1; + } else if (value instanceof RegExp || isRegExp(value)) { + return ( + ByteUtils.utf8ByteLength(name) + + 1 + + 1 + + ByteUtils.utf8ByteLength(value.source) + + 1 + + (value.global ? 1 : 0) + + (value.ignoreCase ? 1 : 0) + + (value.multiline ? 1 : 0) + + 1 + ); + } else if (value._bsontype === 'BSONRegExp') { + return ( + ByteUtils.utf8ByteLength(name) + + 1 + + 1 + + ByteUtils.utf8ByteLength(value.pattern) + + 1 + + ByteUtils.utf8ByteLength(value.options) + + 1 + ); + } else { + objectStack.push({ obj: value, ignoreUndefined }); + return ByteUtils.utf8ByteLength(name) + 1 + 1; + } + case 'function': + if (serializeFunctions) { + return ( + ByteUtils.utf8ByteLength(name) + + 1 + + 1 + + 4 + + ByteUtils.utf8ByteLength(value.toString()) + + 1 + ); + } + return 0; + case 'bigint': + return ByteUtils.utf8ByteLength(name) + 1 + (8 + 1); + case 'symbol': + return 0; + default: + throw new BSONError(`Unrecognized JS type: ${typeof value}`); + } +} diff --git a/gateway/node_modules/bson/src/parser/deserializer.ts b/gateway/node_modules/bson/src/parser/deserializer.ts new file mode 100644 index 0000000000000000000000000000000000000000..06eb6a39fa62e6bc0e93a228afe8b1e4f3fe7f42 --- /dev/null +++ b/gateway/node_modules/bson/src/parser/deserializer.ts @@ -0,0 +1,793 @@ +import { Binary, UUID } from '../binary'; +import type { Document } from '../bson'; +import { Code } from '../code'; +import * as constants from '../constants'; +import { DBRef, isDBRefLike } from '../db_ref'; +import { Decimal128 } from '../decimal128'; +import { Double } from '../double'; +import { BSONError } from '../error'; +import { Int32 } from '../int_32'; +import { Long } from '../long'; +import { MaxKey } from '../max_key'; +import { MinKey } from '../min_key'; +import { ObjectId } from '../objectid'; +import { BSONRegExp } from '../regexp'; +import { BSONSymbol } from '../symbol'; +import { Timestamp } from '../timestamp'; +import { ByteUtils } from '../utils/byte_utils'; +import { NumberUtils } from '../utils/number_utils'; + +/** @public */ +export interface DeserializeOptions { + /** + * when deserializing a Long return as a BigInt. + * @defaultValue `false` + */ + useBigInt64?: boolean; + /** + * when deserializing a Long will fit it into a Number if it's smaller than 53 bits. + * @defaultValue `true` + */ + promoteLongs?: boolean; + /** + * when deserializing a Binary will return it as a node.js Buffer instance. + * @defaultValue `false` + */ + promoteBuffers?: boolean; + /** + * when deserializing will promote BSON values to their Node.js closest equivalent types. + * @defaultValue `true` + */ + promoteValues?: boolean; + /** + * allow to specify if there what fields we wish to return as unserialized raw buffer. + * @defaultValue `null` + */ + fieldsAsRaw?: Document; + /** + * return BSON regular expressions as BSONRegExp instances. + * @defaultValue `false` + */ + bsonRegExp?: boolean; + /** + * allows the buffer to be larger than the parsed BSON object. + * @defaultValue `false` + */ + allowObjectSmallerThanBufferSize?: boolean; + /** + * Offset into buffer to begin reading document from + * @defaultValue `0` + */ + index?: number; + + raw?: boolean; + /** Allows for opt-out utf-8 validation for all keys or + * specified keys. Must be all true or all false. + * + * @example + * ```js + * // disables validation on all keys + * validation: { utf8: false } + * + * // enables validation only on specified keys a, b, and c + * validation: { utf8: { a: true, b: true, c: true } } + * + * // disables validation only on specified keys a, b + * validation: { utf8: { a: false, b: false } } + * ``` + */ + validation?: { utf8: boolean | Record | Record }; +} + +// Internal long versions +const JS_INT_MAX_LONG = Long.fromNumber(constants.JS_INT_MAX); +const JS_INT_MIN_LONG = Long.fromNumber(constants.JS_INT_MIN); + +export function internalDeserialize( + buffer: Uint8Array, + options: DeserializeOptions, + isArray?: boolean +): Document { + options = options == null ? {} : options; + const index = options && options.index ? options.index : 0; + // Read the document size + const size = NumberUtils.getInt32LE(buffer, index); + + if (size < 5) { + throw new BSONError(`bson size must be >= 5, is ${size}`); + } + + if (options.allowObjectSmallerThanBufferSize && buffer.length < size) { + throw new BSONError(`buffer length ${buffer.length} must be >= bson size ${size}`); + } + + if (!options.allowObjectSmallerThanBufferSize && buffer.length !== size) { + throw new BSONError(`buffer length ${buffer.length} must === bson size ${size}`); + } + + if (size + index > buffer.byteLength) { + throw new BSONError( + `(bson size ${size} + options.index ${index} must be <= buffer length ${buffer.byteLength})` + ); + } + + // Illegal end value + if (buffer[index + size - 1] !== 0) { + throw new BSONError( + "One object, sized correctly, with a spot for an EOO, but the EOO isn't 0x00" + ); + } + + // Start deserialization + return deserializeObject(buffer, index, options, isArray); +} + +interface NestedParsingFrame { + // One of 3 supported types: + // - constants.BSON_DATA_OBJECT + // - constants.BSON_DATA_ARRAY + // - constants.BSON_DATA_CODE_W_SCOPE + elementType: + | typeof constants.BSON_DATA_OBJECT + | typeof constants.BSON_DATA_ARRAY + | typeof constants.BSON_DATA_CODE_W_SCOPE; + // Document that we will fill out as we parse the nested object + holdingDocument: Document; + // The name of the key we will set the parsed object on once we finish parsing the nested object, this is used in the onComplete callback to know where to set the parsed nested object in the parent document + propertyName: string | number; + // The index in the buffer where the current object ends, used to know when we are done parsing the current object and can pop the stack + lastIndex: number; + // Whether the current frame is parsing an array, used to know whether to interpret keys as strings or array indices + isArray: boolean; + // The next array index to use if this frame is parsing an array, used to assign numeric keys to array elements without having to utf-8 decode the key from the buffer + arrayIndex: number; + // When true, all objects in this frame will be returned as raw bson buffers without parsing. + // This is used when the fieldsAsRaw option is used on a parent object, and is inherited by nested frames. + // It can also be set to true if the global raw option is set, but it cannot be set to true for a frame if the global raw option is false. + raw: boolean; + // When true, this frame may be a DBRef. This is set to false if we encounter a key that is not valid for a DBRef, and is left as null for arrays since they cannot be DBRefs. + isPossibleDBRef: boolean | null; + // The utf-8 validation setting for this frame, used to determine whether to utf-8 validate keys in this frame. This is determined based on the global utf-8 validation setting and the specific keys specified in the validation option. + validationSetting: boolean; + functionString: string | null; // only used for Code with Scope + // The enclosing frame, or null at the top level. The parsing stack is a linked list threaded + // through this field rather than a separate array, which avoids array push/pop churn per frame. + prev: NestedParsingFrame | null; +} + +const allowedDBRefKeys = /^\$ref$|^\$id$|^\$db$/; + +// Assigns a parsed value into the destination document, guarding the __proto__ key to avoid +// prototype pollution. +function assignValue(dest: Document, name: string | number, value: unknown): void { + if (name === '__proto__') { + Object.defineProperty(dest, name, { + value, + writable: true, + enumerable: true, + configurable: true + }); + } else { + dest[name] = value; + } +} + +// Promotes a plain document to a DBRef instance when it has the DBRef shape ($ref/$id[/$db]). +function toPotentialDbRef(doc: Document): DBRef | Document { + if (isDBRefLike(doc)) { + const { $ref, $id, $db, ...fields } = doc; + return new DBRef($ref, $id, $db, fields); + } + return doc; +} + +function deserializeObject( + buffer: Uint8Array, + index: number, + options: DeserializeOptions, + isArray = false +) { + // Settings configured from options parameter + + // Strips prototype chain so inherited properties don't affect option reads. + options = { ...options }; + + // Used to track fields that should be returned as raw bson buffers without parsing, this is set based on the fieldsAsRaw option and is inherited by nested frames when parsing nested objects + const fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw']; + + // Return raw bson buffer instead of parsing it + const raw = options['raw'] == null ? false : options['raw']; + + // Return BSONRegExp objects instead of native regular expressions + const bsonRegExp = typeof options['bsonRegExp'] === 'boolean' ? options['bsonRegExp'] : false; + + // Controls the promotion of values vs wrapper classes + const promoteBuffers = options.promoteBuffers ?? false; + const promoteLongs = options.promoteLongs ?? true; + const promoteValues = options.promoteValues ?? true; + const useBigInt64 = options.useBigInt64 ?? false; + + // Validate bigint and long promotion settings + if (useBigInt64 && !promoteValues) { + throw new BSONError('Must either request bigint or Long for int64 deserialization'); + } + if (useBigInt64 && !promoteLongs) { + throw new BSONError('Must either request bigint or Long for int64 deserialization'); + } + + // Ensures default validation option if none given + const validation = options.validation == null ? { utf8: true } : options.validation; + + // Shows if global utf-8 validation is enabled or disabled + let globalUTFValidation = true; + // Reflects utf-8 validation setting regardless of global or specific key validation + let validationSetting: boolean; + // Set of keys either to enable or disable validation on + let utf8KeysSet; + + // Check for boolean uniformity and empty validation option + const utf8ValidatedKeys = validation.utf8; + if (typeof utf8ValidatedKeys === 'boolean') { + validationSetting = utf8ValidatedKeys; + } else { + globalUTFValidation = false; + const utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) { + return utf8ValidatedKeys[key]; + }); + if (utf8ValidationValues.length === 0) { + throw new BSONError('UTF-8 validation setting cannot be empty'); + } + if (typeof utf8ValidationValues[0] !== 'boolean') { + throw new BSONError('Invalid UTF-8 validation option, must specify boolean values'); + } + validationSetting = utf8ValidationValues[0]; + // Ensures boolean uniformity in utf-8 validation (all true or all false) + if (!utf8ValidationValues.every(item => item === validationSetting)) { + throw new BSONError('Invalid UTF-8 validation option - keys must be all true or all false'); + } + } + + // Add keys to set that will either be validated or not based on validationSetting + if (!globalUTFValidation) { + utf8KeysSet = new Set(); + + for (const key of Object.keys(utf8ValidatedKeys)) { + utf8KeysSet.add(key); + } + } + + // Begin parsing the document + + // Set the start index + const startIndex = index; + + // Validate that we have at least 4 bytes of buffer + if (buffer.length < 5) throw new BSONError('corrupt bson message < 5 bytes long'); + + // Read the document size + const size = NumberUtils.getInt32LE(buffer, index); + // Skip past the size field + index += 4; + + // Ensure buffer is valid size + if (size < 5 || size > buffer.length) throw new BSONError('corrupt bson message'); + + // Create holding object + const rootObject: Document = isArray ? [] : {}; + // Used for arrays to skip having to perform utf8 decoding + let arrayIndex = 0; + + let isPossibleDBRef = isArray ? false : null; + + // Top of the parsing stack (a linked list via each frame's `prev`), or null at the top level. + let currentFrame: NestedParsingFrame | null = null; + // Destination object for the current frame (the parent's holdingDocument, or rootObject at the + // top level). Maintained alongside currentFrame so per-field assignment never recomputes it. + let currentDest: Document = rootObject; + // Whether the current frame is an array. Maintained alongside currentFrame so the per-field key + // logic does not branch on currentFrame every iteration. + let currentIsArray = isArray; + + // While we have more left data left keep parsing + while (true) { + // Read the type + const elementType = buffer[index++]; + + // If we get a zero it's the last byte, exit + if (elementType === 0) { + // 0 byte marks end of document. + if (currentFrame) { + // If we're in a frame, that means the end of the current nested document + if (index === currentFrame.lastIndex) { + // Snapshot the completed frame before updating currentFrame to the parent. + const completedFrame: NestedParsingFrame = currentFrame; + currentFrame = completedFrame.prev; + if (currentFrame === null) { + currentDest = rootObject; + currentIsArray = isArray; + } else { + currentDest = currentFrame.holdingDocument; + currentIsArray = currentFrame.isArray; + } + // finish the frame + let result: Document = completedFrame.holdingDocument; + switch (completedFrame.elementType) { + case constants.BSON_DATA_OBJECT: + // if this is a DBRef, we need to construct a DBRef object instead of a plain object + if (completedFrame.isPossibleDBRef) { + result = toPotentialDbRef(result); + } + break; + case constants.BSON_DATA_ARRAY: + // nothing to do, the holding document is already an array and the keys were set as numeric indices + break; + case constants.BSON_DATA_CODE_W_SCOPE: + // the holding document is the scope, we need to construct a Code object with the function string and scope + result = new Code(completedFrame.functionString!, completedFrame.holdingDocument); + break; + default: + throw new BSONError('Unexpected element type in frame stack'); + } + // set the value in the parent document (currentDest now points to the parent's document) + assignValue(currentDest, completedFrame.propertyName, result); + continue; + } else { + // Current index does not match the last index of the frame, the document is malformed + if (currentFrame.elementType === constants.BSON_DATA_ARRAY) { + throw new BSONError('corrupted array bson'); + } + throw new BSONError('Bad BSON Document: object not properly terminated'); + } + } else { + // If we're not in a frame, that means the end of the root document, so we break out of the loop and return the object + break; + } + } + + // Get the start search index + let i = index; + // Locate the end of the c string + while (buffer[i] !== 0x00 && i < buffer.length) { + i++; + } + + // If are at the end of the buffer there is a problem with the document + if (i >= buffer.byteLength) throw new BSONError('Bad BSON Document: illegal CString'); + + // Represents the key + const name = currentIsArray + ? currentFrame !== null + ? currentFrame.arrayIndex++ + : arrayIndex++ + : ByteUtils.toUTF8(buffer, index, i, false); + + // shouldValidateKey is true if the key should be validated, false otherwise. + // Within a nested frame the original code passed a collapsed boolean validation option, + // so all keys in the frame are validated uniformly using the frame's setting. + let shouldValidateKey: boolean; + if (currentFrame !== null) { + shouldValidateKey = currentFrame.validationSetting; + } else if (globalUTFValidation || utf8KeysSet?.has(name)) { + shouldValidateKey = validationSetting; + } else { + shouldValidateKey = !validationSetting; + } + + // Route DBRef key tracking to the current frame; the root variable handles the root doc. + if (currentFrame !== null) { + if (currentFrame.isPossibleDBRef !== false && typeof name === 'string' && name[0] === '$') { + currentFrame.isPossibleDBRef = allowedDBRefKeys.test(name); + } + } else if (isPossibleDBRef !== false && (name as string)[0] === '$') { + isPossibleDBRef = allowedDBRefKeys.test(name as string); + } + let value; + let isDeferredValue = false; + + index = i + 1; + + if (elementType === constants.BSON_DATA_STRING) { + const stringSize = NumberUtils.getInt32LE(buffer, index); + index += 4; + if ( + stringSize <= 0 || + stringSize > buffer.length - index || + buffer[index + stringSize - 1] !== 0 + ) { + throw new BSONError('bad string length in bson'); + } + value = ByteUtils.toUTF8(buffer, index, index + stringSize - 1, shouldValidateKey); + index = index + stringSize; + } else if (elementType === constants.BSON_DATA_OID) { + // ObjectId reads these 12 bytes from the wire buffer at this offset into its own fields + // and keeps no reference to the source, so the buffer can be passed directly. + value = new ObjectId(buffer, index); + index = index + 12; + } else if (elementType === constants.BSON_DATA_INT && promoteValues === false) { + value = new Int32(NumberUtils.getInt32LE(buffer, index)); + index += 4; + } else if (elementType === constants.BSON_DATA_INT) { + value = NumberUtils.getInt32LE(buffer, index); + index += 4; + } else if (elementType === constants.BSON_DATA_NUMBER) { + value = NumberUtils.getFloat64LE(buffer, index); + index += 8; + if (promoteValues === false) value = new Double(value); + } else if (elementType === constants.BSON_DATA_DATE) { + const lowBits = NumberUtils.getInt32LE(buffer, index); + const highBits = NumberUtils.getInt32LE(buffer, index + 4); + index += 8; + + value = new Date(new Long(lowBits, highBits).toNumber()); + } else if (elementType === constants.BSON_DATA_BOOLEAN) { + if (buffer[index] !== 0 && buffer[index] !== 1) + throw new BSONError('illegal boolean type value'); + value = buffer[index++] === 1; + } else if (elementType === constants.BSON_DATA_OBJECT) { + const objectSize = NumberUtils.getInt32LE(buffer, index); + + if (objectSize < 5 || objectSize > buffer.length - index) + throw new BSONError('bad embedded document length in bson'); + + // We have a raw value: either the global raw option, or the parent frame requested raw elements. + if (raw || (currentFrame?.raw ?? false)) { + value = buffer.subarray(index, index + objectSize); + index = index + objectSize; + } else { + isDeferredValue = true; + const objectFrame: NestedParsingFrame = { + holdingDocument: {}, + elementType: constants.BSON_DATA_OBJECT, + propertyName: name, + functionString: null, + lastIndex: index + objectSize, + isArray: false, + arrayIndex: 0, + raw: false, + isPossibleDBRef: null, // we don't know if this is a DBRef until we parse the keys, so we start with null and set to false if we encounter a key that is not valid for a DBRef + validationSetting: shouldValidateKey, + prev: currentFrame + }; + currentFrame = objectFrame; + currentDest = objectFrame.holdingDocument; + currentIsArray = false; + index = index + 4; + } + } else if (elementType === constants.BSON_DATA_ARRAY) { + const objectSize = NumberUtils.getInt32LE(buffer, index); + + if (objectSize < 5 || objectSize > buffer.length - index) + throw new BSONError('bad embedded array length in bson'); + + // Stop index + const stopIndex = index + objectSize; + + // fieldsAsRaw match: push with raw=true so embedded objects inside come back as raw bytes. + // Also propagate raw from the parent frame (nested arrays inside a raw array stay raw). + const arrayRaw = !!(fieldsAsRaw && fieldsAsRaw[name]) || (currentFrame?.raw ?? false); + isDeferredValue = true; + const arrayFrame: NestedParsingFrame = { + holdingDocument: [], + elementType: constants.BSON_DATA_ARRAY, + propertyName: name, + functionString: null, + lastIndex: stopIndex, + isArray: true, + arrayIndex: 0, + raw: arrayRaw, + isPossibleDBRef: false, + validationSetting: shouldValidateKey, + prev: currentFrame + }; + currentFrame = arrayFrame; + currentDest = arrayFrame.holdingDocument; + currentIsArray = true; + index = index + 4; + } else if (elementType === constants.BSON_DATA_UNDEFINED) { + value = undefined; + } else if (elementType === constants.BSON_DATA_NULL) { + value = null; + } else if (elementType === constants.BSON_DATA_LONG) { + if (useBigInt64) { + value = NumberUtils.getBigInt64LE(buffer, index); + index += 8; + } else { + // Unpack the low and high bits + const lowBits = NumberUtils.getInt32LE(buffer, index); + const highBits = NumberUtils.getInt32LE(buffer, index + 4); + index += 8; + + const long = new Long(lowBits, highBits); + // Promote the long if possible + if (promoteLongs && promoteValues === true) { + value = + long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG) + ? long.toNumber() + : long; + } else { + value = long; + } + } + } else if (elementType === constants.BSON_DATA_DECIMAL128) { + // Buffer to contain the decimal bytes + const bytes = ByteUtils.allocateUnsafe(16); + // Copy the next 16 bytes into the bytes buffer + for (let i = 0; i < 16; i++) bytes[i] = buffer[index + i]; + // Update index + index = index + 16; + // Assign the new Decimal128 value + value = new Decimal128(bytes); + } else if (elementType === constants.BSON_DATA_BINARY) { + let binarySize = NumberUtils.getInt32LE(buffer, index); + index += 4; + const totalBinarySize = binarySize; + const subType = buffer[index++]; + + // Did we have a negative binary size, throw + if (binarySize < 0) throw new BSONError('Negative binary type element size found'); + + // Is the length longer than the document + if (binarySize > buffer.byteLength) + throw new BSONError('Binary type size larger than document size'); + + // If we have subtype 2 skip the 4 bytes for the size + if (subType === Binary.SUBTYPE_BYTE_ARRAY) { + binarySize = NumberUtils.getInt32LE(buffer, index); + index += 4; + if (binarySize < 0) + throw new BSONError('Negative binary type element size found for subtype 0x02'); + if (binarySize > totalBinarySize - 4) + throw new BSONError('Binary type with subtype 0x02 contains too long binary size'); + if (binarySize < totalBinarySize - 4) + throw new BSONError('Binary type with subtype 0x02 contains too short binary size'); + } + + if (promoteBuffers && promoteValues) { + value = ByteUtils.toLocalBufferType(buffer.subarray(index, index + binarySize)); + } else { + value = new Binary(buffer.subarray(index, index + binarySize), subType); + if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) { + value = value.toUUID(); + } + } + + // Update the index + index = index + binarySize; + } else if (elementType === constants.BSON_DATA_REGEXP && bsonRegExp === false) { + // Get the start search index + i = index; + // Locate the end of the c string + while (buffer[i] !== 0x00 && i < buffer.length) { + i++; + } + // If are at the end of the buffer there is a problem with the document + if (i >= buffer.length) throw new BSONError('Bad BSON Document: illegal CString'); + // Return the C string + const source = ByteUtils.toUTF8(buffer, index, i, false); + // Create the regexp + index = i + 1; + + // Get the start search index + i = index; + // Locate the end of the c string + while (buffer[i] !== 0x00 && i < buffer.length) { + i++; + } + // If are at the end of the buffer there is a problem with the document + if (i >= buffer.length) throw new BSONError('Bad BSON Document: illegal CString'); + // Return the C string + const regExpOptions = ByteUtils.toUTF8(buffer, index, i, false); + index = i + 1; + + // For each option add the corresponding one for javascript + const optionsArray = new Array(regExpOptions.length); + + // Parse options + for (i = 0; i < regExpOptions.length; i++) { + switch (regExpOptions[i]) { + case 'm': + optionsArray[i] = 'm'; + break; + case 's': + optionsArray[i] = 'g'; + break; + case 'i': + optionsArray[i] = 'i'; + break; + } + } + + value = new RegExp(source, optionsArray.join('')); + } else if (elementType === constants.BSON_DATA_REGEXP && bsonRegExp === true) { + // Get the start search index + i = index; + // Locate the end of the c string + while (buffer[i] !== 0x00 && i < buffer.length) { + i++; + } + // If are at the end of the buffer there is a problem with the document + if (i >= buffer.length) throw new BSONError('Bad BSON Document: illegal CString'); + // Return the C string + const source = ByteUtils.toUTF8(buffer, index, i, false); + index = i + 1; + + // Get the start search index + i = index; + // Locate the end of the c string + while (buffer[i] !== 0x00 && i < buffer.length) { + i++; + } + // If are at the end of the buffer there is a problem with the document + if (i >= buffer.length) throw new BSONError('Bad BSON Document: illegal CString'); + // Return the C string + const regExpOptions = ByteUtils.toUTF8(buffer, index, i, false); + index = i + 1; + + // Set the object + value = new BSONRegExp(source, regExpOptions); + } else if (elementType === constants.BSON_DATA_SYMBOL) { + const stringSize = NumberUtils.getInt32LE(buffer, index); + index += 4; + if ( + stringSize <= 0 || + stringSize > buffer.length - index || + buffer[index + stringSize - 1] !== 0 + ) { + throw new BSONError('bad string length in bson'); + } + const symbol = ByteUtils.toUTF8(buffer, index, index + stringSize - 1, shouldValidateKey); + value = promoteValues ? symbol : new BSONSymbol(symbol); + index = index + stringSize; + } else if (elementType === constants.BSON_DATA_TIMESTAMP) { + value = new Timestamp({ + i: NumberUtils.getUint32LE(buffer, index), + t: NumberUtils.getUint32LE(buffer, index + 4) + }); + index += 8; + } else if (elementType === constants.BSON_DATA_MIN_KEY) { + value = new MinKey(); + } else if (elementType === constants.BSON_DATA_MAX_KEY) { + value = new MaxKey(); + } else if (elementType === constants.BSON_DATA_CODE) { + const stringSize = NumberUtils.getInt32LE(buffer, index); + index += 4; + if ( + stringSize <= 0 || + stringSize > buffer.length - index || + buffer[index + stringSize - 1] !== 0 + ) { + throw new BSONError('bad string length in bson'); + } + const functionString = ByteUtils.toUTF8( + buffer, + index, + index + stringSize - 1, + shouldValidateKey + ); + + value = new Code(functionString); + + // Update parse index position + index = index + stringSize; + } else if (elementType === constants.BSON_DATA_CODE_W_SCOPE) { + const totalSize = NumberUtils.getInt32LE(buffer, index); + index += 4; + + // Element cannot be shorter than totalSize + stringSize + documentSize + terminator + if (totalSize < 4 + 4 + 4 + 1) { + throw new BSONError('code_w_scope total size shorter minimum expected length'); + } + + // Get the code string size + const stringSize = NumberUtils.getInt32LE(buffer, index); + index += 4; + // Check if we have a valid string + if ( + stringSize <= 0 || + stringSize > buffer.length - index || + buffer[index + stringSize - 1] !== 0 + ) { + throw new BSONError('bad string length in bson'); + } + + // Javascript function + const functionString = ByteUtils.toUTF8( + buffer, + index, + index + stringSize - 1, + shouldValidateKey + ); + // Update parse index position + index = index + stringSize; + // Parse the element + const _index = index; + // Decode the size of the object document + const objectSize = NumberUtils.getInt32LE(buffer, index); + + if (objectSize < 5 || objectSize > buffer.length - index) + throw new BSONError('bad scope document size in code_w_scope'); + + // Check if field length is too short + if (totalSize < 4 + 4 + objectSize + stringSize) { + throw new BSONError('code_w_scope total size is too short, truncating scope'); + } + + // Check if totalSize field is too long + if (totalSize > 4 + 4 + objectSize + stringSize) { + throw new BSONError('code_w_scope total size is too long, clips outer document'); + } + + isDeferredValue = true; + const scopeFrame: NestedParsingFrame = { + holdingDocument: {}, + elementType: constants.BSON_DATA_CODE_W_SCOPE, + propertyName: name, + functionString: functionString, + lastIndex: _index + objectSize, + isArray: false, + arrayIndex: 0, + raw: false, + isPossibleDBRef: null, + validationSetting: shouldValidateKey, + prev: currentFrame + }; + currentFrame = scopeFrame; + currentDest = scopeFrame.holdingDocument; + currentIsArray = false; + index = index + 4; // move index past the size of the object, the rest of the object will be parsed in subsequent iterations of this loop + } else if (elementType === constants.BSON_DATA_DBPOINTER) { + // Get the code string size + const stringSize = NumberUtils.getInt32LE(buffer, index); + index += 4; + // Check if we have a valid string + if ( + stringSize <= 0 || + stringSize > buffer.length - index || + buffer[index + stringSize - 1] !== 0 + ) + throw new BSONError('bad string length in bson'); + // Namespace + const namespace = ByteUtils.toUTF8(buffer, index, index + stringSize - 1, shouldValidateKey); + // Update parse index position + index = index + stringSize; + + // Read the oid + const oidBuffer = ByteUtils.allocateUnsafe(12); + for (let i = 0; i < 12; i++) oidBuffer[i] = buffer[index + i]; + const oid = new ObjectId(oidBuffer); + + // Update the index + index = index + 12; + + // Upgrade to DBRef type + value = new DBRef(namespace, oid); + } else { + throw new BSONError( + `Detected unknown BSON type ${elementType.toString(16)} for fieldname "${name}"` + ); + } + + // If we have the value, set it on the target object + if (!isDeferredValue) { + assignValue(currentDest, name, value); + } + } + + // Check if we have any frames left on the stack, if we do then we had a malformed document + if (currentFrame !== null) { + throw new BSONError('corrupted bson, more objects expected based on the current document size'); + } + const object = rootObject; + + // Check if the deserialization was against a valid array/object + if (size !== index - startIndex) { + if (isArray) throw new BSONError('corrupt array bson'); + throw new BSONError('corrupt object bson'); + } + + // if we did not find "$ref", "$id", "$db", or found an extraneous $key, don't make a DBRef + if (!isPossibleDBRef) return object; + + // If the object is DBRef-like, create a new DBRef instance + return toPotentialDbRef(object); +} diff --git a/gateway/node_modules/bson/src/parser/on_demand/index.ts b/gateway/node_modules/bson/src/parser/on_demand/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..f099c11528b3ce438779985249e4ca84e2a36b16 --- /dev/null +++ b/gateway/node_modules/bson/src/parser/on_demand/index.ts @@ -0,0 +1,32 @@ +import { ByteUtils } from '../../utils/byte_utils'; +import { NumberUtils } from '../../utils/number_utils'; +import { type BSONElement, parseToElements } from './parse_to_elements'; +/** + * @experimental + * @public + * + * A new set of BSON APIs that are currently experimental and not intended for production use. + */ +export type OnDemand = { + parseToElements: (this: void, bytes: Uint8Array, startOffset?: number) => Iterable; + // Types + BSONElement: BSONElement; + + // Utils + ByteUtils: ByteUtils; + NumberUtils: NumberUtils; +}; + +/** + * @experimental + * @public + */ +const onDemand: OnDemand = Object.create(null); + +onDemand.parseToElements = parseToElements; +onDemand.ByteUtils = ByteUtils; +onDemand.NumberUtils = NumberUtils; + +Object.freeze(onDemand); + +export { onDemand }; diff --git a/gateway/node_modules/bson/src/parser/on_demand/parse_to_elements.ts b/gateway/node_modules/bson/src/parser/on_demand/parse_to_elements.ts new file mode 100644 index 0000000000000000000000000000000000000000..cc5366aad88940798f2e1c78a7b3dedc12c3b438 --- /dev/null +++ b/gateway/node_modules/bson/src/parser/on_demand/parse_to_elements.ts @@ -0,0 +1,190 @@ +import { BSONOffsetError } from '../../error'; +import { NumberUtils } from '../../utils/number_utils'; + +/** + * @internal + * + * @remarks + * - This enum is const so the code we produce will inline the numbers + * - `minKey` is set to 255 so unsigned comparisons succeed + * - Modify with caution, double check the bundle contains literals + */ +const BSONElementType = { + double: 1, + string: 2, + object: 3, + array: 4, + binData: 5, + undefined: 6, + objectId: 7, + bool: 8, + date: 9, + null: 10, + regex: 11, + dbPointer: 12, + javascript: 13, + symbol: 14, + javascriptWithScope: 15, + int: 16, + timestamp: 17, + long: 18, + decimal: 19, + minKey: 255, + maxKey: 127 +} as const; + +type BSONElementType = (typeof BSONElementType)[keyof typeof BSONElementType]; + +/** + * @public + * @experimental + */ +export type BSONElement = [ + type: number, + nameOffset: number, + nameLength: number, + offset: number, + length: number +]; + +function getSize(source: Uint8Array, offset: number) { + try { + return NumberUtils.getNonnegativeInt32LE(source, offset); + } catch (cause) { + throw new BSONOffsetError('BSON size cannot be negative', offset, { cause }); + } +} + +/** + * Searches for null terminator of a BSON element's value (Never the document null terminator) + * **Does not** bounds check since this should **ONLY** be used within parseToElements which has asserted that `bytes` ends with a `0x00`. + * So this will at most iterate to the document's terminator and error if that is the offset reached. + */ +function findNull(bytes: Uint8Array, offset: number): number { + let nullTerminatorOffset = offset; + + for (; bytes[nullTerminatorOffset] !== 0x00; nullTerminatorOffset++); + + if (nullTerminatorOffset === bytes.length - 1) { + // We reached the null terminator of the document, not a value's + throw new BSONOffsetError('Null terminator not found', offset); + } + + return nullTerminatorOffset; +} + +/** + * @public + * @experimental + */ +export function parseToElements( + bytes: Uint8Array, + startOffset: number | null = 0 +): Iterable { + startOffset ??= 0; + + if (bytes.length < 5) { + throw new BSONOffsetError( + `Input must be at least 5 bytes, got ${bytes.length} bytes`, + startOffset + ); + } + + const documentSize = getSize(bytes, startOffset); + + if (documentSize > bytes.length - startOffset) { + throw new BSONOffsetError( + `Parsed documentSize (${documentSize} bytes) does not match input length (${bytes.length} bytes)`, + startOffset + ); + } + + if (bytes[startOffset + documentSize - 1] !== 0x00) { + throw new BSONOffsetError('BSON documents must end in 0x00', startOffset + documentSize); + } + + const elements: BSONElement[] = []; + let offset = startOffset + 4; + + while (offset <= documentSize + startOffset) { + const type = bytes[offset]; + offset += 1; + + if (type === 0) { + if (offset - startOffset !== documentSize) { + throw new BSONOffsetError(`Invalid 0x00 type byte`, offset); + } + break; + } + + const nameOffset = offset; + const nameLength = findNull(bytes, offset) - nameOffset; + offset += nameLength + 1; + + let length: number; + + if ( + type === BSONElementType.double || + type === BSONElementType.long || + type === BSONElementType.date || + type === BSONElementType.timestamp + ) { + length = 8; + } else if (type === BSONElementType.int) { + length = 4; + } else if (type === BSONElementType.objectId) { + length = 12; + } else if (type === BSONElementType.decimal) { + length = 16; + } else if (type === BSONElementType.bool) { + length = 1; + } else if ( + type === BSONElementType.null || + type === BSONElementType.undefined || + type === BSONElementType.maxKey || + type === BSONElementType.minKey + ) { + length = 0; + } + // Needs a size calculation + else if (type === BSONElementType.regex) { + length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset; + } else if ( + type === BSONElementType.object || + type === BSONElementType.array || + type === BSONElementType.javascriptWithScope + ) { + length = getSize(bytes, offset); + } else if ( + type === BSONElementType.string || + type === BSONElementType.binData || + type === BSONElementType.dbPointer || + type === BSONElementType.javascript || + type === BSONElementType.symbol + ) { + length = getSize(bytes, offset) + 4; + if (type === BSONElementType.binData) { + // binary subtype + length += 1; + } + if (type === BSONElementType.dbPointer) { + // dbPointer's objectId + length += 12; + } + } else { + throw new BSONOffsetError( + `Invalid 0x${type.toString(16).padStart(2, '0')} type byte`, + offset + ); + } + + if (length > documentSize) { + throw new BSONOffsetError('value reports length larger than document', offset); + } + + elements.push([type, nameOffset, nameLength, offset, length]); + offset += length; + } + + return elements; +} diff --git a/gateway/node_modules/bson/src/parser/serializer.ts b/gateway/node_modules/bson/src/parser/serializer.ts new file mode 100644 index 0000000000000000000000000000000000000000..7338bd9aa6fd2869ece31f9c2a832ebe6e30312c --- /dev/null +++ b/gateway/node_modules/bson/src/parser/serializer.ts @@ -0,0 +1,748 @@ +import { Binary, validateBinaryVector } from '../binary'; +import type { BSONSymbol, DBRef, Document, MaxKey } from '../bson'; +import { bsonType } from '../bson_value'; +import type { Code } from '../code'; +import * as constants from '../constants'; +import type { Decimal128 } from '../decimal128'; +import type { Double } from '../double'; +import { BSONError, BSONVersionError } from '../error'; +import type { Int32 } from '../int_32'; +import { Long } from '../long'; +import type { MinKey } from '../min_key'; +import type { ObjectId } from '../objectid'; +import type { BSONRegExp } from '../regexp'; +import { ByteUtils } from '../utils/byte_utils'; +import { NumberUtils } from '../utils/number_utils'; +import { isAnyArrayBuffer, isDate, isMap, isRegExp, isUint8Array } from './utils'; + +/** @public */ +export interface SerializeOptions { + /** + * the serializer will check if keys are valid. + * @defaultValue `false` + */ + checkKeys?: boolean; + /** + * serialize the javascript functions + * @defaultValue `false` + */ + serializeFunctions?: boolean; + /** + * serialize will not emit undefined fields + * note that the driver sets this to `false` + * @defaultValue `true` + */ + ignoreUndefined?: boolean; + /** @internal Resize internal buffer */ + minInternalBufferSize?: number; + /** + * the index in the buffer where we wish to start serializing into + * @defaultValue `0` + */ + index?: number; +} + +const regexp = /\x00/; // eslint-disable-line no-control-regex +const ignoreKeys = new Set(['$db', '$ref', '$id', '$clusterTime']); + +function serializeString(buffer: Uint8Array, key: string, value: string, index: number) { + // Encode String type + buffer[index++] = constants.BSON_DATA_STRING; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Write the string + const size = ByteUtils.encodeUTF8Into(buffer, value, index + 4); + // Write the size of the string to buffer + NumberUtils.setInt32LE(buffer, index, size + 1); + // Update index + index = index + 4 + size; + // Write zero + buffer[index++] = 0; + return index; +} + +function serializeNumber(buffer: Uint8Array, key: string, value: number, index: number) { + const isNegativeZero = Object.is(value, -0); + + const type = + !isNegativeZero && + Number.isSafeInteger(value) && + value <= constants.BSON_INT32_MAX && + value >= constants.BSON_INT32_MIN + ? constants.BSON_DATA_INT + : constants.BSON_DATA_NUMBER; + + buffer[index++] = type; + + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + index = index + numberOfWrittenBytes; + buffer[index++] = 0x00; + + if (type === constants.BSON_DATA_INT) { + index += NumberUtils.setInt32LE(buffer, index, value); + } else { + index += NumberUtils.setFloat64LE(buffer, index, value); + } + + return index; +} + +function serializeBigInt(buffer: Uint8Array, key: string, value: bigint, index: number) { + buffer[index++] = constants.BSON_DATA_LONG; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index += numberOfWrittenBytes; + buffer[index++] = 0; + + index += NumberUtils.setBigInt64LE(buffer, index, value); + + return index; +} + +function serializeNull(buffer: Uint8Array, key: string, _: unknown, index: number) { + // Set long type + buffer[index++] = constants.BSON_DATA_NULL; + + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + return index; +} + +function serializeBoolean(buffer: Uint8Array, key: string, value: boolean, index: number) { + // Write the type + buffer[index++] = constants.BSON_DATA_BOOLEAN; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Encode the boolean value + buffer[index++] = value ? 1 : 0; + return index; +} + +function serializeDate(buffer: Uint8Array, key: string, value: Date, index: number) { + // Write the type + buffer[index++] = constants.BSON_DATA_DATE; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + + // Write the date + const dateInMilis = Long.fromNumber(value.getTime()); + const lowBits = dateInMilis.getLowBits(); + const highBits = dateInMilis.getHighBits(); + // Encode low bits + index += NumberUtils.setInt32LE(buffer, index, lowBits); + // Encode high bits + index += NumberUtils.setInt32LE(buffer, index, highBits); + return index; +} + +function serializeRegExp(buffer: Uint8Array, key: string, value: RegExp, index: number) { + // Write the type + buffer[index++] = constants.BSON_DATA_REGEXP; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + if (value.source && value.source.match(regexp) != null) { + throw new BSONError('value ' + value.source + ' must not contain null bytes'); + } + // Adjust the index + index = index + ByteUtils.encodeUTF8Into(buffer, value.source, index); + // Write zero + buffer[index++] = 0x00; + // Write the parameters + if (value.ignoreCase) buffer[index++] = 0x69; // i + if (value.global) buffer[index++] = 0x73; // s + if (value.multiline) buffer[index++] = 0x6d; // m + + // Add ending zero + buffer[index++] = 0x00; + return index; +} + +function serializeBSONRegExp(buffer: Uint8Array, key: string, value: BSONRegExp, index: number) { + // Write the type + buffer[index++] = constants.BSON_DATA_REGEXP; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + + // Check the pattern for 0 bytes + if (value.pattern.match(regexp) != null) { + // The BSON spec doesn't allow keys with null bytes because keys are + // null-terminated. + throw new BSONError('pattern ' + value.pattern + ' must not contain null bytes'); + } + + // Adjust the index + index = index + ByteUtils.encodeUTF8Into(buffer, value.pattern, index); + // Write zero + buffer[index++] = 0x00; + // Write the options + const sortedOptions = value.options.split('').sort().join(''); + index = index + ByteUtils.encodeUTF8Into(buffer, sortedOptions, index); + // Add ending zero + buffer[index++] = 0x00; + return index; +} + +function serializeMinMax(buffer: Uint8Array, key: string, value: MinKey | MaxKey, index: number) { + // Write the type of either min or max key + if (value === null) { + buffer[index++] = constants.BSON_DATA_NULL; + } else if (value[bsonType] === 'MinKey') { + buffer[index++] = constants.BSON_DATA_MIN_KEY; + } else { + buffer[index++] = constants.BSON_DATA_MAX_KEY; + } + + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + return index; +} + +function serializeObjectId(buffer: Uint8Array, key: string, value: ObjectId, index: number) { + // Write the type + buffer[index++] = constants.BSON_DATA_OID; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + + index += value.serializeInto(buffer, index); + + // Adjust index + return index; +} + +function serializeBuffer(buffer: Uint8Array, key: string, value: Uint8Array, index: number) { + // Write the type + buffer[index++] = constants.BSON_DATA_BINARY; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Get size of the buffer (current write point) + const size = value.length; + // Write the size of the string to buffer + index += NumberUtils.setInt32LE(buffer, index, size); + // Write the default subtype + buffer[index++] = constants.BSON_BINARY_SUBTYPE_DEFAULT; + // Copy the content form the binary field to the buffer + if (size <= 16) { + for (let i = 0; i < size; i++) buffer[index + i] = value[i]; + } else { + buffer.set(value, index); + } + // Adjust the index + index = index + size; + return index; +} + +function serializeDecimal128(buffer: Uint8Array, key: string, value: Decimal128, index: number) { + buffer[index++] = constants.BSON_DATA_DECIMAL128; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Write the data from the value + for (let i = 0; i < 16; i++) buffer[index + i] = value.bytes[i]; + return index + 16; +} + +function serializeLong(buffer: Uint8Array, key: string, value: Long, index: number) { + // Write the type + buffer[index++] = + value[bsonType] === 'Long' ? constants.BSON_DATA_LONG : constants.BSON_DATA_TIMESTAMP; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Write the date + const lowBits = value.getLowBits(); + const highBits = value.getHighBits(); + // Encode low bits + index += NumberUtils.setInt32LE(buffer, index, lowBits); + // Encode high bits + index += NumberUtils.setInt32LE(buffer, index, highBits); + return index; +} + +function serializeInt32(buffer: Uint8Array, key: string, value: Int32 | number, index: number) { + value = value.valueOf(); + // Set int type 32 bits or less + buffer[index++] = constants.BSON_DATA_INT; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Write the int value + index += NumberUtils.setInt32LE(buffer, index, value); + return index; +} + +function serializeDouble(buffer: Uint8Array, key: string, value: Double, index: number) { + // Encode as double + buffer[index++] = constants.BSON_DATA_NUMBER; + + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + + // Write float + index += NumberUtils.setFloat64LE(buffer, index, value.value); + + return index; +} + +function serializeFunction(buffer: Uint8Array, key: string, value: Function, index: number) { + buffer[index++] = constants.BSON_DATA_CODE; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Function string + const functionString = value.toString(); + + // Write the string + const size = ByteUtils.encodeUTF8Into(buffer, functionString, index + 4) + 1; + // Write the size of the string to buffer + NumberUtils.setInt32LE(buffer, index, size); + // Update index + index = index + 4 + size - 1; + // Write zero + buffer[index++] = 0; + return index; +} + +function serializeBinary(buffer: Uint8Array, key: string, value: Binary, index: number) { + // Write the type + buffer[index++] = constants.BSON_DATA_BINARY; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Extract the buffer + const data = value.buffer; + // Calculate size + let size = value.position; + // Add the deprecated 02 type 4 bytes of size to total + if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) size = size + 4; + // Write the size of the string to buffer + index += NumberUtils.setInt32LE(buffer, index, size); + // Write the subtype to the buffer + buffer[index++] = value.sub_type; + + // If we have binary type 2 the 4 first bytes are the size + if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) { + size = size - 4; + index += NumberUtils.setInt32LE(buffer, index, size); + } + + if (value.sub_type === Binary.SUBTYPE_VECTOR) { + validateBinaryVector(value); + } + + if (size <= 16) { + for (let i = 0; i < size; i++) buffer[index + i] = data[i]; + } else { + buffer.set(data, index); + } + // Adjust the index + index = index + value.position; + return index; +} + +function serializeSymbol(buffer: Uint8Array, key: string, value: BSONSymbol, index: number) { + // Write the type + buffer[index++] = constants.BSON_DATA_SYMBOL; + // Number of written bytes + const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); + // Encode the name + index = index + numberOfWrittenBytes; + buffer[index++] = 0; + // Write the string + const size = ByteUtils.encodeUTF8Into(buffer, value.value, index + 4) + 1; + // Write the size of the string to buffer + NumberUtils.setInt32LE(buffer, index, size); + // Update index + index = index + 4 + size - 1; + // Write zero + buffer[index++] = 0; + return index; +} + +interface SerializationFrame { + /** Original object passed to this frame — used for cycle-detection (path.delete). */ + sourceObject: Document; + /** True when serializing a BSON array; affects key format and type byte. */ + isArray: boolean; + /** Buffer offset where this object's 4-byte size field will be written. */ + objectSizeIndex: number; + /** Buffer offset for code-with-scope wrapper size, or null if not applicable. */ + codeSizeIndex: number | null; + /** + * Object being iterated. For plain objects this may be the toBSON() result of + * sourceObject; for arrays and Maps it equals sourceObject. + */ + iterTarget: Document; + /** + * Pre-computed Object.keys() of iterTarget for plain objects. + * Null for arrays (use keyIndex as numeric index) and Maps (use mapIterator). + */ + keys: string[] | null; + /** Next index into keys[] for plain objects, or next array index for arrays. */ + keyIndex: number; + /** Active iterator for Map objects; null for arrays and plain objects. */ + mapIterator: IterableIterator<[unknown, unknown]> | null; + /** The enclosing frame, or null at the root. The stack is a linked list via this field. */ + prev: SerializationFrame | null; + /** Whether to validate keys for this frame's document (may differ from caller, e.g. DBRef uses false). */ + checkKeys: boolean; + /** Whether undefined values are skipped (may differ from caller, e.g. DBRef uses true). */ + ignoreUndefined: boolean; +} + +function makeFrame( + sourceObject: Document, + objectSizeIndex: number, + codeSizeIndex: number | null, + prev: SerializationFrame | null, + checkKeys: boolean, + ignoreUndefined: boolean +): SerializationFrame { + if (Array.isArray(sourceObject)) { + return { + sourceObject, + isArray: true, + objectSizeIndex, + codeSizeIndex, + iterTarget: sourceObject, + keys: null, + keyIndex: 0, + mapIterator: null, + prev, + checkKeys, + ignoreUndefined + }; + } + if (sourceObject instanceof Map || isMap(sourceObject)) { + return { + sourceObject, + isArray: false, + objectSizeIndex, + codeSizeIndex, + iterTarget: sourceObject, + keys: null, + keyIndex: 0, + mapIterator: (sourceObject as Map).entries(), + prev, + checkKeys, + ignoreUndefined + }; + } + // Plain object: call toBSON() if defined to obtain the object to iterate. + let target: Document = sourceObject; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + if (typeof (target as any)?.toBSON === 'function') { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + target = (target as any).toBSON() as Document; + if (target != null && typeof target !== 'object') { + throw new BSONError('toBSON function did not return an object'); + } + } + return { + sourceObject, + isArray: false, + objectSizeIndex, + codeSizeIndex, + iterTarget: target, + keys: Object.keys(target as object), + keyIndex: 0, + mapIterator: null, + prev, + checkKeys, + ignoreUndefined + }; +} + +export function serializeInto( + buffer: Uint8Array, + object: Document, + checkKeys: boolean, + startingIndex: number, + serializeFunctions: boolean, + ignoreUndefined: boolean, + path: Set | null +): number { + if (path == null) { + // We are at the root input + if (object == null) { + // ONLY the root should turn into an empty document + // BSON Empty document has a size of 5 (LE) + buffer[0] = 0x05; + buffer[1] = 0x00; + buffer[2] = 0x00; + buffer[3] = 0x00; + // All documents end with null terminator + buffer[4] = 0x00; + return 5; + } + + if (Array.isArray(object)) { + throw new BSONError('serialize does not support an array as the root input'); + } + if (typeof object !== 'object') { + throw new BSONError('serialize does not support non-object as the root input'); + } else if ('_bsontype' in object && typeof object._bsontype === 'string') { + throw new BSONError(`BSON types cannot be serialized as a document`); + } else if ( + isDate(object) || + isRegExp(object) || + isUint8Array(object) || + isAnyArrayBuffer(object) + ) { + throw new BSONError(`date, regexp, typedarray, and arraybuffer cannot be BSON documents`); + } + + path = new Set(); + } + + path.add(object); + + let currentFrame: SerializationFrame | null = makeFrame( + object, + startingIndex, + null, + null, + checkKeys, + ignoreUndefined + ); + let index = startingIndex + 4; + + while (currentFrame !== null) { + const frame: SerializationFrame = currentFrame; + + // Advance to the next key-value pair, or finalize the frame if exhausted. + let key: string; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let value: any; + if (frame.mapIterator !== null) { + const next = frame.mapIterator.next(); + if (next.done) { + buffer[index++] = 0x00; + NumberUtils.setInt32LE(buffer, frame.objectSizeIndex, index - frame.objectSizeIndex); + if (frame.codeSizeIndex !== null) { + NumberUtils.setInt32LE(buffer, frame.codeSizeIndex, index - frame.codeSizeIndex); + } + path.delete(frame.sourceObject); + currentFrame = frame.prev; + continue; + } + key = next.value[0] as string; + value = next.value[1]; + } else if (frame.keys !== null) { + if (frame.keyIndex >= frame.keys.length) { + buffer[index++] = 0x00; + NumberUtils.setInt32LE(buffer, frame.objectSizeIndex, index - frame.objectSizeIndex); + if (frame.codeSizeIndex !== null) { + NumberUtils.setInt32LE(buffer, frame.codeSizeIndex, index - frame.codeSizeIndex); + } + path.delete(frame.sourceObject); + currentFrame = frame.prev; + continue; + } + key = frame.keys[frame.keyIndex++]; + value = (frame.iterTarget as Record)[key]; + } else { + // Array: use keyIndex as the numeric index. + const arr = frame.iterTarget as unknown[]; + if (frame.keyIndex >= arr.length) { + buffer[index++] = 0x00; + NumberUtils.setInt32LE(buffer, frame.objectSizeIndex, index - frame.objectSizeIndex); + if (frame.codeSizeIndex !== null) { + NumberUtils.setInt32LE(buffer, frame.codeSizeIndex, index - frame.codeSizeIndex); + } + path.delete(frame.sourceObject); + currentFrame = frame.prev; + continue; + } + const i = frame.keyIndex++; + key = String(i); + value = arr[i]; + } + + if (typeof value?.toBSON === 'function') { + value = value.toBSON(); + } + + if (!frame.isArray && typeof key === 'string' && !(key[0] === '$' && ignoreKeys.has(key))) { + if (regexp.test(key)) { + throw new BSONError('key ' + key + ' must not contain null bytes'); + } + if (frame.checkKeys) { + if ('$' === key[0]) { + throw new BSONError('key ' + key + " must not start with '$'"); + } else if (key.includes('.')) { + throw new BSONError('key ' + key + " must not contain '.'"); + } + } + } + + const type = typeof value; + + if (value === undefined) { + if (frame.isArray || frame.ignoreUndefined === false) { + index = serializeNull(buffer, key, value, index); + } + } else if (value === null) { + index = serializeNull(buffer, key, value, index); + } else if (type === 'string') { + index = serializeString(buffer, key, value, index); + } else if (type === 'number') { + index = serializeNumber(buffer, key, value, index); + } else if (type === 'bigint') { + index = serializeBigInt(buffer, key, value, index); + } else if (type === 'boolean') { + index = serializeBoolean(buffer, key, value, index); + } else if (type === 'object' && value._bsontype == null) { + if (value instanceof Date || isDate(value)) { + index = serializeDate(buffer, key, value, index); + } else if (value instanceof Uint8Array || isUint8Array(value)) { + index = serializeBuffer(buffer, key, value, index); + } else if (value instanceof RegExp || isRegExp(value)) { + index = serializeRegExp(buffer, key, value, index); + } else { + if (path.has(value)) { + throw new BSONError('Cannot convert circular structure to BSON'); + } + const nestedIsArray = Array.isArray(value); + buffer[index++] = nestedIsArray ? constants.BSON_DATA_ARRAY : constants.BSON_DATA_OBJECT; + index += ByteUtils.encodeUTF8Into(buffer, key, index); + buffer[index++] = 0x00; + const nestedStartIndex = index; + path.add(value); + currentFrame = makeFrame( + value, + nestedStartIndex, + null, + frame, + frame.checkKeys, + frame.ignoreUndefined + ); + index += 4; + } + } else if (type === 'object') { + if (value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION) { + throw new BSONVersionError(); + } + const tag = value[bsonType]; + if (tag === 'ObjectId') { + index = serializeObjectId(buffer, key, value, index); + } else if (tag === 'Decimal128') { + index = serializeDecimal128(buffer, key, value, index); + } else if (tag === 'Long' || tag === 'Timestamp') { + index = serializeLong(buffer, key, value, index); + } else if (tag === 'Double') { + index = serializeDouble(buffer, key, value, index); + } else if (tag === 'Code') { + const codeValue = value as Code; + if (codeValue.scope && typeof codeValue.scope === 'object') { + buffer[index++] = constants.BSON_DATA_CODE_W_SCOPE; + index += ByteUtils.encodeUTF8Into(buffer, key, index); + buffer[index++] = 0x00; + const codeTotalSizeIndex = index; + index += 4; + const functionString = codeValue.code; + const codeSize = ByteUtils.encodeUTF8Into(buffer, functionString, index + 4) + 1; + NumberUtils.setInt32LE(buffer, index, codeSize); + buffer[index + 4 + codeSize - 1] = 0; + index = index + codeSize + 4; + const scope = codeValue.scope; + if (path.has(scope)) { + throw new BSONError('Cannot convert circular structure to BSON'); + } + path.add(scope); + currentFrame = makeFrame( + scope, + index, + codeTotalSizeIndex, + frame, + frame.checkKeys, + frame.ignoreUndefined + ); + index += 4; + } else { + buffer[index++] = constants.BSON_DATA_CODE; + index += ByteUtils.encodeUTF8Into(buffer, key, index); + buffer[index++] = 0x00; + const functionString = codeValue.code.toString(); + const size = ByteUtils.encodeUTF8Into(buffer, functionString, index + 4) + 1; + NumberUtils.setInt32LE(buffer, index, size); + index = index + 4 + size - 1; + buffer[index++] = 0; + } + } else if (tag === 'Binary') { + index = serializeBinary(buffer, key, value, index); + } else if (tag === 'BSONSymbol') { + index = serializeSymbol(buffer, key, value, index); + } else if (tag === 'DBRef') { + const dbref = value as DBRef; + const orderedValues: Document = Object.assign( + { $ref: dbref.collection, $id: dbref.oid }, + dbref.db != null ? { $db: dbref.db } : null, + dbref.fields + ); + buffer[index++] = constants.BSON_DATA_OBJECT; + index += ByteUtils.encodeUTF8Into(buffer, key, index); + buffer[index++] = 0x00; + path.add(orderedValues); + currentFrame = makeFrame(orderedValues, index, null, frame, false, true); + index += 4; + } else if (tag === 'BSONRegExp') { + index = serializeBSONRegExp(buffer, key, value, index); + } else if (tag === 'Int32') { + index = serializeInt32(buffer, key, value, index); + } else if (tag === 'MinKey' || tag === 'MaxKey') { + index = serializeMinMax(buffer, key, value, index); + } else if (typeof value._bsontype !== 'undefined') { + throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`); + } + } else if (type === 'function' && serializeFunctions) { + index = serializeFunction(buffer, key, value, index); + } + } + + return index; +} diff --git a/gateway/node_modules/bson/src/parser/utils.ts b/gateway/node_modules/bson/src/parser/utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..2e383991057c000c5140f12997178942ca0e1289 --- /dev/null +++ b/gateway/node_modules/bson/src/parser/utils.ts @@ -0,0 +1,69 @@ +const TypedArrayPrototypeGetSymbolToStringTag = (() => { + // Type check system lovingly referenced from: + // https://github.com/nodejs/node/blob/7450332339ed40481f470df2a3014e2ec355d8d8/lib/internal/util/types.js#L13-L15 + // eslint-disable-next-line @typescript-eslint/unbound-method -- the intention is to call this method with a bound value + const g = Object.getOwnPropertyDescriptor( + Object.getPrototypeOf(Uint8Array.prototype), + Symbol.toStringTag + )!.get!; + + return (value: unknown) => g.call(value); +})(); + +export function isUint8Array(value: unknown): value is Uint8Array { + return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8Array'; +} + +export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer { + return ( + typeof value === 'object' && + value != null && + Symbol.toStringTag in value && + (value[Symbol.toStringTag] === 'ArrayBuffer' || + value[Symbol.toStringTag] === 'SharedArrayBuffer') + ); +} + +export function isRegExp(regexp: unknown): regexp is RegExp { + return regexp instanceof RegExp || Object.prototype.toString.call(regexp) === '[object RegExp]'; +} + +export function isMap(value: unknown): value is Map { + return ( + typeof value === 'object' && + value != null && + Symbol.toStringTag in value && + value[Symbol.toStringTag] === 'Map' + ); +} + +export function isDate(date: unknown): date is Date { + return date instanceof Date || Object.prototype.toString.call(date) === '[object Date]'; +} + +export type InspectFn = (x: unknown, options?: unknown) => string; +export function defaultInspect(x: unknown, _options?: unknown): string { + return JSON.stringify(x, (k: string, v: unknown) => { + if (typeof v === 'bigint') { + return { $numberLong: `${v}` }; + } else if (isMap(v)) { + return Object.fromEntries(v); + } + return v; + }); +} + +/** @internal */ +type StylizeFunction = (x: string, style: string) => string; +/** @internal */ +export function getStylizeFunction(options?: unknown): StylizeFunction | undefined { + const stylizeExists = + options != null && + typeof options === 'object' && + 'stylize' in options && + typeof options.stylize === 'function'; + + if (stylizeExists) { + return options.stylize as StylizeFunction; + } +} diff --git a/gateway/node_modules/bson/src/regexp.ts b/gateway/node_modules/bson/src/regexp.ts new file mode 100644 index 0000000000000000000000000000000000000000..e401a290937724338a5ff35c7d270471e5348b10 --- /dev/null +++ b/gateway/node_modules/bson/src/regexp.ts @@ -0,0 +1,114 @@ +import { BSONValue } from './bson_value'; +import { BSONError } from './error'; +import type { EJSONOptions } from './extended_json'; +import { type InspectFn, defaultInspect, getStylizeFunction } from './parser/utils'; + +function alphabetize(str: string): string { + return str.split('').sort().join(''); +} + +/** @public */ +export interface BSONRegExpExtendedLegacy { + $regex: string | BSONRegExp; + $options: string; +} + +/** @public */ +export interface BSONRegExpExtended { + $regularExpression: { + pattern: string; + options: string; + }; +} + +/** + * A class representation of the BSON RegExp type. + * @public + * @category BSONType + */ +export class BSONRegExp extends BSONValue { + get _bsontype(): 'BSONRegExp' { + return 'BSONRegExp'; + } + + pattern!: string; + options!: string; + /** + * @param pattern - The regular expression pattern to match + * @param options - The regular expression options + */ + constructor(pattern: string, options?: string) { + super(); + this.pattern = pattern; + this.options = alphabetize(options ?? ''); + + if (this.pattern.indexOf('\x00') !== -1) { + throw new BSONError( + `BSON Regex patterns cannot contain null bytes, found: ${JSON.stringify(this.pattern)}` + ); + } + if (this.options.indexOf('\x00') !== -1) { + throw new BSONError( + `BSON Regex options cannot contain null bytes, found: ${JSON.stringify(this.options)}` + ); + } + + // Validate options + for (let i = 0; i < this.options.length; i++) { + if ( + !( + this.options[i] === 'i' || + this.options[i] === 'm' || + this.options[i] === 'x' || + this.options[i] === 'l' || + this.options[i] === 's' || + this.options[i] === 'u' + ) + ) { + throw new BSONError(`The regular expression option [${this.options[i]}] is not supported`); + } + } + } + + static parseOptions(options?: string): string { + return options ? options.split('').sort().join('') : ''; + } + + /** @internal */ + toExtendedJSON(options?: EJSONOptions): BSONRegExpExtendedLegacy | BSONRegExpExtended { + options = options || {}; + if (options.legacy) { + return { $regex: this.pattern, $options: this.options }; + } + return { $regularExpression: { pattern: this.pattern, options: this.options } }; + } + + /** @internal */ + static fromExtendedJSON(doc: BSONRegExpExtendedLegacy | BSONRegExpExtended): BSONRegExp { + if ('$regex' in doc) { + if (typeof doc.$regex !== 'string') { + // This is for $regex query operators that have extended json values. + if (doc.$regex._bsontype === 'BSONRegExp') { + return doc as unknown as BSONRegExp; + } + } else { + return new BSONRegExp(doc.$regex, BSONRegExp.parseOptions(doc.$options)); + } + } + if ('$regularExpression' in doc) { + return new BSONRegExp( + doc.$regularExpression.pattern, + BSONRegExp.parseOptions(doc.$regularExpression.options) + ); + } + throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`); + } + + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + const stylize = getStylizeFunction(options) ?? (v => v); + inspect ??= defaultInspect; + const pattern = stylize(inspect(this.pattern), 'regexp'); + const flags = stylize(inspect(this.options), 'regexp'); + return `new BSONRegExp(${pattern}, ${flags})`; + } +} diff --git a/gateway/node_modules/bson/src/symbol.ts b/gateway/node_modules/bson/src/symbol.ts new file mode 100644 index 0000000000000000000000000000000000000000..6835ab9599b57b27cd30477a58579e0f43efe0f7 --- /dev/null +++ b/gateway/node_modules/bson/src/symbol.ts @@ -0,0 +1,55 @@ +import { BSONValue } from './bson_value'; +import { type InspectFn, defaultInspect } from './parser/utils'; + +/** @public */ +export interface BSONSymbolExtended { + $symbol: string; +} + +/** + * A class representation of the BSON Symbol type. + * @public + * @category BSONType + */ +export class BSONSymbol extends BSONValue { + get _bsontype(): 'BSONSymbol' { + return 'BSONSymbol'; + } + + value!: string; + /** + * @param value - the string representing the symbol. + */ + constructor(value: string) { + super(); + this.value = value; + } + + /** Access the wrapped string value. */ + valueOf(): string { + return this.value; + } + + toString(): string { + return this.value; + } + + toJSON(): string { + return this.value; + } + + /** @internal */ + toExtendedJSON(): BSONSymbolExtended { + return { $symbol: this.value }; + } + + /** @internal */ + static fromExtendedJSON(doc: BSONSymbolExtended): BSONSymbol { + return new BSONSymbol(doc.$symbol); + } + + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + inspect ??= defaultInspect; + return `new BSONSymbol(${inspect(this.value, options)})`; + } +} diff --git a/gateway/node_modules/bson/src/timestamp.ts b/gateway/node_modules/bson/src/timestamp.ts new file mode 100644 index 0000000000000000000000000000000000000000..adc312d923a1fa73840cbcec002ddef48f413a28 --- /dev/null +++ b/gateway/node_modules/bson/src/timestamp.ts @@ -0,0 +1,330 @@ +import { bsonType } from './bson_value'; +import { BSONError } from './error'; +import type { Int32 } from './int_32'; +import { Long } from './long'; +import { type InspectFn, defaultInspect } from './parser/utils'; + +/** + * @public + * @deprecated This type is no longer used internally and will be removed in a future version. + */ +export type TimestampOverrides = + | '_bsontype' + | 'toExtendedJSON' + | 'fromExtendedJSON' + | 'inspect' + | typeof bsonType; + +/** + * @public + * + * Inherited `Long` members surfaced on `Timestamp`. + * When `Long` gains a new member: add it here if it should pass through, or + * add a `@deprecated declare` line on the `Timestamp` class if it should be + * surfaced as deprecated. Anything left unclassified is silently dropped from + * the public type — preventing accidental behavioral bleed-through. + */ +type TimestampKept = + | 'toBigInt' + | 'toString' + | 'compare' + | 'equals' + | 'eq' + | 'notEquals' + | 'neq' + | 'ne' + | 'lessThan' + | 'lt' + | 'lessThanOrEqual' + | 'lte' + | 'le' + | 'greaterThan' + | 'gt' + | 'greaterThanOrEqual' + | 'gte' + | 'ge'; + +/** @public */ +export type LongWithoutOverrides = new ( + low: unknown, + high?: number | boolean, + unsigned?: boolean +) => { + [P in TimestampKept]: Long[P]; +}; +/** @public */ +export const LongWithoutOverridesClass: LongWithoutOverrides = + Long as unknown as LongWithoutOverrides; + +/** @public */ +export interface TimestampExtended { + $timestamp: { + t: number; + i: number; + }; +} + +/** + * @public + * @category BSONType + * + * A special type for _internal_ MongoDB use and is **not** associated with the regular Date type. + */ +export class Timestamp extends LongWithoutOverridesClass { + get _bsontype(): 'Timestamp' { + return 'Timestamp'; + } + get [bsonType](): 'Timestamp' { + return 'Timestamp'; + } + + /** + * @deprecated Use `Long.MAX_UNSIGNED_VALUE` instead. + */ + static readonly MAX_VALUE = Long.MAX_UNSIGNED_VALUE; + + /** + * An incrementing ordinal for operations within a given second. + */ + get i(): number { + return this.low >>> 0; + } + + /** + * A `time_t` value measuring seconds since the Unix epoch + */ + get t(): number { + return this.high >>> 0; + } + + /** + * @param int - A 64-bit bigint representing the Timestamp. + */ + constructor(int: bigint); + /** + * @param long - A 64-bit Long representing the Timestamp. + */ + constructor(long: Long); + /** + * @param value - A pair of two values indicating timestamp and increment. + */ + constructor(value: { t: number; i: number }); + constructor(low?: bigint | Long | { t: number | Int32; i: number | Int32 }) { + if (low == null) { + super(0, 0, true); + } else if (typeof low === 'bigint') { + super(low, true); + } else if (Long.isLong(low)) { + super(low.low, low.high, true); + } else if (typeof low === 'object' && 't' in low && 'i' in low) { + if (typeof low.t !== 'number' && (typeof low.t !== 'object' || low.t._bsontype !== 'Int32')) { + throw new BSONError('Timestamp constructed from { t, i } must provide t as a number'); + } + if (typeof low.i !== 'number' && (typeof low.i !== 'object' || low.i._bsontype !== 'Int32')) { + throw new BSONError('Timestamp constructed from { t, i } must provide i as a number'); + } + const t = Number(low.t); + const i = Number(low.i); + if (t < 0 || Number.isNaN(t)) { + throw new BSONError('Timestamp constructed from { t, i } must provide a positive t'); + } + if (i < 0 || Number.isNaN(i)) { + throw new BSONError('Timestamp constructed from { t, i } must provide a positive i'); + } + if (t > 0xffff_ffff) { + throw new BSONError( + 'Timestamp constructed from { t, i } must provide t equal or less than uint32 max' + ); + } + if (i > 0xffff_ffff) { + throw new BSONError( + 'Timestamp constructed from { t, i } must provide i equal or less than uint32 max' + ); + } + + super(i, t, true); + } else { + throw new BSONError( + 'A Timestamp can only be constructed with: bigint, Long, or { t: number; i: number }' + ); + } + } + + toJSON(): { $timestamp: string } { + return { + // eslint-disable-next-line @typescript-eslint/no-base-to-string + $timestamp: this.toString() + }; + } + + /** + * Returns a Timestamp represented by the given (32-bit) integer value. + * @deprecated Stores `value` in the low 32 bits (increment), leaving t = 0. Use `new Timestamp({ t, i })` or `Timestamp.fromBits(lowBits, highBits)` for explicit construction. + */ + static fromInt(value: number): Timestamp { + return new Timestamp(Long.fromInt(value, true)); + } + + /** + * Returns a Timestamp representing the given number value, provided that it is a finite number. Otherwise, zero is returned. + * @deprecated Splits `value` across (t, i) as a uint64; the result rarely matches user intent. Use `new Timestamp({ t, i })` or `Timestamp.fromBits(lowBits, highBits)` for explicit construction. + */ + static fromNumber(value: number): Timestamp { + return new Timestamp(Long.fromNumber(value, true)); + } + + /** + * Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits. + * + * @param lowBits - the low 32-bits. + * @param highBits - the high 32-bits. + */ + static fromBits(lowBits: number, highBits: number): Timestamp { + return new Timestamp({ i: lowBits, t: highBits }); + } + + /** + * Returns a Timestamp from the given string, optionally using the given radix. + * + * @param str - the textual representation of the Timestamp. + * @param optRadix - the radix in which the text is written. + */ + static fromString(str: string, optRadix: number): Timestamp { + return new Timestamp(Long.fromString(str, true, optRadix)); + } + + /** @internal */ + toExtendedJSON(): TimestampExtended { + return { $timestamp: { t: this.t, i: this.i } }; + } + + /** @internal */ + static fromExtendedJSON(doc: TimestampExtended): Timestamp { + // The Long check is necessary because extended JSON has different behavior given the size of the input number + const i = Long.isLong(doc.$timestamp.i) + ? doc.$timestamp.i.getLowBitsUnsigned() // Need to fetch the least significant 32 bits + : doc.$timestamp.i; + const t = Long.isLong(doc.$timestamp.t) + ? doc.$timestamp.t.getLowBitsUnsigned() // Need to fetch the least significant 32 bits + : doc.$timestamp.t; + return new Timestamp({ t, i }); + } + + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + inspect ??= defaultInspect; + const t = inspect(this.t, options); + const i = inspect(this.i, options); + return `new Timestamp({ t: ${t}, i: ${i} })`; + } + + // ******************** + // **** DEPRECATED **** + // ******************** + // Declare used to avoid runtime effects for field declaration. + // See: https://www.typescriptlang.org/docs/handbook/2/classes.html#type-only-field-declarations + + // Arithmetic + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare add: Long['add']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare subtract: Long['subtract']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare sub: Long['sub']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare multiply: Long['multiply']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare mul: Long['mul']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare divide: Long['divide']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare div: Long['div']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare modulo: Long['modulo']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare mod: Long['mod']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare rem: Long['rem']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned. */ + declare negate: Long['negate']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned. */ + declare neg: Long['neg']; + + // Bitwise / shifts + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare and: Long['and']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare or: Long['or']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare xor: Long['xor']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare not: Long['not']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare shiftLeft: Long['shiftLeft']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare shl: Long['shl']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare shiftRight: Long['shiftRight']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare shr: Long['shr']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare shiftRightUnsigned: Long['shiftRightUnsigned']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare shr_u: Long['shr_u']; + /** @deprecated Not applicable to Timestamp; Timestamp is not intended to be used as a Long. */ + declare shru: Long['shru']; + + // Signing + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned. */ + declare toSigned: Long['toSigned']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned (this is a no-op). */ + declare toUnsigned: Long['toUnsigned']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned (is always false). */ + declare isNegative: Long['isNegative']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned (is always true). */ + declare isPositive: Long['isPositive']; + /** @deprecated Not applicable to Timestamp as the underlying Long is always unsigned (is always true). */ + declare unsigned: Long['unsigned']; + + // Conversion + /** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */ + declare toInt: Long['toInt']; + /** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */ + declare toNumber: Long['toNumber']; + /** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */ + declare toBytes: Long['toBytes']; + /** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */ + declare toBytesLE: Long['toBytesLE']; + /** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch, or `.i` for the increment ordinal. */ + declare toBytesBE: Long['toBytesBE']; + + // Other + /** @deprecated Incompatible with Timestamp: returns a signed integer, but Timestamp is always unsigned. Use `.t` for seconds since the Unix epoch. */ + declare getHighBits: Long['getHighBits']; + /** @deprecated Not applicable to Timestamp; use `.t` for seconds since the Unix epoch. */ + declare getHighBitsUnsigned: Long['getHighBitsUnsigned']; + /** @deprecated Incompatible with Timestamp: returns a signed integer, but Timestamp is always unsigned. Use `.i` for the increment ordinal. */ + declare getLowBits: Long['getLowBits']; + /** @deprecated Not applicable to Timestamp; use `.i` for the increment ordinal. */ + declare getLowBitsUnsigned: Long['getLowBitsUnsigned']; + + /** @deprecated Not applicable to Timestamp; use `.i` instead. */ + declare low: Long['low']; + /** @deprecated Not applicable to Timestamp; use `.t` instead. */ + declare high: Long['high']; + + /** @deprecated Use .compare() for general comparison, or .equals(), .lessThan(), .greaterThan() for specific cases. */ + declare comp: Long['comp']; + /** @deprecated Compare `.t` and `.i` against `0` explicitly. */ + declare isZero: Long['isZero']; + /** @deprecated Compare `.t` and `.i` against `0` explicitly. */ + declare eqz: Long['eqz']; + + /** @deprecated Not applicable to Timestamp. Use the bsonType symbol or _bsontype === 'Timestamp' to identify a Timestamp. */ + declare __isLong__: Long['__isLong__']; + /** @deprecated Not applicable to Timestamp. */ + declare getNumBitsAbs: Long['getNumBitsAbs']; + /** @deprecated Not applicable to Timestamp; tests parity of the increment only. */ + declare isEven: Long['isEven']; + /** @deprecated Not applicable to Timestamp; tests parity of the increment only. */ + declare isOdd: Long['isOdd']; +} diff --git a/gateway/node_modules/bson/src/utils/byte_utils.ts b/gateway/node_modules/bson/src/utils/byte_utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..376258bf8bdaf34a8f2a54077f7dcf0a47b7f17e --- /dev/null +++ b/gateway/node_modules/bson/src/utils/byte_utils.ts @@ -0,0 +1,80 @@ +import { nodeJsByteUtils } from './node_byte_utils'; +import { webByteUtils } from './web_byte_utils'; + +/** + * @public + * @experimental + * + * A collection of functions that help work with data in a Uint8Array. + * ByteUtils is configured at load time to use Node.js or Web based APIs for the internal implementations. + */ +export type ByteUtils = { + /** Checks if the given value is a Uint8Array. */ + isUint8Array: (value: unknown) => value is Uint8Array; + /** Transforms the input to an instance of Buffer if running on node, otherwise Uint8Array */ + toLocalBufferType: (buffer: Uint8Array | ArrayBufferView | ArrayBuffer) => Uint8Array; + /** Create empty space of size */ + allocate: (size: number) => Uint8Array; + /** Create empty space of size, use pooled memory when available */ + allocateUnsafe: (size: number) => Uint8Array; + /** Compare 2 Uint8Arrays lexicographically */ + compare: (buffer1: Uint8Array, buffer2: Uint8Array) => -1 | 0 | 1; + /** Concatenating all the Uint8Arrays in new Uint8Array. */ + concat: (list: Uint8Array[]) => Uint8Array; + /** Copy bytes from source Uint8Array to target Uint8Array */ + copy: ( + source: Uint8Array, + target: Uint8Array, + targetStart?: number, + sourceStart?: number, + sourceEnd?: number + ) => number; + /** Check if two Uint8Arrays are deep equal */ + equals: (a: Uint8Array, b: Uint8Array) => boolean; + /** Create a Uint8Array from an array of numbers */ + fromNumberArray: (array: number[]) => Uint8Array; + /** Create a Uint8Array from a base64 string */ + fromBase64: (base64: string) => Uint8Array; + /** Create a Uint8Array from a UTF8 string */ + fromUTF8: (utf8: string) => Uint8Array; + /** Create a base64 string from bytes */ + toBase64: (buffer: Uint8Array) => string; + /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ + fromISO88591: (codePoints: string) => Uint8Array; + /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ + toISO88591: (buffer: Uint8Array) => string; + /** Create a Uint8Array from a hex string */ + fromHex: (hex: string) => Uint8Array; + /** Create a lowercase hex string from bytes */ + toHex: (buffer: Uint8Array) => string; + /** Create a string from utf8 code units, fatal=true will throw an error if UTF-8 bytes are invalid, fatal=false will insert replacement characters */ + toUTF8: (buffer: Uint8Array, start: number, end: number, fatal: boolean) => string; + /** Get the utf8 code unit count from a string if it were to be transformed to utf8 */ + utf8ByteLength: (input: string) => number; + /** Encode UTF8 bytes generated from `source` string into `destination` at byteOffset. Returns the number of bytes encoded. */ + encodeUTF8Into: (destination: Uint8Array, source: string, byteOffset: number) => number; + /** Generate a Uint8Array filled with random bytes with byteLength */ + randomBytes: (byteLength: number) => Uint8Array; + /** Interprets `buffer` as an array of 32-bit values and swaps the byte order in-place. */ + swap32: (buffer: Uint8Array) => Uint8Array; +}; + +declare const Buffer: { new (): unknown; prototype?: { _isBuffer?: boolean } } | undefined; + +/** + * Check that a global Buffer exists that is a function and + * does not have a '_isBuffer' property defined on the prototype + * (this is to prevent using the npm buffer) + */ +const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true; + +/** + * This is the only ByteUtils that should be used across the rest of the BSON library. + * + * The type annotation is important here, it asserts that each of the platform specific + * utils implementations are compatible with the common one. + * + * @public + * @experimental + */ +export const ByteUtils: ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils; diff --git a/gateway/node_modules/bson/src/utils/latin.ts b/gateway/node_modules/bson/src/utils/latin.ts new file mode 100644 index 0000000000000000000000000000000000000000..5dd5c91f691de1fd1dc841448d93bcef7c54cf10 --- /dev/null +++ b/gateway/node_modules/bson/src/utils/latin.ts @@ -0,0 +1,104 @@ +/** + * This function is an optimization for small basic latin strings. + * @internal + * @remarks + * ### Important characteristics: + * - If the uint8array or distance between start and end is 0 this function returns an empty string + * - If the byteLength of the string is 1, 2, or 3 we invoke String.fromCharCode and manually offset into the buffer + * - If the byteLength of the string is less than or equal to 20 an array of bytes is built and `String.fromCharCode.apply` is called with the result + * - If any byte exceeds 128 this function returns null + * + * @param uint8array - A sequence of bytes that may contain basic latin characters + * @param start - The start index from which to search the uint8array + * @param end - The index to stop searching the uint8array + * @returns string if all bytes are within the basic latin range, otherwise null + */ +export function tryReadBasicLatin( + uint8array: Uint8Array, + start: number, + end: number +): string | null { + if (uint8array.length === 0) { + return ''; + } + + const stringByteLength = end - start; + if (stringByteLength === 0) { + return ''; + } + + if (stringByteLength > 20) { + return null; + } + + if (stringByteLength === 1 && uint8array[start] < 128) { + return String.fromCharCode(uint8array[start]); + } + + if (stringByteLength === 2 && uint8array[start] < 128 && uint8array[start + 1] < 128) { + return String.fromCharCode(uint8array[start]) + String.fromCharCode(uint8array[start + 1]); + } + + if ( + stringByteLength === 3 && + uint8array[start] < 128 && + uint8array[start + 1] < 128 && + uint8array[start + 2] < 128 + ) { + return ( + String.fromCharCode(uint8array[start]) + + String.fromCharCode(uint8array[start + 1]) + + String.fromCharCode(uint8array[start + 2]) + ); + } + + const latinBytes = []; + for (let i = start; i < end; i++) { + const byte = uint8array[i]; + if (byte > 127) { + return null; + } + latinBytes.push(byte); + } + + return String.fromCharCode(...latinBytes); +} + +/** + * This function is an optimization for writing small basic latin strings. + * @internal + * @remarks + * ### Important characteristics: + * - If the string length is 0 return 0, do not perform any work + * - If a string is longer than 25 code units return null + * - If any code unit exceeds 128 this function returns null + * + * @param destination - The uint8array to serialize the string to + * @param source - The string to turn into UTF-8 bytes if it fits in the basic latin range + * @param offset - The position in the destination to begin writing bytes to + * @returns the number of bytes written to destination if all code units are below 128, otherwise null + */ +export function tryWriteBasicLatin( + destination: Uint8Array, + source: string, + offset: number +): number | null { + if (source.length === 0) return 0; + + if (source.length > 25) return null; + + if (destination.length - offset < source.length) return null; + + for ( + let charOffset = 0, destinationOffset = offset; + charOffset < source.length; + charOffset++, destinationOffset++ + ) { + const char = source.charCodeAt(charOffset); + if (char > 127) return null; + + destination[destinationOffset] = char; + } + + return source.length; +} diff --git a/gateway/node_modules/bson/src/utils/node_byte_utils.ts b/gateway/node_modules/bson/src/utils/node_byte_utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..eaad408078b04ae95fc3f21f68a634206cf10f98 --- /dev/null +++ b/gateway/node_modules/bson/src/utils/node_byte_utils.ts @@ -0,0 +1,193 @@ +import { BSONError } from '../error'; +import { parseUtf8 } from '../parse_utf8'; +import { tryReadBasicLatin, tryWriteBasicLatin } from './latin'; +import { isUint8Array } from '../parser/utils'; + +type NodeJsEncoding = 'base64' | 'hex' | 'utf8' | 'binary'; +type NodeJsBuffer = ArrayBufferView & + Uint8Array & { + write(string: string, offset: number, length: undefined, encoding: 'utf8'): number; + copy(target: Uint8Array, targetStart: number, sourceStart: number, sourceEnd: number): number; + toString: (this: Uint8Array, encoding: NodeJsEncoding, start?: number, end?: number) => string; + equals: (this: Uint8Array, other: Uint8Array) => boolean; + swap32: (this: NodeJsBuffer) => NodeJsBuffer; + compare: (this: Uint8Array, other: Uint8Array) => -1 | 0 | 1; + }; +type NodeJsBufferConstructor = Omit & { + alloc: (size: number) => NodeJsBuffer; + allocUnsafe: (size: number) => NodeJsBuffer; + from(array: number[]): NodeJsBuffer; + from(array: Uint8Array): NodeJsBuffer; + from(array: ArrayBuffer): NodeJsBuffer; + from(array: ArrayBufferLike, byteOffset: number, byteLength: number): NodeJsBuffer; + from(base64: string, encoding: NodeJsEncoding): NodeJsBuffer; + byteLength(input: string, encoding: 'utf8'): number; + isBuffer(value: unknown): value is NodeJsBuffer; + concat(list: Uint8Array[]): NodeJsBuffer; +}; + +// This can be nullish, but we gate the nodejs functions on being exported whether or not this exists +// Node.js global +declare const Buffer: NodeJsBufferConstructor; + +/** @internal */ +function nodejsMathRandomBytes(byteLength: number): NodeJsBuffer { + return nodeJsByteUtils.fromNumberArray( + Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)) + ); +} + +/** @internal */ +function nodejsSecureRandomBytes(byteLength: number): NodeJsBuffer { + // @ts-expect-error: crypto.getRandomValues cannot actually be null here + return crypto.getRandomValues(nodeJsByteUtils.allocate(byteLength)); +} + +const nodejsRandomBytes = (() => { + const { crypto } = globalThis as { + crypto?: { getRandomValues?: (space: Uint8Array) => Uint8Array }; + }; + if (crypto != null && typeof crypto.getRandomValues === 'function') { + return nodejsSecureRandomBytes; + } else { + return nodejsMathRandomBytes; + } +})(); + +/** + * @public + * @experimental + */ +export const nodeJsByteUtils = { + isUint8Array: isUint8Array, + + toLocalBufferType(potentialBuffer: Uint8Array | NodeJsBuffer | ArrayBuffer): NodeJsBuffer { + if (Buffer.isBuffer(potentialBuffer)) { + return potentialBuffer; + } + + if (ArrayBuffer.isView(potentialBuffer)) { + return Buffer.from( + potentialBuffer.buffer, + potentialBuffer.byteOffset, + potentialBuffer.byteLength + ); + } + + const stringTag = + potentialBuffer?.[Symbol.toStringTag] ?? Object.prototype.toString.call(potentialBuffer); + if ( + stringTag === 'ArrayBuffer' || + stringTag === 'SharedArrayBuffer' || + stringTag === '[object ArrayBuffer]' || + stringTag === '[object SharedArrayBuffer]' + ) { + return Buffer.from(potentialBuffer); + } + + throw new BSONError(`Cannot create Buffer from the passed potentialBuffer.`); + }, + + allocate(size: number): NodeJsBuffer { + return Buffer.alloc(size); + }, + + allocateUnsafe(size: number): NodeJsBuffer { + return Buffer.allocUnsafe(size); + }, + + compare(a: Uint8Array, b: Uint8Array) { + return nodeJsByteUtils.toLocalBufferType(a).compare(b); + }, + + concat(list: Uint8Array[]): NodeJsBuffer { + return Buffer.concat(list); + }, + + copy( + source: Uint8Array, + target: Uint8Array, + targetStart?: number, + sourceStart?: number, + sourceEnd?: number + ): number { + return nodeJsByteUtils + .toLocalBufferType(source) + .copy(target, targetStart ?? 0, sourceStart ?? 0, sourceEnd ?? source.length); + }, + + equals(a: Uint8Array, b: Uint8Array): boolean { + return nodeJsByteUtils.toLocalBufferType(a).equals(b); + }, + + fromNumberArray(array: number[]): NodeJsBuffer { + return Buffer.from(array); + }, + + fromBase64(base64: string): NodeJsBuffer { + return Buffer.from(base64, 'base64'); + }, + + fromUTF8(utf8: string): NodeJsBuffer { + return Buffer.from(utf8, 'utf8'); + }, + + toBase64(buffer: Uint8Array): string { + return nodeJsByteUtils.toLocalBufferType(buffer).toString('base64'); + }, + + /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ + fromISO88591(codePoints: string): NodeJsBuffer { + return Buffer.from(codePoints, 'binary'); + }, + + /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ + toISO88591(buffer: Uint8Array): string { + return nodeJsByteUtils.toLocalBufferType(buffer).toString('binary'); + }, + + fromHex(hex: string): NodeJsBuffer { + return Buffer.from(hex, 'hex'); + }, + + toHex(buffer: Uint8Array): string { + return nodeJsByteUtils.toLocalBufferType(buffer).toString('hex'); + }, + + toUTF8(buffer: Uint8Array, start: number, end: number, fatal: boolean): string { + const basicLatin = end - start <= 20 ? tryReadBasicLatin(buffer, start, end) : null; + if (basicLatin != null) { + return basicLatin; + } + + const string = nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8', start, end); + if (fatal) { + for (let i = 0; i < string.length; i++) { + if (string.charCodeAt(i) === 0xfffd) { + parseUtf8(buffer, start, end, true); + break; + } + } + } + return string; + }, + + utf8ByteLength(input: string): number { + return Buffer.byteLength(input, 'utf8'); + }, + + encodeUTF8Into(buffer: Uint8Array, source: string, byteOffset: number): number { + const latinBytesWritten = tryWriteBasicLatin(buffer, source, byteOffset); + if (latinBytesWritten != null) { + return latinBytesWritten; + } + + return nodeJsByteUtils.toLocalBufferType(buffer).write(source, byteOffset, undefined, 'utf8'); + }, + + randomBytes: nodejsRandomBytes, + + swap32(buffer: Uint8Array): NodeJsBuffer { + return nodeJsByteUtils.toLocalBufferType(buffer).swap32(); + } +}; diff --git a/gateway/node_modules/bson/src/utils/number_utils.ts b/gateway/node_modules/bson/src/utils/number_utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..6e96a97fc038503a4551cf602385a13f9f57ee94 --- /dev/null +++ b/gateway/node_modules/bson/src/utils/number_utils.ts @@ -0,0 +1,204 @@ +const FLOAT = new Float64Array(1); +const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8); + +FLOAT[0] = -1; +// Little endian [0, 0, 0, 0, 0, 0, 240, 191] +// Big endian [191, 240, 0, 0, 0, 0, 0, 0] +const isBigEndian = FLOAT_BYTES[7] === 0; + +/** + * @experimental + * @public + * + * A collection of functions that get or set various numeric types and bit widths from a Uint8Array. + */ +export type NumberUtils = { + /** Is true if the current system is big endian. */ + isBigEndian: boolean; + /** + * Parses a signed int32 at offset. Throws a `RangeError` if value is negative. + */ + getNonnegativeInt32LE: (source: Uint8Array, offset: number) => number; + getInt32LE: (source: Uint8Array, offset: number) => number; + getUint32LE: (source: Uint8Array, offset: number) => number; + getUint32BE: (source: Uint8Array, offset: number) => number; + getBigInt64LE: (source: Uint8Array, offset: number) => bigint; + getFloat64LE: (source: Uint8Array, offset: number) => number; + setInt32BE: (destination: Uint8Array, offset: number, value: number) => 4; + setInt32LE: (destination: Uint8Array, offset: number, value: number) => 4; + setBigInt64LE: (destination: Uint8Array, offset: number, value: bigint) => 8; + setFloat64LE: (destination: Uint8Array, offset: number, value: number) => 8; +}; + +/** + * Number parsing and serializing utilities. + * + * @experimental + * @public + */ +export const NumberUtils: NumberUtils = { + isBigEndian, + + getNonnegativeInt32LE(source: Uint8Array, offset: number): number { + if (source[offset + 3] > 127) { + throw new RangeError(`Size cannot be negative at offset: ${offset}`); + } + return ( + source[offset] | + (source[offset + 1] << 8) | + (source[offset + 2] << 16) | + (source[offset + 3] << 24) + ); + }, + + /** Reads a little-endian 32-bit integer from source */ + getInt32LE(source: Uint8Array, offset: number): number { + return ( + source[offset] | + (source[offset + 1] << 8) | + (source[offset + 2] << 16) | + (source[offset + 3] << 24) + ); + }, + + /** Reads a little-endian 32-bit unsigned integer from source */ + getUint32LE(source: Uint8Array, offset: number): number { + return ( + source[offset] + + source[offset + 1] * 256 + + source[offset + 2] * 65536 + + source[offset + 3] * 16777216 + ); + }, + + /** Reads a big-endian 32-bit integer from source */ + getUint32BE(source: Uint8Array, offset: number): number { + return ( + source[offset + 3] + + source[offset + 2] * 256 + + source[offset + 1] * 65536 + + source[offset] * 16777216 + ); + }, + + /** Reads a little-endian 64-bit integer from source */ + getBigInt64LE(source: Uint8Array, offset: number): bigint { + const hi = BigInt( + source[offset + 4] + + source[offset + 5] * 256 + + source[offset + 6] * 65536 + + (source[offset + 7] << 24) + ); // Overflow + + const lo = BigInt( + source[offset] + + source[offset + 1] * 256 + + source[offset + 2] * 65536 + + source[offset + 3] * 16777216 + ); + + return (hi << 32n) + lo; + }, + + /** Reads a little-endian 64-bit float from source */ + getFloat64LE: isBigEndian + ? (source: Uint8Array, offset: number) => { + FLOAT_BYTES[7] = source[offset]; + FLOAT_BYTES[6] = source[offset + 1]; + FLOAT_BYTES[5] = source[offset + 2]; + FLOAT_BYTES[4] = source[offset + 3]; + FLOAT_BYTES[3] = source[offset + 4]; + FLOAT_BYTES[2] = source[offset + 5]; + FLOAT_BYTES[1] = source[offset + 6]; + FLOAT_BYTES[0] = source[offset + 7]; + return FLOAT[0]; + } + : (source: Uint8Array, offset: number) => { + FLOAT_BYTES[0] = source[offset]; + FLOAT_BYTES[1] = source[offset + 1]; + FLOAT_BYTES[2] = source[offset + 2]; + FLOAT_BYTES[3] = source[offset + 3]; + FLOAT_BYTES[4] = source[offset + 4]; + FLOAT_BYTES[5] = source[offset + 5]; + FLOAT_BYTES[6] = source[offset + 6]; + FLOAT_BYTES[7] = source[offset + 7]; + return FLOAT[0]; + }, + + /** Writes a big-endian 32-bit integer to destination, can be signed or unsigned */ + setInt32BE(destination: Uint8Array, offset: number, value: number): 4 { + destination[offset + 3] = value; + value >>>= 8; + destination[offset + 2] = value; + value >>>= 8; + destination[offset + 1] = value; + value >>>= 8; + destination[offset] = value; + return 4; + }, + + /** Writes a little-endian 32-bit integer to destination, can be signed or unsigned */ + setInt32LE(destination: Uint8Array, offset: number, value: number): 4 { + destination[offset] = value; + value >>>= 8; + destination[offset + 1] = value; + value >>>= 8; + destination[offset + 2] = value; + value >>>= 8; + destination[offset + 3] = value; + return 4; + }, + + /** Write a little-endian 64-bit integer to source */ + setBigInt64LE(destination: Uint8Array, offset: number, value: bigint): 8 { + const mask32bits = 0xffff_ffffn; + + /** lower 32 bits */ + let lo = Number(value & mask32bits); + destination[offset] = lo; + lo >>= 8; + destination[offset + 1] = lo; + lo >>= 8; + destination[offset + 2] = lo; + lo >>= 8; + destination[offset + 3] = lo; + + let hi = Number((value >> 32n) & mask32bits); + destination[offset + 4] = hi; + hi >>= 8; + destination[offset + 5] = hi; + hi >>= 8; + destination[offset + 6] = hi; + hi >>= 8; + destination[offset + 7] = hi; + + return 8; + }, + + /** Writes a little-endian 64-bit float to destination */ + setFloat64LE: isBigEndian + ? (destination: Uint8Array, offset: number, value: number) => { + FLOAT[0] = value; + destination[offset] = FLOAT_BYTES[7]; + destination[offset + 1] = FLOAT_BYTES[6]; + destination[offset + 2] = FLOAT_BYTES[5]; + destination[offset + 3] = FLOAT_BYTES[4]; + destination[offset + 4] = FLOAT_BYTES[3]; + destination[offset + 5] = FLOAT_BYTES[2]; + destination[offset + 6] = FLOAT_BYTES[1]; + destination[offset + 7] = FLOAT_BYTES[0]; + return 8; + } + : (destination: Uint8Array, offset: number, value: number) => { + FLOAT[0] = value; + destination[offset] = FLOAT_BYTES[0]; + destination[offset + 1] = FLOAT_BYTES[1]; + destination[offset + 2] = FLOAT_BYTES[2]; + destination[offset + 3] = FLOAT_BYTES[3]; + destination[offset + 4] = FLOAT_BYTES[4]; + destination[offset + 5] = FLOAT_BYTES[5]; + destination[offset + 6] = FLOAT_BYTES[6]; + destination[offset + 7] = FLOAT_BYTES[7]; + return 8; + } +}; diff --git a/gateway/node_modules/bson/src/utils/string_utils.ts b/gateway/node_modules/bson/src/utils/string_utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..1ffb118e9d3571f62583d09d1d9892d19cd5f938 --- /dev/null +++ b/gateway/node_modules/bson/src/utils/string_utils.ts @@ -0,0 +1,44 @@ +/** + * @internal + * Removes leading zeros and explicit plus from textual representation of a number. + */ +export function removeLeadingZerosAndExplicitPlus(str: string): string { + if (str === '') { + return str; + } + + let startIndex = 0; + + const isNegative = str[startIndex] === '-'; + const isExplicitlyPositive = str[startIndex] === '+'; + + if (isExplicitlyPositive || isNegative) { + startIndex += 1; + } + + let foundInsignificantZero = false; + + for (; startIndex < str.length && str[startIndex] === '0'; ++startIndex) { + foundInsignificantZero = true; + } + + if (!foundInsignificantZero) { + return isExplicitlyPositive ? str.slice(1) : str; + } + + return `${isNegative ? '-' : ''}${str.length === startIndex ? '0' : str.slice(startIndex)}`; +} + +/** + * @internal + * Returns false for an string that contains invalid characters for its radix, else returns the original string. + * @param str - The textual representation of the Long + * @param radix - The radix in which the text is written (2-36), defaults to 10 + */ +export function validateStringCharacters(str: string, radix?: number): false | string { + radix = radix ?? 10; + const validCharacters = '0123456789abcdefghijklmnopqrstuvwxyz'.slice(0, radix); + // regex is case insensitive and checks that each character within the string is one of the validCharacters + const regex = new RegExp(`[^-+${validCharacters}]`, 'i'); + return regex.test(str) ? false : str; +} diff --git a/gateway/node_modules/bson/src/utils/web_byte_utils.ts b/gateway/node_modules/bson/src/utils/web_byte_utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..608b88cffa9887509f6a26017727864db5b062ab --- /dev/null +++ b/gateway/node_modules/bson/src/utils/web_byte_utils.ts @@ -0,0 +1,304 @@ +import { BSONError } from '../error'; +import { tryReadBasicLatin } from './latin'; +import { parseUtf8 } from '../parse_utf8'; +import { isUint8Array } from '../parser/utils'; + +type TextDecoder = { + readonly encoding: string; + readonly fatal: boolean; + readonly ignoreBOM: boolean; + decode(input?: Uint8Array): string; +}; +type TextDecoderConstructor = { + new (label: 'utf8', options: { fatal: boolean; ignoreBOM?: boolean }): TextDecoder; +}; + +type TextEncoder = { + readonly encoding: string; + encode(input?: string): Uint8Array; +}; +type TextEncoderConstructor = { + new (): TextEncoder; +}; + +// Web global +declare const TextDecoder: TextDecoderConstructor; +declare const TextEncoder: TextEncoderConstructor; +declare const atob: (base64: string) => string; +declare const btoa: (binary: string) => string; + +type ArrayBufferViewWithTag = ArrayBufferView & { + [Symbol.toStringTag]?: string; +}; + +function isReactNative() { + const { navigator } = globalThis as { navigator?: { product?: string } }; + return typeof navigator === 'object' && navigator.product === 'ReactNative'; +} + +/** @internal */ +export function webMathRandomBytes(byteLength: number) { + if (byteLength < 0) { + throw new RangeError(`The argument 'byteLength' is invalid. Received ${byteLength}`); + } + return webByteUtils.fromNumberArray( + Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)) + ); +} + +/** @internal */ +const webRandomBytes: (byteLength: number) => Uint8Array = (() => { + const { crypto } = globalThis as { + crypto?: { getRandomValues?: (space: Uint8Array) => Uint8Array }; + }; + if (crypto != null && typeof crypto.getRandomValues === 'function') { + return (byteLength: number) => { + // @ts-expect-error: crypto.getRandomValues cannot actually be null here + // You cannot separate getRandomValues from crypto (need to have this === crypto) + return crypto.getRandomValues(webByteUtils.allocate(byteLength)); + }; + } else { + if (isReactNative()) { + const { console } = globalThis as { console?: { warn?: (message: string) => void } }; + console?.warn?.( + 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.' + ); + } + return webMathRandomBytes; + } +})(); + +const HEX_DIGIT = /(\d|[a-f])/i; + +/** + * @public + * @experimental + */ +export const webByteUtils = { + isUint8Array: isUint8Array, + + toLocalBufferType( + potentialUint8array: Uint8Array | ArrayBufferViewWithTag | ArrayBuffer + ): Uint8Array { + const stringTag = + potentialUint8array?.[Symbol.toStringTag] ?? + Object.prototype.toString.call(potentialUint8array); + + if (stringTag === 'Uint8Array') { + return potentialUint8array as Uint8Array; + } + + if (ArrayBuffer.isView(potentialUint8array)) { + return new Uint8Array( + potentialUint8array.buffer.slice( + potentialUint8array.byteOffset, + potentialUint8array.byteOffset + potentialUint8array.byteLength + ) + ); + } + + if ( + stringTag === 'ArrayBuffer' || + stringTag === 'SharedArrayBuffer' || + stringTag === '[object ArrayBuffer]' || + stringTag === '[object SharedArrayBuffer]' + ) { + return new Uint8Array(potentialUint8array); + } + + throw new BSONError(`Cannot make a Uint8Array from passed potentialBuffer.`); + }, + + allocate(size: number): Uint8Array { + if (typeof size !== 'number') { + throw new TypeError(`The "size" argument must be of type number. Received ${String(size)}`); + } + return new Uint8Array(size); + }, + + allocateUnsafe(size: number): Uint8Array { + return webByteUtils.allocate(size); + }, + + compare(uint8Array: Uint8Array, otherUint8Array: Uint8Array): -1 | 0 | 1 { + if (uint8Array === otherUint8Array) return 0; + + const len = Math.min(uint8Array.length, otherUint8Array.length); + + for (let i = 0; i < len; i++) { + if (uint8Array[i] < otherUint8Array[i]) return -1; + if (uint8Array[i] > otherUint8Array[i]) return 1; + } + + if (uint8Array.length < otherUint8Array.length) return -1; + if (uint8Array.length > otherUint8Array.length) return 1; + + return 0; + }, + + concat(uint8Arrays: Uint8Array[]): Uint8Array { + if (uint8Arrays.length === 0) return webByteUtils.allocate(0); + + let totalLength = 0; + for (const uint8Array of uint8Arrays) { + totalLength += uint8Array.length; + } + + const result = webByteUtils.allocate(totalLength); + let offset = 0; + + for (const uint8Array of uint8Arrays) { + result.set(uint8Array, offset); + offset += uint8Array.length; + } + + return result; + }, + + copy( + source: Uint8Array, + target: Uint8Array, + targetStart?: number, + sourceStart?: number, + sourceEnd?: number + ): number { + // validate and standardize passed-in sourceEnd + if (sourceEnd !== undefined && sourceEnd < 0) { + throw new RangeError( + `The value of "sourceEnd" is out of range. It must be >= 0. Received ${sourceEnd}` + ); + } + sourceEnd = sourceEnd ?? source.length; + + // validate and standardize passed-in sourceStart + if (sourceStart !== undefined && (sourceStart < 0 || sourceStart > sourceEnd)) { + throw new RangeError( + `The value of "sourceStart" is out of range. It must be >= 0 and <= ${sourceEnd}. Received ${sourceStart}` + ); + } + sourceStart = sourceStart ?? 0; + + // validate and standardize passed-in targetStart + if (targetStart !== undefined && targetStart < 0) { + throw new RangeError( + `The value of "targetStart" is out of range. It must be >= 0. Received ${targetStart}` + ); + } + targetStart = targetStart ?? 0; + + // figure out how many bytes we can copy + const srcSlice = source.subarray(sourceStart, sourceEnd); + const maxLen = Math.min(srcSlice.length, target.length - targetStart); + if (maxLen <= 0) { + return 0; + } + + // perform the copy + target.set(srcSlice.subarray(0, maxLen), targetStart); + return maxLen; + }, + + equals(uint8Array: Uint8Array, otherUint8Array: Uint8Array): boolean { + if (uint8Array.byteLength !== otherUint8Array.byteLength) { + return false; + } + for (let i = 0; i < uint8Array.byteLength; i++) { + if (uint8Array[i] !== otherUint8Array[i]) { + return false; + } + } + return true; + }, + + fromNumberArray(array: number[]): Uint8Array { + return Uint8Array.from(array); + }, + + fromBase64(base64: string): Uint8Array { + return Uint8Array.from(atob(base64), c => c.charCodeAt(0)); + }, + + fromUTF8(utf8: string): Uint8Array { + return new TextEncoder().encode(utf8); + }, + + toBase64(uint8array: Uint8Array): string { + return btoa(webByteUtils.toISO88591(uint8array)); + }, + + /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ + fromISO88591(codePoints: string): Uint8Array { + return Uint8Array.from(codePoints, c => c.charCodeAt(0) & 0xff); + }, + + /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ + toISO88591(uint8array: Uint8Array): string { + return Array.from(Uint16Array.from(uint8array), b => String.fromCharCode(b)).join(''); + }, + + fromHex(hex: string): Uint8Array { + const evenLengthHex = hex.length % 2 === 0 ? hex : hex.slice(0, hex.length - 1); + const buffer = []; + + for (let i = 0; i < evenLengthHex.length; i += 2) { + const firstDigit = evenLengthHex[i]; + const secondDigit = evenLengthHex[i + 1]; + + if (!HEX_DIGIT.test(firstDigit)) { + break; + } + if (!HEX_DIGIT.test(secondDigit)) { + break; + } + + const hexDigit = Number.parseInt(`${firstDigit}${secondDigit}`, 16); + buffer.push(hexDigit); + } + + return Uint8Array.from(buffer); + }, + + toHex(uint8array: Uint8Array): string { + return Array.from(uint8array, byte => byte.toString(16).padStart(2, '0')).join(''); + }, + + toUTF8(uint8array: Uint8Array, start: number, end: number, fatal: boolean): string { + const basicLatin = end - start <= 20 ? tryReadBasicLatin(uint8array, start, end) : null; + if (basicLatin != null) { + return basicLatin; + } + + return parseUtf8(uint8array, start, end, fatal); + }, + + utf8ByteLength(input: string): number { + return new TextEncoder().encode(input).byteLength; + }, + + encodeUTF8Into(uint8array: Uint8Array, source: string, byteOffset: number): number { + const bytes = new TextEncoder().encode(source); + uint8array.set(bytes, byteOffset); + return bytes.byteLength; + }, + + randomBytes: webRandomBytes, + + swap32(buffer: Uint8Array): Uint8Array { + if (buffer.length % 4 !== 0) { + throw new RangeError('Buffer size must be a multiple of 32-bits'); + } + + for (let i = 0; i < buffer.length; i += 4) { + const byte0 = buffer[i]; + const byte1 = buffer[i + 1]; + const byte2 = buffer[i + 2]; + const byte3 = buffer[i + 3]; + buffer[i] = byte3; + buffer[i + 1] = byte2; + buffer[i + 2] = byte1; + buffer[i + 3] = byte0; + } + + return buffer; + } +}; diff --git a/gateway/node_modules/kareem/CHANGELOG.md b/gateway/node_modules/kareem/CHANGELOG.md new file mode 100644 index 0000000000000000000000000000000000000000..ae368ecb0197de9a123b12e39e72773d458e745b --- /dev/null +++ b/gateway/node_modules/kareem/CHANGELOG.md @@ -0,0 +1,845 @@ +# Changelog + + +## 3.3.0 (2026-04-14) + +* perf: avoid cloning args on every pre/post #45 + + +## 3.2.0 (2026-01-29) + +* feat(exec): add filter option to execPreSync and execPostSync #44 + + +## 3.1.0 (2026-01-12) + +* feat(exec): add filter option to allow executing hooks based on a filter function #43 + + +## 3.0.0 (2025-11-18) + +* BREAKING CHANGE: make execPre async and drop callback support #39 +* BREAKING CHANGE: require Node 18 +* feat: overwriteArguments support #42 + + +## 2.6.0 (2024-03-04) + +* feat: add TypeScript types + + +## 2.5.1 (2023-01-06) + +* fix: avoid passing final callback to pre hook, because calling the callback can mess up hook execution #36 Automattic/mongoose#12836 + + +## 2.5.0 (2022-12-01) + +* feat: add errorHandler option to `post()` #34 + + +## 2.4.0 (2022-06-13) + +* feat: add `overwriteResult()` and `skipWrappedFunction()` for more advanced control flow + + +## 2.3.4 (2022-02-10) + +* perf: various performance improvements #27 #24 #23 #22 #21 #20 + + +## 2.3.3 (2021-12-26) + +* fix: handle sync errors in `wrap()` + + +## 2.3.2 (2020-12-08) + +* fix: handle sync errors in pre hooks if there are multiple hooks + + +## 2.3.0 (2018-09-24) + +* chore(release): 2.2.3 ([c8f2695](https://github.com/vkarpov15/kareem/commit/c8f2695)) +* chore(release): 2.2.4 ([a377a4f](https://github.com/vkarpov15/kareem/commit/a377a4f)) +* chore(release): 2.2.5 ([5a495e3](https://github.com/vkarpov15/kareem/commit/5a495e3)) +* fix(filter): copy async pres correctly with `filter()` ([1b1ed8a](https://github.com/vkarpov15/kareem/commit/1b1ed8a)), closes [Automattic/mongoose#3054](https://github.com/Automattic/mongoose/issues/3054) +* feat: add filter() function ([1f641f4](https://github.com/vkarpov15/kareem/commit/1f641f4)) +* feat: support storing options on pre and post hooks ([59220b9](https://github.com/vkarpov15/kareem/commit/59220b9)) + + + + +## 2.2.3 (2018-09-10) + +* chore: release 2.2.3 ([af653a3](https://github.com/vkarpov15/kareem/commit/af653a3)) + + + + +## 2.2.2 (2018-09-10) + +* chore: release 2.2.2 ([3f0144d](https://github.com/vkarpov15/kareem/commit/3f0144d)) +* fix: allow merge() to not clone ([e628d65](https://github.com/vkarpov15/kareem/commit/e628d65)) + + + + +## 2.2.1 (2018-06-05) + +* chore: release 2.2.1 ([4625a64](https://github.com/vkarpov15/kareem/commit/4625a64)) +* chore: remove lockfile from git ([7f3e4e6](https://github.com/vkarpov15/kareem/commit/7f3e4e6)) +* fix: handle numAsync correctly when merging ([fef8e7e](https://github.com/vkarpov15/kareem/commit/fef8e7e)) +* test: repro issue with not copying numAsync ([952d9db](https://github.com/vkarpov15/kareem/commit/952d9db)) + + + + +## 2.2.0 (2018-06-05) + +* chore: release 2.2.0 ([ff9ad03](https://github.com/vkarpov15/kareem/commit/ff9ad03)) +* fix: use maps instead of objects for _pres and _posts so `toString()` doesn't get reported as having ([55df303](https://github.com/vkarpov15/kareem/commit/55df303)), closes [Automattic/mongoose#6538](https://github.com/Automattic/mongoose/issues/6538) + + + + +## 2.1.0 (2018-05-16) + +* chore: release 2.1.0 ([ba5f1bc](https://github.com/vkarpov15/kareem/commit/ba5f1bc)) +* feat: add option to check wrapped function return value for promises ([c9d7dd1](https://github.com/vkarpov15/kareem/commit/c9d7dd1)) +* refactor: use const in wrap() ([0fc21f9](https://github.com/vkarpov15/kareem/commit/0fc21f9)) + + + + +## 2.0.7 (2018-04-28) + +* chore: release 2.0.7 ([0bf91e6](https://github.com/vkarpov15/kareem/commit/0bf91e6)) +* feat: add `hasHooks()` ([225f18d](https://github.com/vkarpov15/kareem/commit/225f18d)), closes [Automattic/mongoose#6385](https://github.com/Automattic/mongoose/issues/6385) + + + + +## 2.0.6 (2018-03-22) + +* chore: release 2.0.6 ([f3d406b](https://github.com/vkarpov15/kareem/commit/f3d406b)) +* fix(wrap): ensure fast path still wraps function in `nextTick()` for chaining ([7000494](https://github.com/vkarpov15/kareem/commit/7000494)), closes [Automattic/mongoose#6250](https://github.com/Automattic/mongoose/issues/6250) [dsanel/mongoose-delete#36](https://github.com/dsanel/mongoose-delete/issues/36) + + + + +## 2.0.5 (2018-02-22) + +* chore: release 2.0.5 ([3286612](https://github.com/vkarpov15/kareem/commit/3286612)) +* perf(createWrapper): don't create wrapper if there are no hooks ([5afc5b9](https://github.com/vkarpov15/kareem/commit/5afc5b9)), closes [Automattic/mongoose#6126](https://github.com/Automattic/mongoose/issues/6126) + + + + +## 2.0.4 (2018-02-08) + +* chore: release 2.0.4 ([2ab0293](https://github.com/vkarpov15/kareem/commit/2ab0293)) + + + + +## 2.0.3 (2018-02-01) + +* chore: release 2.0.3 ([3c1abe5](https://github.com/vkarpov15/kareem/commit/3c1abe5)) +* fix: use process.nextTick() re: Automattic/mongoose#6074 ([e5bfe33](https://github.com/vkarpov15/kareem/commit/e5bfe33)), closes [Automattic/mongoose#6074](https://github.com/Automattic/mongoose/issues/6074) + + + + +## 2.0.2 (2018-01-24) + +* chore: fix license ([a9d755c](https://github.com/vkarpov15/kareem/commit/a9d755c)), closes [#10](https://github.com/vkarpov15/kareem/issues/10) +* chore: release 2.0.2 ([fe87ab6](https://github.com/vkarpov15/kareem/commit/fe87ab6)) + + + + +## 2.0.1 (2018-01-09) + +* chore: release 2.0.1 with lockfile bump ([09c44fb](https://github.com/vkarpov15/kareem/commit/09c44fb)) + + + + +## 2.0.0 (2018-01-09) + +* chore: bump marked re: security ([cc564a9](https://github.com/vkarpov15/kareem/commit/cc564a9)) +* chore: release 2.0.0 ([f511d1c](https://github.com/vkarpov15/kareem/commit/f511d1c)) + + + + +## 2.0.0-rc5 (2017-12-23) + +* chore: fix build on node 4+5 ([6dac5a4](https://github.com/vkarpov15/kareem/commit/6dac5a4)) +* chore: fix built on node 4 + 5 again ([434ef0a](https://github.com/vkarpov15/kareem/commit/434ef0a)) +* chore: release 2.0.0-rc5 ([25a32ee](https://github.com/vkarpov15/kareem/commit/25a32ee)) + + + + +## 2.0.0-rc4 (2017-12-22) + +* chore: release 2.0.0-rc4 ([49fc083](https://github.com/vkarpov15/kareem/commit/49fc083)) +* BREAKING CHANGE: deduplicate when merging hooks re: Automattic/mongoose#2945 ([d458573](https://github.com/vkarpov15/kareem/commit/d458573)), closes [Automattic/mongoose#2945](https://github.com/Automattic/mongoose/issues/2945) + + + + +## 2.0.0-rc3 (2017-12-22) + +* chore: release 2.0.0-rc3 ([adaaa00](https://github.com/vkarpov15/kareem/commit/adaaa00)) +* feat: support returning promises from middleware functions ([05b4480](https://github.com/vkarpov15/kareem/commit/05b4480)), closes [Automattic/mongoose#3779](https://github.com/Automattic/mongoose/issues/3779) + + + + +## 2.0.0-rc2 (2017-12-21) + +* chore: release 2.0.0-rc2 ([76325fa](https://github.com/vkarpov15/kareem/commit/76325fa)) +* fix: ensure next() and done() run in next tick ([6c20684](https://github.com/vkarpov15/kareem/commit/6c20684)) + + + + +## 2.0.0-rc1 (2017-12-21) + +* chore: improve test coverage re: Automattic/mongoose#3232 ([7b45cf0](https://github.com/vkarpov15/kareem/commit/7b45cf0)), closes [Automattic/mongoose#3232](https://github.com/Automattic/mongoose/issues/3232) +* chore: release 2.0.0-rc1 ([9b83f52](https://github.com/vkarpov15/kareem/commit/9b83f52)) +* BREAKING CHANGE: report sync exceptions as errors, only allow calling next() and done() once ([674adcc](https://github.com/vkarpov15/kareem/commit/674adcc)), closes [Automattic/mongoose#3483](https://github.com/Automattic/mongoose/issues/3483) + + + + +## 2.0.0-rc0 (2017-12-17) + +* chore: release 2.0.0-rc0 ([16b44b5](https://github.com/vkarpov15/kareem/commit/16b44b5)) +* BREAKING CHANGE: drop support for node < 4 ([9cbb8c7](https://github.com/vkarpov15/kareem/commit/9cbb8c7)) +* BREAKING CHANGE: remove useLegacyPost and add several new features ([6dd8531](https://github.com/vkarpov15/kareem/commit/6dd8531)), closes [Automattic/mongoose#3232](https://github.com/Automattic/mongoose/issues/3232) + + + + +## 1.5.0 (2017-07-20) + +* chore: release 1.5.0 ([9c491a0](https://github.com/vkarpov15/kareem/commit/9c491a0)) +* fix: improve post error handlers results ([9928dd5](https://github.com/vkarpov15/kareem/commit/9928dd5)), closes [Automattic/mongoose#5466](https://github.com/Automattic/mongoose/issues/5466) + + + + +## 1.4.2 (2017-07-06) + +* chore: release 1.4.2 ([8d14ac5](https://github.com/vkarpov15/kareem/commit/8d14ac5)) +* fix: correct args re: Automattic/mongoose#5405 ([3f28ae6](https://github.com/vkarpov15/kareem/commit/3f28ae6)), closes [Automattic/mongoose#5405](https://github.com/Automattic/mongoose/issues/5405) + + + + +## 1.4.1 (2017-04-25) + +* chore: release 1.4.1 ([5ecf0c2](https://github.com/vkarpov15/kareem/commit/5ecf0c2)) +* fix: handle numAsyncPres with clone() ([c72e857](https://github.com/vkarpov15/kareem/commit/c72e857)), closes [#8](https://github.com/vkarpov15/kareem/issues/8) +* test: repro #8 ([9b4d6b2](https://github.com/vkarpov15/kareem/commit/9b4d6b2)), closes [#8](https://github.com/vkarpov15/kareem/issues/8) + + + + +## 1.4.0 (2017-04-19) + +* chore: release 1.4.0 ([101c5f5](https://github.com/vkarpov15/kareem/commit/101c5f5)) +* feat: add merge() function ([285325e](https://github.com/vkarpov15/kareem/commit/285325e)) + + + + +## 1.3.0 (2017-03-26) + +* chore: release 1.3.0 ([f3a9e50](https://github.com/vkarpov15/kareem/commit/f3a9e50)) +* feat: pass function args to execPre ([4dd466d](https://github.com/vkarpov15/kareem/commit/4dd466d)) + + + + +## 1.2.1 (2017-02-03) + +* chore: release 1.2.1 ([d97081f](https://github.com/vkarpov15/kareem/commit/d97081f)) +* fix: filter out _kareemIgnored args for error handlers re: Automattic/mongoose#4925 ([ddc7aeb](https://github.com/vkarpov15/kareem/commit/ddc7aeb)), closes [Automattic/mongoose#4925](https://github.com/Automattic/mongoose/issues/4925) +* fix: make error handlers handle errors in pre hooks ([af38033](https://github.com/vkarpov15/kareem/commit/af38033)), closes [Automattic/mongoose#4927](https://github.com/Automattic/mongoose/issues/4927) + + + + +## 1.2.0 (2017-01-02) + +* chore: release 1.2.0 ([033225c](https://github.com/vkarpov15/kareem/commit/033225c)) +* chore: upgrade deps ([f9e9a09](https://github.com/vkarpov15/kareem/commit/f9e9a09)) +* feat: add _kareemIgnore re: Automattic/mongoose#4836 ([7957771](https://github.com/vkarpov15/kareem/commit/7957771)), closes [Automattic/mongoose#4836](https://github.com/Automattic/mongoose/issues/4836) + + + + +## 1.1.5 (2016-12-13) + +* chore: release 1.1.5 ([1a9f684](https://github.com/vkarpov15/kareem/commit/1a9f684)) +* fix: correct field name ([04a0e9d](https://github.com/vkarpov15/kareem/commit/04a0e9d)) + + + + +## 1.1.4 (2016-12-09) + +* chore: release 1.1.4 ([ece401c](https://github.com/vkarpov15/kareem/commit/ece401c)) +* chore: run tests on node 6 ([e0cb1cb](https://github.com/vkarpov15/kareem/commit/e0cb1cb)) +* fix: only copy own properties in clone() ([dfe28ce](https://github.com/vkarpov15/kareem/commit/dfe28ce)), closes [#7](https://github.com/vkarpov15/kareem/issues/7) + + + + +## 1.1.3 (2016-06-27) + +* chore: release 1.1.3 ([87171c8](https://github.com/vkarpov15/kareem/commit/87171c8)) +* fix: couple more issues with arg processing ([c65f523](https://github.com/vkarpov15/kareem/commit/c65f523)) + + + + +## 1.1.2 (2016-06-27) + +* chore: release 1.1.2 ([8e102b6](https://github.com/vkarpov15/kareem/commit/8e102b6)) +* fix: add early return ([4feda4e](https://github.com/vkarpov15/kareem/commit/4feda4e)) + + + + +## 1.1.1 (2016-06-27) + +* chore: release 1.1.1 ([8bb3050](https://github.com/vkarpov15/kareem/commit/8bb3050)) +* fix: skip error handlers if no error ([0eb3a44](https://github.com/vkarpov15/kareem/commit/0eb3a44)) + + + + +## 1.1.0 (2016-05-11) + +* chore: release 1.1.0 ([85332d9](https://github.com/vkarpov15/kareem/commit/85332d9)) +* chore: test on node 4 and node 5 ([1faefa1](https://github.com/vkarpov15/kareem/commit/1faefa1)) +* 100% coverage again ([c9aee4e](https://github.com/vkarpov15/kareem/commit/c9aee4e)) +* add support for error post hooks ([d378113](https://github.com/vkarpov15/kareem/commit/d378113)) +* basic setup for sync hooks #4 ([55aa081](https://github.com/vkarpov15/kareem/commit/55aa081)), closes [#4](https://github.com/vkarpov15/kareem/issues/4) +* proof of concept for error handlers ([e4a07d9](https://github.com/vkarpov15/kareem/commit/e4a07d9)) +* refactor out handleWrapError helper ([b19af38](https://github.com/vkarpov15/kareem/commit/b19af38)) + + + + +## 1.0.1 (2015-05-10) + +* Fix #1 ([de60dc6](https://github.com/vkarpov15/kareem/commit/de60dc6)), closes [#1](https://github.com/vkarpov15/kareem/issues/1) +* release 1.0.1 ([6971088](https://github.com/vkarpov15/kareem/commit/6971088)) +* Run tests on iojs in travis ([adcd201](https://github.com/vkarpov15/kareem/commit/adcd201)) +* support legacy post hook behavior in wrap() ([23fa74c](https://github.com/vkarpov15/kareem/commit/23fa74c)) +* Use node 0.12 in travis ([834689d](https://github.com/vkarpov15/kareem/commit/834689d)) + + + + +## 1.0.0 (2015-01-28) + +* Tag 1.0.0 ([4c5a35a](https://github.com/vkarpov15/kareem/commit/4c5a35a)) + + + + +## 0.0.8 (2015-01-27) + +* Add clone function ([688bba7](https://github.com/vkarpov15/kareem/commit/688bba7)) +* Add jscs for style checking ([5c93149](https://github.com/vkarpov15/kareem/commit/5c93149)) +* Bump 0.0.8 ([03c0d2f](https://github.com/vkarpov15/kareem/commit/03c0d2f)) +* Fix jscs config, add gulp rules ([9989abf](https://github.com/vkarpov15/kareem/commit/9989abf)) +* fix Makefile typo ([1f7e61a](https://github.com/vkarpov15/kareem/commit/1f7e61a)) + + + + +## 0.0.7 (2015-01-04) + +* Bump 0.0.7 ([98ef173](https://github.com/vkarpov15/kareem/commit/98ef173)) +* fix LearnBoost/mongoose#2553 - use null instead of undefined for err ([9157b48](https://github.com/vkarpov15/kareem/commit/9157b48)), closes [LearnBoost/mongoose#2553](https://github.com/LearnBoost/mongoose/issues/2553) +* Regenerate docs ([2331cdf](https://github.com/vkarpov15/kareem/commit/2331cdf)) + + + + +## 0.0.6 (2015-01-01) + +* Update docs and bump 0.0.6 ([92c12a7](https://github.com/vkarpov15/kareem/commit/92c12a7)) + + + + +## 0.0.5 (2015-01-01) + +* Add coverage rule to Makefile ([825a91c](https://github.com/vkarpov15/kareem/commit/825a91c)) +* Add coveralls to README ([fb52369](https://github.com/vkarpov15/kareem/commit/fb52369)) +* Add coveralls to travis ([93f6f15](https://github.com/vkarpov15/kareem/commit/93f6f15)) +* Add createWrapper() function ([ea77741](https://github.com/vkarpov15/kareem/commit/ea77741)) +* Add istanbul code coverage ([6eceeef](https://github.com/vkarpov15/kareem/commit/6eceeef)) +* Add some more comments for examples ([c5b0c6f](https://github.com/vkarpov15/kareem/commit/c5b0c6f)) +* Add travis ([e6dcb06](https://github.com/vkarpov15/kareem/commit/e6dcb06)) +* Add travis badge to docs ([ad8c9b3](https://github.com/vkarpov15/kareem/commit/ad8c9b3)) +* Add wrap() tests, 100% coverage ([6945be4](https://github.com/vkarpov15/kareem/commit/6945be4)) +* Better test coverage for execPost ([d9ad539](https://github.com/vkarpov15/kareem/commit/d9ad539)) +* Bump 0.0.5 ([69875b1](https://github.com/vkarpov15/kareem/commit/69875b1)) +* Docs fix ([15b7098](https://github.com/vkarpov15/kareem/commit/15b7098)) +* Fix silly mistake in docs generation ([50373eb](https://github.com/vkarpov15/kareem/commit/50373eb)) +* Fix typo in readme ([fec4925](https://github.com/vkarpov15/kareem/commit/fec4925)) +* Linkify travis badge ([92b25fe](https://github.com/vkarpov15/kareem/commit/92b25fe)) +* Make travis run coverage ([747157b](https://github.com/vkarpov15/kareem/commit/747157b)) +* Move travis status badge ([d52e89b](https://github.com/vkarpov15/kareem/commit/d52e89b)) +* Quick fix for coverage ([50bbddb](https://github.com/vkarpov15/kareem/commit/50bbddb)) +* Typo fix ([adea794](https://github.com/vkarpov15/kareem/commit/adea794)) + + + + +## 0.0.4 (2014-12-13) + +* Bump 0.0.4, run docs generation ([51a15fe](https://github.com/vkarpov15/kareem/commit/51a15fe)) +* Use correct post parameters in wrap() ([9bb5da3](https://github.com/vkarpov15/kareem/commit/9bb5da3)) + + + + +## 0.0.3 (2014-12-12) + +* Add npm test script, fix small bug with args not getting passed through post ([49e3e68](https://github.com/vkarpov15/kareem/commit/49e3e68)) +* Bump 0.0.3 ([65621d8](https://github.com/vkarpov15/kareem/commit/65621d8)) +* Update readme ([901388b](https://github.com/vkarpov15/kareem/commit/901388b)) + + + + +## 0.0.2 (2014-12-12) + +* Add github repo and bump 0.0.2 ([59db8be](https://github.com/vkarpov15/kareem/commit/59db8be)) + + + + +## 0.0.1 (2014-12-12) + +* Add basic docs ([ad29ea4](https://github.com/vkarpov15/kareem/commit/ad29ea4)) +* Add pre hooks ([2ffc356](https://github.com/vkarpov15/kareem/commit/2ffc356)) +* Add wrap function ([68c540c](https://github.com/vkarpov15/kareem/commit/68c540c)) +* Bump to version 0.0.1 ([a4bfd68](https://github.com/vkarpov15/kareem/commit/a4bfd68)) +* Initial commit ([4002458](https://github.com/vkarpov15/kareem/commit/4002458)) +* Initial deposit ([98fc489](https://github.com/vkarpov15/kareem/commit/98fc489)) +* Post hooks ([395b67c](https://github.com/vkarpov15/kareem/commit/395b67c)) +* Some basic setup work ([82df75e](https://github.com/vkarpov15/kareem/commit/82df75e)) +* Support sync pre hooks ([1cc1b9f](https://github.com/vkarpov15/kareem/commit/1cc1b9f)) +* Update package.json description ([978da18](https://github.com/vkarpov15/kareem/commit/978da18)) + + + + +## 2.2.5 (2018-09-24) + + + + + +## 2.2.4 (2018-09-24) + + + + + +## 2.2.3 (2018-09-24) + +* fix(filter): copy async pres correctly with `filter()` ([1b1ed8a](https://github.com/vkarpov15/kareem/commit/1b1ed8a)), closes [Automattic/mongoose#3054](https://github.com/Automattic/mongoose/issues/3054) +* feat: add filter() function ([1f641f4](https://github.com/vkarpov15/kareem/commit/1f641f4)) +* feat: support storing options on pre and post hooks ([59220b9](https://github.com/vkarpov15/kareem/commit/59220b9)) + + + + +## 2.2.3 (2018-09-10) + +* chore: release 2.2.3 ([af653a3](https://github.com/vkarpov15/kareem/commit/af653a3)) + + + + +## 2.2.2 (2018-09-10) + +* chore: release 2.2.2 ([3f0144d](https://github.com/vkarpov15/kareem/commit/3f0144d)) +* fix: allow merge() to not clone ([e628d65](https://github.com/vkarpov15/kareem/commit/e628d65)) + + + + +## 2.2.1 (2018-06-05) + +* chore: release 2.2.1 ([4625a64](https://github.com/vkarpov15/kareem/commit/4625a64)) +* chore: remove lockfile from git ([7f3e4e6](https://github.com/vkarpov15/kareem/commit/7f3e4e6)) +* fix: handle numAsync correctly when merging ([fef8e7e](https://github.com/vkarpov15/kareem/commit/fef8e7e)) +* test: repro issue with not copying numAsync ([952d9db](https://github.com/vkarpov15/kareem/commit/952d9db)) + + + + +## 2.2.0 (2018-06-05) + +* chore: release 2.2.0 ([ff9ad03](https://github.com/vkarpov15/kareem/commit/ff9ad03)) +* fix: use maps instead of objects for _pres and _posts so `toString()` doesn't get reported as having ([55df303](https://github.com/vkarpov15/kareem/commit/55df303)), closes [Automattic/mongoose#6538](https://github.com/Automattic/mongoose/issues/6538) + + + + +## 2.1.0 (2018-05-16) + +* chore: release 2.1.0 ([ba5f1bc](https://github.com/vkarpov15/kareem/commit/ba5f1bc)) +* feat: add option to check wrapped function return value for promises ([c9d7dd1](https://github.com/vkarpov15/kareem/commit/c9d7dd1)) +* refactor: use const in wrap() ([0fc21f9](https://github.com/vkarpov15/kareem/commit/0fc21f9)) + + + + +## 2.0.7 (2018-04-28) + +* chore: release 2.0.7 ([0bf91e6](https://github.com/vkarpov15/kareem/commit/0bf91e6)) +* feat: add `hasHooks()` ([225f18d](https://github.com/vkarpov15/kareem/commit/225f18d)), closes [Automattic/mongoose#6385](https://github.com/Automattic/mongoose/issues/6385) + + + + +## 2.0.6 (2018-03-22) + +* chore: release 2.0.6 ([f3d406b](https://github.com/vkarpov15/kareem/commit/f3d406b)) +* fix(wrap): ensure fast path still wraps function in `nextTick()` for chaining ([7000494](https://github.com/vkarpov15/kareem/commit/7000494)), closes [Automattic/mongoose#6250](https://github.com/Automattic/mongoose/issues/6250) [dsanel/mongoose-delete#36](https://github.com/dsanel/mongoose-delete/issues/36) + + + + +## 2.0.5 (2018-02-22) + +* chore: release 2.0.5 ([3286612](https://github.com/vkarpov15/kareem/commit/3286612)) +* perf(createWrapper): don't create wrapper if there are no hooks ([5afc5b9](https://github.com/vkarpov15/kareem/commit/5afc5b9)), closes [Automattic/mongoose#6126](https://github.com/Automattic/mongoose/issues/6126) + + + + +## 2.0.4 (2018-02-08) + +* chore: release 2.0.4 ([2ab0293](https://github.com/vkarpov15/kareem/commit/2ab0293)) + + + + +## 2.0.3 (2018-02-01) + +* chore: release 2.0.3 ([3c1abe5](https://github.com/vkarpov15/kareem/commit/3c1abe5)) +* fix: use process.nextTick() re: Automattic/mongoose#6074 ([e5bfe33](https://github.com/vkarpov15/kareem/commit/e5bfe33)), closes [Automattic/mongoose#6074](https://github.com/Automattic/mongoose/issues/6074) + + + + +## 2.0.2 (2018-01-24) + +* chore: fix license ([a9d755c](https://github.com/vkarpov15/kareem/commit/a9d755c)), closes [#10](https://github.com/vkarpov15/kareem/issues/10) +* chore: release 2.0.2 ([fe87ab6](https://github.com/vkarpov15/kareem/commit/fe87ab6)) + + + + +## 2.0.1 (2018-01-09) + +* chore: release 2.0.1 with lockfile bump ([09c44fb](https://github.com/vkarpov15/kareem/commit/09c44fb)) + + + + +## 2.0.0 (2018-01-09) + +* chore: bump marked re: security ([cc564a9](https://github.com/vkarpov15/kareem/commit/cc564a9)) +* chore: release 2.0.0 ([f511d1c](https://github.com/vkarpov15/kareem/commit/f511d1c)) + + + + +## 2.0.0-rc5 (2017-12-23) + +* chore: fix build on node 4+5 ([6dac5a4](https://github.com/vkarpov15/kareem/commit/6dac5a4)) +* chore: fix built on node 4 + 5 again ([434ef0a](https://github.com/vkarpov15/kareem/commit/434ef0a)) +* chore: release 2.0.0-rc5 ([25a32ee](https://github.com/vkarpov15/kareem/commit/25a32ee)) + + + + +## 2.0.0-rc4 (2017-12-22) + +* chore: release 2.0.0-rc4 ([49fc083](https://github.com/vkarpov15/kareem/commit/49fc083)) +* BREAKING CHANGE: deduplicate when merging hooks re: Automattic/mongoose#2945 ([d458573](https://github.com/vkarpov15/kareem/commit/d458573)), closes [Automattic/mongoose#2945](https://github.com/Automattic/mongoose/issues/2945) + + + + +## 2.0.0-rc3 (2017-12-22) + +* chore: release 2.0.0-rc3 ([adaaa00](https://github.com/vkarpov15/kareem/commit/adaaa00)) +* feat: support returning promises from middleware functions ([05b4480](https://github.com/vkarpov15/kareem/commit/05b4480)), closes [Automattic/mongoose#3779](https://github.com/Automattic/mongoose/issues/3779) + + + + +## 2.0.0-rc2 (2017-12-21) + +* chore: release 2.0.0-rc2 ([76325fa](https://github.com/vkarpov15/kareem/commit/76325fa)) +* fix: ensure next() and done() run in next tick ([6c20684](https://github.com/vkarpov15/kareem/commit/6c20684)) + + + + +## 2.0.0-rc1 (2017-12-21) + +* chore: improve test coverage re: Automattic/mongoose#3232 ([7b45cf0](https://github.com/vkarpov15/kareem/commit/7b45cf0)), closes [Automattic/mongoose#3232](https://github.com/Automattic/mongoose/issues/3232) +* chore: release 2.0.0-rc1 ([9b83f52](https://github.com/vkarpov15/kareem/commit/9b83f52)) +* BREAKING CHANGE: report sync exceptions as errors, only allow calling next() and done() once ([674adcc](https://github.com/vkarpov15/kareem/commit/674adcc)), closes [Automattic/mongoose#3483](https://github.com/Automattic/mongoose/issues/3483) + + + + +## 2.0.0-rc0 (2017-12-17) + +* chore: release 2.0.0-rc0 ([16b44b5](https://github.com/vkarpov15/kareem/commit/16b44b5)) +* BREAKING CHANGE: drop support for node < 4 ([9cbb8c7](https://github.com/vkarpov15/kareem/commit/9cbb8c7)) +* BREAKING CHANGE: remove useLegacyPost and add several new features ([6dd8531](https://github.com/vkarpov15/kareem/commit/6dd8531)), closes [Automattic/mongoose#3232](https://github.com/Automattic/mongoose/issues/3232) + + + + +## 1.5.0 (2017-07-20) + +* chore: release 1.5.0 ([9c491a0](https://github.com/vkarpov15/kareem/commit/9c491a0)) +* fix: improve post error handlers results ([9928dd5](https://github.com/vkarpov15/kareem/commit/9928dd5)), closes [Automattic/mongoose#5466](https://github.com/Automattic/mongoose/issues/5466) + + + + +## 1.4.2 (2017-07-06) + +* chore: release 1.4.2 ([8d14ac5](https://github.com/vkarpov15/kareem/commit/8d14ac5)) +* fix: correct args re: Automattic/mongoose#5405 ([3f28ae6](https://github.com/vkarpov15/kareem/commit/3f28ae6)), closes [Automattic/mongoose#5405](https://github.com/Automattic/mongoose/issues/5405) + + + + +## 1.4.1 (2017-04-25) + +* chore: release 1.4.1 ([5ecf0c2](https://github.com/vkarpov15/kareem/commit/5ecf0c2)) +* fix: handle numAsyncPres with clone() ([c72e857](https://github.com/vkarpov15/kareem/commit/c72e857)), closes [#8](https://github.com/vkarpov15/kareem/issues/8) +* test: repro #8 ([9b4d6b2](https://github.com/vkarpov15/kareem/commit/9b4d6b2)), closes [#8](https://github.com/vkarpov15/kareem/issues/8) + + + + +## 1.4.0 (2017-04-19) + +* chore: release 1.4.0 ([101c5f5](https://github.com/vkarpov15/kareem/commit/101c5f5)) +* feat: add merge() function ([285325e](https://github.com/vkarpov15/kareem/commit/285325e)) + + + + +## 1.3.0 (2017-03-26) + +* chore: release 1.3.0 ([f3a9e50](https://github.com/vkarpov15/kareem/commit/f3a9e50)) +* feat: pass function args to execPre ([4dd466d](https://github.com/vkarpov15/kareem/commit/4dd466d)) + + + + +## 1.2.1 (2017-02-03) + +* chore: release 1.2.1 ([d97081f](https://github.com/vkarpov15/kareem/commit/d97081f)) +* fix: filter out _kareemIgnored args for error handlers re: Automattic/mongoose#4925 ([ddc7aeb](https://github.com/vkarpov15/kareem/commit/ddc7aeb)), closes [Automattic/mongoose#4925](https://github.com/Automattic/mongoose/issues/4925) +* fix: make error handlers handle errors in pre hooks ([af38033](https://github.com/vkarpov15/kareem/commit/af38033)), closes [Automattic/mongoose#4927](https://github.com/Automattic/mongoose/issues/4927) + + + + +## 1.2.0 (2017-01-02) + +* chore: release 1.2.0 ([033225c](https://github.com/vkarpov15/kareem/commit/033225c)) +* chore: upgrade deps ([f9e9a09](https://github.com/vkarpov15/kareem/commit/f9e9a09)) +* feat: add _kareemIgnore re: Automattic/mongoose#4836 ([7957771](https://github.com/vkarpov15/kareem/commit/7957771)), closes [Automattic/mongoose#4836](https://github.com/Automattic/mongoose/issues/4836) + + + + +## 1.1.5 (2016-12-13) + +* chore: release 1.1.5 ([1a9f684](https://github.com/vkarpov15/kareem/commit/1a9f684)) +* fix: correct field name ([04a0e9d](https://github.com/vkarpov15/kareem/commit/04a0e9d)) + + + + +## 1.1.4 (2016-12-09) + +* chore: release 1.1.4 ([ece401c](https://github.com/vkarpov15/kareem/commit/ece401c)) +* chore: run tests on node 6 ([e0cb1cb](https://github.com/vkarpov15/kareem/commit/e0cb1cb)) +* fix: only copy own properties in clone() ([dfe28ce](https://github.com/vkarpov15/kareem/commit/dfe28ce)), closes [#7](https://github.com/vkarpov15/kareem/issues/7) + + + + +## 1.1.3 (2016-06-27) + +* chore: release 1.1.3 ([87171c8](https://github.com/vkarpov15/kareem/commit/87171c8)) +* fix: couple more issues with arg processing ([c65f523](https://github.com/vkarpov15/kareem/commit/c65f523)) + + + + +## 1.1.2 (2016-06-27) + +* chore: release 1.1.2 ([8e102b6](https://github.com/vkarpov15/kareem/commit/8e102b6)) +* fix: add early return ([4feda4e](https://github.com/vkarpov15/kareem/commit/4feda4e)) + + + + +## 1.1.1 (2016-06-27) + +* chore: release 1.1.1 ([8bb3050](https://github.com/vkarpov15/kareem/commit/8bb3050)) +* fix: skip error handlers if no error ([0eb3a44](https://github.com/vkarpov15/kareem/commit/0eb3a44)) + + + + +## 1.1.0 (2016-05-11) + +* chore: release 1.1.0 ([85332d9](https://github.com/vkarpov15/kareem/commit/85332d9)) +* chore: test on node 4 and node 5 ([1faefa1](https://github.com/vkarpov15/kareem/commit/1faefa1)) +* 100% coverage again ([c9aee4e](https://github.com/vkarpov15/kareem/commit/c9aee4e)) +* add support for error post hooks ([d378113](https://github.com/vkarpov15/kareem/commit/d378113)) +* basic setup for sync hooks #4 ([55aa081](https://github.com/vkarpov15/kareem/commit/55aa081)), closes [#4](https://github.com/vkarpov15/kareem/issues/4) +* proof of concept for error handlers ([e4a07d9](https://github.com/vkarpov15/kareem/commit/e4a07d9)) +* refactor out handleWrapError helper ([b19af38](https://github.com/vkarpov15/kareem/commit/b19af38)) + + + + +## 1.0.1 (2015-05-10) + +* Fix #1 ([de60dc6](https://github.com/vkarpov15/kareem/commit/de60dc6)), closes [#1](https://github.com/vkarpov15/kareem/issues/1) +* release 1.0.1 ([6971088](https://github.com/vkarpov15/kareem/commit/6971088)) +* Run tests on iojs in travis ([adcd201](https://github.com/vkarpov15/kareem/commit/adcd201)) +* support legacy post hook behavior in wrap() ([23fa74c](https://github.com/vkarpov15/kareem/commit/23fa74c)) +* Use node 0.12 in travis ([834689d](https://github.com/vkarpov15/kareem/commit/834689d)) + + + + +## 1.0.0 (2015-01-28) + +* Tag 1.0.0 ([4c5a35a](https://github.com/vkarpov15/kareem/commit/4c5a35a)) + + + + +## 0.0.8 (2015-01-27) + +* Add clone function ([688bba7](https://github.com/vkarpov15/kareem/commit/688bba7)) +* Add jscs for style checking ([5c93149](https://github.com/vkarpov15/kareem/commit/5c93149)) +* Bump 0.0.8 ([03c0d2f](https://github.com/vkarpov15/kareem/commit/03c0d2f)) +* Fix jscs config, add gulp rules ([9989abf](https://github.com/vkarpov15/kareem/commit/9989abf)) +* fix Makefile typo ([1f7e61a](https://github.com/vkarpov15/kareem/commit/1f7e61a)) + + + + +## 0.0.7 (2015-01-04) + +* Bump 0.0.7 ([98ef173](https://github.com/vkarpov15/kareem/commit/98ef173)) +* fix LearnBoost/mongoose#2553 - use null instead of undefined for err ([9157b48](https://github.com/vkarpov15/kareem/commit/9157b48)), closes [LearnBoost/mongoose#2553](https://github.com/LearnBoost/mongoose/issues/2553) +* Regenerate docs ([2331cdf](https://github.com/vkarpov15/kareem/commit/2331cdf)) + + + + +## 0.0.6 (2015-01-01) + +* Update docs and bump 0.0.6 ([92c12a7](https://github.com/vkarpov15/kareem/commit/92c12a7)) + + + + +## 0.0.5 (2015-01-01) + +* Add coverage rule to Makefile ([825a91c](https://github.com/vkarpov15/kareem/commit/825a91c)) +* Add coveralls to README ([fb52369](https://github.com/vkarpov15/kareem/commit/fb52369)) +* Add coveralls to travis ([93f6f15](https://github.com/vkarpov15/kareem/commit/93f6f15)) +* Add createWrapper() function ([ea77741](https://github.com/vkarpov15/kareem/commit/ea77741)) +* Add istanbul code coverage ([6eceeef](https://github.com/vkarpov15/kareem/commit/6eceeef)) +* Add some more comments for examples ([c5b0c6f](https://github.com/vkarpov15/kareem/commit/c5b0c6f)) +* Add travis ([e6dcb06](https://github.com/vkarpov15/kareem/commit/e6dcb06)) +* Add travis badge to docs ([ad8c9b3](https://github.com/vkarpov15/kareem/commit/ad8c9b3)) +* Add wrap() tests, 100% coverage ([6945be4](https://github.com/vkarpov15/kareem/commit/6945be4)) +* Better test coverage for execPost ([d9ad539](https://github.com/vkarpov15/kareem/commit/d9ad539)) +* Bump 0.0.5 ([69875b1](https://github.com/vkarpov15/kareem/commit/69875b1)) +* Docs fix ([15b7098](https://github.com/vkarpov15/kareem/commit/15b7098)) +* Fix silly mistake in docs generation ([50373eb](https://github.com/vkarpov15/kareem/commit/50373eb)) +* Fix typo in readme ([fec4925](https://github.com/vkarpov15/kareem/commit/fec4925)) +* Linkify travis badge ([92b25fe](https://github.com/vkarpov15/kareem/commit/92b25fe)) +* Make travis run coverage ([747157b](https://github.com/vkarpov15/kareem/commit/747157b)) +* Move travis status badge ([d52e89b](https://github.com/vkarpov15/kareem/commit/d52e89b)) +* Quick fix for coverage ([50bbddb](https://github.com/vkarpov15/kareem/commit/50bbddb)) +* Typo fix ([adea794](https://github.com/vkarpov15/kareem/commit/adea794)) + + + + +## 0.0.4 (2014-12-13) + +* Bump 0.0.4, run docs generation ([51a15fe](https://github.com/vkarpov15/kareem/commit/51a15fe)) +* Use correct post parameters in wrap() ([9bb5da3](https://github.com/vkarpov15/kareem/commit/9bb5da3)) + + + + +## 0.0.3 (2014-12-12) + +* Add npm test script, fix small bug with args not getting passed through post ([49e3e68](https://github.com/vkarpov15/kareem/commit/49e3e68)) +* Bump 0.0.3 ([65621d8](https://github.com/vkarpov15/kareem/commit/65621d8)) +* Update readme ([901388b](https://github.com/vkarpov15/kareem/commit/901388b)) + + + + +## 0.0.2 (2014-12-12) + +* Add github repo and bump 0.0.2 ([59db8be](https://github.com/vkarpov15/kareem/commit/59db8be)) + + + + +## 0.0.1 (2014-12-12) + +* Add basic docs ([ad29ea4](https://github.com/vkarpov15/kareem/commit/ad29ea4)) +* Add pre hooks ([2ffc356](https://github.com/vkarpov15/kareem/commit/2ffc356)) +* Add wrap function ([68c540c](https://github.com/vkarpov15/kareem/commit/68c540c)) +* Bump to version 0.0.1 ([a4bfd68](https://github.com/vkarpov15/kareem/commit/a4bfd68)) +* Initial commit ([4002458](https://github.com/vkarpov15/kareem/commit/4002458)) +* Initial deposit ([98fc489](https://github.com/vkarpov15/kareem/commit/98fc489)) +* Post hooks ([395b67c](https://github.com/vkarpov15/kareem/commit/395b67c)) +* Some basic setup work ([82df75e](https://github.com/vkarpov15/kareem/commit/82df75e)) +* Support sync pre hooks ([1cc1b9f](https://github.com/vkarpov15/kareem/commit/1cc1b9f)) +* Update package.json description ([978da18](https://github.com/vkarpov15/kareem/commit/978da18)) diff --git a/gateway/node_modules/kareem/LICENSE b/gateway/node_modules/kareem/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..b0d46d3c2867d3ae9842da77133e6efd838f6c7f --- /dev/null +++ b/gateway/node_modules/kareem/LICENSE @@ -0,0 +1,202 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2014-2022 mongoosejs + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/gateway/node_modules/kareem/README.md b/gateway/node_modules/kareem/README.md new file mode 100644 index 0000000000000000000000000000000000000000..92b01c9f9940b2593bfe3b891284a7d3acddcab2 --- /dev/null +++ b/gateway/node_modules/kareem/README.md @@ -0,0 +1,385 @@ +# kareem + + [![Build Status](https://github.com/mongoosejs/kareem/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/mongoosejs/kareem/actions/workflows/test.yml) + + +Re-imagined take on the [hooks](http://npmjs.org/package/hooks) module, meant to offer additional flexibility in allowing you to execute hooks whenever necessary, as opposed to simply wrapping a single function. + +Named for the NBA's 2nd all-time leading scorer Kareem Abdul-Jabbar, known for his mastery of the [hook shot](http://en.wikipedia.org/wiki/Kareem_Abdul-Jabbar#Skyhook) + + + + + +# API + +## pre hooks + +Much like [hooks](https://npmjs.org/package/hooks), kareem lets you define +pre and post hooks: pre hooks are called before a given function executes. +Unlike hooks, kareem stores hooks and other internal state in a separate +object, rather than relying on inheritance. Furthermore, kareem exposes +an `execPre()` function that allows you to execute your pre hooks when +appropriate, giving you more fine-grained control over your function hooks. + +### It runs without any hooks specified + +```javascript +await hooks.execPre('cook', null); +``` + +### It runs basic serial pre hooks + +pre hook functions can return a promise that resolves when finished. + +```javascript +let count = 0; + +hooks.pre('cook', function() { + ++count; + return Promise.resolve(); +}); + +await hooks.execPre('cook', null); +assert.equal(1, count); +``` + +### It can run multiple pre hooks + +```javascript +let count1 = 0; +let count2 = 0; + +hooks.pre('cook', function() { + ++count1; + return Promise.resolve(); +}); + +hooks.pre('cook', function() { + ++count2; + return Promise.resolve(); +}); + +await hooks.execPre('cook', null); +assert.equal(1, count1); +assert.equal(1, count2); +``` + +### It can run fully synchronous pre hooks + +If your pre hook function takes no parameters, its assumed to be +fully synchronous. + +```javascript +let count1 = 0; +let count2 = 0; + +hooks.pre('cook', function() { + ++count1; +}); + +hooks.pre('cook', function() { + ++count2; +}); + +await hooks.execPre('cook', null); +assert.equal(1, count1); +assert.equal(1, count2); +``` + +### It properly attaches context to pre hooks + +Pre save hook functions are bound to the second parameter to `execPre()` + +```javascript +hooks.pre('cook', function() { + this.bacon = 3; +}); + +hooks.pre('cook', function() { + this.eggs = 4; +}); + +const obj = { bacon: 0, eggs: 0 }; + +// In the pre hooks, `this` will refer to `obj` +await hooks.execPre('cook', obj); +assert.equal(3, obj.bacon); +assert.equal(4, obj.eggs); +``` + +### It supports returning a promise + +You can also return a promise from your pre hooks instead of calling +`next()`. When the returned promise resolves, kareem will kick off the +next middleware. + +```javascript +hooks.pre('cook', function() { + return new Promise(resolve => { + setTimeout(() => { + this.bacon = 3; + resolve(); + }, 100); + }); +}); + +const obj = { bacon: 0 }; + +await hooks.execPre('cook', obj); +assert.equal(3, obj.bacon); +``` + +### It supports filtering which hooks to run + +You can pass a `filter` option to `execPre()` to select which hooks +to run. The filter function receives each hook object and should return +`true` to run the hook or `false` to skip it. + +```javascript +const execed = []; + +const fn1 = function() { execed.push('first'); }; +fn1.skipMe = true; +hooks.pre('cook', fn1); + +const fn2 = function() { execed.push('second'); }; +hooks.pre('cook', fn2); + +// Only runs fn2, skips fn1 because fn1.skipMe is true +await hooks.execPre('cook', null, [], { + filter: hook => !hook.fn.skipMe +}); + +assert.deepStrictEqual(execed, ['second']); +``` + +## post hooks + +### It runs without any hooks specified + +```javascript +const [eggs] = await hooks.execPost('cook', null, [1]); +assert.equal(eggs, 1); +``` + +### It executes with parameters passed in + +```javascript +hooks.post('cook', function(eggs, bacon, callback) { + assert.equal(eggs, 1); + assert.equal(bacon, 2); + callback(); +}); + +const [eggs, bacon] = await hooks.execPost('cook', null, [1, 2]); +assert.equal(eggs, 1); +assert.equal(bacon, 2); +``` + +### It can use synchronous post hooks + +```javascript +const execed = {}; + +hooks.post('cook', function(eggs, bacon) { + execed.first = true; + assert.equal(eggs, 1); + assert.equal(bacon, 2); +}); + +hooks.post('cook', function(eggs, bacon, callback) { + execed.second = true; + assert.equal(eggs, 1); + assert.equal(bacon, 2); + callback(); +}); + +const [eggs, bacon] = await hooks.execPost('cook', null, [1, 2]); +assert.equal(Object.keys(execed).length, 2); +assert.ok(execed.first); +assert.ok(execed.second); +assert.equal(eggs, 1); +assert.equal(bacon, 2); +``` + +### It supports returning a promise + +You can also return a promise from your post hooks instead of calling +`next()`. When the returned promise resolves, kareem will kick off the +next middleware. + +```javascript +hooks.post('cook', function() { + return new Promise(resolve => { + setTimeout(() => { + this.bacon = 3; + resolve(); + }, 100); + }); +}); + +const obj = { bacon: 0 }; + +await hooks.execPost('cook', obj, [obj]); +assert.equal(obj.bacon, 3); +``` + +### It supports filtering which hooks to run + +You can pass a `filter` option to `execPost()` to select which hooks +to run. The filter function receives each hook object and should return +`true` to run the hook or `false` to skip it. + +```javascript +const execed = []; + +const fn1 = function() { execed.push('first'); }; +fn1.skipMe = true; +hooks.post('cook', fn1); + +const fn2 = function() { execed.push('second'); }; +hooks.post('cook', fn2); + +// Only runs fn2, skips fn1 because fn1.skipMe is true +await hooks.execPost('cook', null, [], { + filter: hook => !hook.fn.skipMe +}); + +assert.deepStrictEqual(execed, ['second']); +``` + +## wrap() + +### It wraps pre and post calls into one call + +```javascript +hooks.pre('cook', function() { + return new Promise(resolve => { + this.bacon = 3; + setTimeout(() => { + resolve(); + }, 5); + }); +}); + +hooks.pre('cook', function() { + this.eggs = 4; + return Promise.resolve(); +}); + +hooks.pre('cook', function() { + this.waffles = false; + return Promise.resolve(); +}); + +hooks.post('cook', function(obj) { + obj.tofu = 'no'; +}); + +const obj = { bacon: 0, eggs: 0 }; + +const args = [obj]; + +const result = await hooks.wrap( + 'cook', + function(o) { + assert.equal(obj.bacon, 3); + assert.equal(obj.eggs, 4); + assert.equal(obj.waffles, false); + assert.equal(obj.tofu, undefined); + return o; + }, + obj, + args); + +assert.equal(obj.bacon, 3); +assert.equal(obj.eggs, 4); +assert.equal(obj.waffles, false); +assert.equal(obj.tofu, 'no'); +assert.equal(result, obj); +``` + +## createWrapper() + +### It wraps wrap() into a callable function + +```javascript +hooks.pre('cook', function() { + this.bacon = 3; + return Promise.resolve(); +}); + +hooks.pre('cook', function() { + return new Promise(resolve => { + this.eggs = 4; + setTimeout(function() { + resolve(); + }, 10); + }); +}); + +hooks.pre('cook', function() { + this.waffles = false; + return Promise.resolve(); +}); + +hooks.post('cook', function(obj) { + obj.tofu = 'no'; +}); + +const obj = { bacon: 0, eggs: 0 }; + +const cook = hooks.createWrapper( + 'cook', + function(o) { + assert.equal(3, obj.bacon); + assert.equal(4, obj.eggs); + assert.equal(false, obj.waffles); + assert.equal(undefined, obj.tofu); + return o; + }, + obj); + +const result = await cook(obj); +assert.equal(obj.bacon, 3); +assert.equal(obj.eggs, 4); +assert.equal(obj.waffles, false); +assert.equal(obj.tofu, 'no'); + +assert.equal(result, obj); +``` + +## clone() + +### It clones a Kareem object + +```javascript +const k1 = new Kareem(); +k1.pre('cook', function() {}); +k1.post('cook', function() {}); + +const k2 = k1.clone(); +assert.deepEqual(Array.from(k2._pres.keys()), ['cook']); +assert.deepEqual(Array.from(k2._posts.keys()), ['cook']); +``` + +## merge() + +### It pulls hooks from another Kareem object + +```javascript +const k1 = new Kareem(); +const test1 = function() {}; +k1.pre('cook', test1); +k1.post('cook', function() {}); + +const k2 = new Kareem(); +const test2 = function() {}; +k2.pre('cook', test2); +const k3 = k2.merge(k1); +assert.equal(k3._pres.get('cook').length, 2); +assert.equal(k3._pres.get('cook')[0].fn, test2); +assert.equal(k3._pres.get('cook')[1].fn, test1); +assert.equal(k3._posts.get('cook').length, 1); +``` diff --git a/gateway/node_modules/kareem/SECURITY.md b/gateway/node_modules/kareem/SECURITY.md new file mode 100644 index 0000000000000000000000000000000000000000..da9c516dd744b6c88dd8c91f58f7bcf8f7e8341b --- /dev/null +++ b/gateway/node_modules/kareem/SECURITY.md @@ -0,0 +1,5 @@ +## Security contact information + +To report a security vulnerability, please use the +[Tidelift security contact](https://tidelift.com/security). +Tidelift will coordinate the fix and disclosure. diff --git a/gateway/node_modules/kareem/index.d.ts b/gateway/node_modules/kareem/index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..3688c1deec122abd1d45b6ec0383c404e75c9595 --- /dev/null +++ b/gateway/node_modules/kareem/index.d.ts @@ -0,0 +1,31 @@ +declare module "kareem" { + export default class Kareem { + static skipWrappedFunction(): SkipWrappedFunction; + static overwriteMiddlewareResult(): OverwriteMiddlewareResult; + static overwriteArguments(): OverwriteArguments; + + pre(name: string | RegExp, fn: Function): this; + pre(name: string | RegExp, options: Record, fn: Function, error?: any, unshift?: boolean): this; + post(name: string | RegExp, fn: Function): this; + post(name: string | RegExp, options: Record, fn: Function, unshift?: boolean): this; + + clone(): Kareem; + merge(other: Kareem, clone?: boolean): this; + + createWrapper(name: string, fn: Function, context?: any, options?: Record): Function; + createWrapperSync(name: string, fn: Function): Function; + hasHooks(name: string): boolean; + filter(fn: Function): Kareem; + + wrap(name: string, fn: Function, context: any, args: any[], options?: Record): Function; + + execPostSync(name: string, context: any, args: any[]): any; + execPost(name: string, context: any, args: any[], options?: Record, callback?: Function): void; + execPreSync(name: string, context: any, args: any[]): any; + execPre(name: string, context: any, args: any[], callback?: Function): void; + } + + class SkipWrappedFunction {} + class OverwriteMiddlewareResult {} + class OverwriteArguments {} +} diff --git a/gateway/node_modules/kareem/index.js b/gateway/node_modules/kareem/index.js new file mode 100644 index 0000000000000000000000000000000000000000..022164ebecbbcd884f74bc43343578ac0dba6d65 --- /dev/null +++ b/gateway/node_modules/kareem/index.js @@ -0,0 +1,553 @@ +'use strict'; + +/** + * Create a new instance + */ +function Kareem() { + this._pres = new Map(); + this._posts = new Map(); +} + +Kareem.skipWrappedFunction = function skipWrappedFunction() { + if (!(this instanceof Kareem.skipWrappedFunction)) { + return new Kareem.skipWrappedFunction(...arguments); + } + + this.args = [...arguments]; +}; + +Kareem.overwriteResult = function overwriteResult() { + if (!(this instanceof Kareem.overwriteResult)) { + return new Kareem.overwriteResult(...arguments); + } + + this.args = [...arguments]; +}; + +Kareem.overwriteArguments = function overwriteArguments() { + if (!(this instanceof Kareem.overwriteArguments)) { + return new Kareem.overwriteArguments(...arguments); + } + + this.args = [...arguments]; +}; + +/** + * Execute all "pre" hooks for "name" + * @param {String} name The hook name to execute + * @param {*} context Overwrite the "this" for the hook + * @param {Array} args arguments passed to the pre hooks + * @param {Object} [options] Optional options + * @param {Function} [options.filter] Filter function to select which hooks to run + * @returns {Array} The potentially modified arguments + */ +Kareem.prototype.execPre = async function execPre(name, context, args, options) { + let pres = this._pres.get(name) || []; + if (options?.filter) { + pres = pres.filter(options.filter); + } + const numPres = pres.length; + let $args = args; + let skipWrappedFunction = null; + + if (!numPres) { + return $args; + } + + for (const pre of pres) { + try { + const maybePromiseLike = pre.fn.apply(context, $args); + if (isPromiseLike(maybePromiseLike)) { + const result = await maybePromiseLike; + if (result instanceof Kareem.overwriteArguments) { + $args = result.args; + } + } else if (maybePromiseLike instanceof Kareem.overwriteArguments) { + $args = maybePromiseLike.args; + } + } catch (error) { + if (error instanceof Kareem.skipWrappedFunction) { + skipWrappedFunction = error; + continue; + } + if (error instanceof Kareem.overwriteArguments) { + $args = error.args; + continue; + } + throw error; + } + } + + if (skipWrappedFunction) { + throw skipWrappedFunction; + } + + return $args; +}; + +/** + * Execute all "pre" hooks for "name" synchronously + * @param {String} name The hook name to execute + * @param {*} context Overwrite the "this" for the hook + * @param {Array} [args] Apply custom arguments to the hook + * @param {Object} [options] Optional options + * @param {Function} [options.filter] Filter function to select which hooks to run + * @returns {Array} The potentially modified arguments + */ +Kareem.prototype.execPreSync = function(name, context, args, options) { + let pres = this._pres.get(name) || []; + if (options?.filter) { + pres = pres.filter(options.filter); + } + const numPres = pres.length; + let $args = args || []; + + for (let i = 0; i < numPres; ++i) { + const result = pres[i].fn.apply(context, $args); + if (result instanceof Kareem.overwriteArguments) { + $args = result.args; + } + } + + return $args; +}; + +/** + * Execute all "post" hooks for "name" + * @param {String} name The hook name to execute + * @param {*} context Overwrite the "this" for the hook + * @param {Array} args Apply custom arguments to the hook + * @param {Object} [options] Optional options + * @param {Error} [options.error] Error to pass to error-handling middleware + * @param {Function} [options.filter] Filter function to select which hooks to run + * @returns {void} + */ +Kareem.prototype.execPost = async function execPost(name, context, args, options) { + let posts = this._posts.get(name) || []; + if (options?.filter) { + posts = posts.filter(options.filter); + } + const numPosts = posts.length; + + let firstError = null; + if (options && options.error) { + firstError = options.error; + } + + if (!numPosts) { + if (firstError != null) { + throw firstError; + } + return args; + } + + let cbPromise = null; + let resolve; + let reject; + const nextCallback = function nextCallback(err) { + if (err) { + reject(err); + } else { + resolve(); + } + }; + + let newArgs = args.slice(); + _handleNumCallbackParams(newArgs, options?.numCallbackParams); + let numArgs = newArgs.length; + newArgs.push(nextCallback); + let errorArgs = options?.error ? [firstError, ...newArgs] : null; + + for (const currentPost of posts) { + const post = currentPost.fn; + + cbPromise = new Promise((_resolve, _reject) => { + resolve = _resolve; + reject = _reject; + }); + + if (firstError) { + if (isErrorHandlingMiddleware(currentPost, numArgs)) { + try { + const res = post.apply(context, errorArgs); + if (isPromiseLike(res)) { + await res; + } else if (post.length === numArgs + 2) { + // `numArgs + 2` because we added the error and the callback + await cbPromise; + } + } catch (error) { + if (error instanceof Kareem.overwriteResult) { + args = error.args; + newArgs = args.slice(); + _handleNumCallbackParams(newArgs, options?.numCallbackParams); + numArgs = newArgs.length; + newArgs.push(nextCallback); + continue; + } + firstError = error; + errorArgs = [firstError, ...newArgs]; + } + } else { + continue; + } + } else { + if (isErrorHandlingMiddleware(currentPost, numArgs)) { + // Skip error handlers if no error + continue; + } else { + let res = null; + try { + res = post.apply(context, newArgs); + if (isPromiseLike(res)) { + res = await res; + } else if (post.length === numArgs + 1) { + // If post function takes a callback, wait for the post function to call the callback + res = await cbPromise; + } + } catch (error) { + if (error instanceof Kareem.overwriteResult) { + args = error.args; + newArgs = args.slice(); + _handleNumCallbackParams(newArgs, options?.numCallbackParams); + numArgs = newArgs.length; + newArgs.push(nextCallback); + errorArgs = [firstError, ...newArgs]; + continue; + } + firstError = error; + errorArgs = [firstError, ...newArgs]; + continue; + } + + if (res instanceof Kareem.overwriteResult) { + args = res.args; + newArgs = args.slice(); + _handleNumCallbackParams(newArgs, options?.numCallbackParams); + numArgs = newArgs.length; + newArgs.push(nextCallback); + continue; + } + } + } + } + + if (firstError != null) { + throw firstError; + } + + return args; +}; + +/*! + * Handle the `numCallbackParams` option for `execPostSync`: fill `newArgs` with `null` until + * length is `numCallbackParams` if `numCallbackParams` is a number. + * + * @param {Array} newArgs The arguments to fill + * @param {number|null|undefined} numCallbackParams The number of callback parameters + */ + +function _handleNumCallbackParams(newArgs, numCallbackParams) { + if (typeof numCallbackParams === 'number' && numCallbackParams > newArgs.length) { + for (let i = newArgs.length; i < numCallbackParams; ++i) { + newArgs.push(null); + } + } +} + +/** + * Execute all "post" hooks for "name" synchronously + * @param {String} name The hook name to execute + * @param {*} context Overwrite the "this" for the hook + * @param {Array} args Apply custom arguments to the hook + * @param {Object} [options] Optional options + * @param {Function} [options.filter] Filter function to select which hooks to run + * @returns {Array} The used arguments + */ +Kareem.prototype.execPostSync = function(name, context, args, options) { + let posts = this._posts.get(name) || []; + if (options?.filter) { + posts = posts.filter(options.filter); + } + const numPosts = posts.length; + + for (let i = 0; i < numPosts; ++i) { + const res = posts[i].fn.apply(context, args || []); + if (res instanceof Kareem.overwriteResult) { + args = res.args; + } + } + + return args; +}; + +/** + * Create a synchronous wrapper for "fn" + * @param {String} name The name of the hook + * @param {Function} fn The function to wrap + * @param {*} context Overwrite the "this" for the hook. If null/undefined, uses the calling context. + * @param {Object} [options] Options for the wrapper + * @param {Function} [options.getOptions] Function that receives the wrapper arguments and returns options for execPreSync/execPostSync. Can return `{ filter }` for both, or `{ pre: { filter }, post: { filter } }` for separate options. + * @returns {Function} The wrapped function + */ +Kareem.prototype.createWrapperSync = function(name, fn, context, options) { + const _this = this; + const getOptions = options?.getOptions; + return function syncWrapper() { + const _context = context ?? this; + const args = Array.from(arguments); + const execOptions = typeof getOptions === 'function' ? getOptions(args) : {}; + const preOptions = execOptions.pre ?? execOptions; + const postOptions = execOptions.post ?? execOptions; + + const modifiedArgs = _this.execPreSync(name, _context, args, preOptions); + + const toReturn = fn.apply(_context, modifiedArgs); + + const result = _this.execPostSync(name, _context, [toReturn], postOptions); + + return result[0]; + }; +}; + +/** + * Executes pre hooks, followed by the wrapped function, followed by post hooks. + * @param {String} name The name of the hook + * @param {Function} fn The function for the hook + * @param {*} context Overwrite the "this" for the hook + * @param {Array} args Apply custom arguments to the hook + * @param {Object} options Additional options for the hook + * @returns {void} + */ +Kareem.prototype.wrap = async function wrap(name, fn, context, args, options) { + let ret; + let skipWrappedFunction = false; + let modifiedArgs = args; + try { + modifiedArgs = await this.execPre(name, context, args); + } catch (error) { + if (error instanceof Kareem.skipWrappedFunction) { + ret = error.args; + skipWrappedFunction = true; + } else { + await this.execPost(name, context, args, { ...options, error }); + } + } + + if (!skipWrappedFunction) { + ret = await fn.apply(context, modifiedArgs); + } + + ret = await this.execPost(name, context, [ret], options); + + return ret[0]; +}; + +/** + * Filter current instance for something specific and return the filtered clone + * @param {Function} fn The filter function + * @returns {Kareem} The cloned and filtered instance + */ +Kareem.prototype.filter = function(fn) { + const clone = this.clone(); + + const pres = Array.from(clone._pres.keys()); + for (const name of pres) { + const hooks = this._pres.get(name). + map(h => Object.assign({}, h, { name: name })). + filter(fn); + + if (hooks.length === 0) { + clone._pres.delete(name); + continue; + } + + clone._pres.set(name, hooks); + } + + const posts = Array.from(clone._posts.keys()); + for (const name of posts) { + const hooks = this._posts.get(name). + map(h => Object.assign({}, h, { name: name })). + filter(fn); + + if (hooks.length === 0) { + clone._posts.delete(name); + continue; + } + + clone._posts.set(name, hooks); + } + + return clone; +}; + +/** + * Check for a "name" to exist either in pre or post hooks + * @param {String} name The name of the hook + * @returns {Boolean} "true" if found, "false" otherwise + */ +Kareem.prototype.hasHooks = function(name) { + return this._pres.has(name) || this._posts.has(name); +}; + +/** + * Create a Wrapper for "fn" on "name" and return the wrapped function + * @param {String} name The name of the hook + * @param {Function} fn The function to wrap + * @param {*} context Overwrite the "this" for the hook + * @param {Object} [options] + * @returns {Function} The wrapped function + */ +Kareem.prototype.createWrapper = function(name, fn, context, options) { + const _this = this; + if (!this.hasHooks(name)) { + // Fast path: if there's no hooks for this function, just return the function + return fn; + } + return function kareemWrappedFunction() { + const _context = context || this; + return _this.wrap(name, fn, _context, Array.from(arguments), options); + }; +}; + +/** + * Register a new hook for "pre" + * @param {String} name The name of the hook + * @param {Object} [options] + * @param {Function} fn The function to register for "name" + * @param {never} error Unused + * @param {Boolean} [unshift] Wheter to "push" or to "unshift" the new hook + * @returns {Kareem} + */ +Kareem.prototype.pre = function(name, options, fn, error, unshift) { + if (typeof options === 'function') { + fn = options; + options = {}; + } else if (options == null) { + options = {}; + } + + const pres = this._pres.get(name) || []; + this._pres.set(name, pres); + + if (typeof fn !== 'function') { + throw new Error('pre() requires a function, got "' + typeof fn + '"'); + } + + if (unshift) { + pres.unshift(Object.assign({}, options, { fn: fn })); + } else { + pres.push(Object.assign({}, options, { fn: fn })); + } + + return this; +}; + +/** + * Register a new hook for "post" + * @param {String} name The name of the hook + * @param {Object} [options] + * @param {Boolean} [options.errorHandler] Whether this is an error handler + * @param {Function} fn The function to register for "name" + * @param {Boolean} [unshift] Wheter to "push" or to "unshift" the new hook + * @returns {Kareem} + */ +Kareem.prototype.post = function(name, options, fn, unshift) { + const posts = this._posts.get(name) || []; + + if (typeof options === 'function') { + unshift = !!fn; + fn = options; + options = {}; + } + + if (typeof fn !== 'function') { + throw new Error('post() requires a function, got "' + typeof fn + '"'); + } + + if (unshift) { + posts.unshift(Object.assign({}, options, { fn: fn })); + } else { + posts.push(Object.assign({}, options, { fn: fn })); + } + this._posts.set(name, posts); + return this; +}; + +/** + * Register a new error handler for "name" + * @param {String} name The name of the hook + * @param {Object} [options] + * @param {Function} fn The function to register for "name" + * @param {Boolean} [unshift] Wheter to "push" or to "unshift" the new hook + * @returns {Kareem} + */ + +Kareem.prototype.postError = function postError(name, options, fn, unshift) { + if (typeof options === 'function') { + unshift = !!fn; + fn = options; + options = {}; + } + return this.post(name, { ...options, errorHandler: true }, fn, unshift); +}; + +/** + * Clone the current instance + * @returns {Kareem} The cloned instance + */ +Kareem.prototype.clone = function() { + const n = new Kareem(); + + for (const key of this._pres.keys()) { + const clone = this._pres.get(key).slice(); + n._pres.set(key, clone); + } + for (const key of this._posts.keys()) { + n._posts.set(key, this._posts.get(key).slice()); + } + + return n; +}; + +/** + * Merge "other" into self or "clone" + * @param {Kareem} other The instance to merge with + * @param {Kareem} [clone] The instance to merge onto (if not defined, using "this") + * @returns {Kareem} The merged instance + */ +Kareem.prototype.merge = function(other, clone) { + clone = arguments.length === 1 ? true : clone; + const ret = clone ? this.clone() : this; + + for (const key of other._pres.keys()) { + const sourcePres = ret._pres.get(key) || []; + const deduplicated = other._pres.get(key). + // Deduplicate based on `fn` + filter(p => sourcePres.map(_p => _p.fn).indexOf(p.fn) === -1); + const combined = sourcePres.concat(deduplicated); + ret._pres.set(key, combined); + } + for (const key of other._posts.keys()) { + const sourcePosts = ret._posts.get(key) || []; + const deduplicated = other._posts.get(key). + filter(p => sourcePosts.indexOf(p) === -1); + ret._posts.set(key, sourcePosts.concat(deduplicated)); + } + + return ret; +}; + +function isPromiseLike(v) { + return (typeof v === 'object' && v !== null && typeof v.then === 'function'); +} + +function isErrorHandlingMiddleware(post, numArgs) { + if (post.errorHandler) { + return true; + } + return post.fn.length === numArgs + 2; +} + +module.exports = Kareem; diff --git a/gateway/node_modules/kareem/package.json b/gateway/node_modules/kareem/package.json new file mode 100644 index 0000000000000000000000000000000000000000..340b663806d46ea7b3efa04c51eb53ac609b6021 --- /dev/null +++ b/gateway/node_modules/kareem/package.json @@ -0,0 +1,28 @@ +{ + "name": "kareem", + "version": "3.3.0", + "description": "Next-generation take on pre/post function hooks", + "main": "index.js", + "scripts": { + "lint": "eslint .", + "test": "mocha ./test/*", + "test-coverage": "nyc --reporter lcov mocha ./test/*", + "docs": "node ./docs.js" + }, + "repository": { + "type": "git", + "url": "git://github.com/mongoosejs/kareem.git" + }, + "devDependencies": { + "acquit": "1.x", + "acquit-ignore": "0.2.x", + "eslint": "8.20.0", + "mocha": "11.x", + "nyc": "15.1.0" + }, + "author": "Valeri Karpov ", + "license": "Apache-2.0", + "engines": { + "node": ">=18.0.0" + } +} diff --git a/gateway/node_modules/memory-pager/.travis.yml b/gateway/node_modules/memory-pager/.travis.yml new file mode 100644 index 0000000000000000000000000000000000000000..1c4ab31e974d37bcfd286980ffa53891b2e5b608 --- /dev/null +++ b/gateway/node_modules/memory-pager/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - '4' + - '6' diff --git a/gateway/node_modules/memory-pager/LICENSE b/gateway/node_modules/memory-pager/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..56fce0895e13ffe83650a7274d79fbcfb80d3e2f --- /dev/null +++ b/gateway/node_modules/memory-pager/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2017 Mathias Buus + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/gateway/node_modules/memory-pager/README.md b/gateway/node_modules/memory-pager/README.md new file mode 100644 index 0000000000000000000000000000000000000000..aed176140ca3a3d6ca2d73128a588600b3240299 --- /dev/null +++ b/gateway/node_modules/memory-pager/README.md @@ -0,0 +1,65 @@ +# memory-pager + +Access memory using small fixed sized buffers instead of allocating a huge buffer. +Useful if you are implementing sparse data structures (such as large bitfield). + +![travis](https://travis-ci.org/mafintosh/memory-pager.svg?branch=master) + +``` +npm install memory-pager +``` + +## Usage + +``` js +var pager = require('paged-memory') + +var pages = pager(1024) // use 1kb per page + +var page = pages.get(10) // get page #10 + +console.log(page.offset) // 10240 +console.log(page.buffer) // a blank 1kb buffer +``` + +## API + +#### `var pages = pager(pageSize)` + +Create a new pager. `pageSize` defaults to `1024`. + +#### `var page = pages.get(pageNumber, [noAllocate])` + +Get a page. The page will be allocated at first access. + +Optionally you can set the `noAllocate` flag which will make the +method return undefined if no page has been allocated already + +A page looks like this + +``` js +{ + offset: byteOffset, + buffer: bufferWithPageSize +} +``` + +#### `pages.set(pageNumber, buffer)` + +Explicitly set the buffer for a page. + +#### `pages.updated(page)` + +Mark a page as updated. + +#### `pages.lastUpdate()` + +Get the last page that was updated. + +#### `var buf = pages.toBuffer()` + +Concat all pages allocated pages into a single buffer + +## License + +MIT diff --git a/gateway/node_modules/memory-pager/index.js b/gateway/node_modules/memory-pager/index.js new file mode 100644 index 0000000000000000000000000000000000000000..687f346f30e6e8e6f3247f06c4946643ae2baba8 --- /dev/null +++ b/gateway/node_modules/memory-pager/index.js @@ -0,0 +1,160 @@ +module.exports = Pager + +function Pager (pageSize, opts) { + if (!(this instanceof Pager)) return new Pager(pageSize, opts) + + this.length = 0 + this.updates = [] + this.path = new Uint16Array(4) + this.pages = new Array(32768) + this.maxPages = this.pages.length + this.level = 0 + this.pageSize = pageSize || 1024 + this.deduplicate = opts ? opts.deduplicate : null + this.zeros = this.deduplicate ? alloc(this.deduplicate.length) : null +} + +Pager.prototype.updated = function (page) { + while (this.deduplicate && page.buffer[page.deduplicate] === this.deduplicate[page.deduplicate]) { + page.deduplicate++ + if (page.deduplicate === this.deduplicate.length) { + page.deduplicate = 0 + if (page.buffer.equals && page.buffer.equals(this.deduplicate)) page.buffer = this.deduplicate + break + } + } + if (page.updated || !this.updates) return + page.updated = true + this.updates.push(page) +} + +Pager.prototype.lastUpdate = function () { + if (!this.updates || !this.updates.length) return null + var page = this.updates.pop() + page.updated = false + return page +} + +Pager.prototype._array = function (i, noAllocate) { + if (i >= this.maxPages) { + if (noAllocate) return + grow(this, i) + } + + factor(i, this.path) + + var arr = this.pages + + for (var j = this.level; j > 0; j--) { + var p = this.path[j] + var next = arr[p] + + if (!next) { + if (noAllocate) return + next = arr[p] = new Array(32768) + } + + arr = next + } + + return arr +} + +Pager.prototype.get = function (i, noAllocate) { + var arr = this._array(i, noAllocate) + var first = this.path[0] + var page = arr && arr[first] + + if (!page && !noAllocate) { + page = arr[first] = new Page(i, alloc(this.pageSize)) + if (i >= this.length) this.length = i + 1 + } + + if (page && page.buffer === this.deduplicate && this.deduplicate && !noAllocate) { + page.buffer = copy(page.buffer) + page.deduplicate = 0 + } + + return page +} + +Pager.prototype.set = function (i, buf) { + var arr = this._array(i, false) + var first = this.path[0] + + if (i >= this.length) this.length = i + 1 + + if (!buf || (this.zeros && buf.equals && buf.equals(this.zeros))) { + arr[first] = undefined + return + } + + if (this.deduplicate && buf.equals && buf.equals(this.deduplicate)) { + buf = this.deduplicate + } + + var page = arr[first] + var b = truncate(buf, this.pageSize) + + if (page) page.buffer = b + else arr[first] = new Page(i, b) +} + +Pager.prototype.toBuffer = function () { + var list = new Array(this.length) + var empty = alloc(this.pageSize) + var ptr = 0 + + while (ptr < list.length) { + var arr = this._array(ptr, true) + for (var i = 0; i < 32768 && ptr < list.length; i++) { + list[ptr++] = (arr && arr[i]) ? arr[i].buffer : empty + } + } + + return Buffer.concat(list) +} + +function grow (pager, index) { + while (pager.maxPages < index) { + var old = pager.pages + pager.pages = new Array(32768) + pager.pages[0] = old + pager.level++ + pager.maxPages *= 32768 + } +} + +function truncate (buf, len) { + if (buf.length === len) return buf + if (buf.length > len) return buf.slice(0, len) + var cpy = alloc(len) + buf.copy(cpy) + return cpy +} + +function alloc (size) { + if (Buffer.alloc) return Buffer.alloc(size) + var buf = new Buffer(size) + buf.fill(0) + return buf +} + +function copy (buf) { + var cpy = Buffer.allocUnsafe ? Buffer.allocUnsafe(buf.length) : new Buffer(buf.length) + buf.copy(cpy) + return cpy +} + +function Page (i, buf) { + this.offset = i * buf.length + this.buffer = buf + this.updated = false + this.deduplicate = 0 +} + +function factor (n, out) { + n = (n - (out[0] = (n & 32767))) / 32768 + n = (n - (out[1] = (n & 32767))) / 32768 + out[3] = ((n - (out[2] = (n & 32767))) / 32768) & 32767 +} diff --git a/gateway/node_modules/memory-pager/package.json b/gateway/node_modules/memory-pager/package.json new file mode 100644 index 0000000000000000000000000000000000000000..f4847e8cd8415d5b70c907dc090e9803f352783d --- /dev/null +++ b/gateway/node_modules/memory-pager/package.json @@ -0,0 +1,24 @@ +{ + "name": "memory-pager", + "version": "1.5.0", + "description": "Access memory using small fixed sized buffers", + "main": "index.js", + "dependencies": {}, + "devDependencies": { + "standard": "^9.0.0", + "tape": "^4.6.3" + }, + "scripts": { + "test": "standard && tape test.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/mafintosh/memory-pager.git" + }, + "author": "Mathias Buus (@mafintosh)", + "license": "MIT", + "bugs": { + "url": "https://github.com/mafintosh/memory-pager/issues" + }, + "homepage": "https://github.com/mafintosh/memory-pager" +} diff --git a/gateway/node_modules/memory-pager/test.js b/gateway/node_modules/memory-pager/test.js new file mode 100644 index 0000000000000000000000000000000000000000..16382100376b99a29f748969ba419c1b200080d9 --- /dev/null +++ b/gateway/node_modules/memory-pager/test.js @@ -0,0 +1,80 @@ +var tape = require('tape') +var pager = require('./') + +tape('get page', function (t) { + var pages = pager(1024) + + var page = pages.get(0) + + t.same(page.offset, 0) + t.same(page.buffer, Buffer.alloc(1024)) + t.end() +}) + +tape('get page twice', function (t) { + var pages = pager(1024) + t.same(pages.length, 0) + + var page = pages.get(0) + + t.same(page.offset, 0) + t.same(page.buffer, Buffer.alloc(1024)) + t.same(pages.length, 1) + + var other = pages.get(0) + + t.same(other, page) + t.end() +}) + +tape('get no mutable page', function (t) { + var pages = pager(1024) + + t.ok(!pages.get(141, true)) + t.ok(pages.get(141)) + t.ok(pages.get(141, true)) + + t.end() +}) + +tape('get far out page', function (t) { + var pages = pager(1024) + + var page = pages.get(1000000) + + t.same(page.offset, 1000000 * 1024) + t.same(page.buffer, Buffer.alloc(1024)) + t.same(pages.length, 1000000 + 1) + + var other = pages.get(1) + + t.same(other.offset, 1024) + t.same(other.buffer, Buffer.alloc(1024)) + t.same(pages.length, 1000000 + 1) + t.ok(other !== page) + + t.end() +}) + +tape('updates', function (t) { + var pages = pager(1024) + + t.same(pages.lastUpdate(), null) + + var page = pages.get(10) + + page.buffer[42] = 1 + pages.updated(page) + + t.same(pages.lastUpdate(), page) + t.same(pages.lastUpdate(), null) + + page.buffer[42] = 2 + pages.updated(page) + pages.updated(page) + + t.same(pages.lastUpdate(), page) + t.same(pages.lastUpdate(), null) + + t.end() +}) diff --git a/gateway/node_modules/mongodb-connection-string-url/.esm-wrapper.mjs b/gateway/node_modules/mongodb-connection-string-url/.esm-wrapper.mjs new file mode 100644 index 0000000000000000000000000000000000000000..a0f5be52ef20436fac7014c8340436f129ee1ab6 --- /dev/null +++ b/gateway/node_modules/mongodb-connection-string-url/.esm-wrapper.mjs @@ -0,0 +1,6 @@ +import mod from "./lib/index.js"; + +export default mod["default"]; +export const CommaAndColonSeparatedRecord = mod.CommaAndColonSeparatedRecord; +export const ConnectionString = mod.ConnectionString; +export const redactConnectionString = mod.redactConnectionString; diff --git a/gateway/node_modules/mongodb-connection-string-url/LICENSE b/gateway/node_modules/mongodb-connection-string-url/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..d57f55f4e6010ea1fe2cdf9392cdee40d3ed2d71 --- /dev/null +++ b/gateway/node_modules/mongodb-connection-string-url/LICENSE @@ -0,0 +1,192 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + Copyright 2020 MongoDB Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/gateway/node_modules/mongodb-connection-string-url/README.md b/gateway/node_modules/mongodb-connection-string-url/README.md new file mode 100644 index 0000000000000000000000000000000000000000..0eb65d00fcc31242d01a09024eff5713d814173c --- /dev/null +++ b/gateway/node_modules/mongodb-connection-string-url/README.md @@ -0,0 +1,25 @@ +# mongodb-connection-string-url + +MongoDB connection strings, based on the WhatWG URL API + +```js +import ConnectionString from 'mongodb-connection-string-url'; + +const cs = new ConnectionString('mongodb://localhost'); +cs.searchParams.set('readPreference', 'secondary'); +console.log(cs.href); // 'mongodb://localhost/?readPreference=secondary' +``` + +## Deviations from the WhatWG URL package + +- URL parameters are case-insensitive +- The `.host`, `.hostname` and `.port` properties cannot be set, and reading + them does not return meaningful results (and are typed as `never`in TypeScript) +- The `.hosts` property contains a list of all hosts in the connection string +- The `.href` property cannot be set, only read +- There is an additional `.isSRV` property, set to `true` for `mongodb+srv://` +- There is an additional `.clone()` utility method on the prototype + +## LICENSE + +Apache-2.0 diff --git a/gateway/node_modules/mongodb-connection-string-url/package.json b/gateway/node_modules/mongodb-connection-string-url/package.json new file mode 100644 index 0000000000000000000000000000000000000000..0bb8735db242730fe858259493ff62951eb58722 --- /dev/null +++ b/gateway/node_modules/mongodb-connection-string-url/package.json @@ -0,0 +1,81 @@ +{ + "name": "mongodb-connection-string-url", + "version": "7.0.2", + "description": "MongoDB connection strings, based on the WhatWG URL API", + "keywords": [ + "password", + "prompt", + "tty" + ], + "homepage": "https://github.com/mongodb-js/mongodb-connection-string-url", + "repository": { + "type": "git", + "url": "https://github.com/mongodb-js/mongodb-connection-string-url.git" + }, + "bugs": { + "url": "https://github.com/mongodb-js/mongodb-connection-string-url/issues" + }, + "main": "lib/index.js", + "types": "lib/index.d.ts", + "exports": { + "require": { + "default": "./lib/index.js", + "types": "./lib/index.d.ts" + }, + "import": { + "default": "./.esm-wrapper.mjs", + "types": "./lib/index.d.ts" + } + }, + "files": [ + "LICENSE", + "lib", + "package.json", + "README.md", + ".esm-wrapper.mjs" + ], + "scripts": { + "lint": "ESLINT_USE_FLAT_CONFIG=false eslint \"{src,test}/**/*.ts\"", + "test": "npm run build && tsd && nyc mocha --colors -r ts-node/register test/*.ts", + "build": "npm run compile-ts && gen-esm-wrapper . ./.esm-wrapper.mjs", + "prepack": "npm run build", + "compile-ts": "tsc -p tsconfig.json" + }, + "license": "Apache-2.0", + "devDependencies": { + "@types/chai": "^5.0.1", + "@types/mocha": "^10.0.10", + "@types/node": "^22.9.0", + "@typescript-eslint/eslint-plugin": "^8.39.1", + "@typescript-eslint/parser": "^8.39.1", + "chai": "^4.2.0", + "eslint": "^9.33.0", + "eslint-config-prettier": "^10.1.8", + "eslint-plugin-import": "^2.22.0", + "eslint-plugin-node": "^11.1.0", + "eslint-plugin-prettier": "^5.5.4", + "eslint-plugin-promise": "^7.1.0", + "gen-esm-wrapper": "^1.1.3", + "mocha": "^11.0.1", + "nyc": "^17.1.0", + "ts-node": "^10.9.1", + "tsd": "^0.33.0", + "typescript": "^5.9.2" + }, + "dependencies": { + "@types/whatwg-url": "^13.0.0", + "whatwg-url": "^14.1.0" + }, + "engines": { + "node": ">=20.19.0" + }, + "tsd": { + "directory": "test/types", + "compilerOptions": { + "strict": true, + "target": "esnext", + "module": "commonjs", + "moduleResolution": "node" + } + } +} diff --git a/gateway/node_modules/mongodb/LICENSE.md b/gateway/node_modules/mongodb/LICENSE.md new file mode 100644 index 0000000000000000000000000000000000000000..ad410e11302107da9aa47ce3d46bd5ad011c4c43 --- /dev/null +++ b/gateway/node_modules/mongodb/LICENSE.md @@ -0,0 +1,201 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/gateway/node_modules/mongodb/README.md b/gateway/node_modules/mongodb/README.md new file mode 100644 index 0000000000000000000000000000000000000000..8de46407806bd76b06511591fa487bc5bd496051 --- /dev/null +++ b/gateway/node_modules/mongodb/README.md @@ -0,0 +1,372 @@ +# MongoDB Node.js Driver + +The official [MongoDB](https://www.mongodb.com/) driver for Node.js. + +**Upgrading to version 7? Take a look at our [upgrade guide here](https://github.com/mongodb/node-mongodb-native/blob/HEAD/etc/notes/CHANGES_7.0.0.md)!** + +## Quick Links + +| Site | Link | +| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------- | +| Documentation | [www.mongodb.com/docs/drivers/node](https://www.mongodb.com/docs/drivers/node) | +| API Docs | [mongodb.github.io/node-mongodb-native](https://mongodb.github.io/node-mongodb-native) | +| `npm` package | [www.npmjs.com/package/mongodb](https://www.npmjs.com/package/mongodb) | +| MongoDB | [www.mongodb.com](https://www.mongodb.com) | +| MongoDB University | [learn.mongodb.com](https://learn.mongodb.com/catalog?labels=%5B%22Language%22%5D&values=%5B%22Node.js%22%5D) | +| MongoDB Developer Center | [www.mongodb.com/developer](https://www.mongodb.com/developer/languages/javascript/) | +| Stack Overflow | [stackoverflow.com](https://stackoverflow.com/search?q=%28%5Btypescript%5D+or+%5Bjavascript%5D+or+%5Bnode.js%5D%29+and+%5Bmongodb%5D) | +| Source Code | [github.com/mongodb/node-mongodb-native](https://github.com/mongodb/node-mongodb-native) | +| Upgrade to v7 | [etc/notes/CHANGES_7.0.0.md](https://github.com/mongodb/node-mongodb-native/blob/HEAD/etc/notes/CHANGES_7.0.0.md) | +| Contributing | [CONTRIBUTING.md](https://github.com/mongodb/node-mongodb-native/blob/HEAD/CONTRIBUTING.md) | +| Changelog | [HISTORY.md](https://github.com/mongodb/node-mongodb-native/blob/HEAD/HISTORY.md) | + +### Release Integrity + +Releases are created automatically and signed using the [Node team's GPG key](https://pgp.mongodb.com/node-driver.asc). All release packages provided as part of a GitHub release are signed. To verify the provided packages, download the key and import it using gpg: + +```shell +gpg --import node-driver.asc +``` + +The GitHub release contains a detached signature file for the NPM package (named +`mongodb-X.Y.Z.tgz.sig`). + +The following command returns the link npm package. + +```shell +npm view mongodb@vX.Y.Z dist.tarball +``` + +Using the result of the above command, a `curl` command can return the official npm package for the release. + +To verify the integrity of the downloaded package, run the following command: + +```shell +gpg --verify mongodb-X.Y.Z.tgz.sig mongodb-X.Y.Z.tgz +``` + +> [!Note] +> No GPG verification is done when using npm to install the package. The contents of the GitHub tarball and npm's tarball are identical. + +Releases published to the npm registry also include a [provenance attestation](https://docs.npmjs.com/generating-provenance-statements), which cryptographically links the package to its source repository and build workflow. To verify provenance: + +```shell +npm audit signatures +``` + +The MongoDB Node.js driver follows [semantic versioning](https://semver.org/) for its releases. + +### Bugs / Feature Requests + +Think you’ve found a bug? Want to see a new feature in `node-mongodb-native`? Please open a +case in our issue management tool, JIRA: + +- Create an account and login [jira.mongodb.org](https://jira.mongodb.org). +- Navigate to the NODE project [jira.mongodb.org/browse/NODE](https://jira.mongodb.org/browse/NODE). +- Click **Create Issue** - Please provide as much information as possible about the issue type and how to reproduce it. + +Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the +Core Server (i.e. SERVER) project are **public**. + +### Support / Feedback + +For issues with, questions about, or feedback for the Node.js driver, please look into our [support channels](https://www.mongodb.com/docs/manual/support). Please do not email any of the driver developers directly with issues or questions - you're more likely to get an answer on the [MongoDB Community Forums](https://community.mongodb.com/tags/c/drivers-odms-connectors/7/node-js-driver). + +### Change Log + +Change history can be found in [`HISTORY.md`](https://github.com/mongodb/node-mongodb-native/blob/HEAD/HISTORY.md). + +### Compatibility + +The driver currently supports 4.2+ servers. + +For exhaustive server and runtime version compatibility matrices, please refer to the following links: + +- [MongoDB](https://www.mongodb.com/docs/drivers/node/current/compatibility/#mongodb-compatibility) +- [NodeJS](https://www.mongodb.com/docs/drivers/node/current/compatibility/#language-compatibility) + +#### Component Support Matrix + +The following table describes add-on component version compatibility for the Node.js driver. Only packages with versions in these supported ranges are stable when used in combination. + +| Component | `mongodb@3.x` | `mongodb@4.x` | `mongodb@5.x` | `mongodb@<6.12` | `mongodb@>=6.12` | `mongodb@7.x` | +| ------------------------------------------------------------------------------------ | ------------------ | ------------------ | ------------------ | --------------- | ------------------ | ------------- | +| [bson](https://www.npmjs.com/package/bson) | ^1.0.0 | ^4.0.0 | ^5.0.0 | ^6.0.0 | ^6.0.0 | ^7.0.0 | +| [bson-ext](https://www.npmjs.com/package/bson-ext) | ^1.0.0 \|\| ^2.0.0 | ^4.0.0 | N/A | N/A | N/A | N/A | +| [kerberos](https://www.npmjs.com/package/kerberos) | ^1.0.0 | ^1.0.0 \|\| ^2.0.0 | ^1.0.0 \|\| ^2.0.0 | ^2.0.1 | ^2.0.1 | ^7.0.0 | +| [mongodb-client-encryption](https://www.npmjs.com/package/mongodb-client-encryption) | ^1.0.0 | ^1.0.0 \|\| ^2.0.0 | ^2.3.0 | ^6.0.0 | ^6.0.0 | ^7.0.0 | +| [mongodb-legacy](https://www.npmjs.com/package/mongodb-legacy) | N/A | ^4.0.0 | ^5.0.0 | ^6.0.0 | ^6.0.0 | N/A | +| [@mongodb-js/zstd](https://www.npmjs.com/package/@mongodb-js/zstd) | N/A | ^1.0.0 | ^1.0.0 | ^1.1.0 | ^1.1.0 \|\| ^2.0.0 | ^7.0.0 | + +#### Typescript Version + +We recommend using the latest version of typescript, however we currently ensure the driver's public types compile against `typescript@5.6.0`. +This is the lowest typescript version guaranteed to work with our driver: older versions may or may not work - use at your own risk. +Since typescript [does not restrict breaking changes to major versions](https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes), we consider this support best effort. +If you run into any unexpected compiler failures against our supported TypeScript versions, please let us know by filing an issue on our [JIRA](https://jira.mongodb.org/browse/NODE). + +Additionally, our Typescript types are compatible with the ECMAScript standard for our minimum supported Node version. Currently, our Typescript targets es2023. + +#### Running in Custom Runtimes + +We are working on removing Node.js as a dependency of the driver, so that in the future it will be possible to use the driver in non-Node environments. +This work is currently in progress, and if you're curious, this is [our first runtime adapter commit](https://github.com/mongodb/node-mongodb-native/commit/d2ad07f20903d86334da81222a6df9717f76faaa). + +Some things to keep in mind if you are using a non-Node runtime: + +1. Users of Webpack/Vite may need to prevent `crypto` polyfill injection. +2. Auth mechanism `SCRAM-SHA-1` has a hard dependency on Node.js. +3. Auth mechanism `SCRAM-SHA-1` is not supported in FIPS mode. + +## Installation + +The recommended way to get started using the Node.js driver is by using the `npm` (Node Package Manager) to install the dependency in your project. + +After you've created your own project using `npm init`, you can run: + +```bash +npm install mongodb +``` + +This will download the MongoDB driver and add a dependency entry in your `package.json` file. + +If you are a Typescript user, you will need the Node.js type definitions to use the driver's definitions: + +```sh +npm install -D @types/node +``` + +## Driver Extensions + +The MongoDB driver can optionally be enhanced by the following feature packages: + +Maintained by MongoDB: + +- Zstd network compression - [@mongodb-js/zstd](https://github.com/mongodb-js/zstd) +- MongoDB field level and queryable encryption - [mongodb-client-encryption](https://github.com/mongodb/libmongocrypt#readme) +- GSSAPI / SSPI / Kerberos authentication - [kerberos](https://github.com/mongodb-js/kerberos) + +Some of these packages include native C++ extensions. +Consult the [trouble shooting guide here](https://github.com/mongodb/node-mongodb-native/blob/HEAD/etc/notes/native-extensions.md) if you run into compilation issues. + +Third party: + +- Snappy network compression - [snappy](https://github.com/Brooooooklyn/snappy) +- AWS authentication - [@aws-sdk/credential-providers](https://github.com/aws/aws-sdk-js-v3/tree/main/packages/credential-providers) + +## Quick Start + +This guide will show you how to set up a simple application using Node.js and MongoDB. Its scope is only how to set up the driver and perform the simple CRUD operations. For more in-depth coverage, see the [official documentation](https://www.mongodb.com/docs/drivers/node/). + +### Create the `package.json` file + +First, create a directory where your application will live. + +```bash +mkdir myProject +cd myProject +``` + +Enter the following command and answer the questions to create the initial structure for your new project: + +```bash +npm init -y +``` + +Next, install the driver as a dependency. + +```bash +npm install mongodb +``` + +### Start a MongoDB Server + +For complete MongoDB installation instructions, see [the manual](https://www.mongodb.com/docs/manual/installation/). + +1. Download the right MongoDB version from [MongoDB](https://www.mongodb.org/downloads) +2. Create a database directory (in this case under **/data**). +3. Install and start a `mongod` process. + +```bash +mongod --dbpath=/data +``` + +You should see the **mongod** process start up and print some status information. + +### Connect to MongoDB + +Create a new **app.js** file and add the following code to try out some basic CRUD +operations using the MongoDB driver. + +Add code to connect to the server and the database **myProject**: + +> **NOTE:** Resolving DNS Connection issues +> +> Node.js 18 changed the default DNS resolution ordering from always prioritizing IPv4 to the ordering +> returned by the DNS provider. In some environments, this can result in `localhost` resolving to +> an IPv6 address instead of IPv4 and a consequent failure to connect to the server. +> +> This can be resolved by: +> +> - specifying the IP address family using the MongoClient `family` option (`MongoClient(, { family: 4 } )`) +> - launching mongod or mongos with the ipv6 flag enabled ([--ipv6 mongod option documentation](https://www.mongodb.com/docs/manual/reference/program/mongod/#std-option-mongod.--ipv6)) +> - using a host of `127.0.0.1` in place of localhost +> - specifying the DNS resolution ordering with the `--dns-resolution-order` Node.js command line argument (e.g. `node --dns-resolution-order=ipv4first`) + +```js +const { MongoClient } = require('mongodb'); +// or as an es module: +// import { MongoClient } from 'mongodb' + +// Connection URL +const url = 'mongodb://localhost:27017'; +const client = new MongoClient(url); + +// Database Name +const dbName = 'myProject'; + +async function main() { + // Use connect method to connect to the server + await client.connect(); + console.log('Connected successfully to server'); + const db = client.db(dbName); + const collection = db.collection('documents'); + + // the following code examples can be pasted here... + + return 'done.'; +} + +main() + .then(console.log) + .catch(console.error) + .finally(() => client.close()); +``` + +Run your app from the command line with: + +```bash +node app.js +``` + +The application should print **Connected successfully to server** to the console. + +### Insert a Document + +Add to **app.js** the following function which uses the **insertMany** +method to add three documents to the **documents** collection. + +```js +const insertResult = await collection.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }]); +console.log('Inserted documents =>', insertResult); +``` + +The **insertMany** command returns an object with information about the insert operations. + +### Find All Documents + +Add a query that returns all the documents. + +```js +const findResult = await collection.find({}).toArray(); +console.log('Found documents =>', findResult); +``` + +This query returns all the documents in the **documents** collection. +If you add this below the insertMany example, you'll see the documents you've inserted. + +### Find Documents with a Query Filter + +Add a query filter to find only documents which meet the query criteria. + +```js +const filteredDocs = await collection.find({ a: 3 }).toArray(); +console.log('Found documents filtered by { a: 3 } =>', filteredDocs); +``` + +Only the documents which match `'a' : 3` should be returned. + +### Update a document + +The following operation updates a document in the **documents** collection. + +```js +const updateResult = await collection.updateOne({ a: 3 }, { $set: { b: 1 } }); +console.log('Updated documents =>', updateResult); +``` + +The method updates the first document where the field **a** is equal to **3** by adding a new field **b** to the document set to **1**. `updateResult` contains information about whether there was a matching document to update or not. + +### Remove a document + +Remove the document where the field **a** is equal to **3**. + +```js +const deleteResult = await collection.deleteMany({ a: 3 }); +console.log('Deleted documents =>', deleteResult); +``` + +### Index a Collection + +[Indexes](https://www.mongodb.com/docs/manual/indexes/) can improve your application's +performance. The following function creates an index on the **a** field in the +**documents** collection. + +```js +const indexName = await collection.createIndex({ a: 1 }); +console.log('index name =', indexName); +``` + +For more detailed information, see the [indexing strategies page](https://www.mongodb.com/docs/manual/applications/indexes/). + +## Error Handling + +If you need to filter certain errors from our driver, we have a helpful tree of errors described in [etc/notes/errors.md](https://github.com/mongodb/node-mongodb-native/blob/HEAD/etc/notes/errors.md). + +It is our recommendation to use `instanceof` checks on errors and to avoid relying on parsing `error.message` and `error.name` strings in your code. +We guarantee `instanceof` checks will pass according to semver guidelines, but errors may be sub-classed or their messages may change at any time, even patch releases, as we see fit to increase the helpfulness of the errors. + +Any new errors we add to the driver will directly extend an existing error class and no existing error will be moved to a different parent class outside of a major release. +This means `instanceof` will always be able to accurately capture the errors that our driver throws. + +```typescript +const client = new MongoClient(url); +await client.connect(); +const collection = client.db().collection('collection'); + +try { + await collection.insertOne({ _id: 1 }); + await collection.insertOne({ _id: 1 }); // duplicate key error +} catch (error) { + if (error instanceof MongoServerError) { + console.log(`Error worth logging: ${error}`); // special case for some reason + } + throw error; // still want to crash +} +``` + +## Nightly releases + +If you need to test with a change from the latest `main` branch, our `mongodb` npm package has nightly versions released under the `nightly` tag. + +```sh +npm install mongodb@nightly +``` + +Nightly versions are published regardless of testing outcome. +This means there could be semantic breakages or partially implemented features. +The nightly build is not suitable for production use. + +## Next Steps + +- [MongoDB Documentation](https://www.mongodb.com/docs/manual/) +- [MongoDB Node Driver Documentation](https://www.mongodb.com/docs/drivers/node/) +- [Read about Schemas](https://www.mongodb.com/docs/manual/core/data-modeling-introduction/) +- [Star us on GitHub](https://github.com/mongodb/node-mongodb-native) + +## License + +[Apache 2.0](LICENSE.md) + +© 2012-present MongoDB [Contributors](https://github.com/mongodb/node-mongodb-native/blob/HEAD/CONTRIBUTORS.md) \ +© 2009-2012 Christian Amor Kvalheim diff --git a/gateway/node_modules/mongodb/etc/prepare.js b/gateway/node_modules/mongodb/etc/prepare.js new file mode 100644 index 0000000000000000000000000000000000000000..2039d0b33ade270ab661f33a9d72e6b6af863443 --- /dev/null +++ b/gateway/node_modules/mongodb/etc/prepare.js @@ -0,0 +1,12 @@ +#! /usr/bin/env node +var cp = require('child_process'); +var fs = require('fs'); +var os = require('os'); + +if (fs.existsSync('src')) { + cp.spawn('npm', ['run', 'build:dts'], { stdio: 'inherit', shell: os.platform() === 'win32' }); +} else { + if (!fs.existsSync('lib')) { + console.warn('MongoDB: No compiled javascript present, the driver is not installed correctly.'); + } +} diff --git a/gateway/node_modules/mongodb/mongodb.d.ts b/gateway/node_modules/mongodb/mongodb.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..caf064068d006533997cbf234b3abc52bf2e7d75 --- /dev/null +++ b/gateway/node_modules/mongodb/mongodb.d.ts @@ -0,0 +1,9076 @@ +import type { DeserializeOptions } from 'bson'; +import type { ObjectIdLike } from 'bson'; +import type { SerializeOptions } from 'bson'; +import { Binary } from 'bson'; +import { BSON } from 'bson'; +import { BSONRegExp } from 'bson'; +import { BSONSymbol } from 'bson'; +import { BSONType } from 'bson'; +import { Code } from 'bson'; +import { DBRef } from 'bson'; +import { Decimal128 } from 'bson'; +import { deserialize } from 'bson'; +import { Document } from 'bson'; +import { Double } from 'bson'; +import { Int32 } from 'bson'; +import { Long } from 'bson'; +import { MaxKey } from 'bson'; +import { MinKey } from 'bson'; +import { ObjectId } from 'bson'; +import { serialize } from 'bson'; +import { Timestamp } from 'bson'; +import { UUID } from 'bson'; +import type { SrvRecord } from 'dns'; +import { EventEmitter } from 'events'; +import type { Socket } from 'net'; +import type { TcpNetConnectOpts } from 'net'; +import type * as os from 'os'; +import { Readable } from 'stream'; +import { Writable } from 'stream'; +import type { ConnectionOptions as ConnectionOptions_2 } from 'tls'; +import type { TLSSocket } from 'tls'; +import type { TLSSocketOptions } from 'tls'; + +/** @public */ +export declare type Abortable = { + /** + * @experimental + * When provided, the corresponding `AbortController` can be used to abort an asynchronous action. + * + * The `signal.reason` value is used as the error thrown. + * + * @remarks + * **NOTE:** If an abort signal aborts an operation while the driver is writing to the underlying + * socket or reading the response from the server, the socket will be closed. + * If signals are aborted at a high rate during socket read/writes this can lead to a high rate of connection reestablishment. + * + * We plan to mitigate this in a future release, please follow NODE-6062 (`timeoutMS` expiration suffers the same limitation). + * + * AbortSignals are likely a best fit for human interactive interruption (ex. ctrl-C) where the frequency + * of cancellation is reasonably low. If a signal is programmatically aborted for 100s of operations you can empty + * the driver's connection pool. + * + * @example + * ```js + * const controller = new AbortController(); + * const { signal } = controller; + * process.on('SIGINT', () => controller.abort(new Error('^C pressed'))); + * + * try { + * const res = await fetch('...', { signal }); + * await collection.findOne(await res.json(), { signal }); + * catch (error) { + * if (error === signal.reason) { + * // signal abort error handling + * } + * } + * ``` + */ + signal?: AbortSignal | undefined; +}; + +/** @public */ +export declare abstract class AbstractCursor extends TypedEventEmitter implements AsyncDisposable { + /* Excluded from this release type: cursorId */ + /* Excluded from this release type: cursorSession */ + /* Excluded from this release type: selectedServer */ + /* Excluded from this release type: cursorNamespace */ + /* Excluded from this release type: documents */ + /* Excluded from this release type: cursorClient */ + /* Excluded from this release type: transform */ + /* Excluded from this release type: initialized */ + /* Excluded from this release type: isClosed */ + /* Excluded from this release type: isKilled */ + /* Excluded from this release type: cursorOptions */ + /* Excluded from this release type: timeoutContext */ + /** @event */ + static readonly CLOSE: "close"; + /* Excluded from this release type: deserializationOptions */ + protected signal: AbortSignal | undefined; + private abortListener; + /* Excluded from this release type: __constructor */ + /** + * The cursor has no id until it receives a response from the initial cursor creating command. + * + * It is non-zero for as long as the database has an open cursor. + * + * The initiating command may receive a zero id if the entire result is in the `firstBatch`. + */ + get id(): Long | undefined; + /* Excluded from this release type: isDead */ + /* Excluded from this release type: client */ + /* Excluded from this release type: server */ + get namespace(): MongoDBNamespace; + get readPreference(): ReadPreference; + get readConcern(): ReadConcern | undefined; + /* Excluded from this release type: session */ + /* Excluded from this release type: session */ + /** + * The cursor is closed and all remaining locally buffered documents have been iterated. + */ + get closed(): boolean; + /** + * A `killCursors` command was attempted on this cursor. + * This is performed if the cursor id is non zero. + */ + get killed(): boolean; + get loadBalanced(): boolean; + /** + * An alias for {@link AbstractCursor.close|AbstractCursor.close()}. + */ + [Symbol.asyncDispose](): Promise; + /** Adds cursor to client's tracking so it will be closed by MongoClient.close() */ + private trackCursor; + /** Returns current buffered documents length */ + bufferedCount(): number; + /** Returns current buffered documents */ + readBufferedDocuments(number?: number): NonNullable[]; + [Symbol.asyncIterator](): AsyncGenerator; + stream(): Readable & AsyncIterable; + hasNext(): Promise; + /** Get the next available document from the cursor, returns null if no more documents are available. */ + next(): Promise; + /** + * Try to get the next available document from the cursor or `null` if an empty batch is returned + */ + tryNext(): Promise; + /** + * Iterates over all the documents for this cursor using the iterator, callback pattern. + * + * If the iterator returns `false`, iteration will stop. + * + * @param iterator - The iteration callback. + * @deprecated - Will be removed in a future release. Use for await...of instead. + */ + forEach(iterator: (doc: TSchema) => boolean | void): Promise; + /** + * Frees any client-side resources used by the cursor. + */ + close(options?: { + timeoutMS?: number; + }): Promise; + /** + * Returns an array of documents. The caller is responsible for making sure that there + * is enough memory to store the results. Note that the array only contains partial + * results when this cursor had been previously accessed. In that case, + * cursor.rewind() can be used to reset the cursor. + */ + toArray(): Promise; + /** + * Add a cursor flag to the cursor + * + * @param flag - The flag to set, must be one of following ['tailable', 'oplogReplay', 'noCursorTimeout', 'awaitData', 'partial' -. + * @param value - The flag boolean value. + */ + addCursorFlag(flag: CursorFlag, value: boolean): this; + /** + * Map all documents using the provided function + * If there is a transform set on the cursor, that will be called first and the result passed to + * this function's transform. + * + * @remarks + * + * **Note** Cursors use `null` internally to indicate that there are no more documents in the cursor. Providing a mapping + * function that maps values to `null` will result in the cursor closing itself before it has finished iterating + * all documents. This will **not** result in a memory leak, just surprising behavior. For example: + * + * ```typescript + * const cursor = collection.find({}); + * cursor.map(() => null); + * + * const documents = await cursor.toArray(); + * // documents is always [], regardless of how many documents are in the collection. + * ``` + * + * Other falsey values are allowed: + * + * ```typescript + * const cursor = collection.find({}); + * cursor.map(() => ''); + * + * const documents = await cursor.toArray(); + * // documents is now an array of empty strings + * ``` + * + * **Note for Typescript Users:** adding a transform changes the return type of the iteration of this cursor, + * it **does not** return a new instance of a cursor. This means when calling map, + * you should always assign the result to a new variable in order to get a correctly typed cursor variable. + * Take note of the following example: + * + * @example + * ```typescript + * const cursor: FindCursor = coll.find(); + * const mappedCursor: FindCursor = cursor.map(doc => Object.keys(doc).length); + * const keyCounts: number[] = await mappedCursor.toArray(); // cursor.toArray() still returns Document[] + * ``` + * @param transform - The mapping transformation method. + */ + map(transform: (doc: TSchema) => T): AbstractCursor; + /** + * Set the ReadPreference for the cursor. + * + * @param readPreference - The new read preference for the cursor. + */ + withReadPreference(readPreference: ReadPreferenceLike): this; + /** + * Set the ReadPreference for the cursor. + * + * @param readPreference - The new read preference for the cursor. + */ + withReadConcern(readConcern: ReadConcernLike): this; + /** + * Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher) + * + * @param value - Number of milliseconds to wait before aborting the query. + */ + maxTimeMS(value: number): this; + /** + * Set the batch size for the cursor. + * + * @param value - The number of documents to return per batch. See {@link https://www.mongodb.com/docs/manual/reference/command/find/|find command documentation}. + */ + batchSize(value: number): this; + /** + * Rewind this cursor to its uninitialized state. Any options that are present on the cursor will + * remain in effect. Iterating this cursor will cause new queries to be sent to the server, even + * if the resultant data has already been retrieved by this cursor. + */ + rewind(): void; + /** + * Returns a new uninitialized copy of this cursor, with options matching those that have been set on the current instance + */ + abstract clone(): AbstractCursor; + /* Excluded from this release type: _initialize */ + /* Excluded from this release type: getMore */ + /* Excluded from this release type: cursorInit */ + /* Excluded from this release type: fetchBatch */ + /* Excluded from this release type: cleanup */ + /* Excluded from this release type: hasEmittedClose */ + /* Excluded from this release type: emitClose */ + /* Excluded from this release type: transformDocument */ + /* Excluded from this release type: throwIfInitialized */ +} + +/** @public */ +export declare type AbstractCursorEvents = { + [AbstractCursor.CLOSE](): void; +}; + +/** @public */ +export declare interface AbstractCursorOptions extends BSONSerializeOptions { + session?: ClientSession; + readPreference?: ReadPreferenceLike; + readConcern?: ReadConcernLike; + /** + * Specifies the number of documents to return in each response from MongoDB + */ + batchSize?: number; + /** + * When applicable `maxTimeMS` controls the amount of time the initial command + * that constructs a cursor should take. (ex. find, aggregate, listCollections) + */ + maxTimeMS?: number; + /** + * When applicable `maxAwaitTimeMS` controls the amount of time subsequent getMores + * that a cursor uses to fetch more data should take. (ex. cursor.next()) + */ + maxAwaitTimeMS?: number; + /** + * Comment to apply to the operation. + * + * In server versions pre-4.4, 'comment' must be string. A server + * error will be thrown if any other type is provided. + * + * In server versions 4.4 and above, 'comment' can be any valid BSON type. + */ + comment?: unknown; + /** + * By default, MongoDB will automatically close a cursor when the + * client has exhausted all results in the cursor. However, for [capped collections](https://www.mongodb.com/docs/manual/core/capped-collections) + * you may use a Tailable Cursor that remains open after the client exhausts + * the results in the initial cursor. + */ + tailable?: boolean; + /** + * If awaitData is set to true, when the cursor reaches the end of the capped collection, + * MongoDB blocks the query thread for a period of time waiting for new data to arrive. + * When new data is inserted into the capped collection, the blocked thread is signaled + * to wake up and return the next batch to the client. + */ + awaitData?: boolean; + noCursorTimeout?: boolean; + /** Specifies the time an operation will run until it throws a timeout error. See {@link AbstractCursorOptions.timeoutMode} for more details on how this option applies to cursors. */ + timeoutMS?: number; + /** + * @public + * @experimental + * Specifies how `timeoutMS` is applied to the cursor. Can be either `'cursorLifeTime'` or `'iteration'` + * When set to `'iteration'`, the deadline specified by `timeoutMS` applies to each call of + * `cursor.next()`. + * When set to `'cursorLifetime'`, the deadline applies to the life of the entire cursor. + * + * Depending on the type of cursor being used, this option has different default values. + * For non-tailable cursors, this value defaults to `'cursorLifetime'` + * For tailable cursors, this value defaults to `'iteration'` since tailable cursors, by + * definition can have an arbitrarily long lifetime. + * + * @example + * ```ts + * const cursor = collection.find({}, {timeoutMS: 100, timeoutMode: 'iteration'}); + * for await (const doc of cursor) { + * // process doc + * // This will throw a timeout error if any of the iterator's `next()` calls takes more than 100ms, but + * // will continue to iterate successfully otherwise, regardless of the number of batches. + * } + * ``` + * + * @example + * ```ts + * const cursor = collection.find({}, { timeoutMS: 1000, timeoutMode: 'cursorLifetime' }); + * const docs = await cursor.toArray(); // This entire line will throw a timeout error if all batches are not fetched and returned within 1000ms. + * ``` + */ + timeoutMode?: CursorTimeoutMode; + /* Excluded from this release type: timeoutContext */ +} + +/* Excluded from this release type: AbstractOperation */ + +/** @public */ +export declare type AcceptedFields = { + readonly [key in KeysOfAType]?: AssignableType; +}; + +/** @public */ +export declare type AddToSetOperators = { + $each?: Array>; +}; + +/** + * The **Admin** class is an internal class that allows convenient access to + * the admin functionality and commands for MongoDB. + * + * **ADMIN Cannot directly be instantiated** + * @public + * + * @example + * ```ts + * import { MongoClient } from 'mongodb'; + * + * const client = new MongoClient('mongodb://localhost:27017'); + * const admin = client.db().admin(); + * const dbInfo = await admin.listDatabases(); + * for (const db of dbInfo.databases) { + * console.log(db.name); + * } + * ``` + */ +export declare class Admin { + /* Excluded from this release type: s */ + /* Excluded from this release type: __constructor */ + /** + * Execute a command + * + * The driver will ensure the following fields are attached to the command sent to the server: + * - `lsid` - sourced from an implicit session or options.session + * - `$readPreference` - defaults to primary or can be configured by options.readPreference + * - `$db` - sourced from the name of this database + * + * If the client has a serverApi setting: + * - `apiVersion` + * - `apiStrict` + * - `apiDeprecationErrors` + * + * When in a transaction: + * - `readConcern` - sourced from readConcern set on the TransactionOptions + * - `writeConcern` - sourced from writeConcern set on the TransactionOptions + * + * Attaching any of the above fields to the command will have no effect as the driver will overwrite the value. + * + * @param command - The command to execute + * @param options - Optional settings for the command + */ + command(command: Document, options?: RunCommandOptions): Promise; + /** + * Retrieve the server build information + * + * @param options - Optional settings for the command + */ + buildInfo(options?: CommandOperationOptions): Promise; + /** + * Retrieve the server build information + * + * @param options - Optional settings for the command + */ + serverInfo(options?: CommandOperationOptions): Promise; + /** + * Retrieve this db's server status. + * + * @param options - Optional settings for the command + */ + serverStatus(options?: CommandOperationOptions): Promise; + /** + * Ping the MongoDB server and retrieve results + * + * @param options - Optional settings for the command + */ + ping(options?: CommandOperationOptions): Promise; + /** + * Remove a user from a database + * + * @param username - The username to remove + * @param options - Optional settings for the command + */ + removeUser(username: string, options?: RemoveUserOptions): Promise; + /** + * Validate an existing collection + * + * @param collectionName - The name of the collection to validate. + * @param options - Optional settings for the command + */ + validateCollection(collectionName: string, options?: ValidateCollectionOptions): Promise; + /** + * List the available databases + * + * @param options - Optional settings for the command + */ + listDatabases(options?: ListDatabasesOptions): Promise; + /** + * Get ReplicaSet status + * + * @param options - Optional settings for the command + */ + replSetGetStatus(options?: CommandOperationOptions): Promise; +} + +/* Excluded from this release type: AdminPrivate */ + +/* Excluded from this release type: AggregateOperation */ + +/** @public */ +export declare interface AggregateOptions extends Omit { + /** allowDiskUse lets the server know if it can use disk to store temporary results for the aggregation (requires mongodb 2.6 \>). */ + allowDiskUse?: boolean; + /** The number of documents to return per batch. See [aggregation documentation](https://www.mongodb.com/docs/manual/reference/command/aggregate). */ + batchSize?: number; + /** Allow driver to bypass schema validation. */ + bypassDocumentValidation?: boolean; + /** Return the query as cursor, on 2.6 \> it returns as a real cursor on pre 2.6 it returns as an emulated cursor. */ + cursor?: Document; + /** + * Specifies a cumulative time limit in milliseconds for processing operations on the cursor. MongoDB interrupts the operation at the earliest following interrupt point. + */ + maxTimeMS?: number; + /** The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query. */ + maxAwaitTimeMS?: number; + /** Specify collation. */ + collation?: CollationOptions; + /** Add an index selection hint to an aggregation command */ + hint?: Hint; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + out?: string; + /** + * Specifies the verbosity mode for the explain output. + * @deprecated This API is deprecated in favor of `collection.aggregate().explain()` + * or `db.aggregate().explain()`. + */ + explain?: ExplainOptions['explain']; + /* Excluded from this release type: timeoutMode */ +} + +/** + * The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB + * allowing for iteration over the results returned from the underlying query. It supports + * one by one document iteration, conversion to an array or can be iterated as a Node 4.X + * or higher stream + * @public + */ +export declare class AggregationCursor extends ExplainableCursor { + readonly pipeline: Document[]; + /* Excluded from this release type: aggregateOptions */ + /* Excluded from this release type: __constructor */ + clone(): AggregationCursor; + map(transform: (doc: TSchema) => T): AggregationCursor; + /* Excluded from this release type: _initialize */ + /** Execute the explain for the cursor */ + explain(): Promise; + explain(verbosity: ExplainVerbosityLike | ExplainCommandOptions): Promise; + explain(options: { + timeoutMS?: number; + }): Promise; + explain(verbosity: ExplainVerbosityLike | ExplainCommandOptions, options: { + timeoutMS?: number; + }): Promise; + /** Add a stage to the aggregation pipeline + * @example + * ``` + * const documents = await users.aggregate().addStage({ $match: { name: /Mike/ } }).toArray(); + * ``` + * @example + * ``` + * const documents = await users.aggregate() + * .addStage<{ name: string }>({ $project: { name: true } }) + * .toArray(); // type of documents is { name: string }[] + * ``` + */ + addStage(stage: Document): this; + addStage(stage: Document): AggregationCursor; + /** Add a group stage to the aggregation pipeline */ + group($group: Document): AggregationCursor; + /** Add a limit stage to the aggregation pipeline */ + limit($limit: number): this; + /** Add a match stage to the aggregation pipeline */ + match($match: Document): this; + /** Add an out stage to the aggregation pipeline */ + out($out: { + db: string; + coll: string; + } | string): this; + /** + * Add a project stage to the aggregation pipeline + * + * @remarks + * In order to strictly type this function you must provide an interface + * that represents the effect of your projection on the result documents. + * + * By default chaining a projection to your cursor changes the returned type to the generic {@link Document} type. + * You should specify a parameterized type to have assertions on your final results. + * + * @example + * ```typescript + * // Best way + * const docs: AggregationCursor<{ a: number }> = cursor.project<{ a: number }>({ _id: 0, a: true }); + * // Flexible way + * const docs: AggregationCursor = cursor.project({ _id: 0, a: true }); + * ``` + * + * @remarks + * In order to strictly type this function you must provide an interface + * that represents the effect of your projection on the result documents. + * + * **Note for Typescript Users:** adding a transform changes the return type of the iteration of this cursor, + * it **does not** return a new instance of a cursor. This means when calling project, + * you should always assign the result to a new variable in order to get a correctly typed cursor variable. + * Take note of the following example: + * + * @example + * ```typescript + * const cursor: AggregationCursor<{ a: number; b: string }> = coll.aggregate([]); + * const projectCursor = cursor.project<{ a: number }>({ _id: 0, a: true }); + * const aPropOnlyArray: {a: number}[] = await projectCursor.toArray(); + * + * // or always use chaining and save the final cursor + * + * const cursor = coll.aggregate().project<{ a: string }>({ + * _id: 0, + * a: { $convert: { input: '$a', to: 'string' } + * }}); + * ``` + */ + project($project: Document): AggregationCursor; + /** Add a lookup stage to the aggregation pipeline */ + lookup($lookup: Document): this; + /** Add a redact stage to the aggregation pipeline */ + redact($redact: Document): this; + /** Add a skip stage to the aggregation pipeline */ + skip($skip: number): this; + /** Add a sort stage to the aggregation pipeline */ + sort($sort: Sort): this; + /** Add a unwind stage to the aggregation pipeline */ + unwind($unwind: Document | string): this; + /** Add a geoNear stage to the aggregation pipeline */ + geoNear($geoNear: Document): this; +} + +/** @public */ +export declare interface AggregationCursorOptions extends AbstractCursorOptions, AggregateOptions { +} + +/** + * It is possible to search using alternative types in mongodb e.g. + * string types can be searched using a regex in mongo + * array types can be searched using their element type + * @public + */ +export declare type AlternativeType = T extends ReadonlyArray ? T | RegExpOrString : RegExpOrString; + +/** @public */ +export declare type AnyBulkWriteOperation = { + insertOne: InsertOneModel; +} | { + replaceOne: ReplaceOneModel; +} | { + updateOne: UpdateOneModel; +} | { + updateMany: UpdateManyModel; +} | { + deleteOne: DeleteOneModel; +} | { + deleteMany: DeleteManyModel; +}; + +/** + * Used to represent any of the client bulk write models that can be passed as an array + * to MongoClient#bulkWrite. + * @public + */ +export declare type AnyClientBulkWriteModel = ClientInsertOneModel | ClientReplaceOneModel | ClientUpdateOneModel | ClientUpdateManyModel | ClientDeleteOneModel | ClientDeleteManyModel; + +/** @public */ +export declare type AnyError = MongoError | Error; + +/** @public */ +export declare type ArrayElement = Type extends ReadonlyArray ? Item : never; + +/** @public */ +export declare type ArrayOperator = { + $each?: Array>; + $slice?: number; + $position?: number; + $sort?: Sort; +}; + +/** @public */ +export declare interface Auth { + /** The username for auth */ + username?: string; + /** The password for auth */ + password?: string; +} + +/* Excluded from this release type: AuthContext */ + +/** @public */ +export declare const AuthMechanism: Readonly<{ + readonly MONGODB_AWS: "MONGODB-AWS"; + readonly MONGODB_DEFAULT: "DEFAULT"; + readonly MONGODB_GSSAPI: "GSSAPI"; + readonly MONGODB_PLAIN: "PLAIN"; + readonly MONGODB_SCRAM_SHA1: "SCRAM-SHA-1"; + readonly MONGODB_SCRAM_SHA256: "SCRAM-SHA-256"; + readonly MONGODB_X509: "MONGODB-X509"; + readonly MONGODB_OIDC: "MONGODB-OIDC"; +}>; + +/** @public */ +export declare type AuthMechanism = (typeof AuthMechanism)[keyof typeof AuthMechanism]; + +/** @public */ +export declare interface AuthMechanismProperties extends Document { + SERVICE_HOST?: string; + SERVICE_NAME?: string; + SERVICE_REALM?: string; + CANONICALIZE_HOST_NAME?: GSSAPICanonicalizationValue; + /* Excluded from this release type: AWS_SESSION_TOKEN */ + /** A user provided OIDC machine callback function. */ + OIDC_CALLBACK?: OIDCCallbackFunction; + /** A user provided OIDC human interacted callback function. */ + OIDC_HUMAN_CALLBACK?: OIDCCallbackFunction; + /** The OIDC environment. Note that 'test' is for internal use only. */ + ENVIRONMENT?: 'test' | 'azure' | 'gcp' | 'k8s'; + /** Allowed hosts that OIDC auth can connect to. */ + ALLOWED_HOSTS?: string[]; + /** The resource token for OIDC auth in Azure and GCP. */ + TOKEN_RESOURCE?: string; + /** + * A custom AWS credential provider to use. An example using the AWS SDK default provider chain: + * + * ```ts + * const client = new MongoClient(process.env.MONGODB_URI, { + * authMechanismProperties: { + * AWS_CREDENTIAL_PROVIDER: fromNodeProviderChain() + * } + * }); + * ``` + * + * Using a custom function that returns AWS credentials: + * + * ```ts + * const client = new MongoClient(process.env.MONGODB_URI, { + * authMechanismProperties: { + * AWS_CREDENTIAL_PROVIDER: async () => { + * return { + * accessKeyId: process.env.ACCESS_KEY_ID, + * secretAccessKey: process.env.SECRET_ACCESS_KEY + * } + * } + * } + * }); + * ``` + */ + AWS_CREDENTIAL_PROVIDER?: AWSCredentialProvider; +} + +/* Excluded from this release type: AuthProvider */ + +/* Excluded from this release type: AutoEncrypter */ + +/** + * @public + * + * Extra options related to the mongocryptd process + * \* _Available in MongoDB 6.0 or higher._ + */ +export declare type AutoEncryptionExtraOptions = NonNullable; + +/** @public */ +export declare const AutoEncryptionLoggerLevel: Readonly<{ + readonly FatalError: 0; + readonly Error: 1; + readonly Warning: 2; + readonly Info: 3; + readonly Trace: 4; +}>; + +/** + * @public + * The level of severity of the log message + * + * | Value | Level | + * |-------|-------| + * | 0 | Fatal Error | + * | 1 | Error | + * | 2 | Warning | + * | 3 | Info | + * | 4 | Trace | + */ +export declare type AutoEncryptionLoggerLevel = (typeof AutoEncryptionLoggerLevel)[keyof typeof AutoEncryptionLoggerLevel]; + +/** @public */ +export declare interface AutoEncryptionOptions { + /* Excluded from this release type: metadataClient */ + /** A `MongoClient` used to fetch keys from a key vault */ + keyVaultClient?: MongoClient; + /** The namespace where keys are stored in the key vault */ + keyVaultNamespace?: string; + /** Configuration options that are used by specific KMS providers during key generation, encryption, and decryption. */ + kmsProviders?: KMSProviders; + /** Configuration options for custom credential providers. */ + credentialProviders?: CredentialProviders; + /** + * A map of namespaces to a local JSON schema for encryption + * + * **NOTE**: Supplying options.schemaMap provides more security than relying on JSON Schemas obtained from the server. + * It protects against a malicious server advertising a false JSON Schema, which could trick the client into sending decrypted data that should be encrypted. + * Schemas supplied in the schemaMap only apply to configuring automatic encryption for Client-Side Field Level Encryption. + * Other validation rules in the JSON schema will not be enforced by the driver and will result in an error. + */ + schemaMap?: Document; + /** Supply a schema for the encrypted fields in the document */ + encryptedFieldsMap?: Document; + /** Allows the user to bypass auto encryption, maintaining implicit decryption */ + bypassAutoEncryption?: boolean; + /** Allows users to bypass query analysis */ + bypassQueryAnalysis?: boolean; + /** + * Sets the expiration time for the DEK in the cache in milliseconds. Defaults to 60000. 0 means no timeout. + */ + keyExpirationMS?: number; + options?: { + /** An optional hook to catch logging messages from the underlying encryption engine */ + logger?: (level: AutoEncryptionLoggerLevel, message: string) => void; + }; + extraOptions?: { + /** + * A local process the driver communicates with to determine how to encrypt values in a command. + * Defaults to "mongodb://%2Fvar%2Fmongocryptd.sock" if domain sockets are available or "mongodb://localhost:27020" otherwise + */ + mongocryptdURI?: string; + /** If true, autoEncryption will not attempt to spawn a mongocryptd before connecting */ + mongocryptdBypassSpawn?: boolean; + /** The path to the mongocryptd executable on the system */ + mongocryptdSpawnPath?: `${string}mongocryptd${'.exe' | ''}`; + /** Command line arguments to use when auto-spawning a mongocryptd */ + mongocryptdSpawnArgs?: string[]; + /** + * Full path to a MongoDB Crypt shared library to be used (instead of mongocryptd). + * + * This needs to be the path to the file itself, not a directory. + * It can be an absolute or relative path. If the path is relative and + * its first component is `$ORIGIN`, it will be replaced by the directory + * containing the mongodb-client-encryption native addon file. Otherwise, + * the path will be interpreted relative to the current working directory. + * + * Currently, loading different MongoDB Crypt shared library files from different + * MongoClients in the same process is not supported. + * + * If this option is provided and no MongoDB Crypt shared library could be loaded + * from the specified location, creating the MongoClient will fail. + * + * If this option is not provided and `cryptSharedLibRequired` is not specified, + * the AutoEncrypter will attempt to spawn and/or use mongocryptd according + * to the mongocryptd-specific `extraOptions` options. + * + * Specifying a path prevents mongocryptd from being used as a fallback. + * + * Requires the MongoDB Crypt shared library, available in MongoDB 6.0 or higher. + */ + cryptSharedLibPath?: `${string}mongo_crypt_v${number}.${'so' | 'dll' | 'dylib'}`; + /** + * If specified, never use mongocryptd and instead fail when the MongoDB Crypt + * shared library could not be loaded. + * + * This is always true when `cryptSharedLibPath` is specified. + * + * Requires the MongoDB Crypt shared library, available in MongoDB 6.0 or higher. + */ + cryptSharedLibRequired?: boolean; + /* Excluded from this release type: cryptSharedLibSearchPaths */ + }; + proxyOptions?: ProxyOptions; + /** The TLS options to use connecting to the KMS provider */ + tlsOptions?: CSFLEKMSTlsOptions; +} + +/** @public **/ +export declare type AWSCredentialProvider = () => Promise; + +/** + * @public + * Copy of the AwsCredentialIdentityProvider interface from [`smithy/types`](https://socket.dev/npm/package/\@smithy/types/files/1.1.1/dist-types/identity/awsCredentialIdentity.d.ts), + * the return type of the aws-sdk's `fromNodeProviderChain().provider()`. + */ +export declare interface AWSCredentials { + accessKeyId: string; + secretAccessKey: string; + sessionToken?: string; + expiration?: Date; +} + +/** + * @public + * Configuration options for making an AWS encryption key + */ +export declare interface AWSEncryptionKeyOptions { + /** + * The AWS region of the KMS + */ + region: string; + /** + * The Amazon Resource Name (ARN) to the AWS customer master key (CMK) + */ + key: string; + /** + * An alternate host to send KMS requests to. May include port number. + */ + endpoint?: string | undefined; +} + +/** @public */ +export declare interface AWSKMSProviderConfiguration { + /** + * The access key used for the AWS KMS provider + */ + accessKeyId: string; + /** + * The secret access key used for the AWS KMS provider + */ + secretAccessKey: string; + /** + * An optional AWS session token that will be used as the + * X-Amz-Security-Token header for AWS requests. + */ + sessionToken?: string; +} + +/** + * @public + * Configuration options for making an Azure encryption key + */ +export declare interface AzureEncryptionKeyOptions { + /** + * Key name + */ + keyName: string; + /** + * Key vault URL, typically `.vault.azure.net` + */ + keyVaultEndpoint: string; + /** + * Key version + */ + keyVersion?: string | undefined; +} + +/** @public */ +export declare type AzureKMSProviderConfiguration = { + /** + * The tenant ID identifies the organization for the account + */ + tenantId: string; + /** + * The client ID to authenticate a registered application + */ + clientId: string; + /** + * The client secret to authenticate a registered application + */ + clientSecret: string; + /** + * If present, a host with optional port. E.g. "example.com" or "example.com:443". + * This is optional, and only needed if customer is using a non-commercial Azure instance + * (e.g. a government or China account, which use different URLs). + * Defaults to "login.microsoftonline.com" + */ + identityPlatformEndpoint?: string | undefined; +} | { + /** + * If present, an access token to authenticate with Azure. + */ + accessToken: string; +}; + +/** + * Keeps the state of a unordered batch so we can rewrite the results + * correctly after command execution + * + * @public + */ +export declare class Batch { + originalZeroIndex: number; + currentIndex: number; + originalIndexes: number[]; + batchType: BatchType; + operations: T[]; + size: number; + sizeBytes: number; + constructor(batchType: BatchType, originalZeroIndex: number); +} + +/** @public */ +export declare const BatchType: Readonly<{ + readonly INSERT: 1; + readonly UPDATE: 2; + readonly DELETE: 3; +}>; + +/** @public */ +export declare type BatchType = (typeof BatchType)[keyof typeof BatchType]; + +export { Binary } + +/** @public */ +export declare type BitwiseFilter = number /** numeric bit mask */ | Binary /** BinData bit mask */ | ReadonlyArray; + +export { BSON } + +/* Excluded from this release type: BSONElement */ +export { BSONRegExp } + +/** + * BSON Serialization options. + * @public + */ +export declare interface BSONSerializeOptions extends Omit, Omit { + /** + * Enabling the raw option will return a [Node.js Buffer](https://nodejs.org/api/buffer.html) + * which is allocated using [allocUnsafe API](https://nodejs.org/api/buffer.html#static-method-bufferallocunsafesize). + * See this section from the [Node.js Docs here](https://nodejs.org/api/buffer.html#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe) + * for more detail about what "unsafe" refers to in this context. + * If you need to maintain your own editable clone of the bytes returned for an extended life time of the process, it is recommended you allocate + * your own buffer and clone the contents: + * + * @example + * ```ts + * const raw = await collection.findOne({}, { raw: true }); + * const myBuffer = Buffer.alloc(raw.byteLength); + * myBuffer.set(raw, 0); + * // Only save and use `myBuffer` beyond this point + * ``` + * + * @remarks + * Please note there is a known limitation where this option cannot be used at the MongoClient level (see [NODE-3946](https://jira.mongodb.org/browse/NODE-3946)). + * It does correctly work at `Db`, `Collection`, and per operation the same as other BSON options work. + */ + raw?: boolean; + /** Enable utf8 validation when deserializing BSON documents. Defaults to true. */ + enableUtf8Validation?: boolean; +} + +export { BSONSymbol } + +export { BSONType } + +/** @public */ +export declare type BSONTypeAlias = keyof typeof BSONType; + +/* Excluded from this release type: BufferPool */ + +/** @public */ +export declare abstract class BulkOperationBase { + isOrdered: boolean; + /* Excluded from this release type: s */ + operationId?: number; + private collection; + /* Excluded from this release type: retryWrites */ + /* Excluded from this release type: __constructor */ + /** + * Add a single insert document to the bulk operation + * + * @example + * ```ts + * const bulkOp = collection.initializeOrderedBulkOp(); + * + * // Adds three inserts to the bulkOp. + * bulkOp + * .insert({ a: 1 }) + * .insert({ b: 2 }) + * .insert({ c: 3 }); + * await bulkOp.execute(); + * ``` + */ + insert(document: Document): BulkOperationBase; + /** + * Builds a find operation for an update/updateOne/delete/deleteOne/replaceOne. + * Returns a builder object used to complete the definition of the operation. + * + * @example + * ```ts + * const bulkOp = collection.initializeOrderedBulkOp(); + * + * // Add an updateOne to the bulkOp + * bulkOp.find({ a: 1 }).updateOne({ $set: { b: 2 } }); + * + * // Add an updateMany to the bulkOp + * bulkOp.find({ c: 3 }).update({ $set: { d: 4 } }); + * + * // Add an upsert + * bulkOp.find({ e: 5 }).upsert().updateOne({ $set: { f: 6 } }); + * + * // Add a deletion + * bulkOp.find({ g: 7 }).deleteOne(); + * + * // Add a multi deletion + * bulkOp.find({ h: 8 }).delete(); + * + * // Add a replaceOne + * bulkOp.find({ i: 9 }).replaceOne({writeConcern: { j: 10 }}); + * + * // Update using a pipeline (requires Mongodb 4.2 or higher) + * bulk.find({ k: 11, y: { $exists: true }, z: { $exists: true } }).updateOne([ + * { $set: { total: { $sum: [ '$y', '$z' ] } } } + * ]); + * + * // All of the ops will now be executed + * await bulkOp.execute(); + * ``` + */ + find(selector: Document): FindOperators; + /** Specifies a raw operation to perform in the bulk write. */ + raw(op: AnyBulkWriteOperation): this; + get length(): number; + get bsonOptions(): BSONSerializeOptions; + get writeConcern(): WriteConcern | undefined; + get batches(): Batch[]; + execute(options?: BulkWriteOptions): Promise; + /* Excluded from this release type: handleWriteError */ + abstract addToOperationsList(batchType: BatchType, document: Document | UpdateStatement | DeleteStatement): this; + private shouldForceServerObjectId; +} + +/* Excluded from this release type: BulkOperationPrivate */ + +/* Excluded from this release type: BulkResult */ + +/** @public */ +export declare interface BulkWriteOperationError { + index: number; + code: number; + errmsg: string; + errInfo: Document; + op: Document | UpdateStatement | DeleteStatement; +} + +/** @public */ +export declare interface BulkWriteOptions extends CommandOperationOptions { + /** + * Allow driver to bypass schema validation. + * @defaultValue `false` - documents will be validated by default + **/ + bypassDocumentValidation?: boolean; + /** + * If true, when an insert fails, don't execute the remaining writes. + * If false, continue with remaining inserts when one fails. + * @defaultValue `true` - inserts are ordered by default + */ + ordered?: boolean; + /** + * Force server to assign _id values instead of driver. + * @defaultValue `false` - the driver generates `_id` fields by default + **/ + forceServerObjectId?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /* Excluded from this release type: timeoutContext */ +} + +/** + * @public + * The result of a bulk write. + */ +export declare class BulkWriteResult { + private readonly result; + /** Number of documents inserted. */ + readonly insertedCount: number; + /** Number of documents matched for update. */ + readonly matchedCount: number; + /** Number of documents modified. */ + readonly modifiedCount: number; + /** Number of documents deleted. */ + readonly deletedCount: number; + /** Number of documents upserted. */ + readonly upsertedCount: number; + /** Upserted document generated Id's, hash key is the index of the originating operation */ + readonly upsertedIds: { + [key: number]: any; + }; + /** Inserted document generated Id's, hash key is the index of the originating operation */ + readonly insertedIds: { + [key: number]: any; + }; + private static generateIdMap; + /* Excluded from this release type: __constructor */ + /** Evaluates to true if the bulk operation correctly executes */ + get ok(): number; + /* Excluded from this release type: getSuccessfullyInsertedIds */ + /** Returns the upserted id at the given index */ + getUpsertedIdAt(index: number): Document | undefined; + /** Returns raw internal result */ + getRawResponse(): Document; + /** Returns true if the bulk operation contains a write error */ + hasWriteErrors(): boolean; + /** Returns the number of write errors from the bulk operation */ + getWriteErrorCount(): number; + /** Returns a specific write error object */ + getWriteErrorAt(index: number): WriteError | undefined; + /** Retrieve all write errors */ + getWriteErrors(): WriteError[]; + /** Retrieve the write concern error if one exists */ + getWriteConcernError(): WriteConcernError | undefined; + toString(): string; + isOk(): boolean; +} + +/** + * MongoDB Driver style callback + * @public + */ +export declare type Callback = (error?: AnyError, result?: T) => void; + +/* Excluded from this release type: CancellationToken */ + +/** + * Creates a new Change Stream instance. Normally created using {@link Collection#watch|Collection.watch()}. + * @public + */ +export declare class ChangeStream> extends TypedEventEmitter> implements AsyncDisposable { + /** + * An alias for {@link ChangeStream.close|ChangeStream.close()}. + */ + [Symbol.asyncDispose](): Promise; + pipeline: Document[]; + /** + * @remarks WriteConcern can still be present on the options because + * we inherit options from the client/db/collection. The + * key must be present on the options in order to delete it. + * This allows typescript to delete the key but will + * not allow a writeConcern to be assigned as a property on options. + */ + options: ChangeStreamOptions & { + writeConcern?: never; + }; + parent: MongoClient | Db | Collection; + namespace: MongoDBNamespace; + type: symbol; + /* Excluded from this release type: cursor */ + /* Excluded from this release type: cursorStream */ + /* Excluded from this release type: isClosed */ + /* Excluded from this release type: mode */ + /** @event */ + static readonly RESPONSE: "response"; + /** @event */ + static readonly MORE: "more"; + /** @event */ + static readonly INIT: "init"; + /** @event */ + static readonly CLOSE: "close"; + /** + * Fired for each new matching change in the specified namespace. Attaching a `change` + * event listener to a Change Stream will switch the stream into flowing mode. Data will + * then be passed as soon as it is available. + * @event + */ + static readonly CHANGE: "change"; + /** @event */ + static readonly END: "end"; + /** @event */ + static readonly ERROR: "error"; + /** + * Emitted each time the change stream stores a new resume token. + * @event + */ + static readonly RESUME_TOKEN_CHANGED: "resumeTokenChanged"; + private timeoutContext?; + /** + * Note that this property is here to uniquely identify a ChangeStream instance as the owner of + * the {@link CursorTimeoutContext} instance (see {@link ChangeStream._createChangeStreamCursor}) to ensure + * that {@link AbstractCursor.close} does not mutate the timeoutContext. + */ + private contextOwner; + /* Excluded from this release type: __constructor */ + /** The cached resume token that is used to resume after the most recently returned change. */ + get resumeToken(): ResumeToken; + /** Returns the currently buffered documents length of the underlying cursor. */ + bufferedCount(): number; + /** Check if there is any document still available in the Change Stream */ + hasNext(): Promise; + /** Get the next available document from the Change Stream. */ + next(): Promise; + /** + * Try to get the next available document from the Change Stream's cursor or `null` if an empty batch is returned + */ + tryNext(): Promise; + [Symbol.asyncIterator](): AsyncGenerator; + /** Is the cursor closed */ + get closed(): boolean; + /** + * Frees the internal resources used by the change stream. + */ + close(): Promise; + /** + * Return a modified Readable stream including a possible transform method. + * + * NOTE: When using a Stream to process change stream events, the stream will + * NOT automatically resume in the case a resumable error is encountered. + * + * @throws MongoChangeStreamError if the underlying cursor or the change stream is closed + */ + stream(): Readable & AsyncIterable; + /* Excluded from this release type: _setIsEmitter */ + /* Excluded from this release type: _setIsIterator */ + /* Excluded from this release type: _createChangeStreamCursor */ + /* Excluded from this release type: _closeEmitterModeWithError */ + /* Excluded from this release type: _streamEvents */ + /* Excluded from this release type: _endStream */ + /* Excluded from this release type: _processChange */ + /* Excluded from this release type: _processErrorStreamMode */ + /* Excluded from this release type: _processErrorIteratorMode */ + private _resume; +} + +/** + * Only present when the `showExpandedEvents` flag is enabled. + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/modify/#mongodb-data-modify + */ +export declare interface ChangeStreamCollModDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentCollectionUUID, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'modify'; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/create/#mongodb-data-create + */ +export declare interface ChangeStreamCreateDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentCollectionUUID, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'create'; + /** + * The type of the newly created object. + * + * @sinceServerVersion 8.1.0 + */ + nsType?: 'collection' | 'timeseries' | 'view'; +} + +/** + * Only present when the `showExpandedEvents` flag is enabled. + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/createIndexes/#mongodb-data-createIndexes + */ +export declare interface ChangeStreamCreateIndexDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentCollectionUUID, ChangeStreamDocumentOperationDescription, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'createIndexes'; +} + +/* Excluded from this release type: ChangeStreamCursor */ + +/* Excluded from this release type: ChangeStreamCursorOptions */ + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#delete-event + */ +export declare interface ChangeStreamDeleteDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentKey, ChangeStreamDocumentCollectionUUID, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'delete'; + /** Namespace the delete event occurred on */ + ns: ChangeStreamNameSpace; + /** + * Contains the pre-image of the modified or deleted document if the + * pre-image is available for the change event and either 'required' or + * 'whenAvailable' was specified for the 'fullDocumentBeforeChange' option + * when creating the change stream. If 'whenAvailable' was specified but the + * pre-image is unavailable, this will be explicitly set to null. + */ + fullDocumentBeforeChange?: TSchema; +} + +/** @public */ +export declare type ChangeStreamDocument = ChangeStreamInsertDocument | ChangeStreamUpdateDocument | ChangeStreamReplaceDocument | ChangeStreamDeleteDocument | ChangeStreamDropDocument | ChangeStreamRenameDocument | ChangeStreamDropDatabaseDocument | ChangeStreamInvalidateDocument | ChangeStreamCreateIndexDocument | ChangeStreamCreateDocument | ChangeStreamCollModDocument | ChangeStreamDropIndexDocument | ChangeStreamShardCollectionDocument | ChangeStreamReshardCollectionDocument | ChangeStreamRefineCollectionShardKeyDocument; + +/** @public */ +export declare interface ChangeStreamDocumentCollectionUUID { + /** + * The UUID (Binary subtype 4) of the collection that the operation was performed on. + * + * Only present when the `showExpandedEvents` flag is enabled. + * + * **NOTE:** collectionUUID will be converted to a NodeJS Buffer if the promoteBuffers + * flag is enabled. + * + * @sinceServerVersion 6.1.0 + */ + collectionUUID: Binary; +} + +/** @public */ +export declare interface ChangeStreamDocumentCommon { + /** + * The id functions as an opaque token for use when resuming an interrupted + * change stream. + */ + _id: ResumeToken; + /** + * The timestamp from the oplog entry associated with the event. + * For events that happened as part of a multi-document transaction, the associated change stream + * notifications will have the same clusterTime value, namely the time when the transaction was committed. + * On a sharded cluster, events that occur on different shards can have the same clusterTime but be + * associated with different transactions or even not be associated with any transaction. + * To identify events for a single transaction, you can use the combination of lsid and txnNumber in the change stream event document. + */ + clusterTime?: Timestamp; + /** + * The transaction number. + * Only present if the operation is part of a multi-document transaction. + * + * **NOTE:** txnNumber can be a Long if promoteLongs is set to false + */ + txnNumber?: number; + /** + * The identifier for the session associated with the transaction. + * Only present if the operation is part of a multi-document transaction. + */ + lsid?: ServerSessionId; + /** + * When the change stream's backing aggregation pipeline contains the $changeStreamSplitLargeEvent + * stage, events larger than 16MB will be split into multiple events and contain the + * following information about which fragment the current event is. + */ + splitEvent?: ChangeStreamSplitEvent; +} + +/** @public */ +export declare interface ChangeStreamDocumentKey { + /** + * For unsharded collections this contains a single field `_id`. + * For sharded collections, this will contain all the components of the shard key + */ + documentKey: { + _id: InferIdType; + [shardKey: string]: any; + }; +} + +/** @public */ +export declare interface ChangeStreamDocumentOperationDescription { + /** + * An description of the operation. + * + * Only present when the `showExpandedEvents` flag is enabled. + * + * @sinceServerVersion 6.1.0 + */ + operationDescription?: Document; +} + +/** @public */ +export declare interface ChangeStreamDocumentWallTime { + /** + * The server date and time of the database operation. + * wallTime differs from clusterTime in that clusterTime is a timestamp taken from the oplog entry associated with the database operation event. + * @sinceServerVersion 6.0.0 + */ + wallTime?: Date; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#dropdatabase-event + */ +export declare interface ChangeStreamDropDatabaseDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'dropDatabase'; + /** The database dropped */ + ns: { + db: string; + }; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#drop-event + */ +export declare interface ChangeStreamDropDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentCollectionUUID, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'drop'; + /** Namespace the drop event occurred on */ + ns: ChangeStreamNameSpace; +} + +/** + * Only present when the `showExpandedEvents` flag is enabled. + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/dropIndexes/#mongodb-data-dropIndexes + */ +export declare interface ChangeStreamDropIndexDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentCollectionUUID, ChangeStreamDocumentOperationDescription, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'dropIndexes'; +} + +/** @public */ +export declare type ChangeStreamEvents> = { + resumeTokenChanged(token: ResumeToken): void; + init(response: any): void; + more(response?: any): void; + response(): void; + end(): void; + error(error: Error): void; + change(change: TChange): void; + /** + * @remarks Note that the `close` event is currently emitted whenever the internal `ChangeStreamCursor` + * instance is closed, which can occur multiple times for a given `ChangeStream` instance. + * + * TODO(NODE-6434): address this issue in NODE-6434 + */ + close(): void; +}; + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#insert-event + */ +export declare interface ChangeStreamInsertDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentKey, ChangeStreamDocumentCollectionUUID, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'insert'; + /** This key will contain the document being inserted */ + fullDocument: TSchema; + /** Namespace the insert event occurred on */ + ns: ChangeStreamNameSpace; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event + */ +export declare interface ChangeStreamInvalidateDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'invalidate'; +} + +/** @public */ +export declare interface ChangeStreamNameSpace { + db: string; + coll: string; +} + +/** + * Options that can be passed to a ChangeStream. Note that startAfter, resumeAfter, and startAtOperationTime are all mutually exclusive, and the server will error if more than one is specified. + * @public + */ +export declare interface ChangeStreamOptions extends Omit { + /** + * Allowed values: 'updateLookup', 'whenAvailable', 'required'. + * + * When set to 'updateLookup', the change notification for partial updates + * will include both a delta describing the changes to the document as well + * as a copy of the entire document that was changed from some time after + * the change occurred. + * + * When set to 'whenAvailable', configures the change stream to return the + * post-image of the modified document for replace and update change events + * if the post-image for this event is available. + * + * When set to 'required', the same behavior as 'whenAvailable' except that + * an error is raised if the post-image is not available. + */ + fullDocument?: string; + /** + * Allowed values: 'whenAvailable', 'required', 'off'. + * + * The default is to not send a value, which is equivalent to 'off'. + * + * When set to 'whenAvailable', configures the change stream to return the + * pre-image of the modified document for replace, update, and delete change + * events if it is available. + * + * When set to 'required', the same behavior as 'whenAvailable' except that + * an error is raised if the pre-image is not available. + */ + fullDocumentBeforeChange?: string; + /** The maximum amount of time for the server to wait on new documents to satisfy a change stream query. */ + maxAwaitTimeMS?: number; + /** + * Allows you to start a changeStream after a specified event. + * @see https://www.mongodb.com/docs/manual/changeStreams/#resumeafter-for-change-streams + */ + resumeAfter?: ResumeToken; + /** + * Similar to resumeAfter, but will allow you to start after an invalidated event. + * @see https://www.mongodb.com/docs/manual/changeStreams/#startafter-for-change-streams + */ + startAfter?: ResumeToken; + /** Will start the changeStream after the specified operationTime. */ + startAtOperationTime?: OperationTime; + /** + * The number of documents to return per batch. + * @see https://www.mongodb.com/docs/manual/reference/command/aggregate + */ + batchSize?: number; + /** + * When enabled, configures the change stream to include extra change events. + * + * - createIndexes + * - dropIndexes + * - modify + * - create + * - shardCollection + * - reshardCollection + * - refineCollectionShardKey + */ + showExpandedEvents?: boolean; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/refineCollectionShardKey/#mongodb-data-refineCollectionShardKey + */ +export declare interface ChangeStreamRefineCollectionShardKeyDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentCollectionUUID, ChangeStreamDocumentOperationDescription { + /** Describes the type of operation represented in this change notification */ + operationType: 'refineCollectionShardKey'; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#rename-event + */ +export declare interface ChangeStreamRenameDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentCollectionUUID, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'rename'; + /** The new name for the `ns.coll` collection */ + to: { + db: string; + coll: string; + }; + /** The "from" namespace that the rename occurred on */ + ns: ChangeStreamNameSpace; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#replace-event + */ +export declare interface ChangeStreamReplaceDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentKey, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'replace'; + /** The fullDocument of a replace event represents the document after the insert of the replacement document */ + fullDocument: TSchema; + /** Namespace the replace event occurred on */ + ns: ChangeStreamNameSpace; + /** + * Contains the pre-image of the modified or deleted document if the + * pre-image is available for the change event and either 'required' or + * 'whenAvailable' was specified for the 'fullDocumentBeforeChange' option + * when creating the change stream. If 'whenAvailable' was specified but the + * pre-image is unavailable, this will be explicitly set to null. + */ + fullDocumentBeforeChange?: TSchema; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/reshardCollection/#mongodb-data-reshardCollection + */ +export declare interface ChangeStreamReshardCollectionDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentCollectionUUID, ChangeStreamDocumentOperationDescription { + /** Describes the type of operation represented in this change notification */ + operationType: 'reshardCollection'; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/shardCollection/#mongodb-data-shardCollection + */ +export declare interface ChangeStreamShardCollectionDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentCollectionUUID, ChangeStreamDocumentOperationDescription, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'shardCollection'; +} + +/** @public */ +export declare interface ChangeStreamSplitEvent { + /** Which fragment of the change this is. */ + fragment: number; + /** The total number of fragments. */ + of: number; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#update-event + */ +export declare interface ChangeStreamUpdateDocument extends ChangeStreamDocumentCommon, ChangeStreamDocumentKey, ChangeStreamDocumentCollectionUUID, ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'update'; + /** + * This is only set if `fullDocument` is set to `'updateLookup'` + * Contains the point-in-time post-image of the modified document if the + * post-image is available and either 'required' or 'whenAvailable' was + * specified for the 'fullDocument' option when creating the change stream. + */ + fullDocument?: TSchema; + /** Contains a description of updated and removed fields in this operation */ + updateDescription: UpdateDescription; + /** Namespace the update event occurred on */ + ns: ChangeStreamNameSpace; + /** + * Contains the pre-image of the modified or deleted document if the + * pre-image is available for the change event and either 'required' or + * 'whenAvailable' was specified for the 'fullDocumentBeforeChange' option + * when creating the change stream. If 'whenAvailable' was specified but the + * pre-image is unavailable, this will be explicitly set to null. + */ + fullDocumentBeforeChange?: TSchema; +} + +/** @public */ +export declare interface ClientBulkWriteError { + code: number; + message: string; +} + +/** + * A mapping of namespace strings to collections schemas. + * @public + * + * @example + * ```ts + * type MongoDBSchemas = { + * 'db.books': Book; + * 'db.authors': Author; + * } + * + * const model: ClientBulkWriteModel = { + * namespace: 'db.books' + * name: 'insertOne', + * document: { title: 'Practical MongoDB Aggregations', authorName: 3 } // error `authorName` cannot be number + * }; + * ``` + * + * The type of the `namespace` field narrows other parts of the BulkWriteModel to use the correct schema for type assertions. + * + */ +export declare type ClientBulkWriteModel = Record> = { + [Namespace in keyof SchemaMap]: AnyClientBulkWriteModel & { + namespace: Namespace; + }; +}[keyof SchemaMap]; + +/** @public */ +export declare interface ClientBulkWriteOptions extends CommandOperationOptions { + /** + * If true, when an insert fails, don't execute the remaining writes. + * If false, continue with remaining inserts when one fails. + * @defaultValue `true` - inserts are ordered by default + */ + ordered?: boolean; + /** + * Allow driver to bypass schema validation. + * @defaultValue `false` - documents will be validated by default + **/ + bypassDocumentValidation?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /** + * Whether detailed results for each successful operation should be included in the returned + * BulkWriteResult. + */ + verboseResults?: boolean; +} + +/** @public */ +export declare interface ClientBulkWriteResult { + /** + * Whether the bulk write was acknowledged. + */ + readonly acknowledged: boolean; + /** + * The total number of documents inserted across all insert operations. + */ + readonly insertedCount: number; + /** + * The total number of documents upserted across all update operations. + */ + readonly upsertedCount: number; + /** + * The total number of documents matched across all update operations. + */ + readonly matchedCount: number; + /** + * The total number of documents modified across all update operations. + */ + readonly modifiedCount: number; + /** + * The total number of documents deleted across all delete operations. + */ + readonly deletedCount: number; + /** + * The results of each individual insert operation that was successfully performed. + */ + readonly insertResults?: ReadonlyMap; + /** + * The results of each individual update operation that was successfully performed. + */ + readonly updateResults?: ReadonlyMap; + /** + * The results of each individual delete operation that was successfully performed. + */ + readonly deleteResults?: ReadonlyMap; +} + +/** @public */ +export declare interface ClientDeleteManyModel extends ClientWriteModel { + name: 'deleteMany'; + /** + * The filter used to determine if a document should be deleted. + * For a deleteMany operation, all matches are removed. + */ + filter: Filter; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; +} + +/** @public */ +export declare interface ClientDeleteOneModel extends ClientWriteModel { + name: 'deleteOne'; + /** + * The filter used to determine if a document should be deleted. + * For a deleteOne operation, the first match is removed. + */ + filter: Filter; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; +} + +/** @public */ +export declare interface ClientDeleteResult { + /** + * The number of documents that were deleted. + */ + deletedCount: number; +} + +/** + * @public + * The public interface for explicit in-use encryption + */ +export declare class ClientEncryption { + /* Excluded from this release type: _client */ + /* Excluded from this release type: _keyVaultNamespace */ + /* Excluded from this release type: _keyVaultClient */ + /* Excluded from this release type: _proxyOptions */ + /* Excluded from this release type: _tlsOptions */ + /* Excluded from this release type: _kmsProviders */ + /* Excluded from this release type: _timeoutMS */ + /* Excluded from this release type: _mongoCrypt */ + /* Excluded from this release type: _credentialProviders */ + /* Excluded from this release type: getMongoCrypt */ + /** + * Create a new encryption instance + * + * @example + * ```ts + * new ClientEncryption(mongoClient, { + * keyVaultNamespace: 'client.encryption', + * kmsProviders: { + * local: { + * key: masterKey // The master key used for encryption/decryption. A 96-byte long Buffer + * } + * } + * }); + * ``` + * + * @example + * ```ts + * new ClientEncryption(mongoClient, { + * keyVaultNamespace: 'client.encryption', + * kmsProviders: { + * aws: { + * accessKeyId: AWS_ACCESS_KEY, + * secretAccessKey: AWS_SECRET_KEY + * } + * } + * }); + * ``` + */ + constructor(client: MongoClient, options: ClientEncryptionOptions); + /** + * Creates a data key used for explicit encryption and inserts it into the key vault namespace + * + * @example + * ```ts + * // Using async/await to create a local key + * const dataKeyId = await clientEncryption.createDataKey('local'); + * ``` + * + * @example + * ```ts + * // Using async/await to create an aws key + * const dataKeyId = await clientEncryption.createDataKey('aws', { + * masterKey: { + * region: 'us-east-1', + * key: 'xxxxxxxxxxxxxx' // CMK ARN here + * } + * }); + * ``` + * + * @example + * ```ts + * // Using async/await to create an aws key with a keyAltName + * const dataKeyId = await clientEncryption.createDataKey('aws', { + * masterKey: { + * region: 'us-east-1', + * key: 'xxxxxxxxxxxxxx' // CMK ARN here + * }, + * keyAltNames: [ 'mySpecialKey' ] + * }); + * ``` + */ + createDataKey(provider: ClientEncryptionDataKeyProvider, options?: ClientEncryptionCreateDataKeyProviderOptions): Promise; + /** + * Searches the keyvault for any data keys matching the provided filter. If there are matches, rewrapManyDataKey then attempts to re-wrap the data keys using the provided options. + * + * If no matches are found, then no bulk write is performed. + * + * @example + * ```ts + * // rewrapping all data data keys (using a filter that matches all documents) + * const filter = {}; + * + * const result = await clientEncryption.rewrapManyDataKey(filter); + * if (result.bulkWriteResult != null) { + * // keys were re-wrapped, results will be available in the bulkWrite object. + * } + * ``` + * + * @example + * ```ts + * // attempting to rewrap all data keys with no matches + * const filter = { _id: new Binary() } // assume _id matches no documents in the database + * const result = await clientEncryption.rewrapManyDataKey(filter); + * + * if (result.bulkWriteResult == null) { + * // no keys matched, `bulkWriteResult` does not exist on the result object + * } + * ``` + */ + rewrapManyDataKey(filter: Filter, options?: ClientEncryptionRewrapManyDataKeyProviderOptions): Promise<{ + bulkWriteResult?: BulkWriteResult; + }>; + /** + * Deletes the key with the provided id from the keyvault, if it exists. + * + * @example + * ```ts + * // delete a key by _id + * const id = new Binary(); // id is a bson binary subtype 4 object + * const { deletedCount } = await clientEncryption.deleteKey(id); + * + * if (deletedCount != null && deletedCount > 0) { + * // successful deletion + * } + * ``` + * + */ + deleteKey(_id: Binary): Promise; + /** + * Finds all the keys currently stored in the keyvault. + * + * This method will not throw. + * + * @returns a FindCursor over all keys in the keyvault. + * @example + * ```ts + * // fetching all keys + * const keys = await clientEncryption.getKeys().toArray(); + * ``` + */ + getKeys(): FindCursor; + /** + * Finds a key in the keyvault with the specified _id. + * + * Returns a promise that either resolves to a {@link DataKey} if a document matches the key or null if no documents + * match the id. The promise rejects with an error if an error is thrown. + * @example + * ```ts + * // getting a key by id + * const id = new Binary(); // id is a bson binary subtype 4 object + * const key = await clientEncryption.getKey(id); + * if (!key) { + * // key is null if there was no matching key + * } + * ``` + */ + getKey(_id: Binary): Promise; + /** + * Finds a key in the keyvault which has the specified keyAltName. + * + * @param keyAltName - a keyAltName to search for a key + * @returns Returns a promise that either resolves to a {@link DataKey} if a document matches the key or null if no documents + * match the keyAltName. The promise rejects with an error if an error is thrown. + * @example + * ```ts + * // get a key by alt name + * const keyAltName = 'keyAltName'; + * const key = await clientEncryption.getKeyByAltName(keyAltName); + * if (!key) { + * // key is null if there is no matching key + * } + * ``` + */ + getKeyByAltName(keyAltName: string): Promise | null>; + /** + * Adds a keyAltName to a key identified by the provided _id. + * + * This method resolves to/returns the *old* key value (prior to adding the new altKeyName). + * + * @param _id - The id of the document to update. + * @param keyAltName - a keyAltName to search for a key + * @returns Returns a promise that either resolves to a {@link DataKey} if a document matches the key or null if no documents + * match the id. The promise rejects with an error if an error is thrown. + * @example + * ```ts + * // adding an keyAltName to a data key + * const id = new Binary(); // id is a bson binary subtype 4 object + * const keyAltName = 'keyAltName'; + * const oldKey = await clientEncryption.addKeyAltName(id, keyAltName); + * if (!oldKey) { + * // null is returned if there is no matching document with an id matching the supplied id + * } + * ``` + */ + addKeyAltName(_id: Binary, keyAltName: string): Promise | null>; + /** + * Adds a keyAltName to a key identified by the provided _id. + * + * This method resolves to/returns the *old* key value (prior to removing the new altKeyName). + * + * If the removed keyAltName is the last keyAltName for that key, the `altKeyNames` property is unset from the document. + * + * @param _id - The id of the document to update. + * @param keyAltName - a keyAltName to search for a key + * @returns Returns a promise that either resolves to a {@link DataKey} if a document matches the key or null if no documents + * match the id. The promise rejects with an error if an error is thrown. + * @example + * ```ts + * // removing a key alt name from a data key + * const id = new Binary(); // id is a bson binary subtype 4 object + * const keyAltName = 'keyAltName'; + * const oldKey = await clientEncryption.removeKeyAltName(id, keyAltName); + * + * if (!oldKey) { + * // null is returned if there is no matching document with an id matching the supplied id + * } + * ``` + */ + removeKeyAltName(_id: Binary, keyAltName: string): Promise | null>; + /** + * A convenience method for creating an encrypted collection. + * This method will create data keys for any encryptedFields that do not have a `keyId` defined + * and then create a new collection with the full set of encryptedFields. + * + * @param db - A Node.js driver Db object with which to create the collection + * @param name - The name of the collection to be created + * @param options - Options for createDataKey and for createCollection + * @returns created collection and generated encryptedFields + * @throws MongoCryptCreateDataKeyError - If part way through the process a createDataKey invocation fails, an error will be rejected that has the partial `encryptedFields` that were created. + * @throws MongoCryptCreateEncryptedCollectionError - If creating the collection fails, an error will be rejected that has the entire `encryptedFields` that were created. + */ + createEncryptedCollection(db: Db, name: string, options: { + provider: ClientEncryptionDataKeyProvider; + createCollectionOptions: Omit & { + encryptedFields: Document; + }; + masterKey?: AWSEncryptionKeyOptions | AzureEncryptionKeyOptions | GCPEncryptionKeyOptions; + }): Promise<{ + collection: Collection; + encryptedFields: Document; + }>; + /** + * Explicitly encrypt a provided value. Note that either `options.keyId` or `options.keyAltName` must + * be specified. Specifying both `options.keyId` and `options.keyAltName` is considered an error. + * + * @param value - The value that you wish to serialize. Must be of a type that can be serialized into BSON + * @param options - + * @returns a Promise that either resolves with the encrypted value, or rejects with an error. + * + * @example + * ```ts + * // Encryption with async/await api + * async function encryptMyData(value) { + * const keyId = await clientEncryption.createDataKey('local'); + * return clientEncryption.encrypt(value, { keyId, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' }); + * } + * ``` + * + * @example + * ```ts + * // Encryption using a keyAltName + * async function encryptMyData(value) { + * await clientEncryption.createDataKey('local', { keyAltNames: 'mySpecialKey' }); + * return clientEncryption.encrypt(value, { keyAltName: 'mySpecialKey', algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' }); + * } + * ``` + */ + encrypt(value: unknown, options: ClientEncryptionEncryptOptions): Promise; + /** + * Encrypts a Match Expression or Aggregate Expression to query a range index. + * + * Only supported when queryType is "range" and algorithm is "Range". + * + * @param expression - a BSON document of one of the following forms: + * 1. A Match Expression of this form: + * `{$and: [{: {$gt: }}, {: {$lt: }}]}` + * 2. An Aggregate Expression of this form: + * `{$and: [{$gt: [, ]}, {$lt: [, ]}]}` + * + * `$gt` may also be `$gte`. `$lt` may also be `$lte`. + * + * @param options - + * @returns Returns a Promise that either resolves with the encrypted value or rejects with an error. + */ + encryptExpression(expression: Document, options: ClientEncryptionEncryptOptions): Promise; + /** + * Explicitly decrypt a provided encrypted value + * + * @param value - An encrypted value + * @returns a Promise that either resolves with the decrypted value, or rejects with an error + * + * @example + * ```ts + * // Decrypting value with async/await API + * async function decryptMyValue(value) { + * return clientEncryption.decrypt(value); + * } + * ``` + */ + decrypt(value: Binary): Promise; + /* Excluded from this release type: askForKMSCredentials */ + static get libmongocryptVersion(): string; + /* Excluded from this release type: _encrypt */ +} + +/** + * @public + * Options to provide when creating a new data key. + */ +export declare interface ClientEncryptionCreateDataKeyProviderOptions { + /** + * Identifies a new KMS-specific key used to encrypt the new data key + */ + masterKey?: AWSEncryptionKeyOptions | AzureEncryptionKeyOptions | GCPEncryptionKeyOptions | KMIPEncryptionKeyOptions | undefined; + /** + * An optional list of string alternate names used to reference a key. + * If a key is created with alternate names, then encryption may refer to the key by the unique alternate name instead of by _id. + */ + keyAltNames?: string[] | undefined; + /** @experimental */ + keyMaterial?: Buffer | Binary; + /* Excluded from this release type: timeoutContext */ +} + +/** + * @public + * + * A data key provider. Allowed values: + * + * - aws, gcp, local, kmip or azure + * - (`mongodb-client-encryption>=6.0.1` only) a named key, in the form of: + * `aws:`, `gcp:`, `local:`, `kmip:`, `azure:` + * where `name` is an alphanumeric string, underscores allowed. + */ +export declare type ClientEncryptionDataKeyProvider = keyof KMSProviders; + +/** + * @public + * Options to provide when encrypting data. + */ +export declare interface ClientEncryptionEncryptOptions { + /** + * The algorithm to use for encryption. + */ + algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' | 'AEAD_AES_256_CBC_HMAC_SHA_512-Random' | 'Indexed' | 'Unindexed' | 'Range' | 'String' + /** @deprecated Use `'String'` instead. */ + | 'TextPreview'; + /** + * The id of the Binary dataKey to use for encryption + */ + keyId?: Binary; + /** + * A unique string name corresponding to an already existing dataKey. + */ + keyAltName?: string; + /** The contention factor. */ + contentionFactor?: bigint | number; + /** + * The query type. + */ + queryType?: 'equality' | 'range' | 'prefix' | 'suffix' | 'substring' + /** @deprecated Use `'prefix'` instead. */ + | 'prefixPreview' + /** @deprecated Use `'suffix'` instead. */ + | 'suffixPreview' + /** @deprecated Use `'substring'` instead. */ + | 'substringPreview'; + /** The index options for a Queryable Encryption field supporting "range" queries.*/ + rangeOptions?: RangeOptions; + /** Options for a Queryable Encryption field supporting string queries. Only valid when `algorithm` is `'String'` or the deprecated `'TextPreview'`. */ + stringOptions?: StringQueryOptions; + /** + * Options for a Queryable Encryption field supporting text queries. Only valid when `algorithm` is `'String'` or the deprecated `'TextPreview'`. + * + * @deprecated Use `stringOptions` instead. + */ + textOptions?: StringQueryOptions; +} + +/** + * @public + * Additional settings to provide when creating a new `ClientEncryption` instance. + */ +export declare interface ClientEncryptionOptions { + /** + * The namespace of the key vault, used to store encryption keys + */ + keyVaultNamespace: string; + /** + * A MongoClient used to fetch keys from a key vault. Defaults to client. + */ + keyVaultClient?: MongoClient | undefined; + /** + * Options for specific KMS providers to use + */ + kmsProviders?: KMSProviders; + /** + * Options for user provided custom credential providers. + */ + credentialProviders?: CredentialProviders; + /** + * Options for specifying a Socks5 proxy to use for connecting to the KMS. + */ + proxyOptions?: ProxyOptions; + /** + * TLS options for kms providers to use. + */ + tlsOptions?: CSFLEKMSTlsOptions; + /** + * Sets the expiration time for the DEK in the cache in milliseconds. Defaults to 60000. 0 means no timeout. + */ + keyExpirationMS?: number; + /** + * @experimental + * + * The timeout setting to be used for all the operations on ClientEncryption. + * + * When provided, `timeoutMS` is used as the timeout for each operation executed on + * the ClientEncryption object. For example: + * + * ```typescript + * const clientEncryption = new ClientEncryption(client, { + * timeoutMS: 1_000 + * kmsProviders: { local: { key: '' } } + * }); + * + * // `1_000` is used as the timeout for createDataKey call + * await clientEncryption.createDataKey('local'); + * ``` + * + * If `timeoutMS` is configured on the provided client, the client's `timeoutMS` value + * will be used unless `timeoutMS` is also provided as a client encryption option. + * + * ```typescript + * const client = new MongoClient('', { timeoutMS: 2_000 }); + * + * // timeoutMS is set to 1_000 on clientEncryption + * const clientEncryption = new ClientEncryption(client, { + * timeoutMS: 1_000 + * kmsProviders: { local: { key: '' } } + * }); + * ``` + */ + timeoutMS?: number; +} + +/** + * @public + * @experimental + */ +export declare interface ClientEncryptionRewrapManyDataKeyProviderOptions { + provider: ClientEncryptionDataKeyProvider; + masterKey?: AWSEncryptionKeyOptions | AzureEncryptionKeyOptions | GCPEncryptionKeyOptions | KMIPEncryptionKeyOptions | undefined; +} + +/** + * @public + * @experimental + */ +export declare interface ClientEncryptionRewrapManyDataKeyResult { + /** The result of rewrapping data keys. If unset, no keys matched the filter. */ + bulkWriteResult?: BulkWriteResult; +} + +/** + * @public + * + * Socket options to use for KMS requests. + */ +export declare type ClientEncryptionSocketOptions = Pick; + +/** + * @public + * + * TLS options to use when connecting. The spec specifically calls out which insecure + * tls options are not allowed: + * + * - tlsAllowInvalidCertificates + * - tlsAllowInvalidHostnames + * - tlsInsecure + * + * These options are not included in the type, and are ignored if provided. + */ +export declare type ClientEncryptionTlsOptions = Pick; + +/** @public */ +export declare interface ClientInsertOneModel extends ClientWriteModel { + name: 'insertOne'; + /** The document to insert. */ + document: OptionalId; +} + +/** @public */ +export declare interface ClientInsertOneResult { + /** + * The _id of the inserted document. + */ + insertedId: any; +} + +/* Excluded from this release type: ClientMetadata */ + +/** @public */ +export declare interface ClientReplaceOneModel extends ClientWriteModel { + name: 'replaceOne'; + /** + * The filter used to determine if a document should be replaced. + * For a replaceOne operation, the first match is replaced. + */ + filter: Filter; + /** The document with which to replace the matched document. */ + replacement: WithoutId; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; + /** When true, creates a new document if no document matches the query. */ + upsert?: boolean; + /** Specifies the sort order for the documents matched by the filter. */ + sort?: Sort; +} + +/** + * A class representing a client session on the server + * + * NOTE: not meant to be instantiated directly. + * @public + */ +export declare class ClientSession extends TypedEventEmitter implements AsyncDisposable { + /* Excluded from this release type: client */ + /* Excluded from this release type: sessionPool */ + hasEnded: boolean; + clientOptions: MongoOptions; + supports: { + causalConsistency: boolean; + }; + clusterTime?: ClusterTime; + operationTime?: Timestamp; + explicit: boolean; + /* Excluded from this release type: owner */ + defaultTransactionOptions: TransactionOptions; + /* Excluded from this release type: transaction */ + /* Excluded from this release type: commitAttempted */ + readonly snapshotEnabled: boolean; + /* Excluded from this release type: _serverSession */ + /* Excluded from this release type: snapshotTime */ + /* Excluded from this release type: pinnedConnection */ + /* Excluded from this release type: txnNumberIncrement */ + /** + * @experimental + * Specifies the time an operation in a given `ClientSession` will run until it throws a timeout error + */ + timeoutMS?: number; + /* Excluded from this release type: timeoutContext */ + /* Excluded from this release type: __constructor */ + /** The server id associated with this session */ + get id(): ServerSessionId | undefined; + get serverSession(): ServerSession; + get loadBalanced(): boolean; + /* Excluded from this release type: pin */ + /* Excluded from this release type: unpin */ + get isPinned(): boolean; + /** + * Frees any client-side resources held by the current session. If a session is in a transaction, + * the transaction is aborted. + * + * Does not end the session on the server. + * + * @param options - Optional settings. Currently reserved for future use + */ + endSession(options?: EndSessionOptions): Promise; + /** + * An alias for {@link ClientSession.endSession|ClientSession.endSession()}. + */ + [Symbol.asyncDispose](): Promise; + /** + * Advances the operationTime for a ClientSession. + * + * @param operationTime - the `BSON.Timestamp` of the operation type it is desired to advance to + */ + advanceOperationTime(operationTime: Timestamp): void; + /** + * Advances the clusterTime for a ClientSession to the provided clusterTime of another ClientSession + * + * @param clusterTime - the $clusterTime returned by the server from another session in the form of a document containing the `BSON.Timestamp` clusterTime and signature + */ + advanceClusterTime(clusterTime: ClusterTime): void; + /** + * Used to determine if this session equals another + * + * @param session - The session to compare to + */ + equals(session: ClientSession): boolean; + /** + * Increment the transaction number on the internal ServerSession + * + * @privateRemarks + * This helper increments a value stored on the client session that will be + * added to the serverSession's txnNumber upon applying it to a command. + * This is because the serverSession is lazily acquired after a connection is obtained + */ + incrementTransactionNumber(): void; + /** @returns whether this session is currently in a transaction or not */ + inTransaction(): boolean; + /** + * Starts a new transaction with the given options. + * + * @remarks + * **IMPORTANT**: Running operations in parallel is not supported during a transaction. The use of `Promise.all`, + * `Promise.allSettled`, `Promise.race`, etc to parallelize operations inside a transaction is + * undefined behaviour. + * + * @param options - Options for the transaction + */ + startTransaction(options?: TransactionOptions): void; + /** + * Commits the currently active transaction in this session. + * + * @param options - Optional options, can be used to override `defaultTimeoutMS`. + */ + commitTransaction(options?: { + timeoutMS?: number; + }): Promise; + /** + * Aborts the currently active transaction in this session. + * + * @param options - Optional options, can be used to override `defaultTimeoutMS`. + */ + abortTransaction(options?: { + timeoutMS?: number; + }): Promise; + /* Excluded from this release type: abortTransaction */ + /** + * This is here to ensure that ClientSession is never serialized to BSON. + */ + toBSON(): never; + /** + * Starts a transaction and runs a provided function, ensuring the commitTransaction is always attempted when all operations run in the function have completed. + * + * **IMPORTANT:** This method requires the function passed in to return a Promise. That promise must be made by `await`-ing all operations in such a way that rejections are propagated to the returned promise. + * + * **IMPORTANT:** Running operations in parallel is not supported during a transaction. The use of `Promise.all`, + * `Promise.allSettled`, `Promise.race`, etc to parallelize operations inside a transaction is + * undefined behaviour. + * + * **IMPORTANT:** When running an operation inside a `withTransaction` callback, if it is not + * provided the explicit session in its options, it will not be part of the transaction and it will not respect timeoutMS. + * + * + * @remarks + * - If all operations successfully complete and the `commitTransaction` operation is successful, then the provided function will return the result of the provided function. + * - If the transaction is unable to complete or an error is thrown from within the provided function, then the provided function will throw an error. + * - If the transaction is manually aborted within the provided function it will not throw. + * - If the driver needs to attempt to retry the operations, the provided function may be called multiple times. + * + * Checkout a descriptive example here: + * @see https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-implement-transactions + * + * If a command inside withTransaction fails: + * - It may cause the transaction on the server to be aborted. + * - This situation is normally handled transparently by the driver. + * - However, if the application catches such an error and does not rethrow it, the driver will not be able to determine whether the transaction was aborted or not. + * - The driver will then retry the transaction indefinitely. + * + * To avoid this situation, the application must not silently handle errors within the provided function. + * If the application needs to handle errors within, it must await all operations such that if an operation is rejected it becomes the rejection of the callback function passed into withTransaction. + * + * @param fn - callback to run within a transaction + * @param options - optional settings for the transaction + * @returns A raw command response or undefined + */ + withTransaction(fn: WithTransactionCallback, options?: TransactionOptions & { + /** + * Configures a timeoutMS expiry for the entire withTransactionCallback. + * + * @remarks + * - The remaining timeout will not be applied to callback operations that do not use the ClientSession. + * - Overriding timeoutMS for operations executed using the explicit session inside the provided callback will result in a client-side error. + */ + timeoutMS?: number; + }): Promise; +} + +/** @public */ +export declare type ClientSessionEvents = { + ended(session: ClientSession): void; +}; + +/** @public */ +export declare interface ClientSessionOptions { + /** Whether causal consistency should be enabled on this session */ + causalConsistency?: boolean; + /** Whether all read operations should be read from the same snapshot for this session (NOTE: not compatible with `causalConsistency=true`) */ + snapshot?: boolean; + /** The default TransactionOptions to use for transactions started on this session. */ + defaultTransactionOptions?: TransactionOptions; + /** + * @public + * @experimental + * An overriding timeoutMS value to use for a client-side timeout. + * If not provided the session uses the timeoutMS specified on the MongoClient. + */ + defaultTimeoutMS?: number; + /* Excluded from this release type: owner */ + /* Excluded from this release type: explicit */ + /* Excluded from this release type: initialClusterTime */ +} + +/** @public */ +export declare interface ClientUpdateManyModel extends ClientWriteModel { + name: 'updateMany'; + /** + * The filter used to determine if a document should be updated. + * For an updateMany operation, all matches are updated. + */ + filter: Filter; + /** + * The modifications to apply. The value can be either: + * UpdateFilter - A document that contains update operator expressions, + * Document[] - an aggregation pipeline. + */ + update: UpdateFilter | Document[]; + /** A set of filters specifying to which array elements an update should apply. */ + arrayFilters?: Document[]; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; + /** When true, creates a new document if no document matches the query. */ + upsert?: boolean; +} + +/** @public */ +export declare interface ClientUpdateOneModel extends ClientWriteModel { + name: 'updateOne'; + /** + * The filter used to determine if a document should be updated. + * For an updateOne operation, the first match is updated. + */ + filter: Filter; + /** + * The modifications to apply. The value can be either: + * UpdateFilter - A document that contains update operator expressions, + * Document[] - an aggregation pipeline. + */ + update: UpdateFilter | Document[]; + /** A set of filters specifying to which array elements an update should apply. */ + arrayFilters?: Document[]; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; + /** When true, creates a new document if no document matches the query. */ + upsert?: boolean; + /** Specifies the sort order for the documents matched by the filter. */ + sort?: Sort; +} + +/** @public */ +export declare interface ClientUpdateResult { + /** + * The number of documents that matched the filter. + */ + matchedCount: number; + /** + * The number of documents that were modified. + */ + modifiedCount: number; + /** + * The _id field of the upserted document if an upsert occurred. + * + * It MUST be possible to discern between a BSON Null upserted ID value and this field being + * unset. If necessary, drivers MAY add a didUpsert boolean field to differentiate between + * these two cases. + */ + upsertedId?: any; + /** + * Determines if the upsert did include an _id, which includes the case of the _id being null. + */ + didUpsert: boolean; +} + +/** @public */ +export declare interface ClientWriteModel { + /** + * The namespace for the write. + * + * A namespace is a combination of the database name and the name of the collection: `.`. + * All documents belong to a namespace. + * + * @see https://www.mongodb.com/docs/manual/reference/limits/#std-label-faq-dev-namespace + */ + namespace: string; +} + +/** @public + * Configuration options for clustered collections + * @see https://www.mongodb.com/docs/manual/core/clustered-collections/ + */ +export declare interface ClusteredCollectionOptions extends Document { + name?: string; + key: Document; + unique: boolean; +} + +/** + * @public + * Gossiped in component for the cluster time tracking the state of user databases + * across the cluster. It may optionally include a signature identifying the process that + * generated such a value. + */ +export declare interface ClusterTime { + clusterTime: Timestamp; + /** Used to validate the identity of a request or response's ClusterTime. */ + signature?: { + hash: Binary; + keyId: Long; + }; +} + +export { Code } + +/** @public */ +export declare interface CollationOptions { + locale: string; + caseLevel?: boolean; + caseFirst?: string; + strength?: number; + numericOrdering?: boolean; + alternate?: string; + maxVariable?: string; + backwards?: boolean; + normalization?: boolean; +} + +/** + * The **Collection** class is an internal class that embodies a MongoDB collection + * allowing for insert/find/update/delete and other command operation on that MongoDB collection. + * + * **COLLECTION Cannot directly be instantiated** + * @public + * + * @example + * ```ts + * import { MongoClient } from 'mongodb'; + * + * interface Pet { + * name: string; + * kind: 'dog' | 'cat' | 'fish'; + * } + * + * const client = new MongoClient('mongodb://localhost:27017'); + * const pets = client.db().collection('pets'); + * + * const petCursor = pets.find(); + * + * for await (const pet of petCursor) { + * console.log(`${pet.name} is a ${pet.kind}!`); + * } + * ``` + */ +export declare class Collection { + /* Excluded from this release type: s */ + /* Excluded from this release type: client */ + /** + * Get the database object for the collection. + */ + readonly db: Db; + /* Excluded from this release type: __constructor */ + /** + * The name of the database this collection belongs to + */ + get dbName(): string; + /** + * The name of this collection + */ + get collectionName(): string; + /** + * The namespace of this collection, in the format `${this.dbName}.${this.collectionName}` + */ + get namespace(): string; + /* Excluded from this release type: fullNamespace */ + /** + * The current readConcern of the collection. If not explicitly defined for + * this collection, will be inherited from the parent DB + */ + get readConcern(): ReadConcern | undefined; + /** + * The current readPreference of the collection. If not explicitly defined for + * this collection, will be inherited from the parent DB + */ + get readPreference(): ReadPreference | undefined; + get bsonOptions(): BSONSerializeOptions; + /** + * The current writeConcern of the collection. If not explicitly defined for + * this collection, will be inherited from the parent DB + */ + get writeConcern(): WriteConcern | undefined; + /** The current index hint for the collection */ + get hint(): Hint | undefined; + set hint(v: Hint | undefined); + get timeoutMS(): number | undefined; + /** + * Inserts a single document into MongoDB. If documents passed in do not contain the **_id** field, + * one will be added to each of the documents missing it by the driver, mutating the document. This behavior + * can be overridden by setting the **forceServerObjectId** flag. + * + * @param doc - The document to insert + * @param options - Optional settings for the command + */ + insertOne(doc: OptionalUnlessRequiredId, options?: InsertOneOptions): Promise>; + /** + * Inserts an array of documents into MongoDB. If documents passed in do not contain the **_id** field, + * one will be added to each of the documents missing it by the driver, mutating the document. This behavior + * can be overridden by setting the **forceServerObjectId** flag. + * + * @param docs - The documents to insert + * @param options - Optional settings for the command + */ + insertMany(docs: ReadonlyArray>, options?: BulkWriteOptions): Promise>; + /** + * Perform a bulkWrite operation without a fluent API + * + * Legal operation types are + * - `insertOne` + * - `replaceOne` + * - `updateOne` + * - `updateMany` + * - `deleteOne` + * - `deleteMany` + * + * If documents passed in do not contain the **_id** field, + * one will be added to each of the documents missing it by the driver, mutating the document. This behavior + * can be overridden by setting the **forceServerObjectId** flag. + * + * @param operations - Bulk operations to perform + * @param options - Optional settings for the command + * @throws MongoDriverError if operations is not an array + */ + bulkWrite(operations: ReadonlyArray>, options?: BulkWriteOptions): Promise; + /** + * Update a single document in a collection + * + * The value of `update` can be either: + * - UpdateFilter - A document that contains update operator expressions, + * - Document[] - an aggregation pipeline. + * + * @param filter - The filter used to select the document to update + * @param update - The modifications to apply + * @param options - Optional settings for the command + */ + updateOne(filter: Filter, update: UpdateFilter | Document[], options?: UpdateOptions & { + sort?: Sort; + }): Promise>; + /** + * Replace a document in a collection with another document + * + * @param filter - The filter used to select the document to replace + * @param replacement - The Document that replaces the matching document + * @param options - Optional settings for the command + */ + replaceOne(filter: Filter, replacement: WithoutId, options?: ReplaceOptions): Promise>; + /** + * Update multiple documents in a collection + * + * The value of `update` can be either: + * - UpdateFilter - A document that contains update operator expressions, + * - Document[] - an aggregation pipeline. + * + * @param filter - The filter used to select the document to update + * @param update - The modifications to apply + * @param options - Optional settings for the command + */ + updateMany(filter: Filter, update: UpdateFilter | Document[], options?: UpdateOptions): Promise>; + /** + * Delete a document from a collection + * + * @param filter - The filter used to select the document to remove + * @param options - Optional settings for the command + */ + deleteOne(filter?: Filter, options?: DeleteOptions): Promise; + /** + * Delete multiple documents from a collection + * + * @param filter - The filter used to select the documents to remove + * @param options - Optional settings for the command + */ + deleteMany(filter?: Filter, options?: DeleteOptions): Promise; + /** + * Rename the collection. + * + * @remarks + * This operation does not inherit options from the Db or MongoClient. + * + * @param newName - New name of of the collection. + * @param options - Optional settings for the command + */ + rename(newName: string, options?: RenameOptions): Promise; + /** + * Drop the collection from the database, removing it permanently. New accesses will create a new collection. + * + * @param options - Optional settings for the command + */ + drop(options?: DropCollectionOptions): Promise; + /** + * Fetches the first document that matches the filter + * + * @param filter - Query for find Operation + * @param options - Optional settings for the command + */ + findOne(): Promise | null>; + findOne(filter: Filter): Promise | null>; + findOne(filter: Filter, options: Omit & Abortable): Promise | null>; + findOne(): Promise; + findOne(filter: Filter): Promise; + findOne(filter: Filter, options?: Omit & Abortable): Promise; + /** + * Creates a cursor for a filter that can be used to iterate over results from MongoDB + * + * @param filter - The filter predicate. If unspecified, then all documents in the collection will match the predicate + */ + find(): FindCursor>; + find(filter: Filter, options?: FindOptions & Abortable): FindCursor>; + find(filter: Filter, options?: FindOptions & Abortable): FindCursor; + /** + * Returns the options of the collection. + * + * @param options - Optional settings for the command + */ + options(options?: OperationOptions): Promise; + /** + * Returns if the collection is a capped collection + * + * @param options - Optional settings for the command + */ + isCapped(options?: OperationOptions): Promise; + /** + * Creates an index on the db and collection collection. + * + * @param indexSpec - The field name or index specification to create an index for + * @param options - Optional settings for the command + * + * @example + * ```ts + * const collection = client.db('foo').collection('bar'); + * + * await collection.createIndex({ a: 1, b: -1 }); + * + * // Alternate syntax for { c: 1, d: -1 } that ensures order of indexes + * await collection.createIndex([ [c, 1], [d, -1] ]); + * + * // Equivalent to { e: 1 } + * await collection.createIndex('e'); + * + * // Equivalent to { f: 1, g: 1 } + * await collection.createIndex(['f', 'g']) + * + * // Equivalent to { h: 1, i: -1 } + * await collection.createIndex([ { h: 1 }, { i: -1 } ]); + * + * // Equivalent to { j: 1, k: -1, l: 2d } + * await collection.createIndex(['j', ['k', -1], { l: '2d' }]) + * ``` + */ + createIndex(indexSpec: IndexSpecification, options?: CreateIndexesOptions): Promise; + /** + * Creates multiple indexes in the collection, this method is only supported for + * MongoDB 2.6 or higher. Earlier version of MongoDB will throw a command not supported + * error. + * + * **Note**: Unlike {@link Collection#createIndex| createIndex}, this function takes in raw index specifications. + * Index specifications are defined {@link https://www.mongodb.com/docs/manual/reference/command/createIndexes/| here}. + * + * @param indexSpecs - An array of index specifications to be created + * @param options - Optional settings for the command + * + * @example + * ```ts + * const collection = client.db('foo').collection('bar'); + * await collection.createIndexes([ + * // Simple index on field fizz + * { + * key: { fizz: 1 }, + * } + * // wildcard index + * { + * key: { '$**': 1 } + * }, + * // named index on darmok and jalad + * { + * key: { darmok: 1, jalad: -1 } + * name: 'tanagra' + * } + * ]); + * ``` + */ + createIndexes(indexSpecs: IndexDescription[], options?: CreateIndexesOptions): Promise; + /** + * Drops an index from this collection. + * + * @param indexName - Name of the index to drop. + * @param options - Optional settings for the command + */ + dropIndex(indexName: string, options?: DropIndexesOptions): Promise; + /** + * Drops all indexes from this collection. + * + * @param options - Optional settings for the command + */ + dropIndexes(options?: DropIndexesOptions): Promise; + /** + * Get the list of all indexes information for the collection. + * + * @param options - Optional settings for the command + */ + listIndexes(options?: ListIndexesOptions): ListIndexesCursor; + /** + * Checks if one or more indexes exist on the collection, fails on first non-existing index + * + * @param indexes - One or more index names to check. + * @param options - Optional settings for the command + */ + indexExists(indexes: string | string[], options?: ListIndexesOptions): Promise; + /** + * Retrieves this collections index info. + * + * @param options - Optional settings for the command + */ + indexInformation(options: IndexInformationOptions & { + full: true; + }): Promise; + indexInformation(options: IndexInformationOptions & { + full?: false; + }): Promise; + indexInformation(options: IndexInformationOptions): Promise; + indexInformation(): Promise; + /** + * Gets an estimate of the count of documents in a collection using collection metadata. + * This will always run a count command on all server versions. + * + * due to an oversight in versions 5.0.0-5.0.8 of MongoDB, the count command, + * which estimatedDocumentCount uses in its implementation, was not included in v1 of + * the Stable API, and so users of the Stable API with estimatedDocumentCount are + * recommended to upgrade their server version to 5.0.9+ or set apiStrict: false to avoid + * encountering errors. + * + * @see {@link https://www.mongodb.com/docs/manual/reference/command/count/#behavior|Count: Behavior} + * @param options - Optional settings for the command + */ + estimatedDocumentCount(options?: EstimatedDocumentCountOptions): Promise; + /** + * Gets the number of documents matching the filter. + * For a fast count of the total documents in a collection see {@link Collection#estimatedDocumentCount| estimatedDocumentCount}. + * + * Due to countDocuments using the $match aggregation pipeline stage, certain query operators cannot be used in countDocuments. This includes the $where and $near query operators, among others. Details can be found in the documentation for the $match aggregation pipeline stage. + * + * **Note**: When migrating from {@link Collection#count| count} to {@link Collection#countDocuments| countDocuments} + * the following query operators must be replaced: + * + * | Operator | Replacement | + * | -------- | ----------- | + * | `$where` | [`$expr`][1] | + * | `$near` | [`$geoWithin`][2] with [`$center`][3] | + * | `$nearSphere` | [`$geoWithin`][2] with [`$centerSphere`][4] | + * + * [1]: https://www.mongodb.com/docs/manual/reference/operator/query/expr/ + * [2]: https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/ + * [3]: https://www.mongodb.com/docs/manual/reference/operator/query/center/#op._S_center + * [4]: https://www.mongodb.com/docs/manual/reference/operator/query/centerSphere/#op._S_centerSphere + * + * @param filter - The filter for the count + * @param options - Optional settings for the command + * + * @see https://www.mongodb.com/docs/manual/reference/operator/query/expr/ + * @see https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/ + * @see https://www.mongodb.com/docs/manual/reference/operator/query/center/#op._S_center + * @see https://www.mongodb.com/docs/manual/reference/operator/query/centerSphere/#op._S_centerSphere + */ + countDocuments(filter?: Filter, options?: CountDocumentsOptions & Abortable): Promise; + /** + * The distinct command returns a list of distinct values for the given key across a collection. + * + * @param key - Field of the document to find distinct values for + * @param filter - The filter for filtering the set of documents to which we apply the distinct filter. + * @param options - Optional settings for the command + */ + distinct>(key: Key): Promise[Key]>>>; + distinct>(key: Key, filter: Filter): Promise[Key]>>>; + distinct>(key: Key, filter: Filter, options: DistinctOptions): Promise[Key]>>>; + distinct>(key: Key, filter: Filter, options: DistinctOptions & { + explain: ExplainVerbosityLike | ExplainCommandOptions; + }): Promise; + distinct(key: string): Promise; + distinct(key: string, filter: Filter): Promise; + distinct(key: string, filter: Filter, options: DistinctOptions): Promise; + /** + * Retrieve all the indexes on the collection. + * + * @param options - Optional settings for the command + */ + indexes(options: IndexInformationOptions & { + full?: true; + }): Promise; + indexes(options: IndexInformationOptions & { + full: false; + }): Promise; + indexes(options: IndexInformationOptions): Promise; + indexes(options?: ListIndexesOptions): Promise; + /** + * Find a document and delete it in one atomic operation. Requires a write lock for the duration of the operation. + * + * @param filter - The filter used to select the document to remove + * @param options - Optional settings for the command + */ + findOneAndDelete(filter: Filter, options: FindOneAndDeleteOptions & { + includeResultMetadata: true; + }): Promise>; + findOneAndDelete(filter: Filter, options: FindOneAndDeleteOptions & { + includeResultMetadata: false; + }): Promise | null>; + findOneAndDelete(filter: Filter, options: FindOneAndDeleteOptions): Promise | null>; + findOneAndDelete(filter: Filter): Promise | null>; + /** + * Find a document and replace it in one atomic operation. Requires a write lock for the duration of the operation. + * + * @param filter - The filter used to select the document to replace + * @param replacement - The Document that replaces the matching document + * @param options - Optional settings for the command + */ + findOneAndReplace(filter: Filter, replacement: WithoutId, options: FindOneAndReplaceOptions & { + includeResultMetadata: true; + }): Promise>; + findOneAndReplace(filter: Filter, replacement: WithoutId, options: FindOneAndReplaceOptions & { + includeResultMetadata: false; + }): Promise | null>; + findOneAndReplace(filter: Filter, replacement: WithoutId, options: FindOneAndReplaceOptions): Promise | null>; + findOneAndReplace(filter: Filter, replacement: WithoutId): Promise | null>; + /** + * Find a document and update it in one atomic operation. Requires a write lock for the duration of the operation. + * + * The value of `update` can be either: + * - UpdateFilter - A document that contains update operator expressions, + * - Document[] - an aggregation pipeline consisting of the following stages: + * - $addFields and its alias $set + * - $project and its alias $unset + * - $replaceRoot and its alias $replaceWith. + * See the [findAndModify command documentation](https://www.mongodb.com/docs/manual/reference/command/findAndModify) for details. + * + * @param filter - The filter used to select the document to update + * @param update - The modifications to apply + * @param options - Optional settings for the command + */ + findOneAndUpdate(filter: Filter, update: UpdateFilter | Document[], options: FindOneAndUpdateOptions & { + includeResultMetadata: true; + }): Promise>; + findOneAndUpdate(filter: Filter, update: UpdateFilter | Document[], options: FindOneAndUpdateOptions & { + includeResultMetadata: false; + }): Promise | null>; + findOneAndUpdate(filter: Filter, update: UpdateFilter | Document[], options: FindOneAndUpdateOptions): Promise | null>; + findOneAndUpdate(filter: Filter, update: UpdateFilter | Document[]): Promise | null>; + /** + * Execute an aggregation framework pipeline against the collection, needs MongoDB \>= 2.2 + * + * @param pipeline - An array of aggregation pipelines to execute + * @param options - Optional settings for the command + */ + aggregate(pipeline?: Document[], options?: AggregateOptions & Abortable): AggregationCursor; + /** + * Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this collection. + * + * @remarks + * watch() accepts two generic arguments for distinct use cases: + * - The first is to override the schema that may be defined for this specific collection + * - The second is to override the shape of the change stream document entirely, if it is not provided the type will default to ChangeStreamDocument of the first argument + * @example + * By just providing the first argument I can type the change to be `ChangeStreamDocument<{ _id: number }>` + * ```ts + * collection.watch<{ _id: number }>() + * .on('change', change => console.log(change._id.toFixed(4))); + * ``` + * + * @example + * Passing a second argument provides a way to reflect the type changes caused by an advanced pipeline. + * Here, we are using a pipeline to have MongoDB filter for insert changes only and add a comment. + * No need start from scratch on the ChangeStreamInsertDocument type! + * By using an intersection we can save time and ensure defaults remain the same type! + * ```ts + * collection + * .watch & { comment: string }>([ + * { $addFields: { comment: 'big changes' } }, + * { $match: { operationType: 'insert' } } + * ]) + * .on('change', change => { + * change.comment.startsWith('big'); + * change.operationType === 'insert'; + * // No need to narrow in code because the generics did that for us! + * expectType(change.fullDocument); + * }); + * ``` + * + * @remarks + * When `timeoutMS` is configured for a change stream, it will have different behaviour depending + * on whether the change stream is in iterator mode or emitter mode. In both cases, a change + * stream will time out if it does not receive a change event within `timeoutMS` of the last change + * event. + * + * Note that if a change stream is consistently timing out when watching a collection, database or + * client that is being changed, then this may be due to the server timing out before it can finish + * processing the existing oplog. To address this, restart the change stream with a higher + * `timeoutMS`. + * + * If the change stream times out the initial aggregate operation to establish the change stream on + * the server, then the client will close the change stream. If the getMore calls to the server + * time out, then the change stream will be left open, but will throw a MongoOperationTimeoutError + * when in iterator mode and emit an error event that returns a MongoOperationTimeoutError in + * emitter mode. + * + * To determine whether or not the change stream is still open following a timeout, check the + * {@link ChangeStream.closed} getter. + * + * @example + * In iterator mode, if a next() call throws a timeout error, it will attempt to resume the change stream. + * The next call can just be retried after this succeeds. + * ```ts + * const changeStream = collection.watch([], { timeoutMS: 100 }); + * try { + * await changeStream.next(); + * } catch (e) { + * if (e instanceof MongoOperationTimeoutError && !changeStream.closed) { + * await changeStream.next(); + * } + * throw e; + * } + * ``` + * + * @example + * In emitter mode, if the change stream goes `timeoutMS` without emitting a change event, it will + * emit an error event that returns a MongoOperationTimeoutError, but will not close the change + * stream unless the resume attempt fails. There is no need to re-establish change listeners as + * this will automatically continue emitting change events once the resume attempt completes. + * + * ```ts + * const changeStream = collection.watch([], { timeoutMS: 100 }); + * changeStream.on('change', console.log); + * changeStream.on('error', e => { + * if (e instanceof MongoOperationTimeoutError && !changeStream.closed) { + * // do nothing + * } else { + * changeStream.close(); + * } + * }); + * ``` + * + * @param pipeline - An array of {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents. + * @param options - Optional settings for the command + * @typeParam TLocal - Type of the data being detected by the change stream + * @typeParam TChange - Type of the whole change stream document emitted + */ + watch>(pipeline?: Document[], options?: ChangeStreamOptions): ChangeStream; + /** + * Initiate an Out of order batch write operation. All operations will be buffered into insert/update/remove commands executed out of order. + * + * @throws MongoNotConnectedError + * @remarks + * **NOTE:** MongoClient must be connected prior to calling this method due to a known limitation in this legacy implementation. + * However, `collection.bulkWrite()` provides an equivalent API that does not require prior connecting. + */ + initializeUnorderedBulkOp(options?: BulkWriteOptions): UnorderedBulkOperation; + /** + * Initiate an In order bulk write operation. Operations will be serially executed in the order they are added, creating a new operation for each switch in types. + * + * @throws MongoNotConnectedError + * @remarks + * **NOTE:** MongoClient must be connected prior to calling this method due to a known limitation in this legacy implementation. + * However, `collection.bulkWrite()` provides an equivalent API that does not require prior connecting. + */ + initializeOrderedBulkOp(options?: BulkWriteOptions): OrderedBulkOperation; + /** + * An estimated count of matching documents in the db to a filter. + * + * **NOTE:** This method has been deprecated, since it does not provide an accurate count of the documents + * in a collection. To obtain an accurate count of documents in the collection, use {@link Collection#countDocuments| countDocuments}. + * To obtain an estimated count of all documents in the collection, use {@link Collection#estimatedDocumentCount| estimatedDocumentCount}. + * + * @deprecated use {@link Collection#countDocuments| countDocuments} or {@link Collection#estimatedDocumentCount| estimatedDocumentCount} instead + * + * @param filter - The filter for the count. + * @param options - Optional settings for the command + */ + count(filter?: Filter, options?: CountOptions): Promise; + /** + * Returns all search indexes for the current collection. + * + * @param options - The options for the list indexes operation. + * + * @remarks Only available when used against a 7.0+ Atlas cluster. + */ + listSearchIndexes(options?: ListSearchIndexesOptions): ListSearchIndexesCursor; + /** + * Returns all search indexes for the current collection. + * + * @param name - The name of the index to search for. Only indexes with matching index names will be returned. + * @param options - The options for the list indexes operation. + * + * @remarks Only available when used against a 7.0+ Atlas cluster. + */ + listSearchIndexes(name: string, options?: ListSearchIndexesOptions): ListSearchIndexesCursor; + /** + * Creates a single search index for the collection. + * + * @param description - The index description for the new search index. + * @returns A promise that resolves to the name of the new search index. + * + * @remarks Only available when used against a 7.0+ Atlas cluster. + */ + createSearchIndex(description: SearchIndexDescription): Promise; + /** + * Creates multiple search indexes for the current collection. + * + * @param descriptions - An array of `SearchIndexDescription`s for the new search indexes. + * @returns A promise that resolves to an array of the newly created search index names. + * + * @remarks Only available when used against a 7.0+ Atlas cluster. + * @returns + */ + createSearchIndexes(descriptions: SearchIndexDescription[]): Promise; + /** + * Deletes a search index by index name. + * + * @param name - The name of the search index to be deleted. + * + * @remarks Only available when used against a 7.0+ Atlas cluster. + */ + dropSearchIndex(name: string): Promise; + /** + * Updates a search index by replacing the existing index definition with the provided definition. + * + * @param name - The name of the search index to update. + * @param definition - The new search index definition. + * + * @remarks Only available when used against a 7.0+ Atlas cluster. + */ + updateSearchIndex(name: string, definition: Document): Promise; +} + +/** @public */ +export declare interface CollectionInfo extends Document { + name: string; + type?: string; + options?: Document; + info?: { + readOnly?: false; + uuid?: Binary; + }; + idIndex?: Document; +} + +/** @public */ +export declare interface CollectionOptions extends BSONSerializeOptions, WriteConcernOptions { + /** Specify a read concern for the collection. (only MongoDB 3.2 or higher supported) */ + readConcern?: ReadConcernLike; + /** The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). */ + readPreference?: ReadPreferenceLike; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; +} + +/* Excluded from this release type: CollectionPrivate */ + +/* Excluded from this release type: COMMAND_FAILED */ + +/* Excluded from this release type: COMMAND_STARTED */ + +/* Excluded from this release type: COMMAND_SUCCEEDED */ + +/** + * An event indicating the failure of a given command + * @public + * @category Event + */ +export declare class CommandFailedEvent { + address: string; + /** Driver generated connection id */ + connectionId?: string | number; + /** + * Server generated connection id + * Distinct from the connection id and is returned by the hello or legacy hello response as "connectionId" from the server on 4.2+. + */ + serverConnectionId: bigint | null; + requestId: number; + duration: number; + commandName: string; + failure: Error; + serviceId?: ObjectId; + databaseName: string; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ + get hasServiceId(): boolean; +} + +/* Excluded from this release type: CommandOperation */ + +/** @public */ +export declare interface CommandOperationOptions extends OperationOptions, WriteConcernOptions, ExplainOptions { + /** Specify a read concern and level for the collection. (only MongoDB 3.2 or higher supported) */ + readConcern?: ReadConcernLike; + /** Collation */ + collation?: CollationOptions; + /** + * maxTimeMS is a server-side time limit in milliseconds for processing an operation. + */ + maxTimeMS?: number; + /** + * Comment to apply to the operation. + * + * In server versions pre-4.4, 'comment' must be string. A server + * error will be thrown if any other type is provided. + * + * In server versions 4.4 and above, 'comment' can be any valid BSON type. + */ + comment?: unknown; + dbName?: string; + authdb?: string; + /* Excluded from this release type: rawData */ +} + +/* Excluded from this release type: CommandOptions */ + +/** + * An event indicating the start of a given command + * @public + * @category Event + */ +export declare class CommandStartedEvent { + commandObj?: Document; + requestId: number; + databaseName: string; + commandName: string; + command: Document; + address: string; + /** Driver generated connection id */ + connectionId?: string | number; + /** + * Server generated connection id + * Distinct from the connection id and is returned by the hello or legacy hello response as "connectionId" + * from the server on 4.2+. + */ + serverConnectionId: bigint | null; + serviceId?: ObjectId; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ + get hasServiceId(): boolean; +} + +/** + * An event indicating the success of a given command + * @public + * @category Event + */ +export declare class CommandSucceededEvent { + address: string; + /** Driver generated connection id */ + connectionId?: string | number; + /** + * Server generated connection id + * Distinct from the connection id and is returned by the hello or legacy hello response as "connectionId" from the server on 4.2+. + */ + serverConnectionId: bigint | null; + requestId: number; + duration: number; + commandName: string; + reply: unknown; + serviceId?: ObjectId; + databaseName: string; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ + get hasServiceId(): boolean; +} + +/** @public */ +export declare type CommonEvents = 'newListener' | 'removeListener'; + +/** @public */ +export declare const Compressor: Readonly<{ + readonly none: 0; + readonly snappy: 1; + readonly zlib: 2; + readonly zstd: 3; +}>; + +/** @public */ +export declare type Compressor = (typeof Compressor)[CompressorName]; + +/** @public */ +export declare type CompressorName = keyof typeof Compressor; + +/** @public */ +export declare type Condition = AlternativeType | FilterOperators>; + +/* Excluded from this release type: Connection */ + +/* Excluded from this release type: CONNECTION_CHECK_OUT_FAILED */ + +/* Excluded from this release type: CONNECTION_CHECK_OUT_STARTED */ + +/* Excluded from this release type: CONNECTION_CHECKED_IN */ + +/* Excluded from this release type: CONNECTION_CHECKED_OUT */ + +/* Excluded from this release type: CONNECTION_CLOSED */ + +/* Excluded from this release type: CONNECTION_CREATED */ + +/* Excluded from this release type: CONNECTION_POOL_CLEARED */ + +/* Excluded from this release type: CONNECTION_POOL_CLOSED */ + +/* Excluded from this release type: CONNECTION_POOL_CREATED */ + +/* Excluded from this release type: CONNECTION_POOL_READY */ + +/* Excluded from this release type: CONNECTION_READY */ + +/** + * An event published when a connection is checked into the connection pool + * @public + * @category Event + */ +export declare class ConnectionCheckedInEvent extends ConnectionPoolMonitoringEvent { + /** The id of the connection */ + connectionId: number | ''; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** + * An event published when a connection is checked out of the connection pool + * @public + * @category Event + */ +export declare class ConnectionCheckedOutEvent extends ConnectionPoolMonitoringEvent { + /** The id of the connection */ + connectionId: number | ''; + /* Excluded from this release type: name */ + /** + * The time it took to check out the connection. + * More specifically, the time elapsed between + * emitting a `ConnectionCheckOutStartedEvent` + * and emitting this event as part of the same checking out. + * + */ + durationMS: number; + /* Excluded from this release type: __constructor */ +} + +/** + * An event published when a request to check a connection out fails + * @public + * @category Event + */ +export declare class ConnectionCheckOutFailedEvent extends ConnectionPoolMonitoringEvent { + /** The reason the attempt to check out failed */ + reason: string; + /* Excluded from this release type: error */ + /* Excluded from this release type: name */ + /** + * The time it took to check out the connection. + * More specifically, the time elapsed between + * emitting a `ConnectionCheckOutStartedEvent` + * and emitting this event as part of the same check out. + */ + durationMS: number; + /* Excluded from this release type: __constructor */ +} + +/** + * An event published when a request to check a connection out begins + * @public + * @category Event + */ +export declare class ConnectionCheckOutStartedEvent extends ConnectionPoolMonitoringEvent { + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** + * An event published when a connection is closed + * @public + * @category Event + */ +export declare class ConnectionClosedEvent extends ConnectionPoolMonitoringEvent { + /** The id of the connection */ + connectionId: number | ''; + /** The reason the connection was closed */ + reason: string; + serviceId?: ObjectId; + /* Excluded from this release type: name */ + /* Excluded from this release type: error */ + /* Excluded from this release type: __constructor */ +} + +/** + * An event published when a connection pool creates a new connection + * @public + * @category Event + */ +export declare class ConnectionCreatedEvent extends ConnectionPoolMonitoringEvent { + /** A monotonically increasing, per-pool id for the newly created connection */ + connectionId: number | ''; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** @public */ +export declare type ConnectionEvents = { + commandStarted(event: CommandStartedEvent): void; + commandSucceeded(event: CommandSucceededEvent): void; + commandFailed(event: CommandFailedEvent): void; + clusterTimeReceived(clusterTime: Document): void; + close(): void; + pinned(pinType: string): void; + unpinned(pinType: string): void; +}; + +/** @public */ +export declare interface ConnectionOptions extends SupportedNodeConnectionOptions, StreamDescriptionOptions, ProxyOptions { + id: number | ''; + generation: number; + hostAddress: HostAddress; + /* Excluded from this release type: autoEncrypter */ + serverApi?: ServerApi; + monitorCommands: boolean; + /* Excluded from this release type: connectionType */ + credentials?: MongoCredentials; + /* Excluded from this release type: authProviders */ + connectTimeoutMS?: number; + tls: boolean; + noDelay?: boolean; + socketTimeoutMS?: number; + /* Excluded from this release type: cancellationToken */ + /* Excluded from this release type: metadata */ + /* Excluded from this release type: mongoLogger */ + /* Excluded from this release type: runtime */ +} + +/* Excluded from this release type: ConnectionPool */ + +/** + * An event published when a connection pool is cleared + * @public + * @category Event + */ +export declare class ConnectionPoolClearedEvent extends ConnectionPoolMonitoringEvent { + /* Excluded from this release type: serviceId */ + interruptInUseConnections?: boolean; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** + * An event published when a connection pool is closed + * @public + * @category Event + */ +export declare class ConnectionPoolClosedEvent extends ConnectionPoolMonitoringEvent { + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** + * An event published when a connection pool is created + * @public + * @category Event + */ +export declare class ConnectionPoolCreatedEvent extends ConnectionPoolMonitoringEvent { + /** The options used to create this connection pool */ + options: Pick; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** @public */ +export declare type ConnectionPoolEvents = { + connectionPoolCreated(event: ConnectionPoolCreatedEvent): void; + connectionPoolReady(event: ConnectionPoolReadyEvent): void; + connectionPoolClosed(event: ConnectionPoolClosedEvent): void; + connectionPoolCleared(event: ConnectionPoolClearedEvent): void; + connectionCreated(event: ConnectionCreatedEvent): void; + connectionReady(event: ConnectionReadyEvent): void; + connectionClosed(event: ConnectionClosedEvent): void; + connectionCheckOutStarted(event: ConnectionCheckOutStartedEvent): void; + connectionCheckOutFailed(event: ConnectionCheckOutFailedEvent): void; + connectionCheckedOut(event: ConnectionCheckedOutEvent): void; + connectionCheckedIn(event: ConnectionCheckedInEvent): void; +} & Omit; + +/* Excluded from this release type: ConnectionPoolMetrics */ + +/** + * The base export class for all monitoring events published from the connection pool + * @public + * @category Event + */ +export declare abstract class ConnectionPoolMonitoringEvent { + /** A timestamp when the event was created */ + time: Date; + /** The address (host/port pair) of the pool */ + address: string; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** @public */ +export declare interface ConnectionPoolOptions extends Omit { + /** The maximum number of connections that may be associated with a pool at a given time. This includes in use and available connections. */ + maxPoolSize: number; + /** The minimum number of connections that MUST exist at any moment in a single connection pool. */ + minPoolSize: number; + /** The maximum number of connections that may be in the process of being established concurrently by the connection pool. */ + maxConnecting: number; + /** The maximum amount of time a connection should remain idle in the connection pool before being marked idle. */ + maxIdleTimeMS: number; + /** The maximum amount of time operation execution should wait for a connection to become available. The default is 0 which means there is no limit. */ + waitQueueTimeoutMS: number; + /** If we are in load balancer mode. */ + loadBalanced: boolean; + /* Excluded from this release type: minPoolSizeCheckFrequencyMS */ +} + +/** + * An event published when a connection pool is ready + * @public + * @category Event + */ +export declare class ConnectionPoolReadyEvent extends ConnectionPoolMonitoringEvent { + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** + * An event published when a connection is ready for use + * @public + * @category Event + */ +export declare class ConnectionReadyEvent extends ConnectionPoolMonitoringEvent { + /** The id of the connection */ + connectionId: number | ''; + /** + * The time it took to establish the connection. + * In accordance with the definition of establishment of a connection + * specified by `ConnectionPoolOptions.maxConnecting`, + * it is the time elapsed between emitting a `ConnectionCreatedEvent` + * and emitting this event as part of the same checking out. + * + * Naturally, when establishing a connection is part of checking out, + * this duration is not greater than + * `ConnectionCheckedOutEvent.duration`. + */ + durationMS: number; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** @public */ +export declare interface ConnectOptions { + readPreference?: ReadPreference; +} + +/** @public */ +export declare interface CountDocumentsOptions extends AggregateOptions { + /** The number of documents to skip. */ + skip?: number; + /** The maximum amount of documents to consider. */ + limit?: number; +} + +/** @public */ +export declare interface CountOptions extends CommandOperationOptions { + /** The number of documents to skip. */ + skip?: number; + /** The maximum amounts to count before aborting. */ + limit?: number; + /** + * Number of milliseconds to wait before aborting the query. + */ + maxTimeMS?: number; + /** An index name hint for the query. */ + hint?: string | Document; +} + +/** @public */ +export declare interface CreateCollectionOptions extends Omit { + /** Create a capped collection */ + capped?: boolean; + /** The size of the capped collection in bytes */ + size?: number; + /** The maximum number of documents in the capped collection */ + max?: number; + /** Available for the MMAPv1 storage engine only to set the usePowerOf2Sizes and the noPadding flag */ + flags?: number; + /** Allows users to specify configuration to the storage engine on a per-collection basis when creating a collection */ + storageEngine?: Document; + /** Allows users to specify validation rules or expressions for the collection. For more information, see Document Validation */ + validator?: Document; + /** Determines how strictly MongoDB applies the validation rules to existing documents during an update */ + validationLevel?: string; + /** Determines whether to error on invalid documents or just warn about the violations but allow invalid documents to be inserted */ + validationAction?: string; + /** Allows users to specify a default configuration for indexes when creating a collection */ + indexOptionDefaults?: Document; + /** The name of the source collection or view from which to create the view. The name is not the full namespace of the collection or view (i.e., does not include the database name and implies the same database as the view to create) */ + viewOn?: string; + /** An array that consists of the aggregation pipeline stage. Creates the view by applying the specified pipeline to the viewOn collection or view */ + pipeline?: Document[]; + /** A primary key factory function for generation of custom _id keys. */ + pkFactory?: PkFactory; + /** A document specifying configuration options for timeseries collections. */ + timeseries?: TimeSeriesCollectionOptions; + /** A document specifying configuration options for clustered collections. For MongoDB 5.3 and above. */ + clusteredIndex?: ClusteredCollectionOptions; + /** The number of seconds after which a document in a timeseries or clustered collection expires. */ + expireAfterSeconds?: number; + /** @experimental */ + encryptedFields?: Document; + /** + * If set, enables pre-update and post-update document events to be included for any + * change streams that listen on this collection. + */ + changeStreamPreAndPostImages?: { + enabled: boolean; + }; +} + +/** @public */ +export declare interface CreateIndexesOptions extends Omit { + /** Creates the index in the background, yielding whenever possible. */ + background?: boolean; + /** Creates an unique index. */ + unique?: boolean; + /** Override the autogenerated index name (useful if the resulting name is larger than 128 bytes) */ + name?: string; + /** Creates a partial index based on the given filter object (MongoDB 3.2 or higher) */ + partialFilterExpression?: Document; + /** Creates a sparse index. */ + sparse?: boolean; + /** Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher) */ + expireAfterSeconds?: number; + /** Allows users to configure the storage engine on a per-index basis when creating an index. (MongoDB 3.0 or higher) */ + storageEngine?: Document; + /** (MongoDB 4.4. or higher) Specifies how many data-bearing members of a replica set, including the primary, must complete the index builds successfully before the primary marks the indexes as ready. This option accepts the same values for the "w" field in a write concern plus "votingMembers", which indicates all voting data-bearing nodes. */ + commitQuorum?: number | string; + /** Specifies the index version number, either 0 or 1. */ + version?: number; + weights?: Document; + default_language?: string; + language_override?: string; + textIndexVersion?: number; + '2dsphereIndexVersion'?: number; + bits?: number; + /** For geospatial indexes set the lower bound for the co-ordinates. */ + min?: number; + /** For geospatial indexes set the high bound for the co-ordinates. */ + max?: number; + bucketSize?: number; + wildcardProjection?: Document; + /** Specifies that the index should exist on the target collection but should not be used by the query planner when executing operations. (MongoDB 4.4 or higher) */ + hidden?: boolean; +} + +/** + * @public + * Configuration options for custom credential providers for KMS requests. + */ +export declare interface CredentialProviders { + aws?: AWSCredentialProvider; +} + +/** @public */ +export declare type CSFLEKMSTlsOptions = { + aws?: ClientEncryptionTlsOptions; + gcp?: ClientEncryptionTlsOptions; + kmip?: ClientEncryptionTlsOptions; + local?: ClientEncryptionTlsOptions; + azure?: ClientEncryptionTlsOptions; + [key: string]: ClientEncryptionTlsOptions | undefined; +}; + +/* Excluded from this release type: CSOTTimeoutContext */ + +/* Excluded from this release type: CSOTTimeoutContextOptions */ + +/** @public */ +export declare const CURSOR_FLAGS: readonly ["tailable", "oplogReplay", "noCursorTimeout", "awaitData", "exhaust", "partial"]; + +/** @public */ +export declare type CursorFlag = (typeof CURSOR_FLAGS)[number]; + +/* Excluded from this release type: CursorResponse */ + +/* Excluded from this release type: CursorTimeoutContext */ + +/** + * @public + * @experimental + * Specifies how `timeoutMS` is applied to the cursor. Can be either `'cursorLifeTime'` or `'iteration'` + * When set to `'iteration'`, the deadline specified by `timeoutMS` applies to each call of + * `cursor.next()`. + * When set to `'cursorLifetime'`, the deadline applies to the life of the entire cursor. + * + * Depending on the type of cursor being used, this option has different default values. + * For non-tailable cursors, this value defaults to `'cursorLifetime'` + * For tailable cursors, this value defaults to `'iteration'` since tailable cursors, by + * definition can have an arbitrarily long lifetime. + * + * @example + * ```ts + * const cursor = collection.find({}, {timeoutMS: 100, timeoutMode: 'iteration'}); + * for await (const doc of cursor) { + * // process doc + * // This will throw a timeout error if any of the iterator's `next()` calls takes more than 100ms, but + * // will continue to iterate successfully otherwise, regardless of the number of batches. + * } + * ``` + * + * @example + * ```ts + * const cursor = collection.find({}, { timeoutMS: 1000, timeoutMode: 'cursorLifetime' }); + * const docs = await cursor.toArray(); // This entire line will throw a timeout error if all batches are not fetched and returned within 1000ms. + * ``` + */ +export declare const CursorTimeoutMode: Readonly<{ + readonly ITERATION: "iteration"; + readonly LIFETIME: "cursorLifetime"; +}>; + +/** + * @public + * @experimental + */ +export declare type CursorTimeoutMode = (typeof CursorTimeoutMode)[keyof typeof CursorTimeoutMode]; + +/** + * @public + * The schema for a DataKey in the key vault collection. + */ +export declare interface DataKey { + _id: UUID; + version?: number; + keyAltNames?: string[]; + keyMaterial: Binary; + creationDate: Date; + updateDate: Date; + status: number; + masterKey: Document; +} + +/** + * The **Db** class is a class that represents a MongoDB Database. + * @public + * + * @example + * ```ts + * import { MongoClient } from 'mongodb'; + * + * interface Pet { + * name: string; + * kind: 'dog' | 'cat' | 'fish'; + * } + * + * const client = new MongoClient('mongodb://localhost:27017'); + * const db = client.db(); + * + * // Create a collection that validates our union + * await db.createCollection('pets', { + * validator: { $expr: { $in: ['$kind', ['dog', 'cat', 'fish']] } } + * }) + * ``` + */ +export declare class Db { + /* Excluded from this release type: s */ + /** + * Gets the MongoClient associated with the Db. + * @public + */ + readonly client: MongoClient; + static SYSTEM_NAMESPACE_COLLECTION: string; + static SYSTEM_INDEX_COLLECTION: string; + static SYSTEM_PROFILE_COLLECTION: string; + static SYSTEM_USER_COLLECTION: string; + static SYSTEM_COMMAND_COLLECTION: string; + static SYSTEM_JS_COLLECTION: string; + /** + * Creates a new Db instance. + * + * Db name cannot contain a dot, the server may apply more restrictions when an operation is run. + * + * @param client - The MongoClient for the database. + * @param databaseName - The name of the database this instance represents. + * @param options - Optional settings for Db construction. + */ + constructor(client: MongoClient, databaseName: string, options?: DbOptions); + get databaseName(): string; + get options(): DbOptions | undefined; + /** + * Check if a secondary can be used (because the read preference is *not* set to primary) + */ + get secondaryOk(): boolean; + get readConcern(): ReadConcern | undefined; + /** + * The current readPreference of the Db. If not explicitly defined for + * this Db, will be inherited from the parent MongoClient + */ + get readPreference(): ReadPreference; + get bsonOptions(): BSONSerializeOptions; + get writeConcern(): WriteConcern | undefined; + get namespace(): string; + get timeoutMS(): number | undefined; + /** + * Create a new collection on a server with the specified options. Use this to create capped collections. + * More information about command options available at https://www.mongodb.com/docs/manual/reference/command/create/ + * + * Collection namespace validation is performed server-side. + * + * @param name - The name of the collection to create + * @param options - Optional settings for the command + */ + createCollection(name: string, options?: CreateCollectionOptions): Promise>; + /** + * Execute a command + * + * @remarks + * This command does not inherit options from the MongoClient. + * + * The driver will ensure the following fields are attached to the command sent to the server: + * - `lsid` - sourced from an implicit session or options.session + * - `$readPreference` - defaults to primary or can be configured by options.readPreference + * - `$db` - sourced from the name of this database + * + * If the client has a serverApi setting: + * - `apiVersion` + * - `apiStrict` + * - `apiDeprecationErrors` + * + * When in a transaction: + * - `readConcern` - sourced from readConcern set on the TransactionOptions + * - `writeConcern` - sourced from writeConcern set on the TransactionOptions + * + * Attaching any of the above fields to the command will have no effect as the driver will overwrite the value. + * + * @param command - The command to run + * @param options - Optional settings for the command + */ + command(command: Document, options?: RunCommandOptions & Abortable): Promise; + /** + * Execute an aggregation framework pipeline against the database. + * + * @param pipeline - An array of aggregation stages to be executed + * @param options - Optional settings for the command + */ + aggregate(pipeline?: Document[], options?: AggregateOptions): AggregationCursor; + /** Return the Admin db instance */ + admin(): Admin; + /** + * Returns a reference to a MongoDB Collection. If it does not exist it will be created implicitly. + * + * Collection namespace validation is performed server-side. + * + * @param name - the collection name we wish to access. + * @returns return the new Collection instance + */ + collection(name: string, options?: CollectionOptions): Collection; + /** + * Get all the db statistics. + * + * @param options - Optional settings for the command + */ + stats(options?: DbStatsOptions): Promise; + /** + * List all collections of this database with optional filter + * + * @param filter - Query to filter collections by + * @param options - Optional settings for the command + */ + listCollections(filter: Document, options: Exclude & { + nameOnly: true; + } & Abortable): ListCollectionsCursor>; + listCollections(filter: Document, options: Exclude & { + nameOnly: false; + } & Abortable): ListCollectionsCursor; + listCollections | CollectionInfo = Pick | CollectionInfo>(filter?: Document, options?: ListCollectionsOptions & Abortable): ListCollectionsCursor; + /** + * Rename a collection. + * + * @remarks + * This operation does not inherit options from the MongoClient. + * + * @param fromCollection - Name of current collection to rename + * @param toCollection - New name of of the collection + * @param options - Optional settings for the command + */ + renameCollection(fromCollection: string, toCollection: string, options?: RenameOptions): Promise>; + /** + * Drop a collection from the database, removing it permanently. New accesses will create a new collection. + * + * @param name - Name of collection to drop + * @param options - Optional settings for the command + */ + dropCollection(name: string, options?: DropCollectionOptions): Promise; + /** + * Drop a database, removing it permanently from the server. + * + * @param options - Optional settings for the command + */ + dropDatabase(options?: DropDatabaseOptions): Promise; + /** + * Fetch all collections for the current db. + * + * @param options - Optional settings for the command + */ + collections(options?: ListCollectionsOptions): Promise; + /** + * Creates an index on the db and collection. + * + * @param name - Name of the collection to create the index on. + * @param indexSpec - Specify the field to index, or an index specification + * @param options - Optional settings for the command + */ + createIndex(name: string, indexSpec: IndexSpecification, options?: CreateIndexesOptions): Promise; + /** + * Remove a user from a database + * + * @param username - The username to remove + * @param options - Optional settings for the command + */ + removeUser(username: string, options?: RemoveUserOptions): Promise; + /** + * Set the current profiling level of MongoDB + * + * @param level - The new profiling level (off, slow_only, all). + * @param options - Optional settings for the command + */ + setProfilingLevel(level: ProfilingLevel, options?: SetProfilingLevelOptions): Promise; + /** + * Retrieve the current profiling Level for MongoDB + * + * @param options - Optional settings for the command + */ + profilingLevel(options?: ProfilingLevelOptions): Promise; + /** + * Retrieves this collections index info. + * + * @param name - The name of the collection. + * @param options - Optional settings for the command + */ + indexInformation(name: string, options: IndexInformationOptions & { + full: true; + }): Promise; + indexInformation(name: string, options: IndexInformationOptions & { + full?: false; + }): Promise; + indexInformation(name: string, options: IndexInformationOptions): Promise; + indexInformation(name: string): Promise; + /** + * Create a new Change Stream, watching for new changes (insertions, updates, + * replacements, deletions, and invalidations) in this database. Will ignore all + * changes to system collections. + * + * @remarks + * watch() accepts two generic arguments for distinct use cases: + * - The first is to provide the schema that may be defined for all the collections within this database + * - The second is to override the shape of the change stream document entirely, if it is not provided the type will default to ChangeStreamDocument of the first argument + * + * @remarks + * When `timeoutMS` is configured for a change stream, it will have different behaviour depending + * on whether the change stream is in iterator mode or emitter mode. In both cases, a change + * stream will time out if it does not receive a change event within `timeoutMS` of the last change + * event. + * + * Note that if a change stream is consistently timing out when watching a collection, database or + * client that is being changed, then this may be due to the server timing out before it can finish + * processing the existing oplog. To address this, restart the change stream with a higher + * `timeoutMS`. + * + * If the change stream times out the initial aggregate operation to establish the change stream on + * the server, then the client will close the change stream. If the getMore calls to the server + * time out, then the change stream will be left open, but will throw a MongoOperationTimeoutError + * when in iterator mode and emit an error event that returns a MongoOperationTimeoutError in + * emitter mode. + * + * To determine whether or not the change stream is still open following a timeout, check the + * {@link ChangeStream.closed} getter. + * + * @example + * In iterator mode, if a next() call throws a timeout error, it will attempt to resume the change stream. + * The next call can just be retried after this succeeds. + * ```ts + * const changeStream = collection.watch([], { timeoutMS: 100 }); + * try { + * await changeStream.next(); + * } catch (e) { + * if (e instanceof MongoOperationTimeoutError && !changeStream.closed) { + * await changeStream.next(); + * } + * throw e; + * } + * ``` + * + * @example + * In emitter mode, if the change stream goes `timeoutMS` without emitting a change event, it will + * emit an error event that returns a MongoOperationTimeoutError, but will not close the change + * stream unless the resume attempt fails. There is no need to re-establish change listeners as + * this will automatically continue emitting change events once the resume attempt completes. + * + * ```ts + * const changeStream = collection.watch([], { timeoutMS: 100 }); + * changeStream.on('change', console.log); + * changeStream.on('error', e => { + * if (e instanceof MongoOperationTimeoutError && !changeStream.closed) { + * // do nothing + * } else { + * changeStream.close(); + * } + * }); + * ``` + * @param pipeline - An array of {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents. + * @param options - Optional settings for the command + * @typeParam TSchema - Type of the data being detected by the change stream + * @typeParam TChange - Type of the whole change stream document emitted + */ + watch>(pipeline?: Document[], options?: ChangeStreamOptions): ChangeStream; + /** + * A low level cursor API providing basic driver functionality: + * - ClientSession management + * - ReadPreference for server selection + * - Running getMores automatically when a local batch is exhausted + * + * @param command - The command that will start a cursor on the server. + * @param options - Configurations for running the command, bson options will apply to getMores + */ + runCursorCommand(command: Document, options?: RunCursorCommandOptions): RunCommandCursor; +} + +/* Excluded from this release type: DB_AGGREGATE_COLLECTION */ + +/** @public */ +export declare interface DbOptions extends BSONSerializeOptions, WriteConcernOptions { + /** If the database authentication is dependent on another databaseName. */ + authSource?: string; + /** Force server to assign _id values instead of driver. */ + forceServerObjectId?: boolean; + /** The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). */ + readPreference?: ReadPreferenceLike; + /** A primary key factory object for generation of custom _id keys. */ + pkFactory?: PkFactory; + /** Specify a read concern for the collection. (only MongoDB 3.2 or higher supported) */ + readConcern?: ReadConcern; + /** Should retry failed writes */ + retryWrites?: boolean; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; +} + +/* Excluded from this release type: DbPrivate */ +export { DBRef } + +/** @public */ +export declare interface DbStatsOptions extends Omit { + /** Divide the returned sizes by scale value. */ + scale?: number; +} + +export { Decimal128 } + +/** @public */ +export declare interface DeleteManyModel { + /** The filter to limit the deleted documents. */ + filter: Filter; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; +} + +/** @public */ +export declare interface DeleteOneModel { + /** The filter to limit the deleted documents. */ + filter: Filter; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; +} + +/** @public */ +export declare interface DeleteOptions extends CommandOperationOptions, WriteConcernOptions { + /** If true, when an insert fails, don't execute the remaining writes. If false, continue with remaining inserts when one fails. */ + ordered?: boolean; + /** Specifies the collation to use for the operation */ + collation?: CollationOptions; + /** Specify that the update query should only consider plans using the hinted index */ + hint?: string | Document; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; +} + +/** @public */ +export declare interface DeleteResult { + /** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined. */ + acknowledged: boolean; + /** The number of documents that were deleted */ + deletedCount: number; +} + +/** @public */ +export declare interface DeleteStatement { + /** The query that matches documents to delete. */ + q: Document; + /** The number of matching documents to delete. */ + limit: number; + /** Specifies the collation to use for the operation. */ + collation?: CollationOptions; + /** A document or string that specifies the index to use to support the query predicate. */ + hint?: Hint; +} + +/* Excluded from this release type: DeprioritizedServers */ +export { deserialize } + +/** @public */ +export declare type DistinctOptions = CommandOperationOptions & { + /** + * @sinceServerVersion 7.1 + * + * The index to use. Specify either the index name as a string or the index key pattern. + * If specified, then the query system will only consider plans using the hinted index. + * + * If provided as a string, `hint` must be index name for an index on the collection. + * If provided as an object, `hint` must be an index description for an index defined on the collection. + * + * See https://www.mongodb.com/docs/manual/reference/command/distinct/#command-fields. + */ + hint?: Document | string; +}; + +export { Document } + +export { Double } + +/** @public */ +export declare interface DriverInfo { + name?: string; + version?: string; + platform?: string; +} + +/** @public */ +export declare interface DropCollectionOptions extends Omit { + /** @experimental */ + encryptedFields?: Document; +} + +/** @public */ +export declare type DropDatabaseOptions = CommandOperationOptions; + +/** @public */ +export declare type DropIndexesOptions = CommandOperationOptions; + +/* Excluded from this release type: Encrypter */ + +/* Excluded from this release type: EncrypterOptions */ + +/** @public */ +export declare interface EndSessionOptions { + /* Excluded from this release type: error */ + force?: boolean; + forceClear?: boolean; + /** Specifies the time an operation will run until it throws a timeout error */ + timeoutMS?: number; +} + +/** TypeScript Omit (Exclude to be specific) does not work for objects with an "any" indexed type, and breaks discriminated unions @public */ +export declare type EnhancedOmit = string extends keyof TRecordOrUnion ? TRecordOrUnion : TRecordOrUnion extends any ? Pick> : never; + +/** @public */ +export declare interface ErrorDescription extends Document { + message?: string; + errmsg?: string; + $err?: string; + errorLabels?: string[]; + errInfo?: Document; +} + +/** @public */ +export declare interface EstimatedDocumentCountOptions extends CommandOperationOptions { + /** + * The maximum amount of time to allow the operation to run. + * + * This option is sent only if the caller explicitly provides a value. The default is to not send a value. + */ + maxTimeMS?: number; +} + +/** @public */ +export declare type EventEmitterWithState = { + /* Excluded from this release type: stateChanged */ +}; + +/** + * Event description type + * @public + */ +export declare type EventsDescription = Record; + +/* Excluded from this release type: Explain */ + +/** + * @public + * + * A base class for any cursors that have `explain()` methods. + */ +export declare abstract class ExplainableCursor extends AbstractCursor { + /** Execute the explain for the cursor */ + abstract explain(): Promise; + abstract explain(verbosity: ExplainVerbosityLike | ExplainCommandOptions): Promise; + abstract explain(options: { + timeoutMS?: number; + }): Promise; + abstract explain(verbosity: ExplainVerbosityLike | ExplainCommandOptions, options: { + timeoutMS?: number; + }): Promise; + abstract explain(verbosity?: ExplainVerbosityLike | ExplainCommandOptions | { + timeoutMS?: number; + }, options?: { + timeoutMS?: number; + }): Promise; + protected resolveExplainTimeoutOptions(verbosity?: ExplainVerbosityLike | ExplainCommandOptions | { + timeoutMS?: number; + }, options?: { + timeoutMS?: number; + }): { + timeout?: { + timeoutMS?: number; + }; + explain?: ExplainVerbosityLike | ExplainCommandOptions; + }; +} + +/** @public */ +export declare interface ExplainCommandOptions { + /** The explain verbosity for the command. */ + verbosity: ExplainVerbosity; + /** The maxTimeMS setting for the command. */ + maxTimeMS?: number; +} + +/** + * @public + * + * When set, this configures an explain command. Valid values are boolean (for legacy compatibility, + * see {@link ExplainVerbosityLike}), a string containing the explain verbosity, or an object containing the verbosity and + * an optional maxTimeMS. + * + * Examples of valid usage: + * + * ```typescript + * collection.find({ name: 'john doe' }, { explain: true }); + * collection.find({ name: 'john doe' }, { explain: false }); + * collection.find({ name: 'john doe' }, { explain: 'queryPlanner' }); + * collection.find({ name: 'john doe' }, { explain: { verbosity: 'queryPlanner' } }); + * ``` + * + * maxTimeMS can be configured to limit the amount of time the server + * spends executing an explain by providing an object: + * + * ```typescript + * // limits the `explain` command to no more than 2 seconds + * collection.find({ name: 'john doe' }, { + * explain: { + * verbosity: 'queryPlanner', + * maxTimeMS: 2000 + * } + * }); + * ``` + */ +export declare interface ExplainOptions { + /** Specifies the verbosity mode for the explain output. */ + explain?: ExplainVerbosityLike | ExplainCommandOptions; +} + +/** @public */ +export declare const ExplainVerbosity: Readonly<{ + readonly queryPlanner: "queryPlanner"; + readonly queryPlannerExtended: "queryPlannerExtended"; + readonly executionStats: "executionStats"; + readonly allPlansExecution: "allPlansExecution"; +}>; + +/** @public */ +export declare type ExplainVerbosity = string; + +/** + * For backwards compatibility, true is interpreted as "allPlansExecution" + * and false as "queryPlanner". + * @public + */ +export declare type ExplainVerbosityLike = ExplainVerbosity | boolean; + +/** A MongoDB filter can be some portion of the schema or a set of operators @public */ +export declare type Filter = { + [P in keyof WithId]?: Condition[P]>; +} & RootFilterOperators>; + +/** @public */ +export declare type FilterOperations = T extends Record ? { + [key in keyof T]?: FilterOperators; +} : FilterOperators; + +/** @public */ +export declare interface FilterOperators extends NonObjectIdLikeDocument { + $eq?: TValue; + $gt?: TValue; + $gte?: TValue; + $in?: ReadonlyArray; + $lt?: TValue; + $lte?: TValue; + $ne?: TValue; + $nin?: ReadonlyArray; + $not?: TValue extends string ? FilterOperators | RegExp : FilterOperators; + /** + * When `true`, `$exists` matches the documents that contain the field, + * including documents where the field value is null. + */ + $exists?: boolean; + $type?: BSONType | BSONTypeAlias; + $expr?: Record; + $jsonSchema?: Record; + $mod?: TValue extends number ? [number, number] : never; + $regex?: TValue extends string ? RegExp | BSONRegExp | string : never; + $options?: TValue extends string ? string : never; + $geoIntersects?: { + $geometry: Document; + }; + $geoWithin?: Document; + $near?: Document; + $nearSphere?: Document; + $maxDistance?: number; + $all?: ReadonlyArray; + $elemMatch?: Document; + $size?: TValue extends ReadonlyArray ? number : never; + $bitsAllClear?: BitwiseFilter; + $bitsAllSet?: BitwiseFilter; + $bitsAnyClear?: BitwiseFilter; + $bitsAnySet?: BitwiseFilter; + $rand?: Record; +} + +/** @public */ +export declare class FindCursor extends ExplainableCursor { + /* Excluded from this release type: cursorFilter */ + /* Excluded from this release type: numReturned */ + /* Excluded from this release type: findOptions */ + /* Excluded from this release type: __constructor */ + clone(): FindCursor; + map(transform: (doc: TSchema) => T): FindCursor; + /* Excluded from this release type: _initialize */ + /* Excluded from this release type: getMore */ + /** + * Get the count of documents for this cursor + * @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead + */ + count(options?: CountOptions): Promise; + /** Execute the explain for the cursor */ + explain(): Promise; + explain(verbosity: ExplainVerbosityLike | ExplainCommandOptions): Promise; + explain(options: { + timeoutMS?: number; + }): Promise; + explain(verbosity: ExplainVerbosityLike | ExplainCommandOptions, options: { + timeoutMS?: number; + }): Promise; + /** Set the cursor query */ + filter(filter: Document): this; + /** + * Set the cursor hint + * + * @param hint - If specified, then the query system will only consider plans using the hinted index. + */ + hint(hint: Hint): this; + /** + * Set the cursor min + * + * @param min - Specify a $min value to specify the inclusive lower bound for a specific index in order to constrain the results of find(). The $min specifies the lower bound for all keys of a specific index in order. + */ + min(min: Document): this; + /** + * Set the cursor max + * + * @param max - Specify a $max value to specify the exclusive upper bound for a specific index in order to constrain the results of find(). The $max specifies the upper bound for all keys of a specific index in order. + */ + max(max: Document): this; + /** + * Set the cursor returnKey. + * If set to true, modifies the cursor to only return the index field or fields for the results of the query, rather than documents. + * If set to true and the query does not use an index to perform the read operation, the returned documents will not contain any fields. + * + * @param value - the returnKey value. + */ + returnKey(value: boolean): this; + /** + * Modifies the output of a query by adding a field $recordId to matching documents. $recordId is the internal key which uniquely identifies a document in a collection. + * + * @param value - The $showDiskLoc option has now been deprecated and replaced with the showRecordId field. $showDiskLoc will still be accepted for OP_QUERY stye find. + */ + showRecordId(value: boolean): this; + /** + * Add a query modifier to the cursor query + * + * @param name - The query modifier (must start with $, such as $orderby etc) + * @param value - The modifier value. + */ + addQueryModifier(name: string, value: string | boolean | number | Document): this; + /** + * Add a comment to the cursor query allowing for tracking the comment in the log. + * + * @param value - The comment attached to this query. + */ + comment(value: string): this; + /** + * Set a maxAwaitTimeMS on a tailing cursor query to allow to customize the timeout value for the option awaitData (Only supported on MongoDB 3.2 or higher, ignored otherwise) + * + * @param value - Number of milliseconds to wait before aborting the tailed query. + */ + maxAwaitTimeMS(value: number): this; + /** + * Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher) + * + * @param value - Number of milliseconds to wait before aborting the query. + */ + maxTimeMS(value: number): this; + /** + * Add a project stage to the aggregation pipeline + * + * @remarks + * In order to strictly type this function you must provide an interface + * that represents the effect of your projection on the result documents. + * + * By default chaining a projection to your cursor changes the returned type to the generic + * {@link Document} type. + * You should specify a parameterized type to have assertions on your final results. + * + * @example + * ```typescript + * // Best way + * const docs: FindCursor<{ a: number }> = cursor.project<{ a: number }>({ _id: 0, a: true }); + * // Flexible way + * const docs: FindCursor = cursor.project({ _id: 0, a: true }); + * ``` + * + * @remarks + * + * **Note for Typescript Users:** adding a transform changes the return type of the iteration of this cursor, + * it **does not** return a new instance of a cursor. This means when calling project, + * you should always assign the result to a new variable in order to get a correctly typed cursor variable. + * Take note of the following example: + * + * @example + * ```typescript + * const cursor: FindCursor<{ a: number; b: string }> = coll.find(); + * const projectCursor = cursor.project<{ a: number }>({ _id: 0, a: true }); + * const aPropOnlyArray: {a: number}[] = await projectCursor.toArray(); + * + * // or always use chaining and save the final cursor + * + * const cursor = coll.find().project<{ a: string }>({ + * _id: 0, + * a: { $convert: { input: '$a', to: 'string' } + * }}); + * ``` + */ + project(value: Document): FindCursor; + /** + * Sets the sort order of the cursor query. + * + * @param sort - The key or keys set for the sort. + * @param direction - The direction of the sorting (1 or -1). + */ + sort(sort: Sort | string, direction?: SortDirection): this; + /** + * Allows disk use for blocking sort operations exceeding 100MB memory. (MongoDB 3.2 or higher) + * + * @remarks + * {@link https://www.mongodb.com/docs/manual/reference/command/find/#find-cmd-allowdiskuse | find command allowDiskUse documentation} + */ + allowDiskUse(allow?: boolean): this; + /** + * Set the collation options for the cursor. + * + * @param value - The cursor collation options (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields). + */ + collation(value: CollationOptions): this; + /** + * Set the limit for the cursor. + * + * @param value - The limit for the cursor query. + */ + limit(value: number): this; + /** + * Set the skip for the cursor. + * + * @param value - The skip for the cursor query. + */ + skip(value: number): this; +} + +/** @public */ +export declare interface FindOneAndDeleteOptions extends CommandOperationOptions { + /** An optional hint for query optimization. See the {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/ + hint?: Document; + /** Limits the fields to return for all matching documents. */ + projection?: Document; + /** Determines which document the operation modifies if the query selects multiple documents. */ + sort?: Sort; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /** + * Return the ModifyResult instead of the modified document. Defaults to false + */ + includeResultMetadata?: boolean; +} + +/** @public */ +export declare interface FindOneAndReplaceOptions extends CommandOperationOptions { + /** Allow driver to bypass schema validation. */ + bypassDocumentValidation?: boolean; + /** An optional hint for query optimization. See the {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/ + hint?: Document; + /** Limits the fields to return for all matching documents. */ + projection?: Document; + /** When set to 'after', returns the updated document rather than the original. The default is 'before'. */ + returnDocument?: ReturnDocument; + /** Determines which document the operation modifies if the query selects multiple documents. */ + sort?: Sort; + /** Upsert the document if it does not exist. */ + upsert?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /** + * Return the ModifyResult instead of the modified document. Defaults to false + */ + includeResultMetadata?: boolean; +} + +/** @public */ +export declare interface FindOneAndUpdateOptions extends CommandOperationOptions { + /** Optional list of array filters referenced in filtered positional operators */ + arrayFilters?: Document[]; + /** Allow driver to bypass schema validation. */ + bypassDocumentValidation?: boolean; + /** An optional hint for query optimization. See the {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/ + hint?: Document; + /** Limits the fields to return for all matching documents. */ + projection?: Document; + /** When set to 'after', returns the updated document rather than the original. The default is 'before'. */ + returnDocument?: ReturnDocument; + /** Determines which document the operation modifies if the query selects multiple documents. */ + sort?: Sort; + /** Upsert the document if it does not exist. */ + upsert?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /** + * Return the ModifyResult instead of the modified document. Defaults to false + */ + includeResultMetadata?: boolean; +} + +/** @public */ +export declare type FindOneOptions = Omit; + +/** + * A builder object that is returned from {@link BulkOperationBase#find}. + * Is used to build a write operation that involves a query filter. + * + * @public + */ +export declare class FindOperators { + bulkOperation: BulkOperationBase; + /* Excluded from this release type: __constructor */ + /** Add a multiple update operation to the bulk operation */ + update(updateDocument: Document | Document[]): BulkOperationBase; + /** Add a single update operation to the bulk operation */ + updateOne(updateDocument: Document | Document[]): BulkOperationBase; + /** Add a replace one operation to the bulk operation */ + replaceOne(replacement: Document): BulkOperationBase; + /** Add a delete one operation to the bulk operation */ + deleteOne(): BulkOperationBase; + /** Add a delete many operation to the bulk operation */ + delete(): BulkOperationBase; + /** Upsert modifier for update bulk operation, noting that this operation is an upsert. */ + upsert(): this; + /** Specifies the collation for the query condition. */ + collation(collation: CollationOptions): this; + /** Specifies arrayFilters for UpdateOne or UpdateMany bulk operations. */ + arrayFilters(arrayFilters: Document[]): this; + /** Specifies hint for the bulk operation. */ + hint(hint: Hint): this; +} + +/** + * @public + */ +export declare interface FindOptions extends Omit, AbstractCursorOptions { + /** Sets the limit of documents returned in the query. */ + limit?: number; + /** Set to sort the documents coming back from the query. Array of indexes, `[['a', 1]]` etc. */ + sort?: Sort; + /** The fields to return in the query. Object of fields to either include or exclude (one of, not both), `{'a':1, 'b': 1}` **or** `{'a': 0, 'b': 0}` */ + projection?: Document; + /** Set to skip N documents ahead in your query (useful for pagination). */ + skip?: number; + /** Tell the query to use specific indexes in the query. Object of indexes to use, `{'_id':1}` */ + hint?: Hint; + /** Specify if the cursor can timeout. */ + timeout?: boolean; + /** Specify if the cursor is tailable. */ + tailable?: boolean; + /** Specify if the cursor is a tailable-await cursor. Requires `tailable` to be true */ + awaitData?: boolean; + /** Set the batchSize for the getMoreCommand when iterating over the query results. */ + batchSize?: number; + /** If true, returns only the index keys in the resulting documents. */ + returnKey?: boolean; + /** The inclusive lower bound for a specific index */ + min?: Document; + /** The exclusive upper bound for a specific index */ + max?: Document; + /** Number of milliseconds to wait before aborting the query. */ + maxTimeMS?: number; + /** The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query. Requires `tailable` and `awaitData` to be true */ + maxAwaitTimeMS?: number; + /** The server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use. Set this option to prevent that. */ + noCursorTimeout?: boolean; + /** Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields). */ + collation?: CollationOptions; + /** Allows disk use for blocking sort operations exceeding 100MB memory. (MongoDB 3.2 or higher) */ + allowDiskUse?: boolean; + /** Determines whether to close the cursor after the first batch. Defaults to false. */ + singleBatch?: boolean; + /** For queries against a sharded collection, allows the command (or subsequent getMore commands) to return partial results, rather than an error, if one or more queried shards are unavailable. */ + allowPartialResults?: boolean; + /** Determines whether to return the record identifier for each document. If true, adds a field $recordId to the returned documents. */ + showRecordId?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /** + * Option to enable an optimized code path for queries looking for a particular range of `ts` values in the oplog. Requires `tailable` to be true. + * @deprecated Starting from MongoDB 4.4 this flag is not needed and will be ignored. + */ + oplogReplay?: boolean; + /** + * Specifies the verbosity mode for the explain output. + * @deprecated This API is deprecated in favor of `collection.find().explain()`. + */ + explain?: ExplainOptions['explain']; + /* Excluded from this release type: timeoutMode */ +} + +/** @public */ +export declare type Flatten = Type extends ReadonlyArray ? Item : Type; + +/** + * @public + * Configuration options for making an AWS encryption key + */ +export declare interface GCPEncryptionKeyOptions { + /** + * GCP project ID + */ + projectId: string; + /** + * Location name (e.g. "global") + */ + location: string; + /** + * Key ring name + */ + keyRing: string; + /** + * Key name + */ + keyName: string; + /** + * Key version + */ + keyVersion?: string | undefined; + /** + * KMS URL, defaults to `https://www.googleapis.com/auth/cloudkms` + */ + endpoint?: string | undefined; +} + +/** @public */ +export declare type GCPKMSProviderConfiguration = { + /** + * The service account email to authenticate + */ + email: string; + /** + * A PKCS#8 encrypted key. This can either be a base64 string or a binary representation + */ + privateKey: string | Buffer; + /** + * If present, a host with optional port. E.g. "example.com" or "example.com:443". + * Defaults to "oauth2.googleapis.com" + */ + endpoint?: string | undefined; +} | { + /** + * If present, an access token to authenticate with GCP. + */ + accessToken: string; +}; + +/** @public */ +export declare type GenericListener = (...args: any[]) => void; + +/** + * Constructor for a streaming GridFS interface + * @public + */ +export declare class GridFSBucket extends TypedEventEmitter { + /* Excluded from this release type: s */ + /** + * When the first call to openUploadStream is made, the upload stream will + * check to see if it needs to create the proper indexes on the chunks and + * files collections. This event is fired either when 1) it determines that + * no index creation is necessary, 2) when it successfully creates the + * necessary indexes. + * @event + */ + static readonly INDEX: "index"; + constructor(db: Db, options?: GridFSBucketOptions); + /** + * Returns a writable stream (GridFSBucketWriteStream) for writing + * buffers to GridFS. The stream's 'id' property contains the resulting + * file's id. + * + * @param filename - The value of the 'filename' key in the files doc + * @param options - Optional settings. + */ + openUploadStream(filename: string, options?: GridFSBucketWriteStreamOptions): GridFSBucketWriteStream; + /** + * Returns a writable stream (GridFSBucketWriteStream) for writing + * buffers to GridFS for a custom file id. The stream's 'id' property contains the resulting + * file's id. + */ + openUploadStreamWithId(id: ObjectId, filename: string, options?: GridFSBucketWriteStreamOptions): GridFSBucketWriteStream; + /** Returns a readable stream (GridFSBucketReadStream) for streaming file data from GridFS. */ + openDownloadStream(id: ObjectId, options?: GridFSBucketReadStreamOptions): GridFSBucketReadStream; + /** + * Deletes a file with the given id + * + * @param id - The id of the file doc + */ + delete(id: ObjectId, options?: { + timeoutMS: number; + }): Promise; + /** Convenience wrapper around find on the files collection */ + find(filter?: Filter, options?: FindOptions): FindCursor; + /** + * Returns a readable stream (GridFSBucketReadStream) for streaming the + * file with the given name from GridFS. If there are multiple files with + * the same name, this will stream the most recent file with the given name + * (as determined by the `uploadDate` field). You can set the `revision` + * option to change this behavior. + */ + openDownloadStreamByName(filename: string, options?: GridFSBucketReadStreamOptionsWithRevision): GridFSBucketReadStream; + /** + * Renames the file with the given _id to the given string + * + * @param id - the id of the file to rename + * @param filename - new name for the file + */ + rename(id: ObjectId, filename: string, options?: { + timeoutMS: number; + }): Promise; + /** Removes this bucket's files collection, followed by its chunks collection. */ + drop(options?: { + timeoutMS: number; + }): Promise; +} + +/** @public */ +export declare type GridFSBucketEvents = { + index(): void; +}; + +/** @public */ +export declare interface GridFSBucketOptions extends WriteConcernOptions { + /** The 'files' and 'chunks' collections will be prefixed with the bucket name followed by a dot. */ + bucketName?: string; + /** Number of bytes stored in each chunk. Defaults to 255KB */ + chunkSizeBytes?: number; + /** Read preference to be passed to read operations */ + readPreference?: ReadPreference; + /** + * @experimental + * Specifies the lifetime duration of a gridFS stream. If any async operations are in progress + * when this timeout expires, the stream will throw a timeout error. + */ + timeoutMS?: number; +} + +/* Excluded from this release type: GridFSBucketPrivate */ + +/** + * A readable stream that enables you to read buffers from GridFS. + * + * Do not instantiate this class directly. Use `openDownloadStream()` instead. + * @public + */ +export declare class GridFSBucketReadStream extends Readable { + /* Excluded from this release type: s */ + /** + * Fires when the stream loaded the file document corresponding to the provided id. + * @event + */ + static readonly FILE: "file"; + /* Excluded from this release type: __constructor */ + /* Excluded from this release type: _read */ + /** + * Sets the 0-based offset in bytes to start streaming from. Throws + * an error if this stream has entered flowing mode + * (e.g. if you've already called `on('data')`) + * + * @param start - 0-based offset in bytes to start streaming from + */ + start(start?: number): this; + /** + * Sets the 0-based offset in bytes to start streaming from. Throws + * an error if this stream has entered flowing mode + * (e.g. if you've already called `on('data')`) + * + * @param end - Offset in bytes to stop reading at + */ + end(end?: number): this; + /** + * Marks this stream as aborted (will never push another `data` event) + * and kills the underlying cursor. Will emit the 'end' event, and then + * the 'close' event once the cursor is successfully killed. + */ + abort(): Promise; +} + +/** @public */ +export declare interface GridFSBucketReadStreamOptions { + sort?: Sort; + skip?: number; + /** + * 0-indexed non-negative byte offset from the beginning of the file + */ + start?: number; + /** + * 0-indexed non-negative byte offset to the end of the file contents + * to be returned by the stream. `end` is non-inclusive + */ + end?: number; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; +} + +/** @public */ +export declare interface GridFSBucketReadStreamOptionsWithRevision extends GridFSBucketReadStreamOptions { + /** The revision number relative to the oldest file with the given filename. 0 + * gets you the oldest file, 1 gets you the 2nd oldest, -1 gets you the + * newest. */ + revision?: number; +} + +/* Excluded from this release type: GridFSBucketReadStreamPrivate */ + +/** + * A writable stream that enables you to write buffers to GridFS. + * + * Do not instantiate this class directly. Use `openUploadStream()` instead. + * @public + */ +export declare class GridFSBucketWriteStream extends Writable { + bucket: GridFSBucket; + /** A Collection instance where the file's chunks are stored */ + chunks: Collection; + /** A Collection instance where the file's GridFSFile document is stored */ + files: Collection; + /** The name of the file */ + filename: string; + /** Options controlling the metadata inserted along with the file */ + options: GridFSBucketWriteStreamOptions; + /** Indicates the stream is finished uploading */ + done: boolean; + /** The ObjectId used for the `_id` field on the GridFSFile document */ + id: ObjectId; + /** The number of bytes that each chunk will be limited to */ + chunkSizeBytes: number; + /** Space used to store a chunk currently being inserted */ + bufToStore: Uint8Array; + /** Accumulates the number of bytes inserted as the stream uploads chunks */ + length: number; + /** Accumulates the number of chunks inserted as the stream uploads file contents */ + n: number; + /** Tracks the current offset into the buffered bytes being uploaded */ + pos: number; + /** Contains a number of properties indicating the current state of the stream */ + state: { + /** If set the stream has ended */ + streamEnd: boolean; + /** Indicates the number of chunks that still need to be inserted to exhaust the current buffered data */ + outstandingRequests: number; + /** If set an error occurred during insertion */ + errored: boolean; + /** If set the stream was intentionally aborted */ + aborted: boolean; + }; + /** The write concern setting to be used with every insert operation */ + writeConcern?: WriteConcern; + /** + * The document containing information about the inserted file. + * This property is defined _after_ the finish event has been emitted. + * It will remain `null` if an error occurs. + * + * @example + * ```ts + * fs.createReadStream('file.txt') + * .pipe(bucket.openUploadStream('file.txt')) + * .on('finish', function () { + * console.log(this.gridFSFile) + * }) + * ``` + */ + gridFSFile: GridFSFile | null; + /* Excluded from this release type: timeoutContext */ + /* Excluded from this release type: __constructor */ + /* Excluded from this release type: _construct */ + /* Excluded from this release type: _write */ + /* Excluded from this release type: _final */ + /** + * Places this write stream into an aborted state (all future writes fail) + * and deletes all chunks that have already been written. + */ + abort(): Promise; +} + +/** @public */ +export declare interface GridFSBucketWriteStreamOptions extends WriteConcernOptions { + /** Overwrite this bucket's chunkSizeBytes for this file */ + chunkSizeBytes?: number; + /** Custom file id for the GridFS file. */ + id?: ObjectId; + /** Object to store in the file document's `metadata` field */ + metadata?: Document; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; +} + +/** @public */ +export declare interface GridFSChunk { + _id: ObjectId; + files_id: ObjectId; + n: number; + data: Buffer | Uint8Array; +} + +/** @public */ +export declare interface GridFSFile { + _id: ObjectId; + length: number; + chunkSize: number; + filename: string; + metadata?: Document; + uploadDate: Date; +} + +/** @public */ +export declare const GSSAPICanonicalizationValue: Readonly<{ + readonly on: true; + readonly off: false; + readonly none: "none"; + readonly forward: "forward"; + readonly forwardAndReverse: "forwardAndReverse"; +}>; + +/** @public */ +export declare type GSSAPICanonicalizationValue = (typeof GSSAPICanonicalizationValue)[keyof typeof GSSAPICanonicalizationValue]; + +/* Excluded from this release type: HandshakeDocument */ + +/** @public */ +export declare interface HedgeOptions { + /** Explicitly enable or disable hedged reads. */ + enabled?: boolean; +} + +/** @public */ +export declare type Hint = string | Document; + +/** @public */ +export declare class HostAddress { + host: string | undefined; + port: number | undefined; + socketPath: string | undefined; + isIPv6: boolean; + constructor(hostString: string); + inspect(): string; + toString(): string; + static fromString(this: void, s: string): HostAddress; + static fromHostPort(host: string, port: number): HostAddress; + static fromSrvRecord({ name, port }: SrvRecord): HostAddress; + toHostPort(): { + host: string; + port: number; + }; +} + +/** + * The information returned by the server on the IDP server. + * @public + */ +export declare interface IdPInfo { + /** + * A URL which describes the Authentication Server. This identifier should + * be the iss of provided access tokens, and be viable for RFC8414 metadata + * discovery and RFC9207 identification. + */ + issuer: string; + /** A unique client ID for this OIDC client. */ + clientId: string; + /** A list of additional scopes to request from IdP. */ + requestScopes?: string[]; +} + +/** + * The response from the IdP server with the access token and + * optional expiration time and refresh token. + * @public + */ +export declare interface IdPServerResponse { + /** The OIDC access token. */ + accessToken: string; + /** The time when the access token expires. For future use. */ + expiresInSeconds?: number; + /** The refresh token, if applicable, to be used by the callback to request a new token from the issuer. */ + refreshToken?: string; +} + +/** @public */ +export declare interface IndexDescription extends Pick { + collation?: CollationOptions; + name?: string; + key: { + [key: string]: IndexDirection; + } | Map; +} + +/** @public */ +export declare type IndexDescriptionCompact = Record; + +/** + * @public + * The index information returned by the listIndexes command. https://www.mongodb.com/docs/manual/reference/command/listIndexes/#mongodb-dbcommand-dbcmd.listIndexes + */ +export declare type IndexDescriptionInfo = Omit & { + key: { + [key: string]: IndexDirection; + }; + v?: IndexDescription['version']; +} & Document; + +/** @public */ +export declare type IndexDirection = -1 | 1 | '2d' | '2dsphere' | 'text' | 'geoHaystack' | 'hashed' | number; + +/** @public */ +export declare interface IndexInformationOptions extends ListIndexesOptions { + /** + * When `true`, an array of index descriptions is returned. + * When `false`, the driver returns an object that with keys corresponding to index names with values + * corresponding to the entries of the indexes' key. + * + * For example, the given the following indexes: + * ``` + * [ { name: 'a_1', key: { a: 1 } }, { name: 'b_1_c_1' , key: { b: 1, c: 1 } }] + * ``` + * + * When `full` is `true`, the above array is returned. When `full` is `false`, the following is returned: + * ``` + * { + * 'a_1': [['a', 1]], + * 'b_1_c_1': [['b', 1], ['c', 1]], + * } + * ``` + */ + full?: boolean; +} + +/** @public */ +export declare type IndexSpecification = OneOrMore>; + +/** Given an object shaped type, return the type of the _id field or default to ObjectId @public */ +export declare type InferIdType = TSchema extends { + _id: infer IdType; +} ? Record extends IdType ? never : IdType : TSchema extends { + _id?: infer IdType; +} ? unknown extends IdType ? ObjectId : IdType : ObjectId; + +/* Excluded from this release type: InitialCursorResponse */ + +/** @public */ +export declare interface InsertManyResult { + /** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */ + acknowledged: boolean; + /** The number of inserted documents for this operations */ + insertedCount: number; + /** Map of the index of the inserted document to the id of the inserted document */ + insertedIds: { + [key: number]: InferIdType; + }; +} + +/** @public */ +export declare interface InsertOneModel { + /** The document to insert. */ + document: OptionalId; +} + +/** @public */ +export declare interface InsertOneOptions extends CommandOperationOptions { + /** Allow driver to bypass schema validation. */ + bypassDocumentValidation?: boolean; + /** Force server to assign _id values instead of driver. */ + forceServerObjectId?: boolean; +} + +/** @public */ +export declare interface InsertOneResult { + /** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */ + acknowledged: boolean; + /** The identifier that was inserted. If the server generated the identifier, this value will be null as the driver does not have access to that data */ + insertedId: InferIdType; +} + +export { Int32 } + +/** @public */ +export declare type IntegerType = number | Int32 | Long | bigint; + +/* Excluded from this release type: InternalAbstractCursorOptions */ + +/** @public */ +export declare type IsAny = true extends false & Type ? ResultIfAny : ResultIfNotAny; + +/** + * Helper types for dot-notation filter attributes + */ +/** @public */ +export declare type Join = T extends [] ? '' : T extends [string | number] ? `${T[0]}` : T extends [string | number, ...infer R] ? `${T[0]}${D}${Join}` : string; + +/* Excluded from this release type: JSTypeOf */ + +/* Excluded from this release type: kDecorateResult */ + +/** @public */ +export declare type KeysOfAType = { + [key in keyof TSchema]: NonNullable extends Type ? key : never; +}[keyof TSchema]; + +/** @public */ +export declare type KeysOfOtherType = { + [key in keyof TSchema]: NonNullable extends Type ? never : key; +}[keyof TSchema]; + +/** + * @public + * Configuration options for making a KMIP encryption key + */ +export declare interface KMIPEncryptionKeyOptions { + /** + * keyId is the KMIP Unique Identifier to a 96 byte KMIP Secret Data managed object. + * + * If keyId is omitted, a random 96 byte KMIP Secret Data managed object will be created. + */ + keyId?: string; + /** + * Host with optional port. + */ + endpoint?: string; + /** + * If true, this key should be decrypted by the KMIP server. + * + * Requires `mongodb-client-encryption>=6.0.1`. + */ + delegated?: boolean; +} + +/** @public */ +export declare interface KMIPKMSProviderConfiguration { + /** + * The output endpoint string. + * The endpoint consists of a hostname and port separated by a colon. + * E.g. "example.com:123". A port is always present. + */ + endpoint?: string; +} + +/** + * @public + * Configuration options that are used by specific KMS providers during key generation, encryption, and decryption. + * + * Named KMS providers _are not supported_ for automatic KMS credential fetching. + */ +export declare interface KMSProviders { + /** + * Configuration options for using 'aws' as your KMS provider + */ + aws?: AWSKMSProviderConfiguration | Record; + [key: `aws:${string}`]: AWSKMSProviderConfiguration; + /** + * Configuration options for using 'local' as your KMS provider + */ + local?: LocalKMSProviderConfiguration; + [key: `local:${string}`]: LocalKMSProviderConfiguration; + /** + * Configuration options for using 'kmip' as your KMS provider + */ + kmip?: KMIPKMSProviderConfiguration; + [key: `kmip:${string}`]: KMIPKMSProviderConfiguration; + /** + * Configuration options for using 'azure' as your KMS provider + */ + azure?: AzureKMSProviderConfiguration | Record; + [key: `azure:${string}`]: AzureKMSProviderConfiguration; + /** + * Configuration options for using 'gcp' as your KMS provider + */ + gcp?: GCPKMSProviderConfiguration | Record; + [key: `gcp:${string}`]: GCPKMSProviderConfiguration; +} + +/* Excluded from this release type: LegacyTimeoutContext */ + +/* Excluded from this release type: LegacyTimeoutContextOptions */ + +/** @public */ +export declare const LEGAL_TCP_SOCKET_OPTIONS: readonly ["autoSelectFamily", "autoSelectFamilyAttemptTimeout", "keepAliveInitialDelay", "family", "hints", "localAddress", "localPort", "lookup"]; + +/** @public */ +export declare const LEGAL_TLS_SOCKET_OPTIONS: readonly ["allowPartialTrustChain", "ALPNProtocols", "ca", "cert", "checkServerIdentity", "ciphers", "crl", "ecdhCurve", "key", "minDHSize", "passphrase", "pfx", "rejectUnauthorized", "secureContext", "secureProtocol", "servername", "session"]; + +/* Excluded from this release type: List */ + +/** @public */ +export declare class ListCollectionsCursor | CollectionInfo = Pick | CollectionInfo> extends AbstractCursor { + parent: Db; + filter: Document; + options?: ListCollectionsOptions & Abortable; + constructor(db: Db, filter: Document, options?: ListCollectionsOptions & Abortable); + clone(): ListCollectionsCursor; + /* Excluded from this release type: _initialize */ +} + +/** @public */ +export declare interface ListCollectionsOptions extends Omit, Abortable { + /** Since 4.0: If true, will only return the collection name in the response, and will omit additional info */ + nameOnly?: boolean; + /** Since 4.0: If true and nameOnly is true, allows a user without the required privilege (i.e. listCollections action on the database) to run the command when access control is enforced. */ + authorizedCollections?: boolean; + /** The batchSize for the returned command cursor or if pre 2.8 the systems batch collection */ + batchSize?: number; + /* Excluded from this release type: timeoutMode */ + /* Excluded from this release type: timeoutContext */ +} + +/** @public */ +export declare interface ListDatabasesOptions extends Omit { + /** A query predicate that determines which databases are listed */ + filter?: Document; + /** A flag to indicate whether the command should return just the database names, or return both database names and size information */ + nameOnly?: boolean; + /** A flag that determines which databases are returned based on the user privileges when access control is enabled */ + authorizedDatabases?: boolean; +} + +/** @public */ +export declare interface ListDatabasesResult { + databases: ({ + name: string; + sizeOnDisk?: number; + empty?: boolean; + } & Document)[]; + totalSize?: number; + totalSizeMb?: number; + ok: 1 | 0; +} + +/** @public */ +export declare class ListIndexesCursor extends AbstractCursor { + parent: Collection; + options?: ListIndexesOptions; + constructor(collection: Collection, options?: ListIndexesOptions); + clone(): ListIndexesCursor; + /* Excluded from this release type: _initialize */ +} + +/** @public */ +export declare type ListIndexesOptions = AbstractCursorOptions & { + /* Excluded from this release type: omitMaxTimeMS */ + /* Excluded from this release type: rawData */ +}; + +/** @public */ +export declare class ListSearchIndexesCursor extends AggregationCursor<{ + name: string; +}> { + /* Excluded from this release type: __constructor */ +} + +/** @public */ +export declare type ListSearchIndexesOptions = Omit; + +/** @public */ +export declare interface LocalKMSProviderConfiguration { + /** + * The master key used to encrypt/decrypt data keys. + * A 96-byte long Buffer or base64 encoded string. + */ + key: Binary | Uint8Array | string; +} + +/** @public */ +export declare interface Log extends Record { + t: Date; + c: MongoLoggableComponent; + s: SeverityLevel; + message?: string; +} + +/** @public */ +export declare interface LogComponentSeveritiesClientOptions { + /** Optional severity level for command component */ + command?: SeverityLevel; + /** Optional severity level for topology component */ + topology?: SeverityLevel; + /** Optional severity level for server selection component */ + serverSelection?: SeverityLevel; + /** Optional severity level for connection component */ + connection?: SeverityLevel; + /** Optional severity level for client component */ + client?: SeverityLevel; + /** Optional default severity level to be used if any of the above are unset */ + default?: SeverityLevel; +} + +/* Excluded from this release type: LogConvertible */ + +/* Excluded from this release type: Loggable */ + +/* Excluded from this release type: LoggableCommandFailedEvent */ + +/* Excluded from this release type: LoggableCommandSucceededEvent */ + +/* Excluded from this release type: LoggableEvent */ + +/* Excluded from this release type: LoggableServerHeartbeatFailedEvent */ + +/* Excluded from this release type: LoggableServerHeartbeatStartedEvent */ + +/* Excluded from this release type: LoggableServerHeartbeatSucceededEvent */ +export { Long } + +/** @public */ +export declare type MatchKeysAndValues = Readonly> & Record; + +export { MaxKey } + +/* Excluded from this release type: MessageHeader */ +export { MinKey } + +/** @public */ +export declare interface ModifyResult { + value: WithId | null; + lastErrorObject?: Document; + ok: 0 | 1; +} + +/** @public */ +export declare const MONGO_CLIENT_EVENTS: readonly ["connectionPoolCreated", "connectionPoolReady", "connectionPoolCleared", "connectionPoolClosed", "connectionCreated", "connectionReady", "connectionClosed", "connectionCheckOutStarted", "connectionCheckOutFailed", "connectionCheckedOut", "connectionCheckedIn", "commandStarted", "commandSucceeded", "commandFailed", "serverOpening", "serverClosed", "serverDescriptionChanged", "topologyOpening", "topologyClosed", "topologyDescriptionChanged", "error", "timeout", "close", "serverHeartbeatStarted", "serverHeartbeatSucceeded", "serverHeartbeatFailed"]; + +/** + * An error generated when the driver API is used incorrectly + * + * @privateRemarks + * Should **never** be directly instantiated + * + * @public + * @category Error + */ +export declare class MongoAPIError extends MongoDriverError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { + cause?: Error; + }); + get name(): string; +} + +/** + * A error generated when the user attempts to authenticate + * via AWS, but fails + * + * @public + * @category Error + */ +export declare class MongoAWSError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { + cause?: Error; + }); + get name(): string; +} + +/** + * A error generated when the user attempts to authenticate + * via Azure, but fails. + * + * @public + * @category Error + */ +export declare class MongoAzureError extends MongoOIDCError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * An error generated when a batch command is re-executed after one of the commands in the batch + * has failed + * + * @public + * @category Error + */ +export declare class MongoBatchReExecutionError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message?: string); + get name(): string; +} + +/** + * An error indicating an unsuccessful Bulk Write + * @public + * @category Error + */ +export declare class MongoBulkWriteError extends MongoServerError { + result: BulkWriteResult; + writeErrors: OneOrMore; + err?: WriteConcernError; + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(error: { + message: string; + code: number; + writeErrors?: WriteError[]; + } | WriteConcernError | AnyError, result: BulkWriteResult); + get name(): string; + /** Number of documents inserted. */ + get insertedCount(): number; + /** Number of documents matched for update. */ + get matchedCount(): number; + /** Number of documents modified. */ + get modifiedCount(): number; + /** Number of documents deleted. */ + get deletedCount(): number; + /** Number of documents upserted. */ + get upsertedCount(): number; + /** Inserted document generated Id's, hash key is the index of the originating operation */ + get insertedIds(): { + [key: number]: any; + }; + /** Upserted document generated Id's, hash key is the index of the originating operation */ + get upsertedIds(): { + [key: number]: any; + }; +} + +/** + * An error generated when a ChangeStream operation fails to execute. + * + * @public + * @category Error + */ +export declare class MongoChangeStreamError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * @public + * + * The **MongoClient** class is a class that allows for making Connections to MongoDB. + * + * **NOTE:** The programmatically provided options take precedence over the URI options. + * + * @remarks + * + * A MongoClient is the entry point to connecting to a MongoDB server. + * + * It handles a multitude of features on your application's behalf: + * - **Server Host Connection Configuration**: A MongoClient is responsible for reading TLS cert, ca, and crl files if provided. + * - **SRV Record Polling**: A "`mongodb+srv`" style connection string is used to have the MongoClient resolve DNS SRV records of all server hostnames which the driver periodically monitors for changes and adjusts its current view of hosts correspondingly. + * - **Server Monitoring**: The MongoClient automatically keeps monitoring the health of server nodes in your cluster to reach out to the correct and lowest latency one available. + * - **Connection Pooling**: To avoid paying the cost of rebuilding a connection to the server on every operation the MongoClient keeps idle connections preserved for reuse. + * - **Session Pooling**: The MongoClient creates logical sessions that enable retryable writes, causal consistency, and transactions. It handles pooling these sessions for reuse in subsequent operations. + * - **Cursor Operations**: A MongoClient's cursors use the health monitoring system to send the request for more documents to the same server the query began on. + * - **Mongocryptd process**: When using auto encryption, a MongoClient will launch a `mongocryptd` instance for handling encryption if the mongocrypt shared library isn't in use. + * + * There are many more features of a MongoClient that are not listed above. + * + * In order to enable these features, a number of asynchronous Node.js resources are established by the driver: Timers, FS Requests, Sockets, etc. + * For details on cleanup, please refer to the MongoClient `close()` documentation. + * + * @example + * ```ts + * import { MongoClient } from 'mongodb'; + * // Enable command monitoring for debugging + * const client = new MongoClient('mongodb://localhost:27017?appName=mflix', { monitorCommands: true }); + * ``` + */ +export declare class MongoClient extends TypedEventEmitter implements AsyncDisposable { + /* Excluded from this release type: s */ + /* Excluded from this release type: topology */ + /* Excluded from this release type: mongoLogger */ + /* Excluded from this release type: connectionLock */ + /* Excluded from this release type: closeLock */ + /** + * The consolidate, parsed, transformed and merged options. + */ + readonly options: Readonly> & Pick & { + /* Excluded from this release type: metadata */ + }; + private driverInfoList; + constructor(url: string, options?: MongoClientOptions); + /** + * An alias for {@link MongoClient.close|MongoClient.close()}. + */ + [Symbol.asyncDispose](): Promise; + /** + * Append metadata to the client metadata after instantiation. + * @param driverInfo - Information about the application or library. + */ + appendMetadata(driverInfo: DriverInfo): void; + /* Excluded from this release type: checkForNonGenuineHosts */ + get serverApi(): Readonly; + /* Excluded from this release type: monitorCommands */ + /* Excluded from this release type: monitorCommands */ + /* Excluded from this release type: autoEncrypter */ + get readConcern(): ReadConcern | undefined; + get writeConcern(): WriteConcern | undefined; + get readPreference(): ReadPreference; + get bsonOptions(): BSONSerializeOptions; + get timeoutMS(): number | undefined; + /** + * Executes a client bulk write operation, available on server 8.0+. + * @param models - The client bulk write models. + * @param options - The client bulk write options. + * @returns A ClientBulkWriteResult for acknowledged writes and ok: 1 for unacknowledged writes. + */ + bulkWrite = Record>(models: ReadonlyArray>, options?: ClientBulkWriteOptions): Promise; + /** + * An optional method to verify a handful of assumptions that are generally useful at application boot-time before using a MongoClient. + * For detailed information about the connect process see the MongoClient.connect static method documentation. + * + * @param url - The MongoDB connection string (supports `mongodb://` and `mongodb+srv://` schemes) + * @param options - Optional configuration options for the client + * + * @see https://www.mongodb.com/docs/manual/reference/connection-string/ + */ + connect(): Promise; + /* Excluded from this release type: _connect */ + /** + * Cleans up resources managed by the MongoClient. + * + * The close method clears and closes all resources whose lifetimes are managed by the MongoClient. + * Please refer to the `MongoClient` class documentation for a high level overview of the client's key features and responsibilities. + * + * **However,** the close method does not handle the cleanup of resources explicitly created by the user. + * Any user-created driver resource with its own `close()` method should be explicitly closed by the user before calling MongoClient.close(). + * This method is written as a "best effort" attempt to leave behind the least amount of resources server-side when possible. + * + * The following list defines ideal preconditions and consequent pitfalls if they are not met. + * The MongoClient, ClientSession, Cursors and ChangeStreams all support [explicit resource management](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html). + * By using explicit resource management to manage the lifetime of driver resources instead of manually managing their lifetimes, the pitfalls outlined below can be avoided. + * + * The close method performs the following in the order listed: + * - Client-side: + * - **Close in-use connections**: Any connections that are currently waiting on a response from the server will be closed. + * This is performed _first_ to avoid reaching the next step (server-side clean up) and having no available connections to check out. + * - _Ideal_: All operations have been awaited or cancelled, and the outcomes, regardless of success or failure, have been processed before closing the client servicing the operation. + * - _Pitfall_: When `client.close()` is called and all connections are in use, after closing them, the client must create new connections for cleanup operations, which comes at the cost of new TLS/TCP handshakes and authentication steps. + * - Server-side: + * - **Close active cursors**: All cursors that haven't been completed will have a `killCursor` operation sent to the server they were initialized on, freeing the server-side resource. + * - _Ideal_: Cursors are explicitly closed or completed before `client.close()` is called. + * - _Pitfall_: `killCursors` may have to build a new connection if the in-use closure ended all pooled connections. + * - **End active sessions**: In-use sessions created with `client.startSession()` or `client.withSession()` or implicitly by the driver will have their `.endSession()` method called. + * Contrary to the name of the method, `endSession()` returns the session to the client's pool of sessions rather than end them on the server. + * - _Ideal_: Transaction outcomes are awaited and their corresponding explicit sessions are ended before `client.close()` is called. + * - _Pitfall_: **This step aborts in-progress transactions**. It is advisable to observe the outcome of a transaction before closing your client. + * - **End all pooled sessions**: The `endSessions` command with all session IDs the client has pooled is sent to the server to inform the cluster it can clean them up. + * - _Ideal_: No user intervention is expected. + * - _Pitfall_: None. + * + * The remaining shutdown is of the MongoClient resources that are intended to be entirely internal but is documented here as their existence relates to the JS event loop. + * + * - Client-side (again): + * - **Stop all server monitoring**: Connections kept live for detecting cluster changes and roundtrip time measurements are shutdown. + * - **Close all pooled connections**: Each server node in the cluster has a corresponding connection pool and all connections in the pool are closed. Any operations waiting to check out a connection will have an error thrown instead of a connection returned. + * - **Clear out server selection queue**: Any operations that are in the process of waiting for a server to be selected will have an error thrown instead of a server returned. + * - **Close encryption-related resources**: An internal MongoClient created for communicating with `mongocryptd` or other encryption purposes is closed. (Using this same method of course!) + * + * After the close method completes there should be no MongoClient related resources [ref-ed in Node.js' event loop](https://docs.libuv.org/en/v1.x/handle.html#reference-counting). + * This should allow Node.js to exit gracefully if MongoClient resources were the only active handles in the event loop. + * + * @param _force - currently an unused flag that has no effect. Defaults to `false`. + */ + close(_force?: boolean): Promise; + private _close; + /** + * Create a new Db instance sharing the current socket connections. + * + * @param dbName - The name of the database we want to use. If not provided, use database name from connection string. + * @param options - Optional settings for Db construction + */ + db(dbName?: string, options?: DbOptions): Db; + /** + * Creates a new MongoClient instance and immediately connects it to MongoDB. + * This convenience method combines `new MongoClient(url, options)` and `client.connect()` in a single step. + * + * Connect can be helpful to detect configuration issues early by validating: + * - **DNS Resolution**: Verifies that SRV records and hostnames in the connection string resolve DNS entries + * - **Network Connectivity**: Confirms that host addresses are reachable and ports are open + * - **TLS Configuration**: Validates SSL/TLS certificates, CA files, and encryption settings are correct + * - **Authentication**: Verifies that provided credentials are valid + * - **Server Compatibility**: Ensures the MongoDB server version is supported by this driver version + * - **Load Balancer Setup**: For load-balanced deployments, confirms the service is properly configured + * + * @returns A promise that resolves to the same MongoClient instance once connected + * + * @remarks + * **Connection is Optional:** Calling `connect` is optional since any operation method (`find`, `insertOne`, etc.) + * will automatically perform these same validation steps if the client is not already connected. + * However, explicitly calling `connect` can make sense for: + * - **Fail-fast Error Detection**: Non-transient connection issues (hostname unresolved, port refused connection) are discovered immediately rather than during your first operation + * - **Predictable Performance**: Eliminates first connection overhead from your first database operation + * + * @remarks + * **Connection Pooling Impact:** Calling `connect` will populate the connection pool with one connection + * to a server selected by the client's configured `readPreference` (defaults to primary). + * + * @remarks + * **Timeout Behavior:** When using `timeoutMS`, the connection establishment time does not count against + * the timeout for subsequent operations. This means `connect` runs without a `timeoutMS` limit, while + * your database operations will still respect the configured timeout. If you need predictable operation + * timing with `timeoutMS`, call `connect` explicitly before performing operations. + * + * @see https://www.mongodb.com/docs/manual/reference/connection-string/ + */ + static connect(url: string, options?: MongoClientOptions): Promise; + /** + * Creates a new ClientSession. When using the returned session in an operation + * a corresponding ServerSession will be created. + * + * @remarks + * A ClientSession instance may only be passed to operations being performed on the same + * MongoClient it was started from. + */ + startSession(options?: ClientSessionOptions): ClientSession; + /** + * A convenience method for creating and handling the clean up of a ClientSession. + * The session will always be ended when the executor finishes. + * + * @param executor - An executor function that all operations using the provided session must be invoked in + * @param options - optional settings for the session + */ + withSession(executor: WithSessionCallback): Promise; + withSession(options: ClientSessionOptions, executor: WithSessionCallback): Promise; + /** + * Create a new Change Stream, watching for new changes (insertions, updates, + * replacements, deletions, and invalidations) in this cluster. Will ignore all + * changes to system collections, as well as the local, admin, and config databases. + * + * @remarks + * watch() accepts two generic arguments for distinct use cases: + * - The first is to provide the schema that may be defined for all the data within the current cluster + * - The second is to override the shape of the change stream document entirely, if it is not provided the type will default to ChangeStreamDocument of the first argument + * + * @remarks + * When `timeoutMS` is configured for a change stream, it will have different behaviour depending + * on whether the change stream is in iterator mode or emitter mode. In both cases, a change + * stream will time out if it does not receive a change event within `timeoutMS` of the last change + * event. + * + * Note that if a change stream is consistently timing out when watching a collection, database or + * client that is being changed, then this may be due to the server timing out before it can finish + * processing the existing oplog. To address this, restart the change stream with a higher + * `timeoutMS`. + * + * If the change stream times out the initial aggregate operation to establish the change stream on + * the server, then the client will close the change stream. If the getMore calls to the server + * time out, then the change stream will be left open, but will throw a MongoOperationTimeoutError + * when in iterator mode and emit an error event that returns a MongoOperationTimeoutError in + * emitter mode. + * + * To determine whether or not the change stream is still open following a timeout, check the + * {@link ChangeStream.closed} getter. + * + * @example + * In iterator mode, if a next() call throws a timeout error, it will attempt to resume the change stream. + * The next call can just be retried after this succeeds. + * ```ts + * const changeStream = collection.watch([], { timeoutMS: 100 }); + * try { + * await changeStream.next(); + * } catch (e) { + * if (e instanceof MongoOperationTimeoutError && !changeStream.closed) { + * await changeStream.next(); + * } + * throw e; + * } + * ``` + * + * @example + * In emitter mode, if the change stream goes `timeoutMS` without emitting a change event, it will + * emit an error event that returns a MongoOperationTimeoutError, but will not close the change + * stream unless the resume attempt fails. There is no need to re-establish change listeners as + * this will automatically continue emitting change events once the resume attempt completes. + * + * ```ts + * const changeStream = collection.watch([], { timeoutMS: 100 }); + * changeStream.on('change', console.log); + * changeStream.on('error', e => { + * if (e instanceof MongoOperationTimeoutError && !changeStream.closed) { + * // do nothing + * } else { + * changeStream.close(); + * } + * }); + * ``` + * @param pipeline - An array of {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents. + * @param options - Optional settings for the command + * @typeParam TSchema - Type of the data being detected by the change stream + * @typeParam TChange - Type of the whole change stream document emitted + */ + watch>(pipeline?: Document[], options?: ChangeStreamOptions): ChangeStream; +} + +/* Excluded from this release type: MongoClientAuthProviders */ + +/** + * An error indicating that an error occurred when processing bulk write results. + * + * @public + * @category Error + */ +export declare class MongoClientBulkWriteCursorError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * An error indicating that an error occurred when executing the bulk write. + * + * @public + * @category Error + */ +export declare class MongoClientBulkWriteError extends MongoServerError { + /** + * Write concern errors that occurred while executing the bulk write. This list may have + * multiple items if more than one server command was required to execute the bulk write. + */ + writeConcernErrors: Document[]; + /** + * Errors that occurred during the execution of individual write operations. This map will + * contain at most one entry if the bulk write was ordered. + */ + writeErrors: Map; + /** + * The results of any successful operations that were performed before the error was + * encountered. + */ + partialResult?: ClientBulkWriteResult; + /** + * Initialize the client bulk write error. + * @param message - The error message. + */ + constructor(message: ErrorDescription); + get name(): string; +} + +/** + * An error indicating that an error occurred on the client when executing a client bulk write. + * + * @public + * @category Error + */ +export declare class MongoClientBulkWriteExecutionError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * An error generated when the MongoClient is closed and async + * operations are interrupted. + * + * @public + * @category Error + */ +export declare class MongoClientClosedError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(); + get name(): string; +} + +/** @public */ +export declare type MongoClientEvents = Pick & { + open(mongoClient: MongoClient): void; +}; + +/** + * Describes all possible URI query options for the mongo client + * @public + * @see https://www.mongodb.com/docs/manual/reference/connection-string + */ +export declare interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeConnectionOptions { + /** Specifies the name of the replica set, if the mongod is a member of a replica set. */ + replicaSet?: string; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; + /** Enables or disables TLS/SSL for the connection. */ + tls?: boolean; + /** A boolean to enable or disables TLS/SSL for the connection. (The ssl option is equivalent to the tls option.) */ + ssl?: boolean; + /** Specifies the location of a local .pem file that contains either the client's TLS/SSL certificate and key. */ + tlsCertificateKeyFile?: string; + /** Specifies the password to de-crypt the tlsCertificateKeyFile. */ + tlsCertificateKeyFilePassword?: string; + /** Specifies the location of a local .pem file that contains the root certificate chain from the Certificate Authority. This file is used to validate the certificate presented by the mongod/mongos instance. */ + tlsCAFile?: string; + /** Specifies the location of a local CRL .pem file that contains the client revokation list. */ + tlsCRLFile?: string; + /** Bypasses validation of the certificates presented by the mongod/mongos instance */ + tlsAllowInvalidCertificates?: boolean; + /** Disables hostname validation of the certificate presented by the mongod/mongos instance. */ + tlsAllowInvalidHostnames?: boolean; + /** Disables various certificate validations. */ + tlsInsecure?: boolean; + /** The time in milliseconds to attempt a connection before timing out. */ + connectTimeoutMS?: number; + /** The time in milliseconds to attempt a send or receive on a socket before the attempt times out. */ + socketTimeoutMS?: number; + /** An array or comma-delimited string of compressors to enable network compression for communication between this client and a mongod/mongos instance. */ + compressors?: CompressorName[] | string; + /** An integer that specifies the compression level if using zlib for network compression. */ + zlibCompressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | undefined; + /** The maximum number of hosts to connect to when using an srv connection string, a setting of `0` means unlimited hosts */ + srvMaxHosts?: number; + /** + * Modifies the srv URI to look like: + * + * `_{srvServiceName}._tcp.{hostname}.{domainname}` + * + * Querying this DNS URI is expected to respond with SRV records + */ + srvServiceName?: string; + /** The maximum number of connections in the connection pool. */ + maxPoolSize?: number; + /** The minimum number of connections in the connection pool. */ + minPoolSize?: number; + /** The maximum number of connections that may be in the process of being established concurrently by the connection pool. */ + maxConnecting?: number; + /** + * The maximum amount of time a connection should remain idle in the connection pool before being marked idle, in milliseconds. + * If specified, this must be a number greater than or equal to 0, where 0 means there is no limit. Defaults to 0. After this + * time passes, the idle collection can be automatically cleaned up in the background. + */ + maxIdleTimeMS?: number; + /** The maximum time in milliseconds that a thread can wait for a connection to become available. */ + waitQueueTimeoutMS?: number; + /** Specify a read concern for the collection (only MongoDB 3.2 or higher supported) */ + readConcern?: ReadConcernLike; + /** The level of isolation */ + readConcernLevel?: ReadConcernLevel; + /** Specifies the read preferences for this connection */ + readPreference?: ReadPreferenceMode | ReadPreference; + /** Specifies, in seconds, how stale a secondary can be before the client stops using it for read operations. */ + maxStalenessSeconds?: number; + /** Specifies the tags document as a comma-separated list of colon-separated key-value pairs. */ + readPreferenceTags?: TagSet[]; + /** The auth settings for when connection to server. */ + auth?: Auth; + /** Specify the database name associated with the user’s credentials. */ + authSource?: string; + /** Specify the authentication mechanism that MongoDB will use to authenticate the connection. */ + authMechanism?: AuthMechanism; + /** Specify properties for the specified authMechanism as a comma-separated list of colon-separated key-value pairs. */ + authMechanismProperties?: AuthMechanismProperties; + /** The size (in milliseconds) of the latency window for selecting among multiple suitable MongoDB instances. */ + localThresholdMS?: number; + /** Specifies how long (in milliseconds) to block for server selection before throwing an exception. */ + serverSelectionTimeoutMS?: number; + /** heartbeatFrequencyMS controls when the driver checks the state of the MongoDB deployment. Specify the interval (in milliseconds) between checks, counted from the end of the previous check until the beginning of the next one. */ + heartbeatFrequencyMS?: number; + /** Sets the minimum heartbeat frequency. In the event that the driver has to frequently re-check a server's availability, it will wait at least this long since the previous check to avoid wasted effort. */ + minHeartbeatFrequencyMS?: number; + /** The name of the application that created this MongoClient instance. MongoDB 3.4 and newer will print this value in the server log upon establishing each connection. It is also recorded in the slow query log and profile collections */ + appName?: string; + /** Enables retryable reads. */ + retryReads?: boolean; + /** Enable retryable writes. */ + retryWrites?: boolean; + /** + * The maximum number of retries during server overload. Set to 0 to disable overload retries. Defaults to 2. + * @see https://www.mongodb.com/docs/atlas/overload-errors + * + * This option works with MongoDB Atlas Server Version 9.0 and above. + * */ + maxAdaptiveRetries?: number; + /** + * Whether or not to enable overload retargeting. Defaults to false. + * @see https://www.mongodb.com/docs/atlas/overload-errors + * More information about the overload policy in drivers: + * @see https://github.com/mongodb/specifications/blob/master/source/client-backpressure/client-backpressure.md#overload-retry-policy + * + * This option works with MongoDB Atlas Server Version 9.0 and above. + * */ + enableOverloadRetargeting?: boolean; + /** Allow a driver to force a Single topology type with a connection string containing one host */ + directConnection?: boolean; + /** Instruct the driver it is connecting to a load balancer fronting a mongos like service */ + loadBalanced?: boolean; + /** + * The write concern w value + * @deprecated Please use the `writeConcern` option instead + */ + w?: W; + /** + * The write concern timeout + * @deprecated Please use the `writeConcern` option instead + */ + wtimeoutMS?: number; + /** + * The journal write concern + * @deprecated Please use the `writeConcern` option instead + */ + journal?: boolean; + /** + * A MongoDB WriteConcern, which describes the level of acknowledgement + * requested from MongoDB for write operations. + * + * @see https://www.mongodb.com/docs/manual/reference/write-concern/ + */ + writeConcern?: WriteConcern | WriteConcernSettings; + /** TCP Connection no delay */ + noDelay?: boolean; + /** Force server to assign `_id` values instead of driver */ + forceServerObjectId?: boolean; + /** A primary key factory function for generation of custom `_id` keys */ + pkFactory?: PkFactory; + /** Enable command monitoring for this client */ + monitorCommands?: boolean; + /** Server API version */ + serverApi?: ServerApi | ServerApiVersion; + /** + * Optionally enable in-use auto encryption + * + * @remarks + * Automatic encryption is an enterprise only feature that only applies to operations on a collection. Automatic encryption is not supported for operations on a database or view, and operations that are not bypassed will result in error + * (see [libmongocrypt: Auto Encryption Allow-List](https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/client-side-encryption.md#libmongocrypt-auto-encryption-allow-list)). To bypass automatic encryption for all operations, set bypassAutoEncryption=true in AutoEncryptionOpts. + * + * Automatic encryption requires the authenticated user to have the [listCollections privilege action](https://www.mongodb.com/docs/manual/reference/command/listCollections/#dbcmd.listCollections). + * + * If a MongoClient with a limited connection pool size (i.e a non-zero maxPoolSize) is configured with AutoEncryptionOptions, a separate internal MongoClient is created if any of the following are true: + * - AutoEncryptionOptions.keyVaultClient is not passed. + * - AutoEncryptionOptions.bypassAutomaticEncryption is false. + * + * If an internal MongoClient is created, it is configured with the same options as the parent MongoClient except minPoolSize is set to 0 and AutoEncryptionOptions is omitted. + */ + autoEncryption?: AutoEncryptionOptions; + /** + * Allows a wrapping driver to amend the client metadata generated by the driver to include information about the wrapping driver + /* @deprecated - Will be made internal in a future major release. + */ + driverInfo?: DriverInfo; + /** Configures a Socks5 proxy host used for creating TCP connections. */ + proxyHost?: string; + /** Configures a Socks5 proxy port used for creating TCP connections. */ + proxyPort?: number; + /** Configures a Socks5 proxy username when the proxy in proxyHost requires username/password authentication. */ + proxyUsername?: string; + /** Configures a Socks5 proxy password when the proxy in proxyHost requires username/password authentication. */ + proxyPassword?: string; + /** Instructs the driver monitors to use a specific monitoring mode */ + serverMonitoringMode?: ServerMonitoringMode; + /** + * @public + * Specifies the destination of the driver's logging. The default is stderr. + */ + mongodbLogPath?: 'stderr' | 'stdout' | MongoDBLogWritable; + /** + * @public + * Enable logging level per component or use `default` to control any unset components. + */ + mongodbLogComponentSeverities?: LogComponentSeveritiesClientOptions; + /** + * @public + * All BSON documents are stringified to EJSON. This controls the maximum length of those strings. + * It is defaulted to 1000. + */ + mongodbLogMaxDocumentLength?: number; + /* Excluded from this release type: srvPoller */ + /* Excluded from this release type: connectionType */ + /* Excluded from this release type: __skipPingOnConnect */ + /** + * @experimental + * + * If provided, any adapters provided will be used in place of the corresponding Node.js module. + */ + runtimeAdapters?: RuntimeAdapters; +} + +/* Excluded from this release type: MongoClientPrivate */ + +/** + * An error generated when a feature that is not enabled or allowed for the current server + * configuration is used + * + * + * @public + * @category Error + */ +export declare class MongoCompatibilityError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * A representation of the credentials used by MongoDB + * @public + */ +export declare class MongoCredentials { + /** The username used for authentication */ + readonly username: string; + /** The password used for authentication */ + readonly password: string; + /** The database that the user should authenticate against */ + readonly source: string; + /** The method used to authenticate */ + readonly mechanism: AuthMechanism; + /** Special properties used by some types of auth mechanisms */ + readonly mechanismProperties: AuthMechanismProperties; + constructor(options: MongoCredentialsOptions); + /** Determines if two MongoCredentials objects are equivalent */ + equals(other: MongoCredentials): boolean; + /** + * If the authentication mechanism is set to "default", resolves the authMechanism + * based on the server version and server supported sasl mechanisms. + * + * @param hello - A hello response from the server + */ + resolveAuthMechanism(hello: Document | null): MongoCredentials; + validate(): void; + static merge(creds: MongoCredentials | undefined, options: Partial): MongoCredentials; +} + +/** @public */ +export declare interface MongoCredentialsOptions { + username?: string; + password: string; + source: string; + db?: string; + mechanism?: AuthMechanism; + mechanismProperties: AuthMechanismProperties; +} + +/** + * @public + * An error indicating that mongodb-client-encryption failed to auto-refresh Azure KMS credentials. + */ +export declare class MongoCryptAzureKMSRequestError extends MongoCryptError { + /** The body of the http response that failed, if present. */ + body?: Document; + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, body?: Document); + get name(): string; +} + +/** + * @public + * An error indicating that `ClientEncryption.createEncryptedCollection()` failed to create data keys + */ +export declare class MongoCryptCreateDataKeyError extends MongoCryptError { + encryptedFields: Document; + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(encryptedFields: Document, { cause }: { + cause: Error; + }); + get name(): string; +} + +/** + * @public + * An error indicating that `ClientEncryption.createEncryptedCollection()` failed to create a collection + */ +export declare class MongoCryptCreateEncryptedCollectionError extends MongoCryptError { + encryptedFields: Document; + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(encryptedFields: Document, { cause }: { + cause: Error; + }); + get name(): string; +} + +/* Excluded from this release type: MongocryptdManager */ + +/** + * @public + * An error indicating that something went wrong specifically with MongoDB Client Encryption + */ +export declare class MongoCryptError extends MongoError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { + cause?: Error; + }); + get name(): string; +} + +/** + * @public + * + * An error indicating an invalid argument was provided to an encryption API. + */ +export declare class MongoCryptInvalidArgumentError extends MongoCryptError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** @public */ +export declare class MongoCryptKMSRequestNetworkTimeoutError extends MongoCryptError { + get name(): string; +} + +/** + * An error thrown when an attempt is made to read from a cursor that has been exhausted + * + * @public + * @category Error + */ +export declare class MongoCursorExhaustedError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message?: string); + get name(): string; +} + +/** + * An error thrown when the user attempts to add options to a cursor that has already been + * initialized + * + * @public + * @category Error + */ +export declare class MongoCursorInUseError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message?: string); + get name(): string; +} + +/** + * @public + * + * A class representing a collection's namespace. This class enforces (through Typescript) that + * the `collection` portion of the namespace is defined and should only be + * used in scenarios where this can be guaranteed. + */ +export declare class MongoDBCollectionNamespace extends MongoDBNamespace { + collection: string; + constructor(db: string, collection: string); + static fromString(namespace?: string): MongoDBCollectionNamespace; +} + +/** + * @public + * + * A custom destination for structured logging messages. + */ +export declare interface MongoDBLogWritable { + /** + * This function will be called for every enabled log message. + * + * It can be sync or async: + * - If it is synchronous it will block the driver from proceeding until this method returns. + * - If it is asynchronous the driver will not await the returned promise. It will attach fulfillment handling (`.then`). + * If the promise rejects the logger will write an error message to stderr and stop functioning. + * If the promise resolves the driver proceeds to the next log message (or waits for new ones to occur). + * + * Tips: + * - We recommend writing an async `write` function that _never_ rejects. + * Instead handle logging errors as necessary to your use case and make the write function a noop, until it can be recovered. + * - The Log messages are structured but **subject to change** since the intended purpose is informational. + * Program against this defensively and err on the side of stringifying whatever is passed in to write in some form or another. + * + */ + write(log: Log): PromiseLike | unknown; +} + +/** @public */ +export declare class MongoDBNamespace { + db: string; + collection?: string; + /** + * Create a namespace object + * + * @param db - database name + * @param collection - collection name + */ + constructor(db: string, collection?: string); + toString(): string; + withCollection(collection: string): MongoDBCollectionNamespace; + static fromString(namespace?: string): MongoDBNamespace; +} + +/* Excluded from this release type: MongoDBResponse */ + +/* Excluded from this release type: MongoDBResponseConstructor */ + +/** + * An error generated when the driver fails to decompress + * data received from the server. + * + * @public + * @category Error + */ +export declare class MongoDecompressionError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * An error generated by the driver + * + * @public + * @category Error + */ +export declare class MongoDriverError extends MongoError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { + cause?: Error; + }); + get name(): string; +} + +/** + * @public + * @category Error + * + * @privateRemarks + * mongodb-client-encryption has a dependency on this error, it uses the constructor with a string argument + */ +export declare class MongoError extends Error { + /* Excluded from this release type: errorLabelSet */ + get errorLabels(): string[]; + /** + * This is a number in MongoServerError and a string in MongoDriverError + * @privateRemarks + * Define the type override on the subclasses when we can use the override keyword + */ + code?: number | string; + topologyVersion?: TopologyVersion; + connectionGeneration?: number; + cause?: Error; + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { + cause?: Error; + }); + /* Excluded from this release type: buildErrorMessage */ + get name(): string; + /** Legacy name for server error responses */ + get errmsg(): string; + /** + * Checks the error to see if it has an error label + * + * @param label - The error label to check for + * @returns returns true if the error has the provided error label + */ + hasErrorLabel(label: string): boolean; + addErrorLabel(label: string): void; +} + +/** @public */ +export declare const MongoErrorLabel: Readonly<{ + readonly RetryableWriteError: "RetryableWriteError"; + readonly TransientTransactionError: "TransientTransactionError"; + readonly UnknownTransactionCommitResult: "UnknownTransactionCommitResult"; + readonly ResumableChangeStreamError: "ResumableChangeStreamError"; + readonly HandshakeError: "HandshakeError"; + readonly ResetPool: "ResetPool"; + readonly PoolRequestedRetry: "PoolRequestedRetry"; + readonly InterruptInUseConnections: "InterruptInUseConnections"; + readonly NoWritesPerformed: "NoWritesPerformed"; + readonly RetryableError: "RetryableError"; + readonly SystemOverloadedError: "SystemOverloadedError"; +}>; + +/** @public */ +export declare type MongoErrorLabel = (typeof MongoErrorLabel)[keyof typeof MongoErrorLabel]; + +/** + * An error generated when the user attempts to operate + * on a session that has expired or has been closed. + * + * @public + * @category Error + */ +export declare class MongoExpiredSessionError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message?: string); + get name(): string; +} + +/** + * A error generated when the user attempts to authenticate + * via GCP, but fails. + * + * @public + * @category Error + */ +export declare class MongoGCPError extends MongoOIDCError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * An error generated when a malformed or invalid chunk is + * encountered when reading from a GridFSStream. + * + * @public + * @category Error + */ +export declare class MongoGridFSChunkError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** An error generated when a GridFSStream operation fails to execute. + * + * @public + * @category Error + */ +export declare class MongoGridFSStreamError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * An error generated when the user supplies malformed or unexpected arguments + * or when a required argument or field is not provided. + * + * + * @public + * @category Error + */ +export declare class MongoInvalidArgumentError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { + cause?: Error; + }); + get name(): string; +} + +/** + * A error generated when the user attempts to authenticate + * via Kerberos, but fails to connect to the Kerberos client. + * + * @public + * @category Error + */ +export declare class MongoKerberosError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** @public */ +export declare const MongoLoggableComponent: Readonly<{ + readonly COMMAND: "command"; + readonly TOPOLOGY: "topology"; + readonly SERVER_SELECTION: "serverSelection"; + readonly CONNECTION: "connection"; + readonly CLIENT: "client"; +}>; + +/** @public */ +export declare type MongoLoggableComponent = (typeof MongoLoggableComponent)[keyof typeof MongoLoggableComponent]; + +/* Excluded from this release type: MongoLogger */ + +/* Excluded from this release type: MongoLoggerEnvOptions */ + +/* Excluded from this release type: MongoLoggerMongoClientOptions */ + +/* Excluded from this release type: MongoLoggerOptions */ + +/** + * An error generated when the user fails to provide authentication credentials before attempting + * to connect to a mongo server instance. + * + * + * @public + * @category Error + */ +export declare class MongoMissingCredentialsError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * An error generated when a required module or dependency is not present in the local environment + * + * @public + * @category Error + */ +export declare class MongoMissingDependencyError extends MongoAPIError { + dependencyName: string; + /** @remarks This property is assigned in the `Error` constructor. */ + cause: Error; + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options: { + cause: Error; + dependencyName: string; + }); + get name(): string; +} + +/** + * An error indicating an issue with the network, including TCP errors and timeouts. + * @public + * @category Error + */ +export declare class MongoNetworkError extends MongoError { + /* Excluded from this release type: beforeHandshake */ + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: MongoNetworkErrorOptions); + get name(): string; +} + +/** @public */ +export declare interface MongoNetworkErrorOptions { + /** Indicates the timeout happened before a connection handshake completed */ + beforeHandshake?: boolean; + cause?: Error; +} + +/** + * An error indicating a network timeout occurred + * @public + * @category Error + * + * @privateRemarks + * mongodb-client-encryption has a dependency on this error with an instanceof check + */ +export declare class MongoNetworkTimeoutError extends MongoNetworkError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: MongoNetworkErrorOptions); + get name(): string; +} + +/** + * An error thrown when the user attempts to operate on a database or collection through a MongoClient + * that has not yet successfully called the "connect" method + * + * @public + * @category Error + */ +export declare class MongoNotConnectedError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * A error generated when the user attempts to authenticate + * via OIDC callbacks, but fails. + * + * @public + * @category Error + */ +export declare class MongoOIDCError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * @public + * @category Error + * + * The `MongoOperationTimeoutError` class represents an error that occurs when an operation could not be completed within the specified `timeoutMS`. + * It is generated by the driver in support of the "client side operation timeout" feature so inherits from `MongoDriverError`. + * When `timeoutMS` is enabled `MongoServerError`s relating to `MaxTimeExpired` errors will be converted to `MongoOperationTimeoutError` + * + * @example + * ```ts + * try { + * await blogs.insertOne(blogPost, { timeoutMS: 60_000 }) + * } catch (error) { + * if (error instanceof MongoOperationTimeoutError) { + * console.log(`Oh no! writer's block!`, error); + * } + * } + * ``` + */ +export declare class MongoOperationTimeoutError extends MongoDriverError { + get name(): string; +} + +/** + * Parsed Mongo Client Options. + * + * User supplied options are documented by `MongoClientOptions`. + * + * **NOTE:** The client's options parsing is subject to change to support new features. + * This type is provided to aid with inspection of options after parsing, it should not be relied upon programmatically. + * + * Options are sourced from: + * - connection string + * - options object passed to the MongoClient constructor + * - file system (ex. tls settings) + * - environment variables + * - DNS SRV records and TXT records + * + * Not all options may be present after client construction as some are obtained from asynchronous operations. + * + * @public + */ +export declare interface MongoOptions extends Required>, SupportedNodeConnectionOptions { + appName?: string; + hosts: HostAddress[]; + srvHost?: string; + credentials?: MongoCredentials; + readPreference: ReadPreference; + readConcern: ReadConcern; + loadBalanced: boolean; + directConnection: boolean; + serverApi: ServerApi; + compressors: CompressorName[]; + writeConcern: WriteConcern; + dbName: string; + /* Excluded from this release type: metadata */ + /* Excluded from this release type: autoEncrypter */ + /* Excluded from this release type: tokenCache */ + proxyHost?: string; + proxyPort?: number; + proxyUsername?: string; + proxyPassword?: string; + serverMonitoringMode: ServerMonitoringMode; + /* Excluded from this release type: connectionType */ + /* Excluded from this release type: authProviders */ + /* Excluded from this release type: encrypter */ + /* Excluded from this release type: userSpecifiedAuthSource */ + /* Excluded from this release type: userSpecifiedReplicaSet */ + /** + * # NOTE ABOUT TLS Options + * + * If `tls` is provided as an option, it is equivalent to setting the `ssl` option. + * + * NodeJS native TLS options are passed through to the socket and retain their original types. + * + * ### Additional options: + * + * | nodejs native option | driver spec equivalent option name | driver option type | + * |:----------------------|:----------------------------------------------|:-------------------| + * | `ca` | `tlsCAFile` | `string` | + * | `crl` | `tlsCRLFile` | `string` | + * | `cert` | `tlsCertificateKeyFile` | `string` | + * | `key` | `tlsCertificateKeyFile` | `string` | + * | `passphrase` | `tlsCertificateKeyFilePassword` | `string` | + * | `rejectUnauthorized` | `tlsAllowInvalidCertificates` | `boolean` | + * | `checkServerIdentity` | `tlsAllowInvalidHostnames` | `boolean` | + * | see note below | `tlsInsecure` | `boolean` | + * + * If `tlsInsecure` is set to `true`, then it will set the node native options `checkServerIdentity` + * to a no-op and `rejectUnauthorized` to `false`. + * + * If `tlsInsecure` is set to `false`, then it will set the node native options `checkServerIdentity` + * to a no-op and `rejectUnauthorized` to the inverse value of `tlsAllowInvalidCertificates`. If + * `tlsAllowInvalidCertificates` is not set, then `rejectUnauthorized` will be set to `true`. + * + * ### Note on `tlsCAFile`, `tlsCertificateKeyFile` and `tlsCRLFile` + * + * The files specified by the paths passed in to the `tlsCAFile`, `tlsCertificateKeyFile` and `tlsCRLFile` + * fields are read lazily on the first call to `MongoClient.connect`. Once these files have been read and + * the `ca`, `cert`, `crl` and `key` fields are populated, they will not be read again on subsequent calls to + * `MongoClient.connect`. As a result, until the first call to `MongoClient.connect`, the `ca`, + * `cert`, `crl` and `key` fields will be undefined. + */ + tls: boolean; + tlsCAFile?: string; + tlsCRLFile?: string; + tlsCertificateKeyFile?: string; + /* Excluded from this release type: mongoLoggerOptions */ + /* Excluded from this release type: mongodbLogPath */ + timeoutMS?: number; + /* Excluded from this release type: __skipPingOnConnect */ + /* Excluded from this release type: runtime */ +} + +/** + * An error used when attempting to parse a value (like a connection string) + * @public + * @category Error + */ +export declare class MongoParseError extends MongoDriverError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * An error generated when the driver encounters unexpected input + * or reaches an unexpected/invalid internal state. + * + * @privateRemarks + * Should **never** be directly instantiated. + * + * @public + * @category Error + */ +export declare class MongoRuntimeError extends MongoDriverError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { + cause?: Error; + }); + get name(): string; +} + +/** + * An error generated when an attempt is made to operate + * on a closed/closing server. + * + * @public + * @category Error + */ +export declare class MongoServerClosedError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message?: string); + get name(): string; +} + +/** + * An error coming from the mongo server + * + * @public + * @category Error + */ +export declare class MongoServerError extends MongoError { + /** Raw error result document returned by server. */ + errorResponse: ErrorDescription; + codeName?: string; + writeConcernError?: Document; + errInfo?: Document; + ok?: number; + [key: string]: any; + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: ErrorDescription); + get name(): string; +} + +/** + * An error signifying a client-side server selection error + * @public + * @category Error + */ +export declare class MongoServerSelectionError extends MongoSystemError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, reason: TopologyDescription); + get name(): string; +} + +/** + * An error generated when a primary server is marked stale, never directly thrown + * + * @public + * @category Error + */ +export declare class MongoStalePrimaryError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { + cause?: Error; + }); + get name(): string; +} + +/** + * An error signifying a general system issue + * @public + * @category Error + */ +export declare class MongoSystemError extends MongoError { + /** An optional reason context, such as an error saved during flow of monitoring and selecting servers */ + reason?: TopologyDescription; + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, reason: TopologyDescription); + get name(): string; +} + +/** + * An error thrown when the user calls a function or method not supported on a tailable cursor + * + * @public + * @category Error + */ +export declare class MongoTailableCursorError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message?: string); + get name(): string; +} + +/** + * An error generated when an attempt is made to operate on a + * dropped, or otherwise unavailable, database. + * + * @public + * @category Error + */ +export declare class MongoTopologyClosedError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message?: string); + get name(): string; +} + +/** + * An error generated when the user makes a mistake in the usage of transactions. + * (e.g. attempting to commit a transaction with a readPreference other than primary) + * + * @public + * @category Error + */ +export declare class MongoTransactionError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string); + get name(): string; +} + +/** + * An error generated when a **parsable** unexpected response comes from the server. + * This is generally an error where the driver in a state expecting a certain behavior to occur in + * the next message from MongoDB but it receives something else. + * This error **does not** represent an issue with wire message formatting. + * + * #### Example + * When an operation fails, it is the driver's job to retry it. It must perform serverSelection + * again to make sure that it attempts the operation against a server in a good state. If server + * selection returns a server that does not support retryable operations, this error is used. + * This scenario is unlikely as retryable support would also have been determined on the first attempt + * but it is possible the state change could report a selectable server that does not support retries. + * + * @public + * @category Error + */ +export declare class MongoUnexpectedServerResponseError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { + cause?: Error; + }); + get name(): string; +} + +/** + * An error thrown when the server reports a writeConcernError + * @public + * @category Error + */ +export declare class MongoWriteConcernError extends MongoServerError { + /** The result document */ + result: Document; + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(result: WriteConcernErrorResult); + get name(): string; +} + +/* Excluded from this release type: Monitor */ + +/** @public */ +export declare type MonitorEvents = { + serverHeartbeatStarted(event: ServerHeartbeatStartedEvent): void; + serverHeartbeatSucceeded(event: ServerHeartbeatSucceededEvent): void; + serverHeartbeatFailed(event: ServerHeartbeatFailedEvent): void; + resetServer(error?: MongoError): void; + resetConnectionPool(): void; + close(): void; +} & EventEmitterWithState; + +/* Excluded from this release type: MonitorInterval */ + +/* Excluded from this release type: MonitorIntervalOptions */ + +/** @public */ +export declare interface MonitorOptions extends Omit { + connectTimeoutMS: number; + heartbeatFrequencyMS: number; + minHeartbeatFrequencyMS: number; + serverMonitoringMode: ServerMonitoringMode; +} + +/* Excluded from this release type: MonitorPrivate */ + +/** + * @public + * returns tuple of strings (keys to be joined on '.') that represent every path into a schema + * https://www.mongodb.com/docs/manual/tutorial/query-embedded-documents/ + * + * @remarks + * Through testing we determined that a depth of 8 is safe for the typescript compiler + * and provides reasonable compilation times. This number is otherwise not special and + * should be changed if issues are found with this level of checking. Beyond this + * depth any helpers that make use of NestedPaths should devolve to not asserting any + * type safety on the input. + */ +export declare type NestedPaths = Depth['length'] extends 8 ? [] : Type extends string | number | bigint | boolean | Date | RegExp | Buffer | Uint8Array | ((...args: any[]) => any) | { + _bsontype: string; +} ? [] : Type extends ReadonlyArray ? [] | [number, ...NestedPaths] : Type extends Map ? [string] : Type extends object ? { + [Key in Extract]: Type[Key] extends Type ? [Key] : Type extends Type[Key] ? [Key] : Type[Key] extends ReadonlyArray ? Type extends ArrayType ? [Key] : ArrayType extends Type ? [Key] : [ + Key, + ...NestedPaths + ] : // child is not structured the same as the parent + [ + Key, + ...NestedPaths + ] | [Key]; +}[Extract] : []; + +/** + * @public + * returns keys (strings) for every path into a schema with a value of type + * https://www.mongodb.com/docs/manual/tutorial/query-embedded-documents/ + */ +export declare type NestedPathsOfType = KeysOfAType<{ + [Property in Join, '.'>]: PropertyType; +}, Type>; + +/** + * @public + * A type that extends Document but forbids anything that "looks like" an object id. + */ +export declare type NonObjectIdLikeDocument = { + [key in keyof ObjectIdLike]?: never; +} & Document; + +/** It avoids using fields with not acceptable types @public */ +export declare type NotAcceptedFields = { + readonly [key in KeysOfOtherType]?: never; +}; + +/** @public */ +export declare type NumericType = IntegerType | Decimal128 | Double; + +export { ObjectId } + +/** + * The signature of the human or machine callback functions. + * @public + */ +export declare type OIDCCallbackFunction = (params: OIDCCallbackParams) => Promise; + +/** + * The parameters that the driver provides to the user supplied + * human or machine callback. + * + * The version number is used to communicate callback API changes that are not breaking but that + * users may want to know about and review their implementation. Users may wish to check the version + * number and throw an error if their expected version number and the one provided do not match. + * @public + */ +export declare interface OIDCCallbackParams { + /** Optional username. */ + username?: string; + /** The context in which to timeout the OIDC callback. */ + timeoutContext: AbortSignal; + /** The current OIDC API version. */ + version: 1; + /** The IdP information returned from the server. */ + idpInfo?: IdPInfo; + /** The refresh token, if applicable, to be used by the callback to request a new token from the issuer. */ + refreshToken?: string; + /** The token audience for GCP and Azure. */ + tokenAudience?: string; +} + +/** + * The response required to be returned from the machine or + * human callback workflows' callback. + * @public + */ +export declare interface OIDCResponse { + /** The OIDC access token. */ + accessToken: string; + /** The time when the access token expires. For future use. */ + expiresInSeconds?: number; + /** The refresh token, if applicable, to be used by the callback to request a new token from the issuer. */ + refreshToken?: string; +} + +/* Excluded from this release type: OnDemandDocument */ + +/* Excluded from this release type: OnDemandDocumentDeserializeOptions */ + +/** @public */ +export declare type OneOrMore = T | ReadonlyArray; + +/** @public */ +export declare type OnlyFieldsOfType = IsAny : Record, AcceptedFields & NotAcceptedFields & Record>; + +/* Excluded from this release type: OpCompressedRequest */ + +/* Excluded from this release type: OpCompressesRequestOptions */ + +/** @public */ +export declare interface OperationOptions extends BSONSerializeOptions { + /** Specify ClientSession for this command */ + session?: ClientSession; + willRetryWrite?: boolean; + /** The preferred read preference (ReadPreference.primary, ReadPreference.primary_preferred, ReadPreference.secondary, ReadPreference.secondary_preferred, ReadPreference.nearest). */ + readPreference?: ReadPreferenceLike; + /* Excluded from this release type: bypassPinningCheck */ + /* Excluded from this release type: omitMaxTimeMS */ + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; +} + +/* Excluded from this release type: OperationParent */ + +/** + * Represents a specific point in time on a server. Can be retrieved by using `db.command()` + * @public + * @see https://www.mongodb.com/docs/manual/reference/method/db.runCommand/#response + */ +export declare type OperationTime = Timestamp; + +/* Excluded from this release type: OpMsgOptions */ + +/* Excluded from this release type: OpMsgRequest */ + +/* Excluded from this release type: OpMsgResponse */ + +/* Excluded from this release type: OpQueryOptions */ + +/* Excluded from this release type: OpQueryRequest */ + +/* Excluded from this release type: OpReply */ + +/** + * Add an optional _id field to an object shaped type + * @public + */ +export declare type OptionalId = EnhancedOmit & { + _id?: InferIdType; +}; + +/** + * Adds an optional _id field to an object shaped type, unless the _id field is required on that type. + * In the case _id is required, this method continues to require_id. + * + * @public + * + * @privateRemarks + * `ObjectId extends TSchema['_id']` is a confusing ordering at first glance. Rather than ask + * `TSchema['_id'] extends ObjectId` which translated to "Is the _id property ObjectId?" + * we instead ask "Does ObjectId look like (have the same shape) as the _id?" + */ +export declare type OptionalUnlessRequiredId = TSchema extends { + _id: any; +} ? TSchema : OptionalId; + +/** @public */ +export declare class OrderedBulkOperation extends BulkOperationBase { + /* Excluded from this release type: __constructor */ + addToOperationsList(batchType: BatchType, document: Document | UpdateStatement | DeleteStatement): this; +} + +/** + * @public + * @experimental + * + * Represents the set of dependencies that the driver uses from the [Node.js OS module](https://nodejs.org/api/os.html). + */ +export declare type OsAdapter = Pick; + +/** @public */ +export declare interface PkFactory { + createPk(): any; +} + +/* Excluded from this release type: PoolState */ + +/** @public */ +export declare const ProfilingLevel: Readonly<{ + readonly off: "off"; + readonly slowOnly: "slow_only"; + readonly all: "all"; +}>; + +/** @public */ +export declare type ProfilingLevel = (typeof ProfilingLevel)[keyof typeof ProfilingLevel]; + +/** @public */ +export declare type ProfilingLevelOptions = Omit; + +/** @public */ +export declare type PropertyType = string extends Property ? unknown : Property extends keyof Type ? Type[Property] : Property extends `${number}` ? Type extends ReadonlyArray ? ArrayType : unknown : Property extends `${infer Key}.${infer Rest}` ? Key extends `${number}` ? Type extends ReadonlyArray ? PropertyType : unknown : Key extends keyof Type ? Type[Key] extends Map ? MapType : PropertyType : unknown : unknown; + +/** @public */ +export declare interface ProxyOptions { + proxyHost?: string; + proxyPort?: number; + proxyUsername?: string; + proxyPassword?: string; +} + +/** @public */ +export declare type PullAllOperator = ({ + readonly [key in KeysOfAType>]?: TSchema[key]; +} & NotAcceptedFields>) & { + readonly [key: string]: ReadonlyArray; +}; + +/** @public */ +export declare type PullOperator = ({ + readonly [key in KeysOfAType>]?: Partial> | FilterOperations>; +} & NotAcceptedFields>) & { + readonly [key: string]: FilterOperators | any; +}; + +/** @public */ +export declare type PushOperator = ({ + readonly [key in KeysOfAType>]?: Flatten | ArrayOperator>>; +} & NotAcceptedFields>) & { + readonly [key: string]: ArrayOperator | any; +}; + +/** + * @public + * RangeOptions specifies index options for a Queryable Encryption field supporting "range" queries. + * min, max, sparsity, trimFactor and range must match the values set in the encryptedFields of the destination collection. + * For double and decimal128, min/max/precision must all be set, or all be unset. + */ +export declare interface RangeOptions { + /** min is the minimum value for the encrypted index. Required if precision is set. */ + min?: any; + /** max is the minimum value for the encrypted index. Required if precision is set. */ + max?: any; + /** sparsity may be used to tune performance. must be non-negative. When omitted, a default value is used. */ + sparsity?: Long | bigint; + /** trimFactor may be used to tune performance. must be non-negative. When omitted, a default value is used. */ + trimFactor?: Int32 | number; + precision?: number; +} + +/** + * The MongoDB ReadConcern, which allows for control of the consistency and isolation properties + * of the data read from replica sets and replica set shards. + * @public + * + * @see https://www.mongodb.com/docs/manual/reference/read-concern/index.html + */ +export declare class ReadConcern { + level: ReadConcernLevel | string; + /** Constructs a ReadConcern from the read concern level.*/ + constructor(level: ReadConcernLevel); + /** + * Construct a ReadConcern given an options object. + * + * @param options - The options object from which to extract the write concern. + */ + static fromOptions(options?: { + readConcern?: ReadConcernLike; + level?: ReadConcernLevel; + }): ReadConcern | undefined; + static get MAJORITY(): 'majority'; + static get AVAILABLE(): 'available'; + static get LINEARIZABLE(): 'linearizable'; + static get SNAPSHOT(): 'snapshot'; + toJSON(): Document; +} + +/** @public */ +export declare const ReadConcernLevel: Readonly<{ + readonly local: "local"; + readonly majority: "majority"; + readonly linearizable: "linearizable"; + readonly available: "available"; + readonly snapshot: "snapshot"; +}>; + +/** @public */ +export declare type ReadConcernLevel = (typeof ReadConcernLevel)[keyof typeof ReadConcernLevel]; + +/** @public */ +export declare type ReadConcernLike = ReadConcern | { + level: ReadConcernLevel; +} | ReadConcernLevel; + +/** + * The **ReadPreference** class is a class that represents a MongoDB ReadPreference and is + * used to construct connections. + * @public + * + * @see https://www.mongodb.com/docs/manual/core/read-preference/ + */ +export declare class ReadPreference { + mode: ReadPreferenceMode; + tags?: TagSet[]; + hedge?: HedgeOptions; + maxStalenessSeconds?: number; + static PRIMARY: "primary"; + static PRIMARY_PREFERRED: "primaryPreferred"; + static SECONDARY: "secondary"; + static SECONDARY_PREFERRED: "secondaryPreferred"; + static NEAREST: "nearest"; + static primary: ReadPreference; + static primaryPreferred: ReadPreference; + static secondary: ReadPreference; + static secondaryPreferred: ReadPreference; + static nearest: ReadPreference; + /** + * @param mode - A string describing the read preference mode (primary|primaryPreferred|secondary|secondaryPreferred|nearest) + * @param tags - A tag set used to target reads to members with the specified tag(s). tagSet is not available if using read preference mode primary. + * @param options - Additional read preference options + */ + constructor(mode: ReadPreferenceMode, tags?: TagSet[], options?: ReadPreferenceOptions); + get preference(): ReadPreferenceMode; + static fromString(mode: string): ReadPreference; + /** + * Construct a ReadPreference given an options object. + * + * @param options - The options object from which to extract the read preference. + */ + static fromOptions(options?: ReadPreferenceFromOptions): ReadPreference | undefined; + /** + * Replaces options.readPreference with a ReadPreference instance + */ + static translate(options: ReadPreferenceLikeOptions): ReadPreferenceLikeOptions; + /** + * Validate if a mode is legal + * + * @param mode - The string representing the read preference mode. + */ + static isValid(mode: string): boolean; + /** + * Validate if a mode is legal + * + * @param mode - The string representing the read preference mode. + */ + isValid(mode?: string): boolean; + /** + * Indicates that this readPreference needs the "SecondaryOk" bit when sent over the wire + * @see https://www.mongodb.com/docs/manual/reference/mongodb-wire-protocol/#op-query + */ + secondaryOk(): boolean; + /** + * Check if the two ReadPreferences are equivalent + * + * @param readPreference - The read preference with which to check equality + */ + equals(readPreference: ReadPreference): boolean; + /** Return JSON representation */ + toJSON(): Document; +} + +/** @public */ +export declare interface ReadPreferenceFromOptions extends ReadPreferenceLikeOptions { + session?: ClientSession; + readPreferenceTags?: TagSet[]; + hedge?: HedgeOptions; +} + +/** @public */ +export declare type ReadPreferenceLike = ReadPreference | ReadPreferenceMode; + +/** @public */ +export declare interface ReadPreferenceLikeOptions extends ReadPreferenceOptions { + readPreference?: ReadPreferenceLike | { + mode?: ReadPreferenceMode; + preference?: ReadPreferenceMode; + tags?: TagSet[]; + maxStalenessSeconds?: number; + }; +} + +/** @public */ +export declare const ReadPreferenceMode: Readonly<{ + readonly primary: "primary"; + readonly primaryPreferred: "primaryPreferred"; + readonly secondary: "secondary"; + readonly secondaryPreferred: "secondaryPreferred"; + readonly nearest: "nearest"; +}>; + +/** @public */ +export declare type ReadPreferenceMode = (typeof ReadPreferenceMode)[keyof typeof ReadPreferenceMode]; + +/** @public */ +export declare interface ReadPreferenceOptions { + /** Max secondary read staleness in seconds, Minimum value is 90 seconds.*/ + maxStalenessSeconds?: number; + /** Server mode in which the same query is dispatched in parallel to multiple replica set members. */ + hedge?: HedgeOptions; +} + +/** @public */ +export declare type RegExpOrString = T extends string ? BSONRegExp | RegExp | T : T; + +/** @public */ +export declare type RemoveUserOptions = Omit; + +/** @public */ +export declare interface RenameOptions extends Omit { + /** Drop the target name collection if it previously exists. */ + dropTarget?: boolean; + /** + * @deprecated + * + * This option has been dead code since at least Node driver version 4.x. It will + * be removed in a future major release. + */ + new_collection?: boolean; +} + +/** @public */ +export declare interface ReplaceOneModel { + /** The filter that specifies which document to replace. In the case of multiple matches, the first document matched is replaced. */ + filter: Filter; + /** The document with which to replace the matched document. */ + replacement: WithoutId; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; + /** When true, creates a new document if no document matches the query. */ + upsert?: boolean; + /** Specifies the sort order for the documents matched by the filter. */ + sort?: Sort; +} + +/** @public */ +export declare interface ReplaceOptions extends CommandOperationOptions { + /** If true, allows the write to opt-out of document level validation */ + bypassDocumentValidation?: boolean; + /** Specifies a collation */ + collation?: CollationOptions; + /** Specify that the update query should only consider plans using the hinted index */ + hint?: string | Document; + /** When true, creates a new document if no document matches the query */ + upsert?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /** Specifies the sort order for the documents matched by the filter. */ + sort?: Sort; +} + +/** + * Represents the logical starting point for a new ChangeStream or resuming a ChangeStream on the server. + * @see https://www.mongodb.com/docs/manual/changeStreams/#std-label-change-stream-resume + * @public + */ +export declare type ResumeToken = unknown; + +/** @public */ +export declare const ReturnDocument: Readonly<{ + readonly BEFORE: "before"; + readonly AFTER: "after"; +}>; + +/** @public */ +export declare type ReturnDocument = (typeof ReturnDocument)[keyof typeof ReturnDocument]; + +/** @public */ +export declare interface RootFilterOperators extends Document { + $and?: Filter[]; + $nor?: Filter[]; + $or?: Filter[]; + $text?: { + $search: string; + $language?: string; + $caseSensitive?: boolean; + $diacriticSensitive?: boolean; + }; + $where?: string | ((this: TSchema) => boolean); + $comment?: string | Document; +} + +/* Excluded from this release type: RTTPinger */ + +/* Excluded from this release type: RTTPingerOptions */ + +/* Excluded from this release type: RTTSampler */ + +/** @public */ +export declare class RunCommandCursor extends AbstractCursor { + readonly command: Readonly>; + readonly getMoreOptions: { + comment?: any; + maxAwaitTimeMS?: number; + batchSize?: number; + }; + /** + * Controls the `getMore.comment` field + * @param comment - any BSON value + */ + setComment(comment: any): this; + /** + * Controls the `getMore.maxTimeMS` field. Only valid when cursor is tailable await + * @param maxTimeMS - the number of milliseconds to wait for new data + */ + setMaxTimeMS(maxTimeMS: number): this; + /** + * Controls the `getMore.batchSize` field + * @param batchSize - the number documents to return in the `nextBatch` + */ + setBatchSize(batchSize: number): this; + /** Unsupported for RunCommandCursor */ + clone(): never; + /** Unsupported for RunCommandCursor: readConcern must be configured directly on command document */ + withReadConcern(_: ReadConcernLike): never; + /** Unsupported for RunCommandCursor: various cursor flags must be configured directly on command document */ + addCursorFlag(_: string, __: boolean): never; + /** + * Unsupported for RunCommandCursor: maxTimeMS must be configured directly on command document + */ + maxTimeMS(_: number): never; + /** Unsupported for RunCommandCursor: batchSize must be configured directly on command document */ + batchSize(_: number): never; + /* Excluded from this release type: db */ + /* Excluded from this release type: __constructor */ + /* Excluded from this release type: _initialize */ + /* Excluded from this release type: getMore */ +} + +/** @public */ +export declare type RunCommandOptions = { + /** Specify ClientSession for this command */ + session?: ClientSession; + /** The read preference */ + readPreference?: ReadPreferenceLike; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; + /* Excluded from this release type: omitMaxTimeMS */ + /* Excluded from this release type: bypassPinningCheck */ +} & BSONSerializeOptions & Abortable; + +/** @public */ +export declare type RunCursorCommandOptions = { + readPreference?: ReadPreferenceLike; + session?: ClientSession; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error. Note that if + * `maxTimeMS` is provided in the command in addition to setting `timeoutMS` in the options, then + * the original value of `maxTimeMS` will be overwritten. + */ + timeoutMS?: number; + /** + * @public + * @experimental + * Specifies how `timeoutMS` is applied to the cursor. Can be either `'cursorLifeTime'` or `'iteration'` + * When set to `'iteration'`, the deadline specified by `timeoutMS` applies to each call of + * `cursor.next()`. + * When set to `'cursorLifetime'`, the deadline applies to the life of the entire cursor. + * + * Depending on the type of cursor being used, this option has different default values. + * For non-tailable cursors, this value defaults to `'cursorLifetime'` + * For tailable cursors, this value defaults to `'iteration'` since tailable cursors, by + * definition can have an arbitrarily long lifetime. + * + * @example + * ```ts + * const cursor = collection.find({}, {timeoutMS: 100, timeoutMode: 'iteration'}); + * for await (const doc of cursor) { + * // process doc + * // This will throw a timeout error if any of the iterator's `next()` calls takes more than 100ms, but + * // will continue to iterate successfully otherwise, regardless of the number of batches. + * } + * ``` + * + * @example + * ```ts + * const cursor = collection.find({}, { timeoutMS: 1000, timeoutMode: 'cursorLifetime' }); + * const docs = await cursor.toArray(); // This entire line will throw a timeout error if all batches are not fetched and returned within 1000ms. + * ``` + */ + timeoutMode?: CursorTimeoutMode; + tailable?: boolean; + awaitData?: boolean; +} & BSONSerializeOptions; + +/* Excluded from this release type: Runtime */ + +/** + * @public + * @experimental + * + * This type represents the set of dependencies that the driver needs from the Javascript runtime in order to function. + */ +export declare interface RuntimeAdapters { + os?: OsAdapter; +} + +/** @public */ +export declare type SchemaMember = { + [P in keyof T]?: V; +} | { + [key: string]: V; +}; + +/** + * @public + */ +export declare interface SearchIndexDescription extends Document { + /** The name of the index. */ + name?: string; + /** The index definition. */ + definition: Document; + /** The type of the index. Currently `search` or `vectorSearch` are supported. */ + type?: string; +} + +/** @public */ +export declare interface SelectServerOptions { + readPreference?: ReadPreferenceLike; + /** How long to block for server selection before throwing an error */ + serverSelectionTimeoutMS?: number; + session?: ClientSession; + operationName: string; + /* Excluded from this release type: deprioritizedServers */ + /* Excluded from this release type: timeoutContext */ +} + +export { serialize } + +/* Excluded from this release type: Server */ + +/* Excluded from this release type: SERVER_CLOSED */ + +/* Excluded from this release type: SERVER_DESCRIPTION_CHANGED */ + +/* Excluded from this release type: SERVER_HEARTBEAT_FAILED */ + +/* Excluded from this release type: SERVER_HEARTBEAT_STARTED */ + +/* Excluded from this release type: SERVER_HEARTBEAT_SUCCEEDED */ + +/* Excluded from this release type: SERVER_OPENING */ + +/* Excluded from this release type: SERVER_SELECTION_FAILED */ + +/* Excluded from this release type: SERVER_SELECTION_STARTED */ + +/* Excluded from this release type: SERVER_SELECTION_SUCCEEDED */ + +/** @public */ +export declare interface ServerApi { + version: ServerApiVersion; + strict?: boolean; + deprecationErrors?: boolean; +} + +/** @public */ +export declare const ServerApiVersion: Readonly<{ + readonly v1: "1"; +}>; + +/** @public */ +export declare type ServerApiVersion = (typeof ServerApiVersion)[keyof typeof ServerApiVersion]; + +/** + * Emitted when server is closed. + * @public + * @category Event + */ +export declare class ServerClosedEvent { + /** A unique identifier for the topology */ + topologyId: number; + /** The address (host/port pair) of the server */ + address: string; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/* Excluded from this release type: ServerCommandOptions */ + +/** + * The client's view of a single server, based on the most recent hello outcome. + * + * Internal type, not meant to be directly instantiated + * @public + */ +export declare class ServerDescription { + address: string; + type: ServerType; + hosts: string[]; + passives: string[]; + arbiters: string[]; + tags: TagSet; + error: MongoError | null; + topologyVersion: TopologyVersion | null; + minWireVersion: number; + maxWireVersion: number; + roundTripTime: number; + /** The minimum measurement of the last 10 measurements of roundTripTime that have been collected */ + minRoundTripTime: number; + lastUpdateTime: number; + lastWriteDate: number; + me: string | null; + primary: string | null; + setName: string | null; + setVersion: number | null; + electionId: ObjectId | null; + logicalSessionTimeoutMinutes: number | null; + /** The max message size in bytes for the server. */ + maxMessageSizeBytes: number | null; + /** The max number of writes in a bulk write command. */ + maxWriteBatchSize: number | null; + /** The max bson object size. */ + maxBsonObjectSize: number | null; + /** Indicates server is a mongocryptd instance. */ + iscryptd: boolean; + $clusterTime?: ClusterTime; + /* Excluded from this release type: __constructor */ + get hostAddress(): HostAddress; + get allHosts(): string[]; + /** Is this server available for reads*/ + get isReadable(): boolean; + /** Is this server data bearing */ + get isDataBearing(): boolean; + /** Is this server available for writes */ + get isWritable(): boolean; + get host(): string; + get port(): number; + /** + * Determines if another `ServerDescription` is equal to this one per the rules defined in the SDAM specification. + * @see https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md + */ + equals(other?: ServerDescription | null): boolean; +} + +/** + * Emitted when server description changes, but does NOT include changes to the RTT. + * @public + * @category Event + */ +export declare class ServerDescriptionChangedEvent { + /** A unique identifier for the topology */ + topologyId: number; + /** The address (host/port pair) of the server */ + address: string; + /** The previous server description */ + previousDescription: ServerDescription; + /** The new server description */ + newDescription: ServerDescription; + name: "serverDescriptionChanged"; + /* Excluded from this release type: __constructor */ +} + +/* Excluded from this release type: ServerDescriptionOptions */ + +/** @public */ +export declare type ServerEvents = { + serverHeartbeatStarted(event: ServerHeartbeatStartedEvent): void; + serverHeartbeatSucceeded(event: ServerHeartbeatSucceededEvent): void; + serverHeartbeatFailed(event: ServerHeartbeatFailedEvent): void; + /* Excluded from this release type: connect */ + descriptionReceived(description: ServerDescription): void; + closed(): void; + ended(): void; +} & ConnectionPoolEvents & EventEmitterWithState; + +/** + * Emitted when the server monitor’s hello fails, either with an “ok: 0” or a socket exception. + * @public + * @category Event + */ +export declare class ServerHeartbeatFailedEvent { + /** The connection id for the command */ + connectionId: string; + /** The execution time of the event in ms */ + duration: number; + /** The command failure */ + failure: Error; + /** Is true when using the streaming protocol */ + awaited: boolean; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** + * Emitted when the server monitor’s hello command is started - immediately before + * the hello command is serialized into raw BSON and written to the socket. + * + * @public + * @category Event + */ +export declare class ServerHeartbeatStartedEvent { + /** The connection id for the command */ + connectionId: string; + /** Is true when using the streaming protocol */ + awaited: boolean; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** + * Emitted when the server monitor’s hello succeeds. + * @public + * @category Event + */ +export declare class ServerHeartbeatSucceededEvent { + /** The connection id for the command */ + connectionId: string; + /** The execution time of the event in ms */ + duration: number; + /** The command reply */ + reply: Document; + /** Is true when using the streaming protocol */ + awaited: boolean; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** @public */ +export declare const ServerMonitoringMode: Readonly<{ + readonly auto: "auto"; + readonly poll: "poll"; + readonly stream: "stream"; +}>; + +/** @public */ +export declare type ServerMonitoringMode = (typeof ServerMonitoringMode)[keyof typeof ServerMonitoringMode]; + +/** + * Emitted when server is initialized. + * @public + * @category Event + */ +export declare class ServerOpeningEvent { + /** A unique identifier for the topology */ + topologyId: number; + /** The address (host/port pair) of the server */ + address: string; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/* Excluded from this release type: ServerOptions */ + +/* Excluded from this release type: ServerPrivate */ + +/* Excluded from this release type: ServerSelectionCallback */ + +/* Excluded from this release type: ServerSelectionEvent */ + +/* Excluded from this release type: ServerSelectionFailedEvent */ + +/* Excluded from this release type: ServerSelectionRequest */ + +/* Excluded from this release type: ServerSelectionStartedEvent */ + +/* Excluded from this release type: ServerSelectionSucceededEvent */ + +/* Excluded from this release type: ServerSelector */ + +/** + * Reflects the existence of a session on the server. Can be reused by the session pool. + * WARNING: not meant to be instantiated directly. For internal use only. + * @public + */ +export declare class ServerSession { + id: ServerSessionId; + lastUse: number; + txnNumber: number; + isDirty: boolean; + /* Excluded from this release type: __constructor */ + /** + * Determines if the server session has timed out. + * + * @param sessionTimeoutMinutes - The server's "logicalSessionTimeoutMinutes" + */ + hasTimedOut(sessionTimeoutMinutes: number): boolean; +} + +/** @public */ +export declare type ServerSessionId = { + id: Binary; +}; + +/* Excluded from this release type: ServerSessionPool */ + +/** + * An enumeration of server types we know about + * @public + */ +export declare const ServerType: Readonly<{ + readonly Standalone: "Standalone"; + readonly Mongos: "Mongos"; + readonly PossiblePrimary: "PossiblePrimary"; + readonly RSPrimary: "RSPrimary"; + readonly RSSecondary: "RSSecondary"; + readonly RSArbiter: "RSArbiter"; + readonly RSOther: "RSOther"; + readonly RSGhost: "RSGhost"; + readonly Unknown: "Unknown"; + readonly LoadBalancer: "LoadBalancer"; +}>; + +/** @public */ +export declare type ServerType = (typeof ServerType)[keyof typeof ServerType]; + +/** @public */ +export declare type SetFields = ({ + readonly [key in KeysOfAType | undefined>]?: OptionalId> | AddToSetOperators>>>; +} & IsAny | undefined>>) & { + readonly [key: string]: AddToSetOperators | any; +}; + +/** @public */ +export declare type SetProfilingLevelOptions = Omit; + +/** + * @public + * Severity levels align with unix syslog. + * Most typical driver functions will log to debug. + */ +export declare const SeverityLevel: Readonly<{ + readonly EMERGENCY: "emergency"; + readonly ALERT: "alert"; + readonly CRITICAL: "critical"; + readonly ERROR: "error"; + readonly WARNING: "warn"; + readonly NOTICE: "notice"; + readonly INFORMATIONAL: "info"; + readonly DEBUG: "debug"; + readonly TRACE: "trace"; + readonly OFF: "off"; +}>; + +/** @public */ +export declare type SeverityLevel = (typeof SeverityLevel)[keyof typeof SeverityLevel]; + +/** @public */ +export declare type Sort = string | Exclude | ReadonlyArray | { + readonly [key: string]: SortDirection; +} | ReadonlyMap | ReadonlyArray | readonly [string, SortDirection]; + +/** @public */ +export declare type SortDirection = 1 | -1 | 'asc' | 'desc' | 'ascending' | 'descending' | { + readonly $meta: string; +}; + +/** Below stricter types were created for sort that correspond with type that the cmd takes */ +/** @public */ +export declare type SortDirectionForCmd = 1 | -1 | { + $meta: string; +}; + +/** @public */ +export declare type SortForCmd = Map; + +/* Excluded from this release type: SrvPoller */ + +/* Excluded from this release type: SrvPollerEvents */ + +/* Excluded from this release type: SrvPollerOptions */ + +/* Excluded from this release type: SrvPollingEvent */ + +/* Excluded from this release type: StateMachineExecutable */ + +/** @public */ +export declare type Stream = Socket | TLSSocket; + +/** @public */ +export declare class StreamDescription { + address: string; + type: ServerType; + minWireVersion?: number; + maxWireVersion?: number; + maxBsonObjectSize: number; + maxMessageSizeBytes: number; + maxWriteBatchSize: number; + compressors: CompressorName[]; + compressor?: CompressorName; + logicalSessionTimeoutMinutes?: number; + loadBalanced: boolean; + __nodejs_mock_server__?: boolean; + zlibCompressionLevel?: number; + serverConnectionId: bigint | null; + hello: Document | null; + constructor(address: string, options?: StreamDescriptionOptions); + receiveResponse(response: Document | null): void; + parseServerConnectionID(serverConnectionId: number | Double | bigint | Long): bigint; +} + +/** @public */ +export declare interface StreamDescriptionOptions { + compressors?: CompressorName[]; + logicalSessionTimeoutMinutes?: number; + loadBalanced: boolean; +} + +/** + * @public + * @experimental + */ +export declare type StrictFilter = Partial | ({ + [Property in Join, []>, '.'>]?: Condition, Property>>; +} & RootFilterOperators>); + +/** + * @public + * @experimental + */ +export declare type StrictMatchKeysAndValues = Readonly<{ + [Property in Join, '.'>]?: PropertyType; +} & { + [Property in `${NestedPathsOfType}.$${`[${string}]` | ''}`]?: ArrayElement>; +} & { + [Property in `${NestedPathsOfType[]>}.$${`[${string}]` | ''}.${string}`]?: any; +} & Document>; + +/** + * @public + * @experimental + */ +export declare type StrictUpdateFilter = { + $currentDate?: OnlyFieldsOfType; + $inc?: OnlyFieldsOfType; + $min?: StrictMatchKeysAndValues; + $max?: StrictMatchKeysAndValues; + $mul?: OnlyFieldsOfType; + $rename?: Record; + $set?: StrictMatchKeysAndValues; + $setOnInsert?: StrictMatchKeysAndValues; + $unset?: OnlyFieldsOfType; + $addToSet?: SetFields; + $pop?: OnlyFieldsOfType, 1 | -1>; + $pull?: PullOperator; + $push?: PushOperator; + $pullAll?: PullAllOperator; + $bit?: OnlyFieldsOfType; +} & Document; + +/** + * Options for a Queryable Encryption field supporting string queries. + * + * @public + */ +export declare interface StringQueryOptions { + /** Indicates that text indexes for this field are case sensitive */ + caseSensitive: boolean; + /** Indicates that text indexes for this field are diacritic sensitive. */ + diacriticSensitive: boolean; + prefix?: { + /** The maximum allowed query length. */ + strMaxQueryLength: Int32 | number; + /** The minimum allowed query length. */ + strMinQueryLength: Int32 | number; + }; + suffix?: { + /** The maximum allowed query length. */ + strMaxQueryLength: Int32 | number; + /** The minimum allowed query length. */ + strMinQueryLength: Int32 | number; + }; + substring?: { + /** The maximum allowed length to insert. */ + strMaxLength: Int32 | number; + /** The maximum allowed query length. */ + strMaxQueryLength: Int32 | number; + /** The minimum allowed query length. */ + strMinQueryLength: Int32 | number; + }; +} + +/** @public */ +export declare type SupportedNodeConnectionOptions = SupportedTLSConnectionOptions & SupportedTLSSocketOptions & SupportedSocketOptions; + +/** @public */ +export declare type SupportedSocketOptions = Pick; + +/** @public */ +export declare type SupportedTLSConnectionOptions = Pick; + +/** @public */ +export declare type SupportedTLSSocketOptions = Pick>; + +/** @public */ +export declare type TagSet = { + [key: string]: string; +}; + +/** + * @public + * @deprecated Use {@link StringQueryOptions} instead. + */ +export declare type TextQueryOptions = StringQueryOptions; + +/* Excluded from this release type: Timeout */ + +/* Excluded from this release type: TimeoutContext */ + +/* Excluded from this release type: TimeoutContextOptions */ + +/** @public + * Configuration options for timeseries collections + * @see https://www.mongodb.com/docs/manual/core/timeseries-collections/ + */ +export declare interface TimeSeriesCollectionOptions extends Document { + timeField: string; + metaField?: string; + granularity?: 'seconds' | 'minutes' | 'hours' | string; + bucketMaxSpanSeconds?: number; + bucketRoundingSeconds?: number; +} + +export { Timestamp } + +/* Excluded from this release type: TokenCache */ + +/* Excluded from this release type: Topology */ + +/* Excluded from this release type: TOPOLOGY_CLOSED */ + +/* Excluded from this release type: TOPOLOGY_DESCRIPTION_CHANGED */ + +/* Excluded from this release type: TOPOLOGY_OPENING */ + +/** + * Emitted when topology is closed. + * @public + * @category Event + */ +export declare class TopologyClosedEvent { + /** A unique identifier for the topology */ + topologyId: number; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** + * Representation of a deployment of servers + * @public + */ +export declare class TopologyDescription { + type: TopologyType; + setName: string | null; + maxSetVersion: number | null; + maxElectionId: ObjectId | null; + servers: Map; + stale: boolean; + compatible: boolean; + compatibilityError?: string; + logicalSessionTimeoutMinutes: number | null; + heartbeatFrequencyMS: number; + localThresholdMS: number; + commonWireVersion: number; + /** + * Create a TopologyDescription + */ + constructor(topologyType: TopologyType, serverDescriptions?: Map | null, setName?: string | null, maxSetVersion?: number | null, maxElectionId?: ObjectId | null, commonWireVersion?: number | null, options?: TopologyDescriptionOptions | null); + /* Excluded from this release type: updateFromSrvPollingEvent */ + /* Excluded from this release type: update */ + get error(): MongoError | null; + /** + * Determines if the topology description has any known servers + */ + get hasKnownServers(): boolean; + /** + * Determines if this topology description has a data-bearing server available. + */ + get hasDataBearingServers(): boolean; + /* Excluded from this release type: hasServer */ + /** + * Returns a JSON-serializable representation of the TopologyDescription. This is primarily + * intended for use with JSON.stringify(). + * + * This method will not throw. + */ + toJSON(): Document; +} + +/** + * Emitted when topology description changes. + * @public + * @category Event + */ +export declare class TopologyDescriptionChangedEvent { + /** A unique identifier for the topology */ + topologyId: number; + /** The old topology description */ + previousDescription: TopologyDescription; + /** The new topology description */ + newDescription: TopologyDescription; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/** @public */ +export declare interface TopologyDescriptionOptions { + heartbeatFrequencyMS?: number; + localThresholdMS?: number; +} + +/** @public */ +export declare type TopologyEvents = { + /* Excluded from this release type: connect */ + serverOpening(event: ServerOpeningEvent): void; + serverClosed(event: ServerClosedEvent): void; + serverDescriptionChanged(event: ServerDescriptionChangedEvent): void; + topologyClosed(event: TopologyClosedEvent): void; + topologyOpening(event: TopologyOpeningEvent): void; + topologyDescriptionChanged(event: TopologyDescriptionChangedEvent): void; + error(error: Error): void; + /* Excluded from this release type: open */ + close(): void; + timeout(): void; +} & Omit & ConnectionPoolEvents & ConnectionEvents & EventEmitterWithState; + +/** + * Emitted when topology is initialized. + * @public + * @category Event + */ +export declare class TopologyOpeningEvent { + /** A unique identifier for the topology */ + topologyId: number; + /* Excluded from this release type: name */ + /* Excluded from this release type: __constructor */ +} + +/* Excluded from this release type: TopologyOptions */ + +/* Excluded from this release type: TopologyPrivate */ + +/** + * An enumeration of topology types we know about + * @public + */ +export declare const TopologyType: Readonly<{ + readonly Single: "Single"; + readonly ReplicaSetNoPrimary: "ReplicaSetNoPrimary"; + readonly ReplicaSetWithPrimary: "ReplicaSetWithPrimary"; + readonly Sharded: "Sharded"; + readonly Unknown: "Unknown"; + readonly LoadBalanced: "LoadBalanced"; +}>; + +/** @public */ +export declare type TopologyType = (typeof TopologyType)[keyof typeof TopologyType]; + +/** @public */ +export declare interface TopologyVersion { + processId: ObjectId; + counter: Long; +} + +/* Excluded from this release type: Transaction */ + +/** + * Configuration options for a transaction. + * @public + */ +export declare interface TransactionOptions extends Omit { + /** A default read concern for commands in this transaction */ + readConcern?: ReadConcernLike; + /** A default writeConcern for commands in this transaction */ + writeConcern?: WriteConcern; + /** A default read preference for commands in this transaction */ + readPreference?: ReadPreferenceLike; + /** Specifies the maximum amount of time to allow a commit action on a transaction to run in milliseconds */ + maxCommitTimeMS?: number; +} + +/* Excluded from this release type: TxnState */ + +/** + * Typescript type safe event emitter + * @public + */ +export declare interface TypedEventEmitter extends EventEmitter { + addListener(event: EventKey, listener: Events[EventKey]): this; + addListener(event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void): this; + addListener(event: string | symbol, listener: GenericListener): this; + on(event: EventKey, listener: Events[EventKey]): this; + on(event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void): this; + on(event: string | symbol, listener: GenericListener): this; + once(event: EventKey, listener: Events[EventKey]): this; + once(event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void): this; + once(event: string | symbol, listener: GenericListener): this; + removeListener(event: EventKey, listener: Events[EventKey]): this; + removeListener(event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void): this; + removeListener(event: string | symbol, listener: GenericListener): this; + off(event: EventKey, listener: Events[EventKey]): this; + off(event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void): this; + off(event: string | symbol, listener: GenericListener): this; + removeAllListeners(event?: EventKey | CommonEvents | symbol | string): this; + listeners(event: EventKey | CommonEvents | symbol | string): Events[EventKey][]; + rawListeners(event: EventKey | CommonEvents | symbol | string): Events[EventKey][]; + emit(event: EventKey | symbol, ...args: Parameters): boolean; + listenerCount(type: EventKey | CommonEvents | symbol | string): number; + prependListener(event: EventKey, listener: Events[EventKey]): this; + prependListener(event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void): this; + prependListener(event: string | symbol, listener: GenericListener): this; + prependOnceListener(event: EventKey, listener: Events[EventKey]): this; + prependOnceListener(event: CommonEvents, listener: (eventName: string | symbol, listener: GenericListener) => void): this; + prependOnceListener(event: string | symbol, listener: GenericListener): this; + eventNames(): string[]; + getMaxListeners(): number; + setMaxListeners(n: number): this; +} + +/** + * Typescript type safe event emitter + * @public + */ +export declare class TypedEventEmitter extends EventEmitter { + /* Excluded from this release type: mongoLogger */ + /* Excluded from this release type: component */ + /* Excluded from this release type: emitAndLog */ + /* Excluded from this release type: emitAndLogHeartbeat */ + /* Excluded from this release type: emitAndLogCommand */ +} + +/** @public */ +export declare class UnorderedBulkOperation extends BulkOperationBase { + /* Excluded from this release type: __constructor */ + handleWriteError(writeResult: BulkWriteResult): void; + addToOperationsList(batchType: BatchType, document: Document | UpdateStatement | DeleteStatement): this; +} + +/** @public */ +export declare interface UpdateDescription { + /** + * A document containing key:value pairs of names of the fields that were + * changed, and the new value for those fields. + */ + updatedFields?: Partial; + /** + * An array of field names that were removed from the document. + */ + removedFields?: string[]; + /** + * An array of documents which record array truncations performed with pipeline-based updates using one or more of the following stages: + * - $addFields + * - $set + * - $replaceRoot + * - $replaceWith + */ + truncatedArrays?: Array<{ + /** The name of the truncated field. */ + field: string; + /** The number of elements in the truncated array. */ + newSize: number; + }>; + /** + * A document containing additional information about any ambiguous update paths from the update event. The document + * maps the full ambiguous update path to an array containing the actual resolved components of the path. For example, + * given a document shaped like `{ a: { '0': 0 } }`, and an update of `{ $inc: 'a.0' }`, disambiguated paths would look like + * the following: + * + * ``` + * { + * 'a.0': ['a', '0'] + * } + * ``` + * + * This field is only present when there are ambiguous paths that are updated as a part of the update event. + * + * On \<8.2.0 servers, this field is only present when `showExpandedEvents` is set to true. + * is enabled for the change stream. + * + * On 8.2.0+ servers, this field is present for update events regardless of whether `showExpandedEvents` is enabled. + * @sinceServerVersion 6.1.0 + */ + disambiguatedPaths?: Document; +} + +/** @public */ +export declare type UpdateFilter = { + $currentDate?: OnlyFieldsOfType; + $inc?: OnlyFieldsOfType; + $min?: MatchKeysAndValues; + $max?: MatchKeysAndValues; + $mul?: OnlyFieldsOfType; + $rename?: Record; + $set?: MatchKeysAndValues; + $setOnInsert?: MatchKeysAndValues; + $unset?: OnlyFieldsOfType; + $addToSet?: SetFields; + $pop?: OnlyFieldsOfType, 1 | -1>; + $pull?: PullOperator; + $push?: PushOperator; + $pullAll?: PullAllOperator; + $bit?: OnlyFieldsOfType; +} & Document; + +/** @public */ +export declare interface UpdateManyModel { + /** The filter to limit the updated documents. */ + filter: Filter; + /** + * The modifications to apply. The value can be either: + * UpdateFilter - A document that contains update operator expressions, + * Document[] - an aggregation pipeline. + */ + update: UpdateFilter | Document[]; + /** A set of filters specifying to which array elements an update should apply. */ + arrayFilters?: Document[]; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; + /** When true, creates a new document if no document matches the query. */ + upsert?: boolean; +} + +/** @public */ +export declare interface UpdateOneModel { + /** The filter that specifies which document to update. In the case of multiple matches, the first document matched is updated. */ + filter: Filter; + /** + * The modifications to apply. The value can be either: + * UpdateFilter - A document that contains update operator expressions, + * Document[] - an aggregation pipeline. + */ + update: UpdateFilter | Document[]; + /** A set of filters specifying to which array elements an update should apply. */ + arrayFilters?: Document[]; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; + /** When true, creates a new document if no document matches the query. */ + upsert?: boolean; + /** Specifies the sort order for the documents matched by the filter. */ + sort?: Sort; +} + +/** @public */ +export declare interface UpdateOptions extends CommandOperationOptions { + /** A set of filters specifying to which array elements an update should apply */ + arrayFilters?: Document[]; + /** If true, allows the write to opt-out of document level validation */ + bypassDocumentValidation?: boolean; + /** Specifies a collation */ + collation?: CollationOptions; + /** Specify that the update query should only consider plans using the hinted index */ + hint?: Hint; + /** When true, creates a new document if no document matches the query */ + upsert?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; +} + +/** + * @public + * `TSchema` is the schema of the collection + */ +export declare interface UpdateResult { + /** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */ + acknowledged: boolean; + /** The number of documents that matched the filter */ + matchedCount: number; + /** The number of documents that were modified */ + modifiedCount: number; + /** The number of documents that were upserted */ + upsertedCount: number; + /** The identifier of the inserted document if an upsert took place */ + upsertedId: InferIdType | null; +} + +/** @public */ +export declare interface UpdateStatement { + /** The query that matches documents to update. */ + q: Document; + /** The modifications to apply. */ + u: Document | Document[]; + /** If true, perform an insert if no documents match the query. */ + upsert?: boolean; + /** If true, updates all documents that meet the query criteria. */ + multi?: boolean; + /** Specifies the collation to use for the operation. */ + collation?: CollationOptions; + /** An array of filter documents that determines which array elements to modify for an update operation on an array field. */ + arrayFilters?: Document[]; + /** A document or string that specifies the index to use to support the query predicate. */ + hint?: Hint; + /** Specifies the sort order for the documents matched by the filter. */ + sort?: SortForCmd; +} + +export { UUID } + +/** @public */ +export declare interface ValidateCollectionOptions extends Omit { + /** Validates a collection in the background, without interrupting read or write traffic (only in MongoDB 4.4+) */ + background?: boolean; +} + +/** @public */ +export declare type W = number | 'majority'; + +/* Excluded from this release type: WAITING_FOR_SUITABLE_SERVER */ + +/* Excluded from this release type: WaitingForSuitableServerEvent */ + +/* Excluded from this release type: WaitQueueMember */ + +/* Excluded from this release type: WithConnectionCallback */ + +/** Add an _id field to an object shaped type @public */ +export declare type WithId = EnhancedOmit & { + _id: InferIdType; +}; + +/** Remove the _id field from an object shaped type @public */ +export declare type WithoutId = Omit; + +/** @public */ +export declare type WithSessionCallback = (session: ClientSession) => Promise; + +/** @public */ +export declare type WithTransactionCallback = (session: ClientSession) => Promise; + +/* Excluded from this release type: Workflow */ + +/** + * A MongoDB WriteConcern, which describes the level of acknowledgement + * requested from MongoDB for write operations. + * @public + * + * @see https://www.mongodb.com/docs/manual/reference/write-concern/ + */ +export declare class WriteConcern { + /** + * Request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags. + * If w is 0 and is set on a write operation, the server will not send a response. + */ + readonly w?: W; + /** Request acknowledgment that the write operation has been written to the on-disk journal */ + readonly journal?: boolean; + /** + * Specify a time limit to prevent write operations from blocking indefinitely. + */ + readonly wtimeoutMS?: number; + /** + * Specify a time limit to prevent write operations from blocking indefinitely. + * @deprecated Will be removed in the next major version. Please use wtimeoutMS. + */ + wtimeout?: number; + /** + * Request acknowledgment that the write operation has been written to the on-disk journal. + * @deprecated Will be removed in the next major version. Please use journal. + */ + j?: boolean; + /** + * Equivalent to the j option. + * @deprecated Will be removed in the next major version. Please use journal. + */ + fsync?: boolean | 1; + /** + * Constructs a WriteConcern from the write concern properties. + * @param w - request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags. + * @param wtimeoutMS - specify a time limit to prevent write operations from blocking indefinitely + * @param journal - request acknowledgment that the write operation has been written to the on-disk journal + * @param fsync - equivalent to the j option. Is deprecated and will be removed in the next major version. + */ + constructor(w?: W, wtimeoutMS?: number, journal?: boolean, fsync?: boolean | 1); + /** + * Apply a write concern to a command document. Will modify and return the command. + */ + static apply(command: Document, writeConcern: WriteConcern): Document; + /** Construct a WriteConcern given an options object. */ + static fromOptions(options?: WriteConcernOptions | WriteConcern | W, inherit?: WriteConcernOptions | WriteConcern): WriteConcern | undefined; +} + +/** + * An error representing a failure by the server to apply the requested write concern to the bulk operation. + * @public + * @category Error + */ +export declare class WriteConcernError { + /* Excluded from this release type: serverError */ + constructor(error: WriteConcernErrorData); + /** Write concern error code. */ + get code(): number | undefined; + /** Write concern error message. */ + get errmsg(): string | undefined; + /** Write concern error info. */ + get errInfo(): Document | undefined; + toJSON(): WriteConcernErrorData; + toString(): string; +} + +/** @public */ +export declare interface WriteConcernErrorData { + code: number; + errmsg: string; + errInfo?: Document; +} + +/** + * The type of the result property of MongoWriteConcernError + * @public + */ +export declare interface WriteConcernErrorResult { + writeConcernError: { + code: number; + errmsg: string; + codeName?: string; + errInfo?: Document; + }; + ok: number; + code?: number; + errorLabels?: string[]; + [x: string | number]: unknown; +} + +/** @public */ +export declare interface WriteConcernOptions { + /** Write Concern as an object */ + writeConcern?: WriteConcern | WriteConcernSettings; +} + +/** @public */ +export declare interface WriteConcernSettings { + /** The write concern */ + w?: W; + /** + * The write concern timeout. + */ + wtimeoutMS?: number; + /** The journal write concern */ + journal?: boolean; + /** + * The journal write concern. + * @deprecated Will be removed in the next major version. Please use the journal option. + */ + j?: boolean; + /** + * The write concern timeout. + */ + wtimeout?: number; + /** + * The file sync write concern. + * @deprecated Will be removed in the next major version. Please use the journal option. + */ + fsync?: boolean | 1; +} + +/** + * An error that occurred during a BulkWrite on the server. + * @public + * @category Error + */ +export declare class WriteError { + err: BulkWriteOperationError; + constructor(err: BulkWriteOperationError); + /** WriteError code. */ + get code(): number; + /** WriteError original bulk operation index. */ + get index(): number; + /** WriteError message. */ + get errmsg(): string | undefined; + /** WriteError details. */ + get errInfo(): Document | undefined; + /** Returns the underlying operation that caused the error */ + getOperation(): Document; + toJSON(): { + code: number; + index: number; + errmsg?: string; + op: Document; + }; + toString(): string; +} + +/* Excluded from this release type: WriteProtocolMessageType */ + +export { } diff --git a/gateway/node_modules/mongodb/package.json b/gateway/node_modules/mongodb/package.json new file mode 100644 index 0000000000000000000000000000000000000000..23b0ff7cc35b8235cc926eff98e1792114cc2025 --- /dev/null +++ b/gateway/node_modules/mongodb/package.json @@ -0,0 +1,183 @@ +{ + "name": "mongodb", + "version": "7.5.0", + "description": "The official MongoDB driver for Node.js", + "main": "lib/index.js", + "files": [ + "lib", + "src", + "etc/prepare.js", + "mongodb.d.ts", + "tsconfig.json" + ], + "types": "mongodb.d.ts", + "repository": { + "type": "git", + "url": "git@github.com:mongodb/node-mongodb-native.git" + }, + "keywords": [ + "mongodb", + "driver", + "official" + ], + "author": { + "name": "The MongoDB NodeJS Team", + "email": "dbx-node@mongodb.com" + }, + "dependencies": { + "@mongodb-js/saslprep": "^1.4.11", + "bson": "^7.2.0", + "mongodb-connection-string-url": "^7.0.1" + }, + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.806.0", + "@mongodb-js/zstd": "^7.0.0", + "gcp-metadata": "^7.0.1", + "kerberos": "^7.0.0", + "mongodb-client-encryption": "^7.2.0", + "snappy": "^7.3.2", + "socks": "^2.8.6" + }, + "peerDependenciesMeta": { + "@aws-sdk/credential-providers": { + "optional": true + }, + "@mongodb-js/zstd": { + "optional": true + }, + "kerberos": { + "optional": true + }, + "snappy": { + "optional": true + }, + "mongodb-client-encryption": { + "optional": true + }, + "gcp-metadata": { + "optional": true + }, + "socks": { + "optional": true + } + }, + "devDependencies": { + "@aws-sdk/credential-providers": "^3.876.0", + "@iarna/toml": "^2.2.5", + "@istanbuljs/nyc-config-typescript": "^1.0.2", + "@microsoft/api-extractor": "^7.58.7", + "@microsoft/tsdoc-config": "^0.18.1", + "@mongodb-js/zstd": "^7.0.0", + "@types/chai-subset": "^1.3.5", + "@types/express": "^5.0.6", + "@types/kerberos": "^1.1.5", + "@types/mocha": "^10.0.9", + "@types/node": "^22.15.3", + "@types/saslprep": "^1.0.3", + "@types/semver": "^7.7.0", + "@types/sinon": "^17.0.4", + "@types/whatwg-url": "^13.0.0", + "@typescript-eslint/eslint-plugin": "^8.60.1", + "@typescript-eslint/parser": "^8.31.1", + "aws4": "^1.13.2", + "chai": "^5.3.3", + "chai-subset": "^1.6.0", + "chalk": "^4.1.2", + "esbuild": "^0.28.0", + "eslint": "^9.39.1", + "eslint-config-prettier": "^10.1.8", + "eslint-plugin-mocha": "^10.4.1", + "eslint-plugin-prettier": "^5.5.6", + "eslint-plugin-simple-import-sort": "^12.1.1", + "eslint-plugin-tsdoc": "^0.5.2", + "eslint-plugin-unused-imports": "^4.4.1", + "express": "^5.2.1", + "gcp-metadata": "^7.0.1", + "js-yaml": "^4.2.0", + "mocha": "^11.7.6", + "mocha-sinon": "^2.1.2", + "mongodb-client-encryption": "^7.2.0", + "nyc": "^17.1.0", + "prettier": "^3.6.2", + "semver": "^7.7.2", + "sinon": "^18.0.1", + "sinon-chai": "^4.0.1", + "snappy": "^7.3.2", + "socks": "^2.8.7", + "source-map-support": "^0.5.21", + "ts-node": "^10.9.2", + "tsd": "^0.33.0", + "typescript": "5.8.3", + "typescript-cached-transpile": "^0.0.6", + "v8-heapsnapshot": "^1.3.1", + "yargs": "^18.0.0" + }, + "license": "Apache-2.0", + "engines": { + "node": ">=20.19.0" + }, + "bugs": { + "url": "https://jira.mongodb.org/projects/NODE/issues/" + }, + "homepage": "https://github.com/mongodb/node-mongodb-native", + "scripts": { + "build:evergreen": "node .evergreen/generate_evergreen_tasks.js", + "build:ts": "node ./node_modules/typescript/bin/tsc", + "build:dts": "npm run build:ts && api-extractor run && node etc/clean_definition_files.cjs && ESLINT_USE_FLAT_CONFIG=false eslint --no-ignore --fix mongodb.d.ts", + "build:docs": "./etc/docs/build.ts", + "build:typedoc": "typedoc", + "build:nightly": "node ./.github/scripts/nightly.mjs", + "check:bench": "npm --prefix test/benchmarks/driver_bench start", + "check:coverage": "nyc npm run test:all", + "check:integration-coverage": "nyc npm run check:test", + "check:lambda": "npm run build:bundle && nyc mocha --config test/mocha_lambda.js test/integration/node-specific/examples/handler.test.js", + "check:lambda:aws": "npm run build:bundle && nyc mocha --config test/mocha_lambda.js test/integration/node-specific/examples/aws_handler.test.js", + "check:lint": "npm run build:dts && npm run check:dts && npm run check:eslint && npm run check:tsd", + "check:eslint": "npm run build:dts && ESLINT_USE_FLAT_CONFIG=false eslint -v && ESLINT_USE_FLAT_CONFIG=false eslint --max-warnings=0 --ext '.js,.ts' src test", + "check:tsd": "tsd --version && tsd", + "check:dependencies": "npm run build:bundle && mocha test/action/dependency.test.ts", + "check:dts": "npm run build:bundle && node ./node_modules/typescript/bin/tsc --target es2023 --module commonjs --noEmit mongodb.d.ts && tsd", + "check:search-indexes": "npm run build:bundle && nyc mocha --config test/mocha_mongodb.js test/manual/search-index-management.prose.test.ts", + "check:test": "npm run build:bundle && nyc mocha --config test/mocha_mongodb.js test/integration", + "check:test:debug": "npm run mocha:debug -- --config test/mocha_mongodb.js test/integration", + "check:test-bundled": "MONGODB_BUNDLED=true npm run check:test", + "check:unit": "npm run build:bundle && nyc mocha test/unit", + "check:unit:debug": "npm run mocha:debug -- test/unit", + "check:unit-bundled": "MONGODB_BUNDLED=true npm run check:unit", + "check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit", + "check:atlas": "npm run build:bundle && nyc mocha --config test/manual/mocharc.js test/manual/atlas_connectivity.test.ts", + "check:drivers-atlas-testing": "npm run build:bundle && nyc mocha --config test/mocha_mongodb.js test/atlas/drivers_atlas_testing.test.ts", + "check:aws": "npm run build:bundle && nyc mocha --config test/mocha_mongodb.js test/integration/auth/mongodb_aws.test.ts test/integration/auth/mongodb_aws.prose.test.ts", + "check:oidc-auth": "npm run build:bundle && nyc mocha --config test/mocha_mongodb.js test/integration/auth/auth.spec.test.ts", + "check:oidc-test": "npm run build:bundle && nyc mocha --config test/mocha_mongodb.js test/integration/auth/mongodb_oidc.prose.test.ts", + "check:kerberos": "npm run build:bundle && nyc mocha --config test/manual/mocharc.js test/manual/kerberos.test.ts", + "check:tls": "npm run build:bundle && nyc mocha --config test/manual/mocharc.js test/manual/tls_support.test.ts", + "check:ldap": "npm run build:bundle && nyc mocha --config test/manual/mocharc.js test/manual/ldap.test.ts", + "check:socks5": "npm run build:bundle && nyc mocha --config test/manual/mocharc.js test/manual/socks5.test.ts", + "check:csfle": "npm run build:bundle && nyc mocha --config test/mocha_mongodb.js test/integration/client-side-encryption", + "check:snappy": "npm run build:bundle && nyc mocha test/unit/assorted/snappy.test.js", + "check:x509": "npm run build:bundle && nyc mocha test/manual/x509_auth.test.ts", + "mocha:debug": "npm run build:bundle && node --inspect --enable-source-maps --no-experimental-strip-types ./node_modules/mocha/bin/mocha.js", + "build:bundle": "npm run bundle:driver && npm run bundle:types && npm run build:runtime-barrel", + "build:runtime-barrel": "node etc/build-runtime-barrel.mjs", + "bundle:driver": "node etc/bundle-driver.mjs", + "bundle:types": "npx tsc --project ./tsconfig.json --declaration --emitDeclarationOnly --declarationDir test/tools/runner/bundle/types", + "fix:eslint": "npm run check:eslint -- --fix", + "prepare": "node etc/prepare.js", + "preview:docs": "ts-node etc/docs/preview.ts", + "switch:to-bundled": "MONGODB_BUNDLED=true npm run build:runtime-barrel", + "switch:to-unbundled": "MONGODB_BUNDLED=false npm run build:runtime-barrel", + "test": "npm run check:lint && npm run test:all", + "test:all": "npm run check:unit && npm run check:test", + "update:docs": "npm run build:docs -- --yes" + }, + "tsd": { + "directory": "test/types", + "compilerOptions": { + "strict": true, + "target": "esnext", + "module": "commonjs", + "moduleResolution": "node" + } + } +} diff --git a/gateway/node_modules/mongodb/src/admin.ts b/gateway/node_modules/mongodb/src/admin.ts new file mode 100644 index 0000000000000000000000000000000000000000..c75a51fa9dd6a7831c24edb4e35ebaa397bdc0a5 --- /dev/null +++ b/gateway/node_modules/mongodb/src/admin.ts @@ -0,0 +1,173 @@ +import { type Document, resolveBSONOptions } from './bson'; +import type { Db } from './db'; +import type { CommandOperationOptions } from './operations/command'; +import { executeOperation } from './operations/execute_operation'; +import { + ListDatabasesOperation, + type ListDatabasesOptions, + type ListDatabasesResult +} from './operations/list_databases'; +import { RemoveUserOperation, type RemoveUserOptions } from './operations/remove_user'; +import { RunCommandOperation, type RunCommandOptions } from './operations/run_command'; +import { + ValidateCollectionOperation, + type ValidateCollectionOptions +} from './operations/validate_collection'; +import { MongoDBNamespace } from './utils'; + +/** @internal */ +export interface AdminPrivate { + db: Db; +} + +/** + * The **Admin** class is an internal class that allows convenient access to + * the admin functionality and commands for MongoDB. + * + * **ADMIN Cannot directly be instantiated** + * @public + * + * @example + * ```ts + * import { MongoClient } from 'mongodb'; + * + * const client = new MongoClient('mongodb://localhost:27017'); + * const admin = client.db().admin(); + * const dbInfo = await admin.listDatabases(); + * for (const db of dbInfo.databases) { + * console.log(db.name); + * } + * ``` + */ +export class Admin { + /** @internal */ + s: AdminPrivate; + + /** + * Create a new Admin instance + * @internal + */ + constructor(db: Db) { + this.s = { db }; + } + + /** + * Execute a command + * + * The driver will ensure the following fields are attached to the command sent to the server: + * - `lsid` - sourced from an implicit session or options.session + * - `$readPreference` - defaults to primary or can be configured by options.readPreference + * - `$db` - sourced from the name of this database + * + * If the client has a serverApi setting: + * - `apiVersion` + * - `apiStrict` + * - `apiDeprecationErrors` + * + * When in a transaction: + * - `readConcern` - sourced from readConcern set on the TransactionOptions + * - `writeConcern` - sourced from writeConcern set on the TransactionOptions + * + * Attaching any of the above fields to the command will have no effect as the driver will overwrite the value. + * + * @param command - The command to execute + * @param options - Optional settings for the command + */ + async command(command: Document, options?: RunCommandOptions): Promise { + return await executeOperation( + this.s.db.client, + new RunCommandOperation(new MongoDBNamespace('admin'), command, { + ...resolveBSONOptions(options), + session: options?.session, + readPreference: options?.readPreference, + timeoutMS: options?.timeoutMS ?? this.s.db.timeoutMS + }) + ); + } + + /** + * Retrieve the server build information + * + * @param options - Optional settings for the command + */ + async buildInfo(options?: CommandOperationOptions): Promise { + return await this.command({ buildinfo: 1 }, options); + } + + /** + * Retrieve the server build information + * + * @param options - Optional settings for the command + */ + async serverInfo(options?: CommandOperationOptions): Promise { + return await this.command({ buildinfo: 1 }, options); + } + + /** + * Retrieve this db's server status. + * + * @param options - Optional settings for the command + */ + async serverStatus(options?: CommandOperationOptions): Promise { + return await this.command({ serverStatus: 1 }, options); + } + + /** + * Ping the MongoDB server and retrieve results + * + * @param options - Optional settings for the command + */ + async ping(options?: CommandOperationOptions): Promise { + return await this.command({ ping: 1 }, options); + } + + /** + * Remove a user from a database + * + * @param username - The username to remove + * @param options - Optional settings for the command + */ + async removeUser(username: string, options?: RemoveUserOptions): Promise { + return await executeOperation( + this.s.db.client, + new RemoveUserOperation(this.s.db, username, { dbName: 'admin', ...options }) + ); + } + + /** + * Validate an existing collection + * + * @param collectionName - The name of the collection to validate. + * @param options - Optional settings for the command + */ + async validateCollection( + collectionName: string, + options: ValidateCollectionOptions = {} + ): Promise { + return await executeOperation( + this.s.db.client, + new ValidateCollectionOperation(this, collectionName, options) + ); + } + + /** + * List the available databases + * + * @param options - Optional settings for the command + */ + async listDatabases(options?: ListDatabasesOptions): Promise { + return await executeOperation( + this.s.db.client, + new ListDatabasesOperation(this.s.db, { timeoutMS: this.s.db.timeoutMS, ...options }) + ); + } + + /** + * Get ReplicaSet status + * + * @param options - Optional settings for the command + */ + async replSetGetStatus(options?: CommandOperationOptions): Promise { + return await this.command({ replSetGetStatus: 1 }, options); + } +} diff --git a/gateway/node_modules/mongodb/src/bson.ts b/gateway/node_modules/mongodb/src/bson.ts new file mode 100644 index 0000000000000000000000000000000000000000..623913156b7e40e591bb6e12801233405960c39d --- /dev/null +++ b/gateway/node_modules/mongodb/src/bson.ts @@ -0,0 +1,178 @@ +/* eslint-disable no-restricted-imports */ +import { BSON, type DeserializeOptions, NumberUtils, type SerializeOptions } from 'bson'; + +export { + Binary, + BSON, + BSONError, + BSONRegExp, + BSONSymbol, + BSONType, + ByteUtils, + calculateObjectSize, + Code, + DBRef, + Decimal128, + deserialize, + type DeserializeOptions, + Document, + Double, + EJSON, + EJSONOptions, + Int32, + Long, + MaxKey, + MinKey, + NumberUtils, + ObjectId, + type ObjectIdLike, + serialize, + Timestamp, + UUID +} from 'bson'; + +/** @internal */ +export type BSONElement = BSON.OnDemand['BSONElement']; + +export function parseToElementsToArray(bytes: Uint8Array, offset?: number): BSONElement[] { + const res = BSON.onDemand.parseToElements(bytes, offset); + return Array.isArray(res) ? res : [...res]; +} + +// validates buffer inputs, used for read operations +const validateBufferInputs = (buffer: Uint8Array, offset: number, length: number) => { + if (offset < 0 || offset + length > buffer.length) { + throw new RangeError( + `Attempt to access memory outside buffer bounds: buffer length: ${buffer.length}, offset: ${offset}, length: ${length}` + ); + } +}; + +// readInt32LE, reads a 32-bit integer from buffer at given offset +// throws if offset is out of bounds +export const readInt32LE = (buffer: Uint8Array, offset: number): number => { + validateBufferInputs(buffer, offset, 4); + return NumberUtils.getInt32LE(buffer, offset); +}; + +// readUint8, reads a single unsigned byte from buffer at given offset +export const readUint8 = (buffer: Uint8Array, offset: number): number => { + validateBufferInputs(buffer, offset, 1); + return buffer[offset]; +}; + +export const setUint32LE = (destination: Uint8Array, offset: number, value: number): 4 => { + destination[offset] = value; + value >>>= 8; + destination[offset + 1] = value; + value >>>= 8; + destination[offset + 2] = value; + value >>>= 8; + destination[offset + 3] = value; + return 4; +}; + +/** + * BSON Serialization options. + * @public + */ +export interface BSONSerializeOptions + extends Omit, + Omit< + DeserializeOptions, + | 'evalFunctions' + | 'cacheFunctions' + | 'cacheFunctionsCrc32' + | 'allowObjectSmallerThanBufferSize' + | 'index' + | 'validation' + > { + /** + * Enabling the raw option will return a [Node.js Buffer](https://nodejs.org/api/buffer.html) + * which is allocated using [allocUnsafe API](https://nodejs.org/api/buffer.html#static-method-bufferallocunsafesize). + * See this section from the [Node.js Docs here](https://nodejs.org/api/buffer.html#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe) + * for more detail about what "unsafe" refers to in this context. + * If you need to maintain your own editable clone of the bytes returned for an extended life time of the process, it is recommended you allocate + * your own buffer and clone the contents: + * + * @example + * ```ts + * const raw = await collection.findOne({}, { raw: true }); + * const myBuffer = Buffer.alloc(raw.byteLength); + * myBuffer.set(raw, 0); + * // Only save and use `myBuffer` beyond this point + * ``` + * + * @remarks + * Please note there is a known limitation where this option cannot be used at the MongoClient level (see [NODE-3946](https://jira.mongodb.org/browse/NODE-3946)). + * It does correctly work at `Db`, `Collection`, and per operation the same as other BSON options work. + */ + raw?: boolean; + + /** Enable utf8 validation when deserializing BSON documents. Defaults to true. */ + enableUtf8Validation?: boolean; +} + +export function pluckBSONSerializeOptions(options: BSONSerializeOptions): BSONSerializeOptions { + const { + fieldsAsRaw, + useBigInt64, + promoteValues, + promoteBuffers, + promoteLongs, + serializeFunctions, + ignoreUndefined, + bsonRegExp, + raw, + enableUtf8Validation + } = options; + return { + fieldsAsRaw, + useBigInt64, + promoteValues, + promoteBuffers, + promoteLongs, + serializeFunctions, + ignoreUndefined, + bsonRegExp, + raw, + enableUtf8Validation + }; +} + +/** + * Merge the given BSONSerializeOptions, preferring options over the parent's options, and + * substituting defaults for values not set. + * + * @internal + */ +export function resolveBSONOptions( + options?: BSONSerializeOptions, + parent?: { bsonOptions?: BSONSerializeOptions } +): BSONSerializeOptions { + const parentOptions = parent?.bsonOptions; + return { + raw: options?.raw ?? parentOptions?.raw ?? false, + useBigInt64: options?.useBigInt64 ?? parentOptions?.useBigInt64 ?? false, + promoteLongs: options?.promoteLongs ?? parentOptions?.promoteLongs ?? true, + promoteValues: options?.promoteValues ?? parentOptions?.promoteValues ?? true, + promoteBuffers: options?.promoteBuffers ?? parentOptions?.promoteBuffers ?? false, + ignoreUndefined: options?.ignoreUndefined ?? parentOptions?.ignoreUndefined ?? false, + bsonRegExp: options?.bsonRegExp ?? parentOptions?.bsonRegExp ?? false, + serializeFunctions: options?.serializeFunctions ?? parentOptions?.serializeFunctions ?? false, + fieldsAsRaw: options?.fieldsAsRaw ?? parentOptions?.fieldsAsRaw ?? {}, + enableUtf8Validation: + options?.enableUtf8Validation ?? parentOptions?.enableUtf8Validation ?? true + }; +} + +/** @internal */ +export function parseUtf8ValidationOption(options?: { enableUtf8Validation?: boolean }): { + utf8: { writeErrors: false } | false; +} { + const enableUtf8Validation = options?.enableUtf8Validation; + if (enableUtf8Validation === false) { + return { utf8: false }; + } + return { utf8: { writeErrors: false } }; +} diff --git a/gateway/node_modules/mongodb/src/bulk/common.ts b/gateway/node_modules/mongodb/src/bulk/common.ts new file mode 100644 index 0000000000000000000000000000000000000000..26c0d750d9c7d1f45300aeda29ec384527b94710 --- /dev/null +++ b/gateway/node_modules/mongodb/src/bulk/common.ts @@ -0,0 +1,1251 @@ +import { type BSONSerializeOptions, type Document, EJSON, resolveBSONOptions } from '../bson'; +import type { Collection } from '../collection'; +import { + type AnyError, + MongoBatchReExecutionError, + MONGODB_ERROR_CODES, + MongoInvalidArgumentError, + MongoRuntimeError, + MongoServerError, + MongoWriteConcernError +} from '../error'; +import type { Filter, OneOrMore, OptionalId, UpdateFilter, WithoutId } from '../mongo_types'; +import type { CollationOptions, CommandOperationOptions } from '../operations/command'; +import { DeleteOperation, type DeleteStatement, makeDeleteStatement } from '../operations/delete'; +import { executeOperation } from '../operations/execute_operation'; +import { InsertOperation } from '../operations/insert'; +import { type Hint } from '../operations/operation'; +import { makeUpdateStatement, UpdateOperation, type UpdateStatement } from '../operations/update'; +import type { Topology } from '../sdam/topology'; +import { type Sort } from '../sort'; +import { TimeoutContext } from '../timeout'; +import { + getTopology, + hasAtomicOperators, + maybeAddIdToDocuments, + type MongoDBNamespace, + resolveOptions +} from '../utils'; +import { WriteConcern } from '../write_concern'; + +/** @public */ +export const BatchType = Object.freeze({ + INSERT: 1, + UPDATE: 2, + DELETE: 3 +} as const); + +/** @public */ +export type BatchType = (typeof BatchType)[keyof typeof BatchType]; + +/** @public */ +export interface InsertOneModel { + /** The document to insert. */ + document: OptionalId; +} + +/** @public */ +export interface DeleteOneModel { + /** The filter to limit the deleted documents. */ + filter: Filter; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; +} + +/** @public */ +export interface DeleteManyModel { + /** The filter to limit the deleted documents. */ + filter: Filter; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; +} + +/** @public */ +export interface ReplaceOneModel { + /** The filter that specifies which document to replace. In the case of multiple matches, the first document matched is replaced. */ + filter: Filter; + /** The document with which to replace the matched document. */ + replacement: WithoutId; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; + /** When true, creates a new document if no document matches the query. */ + upsert?: boolean; + /** Specifies the sort order for the documents matched by the filter. */ + sort?: Sort; +} + +/** @public */ +export interface UpdateOneModel { + /** The filter that specifies which document to update. In the case of multiple matches, the first document matched is updated. */ + filter: Filter; + /** + * The modifications to apply. The value can be either: + * UpdateFilter - A document that contains update operator expressions, + * Document[] - an aggregation pipeline. + */ + update: UpdateFilter | Document[]; + /** A set of filters specifying to which array elements an update should apply. */ + arrayFilters?: Document[]; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; + /** When true, creates a new document if no document matches the query. */ + upsert?: boolean; + /** Specifies the sort order for the documents matched by the filter. */ + sort?: Sort; +} + +/** @public */ +export interface UpdateManyModel { + /** The filter to limit the updated documents. */ + filter: Filter; + /** + * The modifications to apply. The value can be either: + * UpdateFilter - A document that contains update operator expressions, + * Document[] - an aggregation pipeline. + */ + update: UpdateFilter | Document[]; + /** A set of filters specifying to which array elements an update should apply. */ + arrayFilters?: Document[]; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; + /** When true, creates a new document if no document matches the query. */ + upsert?: boolean; +} + +/** @public */ +export type AnyBulkWriteOperation = + | { insertOne: InsertOneModel } + | { replaceOne: ReplaceOneModel } + | { updateOne: UpdateOneModel } + | { updateMany: UpdateManyModel } + | { deleteOne: DeleteOneModel } + | { deleteMany: DeleteManyModel }; + +/** @internal */ +export interface BulkResult { + ok: number; + writeErrors: WriteError[]; + writeConcernErrors: WriteConcernError[]; + insertedIds: Document[]; + nInserted: number; + nUpserted: number; + nMatched: number; + nModified: number; + nRemoved: number; + upserted: Document[]; +} + +/** + * Keeps the state of a unordered batch so we can rewrite the results + * correctly after command execution + * + * @public + */ +export class Batch { + originalZeroIndex: number; + currentIndex: number; + originalIndexes: number[]; + batchType: BatchType; + operations: T[]; + size: number; + sizeBytes: number; + + constructor(batchType: BatchType, originalZeroIndex: number) { + this.originalZeroIndex = originalZeroIndex; + this.currentIndex = 0; + this.originalIndexes = []; + this.batchType = batchType; + this.operations = []; + this.size = 0; + this.sizeBytes = 0; + } +} + +/** + * @public + * The result of a bulk write. + */ +export class BulkWriteResult { + private readonly result: BulkResult; + /** Number of documents inserted. */ + readonly insertedCount: number; + /** Number of documents matched for update. */ + readonly matchedCount: number; + /** Number of documents modified. */ + readonly modifiedCount: number; + /** Number of documents deleted. */ + readonly deletedCount: number; + /** Number of documents upserted. */ + readonly upsertedCount: number; + /** Upserted document generated Id's, hash key is the index of the originating operation */ + readonly upsertedIds: { [key: number]: any }; + /** Inserted document generated Id's, hash key is the index of the originating operation */ + readonly insertedIds: { [key: number]: any }; + + private static generateIdMap(ids: Document[]): { [key: number]: any } { + const idMap: { [index: number]: any } = {}; + for (const doc of ids) { + idMap[doc.index] = doc._id; + } + return idMap; + } + + /** + * Create a new BulkWriteResult instance + * @internal + */ + constructor(bulkResult: BulkResult, isOrdered: boolean) { + this.result = bulkResult; + this.insertedCount = this.result.nInserted ?? 0; + this.matchedCount = this.result.nMatched ?? 0; + this.modifiedCount = this.result.nModified ?? 0; + this.deletedCount = this.result.nRemoved ?? 0; + this.upsertedCount = this.result.upserted.length ?? 0; + this.upsertedIds = BulkWriteResult.generateIdMap(this.result.upserted); + this.insertedIds = BulkWriteResult.generateIdMap( + this.getSuccessfullyInsertedIds(bulkResult, isOrdered) + ); + Object.defineProperty(this, 'result', { value: this.result, enumerable: false }); + } + + /** Evaluates to true if the bulk operation correctly executes */ + get ok(): number { + return this.result.ok; + } + + /** + * Returns document_ids that were actually inserted + * @internal + */ + private getSuccessfullyInsertedIds(bulkResult: BulkResult, isOrdered: boolean): Document[] { + if (bulkResult.writeErrors.length === 0) return bulkResult.insertedIds; + + if (isOrdered) { + return bulkResult.insertedIds.slice(0, bulkResult.writeErrors[0].index); + } + + return bulkResult.insertedIds.filter( + ({ index }) => !bulkResult.writeErrors.some(writeError => index === writeError.index) + ); + } + + /** Returns the upserted id at the given index */ + getUpsertedIdAt(index: number): Document | undefined { + return this.result.upserted[index]; + } + + /** Returns raw internal result */ + getRawResponse(): Document { + return this.result; + } + + /** Returns true if the bulk operation contains a write error */ + hasWriteErrors(): boolean { + return this.result.writeErrors.length > 0; + } + + /** Returns the number of write errors from the bulk operation */ + getWriteErrorCount(): number { + return this.result.writeErrors.length; + } + + /** Returns a specific write error object */ + getWriteErrorAt(index: number): WriteError | undefined { + return index < this.result.writeErrors.length ? this.result.writeErrors[index] : undefined; + } + + /** Retrieve all write errors */ + getWriteErrors(): WriteError[] { + return this.result.writeErrors; + } + + /** Retrieve the write concern error if one exists */ + getWriteConcernError(): WriteConcernError | undefined { + if (this.result.writeConcernErrors.length === 0) { + return; + } else if (this.result.writeConcernErrors.length === 1) { + // Return the error + return this.result.writeConcernErrors[0]; + } else { + // Combine the errors + let errmsg = ''; + for (let i = 0; i < this.result.writeConcernErrors.length; i++) { + const err = this.result.writeConcernErrors[i]; + errmsg = errmsg + err.errmsg; + + // TODO: Something better + if (i === 0) errmsg = errmsg + ' and '; + } + + return new WriteConcernError({ errmsg, code: MONGODB_ERROR_CODES.WriteConcernTimeout }); + } + } + + toString(): string { + return `BulkWriteResult(${EJSON.stringify(this.result)})`; + } + + isOk(): boolean { + return this.result.ok === 1; + } +} + +/** @public */ +export interface WriteConcernErrorData { + code: number; + errmsg: string; + errInfo?: Document; +} + +/** + * An error representing a failure by the server to apply the requested write concern to the bulk operation. + * @public + * @category Error + */ +export class WriteConcernError { + /** @internal */ + private serverError: WriteConcernErrorData; + + constructor(error: WriteConcernErrorData) { + this.serverError = error; + } + + /** Write concern error code. */ + get code(): number | undefined { + return this.serverError.code; + } + + /** Write concern error message. */ + get errmsg(): string | undefined { + return this.serverError.errmsg; + } + + /** Write concern error info. */ + get errInfo(): Document | undefined { + return this.serverError.errInfo; + } + + toJSON(): WriteConcernErrorData { + return this.serverError; + } + + toString(): string { + return `WriteConcernError(${this.errmsg})`; + } +} + +/** @public */ +export interface BulkWriteOperationError { + index: number; + code: number; + errmsg: string; + errInfo: Document; + op: Document | UpdateStatement | DeleteStatement; +} + +/** + * An error that occurred during a BulkWrite on the server. + * @public + * @category Error + */ +export class WriteError { + err: BulkWriteOperationError; + + constructor(err: BulkWriteOperationError) { + this.err = err; + } + + /** WriteError code. */ + get code(): number { + return this.err.code; + } + + /** WriteError original bulk operation index. */ + get index(): number { + return this.err.index; + } + + /** WriteError message. */ + get errmsg(): string | undefined { + return this.err.errmsg; + } + + /** WriteError details. */ + get errInfo(): Document | undefined { + return this.err.errInfo; + } + + /** Returns the underlying operation that caused the error */ + getOperation(): Document { + return this.err.op; + } + + toJSON(): { code: number; index: number; errmsg?: string; op: Document } { + return { code: this.err.code, index: this.err.index, errmsg: this.err.errmsg, op: this.err.op }; + } + + toString(): string { + return `WriteError(${JSON.stringify(this.toJSON())})`; + } +} + +/** Merges results into shared data structure */ +export function mergeBatchResults( + batch: Batch, + bulkResult: BulkResult, + err?: AnyError, + result?: Document +): void { + // If we have an error set the result to be the err object + if (err) { + result = err; + } else if (result && result.result) { + result = result.result; + } + + if (result == null) { + return; + } + + // Do we have a top level error stop processing and return + if (result.ok === 0 && bulkResult.ok === 1) { + bulkResult.ok = 0; + + const writeError = { + index: 0, + code: result.code || 0, + errmsg: result.message, + errInfo: result.errInfo, + op: batch.operations[0] + }; + + bulkResult.writeErrors.push(new WriteError(writeError)); + return; + } else if (result.ok === 0 && bulkResult.ok === 0) { + return; + } + + // If we have an insert Batch type + if (isInsertBatch(batch) && result.n) { + bulkResult.nInserted = bulkResult.nInserted + result.n; + } + + // If we have an insert Batch type + if (isDeleteBatch(batch) && result.n) { + bulkResult.nRemoved = bulkResult.nRemoved + result.n; + } + + let nUpserted = 0; + + // We have an array of upserted values, we need to rewrite the indexes + if (Array.isArray(result.upserted)) { + nUpserted = result.upserted.length; + + for (let i = 0; i < result.upserted.length; i++) { + bulkResult.upserted.push({ + index: result.upserted[i].index + batch.originalZeroIndex, + _id: result.upserted[i]._id + }); + } + } else if (result.upserted) { + nUpserted = 1; + + bulkResult.upserted.push({ + index: batch.originalZeroIndex, + _id: result.upserted + }); + } + + // If we have an update Batch type + if (isUpdateBatch(batch) && result.n) { + const nModified = result.nModified; + bulkResult.nUpserted = bulkResult.nUpserted + nUpserted; + bulkResult.nMatched = bulkResult.nMatched + (result.n - nUpserted); + + if (typeof nModified === 'number') { + bulkResult.nModified = bulkResult.nModified + nModified; + } else { + bulkResult.nModified = 0; + } + } + + if (Array.isArray(result.writeErrors)) { + for (let i = 0; i < result.writeErrors.length; i++) { + const writeError = { + index: batch.originalIndexes[result.writeErrors[i].index], + code: result.writeErrors[i].code, + errmsg: result.writeErrors[i].errmsg, + errInfo: result.writeErrors[i].errInfo, + op: batch.operations[result.writeErrors[i].index] + }; + + bulkResult.writeErrors.push(new WriteError(writeError)); + } + } + + if (result.writeConcernError) { + bulkResult.writeConcernErrors.push(new WriteConcernError(result.writeConcernError)); + } +} + +async function executeCommands( + bulkOperation: BulkOperationBase, + options: BulkWriteOptions & { timeoutContext?: TimeoutContext | null } +): Promise { + if (bulkOperation.s.batches.length === 0) { + return new BulkWriteResult(bulkOperation.s.bulkResult, bulkOperation.isOrdered); + } + + for (const batch of bulkOperation.s.batches) { + const finalOptions = resolveOptions(bulkOperation, { + ...options, + ordered: bulkOperation.isOrdered + }); + + if (finalOptions.bypassDocumentValidation !== true) { + delete finalOptions.bypassDocumentValidation; + } + + // Is the bypassDocumentValidation options specific + if (bulkOperation.s.bypassDocumentValidation === true) { + finalOptions.bypassDocumentValidation = true; + } + + // Is the checkKeys option disabled + if (bulkOperation.s.checkKeys === false) { + finalOptions.checkKeys = false; + } + + if (bulkOperation.retryWrites) { + if (isUpdateBatch(batch)) { + bulkOperation.retryWrites = + bulkOperation.retryWrites && !batch.operations.some(op => op.multi); + } + + if (isDeleteBatch(batch)) { + bulkOperation.retryWrites = + bulkOperation.retryWrites && !batch.operations.some(op => op.limit === 0); + } + } + + const operation = isInsertBatch(batch) + ? new InsertOperation(bulkOperation.s.namespace, batch.operations, finalOptions) + : isUpdateBatch(batch) + ? new UpdateOperation(bulkOperation.s.namespace, batch.operations, finalOptions) + : isDeleteBatch(batch) + ? new DeleteOperation(bulkOperation.s.namespace, batch.operations, finalOptions) + : null; + + if (operation == null) throw new MongoRuntimeError(`Unknown batchType: ${batch.batchType}`); + + let thrownError = null; + let result; + try { + result = await executeOperation( + bulkOperation.s.collection.client, + operation, + finalOptions.timeoutContext + ); + } catch (error) { + thrownError = error; + } + + if (thrownError != null) { + if (thrownError instanceof MongoWriteConcernError) { + mergeBatchResults(batch, bulkOperation.s.bulkResult, thrownError, result); + const writeResult = new BulkWriteResult( + bulkOperation.s.bulkResult, + bulkOperation.isOrdered + ); + + throw new MongoBulkWriteError( + { + message: thrownError.result.writeConcernError.errmsg, + code: thrownError.result.writeConcernError.code + }, + writeResult + ); + } else { + // Error is a driver related error not a bulk op error, return early + throw new MongoBulkWriteError( + thrownError, + new BulkWriteResult(bulkOperation.s.bulkResult, bulkOperation.isOrdered) + ); + } + } + + mergeBatchResults(batch, bulkOperation.s.bulkResult, thrownError, result); + const writeResult = new BulkWriteResult(bulkOperation.s.bulkResult, bulkOperation.isOrdered); + bulkOperation.handleWriteError(writeResult); + } + + bulkOperation.s.batches.length = 0; + + const writeResult = new BulkWriteResult(bulkOperation.s.bulkResult, bulkOperation.isOrdered); + bulkOperation.handleWriteError(writeResult); + return writeResult; +} + +/** + * An error indicating an unsuccessful Bulk Write + * @public + * @category Error + */ +export class MongoBulkWriteError extends MongoServerError { + result: BulkWriteResult; + writeErrors: OneOrMore = []; + err?: WriteConcernError; + + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor( + error: + | { message: string; code: number; writeErrors?: WriteError[] } + | WriteConcernError + | AnyError, + result: BulkWriteResult + ) { + super(error); + + if (error instanceof WriteConcernError) this.err = error; + else if (!(error instanceof Error)) { + this.message = error.message; + this.code = error.code; + this.writeErrors = error.writeErrors ?? []; + } + + this.result = result; + Object.assign(this, error); + } + + override get name(): string { + return 'MongoBulkWriteError'; + } + + /** Number of documents inserted. */ + get insertedCount(): number { + return this.result.insertedCount; + } + /** Number of documents matched for update. */ + get matchedCount(): number { + return this.result.matchedCount; + } + /** Number of documents modified. */ + get modifiedCount(): number { + return this.result.modifiedCount; + } + /** Number of documents deleted. */ + get deletedCount(): number { + return this.result.deletedCount; + } + /** Number of documents upserted. */ + get upsertedCount(): number { + return this.result.upsertedCount; + } + /** Inserted document generated Id's, hash key is the index of the originating operation */ + get insertedIds(): { [key: number]: any } { + return this.result.insertedIds; + } + /** Upserted document generated Id's, hash key is the index of the originating operation */ + get upsertedIds(): { [key: number]: any } { + return this.result.upsertedIds; + } +} + +/** + * A builder object that is returned from {@link BulkOperationBase#find}. + * Is used to build a write operation that involves a query filter. + * + * @public + */ +export class FindOperators { + bulkOperation: BulkOperationBase; + + /** + * Creates a new FindOperators object. + * @internal + */ + constructor(bulkOperation: BulkOperationBase) { + this.bulkOperation = bulkOperation; + } + + /** Add a multiple update operation to the bulk operation */ + update(updateDocument: Document | Document[]): BulkOperationBase { + const currentOp = buildCurrentOp(this.bulkOperation); + return this.bulkOperation.addToOperationsList( + BatchType.UPDATE, + makeUpdateStatement(currentOp.selector, updateDocument, { + ...currentOp, + multi: true + }) + ); + } + + /** Add a single update operation to the bulk operation */ + updateOne(updateDocument: Document | Document[]): BulkOperationBase { + if (!hasAtomicOperators(updateDocument, this.bulkOperation.bsonOptions)) { + throw new MongoInvalidArgumentError('Update document requires atomic operators'); + } + + const currentOp = buildCurrentOp(this.bulkOperation); + return this.bulkOperation.addToOperationsList( + BatchType.UPDATE, + makeUpdateStatement(currentOp.selector, updateDocument, { ...currentOp, multi: false }) + ); + } + + /** Add a replace one operation to the bulk operation */ + replaceOne(replacement: Document): BulkOperationBase { + if (hasAtomicOperators(replacement)) { + throw new MongoInvalidArgumentError('Replacement document must not use atomic operators'); + } + + const currentOp = buildCurrentOp(this.bulkOperation); + return this.bulkOperation.addToOperationsList( + BatchType.UPDATE, + makeUpdateStatement(currentOp.selector, replacement, { ...currentOp, multi: false }) + ); + } + + /** Add a delete one operation to the bulk operation */ + deleteOne(): BulkOperationBase { + const currentOp = buildCurrentOp(this.bulkOperation); + return this.bulkOperation.addToOperationsList( + BatchType.DELETE, + makeDeleteStatement(currentOp.selector, { ...currentOp, limit: 1 }) + ); + } + + /** Add a delete many operation to the bulk operation */ + delete(): BulkOperationBase { + const currentOp = buildCurrentOp(this.bulkOperation); + return this.bulkOperation.addToOperationsList( + BatchType.DELETE, + makeDeleteStatement(currentOp.selector, { ...currentOp, limit: 0 }) + ); + } + + /** Upsert modifier for update bulk operation, noting that this operation is an upsert. */ + upsert(): this { + if (!this.bulkOperation.s.currentOp) { + this.bulkOperation.s.currentOp = {}; + } + + this.bulkOperation.s.currentOp.upsert = true; + return this; + } + + /** Specifies the collation for the query condition. */ + collation(collation: CollationOptions): this { + if (!this.bulkOperation.s.currentOp) { + this.bulkOperation.s.currentOp = {}; + } + + this.bulkOperation.s.currentOp.collation = collation; + return this; + } + + /** Specifies arrayFilters for UpdateOne or UpdateMany bulk operations. */ + arrayFilters(arrayFilters: Document[]): this { + if (!this.bulkOperation.s.currentOp) { + this.bulkOperation.s.currentOp = {}; + } + + this.bulkOperation.s.currentOp.arrayFilters = arrayFilters; + return this; + } + + /** Specifies hint for the bulk operation. */ + hint(hint: Hint): this { + if (!this.bulkOperation.s.currentOp) { + this.bulkOperation.s.currentOp = {}; + } + + this.bulkOperation.s.currentOp.hint = hint; + return this; + } +} + +/** @internal */ +export interface BulkOperationPrivate { + bulkResult: BulkResult; + currentBatch?: Batch; + currentIndex: number; + // ordered specific + currentBatchSize: number; + currentBatchSizeBytes: number; + // unordered specific + currentInsertBatch?: Batch; + currentUpdateBatch?: Batch; + currentRemoveBatch?: Batch; + batches: Batch[]; + // Write concern + writeConcern?: WriteConcern; + // Max batch size options + maxBsonObjectSize: number; + maxBatchSizeBytes: number; + maxWriteBatchSize: number; + maxKeySize: number; + // Namespace + namespace: MongoDBNamespace; + // Topology + topology: Topology; + // Options + options: BulkWriteOptions; + // BSON options + bsonOptions: BSONSerializeOptions; + // Document used to build a bulk operation + currentOp?: Document; + // Executed + executed: boolean; + // Collection + collection: Collection; + // Fundamental error + err?: AnyError; + // check keys + checkKeys: boolean; + bypassDocumentValidation?: boolean; +} + +/** @public */ +export interface BulkWriteOptions extends CommandOperationOptions { + /** + * Allow driver to bypass schema validation. + * @defaultValue `false` - documents will be validated by default + **/ + bypassDocumentValidation?: boolean; + /** + * If true, when an insert fails, don't execute the remaining writes. + * If false, continue with remaining inserts when one fails. + * @defaultValue `true` - inserts are ordered by default + */ + ordered?: boolean; + /** + * Force server to assign _id values instead of driver. + * @defaultValue `false` - the driver generates `_id` fields by default + **/ + forceServerObjectId?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + + /** @internal */ + timeoutContext?: TimeoutContext; +} + +/** @public */ +export abstract class BulkOperationBase { + isOrdered: boolean; + /** @internal */ + s: BulkOperationPrivate; + operationId?: number; + private collection: Collection; + /** @internal */ + retryWrites?: boolean; + + /** + * Create a new OrderedBulkOperation or UnorderedBulkOperation instance + * @internal + */ + constructor(collection: Collection, options: BulkWriteOptions, isOrdered: boolean) { + this.collection = collection; + this.retryWrites = collection.db.options?.retryWrites; + // determine whether bulkOperation is ordered or unordered + this.isOrdered = isOrdered; + + const topology = getTopology(collection); + options = options == null ? {} : options; + // TODO Bring from driver information in hello + // Get the namespace for the write operations + const namespace = collection.s.namespace; + // Used to mark operation as executed + const executed = false; + + // Current item + const currentOp = undefined; + + // Set max byte size + const hello = topology.lastHello(); + + // If we have autoEncryption on, batch-splitting must be done on 2mb chunks, but single documents + // over 2mb are still allowed + const usingAutoEncryption = !!(topology.s.options && topology.s.options.autoEncrypter); + const maxBsonObjectSize = + hello && hello.maxBsonObjectSize ? hello.maxBsonObjectSize : 1024 * 1024 * 16; + const maxBatchSizeBytes = usingAutoEncryption ? 1024 * 1024 * 2 : maxBsonObjectSize; + const maxWriteBatchSize = hello && hello.maxWriteBatchSize ? hello.maxWriteBatchSize : 1000; + + // Calculates the largest possible size of an Array key, represented as a BSON string + // element. This calculation: + // 1 byte for BSON type + // # of bytes = length of (string representation of (maxWriteBatchSize - 1)) + // + 1 bytes for null terminator + const maxKeySize = (maxWriteBatchSize - 1).toString(10).length + 2; + + // Final results + const bulkResult: BulkResult = { + ok: 1, + writeErrors: [], + writeConcernErrors: [], + insertedIds: [], + nInserted: 0, + nUpserted: 0, + nMatched: 0, + nModified: 0, + nRemoved: 0, + upserted: [] + }; + + // Internal state + this.s = { + // Final result + bulkResult, + // Current batch state + currentBatch: undefined, + currentIndex: 0, + // ordered specific + currentBatchSize: 0, + currentBatchSizeBytes: 0, + // unordered specific + currentInsertBatch: undefined, + currentUpdateBatch: undefined, + currentRemoveBatch: undefined, + batches: [], + // Write concern + writeConcern: WriteConcern.fromOptions(options), + // Max batch size options + maxBsonObjectSize, + maxBatchSizeBytes, + maxWriteBatchSize, + maxKeySize, + // Namespace + namespace, + // Topology + topology, + // Options + options: options, + // BSON options + bsonOptions: resolveBSONOptions(options), + // Current operation + currentOp, + // Executed + executed, + // Collection + collection, + // Fundamental error + err: undefined, + // check keys + checkKeys: typeof options.checkKeys === 'boolean' ? options.checkKeys : false + }; + + // bypass Validation + if (options.bypassDocumentValidation === true) { + this.s.bypassDocumentValidation = true; + } + } + + /** + * Add a single insert document to the bulk operation + * + * @example + * ```ts + * const bulkOp = collection.initializeOrderedBulkOp(); + * + * // Adds three inserts to the bulkOp. + * bulkOp + * .insert({ a: 1 }) + * .insert({ b: 2 }) + * .insert({ c: 3 }); + * await bulkOp.execute(); + * ``` + */ + insert(document: Document): BulkOperationBase { + maybeAddIdToDocuments(this.collection, document, { + forceServerObjectId: this.shouldForceServerObjectId() + }); + + return this.addToOperationsList(BatchType.INSERT, document); + } + + /** + * Builds a find operation for an update/updateOne/delete/deleteOne/replaceOne. + * Returns a builder object used to complete the definition of the operation. + * + * @example + * ```ts + * const bulkOp = collection.initializeOrderedBulkOp(); + * + * // Add an updateOne to the bulkOp + * bulkOp.find({ a: 1 }).updateOne({ $set: { b: 2 } }); + * + * // Add an updateMany to the bulkOp + * bulkOp.find({ c: 3 }).update({ $set: { d: 4 } }); + * + * // Add an upsert + * bulkOp.find({ e: 5 }).upsert().updateOne({ $set: { f: 6 } }); + * + * // Add a deletion + * bulkOp.find({ g: 7 }).deleteOne(); + * + * // Add a multi deletion + * bulkOp.find({ h: 8 }).delete(); + * + * // Add a replaceOne + * bulkOp.find({ i: 9 }).replaceOne({writeConcern: { j: 10 }}); + * + * // Update using a pipeline (requires Mongodb 4.2 or higher) + * bulk.find({ k: 11, y: { $exists: true }, z: { $exists: true } }).updateOne([ + * { $set: { total: { $sum: [ '$y', '$z' ] } } } + * ]); + * + * // All of the ops will now be executed + * await bulkOp.execute(); + * ``` + */ + find(selector: Document): FindOperators { + if (!selector) { + throw new MongoInvalidArgumentError('Bulk find operation must specify a selector'); + } + + // Save a current selector + this.s.currentOp = { + selector: selector + }; + + return new FindOperators(this); + } + + /** Specifies a raw operation to perform in the bulk write. */ + raw(op: AnyBulkWriteOperation): this { + if (op == null || typeof op !== 'object') { + throw new MongoInvalidArgumentError('Operation must be an object with an operation key'); + } + if ('insertOne' in op) { + const forceServerObjectId = this.shouldForceServerObjectId(); + const document = + op.insertOne && op.insertOne.document == null + ? // TODO(NODE-6003): remove support for omitting the `documents` subdocument in bulk inserts + (op.insertOne as Document) + : op.insertOne.document; + + maybeAddIdToDocuments(this.collection, document, { forceServerObjectId }); + + return this.addToOperationsList(BatchType.INSERT, document); + } + + if ('replaceOne' in op || 'updateOne' in op || 'updateMany' in op) { + if ('replaceOne' in op) { + if ('q' in op.replaceOne) { + throw new MongoInvalidArgumentError('Raw operations are not allowed'); + } + const updateStatement = makeUpdateStatement( + op.replaceOne.filter, + op.replaceOne.replacement, + { ...op.replaceOne, multi: false } + ); + if (hasAtomicOperators(updateStatement.u)) { + throw new MongoInvalidArgumentError('Replacement document must not use atomic operators'); + } + return this.addToOperationsList(BatchType.UPDATE, updateStatement); + } + + if ('updateOne' in op) { + if ('q' in op.updateOne) { + throw new MongoInvalidArgumentError('Raw operations are not allowed'); + } + const updateStatement = makeUpdateStatement(op.updateOne.filter, op.updateOne.update, { + ...op.updateOne, + multi: false + }); + if (!hasAtomicOperators(updateStatement.u, this.bsonOptions)) { + throw new MongoInvalidArgumentError('Update document requires atomic operators'); + } + return this.addToOperationsList(BatchType.UPDATE, updateStatement); + } + + if ('updateMany' in op) { + if ('q' in op.updateMany) { + throw new MongoInvalidArgumentError('Raw operations are not allowed'); + } + const updateStatement = makeUpdateStatement(op.updateMany.filter, op.updateMany.update, { + ...op.updateMany, + multi: true + }); + if (!hasAtomicOperators(updateStatement.u, this.bsonOptions)) { + throw new MongoInvalidArgumentError('Update document requires atomic operators'); + } + return this.addToOperationsList(BatchType.UPDATE, updateStatement); + } + } + + if ('deleteOne' in op) { + if ('q' in op.deleteOne) { + throw new MongoInvalidArgumentError('Raw operations are not allowed'); + } + return this.addToOperationsList( + BatchType.DELETE, + makeDeleteStatement(op.deleteOne.filter, { ...op.deleteOne, limit: 1 }) + ); + } + + if ('deleteMany' in op) { + if ('q' in op.deleteMany) { + throw new MongoInvalidArgumentError('Raw operations are not allowed'); + } + return this.addToOperationsList( + BatchType.DELETE, + makeDeleteStatement(op.deleteMany.filter, { ...op.deleteMany, limit: 0 }) + ); + } + + // otherwise an unknown operation was provided + throw new MongoInvalidArgumentError( + 'bulkWrite only supports insertOne, updateOne, updateMany, deleteOne, deleteMany' + ); + } + + get length(): number { + return this.s.currentIndex; + } + + get bsonOptions(): BSONSerializeOptions { + return this.s.bsonOptions; + } + + get writeConcern(): WriteConcern | undefined { + return this.s.writeConcern; + } + + get batches(): Batch[] { + const batches = [...this.s.batches]; + if (this.isOrdered) { + if (this.s.currentBatch) batches.push(this.s.currentBatch); + } else { + if (this.s.currentInsertBatch) batches.push(this.s.currentInsertBatch); + if (this.s.currentUpdateBatch) batches.push(this.s.currentUpdateBatch); + if (this.s.currentRemoveBatch) batches.push(this.s.currentRemoveBatch); + } + return batches; + } + + async execute(options: BulkWriteOptions = {}): Promise { + if (this.s.executed) { + throw new MongoBatchReExecutionError(); + } + + const writeConcern = WriteConcern.fromOptions(options); + if (writeConcern) { + this.s.writeConcern = writeConcern; + } + + // If we have current batch + if (this.isOrdered) { + if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch); + } else { + if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch); + if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch); + if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch); + } + // If we have no operations in the bulk raise an error + if (this.s.batches.length === 0) { + throw new MongoInvalidArgumentError('Invalid BulkOperation, Batch cannot be empty'); + } + + this.s.executed = true; + const finalOptions = resolveOptions(this.collection, { ...this.s.options, ...options }); + + // if there is no timeoutContext provided, create a timeoutContext and use it for + // all batches in the bulk operation + finalOptions.timeoutContext ??= TimeoutContext.create({ + session: finalOptions.session, + timeoutMS: finalOptions.timeoutMS, + serverSelectionTimeoutMS: this.collection.client.s.options.serverSelectionTimeoutMS, + waitQueueTimeoutMS: this.collection.client.s.options.waitQueueTimeoutMS + }); + + if (finalOptions.session == null) { + // if there is not an explicit session provided to `execute()`, create + // an implicit session and use that for all batches in the bulk operation + return await this.collection.client.withSession({ explicit: false }, async session => { + return await executeCommands(this, { ...finalOptions, session }); + }); + } + + return await executeCommands(this, { ...finalOptions }); + } + + /** + * Handles the write error before executing commands + * @internal + */ + handleWriteError(writeResult: BulkWriteResult): void { + if (this.s.bulkResult.writeErrors.length > 0) { + const msg = this.s.bulkResult.writeErrors[0].errmsg + ? this.s.bulkResult.writeErrors[0].errmsg + : 'write operation failed'; + + throw new MongoBulkWriteError( + { + message: msg, + code: this.s.bulkResult.writeErrors[0].code, + writeErrors: this.s.bulkResult.writeErrors + }, + writeResult + ); + } + + const writeConcernError = writeResult.getWriteConcernError(); + if (writeConcernError) { + throw new MongoBulkWriteError(writeConcernError, writeResult); + } + } + + abstract addToOperationsList( + batchType: BatchType, + document: Document | UpdateStatement | DeleteStatement + ): this; + + private shouldForceServerObjectId(): boolean { + return ( + this.s.options.forceServerObjectId === true || + this.s.collection.db.options?.forceServerObjectId === true + ); + } +} + +function isInsertBatch(batch: Batch): boolean { + return batch.batchType === BatchType.INSERT; +} + +function isUpdateBatch(batch: Batch): batch is Batch { + return batch.batchType === BatchType.UPDATE; +} + +function isDeleteBatch(batch: Batch): batch is Batch { + return batch.batchType === BatchType.DELETE; +} + +function buildCurrentOp(bulkOp: BulkOperationBase): Document { + let { currentOp } = bulkOp.s; + bulkOp.s.currentOp = undefined; + if (!currentOp) currentOp = {}; + return currentOp; +} diff --git a/gateway/node_modules/mongodb/src/bulk/ordered.ts b/gateway/node_modules/mongodb/src/bulk/ordered.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a9e9ece2b15b3bcbc1463b415b75ff883f87b71 --- /dev/null +++ b/gateway/node_modules/mongodb/src/bulk/ordered.ts @@ -0,0 +1,83 @@ +import type { Document } from '../bson'; +import * as BSON from '../bson'; +import type { Collection } from '../collection'; +import { MongoInvalidArgumentError } from '../error'; +import type { DeleteStatement } from '../operations/delete'; +import type { UpdateStatement } from '../operations/update'; +import { Batch, BatchType, BulkOperationBase, type BulkWriteOptions } from './common'; + +/** @public */ +export class OrderedBulkOperation extends BulkOperationBase { + /** @internal */ + constructor(collection: Collection, options: BulkWriteOptions) { + super(collection, options, true); + } + + addToOperationsList( + batchType: BatchType, + document: Document | UpdateStatement | DeleteStatement + ): this { + // Get the bsonSize + const bsonSize = BSON.calculateObjectSize(document, { + checkKeys: false, + // Since we don't know what the user selected for BSON options here, + // err on the safe side, and check the size with ignoreUndefined: false. + ignoreUndefined: false + } as any); + + // Throw error if the doc is bigger than the max BSON size + if (bsonSize >= this.s.maxBsonObjectSize) + // TODO(NODE-3483): Change this to MongoBSONError + throw new MongoInvalidArgumentError( + `Document is larger than the maximum size ${this.s.maxBsonObjectSize}` + ); + + // Create a new batch object if we don't have a current one + if (this.s.currentBatch == null) { + this.s.currentBatch = new Batch(batchType, this.s.currentIndex); + } + + const maxKeySize = this.s.maxKeySize; + + // Check if we need to create a new batch + if ( + // New batch if we exceed the max batch op size + this.s.currentBatchSize + 1 >= this.s.maxWriteBatchSize || + // New batch if we exceed the maxBatchSizeBytes. Only matters if batch already has a doc, + // since we can't sent an empty batch + (this.s.currentBatchSize > 0 && + this.s.currentBatchSizeBytes + maxKeySize + bsonSize >= this.s.maxBatchSizeBytes) || + // New batch if the new op does not have the same op type as the current batch + this.s.currentBatch.batchType !== batchType + ) { + // Save the batch to the execution stack + this.s.batches.push(this.s.currentBatch); + + // Create a new batch + this.s.currentBatch = new Batch(batchType, this.s.currentIndex); + + // Reset the current size trackers + this.s.currentBatchSize = 0; + this.s.currentBatchSizeBytes = 0; + } + + if (batchType === BatchType.INSERT) { + this.s.bulkResult.insertedIds.push({ + index: this.s.currentIndex, + _id: (document as Document)._id + }); + } + + // We have an array of documents + if (Array.isArray(document)) { + throw new MongoInvalidArgumentError('Operation passed in cannot be an Array'); + } + + this.s.currentBatch.originalIndexes.push(this.s.currentIndex); + this.s.currentBatch.operations.push(document); + this.s.currentBatchSize += 1; + this.s.currentBatchSizeBytes += maxKeySize + bsonSize; + this.s.currentIndex += 1; + return this; + } +} diff --git a/gateway/node_modules/mongodb/src/bulk/unordered.ts b/gateway/node_modules/mongodb/src/bulk/unordered.ts new file mode 100644 index 0000000000000000000000000000000000000000..97a134613b05a4a3a7c59b8a307c5592056b3b86 --- /dev/null +++ b/gateway/node_modules/mongodb/src/bulk/unordered.ts @@ -0,0 +1,115 @@ +import type { Document } from '../bson'; +import * as BSON from '../bson'; +import type { Collection } from '../collection'; +import { MongoInvalidArgumentError } from '../error'; +import type { DeleteStatement } from '../operations/delete'; +import type { UpdateStatement } from '../operations/update'; +import { + Batch, + BatchType, + BulkOperationBase, + type BulkWriteOptions, + type BulkWriteResult +} from './common'; + +/** @public */ +export class UnorderedBulkOperation extends BulkOperationBase { + /** @internal */ + constructor(collection: Collection, options: BulkWriteOptions) { + super(collection, options, false); + } + + override handleWriteError(writeResult: BulkWriteResult): void { + if (this.s.batches.length) { + return; + } + + return super.handleWriteError(writeResult); + } + + addToOperationsList( + batchType: BatchType, + document: Document | UpdateStatement | DeleteStatement + ): this { + // Get the bsonSize + const bsonSize = BSON.calculateObjectSize(document, { + checkKeys: false, + + // Since we don't know what the user selected for BSON options here, + // err on the safe side, and check the size with ignoreUndefined: false. + ignoreUndefined: false + } as any); + + // Throw error if the doc is bigger than the max BSON size + if (bsonSize >= this.s.maxBsonObjectSize) { + // TODO(NODE-3483): Change this to MongoBSONError + throw new MongoInvalidArgumentError( + `Document is larger than the maximum size ${this.s.maxBsonObjectSize}` + ); + } + + // Holds the current batch + this.s.currentBatch = undefined; + // Get the right type of batch + if (batchType === BatchType.INSERT) { + this.s.currentBatch = this.s.currentInsertBatch; + } else if (batchType === BatchType.UPDATE) { + this.s.currentBatch = this.s.currentUpdateBatch; + } else if (batchType === BatchType.DELETE) { + this.s.currentBatch = this.s.currentRemoveBatch; + } + + const maxKeySize = this.s.maxKeySize; + + // Create a new batch object if we don't have a current one + if (this.s.currentBatch == null) { + this.s.currentBatch = new Batch(batchType, this.s.currentIndex); + } + + // Check if we need to create a new batch + if ( + // New batch if we exceed the max batch op size + this.s.currentBatch.size + 1 >= this.s.maxWriteBatchSize || + // New batch if we exceed the maxBatchSizeBytes. Only matters if batch already has a doc, + // since we can't sent an empty batch + (this.s.currentBatch.size > 0 && + this.s.currentBatch.sizeBytes + maxKeySize + bsonSize >= this.s.maxBatchSizeBytes) || + // New batch if the new op does not have the same op type as the current batch + this.s.currentBatch.batchType !== batchType + ) { + // Save the batch to the execution stack + this.s.batches.push(this.s.currentBatch); + + // Create a new batch + this.s.currentBatch = new Batch(batchType, this.s.currentIndex); + } + + // We have an array of documents + if (Array.isArray(document)) { + throw new MongoInvalidArgumentError('Operation passed in cannot be an Array'); + } + + this.s.currentBatch.operations.push(document); + this.s.currentBatch.originalIndexes.push(this.s.currentIndex); + this.s.currentIndex = this.s.currentIndex + 1; + + // Save back the current Batch to the right type + if (batchType === BatchType.INSERT) { + this.s.currentInsertBatch = this.s.currentBatch; + this.s.bulkResult.insertedIds.push({ + index: this.s.bulkResult.insertedIds.length, + _id: (document as Document)._id + }); + } else if (batchType === BatchType.UPDATE) { + this.s.currentUpdateBatch = this.s.currentBatch; + } else if (batchType === BatchType.DELETE) { + this.s.currentRemoveBatch = this.s.currentBatch; + } + + // Update current batch size + this.s.currentBatch.size += 1; + this.s.currentBatch.sizeBytes += maxKeySize + bsonSize; + + return this; + } +} diff --git a/gateway/node_modules/mongodb/src/change_stream.ts b/gateway/node_modules/mongodb/src/change_stream.ts new file mode 100644 index 0000000000000000000000000000000000000000..189fe3ee6d72ef9f0bc304a63b5cd1615f362057 --- /dev/null +++ b/gateway/node_modules/mongodb/src/change_stream.ts @@ -0,0 +1,1164 @@ +import type { Readable } from 'stream'; + +import type { Binary, Document, Timestamp } from './bson'; +import { Collection } from './collection'; +import { CHANGE, CLOSE, END, ERROR, INIT, MORE, RESPONSE, RESUME_TOKEN_CHANGED } from './constants'; +import { CursorTimeoutContext } from './cursor/abstract_cursor'; +import { ChangeStreamCursor, type ChangeStreamCursorOptions } from './cursor/change_stream_cursor'; +import { Db } from './db'; +import { + type AnyError, + isResumableError, + MongoAPIError, + MongoChangeStreamError, + MongoOperationTimeoutError, + MongoRuntimeError +} from './error'; +import { MongoClient } from './mongo_client'; +import { type InferIdType, TypedEventEmitter } from './mongo_types'; +import type { AggregateOptions } from './operations/aggregate'; +import type { OperationParent } from './operations/command'; +import { DeprioritizedServers } from './sdam/server_selection'; +import type { ServerSessionId } from './sessions'; +import { CSOTTimeoutContext, type TimeoutContext } from './timeout'; +import { type AnyOptions, getTopology, type MongoDBNamespace, squashError } from './utils'; + +const CHANGE_DOMAIN_TYPES = { + COLLECTION: Symbol('Collection'), + DATABASE: Symbol('Database'), + CLUSTER: Symbol('Cluster') +}; + +const CHANGE_STREAM_EVENTS = [RESUME_TOKEN_CHANGED, END, CLOSE] as const; + +const NO_RESUME_TOKEN_ERROR = + 'A change stream document has been received that lacks a resume token (_id).'; +const CHANGESTREAM_CLOSED_ERROR = 'ChangeStream is closed'; + +const INVALID_STAGE_OPTIONS = buildDisallowedChangeStreamOptions(); + +export function filterOutOptions(options: AnyOptions): AnyOptions { + return Object.fromEntries( + Object.entries(options).filter(([k, _]) => !INVALID_STAGE_OPTIONS.has(k)) + ); +} + +/** + * Represents the logical starting point for a new ChangeStream or resuming a ChangeStream on the server. + * @see https://www.mongodb.com/docs/manual/changeStreams/#std-label-change-stream-resume + * @public + */ +export type ResumeToken = unknown; + +/** + * Represents a specific point in time on a server. Can be retrieved by using `db.command()` + * @public + * @see https://www.mongodb.com/docs/manual/reference/method/db.runCommand/#response + */ +export type OperationTime = Timestamp; + +/** + * Options that can be passed to a ChangeStream. Note that startAfter, resumeAfter, and startAtOperationTime are all mutually exclusive, and the server will error if more than one is specified. + * @public + */ +export interface ChangeStreamOptions extends Omit { + /** + * Allowed values: 'updateLookup', 'whenAvailable', 'required'. + * + * When set to 'updateLookup', the change notification for partial updates + * will include both a delta describing the changes to the document as well + * as a copy of the entire document that was changed from some time after + * the change occurred. + * + * When set to 'whenAvailable', configures the change stream to return the + * post-image of the modified document for replace and update change events + * if the post-image for this event is available. + * + * When set to 'required', the same behavior as 'whenAvailable' except that + * an error is raised if the post-image is not available. + */ + fullDocument?: string; + + /** + * Allowed values: 'whenAvailable', 'required', 'off'. + * + * The default is to not send a value, which is equivalent to 'off'. + * + * When set to 'whenAvailable', configures the change stream to return the + * pre-image of the modified document for replace, update, and delete change + * events if it is available. + * + * When set to 'required', the same behavior as 'whenAvailable' except that + * an error is raised if the pre-image is not available. + */ + fullDocumentBeforeChange?: string; + /** The maximum amount of time for the server to wait on new documents to satisfy a change stream query. */ + maxAwaitTimeMS?: number; + /** + * Allows you to start a changeStream after a specified event. + * @see https://www.mongodb.com/docs/manual/changeStreams/#resumeafter-for-change-streams + */ + resumeAfter?: ResumeToken; + /** + * Similar to resumeAfter, but will allow you to start after an invalidated event. + * @see https://www.mongodb.com/docs/manual/changeStreams/#startafter-for-change-streams + */ + startAfter?: ResumeToken; + /** Will start the changeStream after the specified operationTime. */ + startAtOperationTime?: OperationTime; + /** + * The number of documents to return per batch. + * @see https://www.mongodb.com/docs/manual/reference/command/aggregate + */ + batchSize?: number; + + /** + * When enabled, configures the change stream to include extra change events. + * + * - createIndexes + * - dropIndexes + * - modify + * - create + * - shardCollection + * - reshardCollection + * - refineCollectionShardKey + */ + showExpandedEvents?: boolean; +} + +/** @public */ +export interface ChangeStreamNameSpace { + db: string; + coll: string; +} + +/** @public */ +export interface ChangeStreamDocumentKey { + /** + * For unsharded collections this contains a single field `_id`. + * For sharded collections, this will contain all the components of the shard key + */ + documentKey: { _id: InferIdType; [shardKey: string]: any }; +} + +/** @public */ +export interface ChangeStreamSplitEvent { + /** Which fragment of the change this is. */ + fragment: number; + /** The total number of fragments. */ + of: number; +} + +/** @public */ +export interface ChangeStreamDocumentCommon { + /** + * The id functions as an opaque token for use when resuming an interrupted + * change stream. + */ + _id: ResumeToken; + /** + * The timestamp from the oplog entry associated with the event. + * For events that happened as part of a multi-document transaction, the associated change stream + * notifications will have the same clusterTime value, namely the time when the transaction was committed. + * On a sharded cluster, events that occur on different shards can have the same clusterTime but be + * associated with different transactions or even not be associated with any transaction. + * To identify events for a single transaction, you can use the combination of lsid and txnNumber in the change stream event document. + */ + clusterTime?: Timestamp; + + /** + * The transaction number. + * Only present if the operation is part of a multi-document transaction. + * + * **NOTE:** txnNumber can be a Long if promoteLongs is set to false + */ + txnNumber?: number; + + /** + * The identifier for the session associated with the transaction. + * Only present if the operation is part of a multi-document transaction. + */ + lsid?: ServerSessionId; + + /** + * When the change stream's backing aggregation pipeline contains the $changeStreamSplitLargeEvent + * stage, events larger than 16MB will be split into multiple events and contain the + * following information about which fragment the current event is. + */ + splitEvent?: ChangeStreamSplitEvent; +} + +/** @public */ +export interface ChangeStreamDocumentWallTime { + /** + * The server date and time of the database operation. + * wallTime differs from clusterTime in that clusterTime is a timestamp taken from the oplog entry associated with the database operation event. + * @sinceServerVersion 6.0.0 + */ + wallTime?: Date; +} + +/** @public */ +export interface ChangeStreamDocumentCollectionUUID { + /** + * The UUID (Binary subtype 4) of the collection that the operation was performed on. + * + * Only present when the `showExpandedEvents` flag is enabled. + * + * **NOTE:** collectionUUID will be converted to a NodeJS Buffer if the promoteBuffers + * flag is enabled. + * + * @sinceServerVersion 6.1.0 + */ + collectionUUID: Binary; +} + +/** @public */ +export interface ChangeStreamDocumentOperationDescription { + /** + * An description of the operation. + * + * Only present when the `showExpandedEvents` flag is enabled. + * + * @sinceServerVersion 6.1.0 + */ + operationDescription?: Document; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#insert-event + */ +export interface ChangeStreamInsertDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentKey, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'insert'; + /** This key will contain the document being inserted */ + fullDocument: TSchema; + /** Namespace the insert event occurred on */ + ns: ChangeStreamNameSpace; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#update-event + */ +export interface ChangeStreamUpdateDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentKey, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'update'; + /** + * This is only set if `fullDocument` is set to `'updateLookup'` + * Contains the point-in-time post-image of the modified document if the + * post-image is available and either 'required' or 'whenAvailable' was + * specified for the 'fullDocument' option when creating the change stream. + */ + fullDocument?: TSchema; + /** Contains a description of updated and removed fields in this operation */ + updateDescription: UpdateDescription; + /** Namespace the update event occurred on */ + ns: ChangeStreamNameSpace; + /** + * Contains the pre-image of the modified or deleted document if the + * pre-image is available for the change event and either 'required' or + * 'whenAvailable' was specified for the 'fullDocumentBeforeChange' option + * when creating the change stream. If 'whenAvailable' was specified but the + * pre-image is unavailable, this will be explicitly set to null. + */ + fullDocumentBeforeChange?: TSchema; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#replace-event + */ +export interface ChangeStreamReplaceDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentKey, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'replace'; + /** The fullDocument of a replace event represents the document after the insert of the replacement document */ + fullDocument: TSchema; + /** Namespace the replace event occurred on */ + ns: ChangeStreamNameSpace; + /** + * Contains the pre-image of the modified or deleted document if the + * pre-image is available for the change event and either 'required' or + * 'whenAvailable' was specified for the 'fullDocumentBeforeChange' option + * when creating the change stream. If 'whenAvailable' was specified but the + * pre-image is unavailable, this will be explicitly set to null. + */ + fullDocumentBeforeChange?: TSchema; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#delete-event + */ +export interface ChangeStreamDeleteDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentKey, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'delete'; + /** Namespace the delete event occurred on */ + ns: ChangeStreamNameSpace; + /** + * Contains the pre-image of the modified or deleted document if the + * pre-image is available for the change event and either 'required' or + * 'whenAvailable' was specified for the 'fullDocumentBeforeChange' option + * when creating the change stream. If 'whenAvailable' was specified but the + * pre-image is unavailable, this will be explicitly set to null. + */ + fullDocumentBeforeChange?: TSchema; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#drop-event + */ +export interface ChangeStreamDropDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'drop'; + /** Namespace the drop event occurred on */ + ns: ChangeStreamNameSpace; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#rename-event + */ +export interface ChangeStreamRenameDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'rename'; + /** The new name for the `ns.coll` collection */ + to: { db: string; coll: string }; + /** The "from" namespace that the rename occurred on */ + ns: ChangeStreamNameSpace; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#dropdatabase-event + */ +export interface ChangeStreamDropDatabaseDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'dropDatabase'; + /** The database dropped */ + ns: { db: string }; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/#invalidate-event + */ +export interface ChangeStreamInvalidateDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'invalidate'; +} + +/** + * Only present when the `showExpandedEvents` flag is enabled. + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/createIndexes/#mongodb-data-createIndexes + */ +export interface ChangeStreamCreateIndexDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentOperationDescription, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'createIndexes'; +} + +/** + * Only present when the `showExpandedEvents` flag is enabled. + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/dropIndexes/#mongodb-data-dropIndexes + */ +export interface ChangeStreamDropIndexDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentOperationDescription, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'dropIndexes'; +} + +/** + * Only present when the `showExpandedEvents` flag is enabled. + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/modify/#mongodb-data-modify + */ +export interface ChangeStreamCollModDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'modify'; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/create/#mongodb-data-create + */ +export interface ChangeStreamCreateDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'create'; + + /** + * The type of the newly created object. + * + * @sinceServerVersion 8.1.0 + */ + nsType?: 'collection' | 'timeseries' | 'view'; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/shardCollection/#mongodb-data-shardCollection + */ +export interface ChangeStreamShardCollectionDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentOperationDescription, + ChangeStreamDocumentWallTime { + /** Describes the type of operation represented in this change notification */ + operationType: 'shardCollection'; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/reshardCollection/#mongodb-data-reshardCollection + */ +export interface ChangeStreamReshardCollectionDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentOperationDescription { + /** Describes the type of operation represented in this change notification */ + operationType: 'reshardCollection'; +} + +/** + * @public + * @see https://www.mongodb.com/docs/manual/reference/change-events/refineCollectionShardKey/#mongodb-data-refineCollectionShardKey + */ +export interface ChangeStreamRefineCollectionShardKeyDocument + extends ChangeStreamDocumentCommon, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentOperationDescription { + /** Describes the type of operation represented in this change notification */ + operationType: 'refineCollectionShardKey'; +} + +/** @public */ +export type ChangeStreamDocument = + | ChangeStreamInsertDocument + | ChangeStreamUpdateDocument + | ChangeStreamReplaceDocument + | ChangeStreamDeleteDocument + | ChangeStreamDropDocument + | ChangeStreamRenameDocument + | ChangeStreamDropDatabaseDocument + | ChangeStreamInvalidateDocument + | ChangeStreamCreateIndexDocument + | ChangeStreamCreateDocument + | ChangeStreamCollModDocument + | ChangeStreamDropIndexDocument + | ChangeStreamShardCollectionDocument + | ChangeStreamReshardCollectionDocument + | ChangeStreamRefineCollectionShardKeyDocument; + +/** @public */ +export interface UpdateDescription { + /** + * A document containing key:value pairs of names of the fields that were + * changed, and the new value for those fields. + */ + updatedFields?: Partial; + + /** + * An array of field names that were removed from the document. + */ + removedFields?: string[]; + + /** + * An array of documents which record array truncations performed with pipeline-based updates using one or more of the following stages: + * - $addFields + * - $set + * - $replaceRoot + * - $replaceWith + */ + truncatedArrays?: Array<{ + /** The name of the truncated field. */ + field: string; + /** The number of elements in the truncated array. */ + newSize: number; + }>; + + /** + * A document containing additional information about any ambiguous update paths from the update event. The document + * maps the full ambiguous update path to an array containing the actual resolved components of the path. For example, + * given a document shaped like `{ a: { '0': 0 } }`, and an update of `{ $inc: 'a.0' }`, disambiguated paths would look like + * the following: + * + * ``` + * { + * 'a.0': ['a', '0'] + * } + * ``` + * + * This field is only present when there are ambiguous paths that are updated as a part of the update event. + * + * On \<8.2.0 servers, this field is only present when `showExpandedEvents` is set to true. + * is enabled for the change stream. + * + * On 8.2.0+ servers, this field is present for update events regardless of whether `showExpandedEvents` is enabled. + * @sinceServerVersion 6.1.0 + */ + disambiguatedPaths?: Document; +} + +/** @public */ +export type ChangeStreamEvents< + TSchema extends Document = Document, + TChange extends Document = ChangeStreamDocument +> = { + resumeTokenChanged(token: ResumeToken): void; + init(response: any): void; + more(response?: any): void; + response(): void; + end(): void; + error(error: Error): void; + change(change: TChange): void; + /** + * @remarks Note that the `close` event is currently emitted whenever the internal `ChangeStreamCursor` + * instance is closed, which can occur multiple times for a given `ChangeStream` instance. + * + * TODO(NODE-6434): address this issue in NODE-6434 + */ + close(): void; +}; + +/** + * Creates a new Change Stream instance. Normally created using {@link Collection#watch|Collection.watch()}. + * @public + */ +export class ChangeStream< + TSchema extends Document = Document, + TChange extends Document = ChangeStreamDocument + > + extends TypedEventEmitter> + implements AsyncDisposable +{ + /** + * An alias for {@link ChangeStream.close|ChangeStream.close()}. + */ + async [Symbol.asyncDispose]() { + await this.close(); + } + + pipeline: Document[]; + /** + * @remarks WriteConcern can still be present on the options because + * we inherit options from the client/db/collection. The + * key must be present on the options in order to delete it. + * This allows typescript to delete the key but will + * not allow a writeConcern to be assigned as a property on options. + */ + options: ChangeStreamOptions & { writeConcern?: never }; + parent: MongoClient | Db | Collection; + namespace: MongoDBNamespace; + type: symbol; + /** @internal */ + private cursor: ChangeStreamCursor; + /** @internal */ + private cursorStream?: Readable & AsyncIterable; + /** @internal */ + private isClosed: boolean; + /** @internal */ + private mode: false | 'iterator' | 'emitter'; + + /** @event */ + static readonly RESPONSE = RESPONSE; + /** @event */ + static readonly MORE = MORE; + /** @event */ + static readonly INIT = INIT; + /** @event */ + static readonly CLOSE = CLOSE; + /** + * Fired for each new matching change in the specified namespace. Attaching a `change` + * event listener to a Change Stream will switch the stream into flowing mode. Data will + * then be passed as soon as it is available. + * @event + */ + static readonly CHANGE = CHANGE; + /** @event */ + static readonly END = END; + /** @event */ + static readonly ERROR = ERROR; + /** + * Emitted each time the change stream stores a new resume token. + * @event + */ + static readonly RESUME_TOKEN_CHANGED = RESUME_TOKEN_CHANGED; + + private timeoutContext?: TimeoutContext; + /** + * Note that this property is here to uniquely identify a ChangeStream instance as the owner of + * the {@link CursorTimeoutContext} instance (see {@link ChangeStream._createChangeStreamCursor}) to ensure + * that {@link AbstractCursor.close} does not mutate the timeoutContext. + */ + private contextOwner: symbol; + /** + * @internal + * + * @param parent - The parent object that created this change stream + * @param pipeline - An array of {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents + */ + constructor( + parent: OperationParent, + pipeline: Document[] = [], + options: ChangeStreamOptions = {} + ) { + super(); + + this.pipeline = pipeline; + this.options = { ...options }; + let serverSelectionTimeoutMS: number; + delete this.options.writeConcern; + + if (parent instanceof Collection) { + this.type = CHANGE_DOMAIN_TYPES.COLLECTION; + serverSelectionTimeoutMS = parent.s.db.client.options.serverSelectionTimeoutMS; + } else if (parent instanceof Db) { + this.type = CHANGE_DOMAIN_TYPES.DATABASE; + serverSelectionTimeoutMS = parent.client.options.serverSelectionTimeoutMS; + } else if (parent instanceof MongoClient) { + this.type = CHANGE_DOMAIN_TYPES.CLUSTER; + serverSelectionTimeoutMS = parent.options.serverSelectionTimeoutMS; + } else { + throw new MongoChangeStreamError( + 'Parent provided to ChangeStream constructor must be an instance of Collection, Db, or MongoClient' + ); + } + + this.contextOwner = Symbol(); + this.parent = parent; + this.namespace = parent.s.namespace; + if (!this.options.readPreference && parent.readPreference) { + this.options.readPreference = parent.readPreference; + } + + // Create contained Change Stream cursor + this.cursor = this._createChangeStreamCursor(options); + + this.isClosed = false; + this.mode = false; + + // Listen for any `change` listeners being added to ChangeStream + this.on('newListener', eventName => { + if (eventName === 'change' && this.cursor && this.listenerCount('change') === 0) { + this._streamEvents(this.cursor); + } + }); + + this.on('removeListener', eventName => { + if (eventName === 'change' && this.listenerCount('change') === 0 && this.cursor) { + this.cursorStream?.removeAllListeners('data'); + } + }); + + if (this.options.timeoutMS != null) { + this.timeoutContext = new CSOTTimeoutContext({ + timeoutMS: this.options.timeoutMS, + serverSelectionTimeoutMS + }); + } + } + + /** The cached resume token that is used to resume after the most recently returned change. */ + get resumeToken(): ResumeToken { + return this.cursor?.resumeToken; + } + + /** Returns the currently buffered documents length of the underlying cursor. */ + bufferedCount(): number { + return this.cursor?.bufferedCount() ?? 0; + } + + /** Check if there is any document still available in the Change Stream */ + async hasNext(): Promise { + this._setIsIterator(); + // Change streams must resume indefinitely while each resume event succeeds. + // This loop continues until either a change event is received or until a resume attempt + // fails. + + this.timeoutContext?.refresh(); + try { + while (true) { + try { + const hasNext = await this.cursor.hasNext(); + return hasNext; + } catch (error) { + try { + await this._processErrorIteratorMode(error, this.cursor.id != null); + } catch (error) { + if (error instanceof MongoOperationTimeoutError && this.cursor.id == null) { + throw error; + } + try { + await this.close(); + } catch (error) { + squashError(error); + } + throw error; + } + } + } + } finally { + this.timeoutContext?.clear(); + } + } + + /** Get the next available document from the Change Stream. */ + async next(): Promise { + this._setIsIterator(); + // Change streams must resume indefinitely while each resume event succeeds. + // This loop continues until either a change event is received or until a resume attempt + // fails. + this.timeoutContext?.refresh(); + + try { + while (true) { + try { + const change = await this.cursor.next(); + const processedChange = this._processChange(change ?? null); + return processedChange; + } catch (error) { + try { + await this._processErrorIteratorMode(error, this.cursor.id != null); + } catch (error) { + if (error instanceof MongoOperationTimeoutError && this.cursor.id == null) { + throw error; + } + try { + await this.close(); + } catch (error) { + squashError(error); + } + throw error; + } + } + } + } finally { + this.timeoutContext?.clear(); + } + } + + /** + * Try to get the next available document from the Change Stream's cursor or `null` if an empty batch is returned + */ + async tryNext(): Promise { + this._setIsIterator(); + // Change streams must resume indefinitely while each resume event succeeds. + // This loop continues until either a change event is received or until a resume attempt + // fails. + this.timeoutContext?.refresh(); + + try { + while (true) { + try { + const change = await this.cursor.tryNext(); + if (!change) { + return null; + } + const processedChange = this._processChange(change); + return processedChange; + } catch (error) { + try { + await this._processErrorIteratorMode(error, this.cursor.id != null); + } catch (error) { + if (error instanceof MongoOperationTimeoutError && this.cursor.id == null) throw error; + try { + await this.close(); + } catch (error) { + squashError(error); + } + throw error; + } + } + } + } finally { + this.timeoutContext?.clear(); + } + } + + async *[Symbol.asyncIterator](): AsyncGenerator { + if (this.closed) { + return; + } + + try { + // Change streams run indefinitely as long as errors are resumable + // So the only loop breaking condition is if `next()` throws + while (true) { + yield await this.next(); + } + } finally { + try { + await this.close(); + } catch (error) { + squashError(error); + } + } + } + + /** Is the cursor closed */ + public get closed(): boolean { + return this.isClosed || this.cursor.closed; + } + + /** + * Frees the internal resources used by the change stream. + */ + async close(): Promise { + this.timeoutContext?.clear(); + this.timeoutContext = undefined; + this.isClosed = true; + + const cursor = this.cursor; + try { + await cursor.close(); + } finally { + this._endStream(); + } + } + + /** + * Return a modified Readable stream including a possible transform method. + * + * NOTE: When using a Stream to process change stream events, the stream will + * NOT automatically resume in the case a resumable error is encountered. + * + * @throws MongoChangeStreamError if the underlying cursor or the change stream is closed + */ + stream(): Readable & AsyncIterable { + if (this.closed) { + throw new MongoChangeStreamError(CHANGESTREAM_CLOSED_ERROR); + } + + return this.cursor.stream(); + } + + /** @internal */ + private _setIsEmitter(): void { + if (this.mode === 'iterator') { + // TODO(NODE-3485): Replace with MongoChangeStreamModeError + throw new MongoAPIError( + 'ChangeStream cannot be used as an EventEmitter after being used as an iterator' + ); + } + this.mode = 'emitter'; + } + + /** @internal */ + private _setIsIterator(): void { + if (this.mode === 'emitter') { + // TODO(NODE-3485): Replace with MongoChangeStreamModeError + throw new MongoAPIError( + 'ChangeStream cannot be used as an iterator after being used as an EventEmitter' + ); + } + this.mode = 'iterator'; + } + + /** + * Create a new change stream cursor based on self's configuration + * @internal + */ + private _createChangeStreamCursor( + options: ChangeStreamOptions | ChangeStreamCursorOptions + ): ChangeStreamCursor { + const changeStreamStageOptions: Document = filterOutOptions(options); + if (this.type === CHANGE_DOMAIN_TYPES.CLUSTER) { + changeStreamStageOptions.allChangesForCluster = true; + } + const pipeline = [{ $changeStream: changeStreamStageOptions }, ...this.pipeline]; + + const client: MongoClient | null = + this.type === CHANGE_DOMAIN_TYPES.CLUSTER + ? (this.parent as MongoClient) + : this.type === CHANGE_DOMAIN_TYPES.DATABASE + ? (this.parent as Db).client + : this.type === CHANGE_DOMAIN_TYPES.COLLECTION + ? (this.parent as Collection).client + : null; + + if (client == null) { + // This should never happen because of the assertion in the constructor + throw new MongoRuntimeError( + `Changestream type should only be one of cluster, database, collection. Found ${this.type.toString()}` + ); + } + + const changeStreamCursor = new ChangeStreamCursor( + client, + this.namespace, + pipeline, + { + ...options, + timeoutContext: this.timeoutContext + ? new CursorTimeoutContext(this.timeoutContext, this.contextOwner) + : undefined + } + ); + + for (const event of CHANGE_STREAM_EVENTS) { + changeStreamCursor.on(event, e => this.emit(event, e)); + } + + if (this.listenerCount(ChangeStream.CHANGE) > 0) { + this._streamEvents(changeStreamCursor); + } + + return changeStreamCursor; + } + + /** @internal */ + private _closeEmitterModeWithError(error: AnyError): void { + this.emit(ChangeStream.ERROR, error); + + this.close().then(undefined, squashError); + } + + /** @internal */ + private _streamEvents(cursor: ChangeStreamCursor): void { + this._setIsEmitter(); + const stream = this.cursorStream ?? cursor.stream(); + this.cursorStream = stream; + stream.on('data', change => { + try { + const processedChange = this._processChange(change); + this.emit(ChangeStream.CHANGE, processedChange); + } catch (error) { + this.emit(ChangeStream.ERROR, error); + } + this.timeoutContext?.refresh(); + }); + stream.on('error', error => this._processErrorStreamMode(error, this.cursor.id != null)); + } + + /** @internal */ + private _endStream(): void { + this.cursorStream?.removeAllListeners('data'); + this.cursorStream?.removeAllListeners('close'); + this.cursorStream?.removeAllListeners('end'); + this.cursorStream?.destroy(); + this.cursorStream = undefined; + } + + /** @internal */ + private _processChange(change: TChange | null): TChange { + if (this.isClosed) { + // TODO(NODE-3485): Replace with MongoChangeStreamClosedError + throw new MongoAPIError(CHANGESTREAM_CLOSED_ERROR); + } + + // a null change means the cursor has been notified, implicitly closing the change stream + if (change == null) { + // TODO(NODE-3485): Replace with MongoChangeStreamClosedError + throw new MongoRuntimeError(CHANGESTREAM_CLOSED_ERROR); + } + + if (change && !change._id) { + throw new MongoChangeStreamError(NO_RESUME_TOKEN_ERROR); + } + + // cache the resume token + this.cursor.cacheResumeToken(change._id); + + // wipe the startAtOperationTime if there was one so that there won't be a conflict + // between resumeToken and startAtOperationTime if we need to reconnect the cursor + this.options.startAtOperationTime = undefined; + + return change; + } + + /** @internal */ + private _processErrorStreamMode(changeStreamError: AnyError, cursorInitialized: boolean) { + // If the change stream has been closed explicitly, do not process error. + if (this.isClosed) return; + + if ( + cursorInitialized && + (isResumableError(changeStreamError, this.cursor.maxWireVersion) || + changeStreamError instanceof MongoOperationTimeoutError) + ) { + this._endStream(); + + this.cursor + .close() + .then( + () => this._resume(changeStreamError), + e => { + squashError(e); + return this._resume(changeStreamError); + } + ) + .then( + () => { + if (changeStreamError instanceof MongoOperationTimeoutError) + this.emit(ChangeStream.ERROR, changeStreamError); + }, + () => this._closeEmitterModeWithError(changeStreamError) + ); + } else { + this._closeEmitterModeWithError(changeStreamError); + } + } + + /** @internal */ + private async _processErrorIteratorMode(changeStreamError: AnyError, cursorInitialized: boolean) { + if (this.isClosed) { + // TODO(NODE-3485): Replace with MongoChangeStreamClosedError + throw new MongoAPIError(CHANGESTREAM_CLOSED_ERROR); + } + + if ( + cursorInitialized && + (isResumableError(changeStreamError, this.cursor.maxWireVersion) || + changeStreamError instanceof MongoOperationTimeoutError) + ) { + try { + await this.cursor.close(); + } catch (error) { + squashError(error); + } + + await this._resume(changeStreamError); + + if (changeStreamError instanceof MongoOperationTimeoutError) throw changeStreamError; + } else { + try { + await this.close(); + } catch (error) { + squashError(error); + } + + throw changeStreamError; + } + } + + private async _resume(changeStreamError: AnyError) { + this.timeoutContext?.refresh(); + const topology = getTopology(this.parent); + try { + await topology.selectServer(this.cursor.readPreference, { + operationName: 'reconnect topology in change stream', + timeoutContext: this.timeoutContext, + deprioritizedServers: new DeprioritizedServers() + }); + this.cursor = this._createChangeStreamCursor(this.cursor.resumeOptions); + } catch { + // if the topology can't reconnect, close the stream + await this.close(); + throw changeStreamError; + } + } +} + +/** + * This function returns a list of options that are *not* supported by the $changeStream + * aggregation stage. This is best-effort - it uses the options "officially supported" by the driver + * to derive a list of known, unsupported options for the $changeStream stage. + * + * Notably, at runtime, users can still provide options unknown to the driver and the driver will + * *not* filter them out of the options object (see NODE-5510). + */ +function buildDisallowedChangeStreamOptions(): Set { + /** hard-coded list of allowed ChangeStream options */ + type CSOptions = + | 'resumeAfter' + | 'startAfter' + | 'startAtOperationTime' + | 'fullDocument' + | 'fullDocumentBeforeChange' + | 'showExpandedEvents'; + + /** + * a type representing all known options that the driver supports that are *not* change stream stage options. + * + * each known key is mapped to a non-optional string, so that if new driver-specific options are added, the + * instantiation of `denyList` below results in a TS error. + */ + type DisallowedOptions = { + [k in Exclude< + keyof ChangeStreamOptions & { timeoutContext: TimeoutContext }, + CSOptions + >]: string; + }; + + const denyList: DisallowedOptions = { + allowDiskUse: '', + authdb: '', + batchSize: '', + bsonRegExp: '', + bypassDocumentValidation: '', + bypassPinningCheck: '', + checkKeys: '', + collation: '', + comment: '', + cursor: '', + dbName: '', + enableUtf8Validation: '', + explain: '', + fieldsAsRaw: '', + hint: '', + ignoreUndefined: '', + let: '', + maxAwaitTimeMS: '', + maxTimeMS: '', + omitMaxTimeMS: '', + out: '', + promoteBuffers: '', + promoteLongs: '', + promoteValues: '', + raw: '', + rawData: '', + readConcern: '', + readPreference: '', + serializeFunctions: '', + session: '', + timeoutContext: '', + timeoutMS: '', + timeoutMode: '', + useBigInt64: '', + willRetryWrite: '', + writeConcern: '' + }; + + return new Set(Object.keys(denyList)); +} diff --git a/gateway/node_modules/mongodb/src/client-side-encryption/auto_encrypter.ts b/gateway/node_modules/mongodb/src/client-side-encryption/auto_encrypter.ts new file mode 100644 index 0000000000000000000000000000000000000000..53426609f904bdf5453037b5833a65bb573aa272 --- /dev/null +++ b/gateway/node_modules/mongodb/src/client-side-encryption/auto_encrypter.ts @@ -0,0 +1,475 @@ +import { type MongoCrypt, type MongoCryptOptions } from 'mongodb-client-encryption'; +import * as net from 'net'; + +import { ByteUtils, deserialize, type Document, serialize } from '../bson'; +import { type CommandOptions, type ProxyOptions } from '../cmap/connection'; +import { kDecorateResult } from '../constants'; +import { getMongoDBClientEncryption } from '../deps'; +import { MongoRuntimeError } from '../error'; +import { MongoClient, type MongoClientOptions } from '../mongo_client'; +import { type Abortable } from '../mongo_types'; +import { MongoDBCollectionNamespace } from '../utils'; +import { autoSelectSocketOptions } from './client_encryption'; +import { defaultErrorWrapper, MongoCryptInvalidArgumentError } from './errors'; +import { MongocryptdManager } from './mongocryptd_manager'; +import { + type CredentialProviders, + isEmptyCredentials, + type KMSProviders, + refreshKMSCredentials +} from './providers'; +import { type CSFLEKMSTlsOptions, StateMachine } from './state_machine'; + +/** @public */ +export interface AutoEncryptionOptions { + /** @internal client for metadata lookups */ + metadataClient?: MongoClient; + /** A `MongoClient` used to fetch keys from a key vault */ + keyVaultClient?: MongoClient; + /** The namespace where keys are stored in the key vault */ + keyVaultNamespace?: string; + /** Configuration options that are used by specific KMS providers during key generation, encryption, and decryption. */ + kmsProviders?: KMSProviders; + /** Configuration options for custom credential providers. */ + credentialProviders?: CredentialProviders; + /** + * A map of namespaces to a local JSON schema for encryption + * + * **NOTE**: Supplying options.schemaMap provides more security than relying on JSON Schemas obtained from the server. + * It protects against a malicious server advertising a false JSON Schema, which could trick the client into sending decrypted data that should be encrypted. + * Schemas supplied in the schemaMap only apply to configuring automatic encryption for Client-Side Field Level Encryption. + * Other validation rules in the JSON schema will not be enforced by the driver and will result in an error. + */ + schemaMap?: Document; + /** Supply a schema for the encrypted fields in the document */ + encryptedFieldsMap?: Document; + /** Allows the user to bypass auto encryption, maintaining implicit decryption */ + bypassAutoEncryption?: boolean; + /** Allows users to bypass query analysis */ + bypassQueryAnalysis?: boolean; + /** + * Sets the expiration time for the DEK in the cache in milliseconds. Defaults to 60000. 0 means no timeout. + */ + keyExpirationMS?: number; + options?: { + /** An optional hook to catch logging messages from the underlying encryption engine */ + logger?: (level: AutoEncryptionLoggerLevel, message: string) => void; + }; + extraOptions?: { + /** + * A local process the driver communicates with to determine how to encrypt values in a command. + * Defaults to "mongodb://%2Fvar%2Fmongocryptd.sock" if domain sockets are available or "mongodb://localhost:27020" otherwise + */ + mongocryptdURI?: string; + /** If true, autoEncryption will not attempt to spawn a mongocryptd before connecting */ + mongocryptdBypassSpawn?: boolean; + /** The path to the mongocryptd executable on the system */ + mongocryptdSpawnPath?: `${string}mongocryptd${'.exe' | ''}`; + /** Command line arguments to use when auto-spawning a mongocryptd */ + mongocryptdSpawnArgs?: string[]; + /** + * Full path to a MongoDB Crypt shared library to be used (instead of mongocryptd). + * + * This needs to be the path to the file itself, not a directory. + * It can be an absolute or relative path. If the path is relative and + * its first component is `$ORIGIN`, it will be replaced by the directory + * containing the mongodb-client-encryption native addon file. Otherwise, + * the path will be interpreted relative to the current working directory. + * + * Currently, loading different MongoDB Crypt shared library files from different + * MongoClients in the same process is not supported. + * + * If this option is provided and no MongoDB Crypt shared library could be loaded + * from the specified location, creating the MongoClient will fail. + * + * If this option is not provided and `cryptSharedLibRequired` is not specified, + * the AutoEncrypter will attempt to spawn and/or use mongocryptd according + * to the mongocryptd-specific `extraOptions` options. + * + * Specifying a path prevents mongocryptd from being used as a fallback. + * + * Requires the MongoDB Crypt shared library, available in MongoDB 6.0 or higher. + */ + cryptSharedLibPath?: `${string}mongo_crypt_v${number}.${'so' | 'dll' | 'dylib'}`; + /** + * If specified, never use mongocryptd and instead fail when the MongoDB Crypt + * shared library could not be loaded. + * + * This is always true when `cryptSharedLibPath` is specified. + * + * Requires the MongoDB Crypt shared library, available in MongoDB 6.0 or higher. + */ + cryptSharedLibRequired?: boolean; + /** + * Search paths for a MongoDB Crypt shared library to be used (instead of mongocryptd) + * Only for driver testing! + * @internal + */ + cryptSharedLibSearchPaths?: string[]; + }; + proxyOptions?: ProxyOptions; + /** The TLS options to use connecting to the KMS provider */ + tlsOptions?: CSFLEKMSTlsOptions; +} + +/** + * @public + * + * Extra options related to the mongocryptd process + * \* _Available in MongoDB 6.0 or higher._ + */ +export type AutoEncryptionExtraOptions = NonNullable; + +/** @public */ +export const AutoEncryptionLoggerLevel = Object.freeze({ + FatalError: 0, + Error: 1, + Warning: 2, + Info: 3, + Trace: 4 +} as const); + +/** + * @public + * The level of severity of the log message + * + * | Value | Level | + * |-------|-------| + * | 0 | Fatal Error | + * | 1 | Error | + * | 2 | Warning | + * | 3 | Info | + * | 4 | Trace | + */ +export type AutoEncryptionLoggerLevel = + (typeof AutoEncryptionLoggerLevel)[keyof typeof AutoEncryptionLoggerLevel]; + +/** + * @internal An internal class to be used by the driver for auto encryption + * **NOTE**: Not meant to be instantiated directly, this is for internal use only. + */ +export class AutoEncrypter { + _client: MongoClient; + _bypassEncryption: boolean; + _keyVaultNamespace: string; + _keyVaultClient: MongoClient; + _metaDataClient: MongoClient; + _proxyOptions: ProxyOptions; + _tlsOptions: CSFLEKMSTlsOptions; + _kmsProviders: KMSProviders; + _bypassMongocryptdAndCryptShared: boolean; + _contextCounter: number; + _credentialProviders?: CredentialProviders; + + _mongocryptdManager?: MongocryptdManager; + _mongocryptdClient?: MongoClient; + + /** @internal */ + _mongocrypt: MongoCrypt; + + /** + * Used by devtools to enable decorating decryption results. + * + * When set and enabled, `decrypt` will automatically recursively + * traverse a decrypted document and if a field has been decrypted, + * it will mark it as decrypted. Compass uses this to determine which + * fields were decrypted. + */ + [kDecorateResult] = false; + + /** @internal */ + static getMongoCrypt(): typeof MongoCrypt { + const encryption = getMongoDBClientEncryption(); + if ('kModuleError' in encryption) { + throw encryption.kModuleError; + } + return encryption.MongoCrypt; + } + + /** + * Create an AutoEncrypter + * + * **Note**: Do not instantiate this class directly. Rather, supply the relevant options to a MongoClient + * + * **Note**: Supplying `options.schemaMap` provides more security than relying on JSON Schemas obtained from the server. + * It protects against a malicious server advertising a false JSON Schema, which could trick the client into sending unencrypted data that should be encrypted. + * Schemas supplied in the schemaMap only apply to configuring automatic encryption for Client-Side Field Level Encryption. + * Other validation rules in the JSON schema will not be enforced by the driver and will result in an error. + * + * @example Create an AutoEncrypter that makes use of mongocryptd + * ```ts + * // Enabling autoEncryption via a MongoClient using mongocryptd + * const { MongoClient } = require('mongodb'); + * const client = new MongoClient(URL, { + * autoEncryption: { + * kmsProviders: { + * aws: { + * accessKeyId: AWS_ACCESS_KEY, + * secretAccessKey: AWS_SECRET_KEY + * } + * } + * } + * }); + * ``` + * + * await client.connect(); + * // From here on, the client will be encrypting / decrypting automatically + * @example Create an AutoEncrypter that makes use of libmongocrypt's CSFLE shared library + * ```ts + * // Enabling autoEncryption via a MongoClient using CSFLE shared library + * const { MongoClient } = require('mongodb'); + * const client = new MongoClient(URL, { + * autoEncryption: { + * kmsProviders: { + * aws: {} + * }, + * extraOptions: { + * cryptSharedLibPath: '/path/to/local/crypt/shared/lib', + * cryptSharedLibRequired: true + * } + * } + * }); + * ``` + * + * await client.connect(); + * // From here on, the client will be encrypting / decrypting automatically + */ + constructor(client: MongoClient, options: AutoEncryptionOptions) { + this._client = client; + this._bypassEncryption = options.bypassAutoEncryption === true; + + this._keyVaultNamespace = options.keyVaultNamespace || 'admin.datakeys'; + this._keyVaultClient = options.keyVaultClient || client; + this._metaDataClient = options.metadataClient || client; + this._proxyOptions = options.proxyOptions || {}; + this._tlsOptions = options.tlsOptions || {}; + this._kmsProviders = options.kmsProviders || {}; + this._credentialProviders = options.credentialProviders; + + if (options.credentialProviders?.aws && !isEmptyCredentials('aws', this._kmsProviders)) { + throw new MongoCryptInvalidArgumentError( + 'Can only provide a custom AWS credential provider when the state machine is configured for automatic AWS credential fetching' + ); + } + + const mongoCryptOptions: MongoCryptOptions = { + errorWrapper: defaultErrorWrapper + }; + if (options.schemaMap) { + if (ByteUtils.isUint8Array(options.schemaMap)) { + mongoCryptOptions.schemaMap = options.schemaMap; + } else { + mongoCryptOptions.schemaMap = serialize(options.schemaMap); + } + } + + if (options.encryptedFieldsMap) { + if (ByteUtils.isUint8Array(options.encryptedFieldsMap)) { + mongoCryptOptions.encryptedFieldsMap = options.encryptedFieldsMap; + } else { + mongoCryptOptions.encryptedFieldsMap = serialize(options.encryptedFieldsMap); + } + } + + if (ByteUtils.isUint8Array(this._kmsProviders)) { + mongoCryptOptions.kmsProviders = this._kmsProviders; + } else { + mongoCryptOptions.kmsProviders = serialize(this._kmsProviders); + } + + if (options.options?.logger) { + mongoCryptOptions.logger = options.options.logger; + } + + if (options.extraOptions && options.extraOptions.cryptSharedLibPath) { + mongoCryptOptions.cryptSharedLibPath = options.extraOptions.cryptSharedLibPath; + } + + if (options.bypassQueryAnalysis) { + mongoCryptOptions.bypassQueryAnalysis = options.bypassQueryAnalysis; + } + + if (options.keyExpirationMS != null) { + mongoCryptOptions.keyExpirationMS = options.keyExpirationMS; + } + + this._bypassMongocryptdAndCryptShared = this._bypassEncryption || !!options.bypassQueryAnalysis; + + if (options.extraOptions && options.extraOptions.cryptSharedLibSearchPaths) { + // Only for driver testing + mongoCryptOptions.cryptSharedLibSearchPaths = options.extraOptions.cryptSharedLibSearchPaths; + } else if (!this._bypassMongocryptdAndCryptShared) { + mongoCryptOptions.cryptSharedLibSearchPaths = ['$SYSTEM']; + } + + const MongoCrypt = AutoEncrypter.getMongoCrypt(); + this._mongocrypt = new MongoCrypt(mongoCryptOptions); + this._contextCounter = 0; + + if ( + options.extraOptions && + options.extraOptions.cryptSharedLibRequired && + !this.cryptSharedLibVersionInfo + ) { + throw new MongoCryptInvalidArgumentError( + '`cryptSharedLibRequired` set but no crypt_shared library loaded' + ); + } + + // Only instantiate mongocryptd manager/client once we know for sure + // that we are not using the CSFLE shared library. + if (!this._bypassMongocryptdAndCryptShared && !this.cryptSharedLibVersionInfo) { + this._mongocryptdManager = new MongocryptdManager(options.extraOptions); + const clientOptions: MongoClientOptions = { + serverSelectionTimeoutMS: 10000 + }; + + if ( + (options.extraOptions == null || typeof options.extraOptions.mongocryptdURI !== 'string') && + !net.getDefaultAutoSelectFamily + ) { + // Only set family if autoSelectFamily options are not supported. + clientOptions.family = 4; + } + + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore: TS complains as this always returns true on versions where it is present. + if (net.getDefaultAutoSelectFamily) { + // AutoEncrypter is made inside of MongoClient constructor while options are being parsed, + // we do not have access to the options that are in progress. + // TODO(NODE-6449): AutoEncrypter does not use client options for autoSelectFamily + Object.assign(clientOptions, autoSelectSocketOptions(this._client.s?.options ?? {})); + } + + this._mongocryptdClient = new MongoClient(this._mongocryptdManager.uri, clientOptions); + } + } + + /** + * Initializes the auto encrypter by spawning a mongocryptd and connecting to it. + * + * This function is a no-op when bypassSpawn is set or the crypt shared library is used. + */ + async init(): Promise { + if (this._bypassMongocryptdAndCryptShared || this.cryptSharedLibVersionInfo) { + return; + } + if (!this._mongocryptdManager) { + throw new MongoRuntimeError( + 'Reached impossible state: mongocryptdManager is undefined when neither bypassSpawn nor the shared lib are specified.' + ); + } + if (!this._mongocryptdClient) { + throw new MongoRuntimeError( + 'Reached impossible state: mongocryptdClient is undefined when neither bypassSpawn nor the shared lib are specified.' + ); + } + + if (!this._mongocryptdManager.bypassSpawn) { + await this._mongocryptdManager.spawn(); + } + + try { + const client = await this._mongocryptdClient.connect(); + return client; + } catch (error) { + throw new MongoRuntimeError( + 'Unable to connect to `mongocryptd`, please make sure it is running or in your PATH for auto-spawn', + { cause: error } + ); + } + } + + /** + * Cleans up the `_mongocryptdClient`, if present. + */ + async close(): Promise { + await this._mongocryptdClient?.close(); + } + + /** + * Encrypt a command for a given namespace. + */ + async encrypt( + ns: string, + cmd: Document, + options: CommandOptions & Abortable = {} + ): Promise { + options.signal?.throwIfAborted(); + + if (this._bypassEncryption) { + // If `bypassAutoEncryption` has been specified, don't encrypt + return cmd; + } + + const commandBuffer: Uint8Array = serialize(cmd, options); + const context = this._mongocrypt.makeEncryptionContext( + MongoDBCollectionNamespace.fromString(ns).db, + commandBuffer + ); + + context.id = this._contextCounter++; + context.ns = ns; + context.document = cmd; + + const stateMachine = new StateMachine({ + promoteValues: false, + promoteLongs: false, + proxyOptions: this._proxyOptions, + tlsOptions: this._tlsOptions, + socketOptions: autoSelectSocketOptions(this._client.s.options) + }); + + return deserialize(await stateMachine.execute(this, context, options), { + promoteValues: false, + promoteLongs: false + }); + } + + /** + * Decrypt a command response + */ + async decrypt( + response: Uint8Array, + options: CommandOptions & Abortable = {} + ): Promise { + options.signal?.throwIfAborted(); + + const context = this._mongocrypt.makeDecryptionContext(response); + + context.id = this._contextCounter++; + + const stateMachine = new StateMachine({ + ...options, + proxyOptions: this._proxyOptions, + tlsOptions: this._tlsOptions, + socketOptions: autoSelectSocketOptions(this._client.s.options) + }); + + return await stateMachine.execute(this, context, options); + } + + /** + * Ask the user for KMS credentials. + * + * This returns anything that looks like the kmsProviders original input + * option. It can be empty, and any provider specified here will override + * the original ones. + */ + async askForKMSCredentials(): Promise { + return await refreshKMSCredentials(this._kmsProviders, this._credentialProviders); + } + + /** + * Return the current libmongocrypt's CSFLE shared library version + * as `{ version: bigint, versionStr: string }`, or `null` if no CSFLE + * shared library was loaded. + */ + get cryptSharedLibVersionInfo(): { version: bigint; versionStr: string } | null { + return this._mongocrypt.cryptSharedLibVersionInfo; + } + + static get libmongocryptVersion(): string { + return AutoEncrypter.getMongoCrypt().libmongocryptVersion; + } +} diff --git a/gateway/node_modules/mongodb/src/client-side-encryption/client_encryption.ts b/gateway/node_modules/mongodb/src/client-side-encryption/client_encryption.ts new file mode 100644 index 0000000000000000000000000000000000000000..70476626f6291c1274b2871a2fa3c6812d70549b --- /dev/null +++ b/gateway/node_modules/mongodb/src/client-side-encryption/client_encryption.ts @@ -0,0 +1,1178 @@ +import type { + ExplicitEncryptionContextOptions, + MongoCrypt, + MongoCryptOptions +} from 'mongodb-client-encryption'; + +import { + type Binary, + deserialize, + type Document, + type Int32, + type Long, + serialize, + type UUID +} from '../bson'; +import { type AnyBulkWriteOperation, type BulkWriteResult } from '../bulk/common'; +import { type ProxyOptions } from '../cmap/connection'; +import { type Collection } from '../collection'; +import { type FindCursor } from '../cursor/find_cursor'; +import { type Db } from '../db'; +import { getMongoDBClientEncryption } from '../deps'; +import { type MongoClient, type MongoClientOptions } from '../mongo_client'; +import { type Filter, type WithId } from '../mongo_types'; +import { type CreateCollectionOptions } from '../operations/create_collection'; +import { type DeleteResult } from '../operations/delete'; +import { type CSOTTimeoutContext, TimeoutContext } from '../timeout'; +import { MongoDBCollectionNamespace, resolveTimeoutOptions } from '../utils'; +import { + defaultErrorWrapper, + MongoCryptCreateDataKeyError, + MongoCryptCreateEncryptedCollectionError, + MongoCryptInvalidArgumentError +} from './errors'; +import { + type ClientEncryptionDataKeyProvider, + type CredentialProviders, + isEmptyCredentials, + type KMSProviders, + refreshKMSCredentials +} from './providers/index'; +import { + type ClientEncryptionSocketOptions, + type CSFLEKMSTlsOptions, + StateMachine +} from './state_machine'; + +/** + * @public + * The schema for a DataKey in the key vault collection. + */ +export interface DataKey { + _id: UUID; + version?: number; + keyAltNames?: string[]; + keyMaterial: Binary; + creationDate: Date; + updateDate: Date; + status: number; + masterKey: Document; +} + +/** + * @public + * The public interface for explicit in-use encryption + */ +export class ClientEncryption { + /** @internal */ + _client: MongoClient; + /** @internal */ + _keyVaultNamespace: string; + /** @internal */ + _keyVaultClient: MongoClient; + /** @internal */ + _proxyOptions: ProxyOptions; + /** @internal */ + _tlsOptions: CSFLEKMSTlsOptions; + /** @internal */ + _kmsProviders: KMSProviders; + /** @internal */ + _timeoutMS?: number; + + /** @internal */ + _mongoCrypt: MongoCrypt; + + /** @internal */ + _credentialProviders?: CredentialProviders; + + /** @internal */ + static getMongoCrypt(): typeof MongoCrypt { + const encryption = getMongoDBClientEncryption(); + if ('kModuleError' in encryption) { + throw encryption.kModuleError; + } + return encryption.MongoCrypt; + } + + /** + * Create a new encryption instance + * + * @example + * ```ts + * new ClientEncryption(mongoClient, { + * keyVaultNamespace: 'client.encryption', + * kmsProviders: { + * local: { + * key: masterKey // The master key used for encryption/decryption. A 96-byte long Buffer + * } + * } + * }); + * ``` + * + * @example + * ```ts + * new ClientEncryption(mongoClient, { + * keyVaultNamespace: 'client.encryption', + * kmsProviders: { + * aws: { + * accessKeyId: AWS_ACCESS_KEY, + * secretAccessKey: AWS_SECRET_KEY + * } + * } + * }); + * ``` + */ + constructor(client: MongoClient, options: ClientEncryptionOptions) { + this._client = client; + this._proxyOptions = options.proxyOptions ?? {}; + this._tlsOptions = options.tlsOptions ?? {}; + this._kmsProviders = options.kmsProviders || {}; + const { timeoutMS } = resolveTimeoutOptions(client, options); + this._timeoutMS = timeoutMS; + this._credentialProviders = options.credentialProviders; + + if (options.credentialProviders?.aws && !isEmptyCredentials('aws', this._kmsProviders)) { + throw new MongoCryptInvalidArgumentError( + 'Can only provide a custom AWS credential provider when the state machine is configured for automatic AWS credential fetching' + ); + } + + if (options.keyVaultNamespace == null) { + throw new MongoCryptInvalidArgumentError('Missing required option `keyVaultNamespace`'); + } + + const mongoCryptOptions: MongoCryptOptions = { + ...options, + kmsProviders: serialize(this._kmsProviders), + errorWrapper: defaultErrorWrapper + }; + + this._keyVaultNamespace = options.keyVaultNamespace; + this._keyVaultClient = options.keyVaultClient || client; + const MongoCrypt = ClientEncryption.getMongoCrypt(); + this._mongoCrypt = new MongoCrypt(mongoCryptOptions); + } + + /** + * Creates a data key used for explicit encryption and inserts it into the key vault namespace + * + * @example + * ```ts + * // Using async/await to create a local key + * const dataKeyId = await clientEncryption.createDataKey('local'); + * ``` + * + * @example + * ```ts + * // Using async/await to create an aws key + * const dataKeyId = await clientEncryption.createDataKey('aws', { + * masterKey: { + * region: 'us-east-1', + * key: 'xxxxxxxxxxxxxx' // CMK ARN here + * } + * }); + * ``` + * + * @example + * ```ts + * // Using async/await to create an aws key with a keyAltName + * const dataKeyId = await clientEncryption.createDataKey('aws', { + * masterKey: { + * region: 'us-east-1', + * key: 'xxxxxxxxxxxxxx' // CMK ARN here + * }, + * keyAltNames: [ 'mySpecialKey' ] + * }); + * ``` + */ + async createDataKey( + provider: ClientEncryptionDataKeyProvider, + options: ClientEncryptionCreateDataKeyProviderOptions = {} + ): Promise { + if (options.keyAltNames && !Array.isArray(options.keyAltNames)) { + throw new MongoCryptInvalidArgumentError( + `Option "keyAltNames" must be an array of strings, but was of type ${typeof options.keyAltNames}.` + ); + } + + let keyAltNames = undefined; + if (options.keyAltNames && options.keyAltNames.length > 0) { + keyAltNames = options.keyAltNames.map((keyAltName, i) => { + if (typeof keyAltName !== 'string') { + throw new MongoCryptInvalidArgumentError( + `Option "keyAltNames" must be an array of strings, but item at index ${i} was of type ${typeof keyAltName}` + ); + } + + return serialize({ keyAltName }); + }); + } + + let keyMaterial = undefined; + if (options.keyMaterial) { + keyMaterial = serialize({ keyMaterial: options.keyMaterial }); + } + + const dataKeyBson = serialize({ + provider, + ...options.masterKey + }); + + const context = this._mongoCrypt.makeDataKeyContext(dataKeyBson, { + keyAltNames, + keyMaterial + }); + + const stateMachine = new StateMachine({ + proxyOptions: this._proxyOptions, + tlsOptions: this._tlsOptions, + socketOptions: autoSelectSocketOptions(this._client.s.options) + }); + + const timeoutContext = + options?.timeoutContext ?? + TimeoutContext.create(resolveTimeoutOptions(this._client, { timeoutMS: this._timeoutMS })); + + const dataKey = deserialize( + await stateMachine.execute(this, context, { timeoutContext }) + ) as DataKey; + + const { db: dbName, collection: collectionName } = MongoDBCollectionNamespace.fromString( + this._keyVaultNamespace + ); + + const { insertedId } = await this._keyVaultClient + .db(dbName) + .collection(collectionName) + .insertOne(dataKey, { + writeConcern: { w: 'majority' }, + timeoutMS: timeoutContext?.csotEnabled() + ? timeoutContext?.getRemainingTimeMSOrThrow() + : undefined + }); + + return insertedId; + } + + /** + * Searches the keyvault for any data keys matching the provided filter. If there are matches, rewrapManyDataKey then attempts to re-wrap the data keys using the provided options. + * + * If no matches are found, then no bulk write is performed. + * + * @example + * ```ts + * // rewrapping all data data keys (using a filter that matches all documents) + * const filter = {}; + * + * const result = await clientEncryption.rewrapManyDataKey(filter); + * if (result.bulkWriteResult != null) { + * // keys were re-wrapped, results will be available in the bulkWrite object. + * } + * ``` + * + * @example + * ```ts + * // attempting to rewrap all data keys with no matches + * const filter = { _id: new Binary() } // assume _id matches no documents in the database + * const result = await clientEncryption.rewrapManyDataKey(filter); + * + * if (result.bulkWriteResult == null) { + * // no keys matched, `bulkWriteResult` does not exist on the result object + * } + * ``` + */ + async rewrapManyDataKey( + filter: Filter, + options?: ClientEncryptionRewrapManyDataKeyProviderOptions + ): Promise<{ bulkWriteResult?: BulkWriteResult }> { + let keyEncryptionKeyBson = undefined; + if (options) { + const keyEncryptionKey = Object.assign({ provider: options.provider }, options.masterKey); + keyEncryptionKeyBson = serialize(keyEncryptionKey); + } + const filterBson = serialize(filter); + const context = this._mongoCrypt.makeRewrapManyDataKeyContext(filterBson, keyEncryptionKeyBson); + const stateMachine = new StateMachine({ + proxyOptions: this._proxyOptions, + tlsOptions: this._tlsOptions, + socketOptions: autoSelectSocketOptions(this._client.s.options) + }); + + const timeoutContext = TimeoutContext.create( + resolveTimeoutOptions(this._client, { timeoutMS: this._timeoutMS }) + ); + + const { v: dataKeys } = deserialize( + await stateMachine.execute(this, context, { timeoutContext }) + ); + if (dataKeys.length === 0) { + return {}; + } + + const { db: dbName, collection: collectionName } = MongoDBCollectionNamespace.fromString( + this._keyVaultNamespace + ); + + const replacements = dataKeys.map( + (key: DataKey): AnyBulkWriteOperation => ({ + updateOne: { + filter: { _id: key._id }, + update: { + $set: { + masterKey: key.masterKey, + keyMaterial: key.keyMaterial + }, + $currentDate: { + updateDate: true + } + } + } + }) + ); + + const result = await this._keyVaultClient + .db(dbName) + .collection(collectionName) + .bulkWrite(replacements, { + writeConcern: { w: 'majority' }, + timeoutMS: timeoutContext.csotEnabled() ? timeoutContext?.remainingTimeMS : undefined + }); + + return { bulkWriteResult: result }; + } + + /** + * Deletes the key with the provided id from the keyvault, if it exists. + * + * @example + * ```ts + * // delete a key by _id + * const id = new Binary(); // id is a bson binary subtype 4 object + * const { deletedCount } = await clientEncryption.deleteKey(id); + * + * if (deletedCount != null && deletedCount > 0) { + * // successful deletion + * } + * ``` + * + */ + async deleteKey(_id: Binary): Promise { + const { db: dbName, collection: collectionName } = MongoDBCollectionNamespace.fromString( + this._keyVaultNamespace + ); + + return await this._keyVaultClient + .db(dbName) + .collection(collectionName) + .deleteOne({ _id }, { writeConcern: { w: 'majority' }, timeoutMS: this._timeoutMS }); + } + + /** + * Finds all the keys currently stored in the keyvault. + * + * This method will not throw. + * + * @returns a FindCursor over all keys in the keyvault. + * @example + * ```ts + * // fetching all keys + * const keys = await clientEncryption.getKeys().toArray(); + * ``` + */ + getKeys(): FindCursor { + const { db: dbName, collection: collectionName } = MongoDBCollectionNamespace.fromString( + this._keyVaultNamespace + ); + + return this._keyVaultClient + .db(dbName) + .collection(collectionName) + .find({}, { readConcern: { level: 'majority' }, timeoutMS: this._timeoutMS }); + } + + /** + * Finds a key in the keyvault with the specified _id. + * + * Returns a promise that either resolves to a {@link DataKey} if a document matches the key or null if no documents + * match the id. The promise rejects with an error if an error is thrown. + * @example + * ```ts + * // getting a key by id + * const id = new Binary(); // id is a bson binary subtype 4 object + * const key = await clientEncryption.getKey(id); + * if (!key) { + * // key is null if there was no matching key + * } + * ``` + */ + async getKey(_id: Binary): Promise { + const { db: dbName, collection: collectionName } = MongoDBCollectionNamespace.fromString( + this._keyVaultNamespace + ); + + return await this._keyVaultClient + .db(dbName) + .collection(collectionName) + .findOne({ _id }, { readConcern: { level: 'majority' }, timeoutMS: this._timeoutMS }); + } + + /** + * Finds a key in the keyvault which has the specified keyAltName. + * + * @param keyAltName - a keyAltName to search for a key + * @returns Returns a promise that either resolves to a {@link DataKey} if a document matches the key or null if no documents + * match the keyAltName. The promise rejects with an error if an error is thrown. + * @example + * ```ts + * // get a key by alt name + * const keyAltName = 'keyAltName'; + * const key = await clientEncryption.getKeyByAltName(keyAltName); + * if (!key) { + * // key is null if there is no matching key + * } + * ``` + */ + async getKeyByAltName(keyAltName: string): Promise | null> { + const { db: dbName, collection: collectionName } = MongoDBCollectionNamespace.fromString( + this._keyVaultNamespace + ); + + return await this._keyVaultClient + .db(dbName) + .collection(collectionName) + .findOne( + { keyAltNames: keyAltName }, + { readConcern: { level: 'majority' }, timeoutMS: this._timeoutMS } + ); + } + + /** + * Adds a keyAltName to a key identified by the provided _id. + * + * This method resolves to/returns the *old* key value (prior to adding the new altKeyName). + * + * @param _id - The id of the document to update. + * @param keyAltName - a keyAltName to search for a key + * @returns Returns a promise that either resolves to a {@link DataKey} if a document matches the key or null if no documents + * match the id. The promise rejects with an error if an error is thrown. + * @example + * ```ts + * // adding an keyAltName to a data key + * const id = new Binary(); // id is a bson binary subtype 4 object + * const keyAltName = 'keyAltName'; + * const oldKey = await clientEncryption.addKeyAltName(id, keyAltName); + * if (!oldKey) { + * // null is returned if there is no matching document with an id matching the supplied id + * } + * ``` + */ + async addKeyAltName(_id: Binary, keyAltName: string): Promise | null> { + const { db: dbName, collection: collectionName } = MongoDBCollectionNamespace.fromString( + this._keyVaultNamespace + ); + + const value = await this._keyVaultClient + .db(dbName) + .collection(collectionName) + .findOneAndUpdate( + { _id }, + { $addToSet: { keyAltNames: keyAltName } }, + { writeConcern: { w: 'majority' }, returnDocument: 'before', timeoutMS: this._timeoutMS } + ); + + return value; + } + + /** + * Adds a keyAltName to a key identified by the provided _id. + * + * This method resolves to/returns the *old* key value (prior to removing the new altKeyName). + * + * If the removed keyAltName is the last keyAltName for that key, the `altKeyNames` property is unset from the document. + * + * @param _id - The id of the document to update. + * @param keyAltName - a keyAltName to search for a key + * @returns Returns a promise that either resolves to a {@link DataKey} if a document matches the key or null if no documents + * match the id. The promise rejects with an error if an error is thrown. + * @example + * ```ts + * // removing a key alt name from a data key + * const id = new Binary(); // id is a bson binary subtype 4 object + * const keyAltName = 'keyAltName'; + * const oldKey = await clientEncryption.removeKeyAltName(id, keyAltName); + * + * if (!oldKey) { + * // null is returned if there is no matching document with an id matching the supplied id + * } + * ``` + */ + async removeKeyAltName(_id: Binary, keyAltName: string): Promise | null> { + const { db: dbName, collection: collectionName } = MongoDBCollectionNamespace.fromString( + this._keyVaultNamespace + ); + + const pipeline = [ + { + $set: { + keyAltNames: { + $cond: [ + { + $eq: ['$keyAltNames', [keyAltName]] + }, + '$$REMOVE', + { + $filter: { + input: '$keyAltNames', + cond: { + $ne: ['$$this', keyAltName] + } + } + } + ] + } + } + } + ]; + + const value = await this._keyVaultClient + .db(dbName) + .collection(collectionName) + .findOneAndUpdate({ _id }, pipeline, { + writeConcern: { w: 'majority' }, + returnDocument: 'before', + timeoutMS: this._timeoutMS + }); + + return value; + } + + /** + * A convenience method for creating an encrypted collection. + * This method will create data keys for any encryptedFields that do not have a `keyId` defined + * and then create a new collection with the full set of encryptedFields. + * + * @param db - A Node.js driver Db object with which to create the collection + * @param name - The name of the collection to be created + * @param options - Options for createDataKey and for createCollection + * @returns created collection and generated encryptedFields + * @throws MongoCryptCreateDataKeyError - If part way through the process a createDataKey invocation fails, an error will be rejected that has the partial `encryptedFields` that were created. + * @throws MongoCryptCreateEncryptedCollectionError - If creating the collection fails, an error will be rejected that has the entire `encryptedFields` that were created. + */ + async createEncryptedCollection( + db: Db, + name: string, + options: { + provider: ClientEncryptionDataKeyProvider; + createCollectionOptions: Omit & { + encryptedFields: Document; + }; + masterKey?: AWSEncryptionKeyOptions | AzureEncryptionKeyOptions | GCPEncryptionKeyOptions; + } + ): Promise<{ collection: Collection; encryptedFields: Document }> { + const { + provider, + masterKey, + createCollectionOptions: { + encryptedFields: { ...encryptedFields }, + ...createCollectionOptions + } + } = options; + + const timeoutContext = + this._timeoutMS != null + ? TimeoutContext.create(resolveTimeoutOptions(this._client, { timeoutMS: this._timeoutMS })) + : undefined; + + if (Array.isArray(encryptedFields.fields)) { + const createDataKeyPromises = encryptedFields.fields.map(async field => + field == null || typeof field !== 'object' || field.keyId != null + ? field + : { + ...field, + keyId: await this.createDataKey(provider, { + masterKey, + // clone the timeoutContext + // in order to avoid sharing the same timeout for server selection and connection checkout across different concurrent operations + timeoutContext: timeoutContext?.csotEnabled() ? timeoutContext?.clone() : undefined + }) + } + ); + const createDataKeyResolutions = await Promise.allSettled(createDataKeyPromises); + + encryptedFields.fields = createDataKeyResolutions.map((resolution, index) => + resolution.status === 'fulfilled' ? resolution.value : encryptedFields.fields[index] + ); + + const rejection = createDataKeyResolutions.find( + (result): result is PromiseRejectedResult => result.status === 'rejected' + ); + if (rejection != null) { + throw new MongoCryptCreateDataKeyError(encryptedFields, { cause: rejection.reason }); + } + } + + try { + const collection = await db.createCollection(name, { + ...createCollectionOptions, + encryptedFields, + timeoutMS: timeoutContext?.csotEnabled() + ? timeoutContext?.getRemainingTimeMSOrThrow() + : undefined + }); + return { collection, encryptedFields }; + } catch (cause) { + throw new MongoCryptCreateEncryptedCollectionError(encryptedFields, { cause }); + } + } + + /** + * Explicitly encrypt a provided value. Note that either `options.keyId` or `options.keyAltName` must + * be specified. Specifying both `options.keyId` and `options.keyAltName` is considered an error. + * + * @param value - The value that you wish to serialize. Must be of a type that can be serialized into BSON + * @param options - + * @returns a Promise that either resolves with the encrypted value, or rejects with an error. + * + * @example + * ```ts + * // Encryption with async/await api + * async function encryptMyData(value) { + * const keyId = await clientEncryption.createDataKey('local'); + * return clientEncryption.encrypt(value, { keyId, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' }); + * } + * ``` + * + * @example + * ```ts + * // Encryption using a keyAltName + * async function encryptMyData(value) { + * await clientEncryption.createDataKey('local', { keyAltNames: 'mySpecialKey' }); + * return clientEncryption.encrypt(value, { keyAltName: 'mySpecialKey', algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' }); + * } + * ``` + */ + async encrypt(value: unknown, options: ClientEncryptionEncryptOptions): Promise { + return await this._encrypt(value, false, options); + } + + /** + * Encrypts a Match Expression or Aggregate Expression to query a range index. + * + * Only supported when queryType is "range" and algorithm is "Range". + * + * @param expression - a BSON document of one of the following forms: + * 1. A Match Expression of this form: + * `{$and: [{: {$gt: }}, {: {$lt: }}]}` + * 2. An Aggregate Expression of this form: + * `{$and: [{$gt: [, ]}, {$lt: [, ]}]}` + * + * `$gt` may also be `$gte`. `$lt` may also be `$lte`. + * + * @param options - + * @returns Returns a Promise that either resolves with the encrypted value or rejects with an error. + */ + async encryptExpression( + expression: Document, + options: ClientEncryptionEncryptOptions + ): Promise { + return await this._encrypt(expression, true, options); + } + + /** + * Explicitly decrypt a provided encrypted value + * + * @param value - An encrypted value + * @returns a Promise that either resolves with the decrypted value, or rejects with an error + * + * @example + * ```ts + * // Decrypting value with async/await API + * async function decryptMyValue(value) { + * return clientEncryption.decrypt(value); + * } + * ``` + */ + async decrypt(value: Binary): Promise { + const valueBuffer = serialize({ v: value }); + const context = this._mongoCrypt.makeExplicitDecryptionContext(valueBuffer); + + const stateMachine = new StateMachine({ + proxyOptions: this._proxyOptions, + tlsOptions: this._tlsOptions, + socketOptions: autoSelectSocketOptions(this._client.s.options) + }); + + const timeoutContext = + this._timeoutMS != null + ? TimeoutContext.create(resolveTimeoutOptions(this._client, { timeoutMS: this._timeoutMS })) + : undefined; + + const { v } = deserialize(await stateMachine.execute(this, context, { timeoutContext })); + + return v; + } + + /** + * @internal + * Ask the user for KMS credentials. + * + * This returns anything that looks like the kmsProviders original input + * option. It can be empty, and any provider specified here will override + * the original ones. + */ + async askForKMSCredentials(): Promise { + return await refreshKMSCredentials(this._kmsProviders, this._credentialProviders); + } + + static get libmongocryptVersion() { + return ClientEncryption.getMongoCrypt().libmongocryptVersion; + } + + /** + * @internal + * A helper that perform explicit encryption of values and expressions. + * Explicitly encrypt a provided value. Note that either `options.keyId` or `options.keyAltName` must + * be specified. Specifying both `options.keyId` and `options.keyAltName` is considered an error. + * + * @param value - The value that you wish to encrypt. Must be of a type that can be serialized into BSON + * @param expressionMode - a boolean that indicates whether or not to encrypt the value as an expression + * @param options - options to pass to encrypt + * @returns the raw result of the call to stateMachine.execute(). When expressionMode is set to true, the return + * value will be a bson document. When false, the value will be a BSON Binary. + * + */ + private async _encrypt( + value: unknown, + expressionMode: boolean, + options: ClientEncryptionEncryptOptions + ): Promise { + const { + algorithm, + keyId, + keyAltName, + contentionFactor, + queryType, + rangeOptions, + stringOptions, + textOptions + } = options; + const contextOptions: ExplicitEncryptionContextOptions = { + expressionMode, + algorithm + }; + if (keyId) { + contextOptions.keyId = keyId.buffer; + } + if (keyAltName) { + if (keyId) { + throw new MongoCryptInvalidArgumentError( + `"options" cannot contain both "keyId" and "keyAltName"` + ); + } + if (typeof keyAltName !== 'string') { + throw new MongoCryptInvalidArgumentError( + `"options.keyAltName" must be of type string, but was of type ${typeof keyAltName}` + ); + } + + contextOptions.keyAltName = serialize({ keyAltName }); + } + if (typeof contentionFactor === 'number' || typeof contentionFactor === 'bigint') { + contextOptions.contentionFactor = contentionFactor; + } + if (typeof queryType === 'string') { + contextOptions.queryType = queryType; + } + + if (typeof rangeOptions === 'object') { + contextOptions.rangeOptions = serialize(rangeOptions); + } + + const resolvedStringOptions = stringOptions ?? textOptions; + if (typeof resolvedStringOptions === 'object') { + contextOptions.textOptions = serialize(resolvedStringOptions); + } + + const valueBuffer = serialize({ v: value }); + const stateMachine = new StateMachine({ + proxyOptions: this._proxyOptions, + tlsOptions: this._tlsOptions, + socketOptions: autoSelectSocketOptions(this._client.s.options) + }); + const context = this._mongoCrypt.makeExplicitEncryptionContext(valueBuffer, contextOptions); + + const timeoutContext = + this._timeoutMS != null + ? TimeoutContext.create(resolveTimeoutOptions(this._client, { timeoutMS: this._timeoutMS })) + : undefined; + const { v } = deserialize(await stateMachine.execute(this, context, { timeoutContext })); + return v; + } +} + +/** + * @public + * Options to provide when encrypting data. + */ +export interface ClientEncryptionEncryptOptions { + /** + * The algorithm to use for encryption. + */ + algorithm: + | 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' + | 'AEAD_AES_256_CBC_HMAC_SHA_512-Random' + | 'Indexed' + | 'Unindexed' + | 'Range' + | 'String' + /** @deprecated Use `'String'` instead. */ + | 'TextPreview'; + + /** + * The id of the Binary dataKey to use for encryption + */ + keyId?: Binary; + + /** + * A unique string name corresponding to an already existing dataKey. + */ + keyAltName?: string; + + /** The contention factor. */ + contentionFactor?: bigint | number; + + /** + * The query type. + */ + queryType?: + | 'equality' + | 'range' + | 'prefix' + | 'suffix' + | 'substring' + /** @deprecated Use `'prefix'` instead. */ + | 'prefixPreview' + /** @deprecated Use `'suffix'` instead. */ + | 'suffixPreview' + /** @deprecated Use `'substring'` instead. */ + | 'substringPreview'; + + /** The index options for a Queryable Encryption field supporting "range" queries.*/ + rangeOptions?: RangeOptions; + + /** Options for a Queryable Encryption field supporting string queries. Only valid when `algorithm` is `'String'` or the deprecated `'TextPreview'`. */ + stringOptions?: StringQueryOptions; + + /** + * Options for a Queryable Encryption field supporting text queries. Only valid when `algorithm` is `'String'` or the deprecated `'TextPreview'`. + * + * @deprecated Use `stringOptions` instead. + */ + textOptions?: StringQueryOptions; +} + +/** + * Options for a Queryable Encryption field supporting string queries. + * + * @public + */ +export interface StringQueryOptions { + /** Indicates that text indexes for this field are case sensitive */ + caseSensitive: boolean; + /** Indicates that text indexes for this field are diacritic sensitive. */ + diacriticSensitive: boolean; + + prefix?: { + /** The maximum allowed query length. */ + strMaxQueryLength: Int32 | number; + /** The minimum allowed query length. */ + strMinQueryLength: Int32 | number; + }; + + suffix?: { + /** The maximum allowed query length. */ + strMaxQueryLength: Int32 | number; + /** The minimum allowed query length. */ + strMinQueryLength: Int32 | number; + }; + + substring?: { + /** The maximum allowed length to insert. */ + strMaxLength: Int32 | number; + /** The maximum allowed query length. */ + strMaxQueryLength: Int32 | number; + /** The minimum allowed query length. */ + strMinQueryLength: Int32 | number; + }; +} + +/** + * @public + * @deprecated Use {@link StringQueryOptions} instead. + */ +export type TextQueryOptions = StringQueryOptions; + +/** + * @public + * @experimental + */ +export interface ClientEncryptionRewrapManyDataKeyProviderOptions { + provider: ClientEncryptionDataKeyProvider; + masterKey?: + | AWSEncryptionKeyOptions + | AzureEncryptionKeyOptions + | GCPEncryptionKeyOptions + | KMIPEncryptionKeyOptions + | undefined; +} + +/** + * @public + * Additional settings to provide when creating a new `ClientEncryption` instance. + */ +export interface ClientEncryptionOptions { + /** + * The namespace of the key vault, used to store encryption keys + */ + keyVaultNamespace: string; + + /** + * A MongoClient used to fetch keys from a key vault. Defaults to client. + */ + keyVaultClient?: MongoClient | undefined; + + /** + * Options for specific KMS providers to use + */ + kmsProviders?: KMSProviders; + + /** + * Options for user provided custom credential providers. + */ + credentialProviders?: CredentialProviders; + + /** + * Options for specifying a Socks5 proxy to use for connecting to the KMS. + */ + proxyOptions?: ProxyOptions; + + /** + * TLS options for kms providers to use. + */ + tlsOptions?: CSFLEKMSTlsOptions; + + /** + * Sets the expiration time for the DEK in the cache in milliseconds. Defaults to 60000. 0 means no timeout. + */ + keyExpirationMS?: number; + + /** + * @experimental + * + * The timeout setting to be used for all the operations on ClientEncryption. + * + * When provided, `timeoutMS` is used as the timeout for each operation executed on + * the ClientEncryption object. For example: + * + * ```typescript + * const clientEncryption = new ClientEncryption(client, { + * timeoutMS: 1_000 + * kmsProviders: { local: { key: '' } } + * }); + * + * // `1_000` is used as the timeout for createDataKey call + * await clientEncryption.createDataKey('local'); + * ``` + * + * If `timeoutMS` is configured on the provided client, the client's `timeoutMS` value + * will be used unless `timeoutMS` is also provided as a client encryption option. + * + * ```typescript + * const client = new MongoClient('', { timeoutMS: 2_000 }); + * + * // timeoutMS is set to 1_000 on clientEncryption + * const clientEncryption = new ClientEncryption(client, { + * timeoutMS: 1_000 + * kmsProviders: { local: { key: '' } } + * }); + * ``` + */ + timeoutMS?: number; +} + +/** + * @public + * Configuration options for making an AWS encryption key + */ +export interface AWSEncryptionKeyOptions { + /** + * The AWS region of the KMS + */ + region: string; + + /** + * The Amazon Resource Name (ARN) to the AWS customer master key (CMK) + */ + key: string; + + /** + * An alternate host to send KMS requests to. May include port number. + */ + endpoint?: string | undefined; +} + +/** + * @public + * Configuration options for making an AWS encryption key + */ +export interface GCPEncryptionKeyOptions { + /** + * GCP project ID + */ + projectId: string; + + /** + * Location name (e.g. "global") + */ + location: string; + + /** + * Key ring name + */ + keyRing: string; + + /** + * Key name + */ + keyName: string; + + /** + * Key version + */ + keyVersion?: string | undefined; + + /** + * KMS URL, defaults to `https://www.googleapis.com/auth/cloudkms` + */ + endpoint?: string | undefined; +} + +/** + * @public + * Configuration options for making an Azure encryption key + */ +export interface AzureEncryptionKeyOptions { + /** + * Key name + */ + keyName: string; + + /** + * Key vault URL, typically `.vault.azure.net` + */ + keyVaultEndpoint: string; + + /** + * Key version + */ + keyVersion?: string | undefined; +} + +/** + * @public + * Configuration options for making a KMIP encryption key + */ +export interface KMIPEncryptionKeyOptions { + /** + * keyId is the KMIP Unique Identifier to a 96 byte KMIP Secret Data managed object. + * + * If keyId is omitted, a random 96 byte KMIP Secret Data managed object will be created. + */ + keyId?: string; + + /** + * Host with optional port. + */ + endpoint?: string; + + /** + * If true, this key should be decrypted by the KMIP server. + * + * Requires `mongodb-client-encryption>=6.0.1`. + */ + delegated?: boolean; +} + +/** + * @public + * Options to provide when creating a new data key. + */ +export interface ClientEncryptionCreateDataKeyProviderOptions { + /** + * Identifies a new KMS-specific key used to encrypt the new data key + */ + masterKey?: + | AWSEncryptionKeyOptions + | AzureEncryptionKeyOptions + | GCPEncryptionKeyOptions + | KMIPEncryptionKeyOptions + | undefined; + + /** + * An optional list of string alternate names used to reference a key. + * If a key is created with alternate names, then encryption may refer to the key by the unique alternate name instead of by _id. + */ + keyAltNames?: string[] | undefined; + + /** @experimental */ + keyMaterial?: Buffer | Binary; + + /** @internal */ + timeoutContext?: CSOTTimeoutContext; +} + +/** + * @public + * @experimental + */ +export interface ClientEncryptionRewrapManyDataKeyResult { + /** The result of rewrapping data keys. If unset, no keys matched the filter. */ + bulkWriteResult?: BulkWriteResult; +} + +/** + * @public + * RangeOptions specifies index options for a Queryable Encryption field supporting "range" queries. + * min, max, sparsity, trimFactor and range must match the values set in the encryptedFields of the destination collection. + * For double and decimal128, min/max/precision must all be set, or all be unset. + */ +export interface RangeOptions { + /** min is the minimum value for the encrypted index. Required if precision is set. */ + min?: any; + /** max is the minimum value for the encrypted index. Required if precision is set. */ + max?: any; + /** sparsity may be used to tune performance. must be non-negative. When omitted, a default value is used. */ + sparsity?: Long | bigint; + /** trimFactor may be used to tune performance. must be non-negative. When omitted, a default value is used. */ + trimFactor?: Int32 | number; + /* precision determines the number of significant digits after the decimal point. May only be set for double or decimal128. */ + precision?: number; +} + +/** + * Get the socket options from the client. + * @param baseOptions - The mongo client options. + * @returns ClientEncryptionSocketOptions + */ +export function autoSelectSocketOptions( + baseOptions: MongoClientOptions +): ClientEncryptionSocketOptions { + const options: ClientEncryptionSocketOptions = { autoSelectFamily: true }; + if ('autoSelectFamily' in baseOptions) { + options.autoSelectFamily = baseOptions.autoSelectFamily; + } + if ('autoSelectFamilyAttemptTimeout' in baseOptions) { + options.autoSelectFamilyAttemptTimeout = baseOptions.autoSelectFamilyAttemptTimeout; + } + return options; +} diff --git a/gateway/node_modules/mongodb/src/client-side-encryption/errors.ts b/gateway/node_modules/mongodb/src/client-side-encryption/errors.ts new file mode 100644 index 0000000000000000000000000000000000000000..08a31e9db9d4ee2cb2c7c0f309033e581b2d4e8e --- /dev/null +++ b/gateway/node_modules/mongodb/src/client-side-encryption/errors.ts @@ -0,0 +1,144 @@ +import { type Document } from '../bson'; +import { MongoError } from '../error'; + +/** + * @public + * An error indicating that something went wrong specifically with MongoDB Client Encryption + */ +export class MongoCryptError extends MongoError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options: { cause?: Error } = {}) { + super(message, options); + } + + override get name() { + return 'MongoCryptError'; + } +} + +export const defaultErrorWrapper = (error: Error) => + new MongoCryptError(error.message, { cause: error }); + +/** + * @public + * + * An error indicating an invalid argument was provided to an encryption API. + */ +export class MongoCryptInvalidArgumentError extends MongoCryptError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name() { + return 'MongoCryptInvalidArgumentError'; + } +} +/** + * @public + * An error indicating that `ClientEncryption.createEncryptedCollection()` failed to create data keys + */ +export class MongoCryptCreateDataKeyError extends MongoCryptError { + encryptedFields: Document; + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(encryptedFields: Document, { cause }: { cause: Error }) { + super(`Unable to complete creating data keys: ${cause.message}`, { cause }); + this.encryptedFields = encryptedFields; + } + + override get name() { + return 'MongoCryptCreateDataKeyError'; + } +} + +/** + * @public + * An error indicating that `ClientEncryption.createEncryptedCollection()` failed to create a collection + */ +export class MongoCryptCreateEncryptedCollectionError extends MongoCryptError { + encryptedFields: Document; + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(encryptedFields: Document, { cause }: { cause: Error }) { + super(`Unable to create collection: ${cause.message}`, { cause }); + this.encryptedFields = encryptedFields; + } + + override get name() { + return 'MongoCryptCreateEncryptedCollectionError'; + } +} + +/** + * @public + * An error indicating that mongodb-client-encryption failed to auto-refresh Azure KMS credentials. + */ +export class MongoCryptAzureKMSRequestError extends MongoCryptError { + /** The body of the http response that failed, if present. */ + body?: Document; + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, body?: Document) { + super(message); + this.body = body; + } + + override get name(): string { + return 'MongoCryptAzureKMSRequestError'; + } +} + +/** @public */ +export class MongoCryptKMSRequestNetworkTimeoutError extends MongoCryptError { + override get name(): string { + return 'MongoCryptKMSRequestNetworkTimeoutError'; + } +} diff --git a/gateway/node_modules/mongodb/src/client-side-encryption/mongocryptd_manager.ts b/gateway/node_modules/mongodb/src/client-side-encryption/mongocryptd_manager.ts new file mode 100644 index 0000000000000000000000000000000000000000..24c0abdd4a8e3cd0666078ec081008fd0aee03af --- /dev/null +++ b/gateway/node_modules/mongodb/src/client-side-encryption/mongocryptd_manager.ts @@ -0,0 +1,100 @@ +import type { ChildProcess } from 'child_process'; + +import { MongoNetworkTimeoutError } from '../error'; +import { type AutoEncryptionExtraOptions } from './auto_encrypter'; + +/** + * @internal + * An internal class that handles spawning a mongocryptd. + */ +export class MongocryptdManager { + static DEFAULT_MONGOCRYPTD_URI = 'mongodb://localhost:27020'; + + uri: string; + bypassSpawn: boolean; + spawnPath = ''; + spawnArgs: Array = []; + _child?: ChildProcess; + + constructor(extraOptions: AutoEncryptionExtraOptions = {}) { + this.uri = + typeof extraOptions.mongocryptdURI === 'string' && extraOptions.mongocryptdURI.length > 0 + ? extraOptions.mongocryptdURI + : MongocryptdManager.DEFAULT_MONGOCRYPTD_URI; + + this.bypassSpawn = !!extraOptions.mongocryptdBypassSpawn; + + if (Object.hasOwn(extraOptions, 'mongocryptdSpawnPath') && extraOptions.mongocryptdSpawnPath) { + this.spawnPath = extraOptions.mongocryptdSpawnPath; + } + if ( + Object.hasOwn(extraOptions, 'mongocryptdSpawnArgs') && + Array.isArray(extraOptions.mongocryptdSpawnArgs) + ) { + this.spawnArgs = this.spawnArgs.concat(extraOptions.mongocryptdSpawnArgs); + } + if ( + this.spawnArgs + .filter(arg => typeof arg === 'string') + .every(arg => arg.indexOf('--idleShutdownTimeoutSecs') < 0) + ) { + this.spawnArgs.push('--idleShutdownTimeoutSecs', '60'); + } + } + + /** + * Will check to see if a mongocryptd is up. If it is not up, it will attempt + * to spawn a mongocryptd in a detached process, and then wait for it to be up. + */ + async spawn(): Promise { + const cmdName = this.spawnPath || 'mongocryptd'; + + // eslint-disable-next-line @typescript-eslint/no-require-imports + const { spawn } = require('child_process') as typeof import('child_process'); + + // Spawned with stdio: ignore and detached: true + // to ensure child can outlive parent. + this._child = spawn(cmdName, this.spawnArgs, { + stdio: 'ignore', + detached: true + }); + + this._child.on('error', () => { + // From the FLE spec: + // "The stdout and stderr of the spawned process MUST not be exposed in the driver + // (e.g. redirect to /dev/null). Users can pass the argument --logpath to + // extraOptions.mongocryptdSpawnArgs if they need to inspect mongocryptd logs. + // If spawning is necessary, the driver MUST spawn mongocryptd whenever server + // selection on the MongoClient to mongocryptd fails. If the MongoClient fails to + // connect after spawning, the server selection error is propagated to the user." + // The AutoEncrypter and MongoCryptdManager should work together to spawn + // mongocryptd whenever necessary. Additionally, the `mongocryptd` intentionally + // shuts down after 60s and gets respawned when necessary. We rely on server + // selection timeouts when connecting to the `mongocryptd` to inform users that something + // has been configured incorrectly. For those reasons, we suppress stderr from + // the `mongocryptd` process and immediately unref the process. + }); + + // unref child to remove handle from event loop + this._child.unref(); + } + + /** + * @returns the result of `fn` or rejects with an error. + */ + async withRespawn(fn: () => Promise): ReturnType { + try { + const result = await fn(); + return result; + } catch (err) { + // If we are not bypassing spawning, then we should retry once on a MongoTimeoutError (server selection error) + const shouldSpawn = err instanceof MongoNetworkTimeoutError && !this.bypassSpawn; + if (!shouldSpawn) { + throw err; + } + } + await this.spawn(); + const result = await fn(); + return result; + } +} diff --git a/gateway/node_modules/mongodb/src/client-side-encryption/providers/aws.ts b/gateway/node_modules/mongodb/src/client-side-encryption/providers/aws.ts new file mode 100644 index 0000000000000000000000000000000000000000..e50bb4d232ffc3e5f549e71ebe0337ba9ec8b4f5 --- /dev/null +++ b/gateway/node_modules/mongodb/src/client-side-encryption/providers/aws.ts @@ -0,0 +1,33 @@ +import { + type AWSCredentialProvider, + AWSSDKCredentialProvider +} from '../../cmap/auth/aws_temporary_credentials'; +import { type KMSProviders } from '.'; + +/** + * @internal + */ +export async function loadAWSCredentials( + kmsProviders: KMSProviders, + provider?: AWSCredentialProvider +): Promise { + const credentialProvider = new AWSSDKCredentialProvider(provider); + + // We shouldn't ever receive a response from the AWS SDK that doesn't have a `SecretAccessKey` + // or `AccessKeyId`. However, TS says these fields are optional. We provide empty strings + // and let libmongocrypt error if we're unable to fetch the required keys. + const { + SecretAccessKey = '', + AccessKeyId = '', + Token + } = await credentialProvider.getCredentials(); + const aws: NonNullable = { + secretAccessKey: SecretAccessKey, + accessKeyId: AccessKeyId + }; + // the AWS session token is only required for temporary credentials so only attach it to the + // result if it's present in the response from the aws sdk + Token != null && (aws.sessionToken = Token); + + return { ...kmsProviders, aws }; +} diff --git a/gateway/node_modules/mongodb/src/client-side-encryption/providers/azure.ts b/gateway/node_modules/mongodb/src/client-side-encryption/providers/azure.ts new file mode 100644 index 0000000000000000000000000000000000000000..97a2665ee9a8ff8de0891c1de2d12db4b72db27a --- /dev/null +++ b/gateway/node_modules/mongodb/src/client-side-encryption/providers/azure.ts @@ -0,0 +1,181 @@ +import { type Document } from '../../bson'; +import { MongoNetworkTimeoutError } from '../../error'; +import { get } from '../../utils'; +import { MongoCryptAzureKMSRequestError } from '../errors'; +import { type KMSProviders } from './index'; + +const MINIMUM_TOKEN_REFRESH_IN_MILLISECONDS = 6000; +/** Base URL for getting Azure tokens. */ +export const AZURE_BASE_URL = 'http://169.254.169.254/metadata/identity/oauth2/token?'; + +/** + * The access token that libmongocrypt expects for Azure kms. + */ +interface AccessToken { + accessToken: string; +} + +/** + * The response from the azure idms endpoint, including the `expiresOnTimestamp`. + * `expiresOnTimestamp` is needed for caching. + */ +interface AzureTokenCacheEntry extends AccessToken { + accessToken: string; + expiresOnTimestamp: number; +} + +/** + * @internal + */ +export class AzureCredentialCache { + cachedToken: AzureTokenCacheEntry | null = null; + + async getToken(): Promise { + if (this.cachedToken == null || this.needsRefresh(this.cachedToken)) { + this.cachedToken = await this._getToken(); + } + + return { accessToken: this.cachedToken.accessToken }; + } + + needsRefresh(token: AzureTokenCacheEntry): boolean { + const timeUntilExpirationMS = token.expiresOnTimestamp - Date.now(); + return timeUntilExpirationMS <= MINIMUM_TOKEN_REFRESH_IN_MILLISECONDS; + } + + /** + * exposed for testing + */ + resetCache() { + this.cachedToken = null; + } + + /** + * exposed for testing + */ + _getToken(): Promise { + return fetchAzureKMSToken(); + } +} + +/** @internal */ +export const tokenCache = new AzureCredentialCache(); + +/** @internal */ +async function parseResponse(response: { + body: string; + status?: number; +}): Promise { + const { status, body: rawBody } = response; + + const body: { expires_in?: number; access_token?: string } = (() => { + try { + return JSON.parse(rawBody); + } catch { + throw new MongoCryptAzureKMSRequestError('Malformed JSON body in GET request.'); + } + })(); + + if (status !== 200) { + throw new MongoCryptAzureKMSRequestError('Unable to complete request.', body); + } + + if (!body.access_token) { + throw new MongoCryptAzureKMSRequestError( + 'Malformed response body - missing field `access_token`.' + ); + } + + if (!body.expires_in) { + throw new MongoCryptAzureKMSRequestError( + 'Malformed response body - missing field `expires_in`.' + ); + } + + const expiresInMS = Number(body.expires_in) * 1000; + if (Number.isNaN(expiresInMS)) { + throw new MongoCryptAzureKMSRequestError( + 'Malformed response body - unable to parse int from `expires_in` field.' + ); + } + + return { + accessToken: body.access_token, + expiresOnTimestamp: Date.now() + expiresInMS + }; +} + +/** + * @internal + * + * exposed for CSFLE + * [prose test 18](https://github.com/mongodb/specifications/tree/master/source/client-side-encryption/tests#azure-imds-credentials) + */ +export interface AzureKMSRequestOptions { + headers?: Document; + url?: URL | string; +} + +/** + * @internal + * Get the Azure endpoint URL. + */ +export function addAzureParams(url: URL, resource: string, username?: string): URL { + url.searchParams.append('api-version', '2018-02-01'); + url.searchParams.append('resource', resource); + if (username) { + url.searchParams.append('client_id', username); + } + return url; +} + +/** + * @internal + * + * parses any options provided by prose tests to `fetchAzureKMSToken` and merges them with + * the default values for headers and the request url. + */ +export function prepareRequest(options: AzureKMSRequestOptions): { + headers: Document; + url: URL; +} { + const url = new URL(options.url?.toString() ?? AZURE_BASE_URL); + addAzureParams(url, 'https://vault.azure.net'); + const headers = { ...options.headers, 'Content-Type': 'application/json', Metadata: true }; + return { headers, url }; +} + +/** + * @internal + * + * `AzureKMSRequestOptions` allows prose tests to modify the http request sent to the idms + * servers. This is required to simulate different server conditions. No options are expected to + * be set outside of tests. + * + * exposed for CSFLE + * [prose test 18](https://github.com/mongodb/specifications/tree/master/source/client-side-encryption/tests#azure-imds-credentials) + */ +export async function fetchAzureKMSToken( + options: AzureKMSRequestOptions = {} +): Promise { + const { headers, url } = prepareRequest(options); + try { + const response = await get(url, { headers }); + return await parseResponse(response); + } catch (error) { + if (error instanceof MongoNetworkTimeoutError) { + throw new MongoCryptAzureKMSRequestError(`[Azure KMS] ${error.message}`); + } + throw error; + } +} + +/** + * @internal + * + * @throws Will reject with a `MongoCryptError` if the http request fails or the http response is malformed. + */ +export async function loadAzureCredentials(kmsProviders: KMSProviders): Promise { + const azure = await tokenCache.getToken(); + return { ...kmsProviders, azure }; +} diff --git a/gateway/node_modules/mongodb/src/client-side-encryption/providers/gcp.ts b/gateway/node_modules/mongodb/src/client-side-encryption/providers/gcp.ts new file mode 100644 index 0000000000000000000000000000000000000000..1415d216aa964e07fac8f21e5e5987209c9dfd67 --- /dev/null +++ b/gateway/node_modules/mongodb/src/client-side-encryption/providers/gcp.ts @@ -0,0 +1,16 @@ +import { getGcpMetadata } from '../../deps'; +import { type KMSProviders } from '.'; + +/** @internal */ +export async function loadGCPCredentials(kmsProviders: KMSProviders): Promise { + const gcpMetadata = getGcpMetadata(); + + if ('kModuleError' in gcpMetadata) { + return kmsProviders; + } + + const { access_token: accessToken } = await gcpMetadata.instance<{ access_token: string }>({ + property: 'service-accounts/default/token' + }); + return { ...kmsProviders, gcp: { accessToken } }; +} diff --git a/gateway/node_modules/mongodb/src/client-side-encryption/providers/index.ts b/gateway/node_modules/mongodb/src/client-side-encryption/providers/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..63108c7ed9360cabc8286af9717f01d0e354cbff --- /dev/null +++ b/gateway/node_modules/mongodb/src/client-side-encryption/providers/index.ts @@ -0,0 +1,207 @@ +import type { Binary } from '../../bson'; +import { type AWSCredentialProvider } from '../../cmap/auth/aws_temporary_credentials'; +import { loadAWSCredentials } from './aws'; +import { loadAzureCredentials } from './azure'; +import { loadGCPCredentials } from './gcp'; + +/** + * @public + * + * A data key provider. Allowed values: + * + * - aws, gcp, local, kmip or azure + * - (`mongodb-client-encryption>=6.0.1` only) a named key, in the form of: + * `aws:`, `gcp:`, `local:`, `kmip:`, `azure:` + * where `name` is an alphanumeric string, underscores allowed. + */ +export type ClientEncryptionDataKeyProvider = keyof KMSProviders; + +/** @public */ +export interface AWSKMSProviderConfiguration { + /** + * The access key used for the AWS KMS provider + */ + accessKeyId: string; + + /** + * The secret access key used for the AWS KMS provider + */ + secretAccessKey: string; + + /** + * An optional AWS session token that will be used as the + * X-Amz-Security-Token header for AWS requests. + */ + sessionToken?: string; +} + +/** @public */ +export interface LocalKMSProviderConfiguration { + /** + * The master key used to encrypt/decrypt data keys. + * A 96-byte long Buffer or base64 encoded string. + */ + key: Binary | Uint8Array | string; +} + +/** @public */ +export interface KMIPKMSProviderConfiguration { + /** + * The output endpoint string. + * The endpoint consists of a hostname and port separated by a colon. + * E.g. "example.com:123". A port is always present. + */ + endpoint?: string; +} + +/** @public */ +export type AzureKMSProviderConfiguration = + | { + /** + * The tenant ID identifies the organization for the account + */ + tenantId: string; + + /** + * The client ID to authenticate a registered application + */ + clientId: string; + + /** + * The client secret to authenticate a registered application + */ + clientSecret: string; + + /** + * If present, a host with optional port. E.g. "example.com" or "example.com:443". + * This is optional, and only needed if customer is using a non-commercial Azure instance + * (e.g. a government or China account, which use different URLs). + * Defaults to "login.microsoftonline.com" + */ + identityPlatformEndpoint?: string | undefined; + } + | { + /** + * If present, an access token to authenticate with Azure. + */ + accessToken: string; + }; + +/** @public */ +export type GCPKMSProviderConfiguration = + | { + /** + * The service account email to authenticate + */ + email: string; + + /** + * A PKCS#8 encrypted key. This can either be a base64 string or a binary representation + */ + privateKey: string | Buffer; + + /** + * If present, a host with optional port. E.g. "example.com" or "example.com:443". + * Defaults to "oauth2.googleapis.com" + */ + endpoint?: string | undefined; + } + | { + /** + * If present, an access token to authenticate with GCP. + */ + accessToken: string; + }; + +/** + * @public + * Configuration options for custom credential providers for KMS requests. + */ +export interface CredentialProviders { + /* A custom AWS credential provider */ + aws?: AWSCredentialProvider; +} + +/** + * @public + * Configuration options that are used by specific KMS providers during key generation, encryption, and decryption. + * + * Named KMS providers _are not supported_ for automatic KMS credential fetching. + */ +export interface KMSProviders { + /** + * Configuration options for using 'aws' as your KMS provider + */ + aws?: AWSKMSProviderConfiguration | Record; + [key: `aws:${string}`]: AWSKMSProviderConfiguration; + + /** + * Configuration options for using 'local' as your KMS provider + */ + local?: LocalKMSProviderConfiguration; + [key: `local:${string}`]: LocalKMSProviderConfiguration; + + /** + * Configuration options for using 'kmip' as your KMS provider + */ + kmip?: KMIPKMSProviderConfiguration; + [key: `kmip:${string}`]: KMIPKMSProviderConfiguration; + + /** + * Configuration options for using 'azure' as your KMS provider + */ + azure?: AzureKMSProviderConfiguration | Record; + [key: `azure:${string}`]: AzureKMSProviderConfiguration; + + /** + * Configuration options for using 'gcp' as your KMS provider + */ + gcp?: GCPKMSProviderConfiguration | Record; + [key: `gcp:${string}`]: GCPKMSProviderConfiguration; +} + +/** + * Auto credential fetching should only occur when the provider is defined on the kmsProviders map + * and the settings are an empty object. + * + * This is distinct from a nullish provider key. + * + * @internal - exposed for testing purposes only + */ +export function isEmptyCredentials( + providerName: ClientEncryptionDataKeyProvider, + kmsProviders: KMSProviders +): boolean { + const provider = kmsProviders[providerName]; + if (provider == null) { + return false; + } + return typeof provider === 'object' && Object.keys(provider).length === 0; +} + +/** + * Load cloud provider credentials for the user provided KMS providers. + * Credentials will only attempt to get loaded if they do not exist + * and no existing credentials will get overwritten. + * + * @internal + */ +export async function refreshKMSCredentials( + kmsProviders: KMSProviders, + credentialProviders?: CredentialProviders +): Promise { + let finalKMSProviders = kmsProviders; + + if (isEmptyCredentials('aws', kmsProviders)) { + finalKMSProviders = await loadAWSCredentials(finalKMSProviders, credentialProviders?.aws); + } + + if (isEmptyCredentials('gcp', kmsProviders)) { + finalKMSProviders = await loadGCPCredentials(finalKMSProviders); + } + + if (isEmptyCredentials('azure', kmsProviders)) { + finalKMSProviders = await loadAzureCredentials(finalKMSProviders); + } + return finalKMSProviders; +} diff --git a/gateway/node_modules/mongodb/src/client-side-encryption/state_machine.ts b/gateway/node_modules/mongodb/src/client-side-encryption/state_machine.ts new file mode 100644 index 0000000000000000000000000000000000000000..2922758d11ecf38a399de1d955c43571ad6a7b5b --- /dev/null +++ b/gateway/node_modules/mongodb/src/client-side-encryption/state_machine.ts @@ -0,0 +1,651 @@ +import * as fs from 'fs/promises'; +import { type MongoCryptContext, type MongoCryptKMSRequest } from 'mongodb-client-encryption'; +import * as net from 'net'; +import * as process from 'process'; +import * as tls from 'tls'; + +import { + type BSONSerializeOptions, + deserialize, + type Document, + pluckBSONSerializeOptions, + serialize +} from '../bson'; +import { type ProxyOptions } from '../cmap/connection'; +import { CursorTimeoutContext } from '../cursor/abstract_cursor'; +import { getSocks, type SocksLib } from '../deps'; +import { MongoOperationTimeoutError } from '../error'; +import { type MongoClient, type MongoClientOptions } from '../mongo_client'; +import { type Abortable } from '../mongo_types'; +import { type CollectionInfo } from '../operations/list_collections'; +import { Timeout, type TimeoutContext, TimeoutError } from '../timeout'; +import { + addAbortListener, + BufferPool, + kDispose, + MongoDBCollectionNamespace, + promiseWithResolvers +} from '../utils'; +import { autoSelectSocketOptions, type DataKey } from './client_encryption'; +import { MongoCryptError } from './errors'; +import { type MongocryptdManager } from './mongocryptd_manager'; +import { type KMSProviders } from './providers'; + +let socks: SocksLib | null = null; +function loadSocks(): SocksLib { + if (socks == null) { + const socksImport = getSocks(); + if ('kModuleError' in socksImport) { + throw socksImport.kModuleError; + } + socks = socksImport; + } + return socks; +} + +// libmongocrypt states +const MONGOCRYPT_CTX_ERROR = 0; +const MONGOCRYPT_CTX_NEED_MONGO_COLLINFO = 1; +const MONGOCRYPT_CTX_NEED_MONGO_MARKINGS = 2; +const MONGOCRYPT_CTX_NEED_MONGO_KEYS = 3; +const MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS = 7; +const MONGOCRYPT_CTX_NEED_KMS = 4; +const MONGOCRYPT_CTX_READY = 5; +const MONGOCRYPT_CTX_DONE = 6; + +const HTTPS_PORT = 443; + +const stateToString = new Map([ + [MONGOCRYPT_CTX_ERROR, 'MONGOCRYPT_CTX_ERROR'], + [MONGOCRYPT_CTX_NEED_MONGO_COLLINFO, 'MONGOCRYPT_CTX_NEED_MONGO_COLLINFO'], + [MONGOCRYPT_CTX_NEED_MONGO_MARKINGS, 'MONGOCRYPT_CTX_NEED_MONGO_MARKINGS'], + [MONGOCRYPT_CTX_NEED_MONGO_KEYS, 'MONGOCRYPT_CTX_NEED_MONGO_KEYS'], + [MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS, 'MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS'], + [MONGOCRYPT_CTX_NEED_KMS, 'MONGOCRYPT_CTX_NEED_KMS'], + [MONGOCRYPT_CTX_READY, 'MONGOCRYPT_CTX_READY'], + [MONGOCRYPT_CTX_DONE, 'MONGOCRYPT_CTX_DONE'] +]); + +const INSECURE_TLS_OPTIONS = [ + 'tlsInsecure', + 'tlsAllowInvalidCertificates', + 'tlsAllowInvalidHostnames' +]; + +/** + * Helper function for logging. Enabled by setting the environment flag MONGODB_CRYPT_DEBUG. + * @param msg - Anything you want to be logged. + */ +function debug(msg: unknown) { + if (process.env.MONGODB_CRYPT_DEBUG) { + // eslint-disable-next-line no-console + console.error(msg); + } +} + +declare module 'mongodb-client-encryption' { + // the properties added to `MongoCryptContext` here are only used for the `StateMachine`'s + // execute method and are not part of the C++ bindings. + interface MongoCryptContext { + id: number; + document: Document; + ns: string; + } +} + +/** + * @public + * + * TLS options to use when connecting. The spec specifically calls out which insecure + * tls options are not allowed: + * + * - tlsAllowInvalidCertificates + * - tlsAllowInvalidHostnames + * - tlsInsecure + * + * These options are not included in the type, and are ignored if provided. + */ +export type ClientEncryptionTlsOptions = Pick< + MongoClientOptions, + 'tlsCAFile' | 'tlsCertificateKeyFile' | 'tlsCertificateKeyFilePassword' | 'secureContext' +>; + +/** @public */ +export type CSFLEKMSTlsOptions = { + aws?: ClientEncryptionTlsOptions; + gcp?: ClientEncryptionTlsOptions; + kmip?: ClientEncryptionTlsOptions; + local?: ClientEncryptionTlsOptions; + azure?: ClientEncryptionTlsOptions; + + [key: string]: ClientEncryptionTlsOptions | undefined; +}; + +/** + * @public + * + * Socket options to use for KMS requests. + */ +export type ClientEncryptionSocketOptions = Pick< + MongoClientOptions, + 'autoSelectFamily' | 'autoSelectFamilyAttemptTimeout' +>; + +/** + * This is kind of a hack. For `rewrapManyDataKey`, we have tests that + * guarantee that when there are no matching keys, `rewrapManyDataKey` returns + * nothing. We also have tests for auto encryption that guarantee for `encrypt` + * we return an error when there are no matching keys. This error is generated in + * subsequent iterations of the state machine. + * Some apis (`encrypt`) throw if there are no filter matches and others (`rewrapManyDataKey`) + * do not. We set the result manually here, and let the state machine continue. `libmongocrypt` + * will inform us if we need to error by setting the state to `MONGOCRYPT_CTX_ERROR` but + * otherwise we'll return `{ v: [] }`. + */ +let EMPTY_V; + +/** + * @internal + * + * An interface representing an object that can be passed to the `StateMachine.execute` method. + * + * Not all properties are required for all operations. + */ +export interface StateMachineExecutable { + _keyVaultNamespace: string; + _keyVaultClient: MongoClient; + askForKMSCredentials: () => Promise; + + /** only used for auto encryption */ + _metaDataClient?: MongoClient; + /** only used for auto encryption */ + _mongocryptdClient?: MongoClient; + /** only used for auto encryption */ + _mongocryptdManager?: MongocryptdManager; +} + +export type StateMachineOptions = { + /** socks5 proxy options, if set. */ + proxyOptions: ProxyOptions; + + /** TLS options for KMS requests, if set. */ + tlsOptions: CSFLEKMSTlsOptions; + + /** Socket specific options we support. */ + socketOptions: ClientEncryptionSocketOptions; +} & Pick; + +/** + * @internal + * An internal class that executes across a MongoCryptContext until either + * a finishing state or an error is reached. Do not instantiate directly. + */ +// TODO(DRIVERS-2671): clarify CSOT behavior for FLE APIs +export class StateMachine { + private options: StateMachineOptions; + private bsonOptions: BSONSerializeOptions; + + constructor(options: StateMachineOptions, bsonOptions = pluckBSONSerializeOptions(options)) { + this.options = options; + this.bsonOptions = bsonOptions; + } + + /** + * Executes the state machine according to the specification + */ + async execute( + executor: StateMachineExecutable, + context: MongoCryptContext, + options: { timeoutContext?: TimeoutContext } & Abortable + ): Promise { + const keyVaultNamespace = executor._keyVaultNamespace; + const keyVaultClient = executor._keyVaultClient; + const metaDataClient = executor._metaDataClient; + const mongocryptdClient = executor._mongocryptdClient; + const mongocryptdManager = executor._mongocryptdManager; + let result: Uint8Array | null = null; + + // Typescript treats getters just like properties: Once you've tested it for equality + // it cannot change. Which is exactly the opposite of what we use state and status for. + // Every call to at least `addMongoOperationResponse` and `finalize` can change the state. + // These wrappers let us write code more naturally and not add compiler exceptions + // to conditions checks inside the state machine. + const getStatus = () => context.status; + const getState = () => context.state; + + while (getState() !== MONGOCRYPT_CTX_DONE && getState() !== MONGOCRYPT_CTX_ERROR) { + options.signal?.throwIfAborted(); + debug(`[context#${context.id}] ${stateToString.get(getState()) || getState()}`); + + switch (getState()) { + case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO: { + const filter = deserialize(context.nextMongoOperation()); + if (!metaDataClient) { + throw new MongoCryptError( + 'unreachable state machine state: entered MONGOCRYPT_CTX_NEED_MONGO_COLLINFO but metadata client is undefined' + ); + } + + const collInfoCursor = this.fetchCollectionInfo( + metaDataClient, + context.ns, + filter, + options + ); + + for await (const collInfo of collInfoCursor) { + context.addMongoOperationResponse(serialize(collInfo)); + if (getState() === MONGOCRYPT_CTX_ERROR) break; + } + + if (getState() === MONGOCRYPT_CTX_ERROR) break; + + context.finishMongoOperation(); + break; + } + + case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS: { + const command = context.nextMongoOperation(); + if (getState() === MONGOCRYPT_CTX_ERROR) break; + + if (!mongocryptdClient) { + throw new MongoCryptError( + 'unreachable state machine state: entered MONGOCRYPT_CTX_NEED_MONGO_MARKINGS but mongocryptdClient is undefined' + ); + } + + // When we are using the shared library, we don't have a mongocryptd manager. + const markedCommand: Uint8Array = mongocryptdManager + ? await mongocryptdManager.withRespawn( + this.markCommand.bind(this, mongocryptdClient, context.ns, command, options) + ) + : await this.markCommand(mongocryptdClient, context.ns, command, options); + + context.addMongoOperationResponse(markedCommand); + context.finishMongoOperation(); + break; + } + + case MONGOCRYPT_CTX_NEED_MONGO_KEYS: { + const filter = context.nextMongoOperation(); + const keys = await this.fetchKeys(keyVaultClient, keyVaultNamespace, filter, options); + + if (keys.length === 0) { + // See docs on EMPTY_V + result = EMPTY_V ??= serialize({ v: [] }); + } + for (const key of keys) { + context.addMongoOperationResponse(serialize(key)); + } + + context.finishMongoOperation(); + + break; + } + + case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS: { + const kmsProviders = await executor.askForKMSCredentials(); + context.provideKMSProviders(serialize(kmsProviders)); + break; + } + + case MONGOCRYPT_CTX_NEED_KMS: { + await Promise.all(this.requests(context, options)); + context.finishKMSRequests(); + break; + } + + case MONGOCRYPT_CTX_READY: { + const finalizedContext = context.finalize(); + if (getState() === MONGOCRYPT_CTX_ERROR) { + const message = getStatus().message || 'Finalization error'; + throw new MongoCryptError(message); + } + result = finalizedContext; + break; + } + + default: + throw new MongoCryptError(`Unknown state: ${getState()}`); + } + } + + if (getState() === MONGOCRYPT_CTX_ERROR || result == null) { + const message = getStatus().message; + if (!message) { + debug( + `unidentifiable error in MongoCrypt - received an error status from \`libmongocrypt\` but received no error message.` + ); + } + throw new MongoCryptError( + message ?? + 'unidentifiable error in MongoCrypt - received an error status from `libmongocrypt` but received no error message.' + ); + } + + return result; + } + + /** + * Handles the request to the KMS service. Exposed for testing purposes. Do not directly invoke. + * @param kmsContext - A C++ KMS context returned from the bindings + * @returns A promise that resolves when the KMS reply has be fully parsed + */ + async kmsRequest( + request: MongoCryptKMSRequest, + options?: { timeoutContext?: TimeoutContext } & Abortable + ): Promise { + const parsedUrl = request.endpoint.split(':'); + const port = parsedUrl[1] != null ? Number.parseInt(parsedUrl[1], 10) : HTTPS_PORT; + const socketOptions: tls.ConnectionOptions & { + host: string; + port: number; + autoSelectFamily?: boolean; + autoSelectFamilyAttemptTimeout?: number; + } = { + host: parsedUrl[0], + servername: parsedUrl[0], + port, + ...autoSelectSocketOptions(this.options.socketOptions || {}) + }; + const message = request.message; + const buffer = new BufferPool(); + + let netSocket: net.Socket; + let socket: tls.TLSSocket; + + function destroySockets() { + for (const sock of [socket, netSocket]) { + if (sock) { + sock.destroy(); + } + } + } + + function onerror(cause: Error) { + return new MongoCryptError('KMS request failed', { cause }); + } + + function onclose() { + return new MongoCryptError('KMS request closed'); + } + + const tlsOptions = this.options.tlsOptions; + if (tlsOptions) { + const kmsProvider = request.kmsProvider; + const providerTlsOptions = tlsOptions[kmsProvider]; + if (providerTlsOptions) { + const error = this.validateTlsOptions(kmsProvider, providerTlsOptions); + if (error) { + throw error; + } + try { + await this.setTlsOptions(providerTlsOptions, socketOptions); + } catch (err) { + throw onerror(err); + } + } + } + + let abortListener; + + try { + if (this.options.proxyOptions && this.options.proxyOptions.proxyHost) { + netSocket = new net.Socket(); + + const { + promise: willConnect, + reject: rejectOnNetSocketError, + resolve: resolveOnNetSocketConnect + } = promiseWithResolvers(); + + netSocket + .once('error', err => rejectOnNetSocketError(onerror(err))) + .once('close', () => rejectOnNetSocketError(onclose())) + .once('connect', () => resolveOnNetSocketConnect()); + + const netSocketOptions = { + ...socketOptions, + host: this.options.proxyOptions.proxyHost, + port: this.options.proxyOptions.proxyPort || 1080 + }; + + netSocket.connect(netSocketOptions); + + await willConnect; + + try { + socks ??= loadSocks(); + socketOptions.socket = ( + await socks.SocksClient.createConnection({ + existing_socket: netSocket, + command: 'connect', + destination: { host: socketOptions.host, port: socketOptions.port }, + proxy: { + // host and port are ignored because we pass existing_socket + host: 'iLoveJavaScript', + port: 0, + type: 5, + userId: this.options.proxyOptions.proxyUsername, + password: this.options.proxyOptions.proxyPassword + } + }) + ).socket; + } catch (err) { + throw onerror(err); + } + } + + socket = tls.connect(socketOptions, () => { + socket.write(message); + }); + + const { + promise: willResolveKmsRequest, + reject: rejectOnTlsSocketError, + resolve + } = promiseWithResolvers(); + + abortListener = addAbortListener(options?.signal, function () { + destroySockets(); + rejectOnTlsSocketError(this.reason); + }); + + socket + .once('error', err => rejectOnTlsSocketError(onerror(err))) + .once('close', () => rejectOnTlsSocketError(onclose())) + .on('data', data => { + buffer.append(data); + while (request.bytesNeeded > 0 && buffer.length) { + const bytesNeeded = Math.min(request.bytesNeeded, buffer.length); + request.addResponse(buffer.read(bytesNeeded)); + } + + if (request.bytesNeeded <= 0) { + resolve(); + } + }); + await (options?.timeoutContext?.csotEnabled() + ? Promise.all([ + willResolveKmsRequest, + Timeout.expires(options.timeoutContext?.remainingTimeMS) + ]) + : willResolveKmsRequest); + } catch (error) { + if (error instanceof TimeoutError) + throw new MongoOperationTimeoutError('KMS request timed out'); + throw error; + } finally { + // There's no need for any more activity on this socket at this point. + destroySockets(); + abortListener?.[kDispose](); + } + } + + *requests(context: MongoCryptContext, options?: { timeoutContext?: TimeoutContext } & Abortable) { + for ( + let request = context.nextKMSRequest(); + request != null; + request = context.nextKMSRequest() + ) { + yield this.kmsRequest(request, options); + } + } + + /** + * Validates the provided TLS options are secure. + * + * @param kmsProvider - The KMS provider name. + * @param tlsOptions - The client TLS options for the provider. + * + * @returns An error if any option is invalid. + */ + validateTlsOptions( + kmsProvider: string, + tlsOptions: ClientEncryptionTlsOptions + ): MongoCryptError | void { + const tlsOptionNames = Object.keys(tlsOptions); + for (const option of INSECURE_TLS_OPTIONS) { + if (tlsOptionNames.includes(option)) { + return new MongoCryptError(`Insecure TLS options prohibited for ${kmsProvider}: ${option}`); + } + } + } + + /** + * Sets only the valid secure TLS options. + * + * @param tlsOptions - The client TLS options for the provider. + * @param options - The existing connection options. + */ + async setTlsOptions( + tlsOptions: ClientEncryptionTlsOptions, + options: tls.ConnectionOptions + ): Promise { + // If a secureContext is provided, ensure it is set. + if (tlsOptions.secureContext) { + options.secureContext = tlsOptions.secureContext; + } + if (tlsOptions.tlsCertificateKeyFile) { + const cert = await fs.readFile(tlsOptions.tlsCertificateKeyFile); + options.cert = options.key = cert; + } + if (tlsOptions.tlsCAFile) { + options.ca = await fs.readFile(tlsOptions.tlsCAFile); + } + if (tlsOptions.tlsCertificateKeyFilePassword) { + options.passphrase = tlsOptions.tlsCertificateKeyFilePassword; + } + } + + /** + * Fetches collection info for a provided namespace, when libmongocrypt + * enters the `MONGOCRYPT_CTX_NEED_MONGO_COLLINFO` state. The result is + * used to inform libmongocrypt of the schema associated with this + * namespace. Exposed for testing purposes. Do not directly invoke. + * + * @param client - A MongoClient connected to the topology + * @param ns - The namespace to list collections from + * @param filter - A filter for the listCollections command + * @param callback - Invoked with the info of the requested collection, or with an error + */ + fetchCollectionInfo( + client: MongoClient, + ns: string, + filter: Document, + options?: { timeoutContext?: TimeoutContext } & Abortable + ): AsyncIterable { + const { db } = MongoDBCollectionNamespace.fromString(ns); + + const cursor = client.db(db).listCollections(filter, { + promoteLongs: false, + promoteValues: false, + timeoutContext: + options?.timeoutContext && new CursorTimeoutContext(options?.timeoutContext, Symbol()), + signal: options?.signal, + nameOnly: false + }); + + return cursor; + } + + /** + * Calls to the mongocryptd to provide markings for a command. + * Exposed for testing purposes. Do not directly invoke. + * @param client - A MongoClient connected to a mongocryptd + * @param ns - The namespace (database.collection) the command is being executed on + * @param command - The command to execute. + * @param callback - Invoked with the serialized and marked bson command, or with an error + */ + async markCommand( + client: MongoClient, + ns: string, + command: Uint8Array, + options?: { timeoutContext?: TimeoutContext } & Abortable + ): Promise { + const { db } = MongoDBCollectionNamespace.fromString(ns); + const bsonOptions = { promoteLongs: false, promoteValues: false }; + const rawCommand = deserialize(command, bsonOptions); + + const commandOptions: { + timeoutMS?: number; + signal?: AbortSignal; + } = { + timeoutMS: undefined, + signal: undefined + }; + + if (options?.timeoutContext?.csotEnabled()) { + commandOptions.timeoutMS = options.timeoutContext.remainingTimeMS; + } + if (options?.signal) { + commandOptions.signal = options.signal; + } + + const response = await client.db(db).command(rawCommand, { + ...bsonOptions, + ...commandOptions + }); + + return serialize(response, this.bsonOptions); + } + + /** + * Requests keys from the keyVault collection on the topology. + * Exposed for testing purposes. Do not directly invoke. + * @param client - A MongoClient connected to the topology + * @param keyVaultNamespace - The namespace (database.collection) of the keyVault Collection + * @param filter - The filter for the find query against the keyVault Collection + * @param callback - Invoked with the found keys, or with an error + */ + fetchKeys( + client: MongoClient, + keyVaultNamespace: string, + filter: Uint8Array, + options?: { timeoutContext?: TimeoutContext } & Abortable + ): Promise> { + const { db: dbName, collection: collectionName } = + MongoDBCollectionNamespace.fromString(keyVaultNamespace); + + const commandOptions: { + timeoutContext?: CursorTimeoutContext; + signal?: AbortSignal; + } = { + timeoutContext: undefined, + signal: undefined + }; + + if (options?.timeoutContext != null) { + commandOptions.timeoutContext = new CursorTimeoutContext(options.timeoutContext, Symbol()); + } + if (options?.signal != null) { + commandOptions.signal = options.signal; + } + + return client + .db(dbName) + .collection(collectionName, { readConcern: { level: 'majority' } }) + .find(deserialize(filter), commandOptions) + .toArray(); + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/auth_provider.ts b/gateway/node_modules/mongodb/src/cmap/auth/auth_provider.ts new file mode 100644 index 0000000000000000000000000000000000000000..d39de6500c8034c9f7ec1f573022958ab1c2515d --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/auth_provider.ts @@ -0,0 +1,77 @@ +import type { Document } from '../../bson'; +import { MongoRuntimeError } from '../../error'; +import type { HandshakeDocument } from '../connect'; +import type { Connection, ConnectionOptions } from '../connection'; +import type { MongoCredentials } from './mongo_credentials'; + +/** + * Context used during authentication + * @internal + */ +export class AuthContext { + /** The connection to authenticate */ + connection: Connection; + /** The credentials to use for authentication */ + credentials?: MongoCredentials; + /** If the context is for reauthentication. */ + reauthenticating = false; + /** The options passed to the `connect` method */ + options: ConnectionOptions; + + /** A response from an initial auth attempt, only some mechanisms use this (e.g, SCRAM) */ + response?: Document; + /** A random nonce generated for use in an authentication conversation */ + nonce?: Uint8Array; + + constructor( + connection: Connection, + credentials: MongoCredentials | undefined, + options: ConnectionOptions + ) { + this.connection = connection; + this.credentials = credentials; + this.options = options; + } +} + +/** + * Provider used during authentication. + * @internal + */ +export abstract class AuthProvider { + /** + * Prepare the handshake document before the initial handshake. + * + * @param handshakeDoc - The document used for the initial handshake on a connection + * @param authContext - Context for authentication flow + */ + async prepare( + handshakeDoc: HandshakeDocument, + _authContext: AuthContext + ): Promise { + return handshakeDoc; + } + + /** + * Authenticate + * + * @param context - A shared context for authentication flow + */ + abstract auth(context: AuthContext): Promise; + + /** + * Reauthenticate. + * @param context - The shared auth context. + */ + async reauth(context: AuthContext): Promise { + if (context.reauthenticating) { + throw new MongoRuntimeError('Reauthentication already in progress.'); + } + try { + context.reauthenticating = true; + await this.auth(context); + } finally { + context.reauthenticating = false; + } + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/aws4.ts b/gateway/node_modules/mongodb/src/cmap/auth/aws4.ts new file mode 100644 index 0000000000000000000000000000000000000000..522fbe2bebbfee7b8b86cf78db1fc6b7288fefb7 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/aws4.ts @@ -0,0 +1,207 @@ +import { ByteUtils } from '../../bson'; +import { type AWSCredentials } from '../../deps'; + +export type AwsSigv4Options = { + path: '/'; + body: string; + host: string; + method: 'POST'; + headers: { + 'Content-Type': 'application/x-www-form-urlencoded'; + 'Content-Length': number; + 'X-MongoDB-Server-Nonce': string; + 'X-MongoDB-GS2-CB-Flag': 'n'; + }; + service: string; + region: string; + date: Date; +}; + +export type SignedHeaders = { + Authorization: string; + 'X-Amz-Date': string; +}; + +/** + * Calculates the SHA-256 hash of a string. + * + * @param str - String to hash. + * @returns Hexadecimal representation of the hash. + */ +const getHexSha256 = async (str: string): Promise => { + const data = stringToBuffer(str); + const hashBuffer = await crypto.subtle.digest('SHA-256', data); + const hashHex = ByteUtils.toHex(new Uint8Array(hashBuffer)); + return hashHex; +}; + +/** + * Calculates the HMAC-SHA256 of a string using the provided key. + * @param key - Key to use for HMAC calculation. Can be a string or Uint8Array. + * @param str - String to calculate HMAC for. + * @returns Uint8Array containing the HMAC-SHA256 digest. + */ +const getHmacSha256 = async (key: string | Uint8Array, str: string): Promise => { + let keyData: Uint8Array; + if (typeof key === 'string') { + keyData = stringToBuffer(key); + } else { + keyData = key; + } + + const importedKey = await crypto.subtle.importKey( + 'raw', + keyData, + { name: 'HMAC', hash: { name: 'SHA-256' } }, + false, + ['sign'] + ); + const strData = stringToBuffer(str); + const signature = await crypto.subtle.sign('HMAC', importedKey, strData); + const digest = new Uint8Array(signature); + return digest; +}; + +/** + * Converts header values according to AWS requirements, + * From https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv-create-signed-request.html#create-canonical-request + * For values, you must: + - trim any leading or trailing spaces. + - convert sequential spaces to a single space. + * @param value - Header value to convert. + * @returns - Converted header value. + */ +const convertHeaderValue = (value: string | number) => { + return value.toString().trim().replace(/\s+/g, ' '); +}; + +/** + * Returns a Uint8Array representation of a string, encoded in UTF-8. + * @param str - String to convert. + * @returns Uint8Array containing the UTF-8 encoded string. + */ +function stringToBuffer(str: string): Uint8Array { + const data = new Uint8Array(ByteUtils.utf8ByteLength(str)); + ByteUtils.encodeUTF8Into(data, str, 0); + return data; +} + +/** + * This method implements AWS Signature 4 logic for a very specific request format. + * The signing logic is described here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv-create-signed-request.html + */ +export async function aws4Sign( + options: AwsSigv4Options, + credentials: AWSCredentials +): Promise { + /** + * From the spec: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv-create-signed-request.html + * + * Summary of signing steps + * 1. Create a canonical request + * Arrange the contents of your request (host, action, headers, etc.) into a standard canonical format. The canonical request is one of the inputs used to create the string to sign. + * 2. Create a hash of the canonical request + * Hash the canonical request using the same algorithm that you used to create the hash of the payload. The hash of the canonical request is a string of lowercase hexadecimal characters. + * 3. Create a string to sign + * Create a string to sign with the canonical request and extra information such as the algorithm, request date, credential scope, and the hash of the canonical request. + * 4. Derive a signing key + * Use the secret access key to derive the key used to sign the request. + * 5. Calculate the signature + * Perform a keyed hash operation on the string to sign using the derived signing key as the hash key. + * 6. Add the signature to the request + * Add the calculated signature to an HTTP header or to the query string of the request. + */ + + // 1: Create a canonical request + + // Date – The date and time used to sign the request. + const date = options.date; + // RequestDateTime – The date and time used in the credential scope. This value is the current UTC time in ISO 8601 format (for example, 20130524T000000Z). + const requestDateTime = date.toISOString().replace(/[:-]|\.\d{3}/g, ''); + // RequestDate – The date used in the credential scope. This value is the current UTC date in YYYYMMDD format (for example, 20130524). + const requestDate = requestDateTime.substring(0, 8); + // Method – The HTTP request method. For us, this is always 'POST'. + const method = options.method; + // CanonicalUri – The URI-encoded version of the absolute path component URI, starting with the / that follows the domain name and up to the end of the string + // For our requests, this is always '/' + const canonicalUri = options.path; + // CanonicalQueryString – The URI-encoded query string parameters. For our requests, there are no query string parameters, so this is always an empty string. + const canonicalQuerystring = ''; + + // CanonicalHeaders – A list of request headers with their values. Individual header name and value pairs are separated by the newline character ("\n"). + // All of our known/expected headers are included here, there are no extra headers. + const headers = new Headers({ + 'content-length': convertHeaderValue(options.headers['Content-Length']), + 'content-type': convertHeaderValue(options.headers['Content-Type']), + host: convertHeaderValue(options.host), + 'x-amz-date': convertHeaderValue(requestDateTime), + 'x-mongodb-gs2-cb-flag': convertHeaderValue(options.headers['X-MongoDB-GS2-CB-Flag']), + 'x-mongodb-server-nonce': convertHeaderValue(options.headers['X-MongoDB-Server-Nonce']) + }); + // If session token is provided, include it in the headers + if ('sessionToken' in credentials && credentials.sessionToken) { + headers.append('x-amz-security-token', convertHeaderValue(credentials.sessionToken)); + } + + // Canonical headers are lowercased and sorted. + const canonicalHeaders = Array.from(headers.entries()) + .map(([key, value]) => `${key.toLowerCase()}:${value}`) + .sort() + .join('\n'); + const canonicalHeaderNames = Array.from(headers.keys()).map(header => header.toLowerCase()); + // SignedHeaders – An alphabetically sorted, semicolon-separated list of lowercase request header names. + const signedHeaders = canonicalHeaderNames.sort().join(';'); + + // HashedPayload – A string created using the payload in the body of the HTTP request as input to a hash function. This string uses lowercase hexadecimal characters. + const hashedPayload = await getHexSha256(options.body); + + // CanonicalRequest – A string that includes the above elements, separated by newline characters. + const canonicalRequest = [ + method, + canonicalUri, + canonicalQuerystring, + canonicalHeaders + '\n', + signedHeaders, + hashedPayload + ].join('\n'); + + // 2. Create a hash of the canonical request + // HashedCanonicalRequest – A string created by using the canonical request as input to a hash function. + const hashedCanonicalRequest = await getHexSha256(canonicalRequest); + + // 3. Create a string to sign + // Algorithm – The algorithm used to create the hash of the canonical request. For SigV4, use AWS4-HMAC-SHA256. + const algorithm = 'AWS4-HMAC-SHA256'; + // CredentialScope – The credential scope, which restricts the resulting signature to the specified Region and service. + // Has the following format: YYYYMMDD/region/service/aws4_request. + const credentialScope = `${requestDate}/${options.region}/${options.service}/aws4_request`; + // StringToSign – A string that includes the above elements, separated by newline characters. + const stringToSign = [algorithm, requestDateTime, credentialScope, hashedCanonicalRequest].join( + '\n' + ); + + // 4. Derive a signing key + // To derive a signing key for SigV4, perform a succession of keyed hash operations (HMAC) on the request date, Region, and service, with your AWS secret access key as the key for the initial hashing operation. + const dateKey = await getHmacSha256('AWS4' + credentials.secretAccessKey, requestDate); + const dateRegionKey = await getHmacSha256(dateKey, options.region); + const dateRegionServiceKey = await getHmacSha256(dateRegionKey, options.service); + const signingKey = await getHmacSha256(dateRegionServiceKey, 'aws4_request'); + + // 5. Calculate the signature + const signatureBuffer = await getHmacSha256(signingKey, stringToSign); + const signature = ByteUtils.toHex(signatureBuffer); + + // 6. Add the signature to the request + // Calculate the Authorization header + const authorizationHeader = [ + 'AWS4-HMAC-SHA256 Credential=' + credentials.accessKeyId + '/' + credentialScope, + 'SignedHeaders=' + signedHeaders, + 'Signature=' + signature + ].join(', '); + + // Return the calculated headers + return { + Authorization: authorizationHeader, + 'X-Amz-Date': requestDateTime + }; +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/aws_temporary_credentials.ts b/gateway/node_modules/mongodb/src/cmap/auth/aws_temporary_credentials.ts new file mode 100644 index 0000000000000000000000000000000000000000..13d23a5a915e5a2d35d08f120b607cbbaefb6db5 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/aws_temporary_credentials.ts @@ -0,0 +1,129 @@ +import * as process from 'process'; + +import { type AWSCredentials, getAwsCredentialProvider } from '../../deps'; +import { MongoAWSError } from '../../error'; + +/** + * @internal + * This interface matches the final result of fetching temporary credentials manually, outlined + * in the spec [here](https://github.com/mongodb/specifications/blob/master/source/auth/auth.md#ec2-endpoint). + * + * When we use the AWS SDK, we map the response from the SDK to conform to this interface. + */ +export interface AWSTempCredentials { + AccessKeyId?: string; + SecretAccessKey?: string; + Token?: string; + RoleArn?: string; + Expiration?: Date; +} + +/** @public **/ +export type AWSCredentialProvider = () => Promise; + +/** @internal */ +export class AWSSDKCredentialProvider { + private static _awsSDK: ReturnType; + private _provider?: AWSCredentialProvider; + + /** + * Create the SDK credentials provider. + * @param credentialsProvider - The credentials provider. + */ + constructor(credentialsProvider?: AWSCredentialProvider) { + if (credentialsProvider) { + this._provider = credentialsProvider; + } + } + + static get awsSDK() { + AWSSDKCredentialProvider._awsSDK ??= getAwsCredentialProvider(); + return AWSSDKCredentialProvider._awsSDK; + } + + /** + * The AWS SDK caches credentials automatically and handles refresh when the credentials have expired. + * To ensure this occurs, we need to cache the `provider` returned by the AWS sdk and re-use it when fetching credentials. + */ + private get provider(): () => Promise { + if ('kModuleError' in AWSSDKCredentialProvider.awsSDK) { + throw AWSSDKCredentialProvider.awsSDK.kModuleError; + } + if (this._provider) { + return this._provider; + } + let { AWS_STS_REGIONAL_ENDPOINTS = '', AWS_REGION = '' } = process.env; + AWS_STS_REGIONAL_ENDPOINTS = AWS_STS_REGIONAL_ENDPOINTS.toLowerCase(); + AWS_REGION = AWS_REGION.toLowerCase(); + + /** The option setting should work only for users who have explicit settings in their environment, the driver should not encode "defaults" */ + const awsRegionSettingsExist = + AWS_REGION.length !== 0 && AWS_STS_REGIONAL_ENDPOINTS.length !== 0; + + /** + * The following regions use the global AWS STS endpoint, sts.amazonaws.com, by default + * https://docs.aws.amazon.com/sdkref/latest/guide/feature-sts-regionalized-endpoints.html + */ + const LEGACY_REGIONS = new Set([ + 'ap-northeast-1', + 'ap-south-1', + 'ap-southeast-1', + 'ap-southeast-2', + 'aws-global', + 'ca-central-1', + 'eu-central-1', + 'eu-north-1', + 'eu-west-1', + 'eu-west-2', + 'eu-west-3', + 'sa-east-1', + 'us-east-1', + 'us-east-2', + 'us-west-1', + 'us-west-2' + ]); + /** + * If AWS_STS_REGIONAL_ENDPOINTS is set to regional, users are opting into the new behavior of respecting the region settings + * + * If AWS_STS_REGIONAL_ENDPOINTS is set to legacy, then "old" regions need to keep using the global setting. + * Technically the SDK gets this wrong, it reaches out to 'sts.us-east-1.amazonaws.com' when it should be 'sts.amazonaws.com'. + * That is not our bug to fix here. We leave that up to the SDK. + */ + const useRegionalSts = + AWS_STS_REGIONAL_ENDPOINTS === 'regional' || + (AWS_STS_REGIONAL_ENDPOINTS === 'legacy' && !LEGACY_REGIONS.has(AWS_REGION)); + + this._provider = + awsRegionSettingsExist && useRegionalSts + ? AWSSDKCredentialProvider.awsSDK.fromNodeProviderChain({ + clientConfig: { region: AWS_REGION } + }) + : AWSSDKCredentialProvider.awsSDK.fromNodeProviderChain(); + + return this._provider; + } + + async getCredentials(): Promise { + /* + * Creates a credential provider that will attempt to find credentials from the + * following sources (listed in order of precedence): + * + * - Environment variables exposed via process.env + * - SSO credentials from token cache + * - Web identity token credentials + * - Shared credentials and config ini files + * - The EC2/ECS Instance Metadata Service + */ + try { + const creds = await this.provider(); + return { + AccessKeyId: creds.accessKeyId, + SecretAccessKey: creds.secretAccessKey, + Token: creds.sessionToken, + Expiration: creds.expiration + }; + } catch (error) { + throw new MongoAWSError(error.message, { cause: error }); + } + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/gssapi.ts b/gateway/node_modules/mongodb/src/cmap/auth/gssapi.ts new file mode 100644 index 0000000000000000000000000000000000000000..12595242f38c985686231d496b59ae39e677361b --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/gssapi.ts @@ -0,0 +1,206 @@ +import * as dns from 'dns'; + +import { getKerberos, type Kerberos, type KerberosClient } from '../../deps'; +import { MongoInvalidArgumentError, MongoMissingCredentialsError } from '../../error'; +import { ns } from '../../utils'; +import type { Connection } from '../connection'; +import { type AuthContext, AuthProvider } from './auth_provider'; + +/** @public */ +export const GSSAPICanonicalizationValue = Object.freeze({ + on: true, + off: false, + none: 'none', + forward: 'forward', + forwardAndReverse: 'forwardAndReverse' +} as const); + +/** @public */ +export type GSSAPICanonicalizationValue = + (typeof GSSAPICanonicalizationValue)[keyof typeof GSSAPICanonicalizationValue]; + +type MechanismProperties = { + CANONICALIZE_HOST_NAME?: GSSAPICanonicalizationValue; + SERVICE_HOST?: string; + SERVICE_NAME?: string; + SERVICE_REALM?: string; +}; + +async function externalCommand( + connection: Connection, + command: ReturnType | ReturnType +): Promise<{ payload: string; conversationId: number }> { + const response = await connection.command(ns('$external.$cmd'), command); + return response as { payload: string; conversationId: number }; +} + +let krb: Kerberos; + +export class GSSAPI extends AuthProvider { + override async auth(authContext: AuthContext): Promise { + const { connection, credentials } = authContext; + if (credentials == null) { + throw new MongoMissingCredentialsError('Credentials required for GSSAPI authentication'); + } + + const { username } = credentials; + + const client = await makeKerberosClient(authContext); + + const payload = await client.step(''); + + const saslStartResponse = await externalCommand(connection, saslStart(payload)); + + const negotiatedPayload = await negotiate(client, 10, saslStartResponse.payload); + + const saslContinueResponse = await externalCommand( + connection, + saslContinue(negotiatedPayload, saslStartResponse.conversationId) + ); + + const finalizePayload = await finalize(client, username, saslContinueResponse.payload); + + await externalCommand(connection, { + saslContinue: 1, + conversationId: saslContinueResponse.conversationId, + payload: finalizePayload + }); + } +} + +async function makeKerberosClient({ + options: { + hostAddress, + runtime: { os } + }, + credentials +}: AuthContext): Promise { + if (!hostAddress || typeof hostAddress.host !== 'string' || !credentials) { + throw new MongoInvalidArgumentError( + 'Connection must have host and port and credentials defined.' + ); + } + + loadKrb(); + if ('kModuleError' in krb) { + throw krb['kModuleError']; + } + const { initializeClient } = krb; + + const { username, password } = credentials; + const mechanismProperties = credentials.mechanismProperties as MechanismProperties; + + const serviceName = mechanismProperties.SERVICE_NAME ?? 'mongodb'; + + const host = await performGSSAPICanonicalizeHostName(hostAddress.host, mechanismProperties); + + const initOptions = {}; + if (password != null) { + // TODO(NODE-5139): These do not match the typescript options in initializeClient + Object.assign(initOptions, { user: username, password: password }); + } + + const spnHost = mechanismProperties.SERVICE_HOST ?? host; + let spn = `${serviceName}${os.platform() === 'win32' ? '/' : '@'}${spnHost}`; + if ('SERVICE_REALM' in mechanismProperties) { + spn = `${spn}@${mechanismProperties.SERVICE_REALM}`; + } + + return await initializeClient(spn, initOptions); +} + +function saslStart(payload: string) { + return { + saslStart: 1, + mechanism: 'GSSAPI', + payload, + autoAuthorize: 1 + } as const; +} + +function saslContinue(payload: string, conversationId: number) { + return { + saslContinue: 1, + conversationId, + payload + } as const; +} + +async function negotiate( + client: KerberosClient, + retries: number, + payload: string +): Promise { + try { + const response = await client.step(payload); + return response || ''; + } catch (error) { + if (retries === 0) { + // Retries exhausted, raise error + throw error; + } + // Adjust number of retries and call step again + return await negotiate(client, retries - 1, payload); + } +} + +async function finalize(client: KerberosClient, user: string, payload: string): Promise { + // GSS Client Unwrap + const response = await client.unwrap(payload); + return await client.wrap(response || '', { user }); +} + +export async function performGSSAPICanonicalizeHostName( + host: string, + mechanismProperties: MechanismProperties +): Promise { + const mode = mechanismProperties.CANONICALIZE_HOST_NAME; + if (!mode || mode === GSSAPICanonicalizationValue.none) { + return host; + } + + // If forward and reverse or true + if ( + mode === GSSAPICanonicalizationValue.on || + mode === GSSAPICanonicalizationValue.forwardAndReverse + ) { + // Perform the lookup of the ip address. + const { address } = await dns.promises.lookup(host); + + try { + // Perform a reverse ptr lookup on the ip address. + const results = await dns.promises.resolve(address, 'PTR'); + // If the ptr did not error but had no results, return the host. + return results.length > 0 ? results[0] : host; + } catch { + // This can error as ptr records may not exist for all ips. In this case + // fallback to a cname lookup as dns.lookup() does not return the + // cname. + return await resolveCname(host); + } + } else { + // The case for forward is just to resolve the cname as dns.lookup() + // will not return it. + return await resolveCname(host); + } +} + +export async function resolveCname(host: string): Promise { + // Attempt to resolve the host name + try { + const results = await dns.promises.resolve(host, 'CNAME'); + // Get the first resolved host id + return results.length > 0 ? results[0] : host; + } catch { + return host; + } +} + +/** + * Load the Kerberos library. + */ +function loadKrb() { + if (!krb) { + krb = getKerberos(); + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/mongo_credentials.ts b/gateway/node_modules/mongodb/src/cmap/auth/mongo_credentials.ts new file mode 100644 index 0000000000000000000000000000000000000000..f8d7e33c95c0802fd6b4b6cfee5cc7f7490fb165 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/mongo_credentials.ts @@ -0,0 +1,292 @@ +// Resolves the default auth mechanism according to +// Resolves the default auth mechanism according to +import type { Document } from '../../bson'; +import { + MongoAPIError, + MongoInvalidArgumentError, + MongoMissingCredentialsError +} from '../../error'; +import type { AWSCredentialProvider } from './aws_temporary_credentials'; +import { GSSAPICanonicalizationValue } from './gssapi'; +import type { OIDCCallbackFunction } from './mongodb_oidc'; +import { AUTH_MECHS_AUTH_SRC_EXTERNAL, AuthMechanism } from './providers'; + +/** + * @see https://github.com/mongodb/specifications/blob/master/source/auth/auth.md + */ +function getDefaultAuthMechanism(hello: Document | null): AuthMechanism { + if (hello) { + // If hello contains saslSupportedMechs, use scram-sha-256 + // if it is available, else scram-sha-1 + if (Array.isArray(hello.saslSupportedMechs)) { + return hello.saslSupportedMechs.includes(AuthMechanism.MONGODB_SCRAM_SHA256) + ? AuthMechanism.MONGODB_SCRAM_SHA256 + : AuthMechanism.MONGODB_SCRAM_SHA1; + } + } + + // Default auth mechanism for 4.0 and higher. + return AuthMechanism.MONGODB_SCRAM_SHA256; +} + +const ALLOWED_ENVIRONMENT_NAMES: AuthMechanismProperties['ENVIRONMENT'][] = [ + 'test', + 'azure', + 'gcp', + 'k8s' +]; +const ALLOWED_HOSTS_ERROR = 'Auth mechanism property ALLOWED_HOSTS must be an array of strings.'; + +/** @internal */ +export const DEFAULT_ALLOWED_HOSTS = [ + '*.mongodb.net', + '*.mongodb-qa.net', + '*.mongodb-dev.net', + '*.mongodbgov.net', + 'localhost', + '127.0.0.1', + '::1', + '*.mongo.com' +]; + +/** Error for when the token audience is missing in the environment. */ +const TOKEN_RESOURCE_MISSING_ERROR = + 'TOKEN_RESOURCE must be set in the auth mechanism properties when ENVIRONMENT is azure or gcp.'; + +/** @public */ +export interface AuthMechanismProperties extends Document { + SERVICE_HOST?: string; + SERVICE_NAME?: string; + SERVICE_REALM?: string; + CANONICALIZE_HOST_NAME?: GSSAPICanonicalizationValue; + /** @internal */ + AWS_SESSION_TOKEN?: string; + /** A user provided OIDC machine callback function. */ + OIDC_CALLBACK?: OIDCCallbackFunction; + /** A user provided OIDC human interacted callback function. */ + OIDC_HUMAN_CALLBACK?: OIDCCallbackFunction; + /** The OIDC environment. Note that 'test' is for internal use only. */ + ENVIRONMENT?: 'test' | 'azure' | 'gcp' | 'k8s'; + /** Allowed hosts that OIDC auth can connect to. */ + ALLOWED_HOSTS?: string[]; + /** The resource token for OIDC auth in Azure and GCP. */ + TOKEN_RESOURCE?: string; + /** + * A custom AWS credential provider to use. An example using the AWS SDK default provider chain: + * + * ```ts + * const client = new MongoClient(process.env.MONGODB_URI, { + * authMechanismProperties: { + * AWS_CREDENTIAL_PROVIDER: fromNodeProviderChain() + * } + * }); + * ``` + * + * Using a custom function that returns AWS credentials: + * + * ```ts + * const client = new MongoClient(process.env.MONGODB_URI, { + * authMechanismProperties: { + * AWS_CREDENTIAL_PROVIDER: async () => { + * return { + * accessKeyId: process.env.ACCESS_KEY_ID, + * secretAccessKey: process.env.SECRET_ACCESS_KEY + * } + * } + * } + * }); + * ``` + */ + AWS_CREDENTIAL_PROVIDER?: AWSCredentialProvider; +} + +/** @public */ +export interface MongoCredentialsOptions { + username?: string; + password: string; + source: string; + db?: string; + mechanism?: AuthMechanism; + mechanismProperties: AuthMechanismProperties; +} + +/** + * A representation of the credentials used by MongoDB + * @public + */ +export class MongoCredentials { + /** The username used for authentication */ + readonly username: string; + /** The password used for authentication */ + readonly password: string; + /** The database that the user should authenticate against */ + readonly source: string; + /** The method used to authenticate */ + readonly mechanism: AuthMechanism; + /** Special properties used by some types of auth mechanisms */ + readonly mechanismProperties: AuthMechanismProperties; + + constructor(options: MongoCredentialsOptions) { + this.username = options.username ?? ''; + this.password = options.password; + this.source = options.source; + if (!this.source && options.db) { + this.source = options.db; + } + this.mechanism = options.mechanism || AuthMechanism.MONGODB_DEFAULT; + this.mechanismProperties = options.mechanismProperties || {}; + + if (this.mechanism === AuthMechanism.MONGODB_OIDC && !this.mechanismProperties.ALLOWED_HOSTS) { + this.mechanismProperties = { + ...this.mechanismProperties, + ALLOWED_HOSTS: DEFAULT_ALLOWED_HOSTS + }; + } + + Object.freeze(this.mechanismProperties); + Object.freeze(this); + } + + /** Determines if two MongoCredentials objects are equivalent */ + equals(other: MongoCredentials): boolean { + return ( + this.mechanism === other.mechanism && + this.username === other.username && + this.password === other.password && + this.source === other.source + ); + } + + /** + * If the authentication mechanism is set to "default", resolves the authMechanism + * based on the server version and server supported sasl mechanisms. + * + * @param hello - A hello response from the server + */ + resolveAuthMechanism(hello: Document | null): MongoCredentials { + // If the mechanism is not "default", then it does not need to be resolved + if (this.mechanism.match(/DEFAULT/i)) { + return new MongoCredentials({ + username: this.username, + password: this.password, + source: this.source, + mechanism: getDefaultAuthMechanism(hello), + mechanismProperties: this.mechanismProperties + }); + } + + return this; + } + + validate(): void { + if ( + (this.mechanism === AuthMechanism.MONGODB_GSSAPI || + this.mechanism === AuthMechanism.MONGODB_PLAIN || + this.mechanism === AuthMechanism.MONGODB_SCRAM_SHA1 || + this.mechanism === AuthMechanism.MONGODB_SCRAM_SHA256) && + !this.username + ) { + throw new MongoMissingCredentialsError(`Username required for mechanism '${this.mechanism}'`); + } + + if (this.mechanism === AuthMechanism.MONGODB_OIDC) { + if ( + this.username && + this.mechanismProperties.ENVIRONMENT && + this.mechanismProperties.ENVIRONMENT !== 'azure' + ) { + throw new MongoInvalidArgumentError( + `username and ENVIRONMENT '${this.mechanismProperties.ENVIRONMENT}' may not be used together for mechanism '${this.mechanism}'.` + ); + } + + if (this.username && this.password) { + throw new MongoInvalidArgumentError( + `No password is allowed in ENVIRONMENT '${this.mechanismProperties.ENVIRONMENT}' for '${this.mechanism}'.` + ); + } + + if ( + (this.mechanismProperties.ENVIRONMENT === 'azure' || + this.mechanismProperties.ENVIRONMENT === 'gcp') && + !this.mechanismProperties.TOKEN_RESOURCE + ) { + throw new MongoInvalidArgumentError(TOKEN_RESOURCE_MISSING_ERROR); + } + + if ( + this.mechanismProperties.ENVIRONMENT && + !ALLOWED_ENVIRONMENT_NAMES.includes(this.mechanismProperties.ENVIRONMENT) + ) { + throw new MongoInvalidArgumentError( + `Currently only a ENVIRONMENT in ${ALLOWED_ENVIRONMENT_NAMES.join( + ',' + )} is supported for mechanism '${this.mechanism}'.` + ); + } + + if ( + !this.mechanismProperties.ENVIRONMENT && + !this.mechanismProperties.OIDC_CALLBACK && + !this.mechanismProperties.OIDC_HUMAN_CALLBACK + ) { + throw new MongoInvalidArgumentError( + `Either a ENVIRONMENT, OIDC_CALLBACK, or OIDC_HUMAN_CALLBACK must be specified for mechanism '${this.mechanism}'.` + ); + } + + if (this.mechanismProperties.ALLOWED_HOSTS) { + const hosts = this.mechanismProperties.ALLOWED_HOSTS; + if (!Array.isArray(hosts)) { + throw new MongoInvalidArgumentError(ALLOWED_HOSTS_ERROR); + } + for (const host of hosts) { + if (typeof host !== 'string') { + throw new MongoInvalidArgumentError(ALLOWED_HOSTS_ERROR); + } + } + } + } + + if (AUTH_MECHS_AUTH_SRC_EXTERNAL.has(this.mechanism)) { + if (this.source != null && this.source !== '$external') { + // TODO(NODE-3485): Replace this with a MongoAuthValidationError + throw new MongoAPIError( + `Invalid source '${this.source}' for mechanism '${this.mechanism}' specified.` + ); + } + } + + if (this.mechanism === AuthMechanism.MONGODB_PLAIN && this.source == null) { + // TODO(NODE-3485): Replace this with a MongoAuthValidationError + throw new MongoAPIError('PLAIN Authentication Mechanism needs an auth source'); + } + + if (this.mechanism === AuthMechanism.MONGODB_X509 && this.password != null) { + if (this.password === '') { + Reflect.set(this, 'password', undefined); + return; + } + // TODO(NODE-3485): Replace this with a MongoAuthValidationError + throw new MongoAPIError(`Password not allowed for mechanism MONGODB-X509`); + } + + const canonicalization = this.mechanismProperties.CANONICALIZE_HOST_NAME ?? false; + if (!Object.values(GSSAPICanonicalizationValue).includes(canonicalization)) { + throw new MongoAPIError(`Invalid CANONICALIZE_HOST_NAME value: ${canonicalization}`); + } + } + + static merge( + creds: MongoCredentials | undefined, + options: Partial + ): MongoCredentials { + return new MongoCredentials({ + username: options.username ?? creds?.username ?? '', + password: options.password ?? creds?.password ?? '', + mechanism: options.mechanism ?? creds?.mechanism ?? AuthMechanism.MONGODB_DEFAULT, + mechanismProperties: options.mechanismProperties ?? creds?.mechanismProperties ?? {}, + source: options.source ?? options.db ?? creds?.source ?? 'admin' + }); + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/mongodb_aws.ts b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_aws.ts new file mode 100644 index 0000000000000000000000000000000000000000..422beb3dbd2ce805016b087f0f673c30d940e7c3 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_aws.ts @@ -0,0 +1,179 @@ +import { type Binary, type BSONSerializeOptions, ByteUtils } from '../../bson'; +import * as BSON from '../../bson'; +import { + MongoCompatibilityError, + MongoMissingCredentialsError, + MongoRuntimeError +} from '../../error'; +import { maxWireVersion, ns, randomBytes } from '../../utils'; +import { type AuthContext, AuthProvider } from './auth_provider'; +import { + type AWSCredentialProvider, + AWSSDKCredentialProvider, + type AWSTempCredentials +} from './aws_temporary_credentials'; +import { aws4Sign } from './aws4'; +import { MongoCredentials } from './mongo_credentials'; +import { AuthMechanism } from './providers'; + +const ASCII_N = 110; +const bsonOptions: BSONSerializeOptions = { + useBigInt64: false, + promoteLongs: true, + promoteValues: true, + promoteBuffers: false, + bsonRegExp: false +}; + +interface AWSSaslContinuePayload { + a: string; + d: string; + t?: string; +} + +export class MongoDBAWS extends AuthProvider { + private credentialFetcher: AWSSDKCredentialProvider; + + constructor(credentialProvider?: AWSCredentialProvider) { + super(); + this.credentialFetcher = new AWSSDKCredentialProvider(credentialProvider); + } + + override async auth(authContext: AuthContext): Promise { + const { connection } = authContext; + if (!authContext.credentials) { + throw new MongoMissingCredentialsError('AuthContext must provide credentials.'); + } + + if (maxWireVersion(connection) < 9) { + throw new MongoCompatibilityError( + 'MONGODB-AWS authentication requires MongoDB version 4.4 or later' + ); + } + + authContext.credentials = await makeTempCredentials( + authContext.credentials, + this.credentialFetcher + ); + + const { credentials } = authContext; + + const accessKeyId = credentials.username; + const secretAccessKey = credentials.password; + // Allow the user to specify an AWS session token for authentication with temporary credentials. + const sessionToken = credentials.mechanismProperties.AWS_SESSION_TOKEN; + + // If all three defined, include sessionToken, else only include username and pass + const awsCredentials = sessionToken + ? { accessKeyId, secretAccessKey, sessionToken } + : { accessKeyId, secretAccessKey }; + + const db = credentials.source; + const nonce = await randomBytes(32); + + // All messages between MongoDB clients and servers are sent as BSON objects + // in the payload field of saslStart and saslContinue. + const saslStart = { + saslStart: 1, + mechanism: 'MONGODB-AWS', + payload: BSON.serialize({ r: nonce, p: ASCII_N }, bsonOptions) + }; + + const saslStartResponse = await connection.command(ns(`${db}.$cmd`), saslStart, undefined); + + const serverResponse = BSON.deserialize(saslStartResponse.payload.buffer, bsonOptions) as { + s: Binary; + h: string; + }; + const host = serverResponse.h; + const serverNonce = serverResponse.s.buffer; + if (serverNonce.length !== 64) { + // TODO(NODE-3483) + throw new MongoRuntimeError(`Invalid server nonce length ${serverNonce.length}, expected 64`); + } + + if (!ByteUtils.equals(serverNonce.subarray(0, nonce.byteLength), nonce)) { + // throw because the serverNonce's leading 32 bytes must equal the client nonce's 32 bytes + // https://github.com/mongodb/specifications/blob/master/source/auth/auth.md#conversation-5 + + // TODO(NODE-3483) + throw new MongoRuntimeError('Server nonce does not begin with client nonce'); + } + + if (host.length < 1 || host.length > 255 || host.indexOf('..') !== -1) { + // TODO(NODE-3483) + throw new MongoRuntimeError(`Server returned an invalid host: "${host}"`); + } + + const body = 'Action=GetCallerIdentity&Version=2011-06-15'; + const headers = await aws4Sign( + { + method: 'POST', + host, + region: deriveRegion(serverResponse.h), + service: 'sts', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Content-Length': body.length, + 'X-MongoDB-Server-Nonce': ByteUtils.toBase64(serverNonce), + 'X-MongoDB-GS2-CB-Flag': 'n' + }, + path: '/', + body, + date: new Date() + }, + awsCredentials + ); + + const payload: AWSSaslContinuePayload = { + a: headers.Authorization, + d: headers['X-Amz-Date'] + }; + + if (sessionToken) { + payload.t = sessionToken; + } + + const saslContinue = { + saslContinue: 1, + conversationId: saslStartResponse.conversationId, + payload: BSON.serialize(payload, bsonOptions) + }; + + await connection.command(ns(`${db}.$cmd`), saslContinue, undefined); + } +} + +async function makeTempCredentials( + credentials: MongoCredentials, + awsCredentialFetcher: AWSSDKCredentialProvider +): Promise { + function makeMongoCredentialsFromAWSTemp(creds: AWSTempCredentials) { + // The AWS session token (creds.Token) may or may not be set. + if (!creds.AccessKeyId || !creds.SecretAccessKey) { + throw new MongoMissingCredentialsError('Could not obtain temporary MONGODB-AWS credentials'); + } + + return new MongoCredentials({ + username: creds.AccessKeyId, + password: creds.SecretAccessKey, + source: credentials.source, + mechanism: AuthMechanism.MONGODB_AWS, + mechanismProperties: { + AWS_SESSION_TOKEN: creds.Token + } + }); + } + const temporaryCredentials = await awsCredentialFetcher.getCredentials(); + + return makeMongoCredentialsFromAWSTemp(temporaryCredentials); +} + +function deriveRegion(host: string) { + const parts = host.split('.'); + if (parts.length === 1 || parts[1] === 'amazonaws') { + return 'us-east-1'; + } + + return parts[1]; +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc.ts b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc.ts new file mode 100644 index 0000000000000000000000000000000000000000..474f49a8fd5181c12ea35e65d2decb2281e78cd2 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc.ts @@ -0,0 +1,185 @@ +import type { Document } from '../../bson'; +import { MongoInvalidArgumentError, MongoMissingCredentialsError } from '../../error'; +import type { HandshakeDocument } from '../connect'; +import type { Connection } from '../connection'; +import { type AuthContext, AuthProvider } from './auth_provider'; +import type { MongoCredentials } from './mongo_credentials'; +import { AutomatedCallbackWorkflow } from './mongodb_oidc/automated_callback_workflow'; +import { azureCallback } from './mongodb_oidc/azure_machine_workflow'; +import { gcpCallback } from './mongodb_oidc/gcp_machine_workflow'; +import { k8sCallback } from './mongodb_oidc/k8s_machine_workflow'; +import { TokenCache } from './mongodb_oidc/token_cache'; +import { tokenMachineCallback as testCallback } from './mongodb_oidc/token_machine_workflow'; + +/** Error when credentials are missing. */ +const MISSING_CREDENTIALS_ERROR = 'AuthContext must provide credentials.'; + +/** + * The information returned by the server on the IDP server. + * @public + */ +export interface IdPInfo { + /** + * A URL which describes the Authentication Server. This identifier should + * be the iss of provided access tokens, and be viable for RFC8414 metadata + * discovery and RFC9207 identification. + */ + issuer: string; + /** A unique client ID for this OIDC client. */ + clientId: string; + /** A list of additional scopes to request from IdP. */ + requestScopes?: string[]; +} + +/** + * The response from the IdP server with the access token and + * optional expiration time and refresh token. + * @public + */ +export interface IdPServerResponse { + /** The OIDC access token. */ + accessToken: string; + /** The time when the access token expires. For future use. */ + expiresInSeconds?: number; + /** The refresh token, if applicable, to be used by the callback to request a new token from the issuer. */ + refreshToken?: string; +} + +/** + * The response required to be returned from the machine or + * human callback workflows' callback. + * @public + */ +export interface OIDCResponse { + /** The OIDC access token. */ + accessToken: string; + /** The time when the access token expires. For future use. */ + expiresInSeconds?: number; + /** The refresh token, if applicable, to be used by the callback to request a new token from the issuer. */ + refreshToken?: string; +} + +/** + * The parameters that the driver provides to the user supplied + * human or machine callback. + * + * The version number is used to communicate callback API changes that are not breaking but that + * users may want to know about and review their implementation. Users may wish to check the version + * number and throw an error if their expected version number and the one provided do not match. + * @public + */ +export interface OIDCCallbackParams { + /** Optional username. */ + username?: string; + /** The context in which to timeout the OIDC callback. */ + timeoutContext: AbortSignal; + /** The current OIDC API version. */ + version: 1; + /** The IdP information returned from the server. */ + idpInfo?: IdPInfo; + /** The refresh token, if applicable, to be used by the callback to request a new token from the issuer. */ + refreshToken?: string; + /** The token audience for GCP and Azure. */ + tokenAudience?: string; +} + +/** + * The signature of the human or machine callback functions. + * @public + */ +export type OIDCCallbackFunction = (params: OIDCCallbackParams) => Promise; + +/** The current version of OIDC implementation. */ +export const OIDC_VERSION = 1; + +type EnvironmentName = 'test' | 'azure' | 'gcp' | 'k8s' | undefined; + +/** @internal */ +export interface Workflow { + cache: TokenCache; + + /** + * All device workflows must implement this method in order to get the access + * token and then call authenticate with it. + */ + execute( + connection: Connection, + credentials: MongoCredentials, + response?: Document + ): Promise; + + /** + * Each workflow should specify the correct custom behaviour for reauthentication. + */ + reauthenticate(connection: Connection, credentials: MongoCredentials): Promise; + + /** + * Get the document to add for speculative authentication. + */ + speculativeAuth(connection: Connection, credentials: MongoCredentials): Promise; +} + +/** @internal */ +export const OIDC_WORKFLOWS: Map Workflow> = new Map(); +OIDC_WORKFLOWS.set('test', () => new AutomatedCallbackWorkflow(new TokenCache(), testCallback)); +OIDC_WORKFLOWS.set('azure', () => new AutomatedCallbackWorkflow(new TokenCache(), azureCallback)); +OIDC_WORKFLOWS.set('gcp', () => new AutomatedCallbackWorkflow(new TokenCache(), gcpCallback)); +OIDC_WORKFLOWS.set('k8s', () => new AutomatedCallbackWorkflow(new TokenCache(), k8sCallback)); + +/** + * OIDC auth provider. + */ +export class MongoDBOIDC extends AuthProvider { + workflow: Workflow; + + /** + * Instantiate the auth provider. + */ + constructor(workflow?: Workflow) { + super(); + if (!workflow) { + throw new MongoInvalidArgumentError('No workflow provided to the OIDC auth provider.'); + } + this.workflow = workflow; + } + + /** + * Authenticate using OIDC + */ + override async auth(authContext: AuthContext): Promise { + const { connection, reauthenticating, response } = authContext; + if (response?.speculativeAuthenticate?.done && !reauthenticating) { + return; + } + const credentials = getCredentials(authContext); + if (reauthenticating) { + await this.workflow.reauthenticate(connection, credentials); + } else { + await this.workflow.execute(connection, credentials, response); + } + } + + /** + * Add the speculative auth for the initial handshake. + */ + override async prepare( + handshakeDoc: HandshakeDocument, + authContext: AuthContext + ): Promise { + const { connection } = authContext; + const credentials = getCredentials(authContext); + const result = await this.workflow.speculativeAuth(connection, credentials); + return { ...handshakeDoc, ...result }; + } +} + +/** + * Get credentials from the auth context, throwing if they do not exist. + */ +function getCredentials(authContext: AuthContext): MongoCredentials { + const { credentials } = authContext; + if (!credentials) { + throw new MongoMissingCredentialsError(MISSING_CREDENTIALS_ERROR); + } + return credentials; +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts new file mode 100644 index 0000000000000000000000000000000000000000..1f4947d89a97ff7c28cbed57ca4785de9c8ffd31 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts @@ -0,0 +1,88 @@ +import { MONGODB_ERROR_CODES, MongoError, MongoOIDCError } from '../../../error'; +import { Timeout, TimeoutError } from '../../../timeout'; +import { type Connection } from '../../connection'; +import { type MongoCredentials } from '../mongo_credentials'; +import { + OIDC_VERSION, + type OIDCCallbackFunction, + type OIDCCallbackParams, + type OIDCResponse +} from '../mongodb_oidc'; +import { AUTOMATED_TIMEOUT_MS, CallbackWorkflow } from './callback_workflow'; +import { type TokenCache } from './token_cache'; + +/** + * Class implementing behaviour for the non human callback workflow. + * @internal + */ +export class AutomatedCallbackWorkflow extends CallbackWorkflow { + /** + * Instantiate the human callback workflow. + */ + constructor(cache: TokenCache, callback: OIDCCallbackFunction) { + super(cache, callback); + } + + /** + * Execute the OIDC callback workflow. + */ + async execute(connection: Connection, credentials: MongoCredentials): Promise { + // If there is a cached access token, try to authenticate with it. If + // authentication fails with an Authentication error (18), + // invalidate the access token, fetch a new access token, and try + // to authenticate again. + // If the server fails for any other reason, do not clear the cache. + if (this.cache.hasAccessToken) { + const token = this.cache.getAccessToken(); + if (!connection.accessToken) { + connection.accessToken = token; + } + try { + return await this.finishAuthentication(connection, credentials, token); + } catch (error) { + if ( + error instanceof MongoError && + error.code === MONGODB_ERROR_CODES.AuthenticationFailed + ) { + this.cache.removeAccessToken(); + return await this.execute(connection, credentials); + } else { + throw error; + } + } + } + const response = await this.fetchAccessToken(credentials); + this.cache.put(response); + connection.accessToken = response.accessToken; + await this.finishAuthentication(connection, credentials, response.accessToken); + } + + /** + * Fetches the access token using the callback. + */ + protected async fetchAccessToken(credentials: MongoCredentials): Promise { + const controller = new AbortController(); + const params: OIDCCallbackParams = { + timeoutContext: controller.signal, + version: OIDC_VERSION + }; + if (credentials.username) { + params.username = credentials.username; + } + if (credentials.mechanismProperties.TOKEN_RESOURCE) { + params.tokenAudience = credentials.mechanismProperties.TOKEN_RESOURCE; + } + const timeout = Timeout.expires(AUTOMATED_TIMEOUT_MS); + try { + return await Promise.race([this.executeAndValidateCallback(params), timeout]); + } catch (error) { + if (TimeoutError.is(error)) { + controller.abort(); + throw new MongoOIDCError(`OIDC callback timed out after ${AUTOMATED_TIMEOUT_MS}ms.`); + } + throw error; + } finally { + timeout.clear(); + } + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/azure_machine_workflow.ts b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/azure_machine_workflow.ts new file mode 100644 index 0000000000000000000000000000000000000000..5b3cbde1ddd981e231e7a54657adc8807541194b --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/azure_machine_workflow.ts @@ -0,0 +1,73 @@ +import { addAzureParams, AZURE_BASE_URL } from '../../../client-side-encryption/providers/azure'; +import { MongoAzureError } from '../../../error'; +import { get } from '../../../utils'; +import type { OIDCCallbackFunction, OIDCCallbackParams, OIDCResponse } from '../mongodb_oidc'; + +/** Azure request headers. */ +const AZURE_HEADERS = Object.freeze({ Metadata: 'true', Accept: 'application/json' }); + +/** Invalid endpoint result error. */ +const ENDPOINT_RESULT_ERROR = + 'Azure endpoint did not return a value with only access_token and expires_in properties'; + +/** Error for when the token audience is missing in the environment. */ +const TOKEN_RESOURCE_MISSING_ERROR = + 'TOKEN_RESOURCE must be set in the auth mechanism properties when ENVIRONMENT is azure.'; + +/** + * The callback function to be used in the automated callback workflow. + * @param params - The OIDC callback parameters. + * @returns The OIDC response. + */ +export const azureCallback: OIDCCallbackFunction = async ( + params: OIDCCallbackParams +): Promise => { + const tokenAudience = params.tokenAudience; + const username = params.username; + if (!tokenAudience) { + throw new MongoAzureError(TOKEN_RESOURCE_MISSING_ERROR); + } + const response = await getAzureTokenData(tokenAudience, username); + if (!isEndpointResultValid(response)) { + throw new MongoAzureError(ENDPOINT_RESULT_ERROR); + } + return response; +}; + +/** + * Hit the Azure endpoint to get the token data. + */ +async function getAzureTokenData(tokenAudience: string, username?: string): Promise { + const url = new URL(AZURE_BASE_URL); + addAzureParams(url, tokenAudience, username); + const response = await get(url, { + headers: AZURE_HEADERS + }); + if (response.status !== 200) { + throw new MongoAzureError( + `Status code ${response.status} returned from the Azure endpoint. Response body: ${response.body}` + ); + } + const result = JSON.parse(response.body); + return { + accessToken: result.access_token, + expiresInSeconds: Number(result.expires_in) + }; +} + +/** + * Determines if a result returned from the endpoint is valid. + * This means the result is not nullish, contains the access_token required field + * and the expires_in required field. + */ +function isEndpointResultValid( + token: unknown +): token is { access_token: unknown; expires_in: unknown } { + if (token == null || typeof token !== 'object') return false; + return ( + 'accessToken' in token && + typeof token.accessToken === 'string' && + 'expiresInSeconds' in token && + typeof token.expiresInSeconds === 'number' + ); +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/callback_workflow.ts b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/callback_workflow.ts new file mode 100644 index 0000000000000000000000000000000000000000..afa1b96c78d1c97ce541e98f34d945c9e1732971 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/callback_workflow.ts @@ -0,0 +1,188 @@ +import { setTimeout } from 'timers/promises'; + +import { type Document } from '../../../bson'; +import { MongoMissingCredentialsError } from '../../../error'; +import { ns } from '../../../utils'; +import type { Connection } from '../../connection'; +import type { MongoCredentials } from '../mongo_credentials'; +import { + type OIDCCallbackFunction, + type OIDCCallbackParams, + type OIDCResponse, + type Workflow +} from '../mongodb_oidc'; +import { finishCommandDocument, startCommandDocument } from './command_builders'; +import { type TokenCache } from './token_cache'; + +/** 5 minutes in milliseconds */ +export const HUMAN_TIMEOUT_MS = 300000; +/** 1 minute in milliseconds */ +export const AUTOMATED_TIMEOUT_MS = 60000; + +/** Properties allowed on results of callbacks. */ +const RESULT_PROPERTIES = ['accessToken', 'expiresInSeconds', 'refreshToken']; + +/** Error message when the callback result is invalid. */ +const CALLBACK_RESULT_ERROR = + 'User provided OIDC callbacks must return a valid object with an accessToken.'; + +/** The time to throttle callback calls. */ +const THROTTLE_MS = 100; + +/** + * OIDC implementation of a callback based workflow. + * @internal + */ +export abstract class CallbackWorkflow implements Workflow { + cache: TokenCache; + callback: OIDCCallbackFunction; + lastExecutionTime: number; + + /** + * Instantiate the callback workflow. + */ + constructor(cache: TokenCache, callback: OIDCCallbackFunction) { + this.cache = cache; + this.callback = this.withLock(callback); + this.lastExecutionTime = Date.now() - THROTTLE_MS; + } + + /** + * Get the document to add for speculative authentication. This also needs + * to add a db field from the credentials source. + */ + async speculativeAuth(connection: Connection, credentials: MongoCredentials): Promise { + // Check if the Client Cache has an access token. + // If it does, cache the access token in the Connection Cache and send a JwtStepRequest + // with the cached access token in the speculative authentication SASL payload. + if (this.cache.hasAccessToken) { + const accessToken = this.cache.getAccessToken(); + connection.accessToken = accessToken; + const document = finishCommandDocument(accessToken); + document.db = credentials.source; + return { speculativeAuthenticate: document }; + } + return {}; + } + + /** + * Reauthenticate the callback workflow. For this we invalidated the access token + * in the cache and run the authentication steps again. No initial handshake needs + * to be sent. + */ + async reauthenticate(connection: Connection, credentials: MongoCredentials): Promise { + if (this.cache.hasAccessToken) { + // Reauthentication implies the token has expired. + if (connection.accessToken === this.cache.getAccessToken()) { + // If connection's access token is the same as the cache's, remove + // the token from the cache and connection. + this.cache.removeAccessToken(); + delete connection.accessToken; + } else { + // If the connection's access token is different from the cache's, set + // the cache's token on the connection and do not remove from the + // cache. + connection.accessToken = this.cache.getAccessToken(); + } + } + await this.execute(connection, credentials); + } + + /** + * Execute the OIDC callback workflow. + */ + abstract execute( + connection: Connection, + credentials: MongoCredentials, + response?: Document + ): Promise; + + /** + * Starts the callback authentication process. If there is a speculative + * authentication document from the initial handshake, then we will use that + * value to get the issuer, otherwise we will send the saslStart command. + */ + protected async startAuthentication( + connection: Connection, + credentials: MongoCredentials, + response?: Document + ): Promise { + let result; + if (response?.speculativeAuthenticate) { + result = response.speculativeAuthenticate; + } else { + result = await connection.command( + ns(credentials.source), + startCommandDocument(credentials), + undefined + ); + } + return result; + } + + /** + * Finishes the callback authentication process. + */ + protected async finishAuthentication( + connection: Connection, + credentials: MongoCredentials, + token: string, + conversationId?: number + ): Promise { + await connection.command( + ns(credentials.source), + finishCommandDocument(token, conversationId), + undefined + ); + } + + /** + * Executes the callback and validates the output. + */ + protected async executeAndValidateCallback(params: OIDCCallbackParams): Promise { + const result = await this.callback(params); + // Validate that the result returned by the callback is acceptable. If it is not + // we must clear the token result from the cache. + if (isCallbackResultInvalid(result)) { + throw new MongoMissingCredentialsError(CALLBACK_RESULT_ERROR); + } + return result; + } + + /** + * Ensure the callback is only executed one at a time and throttles the calls + * to every 100ms. + */ + protected withLock(callback: OIDCCallbackFunction): OIDCCallbackFunction { + let lock: Promise = Promise.resolve(); + return async (params: OIDCCallbackParams): Promise => { + // We do this to ensure that we would never return the result of the + // previous lock, only the current callback's value would get returned. + await lock; + lock = lock + + .catch(() => null) + + .then(async () => { + const difference = Date.now() - this.lastExecutionTime; + if (difference <= THROTTLE_MS) { + await setTimeout(THROTTLE_MS - difference, { signal: params.timeoutContext }); + } + this.lastExecutionTime = Date.now(); + return await callback(params); + }); + return await lock; + }; + } +} + +/** + * Determines if a result returned from a request or refresh callback + * function is invalid. This means the result is nullish, doesn't contain + * the accessToken required field, and does not contain extra fields. + */ +function isCallbackResultInvalid(tokenResult: unknown): boolean { + if (tokenResult == null || typeof tokenResult !== 'object') return true; + if (!('accessToken' in tokenResult)) return true; + return !Object.getOwnPropertyNames(tokenResult).every(prop => RESULT_PROPERTIES.includes(prop)); +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/command_builders.ts b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/command_builders.ts new file mode 100644 index 0000000000000000000000000000000000000000..da0c09c4c9193932fcc374d38c18df66f8f8b4d3 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/command_builders.ts @@ -0,0 +1,53 @@ +import { Binary, BSON, type Document } from '../../../bson'; +import { type MongoCredentials } from '../mongo_credentials'; +import { AuthMechanism } from '../providers'; + +/** @internal */ +export interface OIDCCommand { + saslStart?: number; + saslContinue?: number; + conversationId?: number; + mechanism?: string; + autoAuthorize?: number; + db?: string; + payload: Binary; +} + +/** + * Generate the finishing command document for authentication. Will be a + * saslStart or saslContinue depending on the presence of a conversation id. + */ +export function finishCommandDocument(token: string, conversationId?: number): OIDCCommand { + if (conversationId != null) { + return { + saslContinue: 1, + conversationId: conversationId, + payload: new Binary(BSON.serialize({ jwt: token })) + }; + } + // saslContinue requires a conversationId in the command to be valid so in this + // case the server allows "step two" to actually be a saslStart with the token + // as the jwt since the use of the cached value has no correlating conversating + // on the particular connection. + return { + saslStart: 1, + mechanism: AuthMechanism.MONGODB_OIDC, + payload: new Binary(BSON.serialize({ jwt: token })) + }; +} + +/** + * Generate the saslStart command document. + */ +export function startCommandDocument(credentials: MongoCredentials): OIDCCommand { + const payload: Document = {}; + if (credentials.username) { + payload.n = credentials.username; + } + return { + saslStart: 1, + autoAuthorize: 1, + mechanism: AuthMechanism.MONGODB_OIDC, + payload: new Binary(BSON.serialize(payload)) + }; +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/gcp_machine_workflow.ts b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/gcp_machine_workflow.ts new file mode 100644 index 0000000000000000000000000000000000000000..f72655e4017575bea22e6ea6d74991a78b57180e --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/gcp_machine_workflow.ts @@ -0,0 +1,46 @@ +import { MongoGCPError } from '../../../error'; +import { get } from '../../../utils'; +import type { OIDCCallbackFunction, OIDCCallbackParams, OIDCResponse } from '../mongodb_oidc'; + +/** GCP base URL. */ +const GCP_BASE_URL = + 'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity'; + +/** GCP request headers. */ +const GCP_HEADERS = Object.freeze({ 'Metadata-Flavor': 'Google' }); + +/** Error for when the token audience is missing in the environment. */ +const TOKEN_RESOURCE_MISSING_ERROR = + 'TOKEN_RESOURCE must be set in the auth mechanism properties when ENVIRONMENT is gcp.'; + +/** + * The callback function to be used in the automated callback workflow. + * @param params - The OIDC callback parameters. + * @returns The OIDC response. + */ +export const gcpCallback: OIDCCallbackFunction = async ( + params: OIDCCallbackParams +): Promise => { + const tokenAudience = params.tokenAudience; + if (!tokenAudience) { + throw new MongoGCPError(TOKEN_RESOURCE_MISSING_ERROR); + } + return await getGcpTokenData(tokenAudience); +}; + +/** + * Hit the GCP endpoint to get the token data. + */ +async function getGcpTokenData(tokenAudience: string): Promise { + const url = new URL(GCP_BASE_URL); + url.searchParams.append('audience', tokenAudience); + const response = await get(url, { + headers: GCP_HEADERS + }); + if (response.status !== 200) { + throw new MongoGCPError( + `Status code ${response.status} returned from the GCP endpoint. Response body: ${response.body}` + ); + } + return { accessToken: response.body }; +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/human_callback_workflow.ts b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/human_callback_workflow.ts new file mode 100644 index 0000000000000000000000000000000000000000..a162ce06f7a354e9f97c2de384fd91a6dcb50207 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/human_callback_workflow.ts @@ -0,0 +1,141 @@ +import { BSON } from '../../../bson'; +import { MONGODB_ERROR_CODES, MongoError, MongoOIDCError } from '../../../error'; +import { Timeout, TimeoutError } from '../../../timeout'; +import { type Connection } from '../../connection'; +import { type MongoCredentials } from '../mongo_credentials'; +import { + type IdPInfo, + OIDC_VERSION, + type OIDCCallbackFunction, + type OIDCCallbackParams, + type OIDCResponse +} from '../mongodb_oidc'; +import { CallbackWorkflow, HUMAN_TIMEOUT_MS } from './callback_workflow'; +import { type TokenCache } from './token_cache'; + +/** + * Class implementing behaviour for the non human callback workflow. + * @internal + */ +export class HumanCallbackWorkflow extends CallbackWorkflow { + /** + * Instantiate the human callback workflow. + */ + constructor(cache: TokenCache, callback: OIDCCallbackFunction) { + super(cache, callback); + } + + /** + * Execute the OIDC human callback workflow. + */ + async execute(connection: Connection, credentials: MongoCredentials): Promise { + // Check if the Client Cache has an access token. + // If it does, cache the access token in the Connection Cache and perform a One-Step SASL conversation + // using the access token. If the server returns an Authentication error (18), + // invalidate the access token token from the Client Cache, clear the Connection Cache, + // and restart the authentication flow. Raise any other errors to the user. On success, exit the algorithm. + if (this.cache.hasAccessToken) { + const token = this.cache.getAccessToken(); + connection.accessToken = token; + try { + return await this.finishAuthentication(connection, credentials, token); + } catch (error) { + if ( + error instanceof MongoError && + error.code === MONGODB_ERROR_CODES.AuthenticationFailed + ) { + this.cache.removeAccessToken(); + delete connection.accessToken; + return await this.execute(connection, credentials); + } else { + throw error; + } + } + } + // Check if the Client Cache has a refresh token. + // If it does, call the OIDC Human Callback with the cached refresh token and IdpInfo to get a + // new access token. Cache the new access token in the Client Cache and Connection Cache. + // Perform a One-Step SASL conversation using the new access token. If the the server returns + // an Authentication error (18), clear the refresh token, invalidate the access token from the + // Client Cache, clear the Connection Cache, and restart the authentication flow. Raise any other + // errors to the user. On success, exit the algorithm. + if (this.cache.hasRefreshToken) { + const refreshToken = this.cache.getRefreshToken(); + const result = await this.fetchAccessToken( + this.cache.getIdpInfo(), + credentials, + refreshToken + ); + this.cache.put(result); + connection.accessToken = result.accessToken; + try { + return await this.finishAuthentication(connection, credentials, result.accessToken); + } catch (error) { + if ( + error instanceof MongoError && + error.code === MONGODB_ERROR_CODES.AuthenticationFailed + ) { + this.cache.removeRefreshToken(); + delete connection.accessToken; + return await this.execute(connection, credentials); + } else { + throw error; + } + } + } + + // Start a new Two-Step SASL conversation. + // Run a PrincipalStepRequest to get the IdpInfo. + // Call the OIDC Human Callback with the new IdpInfo to get a new access token and optional refresh + // token. Drivers MUST NOT pass a cached refresh token to the callback when performing + // a new Two-Step conversation. Cache the new IdpInfo and refresh token in the Client Cache and the + // new access token in the Client Cache and Connection Cache. + // Attempt to authenticate using a JwtStepRequest with the new access token. Raise any errors to the user. + const startResponse = await this.startAuthentication(connection, credentials); + const conversationId = startResponse.conversationId; + const idpInfo = BSON.deserialize(startResponse.payload.buffer) as IdPInfo; + const callbackResponse = await this.fetchAccessToken(idpInfo, credentials); + this.cache.put(callbackResponse, idpInfo); + connection.accessToken = callbackResponse.accessToken; + return await this.finishAuthentication( + connection, + credentials, + callbackResponse.accessToken, + conversationId + ); + } + + /** + * Fetches an access token using the callback. + */ + private async fetchAccessToken( + idpInfo: IdPInfo, + credentials: MongoCredentials, + refreshToken?: string + ): Promise { + const controller = new AbortController(); + const params: OIDCCallbackParams = { + timeoutContext: controller.signal, + version: OIDC_VERSION, + idpInfo: idpInfo + }; + if (credentials.username) { + params.username = credentials.username; + } + if (refreshToken) { + params.refreshToken = refreshToken; + } + const timeout = Timeout.expires(HUMAN_TIMEOUT_MS); + try { + return await Promise.race([this.executeAndValidateCallback(params), timeout]); + } catch (error) { + if (TimeoutError.is(error)) { + controller.abort(); + throw new MongoOIDCError(`OIDC callback timed out after ${HUMAN_TIMEOUT_MS}ms.`); + } + throw error; + } finally { + timeout.clear(); + } + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/k8s_machine_workflow.ts b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/k8s_machine_workflow.ts new file mode 100644 index 0000000000000000000000000000000000000000..95b83f029b04d000e37e9d7cccf91aa5e39bde3e --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/k8s_machine_workflow.ts @@ -0,0 +1,31 @@ +import { readFile } from 'fs/promises'; +import * as process from 'process'; + +import type { OIDCCallbackFunction, OIDCResponse } from '../mongodb_oidc'; + +/** The fallback file name */ +const FALLBACK_FILENAME = '/var/run/secrets/kubernetes.io/serviceaccount/token'; + +/** The azure environment variable for the file name. */ +const AZURE_FILENAME = 'AZURE_FEDERATED_TOKEN_FILE'; + +/** The AWS environment variable for the file name. */ +const AWS_FILENAME = 'AWS_WEB_IDENTITY_TOKEN_FILE'; + +/** + * The callback function to be used in the automated callback workflow. + * @param params - The OIDC callback parameters. + * @returns The OIDC response. + */ +export const k8sCallback: OIDCCallbackFunction = async (): Promise => { + let filename: string; + if (process.env[AZURE_FILENAME]) { + filename = process.env[AZURE_FILENAME]; + } else if (process.env[AWS_FILENAME]) { + filename = process.env[AWS_FILENAME]; + } else { + filename = FALLBACK_FILENAME; + } + const token = await readFile(filename, 'utf8'); + return { accessToken: token }; +}; diff --git a/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/token_cache.ts b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/token_cache.ts new file mode 100644 index 0000000000000000000000000000000000000000..e2f7ad4954b936d769594ba3ff8d7203e7f4c576 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/token_cache.ts @@ -0,0 +1,62 @@ +import { MongoDriverError } from '../../../error'; +import type { IdPInfo, OIDCResponse } from '../mongodb_oidc'; + +class MongoOIDCError extends MongoDriverError {} + +/** @internal */ +export class TokenCache { + private accessToken?: string; + private refreshToken?: string; + private idpInfo?: IdPInfo; + private expiresInSeconds?: number; + + get hasAccessToken(): boolean { + return !!this.accessToken; + } + + get hasRefreshToken(): boolean { + return !!this.refreshToken; + } + + get hasIdpInfo(): boolean { + return !!this.idpInfo; + } + + getAccessToken(): string { + if (!this.accessToken) { + throw new MongoOIDCError('Attempted to get an access token when none exists.'); + } + return this.accessToken; + } + + getRefreshToken(): string { + if (!this.refreshToken) { + throw new MongoOIDCError('Attempted to get a refresh token when none exists.'); + } + return this.refreshToken; + } + + getIdpInfo(): IdPInfo { + if (!this.idpInfo) { + throw new MongoOIDCError('Attempted to get IDP information when none exists.'); + } + return this.idpInfo; + } + + put(response: OIDCResponse, idpInfo?: IdPInfo) { + this.accessToken = response.accessToken; + this.refreshToken = response.refreshToken; + this.expiresInSeconds = response.expiresInSeconds; + if (idpInfo) { + this.idpInfo = idpInfo; + } + } + + removeAccessToken() { + this.accessToken = undefined; + } + + removeRefreshToken() { + this.refreshToken = undefined; + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/token_machine_workflow.ts b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/token_machine_workflow.ts new file mode 100644 index 0000000000000000000000000000000000000000..4362b57b0bc0eba34e735bd56fd299fc1d78ce43 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/mongodb_oidc/token_machine_workflow.ts @@ -0,0 +1,22 @@ +import * as fs from 'fs'; +import * as process from 'process'; + +import { MongoAWSError } from '../../../error'; +import type { OIDCCallbackFunction, OIDCResponse } from '../mongodb_oidc'; + +/** Error for when the token is missing in the environment. */ +const TOKEN_MISSING_ERROR = 'OIDC_TOKEN_FILE must be set in the environment.'; + +/** + * The callback function to be used in the automated callback workflow. + * @param params - The OIDC callback parameters. + * @returns The OIDC response. + */ +export const tokenMachineCallback: OIDCCallbackFunction = async (): Promise => { + const tokenFile = process.env.OIDC_TOKEN_FILE; + if (!tokenFile) { + throw new MongoAWSError(TOKEN_MISSING_ERROR); + } + const token = await fs.promises.readFile(tokenFile, 'utf8'); + return { accessToken: token }; +}; diff --git a/gateway/node_modules/mongodb/src/cmap/auth/plain.ts b/gateway/node_modules/mongodb/src/cmap/auth/plain.ts new file mode 100644 index 0000000000000000000000000000000000000000..00cb711d38f5231136360dc15f05d474ebbc5e25 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/plain.ts @@ -0,0 +1,25 @@ +import { Binary, ByteUtils } from '../../bson'; +import { MongoMissingCredentialsError } from '../../error'; +import { ns } from '../../utils'; +import { type AuthContext, AuthProvider } from './auth_provider'; + +export class Plain extends AuthProvider { + override async auth(authContext: AuthContext): Promise { + const { connection, credentials } = authContext; + if (!credentials) { + throw new MongoMissingCredentialsError('AuthContext must provide credentials.'); + } + + const { username, password } = credentials; + + const payload = new Binary(ByteUtils.fromUTF8(`\x00${username}\x00${password}`)); + const command = { + saslStart: 1, + mechanism: 'PLAIN', + payload: payload, + autoAuthorize: 1 + }; + + await connection.command(ns('$external.$cmd'), command, undefined); + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/providers.ts b/gateway/node_modules/mongodb/src/cmap/auth/providers.ts new file mode 100644 index 0000000000000000000000000000000000000000..fe6a57cef376205ccd27f4ec2121efc4e4fc01e5 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/providers.ts @@ -0,0 +1,22 @@ +/** @public */ +export const AuthMechanism = Object.freeze({ + MONGODB_AWS: 'MONGODB-AWS', + MONGODB_DEFAULT: 'DEFAULT', + MONGODB_GSSAPI: 'GSSAPI', + MONGODB_PLAIN: 'PLAIN', + MONGODB_SCRAM_SHA1: 'SCRAM-SHA-1', + MONGODB_SCRAM_SHA256: 'SCRAM-SHA-256', + MONGODB_X509: 'MONGODB-X509', + MONGODB_OIDC: 'MONGODB-OIDC' +} as const); + +/** @public */ +export type AuthMechanism = (typeof AuthMechanism)[keyof typeof AuthMechanism]; + +/** @internal */ +export const AUTH_MECHS_AUTH_SRC_EXTERNAL = new Set([ + AuthMechanism.MONGODB_GSSAPI, + AuthMechanism.MONGODB_AWS, + AuthMechanism.MONGODB_OIDC, + AuthMechanism.MONGODB_X509 +]); diff --git a/gateway/node_modules/mongodb/src/cmap/auth/scram.ts b/gateway/node_modules/mongodb/src/cmap/auth/scram.ts new file mode 100644 index 0000000000000000000000000000000000000000..675571ee1a86b25d026a839e0f1e83e65b682e62 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/scram.ts @@ -0,0 +1,370 @@ +import { saslprep } from '@mongodb-js/saslprep'; + +import { Binary, ByteUtils, type Document } from '../../bson'; +import { + MongoInvalidArgumentError, + MongoMissingCredentialsError, + MongoRuntimeError +} from '../../error'; +import { ns, randomBytes } from '../../utils'; +import type { HandshakeDocument } from '../connect'; +import { type AuthContext, AuthProvider } from './auth_provider'; +import type { MongoCredentials } from './mongo_credentials'; +import { AuthMechanism } from './providers'; + +type CryptoMethod = 'sha1' | 'sha256'; + +class ScramSHA extends AuthProvider { + cryptoMethod: CryptoMethod; + + constructor(cryptoMethod: CryptoMethod) { + super(); + this.cryptoMethod = cryptoMethod || 'sha1'; + } + + override async prepare( + handshakeDoc: HandshakeDocument, + authContext: AuthContext + ): Promise { + const cryptoMethod = this.cryptoMethod; + const credentials = authContext.credentials; + if (!credentials) { + throw new MongoMissingCredentialsError('AuthContext must provide credentials.'); + } + + const nonce = await randomBytes(24); + // store the nonce for later use + authContext.nonce = nonce; + + const request = { + ...handshakeDoc, + speculativeAuthenticate: { + ...makeFirstMessage(cryptoMethod, credentials, nonce), + db: credentials.source + } + }; + + return request; + } + + override async auth(authContext: AuthContext) { + const { reauthenticating, response } = authContext; + if (response?.speculativeAuthenticate && !reauthenticating) { + return await continueScramConversation( + this.cryptoMethod, + response.speculativeAuthenticate, + authContext + ); + } + return await executeScram(this.cryptoMethod, authContext); + } +} + +function cleanUsername(username: string) { + return username.replace('=', '=3D').replace(',', '=2C'); +} + +function clientFirstMessageBare(username: string, nonce: Uint8Array) { + // NOTE: This is done b/c Javascript uses UTF-16, but the server is hashing in UTF-8. + // Since the username is not sasl-prep-d, we need to do this here. + return ByteUtils.concat([ + ByteUtils.fromUTF8('n='), + ByteUtils.fromUTF8(username), + ByteUtils.fromUTF8(',r='), + ByteUtils.fromUTF8(ByteUtils.toBase64(nonce)) + ]); +} + +function makeFirstMessage( + cryptoMethod: CryptoMethod, + credentials: MongoCredentials, + nonce: Uint8Array +) { + const username = cleanUsername(credentials.username); + const mechanism = + cryptoMethod === 'sha1' ? AuthMechanism.MONGODB_SCRAM_SHA1 : AuthMechanism.MONGODB_SCRAM_SHA256; + + // NOTE: This is done b/c Javascript uses UTF-16, but the server is hashing in UTF-8. + // Since the username is not sasl-prep-d, we need to do this here. + return { + saslStart: 1, + mechanism, + payload: new Binary( + ByteUtils.concat([ByteUtils.fromUTF8('n,,'), clientFirstMessageBare(username, nonce)]) + ), + autoAuthorize: 1, + options: { skipEmptyExchange: true } + }; +} + +async function executeScram(cryptoMethod: CryptoMethod, authContext: AuthContext): Promise { + const { connection, credentials } = authContext; + if (!credentials) { + throw new MongoMissingCredentialsError('AuthContext must provide credentials.'); + } + if (!authContext.nonce) { + throw new MongoInvalidArgumentError('AuthContext must contain a valid nonce property'); + } + const nonce = authContext.nonce; + const db = credentials.source; + + const saslStartCmd = makeFirstMessage(cryptoMethod, credentials, nonce); + const response = await connection.command(ns(`${db}.$cmd`), saslStartCmd, undefined); + await continueScramConversation(cryptoMethod, response, authContext); +} + +async function continueScramConversation( + cryptoMethod: CryptoMethod, + response: Document, + authContext: AuthContext +): Promise { + const connection = authContext.connection; + const credentials = authContext.credentials; + if (!credentials) { + throw new MongoMissingCredentialsError('AuthContext must provide credentials.'); + } + if (!authContext.nonce) { + throw new MongoInvalidArgumentError('Unable to continue SCRAM without valid nonce'); + } + const nonce = authContext.nonce; + + const db = credentials.source; + const username = cleanUsername(credentials.username); + const password = credentials.password; + + const processedPassword = + cryptoMethod === 'sha256' ? saslprep(password) : passwordDigest(username, password); + + const payload: Binary = ByteUtils.isUint8Array(response.payload) + ? new Binary(response.payload) + : response.payload; + + const dict = parsePayload(payload); + + const iterations = parseInt(dict.i, 10); + if (iterations && iterations < 4096) { + // TODO(NODE-3483) + throw new MongoRuntimeError(`Server returned an invalid iteration count ${iterations}`); + } + + const salt = dict.s; + const rnonce = dict.r; + if (rnonce.startsWith('nonce')) { + // TODO(NODE-3483) + throw new MongoRuntimeError(`Server returned an invalid nonce: ${rnonce}`); + } + + // Set up start of proof + const withoutProof = `c=biws,r=${rnonce}`; + const saltedPassword = await HI( + processedPassword, + ByteUtils.fromBase64(salt), + iterations, + cryptoMethod + ); + + const clientKey = await HMAC(cryptoMethod, saltedPassword, 'Client Key'); + const serverKey = await HMAC(cryptoMethod, saltedPassword, 'Server Key'); + const storedKey = await H(cryptoMethod, clientKey); + const firstMessageBytes = clientFirstMessageBare(username, nonce); + const firstMessage = ByteUtils.toUTF8(firstMessageBytes, 0, firstMessageBytes.length, false); + const payloadString = ByteUtils.toUTF8(payload.buffer, 0, payload.position, false); + const authMessage = [firstMessage, payloadString, withoutProof].join(','); + + const clientSignature = await HMAC(cryptoMethod, storedKey, authMessage); + const clientProof = `p=${xor(clientKey, clientSignature)}`; + const clientFinal = [withoutProof, clientProof].join(','); + + const serverSignature = await HMAC(cryptoMethod, serverKey, authMessage); + const saslContinueCmd = { + saslContinue: 1, + conversationId: response.conversationId, + payload: new Binary(ByteUtils.fromUTF8(clientFinal)) + }; + + const r = await connection.command(ns(`${db}.$cmd`), saslContinueCmd, undefined); + const parsedResponse = parsePayload(r.payload); + + if (!compareDigest(ByteUtils.fromBase64(parsedResponse.v), serverSignature)) { + throw new MongoRuntimeError('Server returned an invalid signature'); + } + + if (r.done !== false) { + // If the server sends r.done === true we can save one RTT + return; + } + + const retrySaslContinueCmd = { + saslContinue: 1, + conversationId: r.conversationId, + payload: ByteUtils.allocate(0) + }; + + await connection.command(ns(`${db}.$cmd`), retrySaslContinueCmd, undefined); +} + +function parsePayload(payload: Binary) { + const payloadStr = ByteUtils.toUTF8(payload.buffer, 0, payload.position, false); + const dict: Document = {}; + const parts = payloadStr.split(','); + for (let i = 0; i < parts.length; i++) { + const valueParts = (parts[i].match(/^([^=]*)=(.*)$/) ?? []).slice(1); + dict[valueParts[0]] = valueParts[1]; + } + return dict; +} + +function passwordDigest(username: string, password: string) { + if (typeof username !== 'string') { + throw new MongoInvalidArgumentError('Username must be a string'); + } + + if (typeof password !== 'string') { + throw new MongoInvalidArgumentError('Password must be a string'); + } + + if (password.length === 0) { + throw new MongoInvalidArgumentError('Password cannot be empty'); + } + + let nodeCrypto; + try { + // TODO: NODE-7424 - remove dependency on 'crypto' for SCRAM-SHA-1 authentication + // eslint-disable-next-line @typescript-eslint/no-require-imports + nodeCrypto = require('crypto'); + } catch (e) { + throw new MongoRuntimeError( + 'Node.js crypto module is required for SCRAM-SHA-1 authentication', + { + cause: e + } + ); + } + + try { + const md5 = nodeCrypto.createHash('md5'); + md5.update(`${username}:mongo:${password}`, 'utf8'); + return md5.digest('hex'); + } catch (err) { + if (nodeCrypto.getFips()) { + // This error is (slightly) more helpful than what comes from OpenSSL directly, e.g. + // 'Error: error:060800C8:digital envelope routines:EVP_DigestInit_ex:disabled for FIPS' + throw new Error('Auth mechanism SCRAM-SHA-1 is not supported in FIPS mode'); + } + throw err; + } +} + +// XOR two buffers +function xor(a: Uint8Array, b: Uint8Array) { + const length = Math.max(a.length, b.length); + const res = []; + + for (let i = 0; i < length; i += 1) { + res.push(a[i] ^ b[i]); + } + + return ByteUtils.toBase64(ByteUtils.fromNumberArray(res)); +} + +async function H(method: CryptoMethod, text: Uint8Array): Promise { + const buffer = await crypto.subtle.digest(method === 'sha256' ? 'SHA-256' : 'SHA-1', text); + return new Uint8Array(buffer); +} + +async function HMAC( + method: CryptoMethod, + key: Uint8Array, + text: Uint8Array | string +): Promise { + const keyBuffer = ByteUtils.toLocalBufferType(key); + const cryptoKey = await crypto.subtle.importKey( + 'raw', + keyBuffer, + { name: 'HMAC', hash: { name: method === 'sha256' ? 'SHA-256' : 'SHA-1' } }, + false, + ['sign', 'verify'] + ); + const textData: Uint8Array = typeof text === 'string' ? new TextEncoder().encode(text) : text; + const textBuffer = ByteUtils.toLocalBufferType(textData); + const signature = await crypto.subtle.sign('HMAC', cryptoKey, textBuffer); + return new Uint8Array(signature); +} + +interface HICache { + [key: string]: Uint8Array; +} + +let _hiCache: HICache = {}; +let _hiCacheCount = 0; +function _hiCachePurge() { + _hiCache = {}; + _hiCacheCount = 0; +} + +const hiLengthMap = { + sha256: 32, + sha1: 20 +}; + +async function HI(data: string, salt: Uint8Array, iterations: number, cryptoMethod: CryptoMethod) { + // omit the work if already generated + const key = [data, ByteUtils.toBase64(salt), iterations].join('_'); + if (_hiCache[key] != null) { + return _hiCache[key]; + } + + const keyMaterial = await crypto.subtle.importKey( + 'raw', + new TextEncoder().encode(data), + { name: 'PBKDF2' }, + false, + ['deriveBits'] + ); + const params = { + name: 'PBKDF2', + salt: salt, + iterations: iterations, + hash: { name: cryptoMethod === 'sha256' ? 'SHA-256' : 'SHA-1' } + }; + const derivedBits = await crypto.subtle.deriveBits( + params, + keyMaterial, + hiLengthMap[cryptoMethod] * 8 + ); + const saltedData = new Uint8Array(derivedBits); + + // cache a copy to speed up the next lookup, but prevent unbounded cache growth + if (_hiCacheCount >= 200) { + _hiCachePurge(); + } + + _hiCache[key] = saltedData; + _hiCacheCount += 1; + return saltedData; +} + +function compareDigest(lhs: Uint8Array, rhs: Uint8Array) { + if (lhs.length !== rhs.length) { + return false; + } + + let result = 0; + for (let i = 0; i < lhs.length; i++) { + result |= lhs[i] ^ rhs[i]; + } + + return result === 0; +} + +export class ScramSHA1 extends ScramSHA { + constructor() { + super('sha1'); + } +} + +export class ScramSHA256 extends ScramSHA { + constructor() { + super('sha256'); + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/auth/x509.ts b/gateway/node_modules/mongodb/src/cmap/auth/x509.ts new file mode 100644 index 0000000000000000000000000000000000000000..ec4c0007900eaa1595bb615b6a8579cbcfd83cdb --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/auth/x509.ts @@ -0,0 +1,43 @@ +import type { Document } from '../../bson'; +import { MongoMissingCredentialsError } from '../../error'; +import { ns } from '../../utils'; +import type { HandshakeDocument } from '../connect'; +import { type AuthContext, AuthProvider } from './auth_provider'; +import type { MongoCredentials } from './mongo_credentials'; + +export class X509 extends AuthProvider { + override async prepare( + handshakeDoc: HandshakeDocument, + authContext: AuthContext + ): Promise { + const { credentials } = authContext; + if (!credentials) { + throw new MongoMissingCredentialsError('AuthContext must provide credentials.'); + } + return { ...handshakeDoc, speculativeAuthenticate: x509AuthenticateCommand(credentials) }; + } + + override async auth(authContext: AuthContext) { + const connection = authContext.connection; + const credentials = authContext.credentials; + if (!credentials) { + throw new MongoMissingCredentialsError('AuthContext must provide credentials.'); + } + const response = authContext.response; + + if (response?.speculativeAuthenticate) { + return; + } + + await connection.command(ns('$external.$cmd'), x509AuthenticateCommand(credentials), undefined); + } +} + +function x509AuthenticateCommand(credentials: MongoCredentials) { + const command: Document = { authenticate: 1, mechanism: 'MONGODB-X509' }; + if (credentials.username) { + command.user = credentials.username; + } + + return command; +} diff --git a/gateway/node_modules/mongodb/src/cmap/command_monitoring_events.ts b/gateway/node_modules/mongodb/src/cmap/command_monitoring_events.ts new file mode 100644 index 0000000000000000000000000000000000000000..b5e7c776d57ade5027cad8421d5b82760502cc33 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/command_monitoring_events.ts @@ -0,0 +1,316 @@ +import { type Document, type ObjectId } from '../bson'; +import { + COMMAND_FAILED, + COMMAND_STARTED, + COMMAND_SUCCEEDED, + LEGACY_HELLO_COMMAND, + LEGACY_HELLO_COMMAND_CAMEL_CASE +} from '../constants'; +import { calculateDurationInMs } from '../utils'; +import { + DocumentSequence, + OpMsgRequest, + type OpQueryRequest, + type WriteProtocolMessageType +} from './commands'; +import type { Connection } from './connection'; + +/** + * An event indicating the start of a given command + * @public + * @category Event + */ +export class CommandStartedEvent { + commandObj?: Document; + requestId: number; + databaseName: string; + commandName: string; + command: Document; + address: string; + /** Driver generated connection id */ + connectionId?: string | number; + /** + * Server generated connection id + * Distinct from the connection id and is returned by the hello or legacy hello response as "connectionId" + * from the server on 4.2+. + */ + serverConnectionId: bigint | null; + serviceId?: ObjectId; + /** @internal */ + name = COMMAND_STARTED; + + /** + * Create a started event + * + * @internal + * @param pool - the pool that originated the command + * @param command - the command + */ + constructor( + connection: Connection, + command: WriteProtocolMessageType, + serverConnectionId: bigint | null + ) { + const cmd = extractCommand(command); + const commandName = extractCommandName(cmd); + const { address, connectionId, serviceId } = extractConnectionDetails(connection); + + // TODO: remove in major revision, this is not spec behavior + if (SENSITIVE_COMMANDS.has(commandName)) { + this.commandObj = {}; + this.commandObj[commandName] = true; + } + + this.address = address; + this.connectionId = connectionId; + this.serviceId = serviceId; + this.requestId = command.requestId; + this.databaseName = command.databaseName; + this.commandName = commandName; + this.command = maybeRedact(commandName, cmd, cmd); + this.serverConnectionId = serverConnectionId; + } + + /* @internal */ + get hasServiceId(): boolean { + return !!this.serviceId; + } +} + +/** + * An event indicating the success of a given command + * @public + * @category Event + */ +export class CommandSucceededEvent { + address: string; + /** Driver generated connection id */ + connectionId?: string | number; + /** + * Server generated connection id + * Distinct from the connection id and is returned by the hello or legacy hello response as "connectionId" from the server on 4.2+. + */ + serverConnectionId: bigint | null; + requestId: number; + duration: number; + commandName: string; + reply: unknown; + serviceId?: ObjectId; + databaseName: string; + /** @internal */ + name = COMMAND_SUCCEEDED; + + /** + * Create a succeeded event + * + * @internal + * @param pool - the pool that originated the command + * @param command - the command + * @param reply - the reply for this command from the server + * @param started - a high resolution tuple timestamp of when the command was first sent, to calculate duration + */ + constructor( + connection: Connection, + command: WriteProtocolMessageType, + reply: Document | undefined, + started: number, + serverConnectionId: bigint | null + ) { + const cmd = extractCommand(command); + const commandName = extractCommandName(cmd); + const { address, connectionId, serviceId } = extractConnectionDetails(connection); + + this.address = address; + this.connectionId = connectionId; + this.serviceId = serviceId; + this.requestId = command.requestId; + this.commandName = commandName; + this.duration = calculateDurationInMs(started); + this.reply = maybeRedact(commandName, cmd, extractReply(reply)); + this.serverConnectionId = serverConnectionId; + this.databaseName = command.databaseName; + } + + /* @internal */ + get hasServiceId(): boolean { + return !!this.serviceId; + } +} + +/** + * An event indicating the failure of a given command + * @public + * @category Event + */ +export class CommandFailedEvent { + address: string; + /** Driver generated connection id */ + connectionId?: string | number; + /** + * Server generated connection id + * Distinct from the connection id and is returned by the hello or legacy hello response as "connectionId" from the server on 4.2+. + */ + serverConnectionId: bigint | null; + requestId: number; + duration: number; + commandName: string; + failure: Error; + serviceId?: ObjectId; + databaseName: string; + /** @internal */ + name = COMMAND_FAILED; + + /** + * Create a failure event + * + * @internal + * @param pool - the pool that originated the command + * @param command - the command + * @param error - the generated error or a server error response + * @param started - a high resolution tuple timestamp of when the command was first sent, to calculate duration + */ + constructor( + connection: Connection, + command: WriteProtocolMessageType, + error: Error | Document, + started: number, + serverConnectionId: bigint | null + ) { + const cmd = extractCommand(command); + const commandName = extractCommandName(cmd); + const { address, connectionId, serviceId } = extractConnectionDetails(connection); + + this.address = address; + this.connectionId = connectionId; + this.serviceId = serviceId; + + this.requestId = command.requestId; + this.commandName = commandName; + this.duration = calculateDurationInMs(started); + this.failure = maybeRedact(commandName, cmd, error) as Error; + this.serverConnectionId = serverConnectionId; + this.databaseName = command.databaseName; + } + + /* @internal */ + get hasServiceId(): boolean { + return !!this.serviceId; + } +} + +/** + * Commands that we want to redact because of the sensitive nature of their contents + * @internal + */ +export const SENSITIVE_COMMANDS = new Set([ + 'authenticate', + 'saslStart', + 'saslContinue', + 'getnonce', + 'createUser', + 'updateUser', + 'copydbgetnonce', + 'copydbsaslstart', + 'copydb' +]); + +const HELLO_COMMANDS = new Set(['hello', LEGACY_HELLO_COMMAND, LEGACY_HELLO_COMMAND_CAMEL_CASE]); + +// helper methods +const extractCommandName = (commandDoc: Document) => Object.keys(commandDoc)[0]; +const collectionName = (command: OpQueryRequest) => command.ns.split('.')[1]; +const maybeRedact = (commandName: string, commandDoc: Document, result: Error | Document) => + SENSITIVE_COMMANDS.has(commandName) || + (HELLO_COMMANDS.has(commandName) && commandDoc.speculativeAuthenticate) + ? {} + : result; + +const LEGACY_FIND_QUERY_MAP: { [key: string]: string } = { + $query: 'filter', + $orderby: 'sort', + $hint: 'hint', + $comment: 'comment', + $maxScan: 'maxScan', + $max: 'max', + $min: 'min', + $returnKey: 'returnKey', + $showDiskLoc: 'showRecordId', + $maxTimeMS: 'maxTimeMS', + $snapshot: 'snapshot' +}; + +const LEGACY_FIND_OPTIONS_MAP = { + numberToSkip: 'skip', + numberToReturn: 'batchSize', + returnFieldSelector: 'projection' +} as const; + +/** Extract the actual command from the query, possibly up-converting if it's a legacy format */ +function extractCommand(command: WriteProtocolMessageType): Document { + if (command instanceof OpMsgRequest) { + const cmd = { ...command.command }; + // For OP_MSG with payload type 1 we need to pull the documents + // array out of the document sequence for monitoring. + if (cmd.ops instanceof DocumentSequence) { + cmd.ops = cmd.ops.documents; + } + if (cmd.nsInfo instanceof DocumentSequence) { + cmd.nsInfo = cmd.nsInfo.documents; + } + return cmd; + } + + if (command.query?.$query) { + let result: Document; + if (command.ns === 'admin.$cmd') { + // up-convert legacy command + result = Object.assign({}, command.query.$query); + } else { + // up-convert legacy find command + result = { find: collectionName(command) }; + Object.keys(LEGACY_FIND_QUERY_MAP).forEach(key => { + if (command.query[key] != null) { + result[LEGACY_FIND_QUERY_MAP[key]] = { ...command.query[key] }; + } + }); + } + + Object.keys(LEGACY_FIND_OPTIONS_MAP).forEach(key => { + const legacyKey = key as keyof typeof LEGACY_FIND_OPTIONS_MAP; + if (command[legacyKey] != null) { + result[LEGACY_FIND_OPTIONS_MAP[legacyKey]] = command[legacyKey]; + } + }); + + return result; + } + + let clonedQuery: Record = {}; + const clonedCommand: Record = { ...command }; + if (command.query) { + clonedQuery = { ...command.query }; + clonedCommand.query = clonedQuery; + } + + return command.query ? clonedQuery : clonedCommand; +} + +function extractReply(reply?: Document) { + if (!reply) { + return reply; + } + + return reply.result ? reply.result : reply; +} + +function extractConnectionDetails(connection: Connection) { + let connectionId; + if ('id' in connection) { + connectionId = connection.id; + } + return { + address: connection.address, + serviceId: connection.serviceId, + connectionId + }; +} diff --git a/gateway/node_modules/mongodb/src/cmap/commands.ts b/gateway/node_modules/mongodb/src/cmap/commands.ts new file mode 100644 index 0000000000000000000000000000000000000000..bdc332cbdf137c45bb828a234c5b65cefb13bd8d --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/commands.ts @@ -0,0 +1,807 @@ +import { + BSON, + type BSONSerializeOptions, + ByteUtils, + type Document, + type Long, + NumberUtils, + readInt32LE, + setUint32LE +} from '../bson'; +import { MongoInvalidArgumentError, MongoRuntimeError } from '../error'; +import { type ReadPreference } from '../read_preference'; +import type { ClientSession } from '../sessions'; +import type { CommandOptions } from './connection'; +import { + compress, + Compressor, + type CompressorName, + uncompressibleCommands +} from './wire_protocol/compression'; +import { OP_COMPRESSED, OP_MSG, OP_QUERY } from './wire_protocol/constants'; + +// Incrementing request id +let _requestId = 0; + +// Query flags +const OPTS_TAILABLE_CURSOR = 2; +const OPTS_SECONDARY = 4; +const OPTS_OPLOG_REPLAY = 8; +const OPTS_NO_CURSOR_TIMEOUT = 16; +const OPTS_AWAIT_DATA = 32; +const OPTS_EXHAUST = 64; +const OPTS_PARTIAL = 128; + +// Response flags +const CURSOR_NOT_FOUND = 1; +const QUERY_FAILURE = 2; +const SHARD_CONFIG_STALE = 4; +const AWAIT_CAPABLE = 8; + +const encodeUTF8Into = ByteUtils.encodeUTF8Into; + +/** @internal */ +export type WriteProtocolMessageType = OpQueryRequest | OpMsgRequest; + +/** @internal */ +export interface OpQueryOptions extends CommandOptions { + socketTimeoutMS?: number; + session?: ClientSession; + numberToSkip?: number; + numberToReturn?: number; + returnFieldSelector?: Document; + pre32Limit?: number; + serializeFunctions?: boolean; + ignoreUndefined?: boolean; + maxBsonSize?: number; + checkKeys?: boolean; + secondaryOk?: boolean; + + requestId?: number; + moreToCome?: boolean; + exhaustAllowed?: boolean; +} + +/** @internal */ +export class OpQueryRequest { + ns: string; + numberToSkip: number; + numberToReturn: number; + returnFieldSelector?: Document; + requestId: number; + pre32Limit?: number; + serializeFunctions: boolean; + ignoreUndefined: boolean; + maxBsonSize: number; + checkKeys: boolean; + batchSize: number; + tailable: boolean; + secondaryOk: boolean; + oplogReplay: boolean; + noCursorTimeout: boolean; + awaitData: boolean; + exhaust: boolean; + partial: boolean; + /** moreToCome is an OP_MSG only concept */ + moreToCome = false; + databaseName: string; + query: Document; + + constructor(databaseName: string, query: Document, options: OpQueryOptions) { + // Basic options needed to be passed in + // TODO(NODE-3483): Replace with MongoCommandError + const ns = `${databaseName}.$cmd`; + if (typeof databaseName !== 'string') { + throw new MongoRuntimeError('Database name must be a string for a query'); + } + // TODO(NODE-3483): Replace with MongoCommandError + if (query == null) throw new MongoRuntimeError('A query document must be specified for query'); + + // Validate that we are not passing 0x00 in the collection name + if (ns.indexOf('\x00') !== -1) { + // TODO(NODE-3483): Use MongoNamespace static method + throw new MongoRuntimeError('Namespace cannot contain a null character'); + } + + // Basic optionsa + this.databaseName = databaseName; + this.query = query; + this.ns = ns; + + // Additional options + this.numberToSkip = options.numberToSkip || 0; + this.numberToReturn = options.numberToReturn || 0; + this.returnFieldSelector = options.returnFieldSelector || undefined; + this.requestId = options.requestId ?? OpQueryRequest.getRequestId(); + + // special case for pre-3.2 find commands, delete ASAP + this.pre32Limit = options.pre32Limit; + + // Serialization option + this.serializeFunctions = + typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false; + this.ignoreUndefined = + typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : false; + this.maxBsonSize = options.maxBsonSize || 1024 * 1024 * 16; + this.checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false; + this.batchSize = this.numberToReturn; + + // Flags + this.tailable = false; + this.secondaryOk = typeof options.secondaryOk === 'boolean' ? options.secondaryOk : false; + this.oplogReplay = false; + this.noCursorTimeout = false; + this.awaitData = false; + this.exhaust = false; + this.partial = false; + } + + /** Assign next request Id. */ + incRequestId(): void { + this.requestId = _requestId++; + } + + /** Peek next request Id. */ + nextRequestId(): number { + return _requestId + 1; + } + + /** Increment then return next request Id. */ + static getRequestId(): number { + return ++_requestId; + } + + // Uses a single allocated buffer for the process, avoiding multiple memory allocations + toBin(): Uint8Array[] { + const buffers = []; + let projection = null; + + // Set up the flags + let flags = 0; + if (this.tailable) { + flags |= OPTS_TAILABLE_CURSOR; + } + + if (this.secondaryOk) { + flags |= OPTS_SECONDARY; + } + + if (this.oplogReplay) { + flags |= OPTS_OPLOG_REPLAY; + } + + if (this.noCursorTimeout) { + flags |= OPTS_NO_CURSOR_TIMEOUT; + } + + if (this.awaitData) { + flags |= OPTS_AWAIT_DATA; + } + + if (this.exhaust) { + flags |= OPTS_EXHAUST; + } + + if (this.partial) { + flags |= OPTS_PARTIAL; + } + + // If batchSize is different to this.numberToReturn + if (this.batchSize !== this.numberToReturn) this.numberToReturn = this.batchSize; + + // Allocate write protocol header buffer + const header = ByteUtils.allocate( + 4 * 4 + // Header + 4 + // Flags + ByteUtils.utf8ByteLength(this.ns) + + 1 + // namespace + 4 + // numberToSkip + 4 // numberToReturn + ); + + // Add header to buffers + buffers.push(header); + + // Serialize the query + const query = BSON.serialize(this.query, { + checkKeys: this.checkKeys, + serializeFunctions: this.serializeFunctions, + ignoreUndefined: this.ignoreUndefined + }); + + // Add query document + buffers.push(query); + + if (this.returnFieldSelector && Object.keys(this.returnFieldSelector).length > 0) { + // Serialize the projection document + projection = BSON.serialize(this.returnFieldSelector, { + checkKeys: this.checkKeys, + serializeFunctions: this.serializeFunctions, + ignoreUndefined: this.ignoreUndefined + }); + // Add projection document + buffers.push(projection); + } + + // Total message size + const totalLength = header.length + query.length + (projection ? projection.length : 0); + + // Set up the index + let index = 4; + + // Write total document length + header[3] = (totalLength >> 24) & 0xff; + header[2] = (totalLength >> 16) & 0xff; + header[1] = (totalLength >> 8) & 0xff; + header[0] = totalLength & 0xff; + + // Write header information requestId + header[index + 3] = (this.requestId >> 24) & 0xff; + header[index + 2] = (this.requestId >> 16) & 0xff; + header[index + 1] = (this.requestId >> 8) & 0xff; + header[index] = this.requestId & 0xff; + index = index + 4; + + // Write header information responseTo + header[index + 3] = (0 >> 24) & 0xff; + header[index + 2] = (0 >> 16) & 0xff; + header[index + 1] = (0 >> 8) & 0xff; + header[index] = 0 & 0xff; + index = index + 4; + + // Write header information OP_QUERY + header[index + 3] = (OP_QUERY >> 24) & 0xff; + header[index + 2] = (OP_QUERY >> 16) & 0xff; + header[index + 1] = (OP_QUERY >> 8) & 0xff; + header[index] = OP_QUERY & 0xff; + index = index + 4; + + // Write header information flags + header[index + 3] = (flags >> 24) & 0xff; + header[index + 2] = (flags >> 16) & 0xff; + header[index + 1] = (flags >> 8) & 0xff; + header[index] = flags & 0xff; + index = index + 4; + + // Write collection name + index = index + encodeUTF8Into(header, this.ns, index) + 1; + header[index - 1] = 0; + + // Write header information flags numberToSkip + header[index + 3] = (this.numberToSkip >> 24) & 0xff; + header[index + 2] = (this.numberToSkip >> 16) & 0xff; + header[index + 1] = (this.numberToSkip >> 8) & 0xff; + header[index] = this.numberToSkip & 0xff; + index = index + 4; + + // Write header information flags numberToReturn + header[index + 3] = (this.numberToReturn >> 24) & 0xff; + header[index + 2] = (this.numberToReturn >> 16) & 0xff; + header[index + 1] = (this.numberToReturn >> 8) & 0xff; + header[index] = this.numberToReturn & 0xff; + index = index + 4; + + // Return the buffers + return buffers; + } +} + +/** @internal */ +export interface MessageHeader { + length: number; + requestId: number; + responseTo: number; + opCode: number; + fromCompressed?: boolean; +} + +/** @internal */ +export class OpReply { + parsed: boolean; + raw: Uint8Array; + data: Uint8Array; + opts: BSONSerializeOptions; + length: number; + requestId: number; + responseTo: number; + opCode: number; + fromCompressed?: boolean; + responseFlags?: number; + cursorId?: Long; + startingFrom?: number; + numberReturned?: number; + cursorNotFound?: boolean; + queryFailure?: boolean; + shardConfigStale?: boolean; + awaitCapable?: boolean; + useBigInt64: boolean; + promoteLongs: boolean; + promoteValues: boolean; + promoteBuffers: boolean; + bsonRegExp?: boolean; + index = 0; + sections: Uint8Array[] = []; + + /** moreToCome is an OP_MSG only concept */ + moreToCome = false; + + constructor( + message: Uint8Array, + msgHeader: MessageHeader, + msgBody: Uint8Array, + opts?: BSONSerializeOptions + ) { + this.parsed = false; + this.raw = message; + this.data = msgBody; + this.opts = opts ?? { + useBigInt64: false, + promoteLongs: true, + promoteValues: true, + promoteBuffers: false, + bsonRegExp: false + }; + + // Read the message header + this.length = msgHeader.length; + this.requestId = msgHeader.requestId; + this.responseTo = msgHeader.responseTo; + this.opCode = msgHeader.opCode; + this.fromCompressed = msgHeader.fromCompressed; + + // Flag values + this.useBigInt64 = typeof this.opts.useBigInt64 === 'boolean' ? this.opts.useBigInt64 : false; + this.promoteLongs = typeof this.opts.promoteLongs === 'boolean' ? this.opts.promoteLongs : true; + this.promoteValues = + typeof this.opts.promoteValues === 'boolean' ? this.opts.promoteValues : true; + this.promoteBuffers = + typeof this.opts.promoteBuffers === 'boolean' ? this.opts.promoteBuffers : false; + this.bsonRegExp = typeof this.opts.bsonRegExp === 'boolean' ? this.opts.bsonRegExp : false; + } + + isParsed(): boolean { + return this.parsed; + } + + parse(): Uint8Array { + // Don't parse again if not needed + if (this.parsed) return this.sections[0]; + + // Position within OP_REPLY at which documents start + // (See https://www.mongodb.com/docs/manual/reference/mongodb-wire-protocol/#wire-op-reply) + this.index = 20; + + // Read the message body + this.responseFlags = readInt32LE(this.data, 0); + this.cursorId = new BSON.Long(readInt32LE(this.data, 4), readInt32LE(this.data, 8)); + this.startingFrom = readInt32LE(this.data, 12); + this.numberReturned = readInt32LE(this.data, 16); + + if (this.numberReturned < 0 || this.numberReturned > 2 ** 32 - 1) { + throw new RangeError( + `OP_REPLY numberReturned is an invalid array length ${this.numberReturned}` + ); + } + + this.cursorNotFound = (this.responseFlags & CURSOR_NOT_FOUND) !== 0; + this.queryFailure = (this.responseFlags & QUERY_FAILURE) !== 0; + this.shardConfigStale = (this.responseFlags & SHARD_CONFIG_STALE) !== 0; + this.awaitCapable = (this.responseFlags & AWAIT_CAPABLE) !== 0; + + // Parse Body + for (let i = 0; i < this.numberReturned; i++) { + const bsonSize = + this.data[this.index] | + (this.data[this.index + 1] << 8) | + (this.data[this.index + 2] << 16) | + (this.data[this.index + 3] << 24); + + const section = this.data.subarray(this.index, this.index + bsonSize); + this.sections.push(section); + + // Adjust the index + this.index = this.index + bsonSize; + } + + // Set parsed + this.parsed = true; + + return this.sections[0]; + } +} + +// Msg Flags +const OPTS_CHECKSUM_PRESENT = 1; +const OPTS_MORE_TO_COME = 2; +const OPTS_EXHAUST_ALLOWED = 1 << 16; + +/** @internal */ +export interface OpMsgOptions { + socketTimeoutMS?: number; + session?: ClientSession; + numberToSkip?: number; + numberToReturn?: number; + returnFieldSelector?: Document; + pre32Limit?: number; + serializeFunctions?: boolean; + ignoreUndefined?: boolean; + maxBsonSize?: number; + checkKeys?: boolean; + secondaryOk?: boolean; + + requestId?: number; + moreToCome?: boolean; + exhaustAllowed?: boolean; + readPreference: ReadPreference; +} + +/** @internal */ +export class DocumentSequence { + field: string; + documents: Document[]; + serializedDocumentsLength: number; + private chunks: Uint8Array[]; + private header: Uint8Array; + + /** + * Create a new document sequence for the provided field. + * @param field - The field it will replace. + */ + constructor(field: string, documents?: Document[]) { + this.field = field; + this.documents = []; + this.chunks = []; + this.serializedDocumentsLength = 0; + // Document sequences starts with type 1 at the first byte. + // Field strings must always be UTF-8. + const buffer = ByteUtils.allocateUnsafe(1 + 4 + this.field.length + 1); + buffer[0] = 1; + // Third part is the field name at offset 5 with trailing null byte. + encodeUTF8Into(buffer, `${this.field}\0`, 5); + this.chunks.push(buffer); + this.header = buffer; + if (documents) { + for (const doc of documents) { + this.push(doc, BSON.serialize(doc)); + } + } + } + + /** + * Push a document to the document sequence. Will serialize the document + * as well and return the current serialized length of all documents. + * @param document - The document to add. + * @param buffer - The serialized document in raw BSON. + * @returns The new total document sequence length. + */ + push(document: Document, buffer: Uint8Array): number { + this.serializedDocumentsLength += buffer.length; + // Push the document. + this.documents.push(document); + // Push the document raw bson. + this.chunks.push(buffer); + // Write the new length. + if (this.header) { + NumberUtils.setInt32LE( + this.header, + 1, + 4 + this.field.length + 1 + this.serializedDocumentsLength + ); + } + return this.serializedDocumentsLength + this.header.length; + } + + /** + * Get the fully serialized bytes for the document sequence section. + * @returns The section bytes. + */ + toBin(): Uint8Array { + return ByteUtils.concat(this.chunks); + } +} + +/** @internal */ +export class OpMsgRequest { + requestId: number; + serializeFunctions: boolean; + ignoreUndefined: boolean; + checkKeys: boolean; + maxBsonSize: number; + checksumPresent: boolean; + moreToCome: boolean; + exhaustAllowed: boolean; + databaseName: string; + command: Document; + options: OpQueryOptions; + + constructor(databaseName: string, command: Document, options: OpQueryOptions) { + // Basic options needed to be passed in + if (command == null) + throw new MongoInvalidArgumentError('Query document must be specified for query'); + + // Basic optionsa + this.databaseName = databaseName; + this.command = command; + this.command.$db = databaseName; + + // Ensure empty options + this.options = options ?? {}; + + // Additional options + this.requestId = options.requestId ? options.requestId : OpMsgRequest.getRequestId(); + + // Serialization option + this.serializeFunctions = + typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false; + this.ignoreUndefined = + typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : false; + this.checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false; + this.maxBsonSize = options.maxBsonSize || 1024 * 1024 * 16; + + // flags + this.checksumPresent = false; + this.moreToCome = options.moreToCome ?? command.writeConcern?.w === 0; + this.exhaustAllowed = + typeof options.exhaustAllowed === 'boolean' ? options.exhaustAllowed : false; + } + + toBin(): Uint8Array[] { + const buffers: Uint8Array[] = []; + let flags = 0; + + if (this.checksumPresent) { + flags |= OPTS_CHECKSUM_PRESENT; + } + + if (this.moreToCome) { + flags |= OPTS_MORE_TO_COME; + } + + if (this.exhaustAllowed) { + flags |= OPTS_EXHAUST_ALLOWED; + } + + const header = ByteUtils.allocate( + 4 * 4 + // Header + 4 // Flags + ); + + buffers.push(header); + + let totalLength = header.length; + const command = this.command; + totalLength += this.makeSections(buffers, command); + + NumberUtils.setInt32LE(header, 0, totalLength); // messageLength + NumberUtils.setInt32LE(header, 4, this.requestId); // requestID + NumberUtils.setInt32LE(header, 8, 0); // responseTo + NumberUtils.setInt32LE(header, 12, OP_MSG); // opCode + // The OP_MSG spec calls out that flags is uint32: + // https://github.com/mongodb/specifications/blob/master/source/message/OP_MSG.md#op_msg-1 + setUint32LE(header, 16, flags); // flags + return buffers; + } + + /** + * Add the sections to the OP_MSG request's buffers and returns the length. + */ + makeSections(buffers: Uint8Array[], document: Document): number { + const sequencesBuffer = this.extractDocumentSequences(document); + const payloadTypeBuffer = ByteUtils.allocateUnsafe(1); + payloadTypeBuffer[0] = 0; + + const documentBuffer = this.serializeBson(document); + // First section, type 0 + buffers.push(payloadTypeBuffer); + buffers.push(documentBuffer); + // Subsequent sections, type 1 + buffers.push(sequencesBuffer); + + return payloadTypeBuffer.length + documentBuffer.length + sequencesBuffer.length; + } + + /** + * Extracts the document sequences from the command document and returns + * a buffer to be added as multiple sections after the initial type 0 + * section in the message. + */ + extractDocumentSequences(document: Document): Uint8Array { + // Pull out any field in the command document that's value is a document sequence. + const chunks = []; + for (const [key, value] of Object.entries(document)) { + if (value instanceof DocumentSequence) { + chunks.push(value.toBin()); + // Why are we removing the field from the command? This is because it needs to be + // removed in the OP_MSG request first section, and DocumentSequence is not a + // BSON type and is specific to the MongoDB wire protocol so there's nothing + // our BSON serializer can do about this. Since DocumentSequence is not exposed + // in the public API and only used internally, we are never mutating an original + // command provided by the user, just our own, and it's cheaper to delete from + // our own command than copying it. + delete document[key]; + } + } + if (chunks.length > 0) { + return ByteUtils.concat(chunks); + } + // If we have no document sequences we return an empty buffer for nothing to add + // to the payload. + return ByteUtils.allocate(0); + } + + serializeBson(document: Document): Uint8Array { + return BSON.serialize(document, { + checkKeys: this.checkKeys, + serializeFunctions: this.serializeFunctions, + ignoreUndefined: this.ignoreUndefined + }); + } + + static getRequestId(): number { + _requestId = (_requestId + 1) & 0x7fffffff; + return _requestId; + } +} + +/** @internal */ +export class OpMsgResponse { + parsed: boolean; + raw: Uint8Array; + data: Uint8Array; + opts: BSONSerializeOptions; + length: number; + requestId: number; + responseTo: number; + opCode: number; + fromCompressed?: boolean; + responseFlags: number; + checksumPresent: boolean; + /** Indicates the server will be sending more responses on this connection */ + moreToCome: boolean; + exhaustAllowed: boolean; + useBigInt64: boolean; + promoteLongs: boolean; + promoteValues: boolean; + promoteBuffers: boolean; + bsonRegExp: boolean; + index = 0; + sections: Uint8Array[] = []; + + constructor( + message: Uint8Array, + msgHeader: MessageHeader, + msgBody: Uint8Array, + opts?: BSONSerializeOptions + ) { + this.parsed = false; + this.raw = message; + this.data = msgBody; + this.opts = opts ?? { + useBigInt64: false, + promoteLongs: true, + promoteValues: true, + promoteBuffers: false, + bsonRegExp: false + }; + + // Read the message header + this.length = msgHeader.length; + this.requestId = msgHeader.requestId; + this.responseTo = msgHeader.responseTo; + this.opCode = msgHeader.opCode; + this.fromCompressed = msgHeader.fromCompressed; + + // Read response flags + this.responseFlags = readInt32LE(msgBody, 0); + this.checksumPresent = (this.responseFlags & OPTS_CHECKSUM_PRESENT) !== 0; + this.moreToCome = (this.responseFlags & OPTS_MORE_TO_COME) !== 0; + this.exhaustAllowed = (this.responseFlags & OPTS_EXHAUST_ALLOWED) !== 0; + this.useBigInt64 = typeof this.opts.useBigInt64 === 'boolean' ? this.opts.useBigInt64 : false; + this.promoteLongs = typeof this.opts.promoteLongs === 'boolean' ? this.opts.promoteLongs : true; + this.promoteValues = + typeof this.opts.promoteValues === 'boolean' ? this.opts.promoteValues : true; + this.promoteBuffers = + typeof this.opts.promoteBuffers === 'boolean' ? this.opts.promoteBuffers : false; + this.bsonRegExp = typeof this.opts.bsonRegExp === 'boolean' ? this.opts.bsonRegExp : false; + } + + isParsed(): boolean { + return this.parsed; + } + + parse(): Uint8Array { + // Don't parse again if not needed + if (this.parsed) return this.sections[0]; + + this.index = 4; + + while (this.index < this.data.length) { + const payloadType = this.data[this.index++]; + if (payloadType === 0) { + // BSON spec specifies that this is a 32-bit signed integer: https://bsonspec.org/spec.html#:~:text=%3A%3A%3D-,int32,-e_list%20unsigned_byte(0 + // While allowing negative sizes seems odd, in practice we never expect a negative size. Also, the server's 16mb limit for BSON documents leaves plenty + // of room in an int32 to store a document of the max BSON size that the server supports + const bsonSize = readInt32LE(this.data, this.index); + const bin = this.data.subarray(this.index, this.index + bsonSize); + + this.sections.push(bin); + + this.index += bsonSize; + } else if (payloadType === 1) { + // It was decided that no driver makes use of payload type 1 + + // TODO(NODE-3483): Replace with MongoDeprecationError + throw new MongoRuntimeError('OP_MSG Payload Type 1 detected unsupported protocol'); + } + } + + this.parsed = true; + + return this.sections[0]; + } +} + +const MESSAGE_HEADER_SIZE = 16; +const COMPRESSION_DETAILS_SIZE = 9; // originalOpcode + uncompressedSize, compressorID + +/** + * @internal + */ +export interface OpCompressesRequestOptions { + zlibCompressionLevel: number; + agreedCompressor: CompressorName; +} + +/** + * @internal + * + * An OP_COMPRESSED request wraps either an OP_QUERY or OP_MSG message. + */ +export class OpCompressedRequest { + private command: WriteProtocolMessageType; + private options: OpCompressesRequestOptions; + + constructor(command: WriteProtocolMessageType, options: OpCompressesRequestOptions) { + this.command = command; + this.options = { + zlibCompressionLevel: options.zlibCompressionLevel, + agreedCompressor: options.agreedCompressor + }; + } + + // Return whether a command contains an uncompressible command term + // Will return true if command contains no uncompressible command terms + static canCompress(command: WriteProtocolMessageType) { + const commandDoc = command instanceof OpMsgRequest ? command.command : command.query; + const commandName = Object.keys(commandDoc)[0]; + return !uncompressibleCommands.has(commandName); + } + + async toBin(): Promise { + const concatenatedOriginalCommandBuffer = ByteUtils.concat(this.command.toBin()); + // otherwise, compress the message + const messageToBeCompressed = concatenatedOriginalCommandBuffer.slice(MESSAGE_HEADER_SIZE); + + // Extract information needed for OP_COMPRESSED from the uncompressed message + const originalCommandOpCode = readInt32LE(concatenatedOriginalCommandBuffer, 12); + + // Compress the message body + const compressedMessage = await compress(this.options, messageToBeCompressed); + // Create the msgHeader of OP_COMPRESSED + const msgHeader = ByteUtils.allocate(MESSAGE_HEADER_SIZE); + NumberUtils.setInt32LE( + msgHeader, + 0, + MESSAGE_HEADER_SIZE + COMPRESSION_DETAILS_SIZE + compressedMessage.length + ); // messageLength + NumberUtils.setInt32LE(msgHeader, 4, this.command.requestId); // requestID + NumberUtils.setInt32LE(msgHeader, 8, 0); // responseTo (zero) + NumberUtils.setInt32LE(msgHeader, 12, OP_COMPRESSED); // opCode + // Create the compression details of OP_COMPRESSED + const compressionDetails = ByteUtils.allocate(COMPRESSION_DETAILS_SIZE); + NumberUtils.setInt32LE(compressionDetails, 0, originalCommandOpCode); // originalOpcode + NumberUtils.setInt32LE(compressionDetails, 4, messageToBeCompressed.length); // Size of the uncompressed compressedMessage, excluding the MsgHeader + compressionDetails[8] = Compressor[this.options.agreedCompressor]; // compressorID + return [msgHeader, compressionDetails, compressedMessage]; + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/connect.ts b/gateway/node_modules/mongodb/src/cmap/connect.ts new file mode 100644 index 0000000000000000000000000000000000000000..35d3d622f9cad6e97a3164d8e623b8628ee334d4 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/connect.ts @@ -0,0 +1,538 @@ +import type { Socket, SocketConnectOpts } from 'net'; +import * as net from 'net'; +import type { ConnectionOptions as TLSConnectionOpts, TLSSocket } from 'tls'; +import * as tls from 'tls'; + +import type { Document } from '../bson'; +import { LEGACY_HELLO_COMMAND } from '../constants'; +import { getSocks, type SocksLib } from '../deps'; +import { + MongoCompatibilityError, + MongoError, + MongoErrorLabel, + MongoInvalidArgumentError, + MongoNetworkError, + MongoNetworkTimeoutError, + MongoRuntimeError, + needsRetryableWriteLabel +} from '../error'; +import { HostAddress, ns, promiseWithResolvers } from '../utils'; +import { AuthContext } from './auth/auth_provider'; +import { AuthMechanism } from './auth/providers'; +import { + type CommandOptions, + Connection, + type ConnectionOptions, + CryptoConnection +} from './connection'; +import { + MAX_SUPPORTED_SERVER_VERSION, + MAX_SUPPORTED_WIRE_VERSION, + MIN_SUPPORTED_SERVER_VERSION, + MIN_SUPPORTED_WIRE_VERSION +} from './wire_protocol/constants'; + +/** @public */ +export type Stream = Socket | TLSSocket; + +function applyBackpressureLabels(error: MongoError) { + error.addErrorLabel(MongoErrorLabel.SystemOverloadedError); + error.addErrorLabel(MongoErrorLabel.RetryableError); +} + +export async function connect(options: ConnectionOptions): Promise { + let connection: Connection | null = null; + try { + const socket = await makeSocket(options); + connection = makeConnection(options, socket); + await performInitialHandshake(connection, options); + return connection; + } catch (error) { + connection?.destroy(); + throw error; + } +} + +export function makeConnection(options: ConnectionOptions, socket: Stream): Connection { + let ConnectionType = options.connectionType ?? Connection; + if (options.autoEncrypter) { + ConnectionType = CryptoConnection; + } + + return new ConnectionType(socket, options); +} + +function checkSupportedServer(hello: Document, options: ConnectionOptions) { + const maxWireVersion = Number(hello.maxWireVersion); + const minWireVersion = Number(hello.minWireVersion); + const serverVersionHighEnough = + !Number.isNaN(maxWireVersion) && maxWireVersion >= MIN_SUPPORTED_WIRE_VERSION; + const serverVersionLowEnough = + !Number.isNaN(minWireVersion) && minWireVersion <= MAX_SUPPORTED_WIRE_VERSION; + + if (serverVersionHighEnough) { + if (serverVersionLowEnough) { + return null; + } + + const message = `Server at ${options.hostAddress} reports minimum wire version ${JSON.stringify( + hello.minWireVersion + )}, but this version of the Node.js Driver requires at most ${MAX_SUPPORTED_WIRE_VERSION} (MongoDB ${MAX_SUPPORTED_SERVER_VERSION})`; + return new MongoCompatibilityError(message); + } + + const message = `Server at ${options.hostAddress} reports maximum wire version ${ + JSON.stringify(hello.maxWireVersion) ?? 0 + }, but this version of the Node.js Driver requires at least ${MIN_SUPPORTED_WIRE_VERSION} (MongoDB ${MIN_SUPPORTED_SERVER_VERSION})`; + return new MongoCompatibilityError(message); +} + +export async function performInitialHandshake( + conn: Connection, + options: ConnectionOptions +): Promise { + const credentials = options.credentials; + + if (credentials) { + if ( + !(credentials.mechanism === AuthMechanism.MONGODB_DEFAULT) && + !options.authProviders.getOrCreateProvider( + credentials.mechanism, + credentials.mechanismProperties + ) + ) { + throw new MongoInvalidArgumentError(`AuthMechanism '${credentials.mechanism}' not supported`); + } + } + + const authContext = new AuthContext(conn, credentials, options); + conn.authContext = authContext; + + // If we encounter an error preparing the handshake document, do NOT apply backpressure labels. Errors + // encountered building the handshake document are all client-side, and do not indicate an overloaded server. + const handshakeDoc = await prepareHandshakeDocument(authContext); + + // @ts-expect-error: TODO(NODE-5141): The options need to be filtered properly, Connection options differ from Command options + const handshakeOptions: CommandOptions = { ...options, raw: false }; + if (typeof options.connectTimeoutMS === 'number') { + // The handshake technically is a monitoring check, so its socket timeout should be connectTimeoutMS + handshakeOptions.socketTimeoutMS = options.connectTimeoutMS; + } + + const start = new Date().getTime(); + + const response = await executeHandshake(handshakeDoc, handshakeOptions); + + if (!('isWritablePrimary' in response)) { + // Provide hello-style response document. + response.isWritablePrimary = response[LEGACY_HELLO_COMMAND]; + } + + if (response.helloOk) { + conn.helloOk = true; + } + + const supportedServerErr = checkSupportedServer(response, options); + if (supportedServerErr) { + throw supportedServerErr; + } + + if (options.loadBalanced) { + if (!response.serviceId) { + throw new MongoCompatibilityError( + 'Driver attempted to initialize in load balancing mode, ' + + 'but the server does not support this mode.' + ); + } + } + + // NOTE: This is metadata attached to the connection while porting away from + // handshake being done in the `Server` class. Likely, it should be + // relocated, or at very least restructured. + conn.hello = response; + conn.lastHelloMS = new Date().getTime() - start; + + if (!response.arbiterOnly && credentials) { + // store the response on auth context + authContext.response = response; + + const resolvedCredentials = credentials.resolveAuthMechanism(response); + const provider = options.authProviders.getOrCreateProvider( + resolvedCredentials.mechanism, + resolvedCredentials.mechanismProperties + ); + if (!provider) { + throw new MongoInvalidArgumentError( + `No AuthProvider for ${resolvedCredentials.mechanism} defined.` + ); + } + + try { + await provider.auth(authContext); + } catch (error) { + // NOTE: If we encounter an error authenticating a connection, do NOT apply backpressure labels. + + if (error instanceof MongoError) { + error.addErrorLabel(MongoErrorLabel.HandshakeError); + if (needsRetryableWriteLabel(error, response.maxWireVersion, conn.description.type)) { + error.addErrorLabel(MongoErrorLabel.RetryableWriteError); + } + } + + throw error; + } + } + + // Connection establishment is socket creation (tcp handshake, tls handshake, MongoDB handshake (saslStart, saslContinue)) + // Once connection is established, command logging can log events (if enabled) + conn.established = true; + + async function executeHandshake(handshakeDoc: Document, handshakeOptions: CommandOptions) { + try { + const handshakeResponse = await conn.command( + ns('admin.$cmd'), + handshakeDoc, + handshakeOptions + ); + return handshakeResponse; + } catch (error) { + if (error instanceof MongoError) { + error.addErrorLabel(MongoErrorLabel.HandshakeError); + } + // If we encounter a network error executing the initial handshake, apply backpressure labels. + if (error instanceof MongoNetworkError) { + applyBackpressureLabels(error); + } + + throw error; + } + } +} + +/** + * HandshakeDocument used during authentication. + * @internal + */ +export interface HandshakeDocument extends Document { + /** + * @deprecated Use hello instead + */ + ismaster?: boolean; + hello?: boolean; + helloOk?: boolean; + client: Document; + compression: string[]; + saslSupportedMechs?: string; + loadBalanced?: boolean; + backpressure: true; +} + +/** + * @internal + * + * This function is only exposed for testing purposes. + */ +export async function prepareHandshakeDocument( + authContext: AuthContext +): Promise { + const options = authContext.options; + const compressors = options.compressors ? options.compressors : []; + const { serverApi } = authContext.connection; + const clientMetadata: Document = await options.metadata; + + const handshakeDoc: HandshakeDocument = { + [serverApi?.version || options.loadBalanced === true ? 'hello' : LEGACY_HELLO_COMMAND]: 1, + backpressure: true, + helloOk: true, + client: clientMetadata, + compression: compressors + }; + + if (options.loadBalanced === true) { + handshakeDoc.loadBalanced = true; + } + + const credentials = authContext.credentials; + if (credentials) { + if (credentials.mechanism === AuthMechanism.MONGODB_DEFAULT && credentials.username) { + handshakeDoc.saslSupportedMechs = `${credentials.source}.${credentials.username}`; + + const provider = authContext.options.authProviders.getOrCreateProvider( + AuthMechanism.MONGODB_SCRAM_SHA256, + credentials.mechanismProperties + ); + if (!provider) { + // This auth mechanism is always present. + throw new MongoInvalidArgumentError( + `No AuthProvider for ${AuthMechanism.MONGODB_SCRAM_SHA256} defined.` + ); + } + return await provider.prepare(handshakeDoc, authContext); + } + const provider = authContext.options.authProviders.getOrCreateProvider( + credentials.mechanism, + credentials.mechanismProperties + ); + if (!provider) { + throw new MongoInvalidArgumentError(`No AuthProvider for ${credentials.mechanism} defined.`); + } + return await provider.prepare(handshakeDoc, authContext); + } + return handshakeDoc; +} + +/** + * @internal + * Default TCP keepAlive initial delay in milliseconds. + * Set to half the Azure load balancer idle timeout (240s) to ensure + * probes fire well before cloud LBs (Azure, AWS PrivateLink/NLB) + * drop idle connections. + */ +export const DEFAULT_KEEP_ALIVE_INITIAL_DELAY_MS = 120_000; + +/** @public */ +export const LEGAL_TLS_SOCKET_OPTIONS = [ + 'allowPartialTrustChain', + 'ALPNProtocols', + 'ca', + 'cert', + 'checkServerIdentity', + 'ciphers', + 'crl', + 'ecdhCurve', + 'key', + 'minDHSize', + 'passphrase', + 'pfx', + 'rejectUnauthorized', + 'secureContext', + 'secureProtocol', + 'servername', + 'session' +] as const; + +/** @public */ +export const LEGAL_TCP_SOCKET_OPTIONS = [ + 'autoSelectFamily', + 'autoSelectFamilyAttemptTimeout', + 'keepAliveInitialDelay', + 'family', + 'hints', + 'localAddress', + 'localPort', + 'lookup' +] as const; + +function parseConnectOptions(options: ConnectionOptions): SocketConnectOpts { + const hostAddress = options.hostAddress; + if (!hostAddress) throw new MongoInvalidArgumentError('Option "hostAddress" is required'); + + const result: Partial = {}; + for (const name of LEGAL_TCP_SOCKET_OPTIONS) { + if (options[name] != null) { + (result as Document)[name] = options[name]; + } + } + result.keepAliveInitialDelay ??= DEFAULT_KEEP_ALIVE_INITIAL_DELAY_MS; + result.keepAlive = true; + result.noDelay = options.noDelay ?? true; + + if (typeof hostAddress.socketPath === 'string') { + result.path = hostAddress.socketPath; + return result as net.IpcNetConnectOpts; + } else if (typeof hostAddress.host === 'string') { + result.host = hostAddress.host; + result.port = hostAddress.port; + return result as net.TcpNetConnectOpts; + } else { + // This should never happen since we set up HostAddresses + // But if we don't throw here the socket could hang until timeout + // TODO(NODE-3483) + throw new MongoRuntimeError(`Unexpected HostAddress ${JSON.stringify(hostAddress)}`); + } +} + +type MakeConnectionOptions = ConnectionOptions & { existingSocket?: Stream }; + +function parseSslOptions(options: MakeConnectionOptions): TLSConnectionOpts { + const result: TLSConnectionOpts = parseConnectOptions(options); + // Merge in valid SSL options + for (const name of LEGAL_TLS_SOCKET_OPTIONS) { + if (options[name] != null) { + (result as Document)[name] = options[name]; + } + } + + if (options.existingSocket) { + result.socket = options.existingSocket; + } + + // Set default sni servername to be the same as host + if (result.servername == null && result.host && !net.isIP(result.host)) { + result.servername = result.host; + } + + return result; +} + +export async function makeSocket(options: MakeConnectionOptions): Promise { + const useTLS = options.tls ?? false; + const connectTimeoutMS = options.connectTimeoutMS ?? 30000; + const existingSocket = options.existingSocket; + const keepAliveInitialDelay = + options.keepAliveInitialDelay ?? DEFAULT_KEEP_ALIVE_INITIAL_DELAY_MS; + const noDelay = options.noDelay ?? true; + + let socket: Stream; + + if (options.proxyHost != null) { + // Currently, only Socks5 is supported. + return await makeSocks5Connection({ + ...options, + connectTimeoutMS // Should always be present for Socks5 + }); + } + + if (useTLS) { + const tlsSocket = tls.connect(parseSslOptions(options)); + if (typeof tlsSocket.disableRenegotiation === 'function') { + tlsSocket.disableRenegotiation(); + } + socket = tlsSocket; + } else if (existingSocket) { + // In the TLS case, parseSslOptions() sets options.socket to existingSocket, + // so we only need to handle the non-TLS case here (where existingSocket + // gives us all we need out of the box). + socket = existingSocket; + } else { + socket = net.createConnection(parseConnectOptions(options)); + } + + // Explicit setKeepAlive/setNoDelay are required because tls.connect() silently + // ignores these constructor options due to a Node.js bug. + // See: https://github.com/nodejs/node/issues/62003 + // TODO(NODE-7474): remove this fix once the underlying Node.js issue is resolved. + socket.setKeepAlive(true, keepAliveInitialDelay); + socket.setNoDelay(noDelay); + socket.setTimeout(connectTimeoutMS); + + let cancellationHandler: ((err: Error) => void) | null = null; + + const { promise: connectedSocket, resolve, reject } = promiseWithResolvers(); + if (existingSocket) { + resolve(socket); + } else { + const start = performance.now(); + const connectEvent = useTLS ? 'secureConnect' : 'connect'; + socket + .once(connectEvent, () => resolve(socket)) + .once('error', cause => + reject(new MongoNetworkError(MongoError.buildErrorMessage(cause), { cause })) + ) + .once('timeout', () => { + reject( + new MongoNetworkTimeoutError( + `Socket '${connectEvent}' timed out after ${(performance.now() - start) | 0}ms (connectTimeoutMS: ${connectTimeoutMS})` + ) + ); + }) + .once('close', () => + reject( + new MongoNetworkError( + `Socket closed after ${(performance.now() - start) | 0} during connection establishment` + ) + ) + ); + + if (options.cancellationToken != null) { + cancellationHandler = () => + reject( + new MongoNetworkError( + `Socket connection establishment was cancelled after ${(performance.now() - start) | 0}` + ) + ); + options.cancellationToken.once('cancel', cancellationHandler); + } + } + + try { + socket = await connectedSocket; + return socket; + } catch (error) { + // If we encounter an error while establishing a socket, apply the backpressure labels to it. We cannot + // differentiate between DNS, TLS errors and network errors without refactoring our connection establishment to + // handle all three steps separately. + applyBackpressureLabels(error); + socket.destroy(); + throw error; + } finally { + socket.setTimeout(0); + if (cancellationHandler != null) { + options.cancellationToken?.removeListener('cancel', cancellationHandler); + } + } +} + +let socks: SocksLib | null = null; +function loadSocks() { + if (socks == null) { + const socksImport = getSocks(); + if ('kModuleError' in socksImport) { + throw socksImport.kModuleError; + } + socks = socksImport; + } + return socks; +} + +async function makeSocks5Connection(options: MakeConnectionOptions): Promise { + const hostAddress = HostAddress.fromHostPort( + options.proxyHost ?? '', // proxyHost is guaranteed to set here + options.proxyPort ?? 1080 + ); + + // First, connect to the proxy server itself: + const rawSocket = await makeSocket({ + ...options, + hostAddress, + tls: false, + proxyHost: undefined + }); + + const destination = parseConnectOptions(options) as net.TcpNetConnectOpts; + if (typeof destination.host !== 'string' || typeof destination.port !== 'number') { + throw new MongoInvalidArgumentError('Can only make Socks5 connections to TCP hosts'); + } + + socks ??= loadSocks(); + + let existingSocket: Stream; + + try { + // Then, establish the Socks5 proxy connection: + const connection = await socks.SocksClient.createConnection({ + existing_socket: rawSocket, + timeout: options.connectTimeoutMS, + command: 'connect', + destination: { + host: destination.host, + port: destination.port + }, + proxy: { + // host and port are ignored because we pass existing_socket + host: 'iLoveJavaScript', + port: 0, + type: 5, + userId: options.proxyUsername || undefined, + password: options.proxyPassword || undefined + } + }); + existingSocket = connection.socket; + } catch (cause) { + throw new MongoNetworkError(MongoError.buildErrorMessage(cause), { cause }); + } + + // Finally, now treat the resulting duplex stream as the + // socket over which we send and receive wire protocol messages: + return await makeSocket({ ...options, existingSocket, proxyHost: undefined }); +} diff --git a/gateway/node_modules/mongodb/src/cmap/connection.ts b/gateway/node_modules/mongodb/src/cmap/connection.ts new file mode 100644 index 0000000000000000000000000000000000000000..0c099b8bd8ce9d6f5792d6fe9664ab6f4535548f --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/connection.ts @@ -0,0 +1,938 @@ +import { type Readable, Transform, type TransformCallback } from 'stream'; +import { clearTimeout, setTimeout } from 'timers'; + +import { + type BSONSerializeOptions, + ByteUtils, + deserialize, + type DeserializeOptions, + type Document, + type ObjectId +} from '../bson'; +import { type AutoEncrypter } from '../client-side-encryption/auto_encrypter'; +import { + CLOSE, + CLUSTER_TIME_RECEIVED, + COMMAND_FAILED, + COMMAND_STARTED, + COMMAND_SUCCEEDED, + kDecorateResult, + PINNED, + UNPINNED +} from '../constants'; +import { + MongoCompatibilityError, + MONGODB_ERROR_CODES, + MongoNetworkError, + MongoNetworkTimeoutError, + MongoOperationTimeoutError, + MongoParseError, + MongoRuntimeError, + MongoServerError, + MongoUnexpectedServerResponseError +} from '../error'; +import type { ServerApi, SupportedNodeConnectionOptions } from '../mongo_client'; +import { type MongoClientAuthProviders } from '../mongo_client_auth_providers'; +import { MongoLoggableComponent, type MongoLogger, SeverityLevel } from '../mongo_logger'; +import { type Abortable, type CancellationToken, TypedEventEmitter } from '../mongo_types'; +import { ReadPreference, type ReadPreferenceLike } from '../read_preference'; +import { type Runtime } from '../runtime_adapters'; +import { ServerType } from '../sdam/common'; +import { applySession, type ClientSession, updateSessionFromResponse } from '../sessions'; +import { type TimeoutContext, TimeoutError } from '../timeout'; +import { + BufferPool, + calculateDurationInMs, + type Callback, + decorateDecryptionResult, + HostAddress, + maxWireVersion, + type MongoDBNamespace, + noop, + once, + processTimeMS, + squashError, + uuidV4 +} from '../utils'; +import type { WriteConcern } from '../write_concern'; +import type { AuthContext } from './auth/auth_provider'; +import type { MongoCredentials } from './auth/mongo_credentials'; +import { + CommandFailedEvent, + CommandStartedEvent, + CommandSucceededEvent +} from './command_monitoring_events'; +import { + OpCompressedRequest, + OpMsgRequest, + type OpMsgResponse, + OpQueryRequest, + type OpReply, + type WriteProtocolMessageType +} from './commands'; +import type { Stream } from './connect'; +import type { ClientMetadata } from './handshake/client_metadata'; +import { StreamDescription, type StreamDescriptionOptions } from './stream_description'; +import { type CompressorName, decompressResponse } from './wire_protocol/compression'; +import { onData } from './wire_protocol/on_data'; +import { + CursorResponse, + MongoDBResponse, + type MongoDBResponseConstructor +} from './wire_protocol/responses'; +import { getReadPreference, isSharded } from './wire_protocol/shared'; + +/** @internal */ +export interface CommandOptions extends BSONSerializeOptions { + secondaryOk?: boolean; + /** Specify read preference if command supports it */ + readPreference?: ReadPreferenceLike; + monitoring?: boolean; + socketTimeoutMS?: number; + /** Session to use for the operation */ + session?: ClientSession; + documentsReturnedIn?: string; + omitMaxTimeMS?: boolean; + + // TODO(NODE-2802): Currently the CommandOptions take a property willRetryWrite which is a hint + // from executeOperation that the txnNum should be applied to this command. + // Applying a session to a command should happen as part of command construction, + // most likely in the CommandOperation#executeCommand method, where we have access to + // the details we need to determine if a txnNum should also be applied. + willRetryWrite?: boolean; + + writeConcern?: WriteConcern; + + directConnection?: boolean; + + /** @internal */ + timeoutContext?: TimeoutContext; +} + +/** @public */ +export interface ProxyOptions { + proxyHost?: string; + proxyPort?: number; + proxyUsername?: string; + proxyPassword?: string; +} + +/** @public */ +export interface ConnectionOptions + extends SupportedNodeConnectionOptions, + StreamDescriptionOptions, + ProxyOptions { + // Internal creation info + id: number | ''; + generation: number; + hostAddress: HostAddress; + /** @internal */ + autoEncrypter?: AutoEncrypter; + serverApi?: ServerApi; + monitorCommands: boolean; + /** @internal */ + connectionType?: any; + credentials?: MongoCredentials; + /** @internal */ + authProviders: MongoClientAuthProviders; + connectTimeoutMS?: number; + tls: boolean; + noDelay?: boolean; + socketTimeoutMS?: number; + /** @internal */ + cancellationToken?: CancellationToken; + /** @internal */ + metadata: Promise; + /** @internal */ + mongoLogger?: MongoLogger | undefined; + /** @internal */ + runtime: Runtime; +} + +/** @public */ +export type ConnectionEvents = { + commandStarted(event: CommandStartedEvent): void; + commandSucceeded(event: CommandSucceededEvent): void; + commandFailed(event: CommandFailedEvent): void; + clusterTimeReceived(clusterTime: Document): void; + close(): void; + pinned(pinType: string): void; + unpinned(pinType: string): void; +}; + +/** @internal */ +export function hasSessionSupport(conn: Connection): boolean { + const description = conn.description; + return description.logicalSessionTimeoutMinutes != null; +} + +function streamIdentifier(stream: Stream, options: ConnectionOptions): string { + if (options.proxyHost) { + // If proxy options are specified, the properties of `stream` itself + // will not accurately reflect what endpoint this is connected to. + return options.hostAddress.toString(); + } + + const { remoteAddress, remotePort } = stream; + if (typeof remoteAddress === 'string' && typeof remotePort === 'number') { + return HostAddress.fromHostPort(remoteAddress, remotePort).toString(); + } + + return ByteUtils.toHex(uuidV4()); +} + +/** @internal */ +export class Connection extends TypedEventEmitter { + public id: number | ''; + public address: string; + public lastHelloMS = -1; + public serverApi?: ServerApi; + public helloOk = false; + public authContext?: AuthContext; + public delayedTimeoutId: NodeJS.Timeout | null = null; + public generation: number; + public accessToken?: string; + public readonly description: Readonly; + /** + * Represents if the connection has been established: + * - TCP handshake + * - TLS negotiated + * - mongodb handshake (saslStart, saslContinue), includes authentication + * + * Once connection is established, command logging can log events (if enabled) + */ + public established: boolean; + /** Indicates that the connection (including underlying TCP socket) has been closed. */ + public closed = false; + + private lastUseTime: number; + private clusterTime: Document | null = null; + private error: Error | null = null; + private dataEvents: AsyncGenerator | null = null; + + private readonly socketTimeoutMS: number; + private readonly monitorCommands: boolean; + private readonly socket: Stream; + private readonly messageStream: Readable; + + /** @event */ + static readonly COMMAND_STARTED = COMMAND_STARTED; + /** @event */ + static readonly COMMAND_SUCCEEDED = COMMAND_SUCCEEDED; + /** @event */ + static readonly COMMAND_FAILED = COMMAND_FAILED; + /** @event */ + static readonly CLUSTER_TIME_RECEIVED = CLUSTER_TIME_RECEIVED; + /** @event */ + static readonly CLOSE = CLOSE; + /** @event */ + static readonly PINNED = PINNED; + /** @event */ + static readonly UNPINNED = UNPINNED; + + constructor(stream: Stream, options: ConnectionOptions) { + super(); + this.on('error', noop); + + this.socket = stream; + this.id = options.id; + this.address = streamIdentifier(stream, options); + this.socketTimeoutMS = options.socketTimeoutMS ?? 0; + this.monitorCommands = options.monitorCommands; + this.serverApi = options.serverApi; + this.mongoLogger = options.mongoLogger; + this.established = false; + + this.description = new StreamDescription(this.address, options); + this.generation = options.generation; + this.lastUseTime = processTimeMS(); + + this.messageStream = this.socket + .on('error', this.onSocketError.bind(this)) + .pipe(new SizedMessageTransform({ connection: this })) + .on('error', this.onTransformError.bind(this)); + this.socket.on('close', this.onClose.bind(this)); + this.socket.on('timeout', this.onTimeout.bind(this)); + + this.messageStream.pause(); + } + + public get hello() { + return this.description.hello; + } + + // the `connect` method stores the result of the handshake hello on the connection + public set hello(response: Document | null) { + this.description.receiveResponse(response); + Object.freeze(this.description); + } + + public get serviceId(): ObjectId | undefined { + return this.hello?.serviceId; + } + + public get loadBalanced(): boolean { + return this.description.loadBalanced; + } + + public get idleTime(): number { + return calculateDurationInMs(this.lastUseTime); + } + + private get hasSessionSupport(): boolean { + return this.description.logicalSessionTimeoutMinutes != null; + } + + private get supportsOpMsg(): boolean { + return ( + this.description != null && + // TODO(NODE-6672,NODE-6287): This guard is primarily for maxWireVersion = 0 + maxWireVersion(this) >= 6 && + !this.description.__nodejs_mock_server__ + ); + } + + private get shouldEmitAndLogCommand(): boolean { + return ( + (this.monitorCommands || + (this.established && + !this.authContext?.reauthenticating && + this.mongoLogger?.willLog(MongoLoggableComponent.COMMAND, SeverityLevel.DEBUG))) ?? + false + ); + } + + public markAvailable(): void { + this.lastUseTime = processTimeMS(); + } + + private onSocketError(cause: Error) { + this.onError(new MongoNetworkError(cause.message, { cause })); + } + + private onTransformError(error: Error) { + this.onError(error); + } + + public onError(error: Error) { + this.cleanup(error); + } + + private onClose() { + const message = `connection ${this.id} to ${this.address} closed`; + this.cleanup(new MongoNetworkError(message)); + } + + private onTimeout() { + this.delayedTimeoutId = setTimeout(() => { + const message = `connection ${this.id} to ${this.address} timed out`; + const beforeHandshake = this.hello == null; + this.cleanup(new MongoNetworkTimeoutError(message, { beforeHandshake })); + }, 1).unref(); // No need for this timer to hold the event loop open + } + + public destroy(): void { + if (this.closed) { + return; + } + + // load balanced mode requires that these listeners remain on the connection + // after cleanup on timeouts, errors or close so we remove them before calling + // cleanup. + this.removeAllListeners(Connection.PINNED); + this.removeAllListeners(Connection.UNPINNED); + const message = `connection ${this.id} to ${this.address} closed`; + this.cleanup(new MongoNetworkError(message)); + } + + /** + * A method that cleans up the connection. When `force` is true, this method + * forcibly destroys the socket. + * + * If an error is provided, any in-flight operations will be closed with the error. + * + * This method does nothing if the connection is already closed. + */ + private cleanup(error: Error): void { + if (this.closed) { + return; + } + + this.socket.destroy(); + this.error = error; + + this.dataEvents?.throw(error).then(undefined, squashError); + this.closed = true; + this.emit(Connection.CLOSE); + } + + private prepareCommand(db: string, command: Document, options: CommandOptions) { + let cmd = { ...command }; + + const readPreference = getReadPreference(options); + const session = options?.session; + + let clusterTime = this.clusterTime; + + if (this.serverApi) { + const { version, strict, deprecationErrors } = this.serverApi; + cmd.apiVersion = version; + if (strict != null) cmd.apiStrict = strict; + if (deprecationErrors != null) cmd.apiDeprecationErrors = deprecationErrors; + } + + if (this.hasSessionSupport && session) { + if ( + session.clusterTime && + clusterTime && + session.clusterTime.clusterTime.greaterThan(clusterTime.clusterTime) + ) { + clusterTime = session.clusterTime; + } + + const sessionError = applySession(session, cmd, options); + if (sessionError) throw sessionError; + } else if (session?.explicit) { + throw new MongoCompatibilityError('Current topology does not support sessions'); + } + + // if we have a known cluster time, gossip it + if (clusterTime) { + cmd.$clusterTime = clusterTime; + } + + // For standalone, drivers MUST NOT set $readPreference. + if (this.description.type !== ServerType.Standalone) { + if ( + !isSharded(this) && + !this.description.loadBalanced && + this.supportsOpMsg && + options.directConnection === true && + readPreference?.mode === 'primary' + ) { + // For mongos and load balancers with 'primary' mode, drivers MUST NOT set $readPreference. + // For all other types with a direct connection, if the read preference is 'primary' + // (driver sets 'primary' as default if no read preference is configured), + // the $readPreference MUST be set to 'primaryPreferred' + // to ensure that any server type can handle the request. + cmd.$readPreference = ReadPreference.primaryPreferred.toJSON(); + } else if (isSharded(this) && !this.supportsOpMsg && readPreference?.mode !== 'primary') { + // When sending a read operation via OP_QUERY and the $readPreference modifier, + // the query MUST be provided using the $query modifier. + cmd = { + $query: cmd, + $readPreference: readPreference.toJSON() + }; + } else if (readPreference?.mode !== 'primary') { + // For mode 'primary', drivers MUST NOT set $readPreference. + // For all other read preference modes (i.e. 'secondary', 'primaryPreferred', ...), + // drivers MUST set $readPreference + cmd.$readPreference = readPreference.toJSON(); + } + } + + const commandOptions = { + numberToSkip: 0, + numberToReturn: -1, + checkKeys: false, + // This value is not overridable + secondaryOk: readPreference.secondaryOk(), + ...options + }; + + options.timeoutContext?.addMaxTimeMSToCommand(cmd, options); + + const message = this.supportsOpMsg + ? new OpMsgRequest(db, cmd, commandOptions) + : new OpQueryRequest(db, cmd, commandOptions); + + return message; + } + + private async *sendWire( + message: WriteProtocolMessageType, + options: CommandOptions & Abortable, + responseType?: MongoDBResponseConstructor + ): AsyncGenerator { + this.throwIfAborted(); + + const timeout = + options.socketTimeoutMS ?? + options?.timeoutContext?.getSocketTimeoutMS() ?? + this.socketTimeoutMS; + this.socket.setTimeout(timeout); + + try { + await this.writeCommand(message, { + agreedCompressor: this.description.compressor ?? 'none', + zlibCompressionLevel: this.description.zlibCompressionLevel, + timeoutContext: options.timeoutContext, + signal: options.signal + }); + + if (message.moreToCome) { + yield MongoDBResponse.empty; + return; + } + + this.throwIfAborted(); + + if ( + options.timeoutContext?.csotEnabled() && + options.timeoutContext.minRoundTripTime != null && + options.timeoutContext.remainingTimeMS < options.timeoutContext.minRoundTripTime + ) { + throw new MongoOperationTimeoutError( + 'Server roundtrip time is greater than the time remaining' + ); + } + + for await (const response of this.readMany(options)) { + this.socket.setTimeout(0); + const bson = response.parse(); + + const document = (responseType ?? MongoDBResponse).make(bson); + + yield document; + this.throwIfAborted(); + + this.socket.setTimeout(timeout); + } + } finally { + this.socket.setTimeout(0); + } + } + + private async *sendCommand( + ns: MongoDBNamespace, + command: Document, + options: CommandOptions & Abortable, + responseType?: MongoDBResponseConstructor + ) { + options?.signal?.throwIfAborted(); + + const message = this.prepareCommand(ns.db, command, options); + let started = 0; + if (this.shouldEmitAndLogCommand) { + started = processTimeMS(); + this.emitAndLogCommand( + this.monitorCommands, + Connection.COMMAND_STARTED, + message.databaseName, + this.established, + new CommandStartedEvent(this, message, this.description.serverConnectionId) + ); + } + + // If `documentsReturnedIn` not set or raw is not enabled, use input bson options + // Otherwise, support raw flag. Raw only works for cursors that hardcode firstBatch/nextBatch fields + const bsonOptions: DeserializeOptions = + options.documentsReturnedIn == null || !options.raw + ? options + : { + ...options, + raw: false, + fieldsAsRaw: { [options.documentsReturnedIn]: true } + }; + + /** MongoDBResponse instance or subclass */ + let document: MongoDBResponse | undefined = undefined; + /** Cached result of a toObject call */ + let object: Document | undefined = undefined; + try { + this.throwIfAborted(); + for await (document of this.sendWire(message, options, responseType)) { + object = undefined; + if (options.session != null) { + updateSessionFromResponse(options.session, document); + } + + if (document.$clusterTime) { + this.clusterTime = document.$clusterTime; + this.emit(Connection.CLUSTER_TIME_RECEIVED, document.$clusterTime); + } + + if (document.ok === 0) { + if (options.timeoutContext?.csotEnabled() && document.isMaxTimeExpiredError) { + throw new MongoOperationTimeoutError('Server reported a timeout error', { + cause: new MongoServerError((object ??= document.toObject(bsonOptions))) + }); + } + throw new MongoServerError((object ??= document.toObject(bsonOptions))); + } + + if (this.shouldEmitAndLogCommand) { + this.emitAndLogCommand( + this.monitorCommands, + Connection.COMMAND_SUCCEEDED, + message.databaseName, + this.established, + new CommandSucceededEvent( + this, + message, + message.moreToCome ? { ok: 1 } : (object ??= document.toObject(bsonOptions)), + started, + this.description.serverConnectionId + ) + ); + } + + if (responseType == null) { + yield (object ??= document.toObject(bsonOptions)); + } else { + yield document; + } + + this.throwIfAborted(); + } + } catch (error) { + if (options.session != null && !(error instanceof MongoServerError)) { + updateSessionFromResponse(options.session, MongoDBResponse.empty); + } + if (this.shouldEmitAndLogCommand) { + this.emitAndLogCommand( + this.monitorCommands, + Connection.COMMAND_FAILED, + message.databaseName, + this.established, + new CommandFailedEvent(this, message, error, started, this.description.serverConnectionId) + ); + } + throw error; + } + } + + public async command( + ns: MongoDBNamespace, + command: Document, + options: CommandOptions | undefined, + responseType: T + ): Promise>; + + public async command( + ns: MongoDBNamespace, + command: Document, + options: CommandOptions | undefined, + responseType: T | undefined + ): Promise>; + + public async command( + ns: MongoDBNamespace, + command: Document, + options?: CommandOptions + ): Promise; + + public async command( + ns: MongoDBNamespace, + command: Document, + options: CommandOptions & Abortable = {}, + responseType?: MongoDBResponseConstructor + ): Promise { + this.throwIfAborted(); + options.signal?.throwIfAborted(); + + for await (const document of this.sendCommand(ns, command, options, responseType)) { + if (options.timeoutContext?.csotEnabled()) { + if (MongoDBResponse.is(document)) { + if (document.isMaxTimeExpiredError) { + throw new MongoOperationTimeoutError('Server reported a timeout error', { + cause: new MongoServerError(document.toObject()) + }); + } + } else { + if ( + (Array.isArray(document?.writeErrors) && + document.writeErrors.some( + error => error?.code === MONGODB_ERROR_CODES.MaxTimeMSExpired + )) || + document?.writeConcernError?.code === MONGODB_ERROR_CODES.MaxTimeMSExpired + ) { + throw new MongoOperationTimeoutError('Server reported a timeout error', { + cause: new MongoServerError(document) + }); + } + } + } + + return document; + } + throw new MongoUnexpectedServerResponseError('Unable to get response from server'); + } + + public exhaustCommand( + ns: MongoDBNamespace, + command: Document, + options: CommandOptions, + replyListener: Callback + ) { + const exhaustLoop = async () => { + this.throwIfAborted(); + for await (const reply of this.sendCommand(ns, command, options)) { + replyListener(undefined, reply); + this.throwIfAborted(); + } + throw new MongoUnexpectedServerResponseError('Server ended moreToCome unexpectedly'); + }; + + exhaustLoop().then(undefined, replyListener); + } + + private throwIfAborted() { + if (this.error) throw this.error; + } + + /** + * @internal + * + * Writes an OP_MSG or OP_QUERY request to the socket, optionally compressing the command. This method + * waits until the socket's buffer has emptied (the Nodejs socket `drain` event has fired). + */ + private async writeCommand( + command: WriteProtocolMessageType, + options: { + agreedCompressor?: CompressorName; + zlibCompressionLevel?: number; + timeoutContext?: TimeoutContext; + } & Abortable + ): Promise { + const finalCommand = + options.agreedCompressor === 'none' || !OpCompressedRequest.canCompress(command) + ? command + : new OpCompressedRequest(command, { + agreedCompressor: options.agreedCompressor ?? 'none', + zlibCompressionLevel: options.zlibCompressionLevel ?? 0 + }); + + const buffer = ByteUtils.concat(await finalCommand.toBin()); + + if (options.timeoutContext?.csotEnabled()) { + if ( + options.timeoutContext.minRoundTripTime != null && + options.timeoutContext.remainingTimeMS < options.timeoutContext.minRoundTripTime + ) { + throw new MongoOperationTimeoutError( + 'Server roundtrip time is greater than the time remaining' + ); + } + } + + try { + if (this.socket.write(buffer)) return; + } catch (writeError) { + const networkError = new MongoNetworkError('unexpected error writing to socket', { + cause: writeError + }); + this.onError(networkError); + throw networkError; + } + + const drainEvent = once(this.socket, 'drain', options); + const timeout = options?.timeoutContext?.timeoutForSocketWrite; + const drained = timeout ? Promise.race([drainEvent, timeout]) : drainEvent; + try { + return await drained; + } catch (writeError) { + if (TimeoutError.is(writeError)) { + const timeoutError = new MongoOperationTimeoutError('Timed out at socket write'); + this.onError(timeoutError); + throw timeoutError; + } else if (writeError === options.signal?.reason) { + this.onError(writeError); + } + throw writeError; + } finally { + timeout?.clear(); + } + } + + /** + * @internal + * + * Returns an async generator that yields full wire protocol messages from the underlying socket. This function + * yields messages until `moreToCome` is false or not present in a response, or the caller cancels the request + * by calling `return` on the generator. + * + * Note that `for-await` loops call `return` automatically when the loop is exited. + */ + private async *readMany( + options: { + timeoutContext?: TimeoutContext; + } & Abortable + ): AsyncGenerator { + try { + this.dataEvents = onData(this.messageStream, options); + this.messageStream.resume(); + + for await (const message of this.dataEvents) { + const response = await decompressResponse(message); + yield response; + + if (!response.moreToCome) { + return; + } + } + } catch (readError) { + if (TimeoutError.is(readError)) { + const timeoutError = new MongoOperationTimeoutError( + `Timed out during socket read (${readError.duration}ms)` + ); + this.dataEvents = null; + this.onError(timeoutError); + throw timeoutError; + } else if (readError === options.signal?.reason) { + this.onError(readError); + } + throw readError; + } finally { + this.dataEvents = null; + this.messageStream.pause(); + } + } +} + +/** @internal */ +export class SizedMessageTransform extends Transform { + bufferPool: BufferPool; + connection: Connection; + + constructor({ connection }: { connection: Connection }) { + super({ writableObjectMode: false, readableObjectMode: true }); + this.bufferPool = new BufferPool(); + this.connection = connection; + } + + override _transform(chunk: Uint8Array, encoding: unknown, callback: TransformCallback): void { + if (this.connection.delayedTimeoutId != null) { + clearTimeout(this.connection.delayedTimeoutId); + this.connection.delayedTimeoutId = null; + } + + this.bufferPool.append(chunk); + + while (this.bufferPool.length) { + // While there are any bytes in the buffer + + // Try to fetch a size from the top 4 bytes + const sizeOfMessage = this.bufferPool.getInt32(); + + if (sizeOfMessage == null) { + // Not even an int32 worth of data. Stop the loop, we need more chunks. + break; + } + + if (sizeOfMessage < 0) { + // The size in the message has a negative value, this is probably corruption, throw: + return callback(new MongoParseError(`Message size cannot be negative: ${sizeOfMessage}`)); + } + + if (sizeOfMessage > this.bufferPool.length) { + // We do not have enough bytes to make a sizeOfMessage chunk + break; + } + + // Add a message to the stream + const message = this.bufferPool.read(sizeOfMessage); + + if (!this.push(message)) { + // We only subscribe to data events so we should never get backpressure + // if we do, we do not have the handling for it. + return callback( + new MongoRuntimeError(`SizedMessageTransform does not support backpressure`) + ); + } + } + + callback(); + } +} + +/** @internal */ +export class CryptoConnection extends Connection { + /** @internal */ + autoEncrypter?: AutoEncrypter; + + constructor(stream: Stream, options: ConnectionOptions) { + super(stream, options); + this.autoEncrypter = options.autoEncrypter; + } + + public override async command( + ns: MongoDBNamespace, + command: Document, + options: CommandOptions | undefined, + responseType: T + ): Promise>; + + public override async command( + ns: MongoDBNamespace, + command: Document, + options?: CommandOptions + ): Promise; + + override async command( + ns: MongoDBNamespace, + cmd: Document, + options?: CommandOptions, + responseType?: T + ): Promise { + const { autoEncrypter } = this; + if (!autoEncrypter) { + throw new MongoRuntimeError('No AutoEncrypter available for encryption'); + } + + const serverWireVersion = maxWireVersion(this); + if (serverWireVersion === 0) { + // This means the initial handshake hasn't happened yet + return await super.command(ns, cmd, options, responseType); + } + + // Save sort or indexKeys based on the command being run + // the encrypt API serializes our JS objects to BSON to pass to the native code layer + // and then deserializes the encrypted result, the protocol level components + // of the command (ex. sort) are then converted to JS objects potentially losing + // import key order information. These fields are never encrypted so we can save the values + // from before the encryption and replace them after encryption has been performed + const sort: Map | null = cmd.find || cmd.findAndModify ? cmd.sort : null; + const indexKeys: Map[] | null = cmd.createIndexes + ? cmd.indexes.map((index: { key: Map }) => index.key) + : null; + + const encrypted = await autoEncrypter.encrypt(ns.toString(), cmd, options); + + // Replace the saved values + if (sort != null && (cmd.find || cmd.findAndModify)) { + encrypted.sort = sort; + } + + if (indexKeys != null && cmd.createIndexes) { + for (const [offset, index] of indexKeys.entries()) { + // @ts-expect-error `encrypted` is a generic "command", but we've narrowed for only `createIndexes` commands here + encrypted.indexes[offset].key = index; + } + } + + const encryptedResponse = await super.command( + ns, + encrypted, + options, + // Eventually we want to require `responseType` which means we would satisfy `T` as the return type. + // In the meantime, we want encryptedResponse to always be _at least_ a MongoDBResponse if not a more specific subclass + // So that we can ensure we have access to the on-demand APIs for decorate response + responseType ?? MongoDBResponse + ); + + const result = await autoEncrypter.decrypt(encryptedResponse.toBytes(), options); + + const decryptedResponse = responseType?.make(result) ?? deserialize(result, options); + + if (autoEncrypter[kDecorateResult]) { + if (responseType == null) { + decorateDecryptionResult(decryptedResponse, encryptedResponse.toObject(), true); + } else if (decryptedResponse instanceof CursorResponse) { + decryptedResponse.encryptedResponse = encryptedResponse; + } + } + + return decryptedResponse; + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/connection_pool.ts b/gateway/node_modules/mongodb/src/cmap/connection_pool.ts new file mode 100644 index 0000000000000000000000000000000000000000..0df9f5d91bcb8903d61fb96d1c54e6a0df992761 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/connection_pool.ts @@ -0,0 +1,831 @@ +import { clearTimeout, setTimeout } from 'timers'; + +import type { ObjectId } from '../bson'; +import { + APM_EVENTS, + CONNECTION_CHECK_OUT_FAILED, + CONNECTION_CHECK_OUT_STARTED, + CONNECTION_CHECKED_IN, + CONNECTION_CHECKED_OUT, + CONNECTION_CLOSED, + CONNECTION_CREATED, + CONNECTION_POOL_CLEARED, + CONNECTION_POOL_CLOSED, + CONNECTION_POOL_CREATED, + CONNECTION_POOL_READY, + CONNECTION_READY +} from '../constants'; +import { + type AnyError, + MongoClientClosedError, + type MongoError, + MongoInvalidArgumentError, + MongoMissingCredentialsError, + MongoNetworkError, + MongoOperationTimeoutError, + MongoRuntimeError, + MongoServerError +} from '../error'; +import { type Abortable, CancellationToken, TypedEventEmitter } from '../mongo_types'; +import type { Server } from '../sdam/server'; +import { type TimeoutContext, TimeoutError } from '../timeout'; +import { + addAbortListener, + type Callback, + kDispose, + List, + makeCounter, + noop, + processTimeMS, + promiseWithResolvers +} from '../utils'; +import { connect } from './connect'; +import { Connection, type ConnectionEvents, type ConnectionOptions } from './connection'; +import { + ConnectionCheckedInEvent, + ConnectionCheckedOutEvent, + ConnectionCheckOutFailedEvent, + ConnectionCheckOutStartedEvent, + ConnectionClosedEvent, + ConnectionCreatedEvent, + ConnectionPoolClearedEvent, + ConnectionPoolClosedEvent, + ConnectionPoolCreatedEvent, + ConnectionPoolReadyEvent, + ConnectionReadyEvent +} from './connection_pool_events'; +import { + PoolClearedError, + PoolClearedOnNetworkError, + PoolClosedError, + WaitQueueTimeoutError +} from './errors'; +import { ConnectionPoolMetrics } from './metrics'; + +/** @public */ +export interface ConnectionPoolOptions extends Omit { + /** The maximum number of connections that may be associated with a pool at a given time. This includes in use and available connections. */ + maxPoolSize: number; + /** The minimum number of connections that MUST exist at any moment in a single connection pool. */ + minPoolSize: number; + /** The maximum number of connections that may be in the process of being established concurrently by the connection pool. */ + maxConnecting: number; + /** The maximum amount of time a connection should remain idle in the connection pool before being marked idle. */ + maxIdleTimeMS: number; + /** The maximum amount of time operation execution should wait for a connection to become available. The default is 0 which means there is no limit. */ + waitQueueTimeoutMS: number; + /** If we are in load balancer mode. */ + loadBalanced: boolean; + /** @internal */ + minPoolSizeCheckFrequencyMS?: number; +} + +/** @internal */ +export interface WaitQueueMember { + resolve: (conn: Connection) => void; + reject: (err: AnyError) => void; + cancelled: boolean; + checkoutTime: number; +} + +/** @internal */ +export const PoolState = Object.freeze({ + paused: 'paused', + ready: 'ready', + closed: 'closed' +} as const); + +type PoolState = (typeof PoolState)[keyof typeof PoolState]; + +/** @public */ +export type ConnectionPoolEvents = { + connectionPoolCreated(event: ConnectionPoolCreatedEvent): void; + connectionPoolReady(event: ConnectionPoolReadyEvent): void; + connectionPoolClosed(event: ConnectionPoolClosedEvent): void; + connectionPoolCleared(event: ConnectionPoolClearedEvent): void; + connectionCreated(event: ConnectionCreatedEvent): void; + connectionReady(event: ConnectionReadyEvent): void; + connectionClosed(event: ConnectionClosedEvent): void; + connectionCheckOutStarted(event: ConnectionCheckOutStartedEvent): void; + connectionCheckOutFailed(event: ConnectionCheckOutFailedEvent): void; + connectionCheckedOut(event: ConnectionCheckedOutEvent): void; + connectionCheckedIn(event: ConnectionCheckedInEvent): void; +} & Omit; + +/** + * A pool of connections which dynamically resizes, and emit events related to pool activity + * @internal + */ +export class ConnectionPool extends TypedEventEmitter { + public options: Readonly; + /** An integer representing the SDAM generation of the pool */ + public generation: number; + /** A map of generations to service ids */ + public serviceGenerations: Map; + + private poolState: PoolState; + private server: Server; + private connections: List; + private pending: number; + private checkedOut: Set; + private minPoolSizeTimer?: NodeJS.Timeout; + private connectionCounter: Generator; + private cancellationToken: CancellationToken; + private waitQueue: List; + private metrics: ConnectionPoolMetrics; + private processingWaitQueue: boolean; + + /** + * Emitted when the connection pool is created. + * @event + */ + static readonly CONNECTION_POOL_CREATED = CONNECTION_POOL_CREATED; + /** + * Emitted once when the connection pool is closed + * @event + */ + static readonly CONNECTION_POOL_CLOSED = CONNECTION_POOL_CLOSED; + /** + * Emitted each time the connection pool is cleared and it's generation incremented + * @event + */ + static readonly CONNECTION_POOL_CLEARED = CONNECTION_POOL_CLEARED; + /** + * Emitted each time the connection pool is marked ready + * @event + */ + static readonly CONNECTION_POOL_READY = CONNECTION_POOL_READY; + /** + * Emitted when a connection is created. + * @event + */ + static readonly CONNECTION_CREATED = CONNECTION_CREATED; + /** + * Emitted when a connection becomes established, and is ready to use + * @event + */ + static readonly CONNECTION_READY = CONNECTION_READY; + /** + * Emitted when a connection is closed + * @event + */ + static readonly CONNECTION_CLOSED = CONNECTION_CLOSED; + /** + * Emitted when an attempt to check out a connection begins + * @event + */ + static readonly CONNECTION_CHECK_OUT_STARTED = CONNECTION_CHECK_OUT_STARTED; + /** + * Emitted when an attempt to check out a connection fails + * @event + */ + static readonly CONNECTION_CHECK_OUT_FAILED = CONNECTION_CHECK_OUT_FAILED; + /** + * Emitted each time a connection is successfully checked out of the connection pool + * @event + */ + static readonly CONNECTION_CHECKED_OUT = CONNECTION_CHECKED_OUT; + /** + * Emitted each time a connection is successfully checked into the connection pool + * @event + */ + static readonly CONNECTION_CHECKED_IN = CONNECTION_CHECKED_IN; + + constructor(server: Server, options: ConnectionPoolOptions) { + super(); + this.on('error', noop); + + this.options = Object.freeze({ + connectionType: Connection, + ...options, + maxPoolSize: options.maxPoolSize ?? 100, + minPoolSize: options.minPoolSize ?? 0, + maxConnecting: options.maxConnecting ?? 2, + maxIdleTimeMS: options.maxIdleTimeMS ?? 0, + waitQueueTimeoutMS: options.waitQueueTimeoutMS ?? 0, + minPoolSizeCheckFrequencyMS: options.minPoolSizeCheckFrequencyMS ?? 100, + autoEncrypter: options.autoEncrypter + }); + + if (this.options.minPoolSize > this.options.maxPoolSize) { + throw new MongoInvalidArgumentError( + 'Connection pool minimum size must not be greater than maximum pool size' + ); + } + + this.poolState = PoolState.paused; + this.server = server; + this.connections = new List(); + this.pending = 0; + this.checkedOut = new Set(); + this.minPoolSizeTimer = undefined; + this.generation = 0; + this.serviceGenerations = new Map(); + this.connectionCounter = makeCounter(1); + this.cancellationToken = new CancellationToken(); + this.cancellationToken.setMaxListeners(Infinity); + this.waitQueue = new List(); + this.metrics = new ConnectionPoolMetrics(); + this.processingWaitQueue = false; + + this.mongoLogger = this.server.topology.client?.mongoLogger; + this.component = 'connection'; + + queueMicrotask(() => { + this.emitAndLog(ConnectionPool.CONNECTION_POOL_CREATED, new ConnectionPoolCreatedEvent(this)); + }); + } + + /** The address of the endpoint the pool is connected to */ + get address(): string { + return this.options.hostAddress.toString(); + } + + /** + * Check if the pool has been closed + * + * TODO(NODE-3263): We can remove this property once shell no longer needs it + */ + get closed(): boolean { + return this.poolState === PoolState.closed; + } + + /** An integer expressing how many total connections (available + pending + in use) the pool currently has */ + get totalConnectionCount(): number { + return ( + this.availableConnectionCount + this.pendingConnectionCount + this.currentCheckedOutCount + ); + } + + /** An integer expressing how many connections are currently available in the pool. */ + get availableConnectionCount(): number { + return this.connections.length; + } + + get pendingConnectionCount(): number { + return this.pending; + } + + get currentCheckedOutCount(): number { + return this.checkedOut.size; + } + + get waitQueueSize(): number { + return this.waitQueue.length; + } + + get loadBalanced(): boolean { + return this.options.loadBalanced; + } + + get serverError() { + return this.server.description.error; + } + + /** + * This is exposed ONLY for use in mongosh, to enable + * killing all connections if a user quits the shell with + * operations in progress. + * + * This property may be removed as a part of NODE-3263. + */ + get checkedOutConnections() { + return this.checkedOut; + } + + /** + * Get the metrics information for the pool when a wait queue timeout occurs. + */ + private waitQueueErrorMetrics(): string { + return this.metrics.info(this.options.maxPoolSize); + } + + /** + * Set the pool state to "ready" + */ + ready(): void { + if (this.poolState !== PoolState.paused) { + return; + } + this.poolState = PoolState.ready; + this.emitAndLog(ConnectionPool.CONNECTION_POOL_READY, new ConnectionPoolReadyEvent(this)); + clearTimeout(this.minPoolSizeTimer); + this.ensureMinPoolSize(); + } + + /** + * Check a connection out of this pool. The connection will continue to be tracked, but no reference to it + * will be held by the pool. This means that if a connection is checked out it MUST be checked back in or + * explicitly destroyed by the new owner. + */ + async checkOut(options: { timeoutContext: TimeoutContext } & Abortable): Promise { + const checkoutTime = processTimeMS(); + this.emitAndLog( + ConnectionPool.CONNECTION_CHECK_OUT_STARTED, + new ConnectionCheckOutStartedEvent(this) + ); + + const { promise, resolve, reject } = promiseWithResolvers(); + + const timeout = options.timeoutContext.connectionCheckoutTimeout; + + const waitQueueMember: WaitQueueMember = { + resolve, + reject, + cancelled: false, + checkoutTime + }; + + const abortListener = addAbortListener(options.signal, function () { + waitQueueMember.cancelled = true; + reject(this.reason); + }); + + this.waitQueue.push(waitQueueMember); + queueMicrotask(() => this.processWaitQueue()); + + try { + timeout?.throwIfExpired(); + return await (timeout ? Promise.race([promise, timeout]) : promise); + } catch (error) { + if (TimeoutError.is(error)) { + timeout?.clear(); + waitQueueMember.cancelled = true; + + this.emitAndLog( + ConnectionPool.CONNECTION_CHECK_OUT_FAILED, + new ConnectionCheckOutFailedEvent(this, 'timeout', waitQueueMember.checkoutTime) + ); + const timeoutError = new WaitQueueTimeoutError( + this.loadBalanced + ? this.waitQueueErrorMetrics() + : 'Timed out while checking out a connection from connection pool', + this.address + ); + if (options.timeoutContext.csotEnabled()) { + throw new MongoOperationTimeoutError('Timed out during connection checkout', { + cause: timeoutError + }); + } + throw timeoutError; + } + throw error; + } finally { + abortListener?.[kDispose](); + timeout?.clear(); + } + } + + /** + * Check a connection into the pool. + * + * @param connection - The connection to check in + */ + checkIn(connection: Connection): void { + if (!this.checkedOut.has(connection)) { + return; + } + const poolClosed = this.closed; + const stale = this.connectionIsStale(connection); + const willDestroy = !!(poolClosed || stale || connection.closed); + + if (!willDestroy) { + connection.markAvailable(); + this.connections.unshift(connection); + } + + this.checkedOut.delete(connection); + this.emitAndLog( + ConnectionPool.CONNECTION_CHECKED_IN, + new ConnectionCheckedInEvent(this, connection) + ); + + if (willDestroy) { + const reason = connection.closed ? 'error' : poolClosed ? 'poolClosed' : 'stale'; + this.destroyConnection(connection, reason); + } + + queueMicrotask(() => this.processWaitQueue()); + } + + /** + * Clear the pool + * + * Pool reset is handled by incrementing the pool's generation count. Any existing connection of a + * previous generation will eventually be pruned during subsequent checkouts. + */ + clear(options: { serviceId?: ObjectId; interruptInUseConnections?: boolean } = {}): void { + if (this.closed) { + return; + } + + // handle load balanced case + if (this.loadBalanced) { + const { serviceId } = options; + if (!serviceId) { + throw new MongoRuntimeError( + 'ConnectionPool.clear() called in load balanced mode with no serviceId.' + ); + } + const sid = serviceId.toHexString(); + const generation = this.serviceGenerations.get(sid); + // Only need to worry if the generation exists, since it should + // always be there but typescript needs the check. + if (generation == null) { + throw new MongoRuntimeError('Service generations are required in load balancer mode.'); + } else { + // Increment the generation for the service id. + this.serviceGenerations.set(sid, generation + 1); + } + this.emitAndLog( + ConnectionPool.CONNECTION_POOL_CLEARED, + new ConnectionPoolClearedEvent(this, { serviceId }) + ); + return; + } + // handle non load-balanced case + const interruptInUseConnections = options.interruptInUseConnections ?? false; + const oldGeneration = this.generation; + this.generation += 1; + const alreadyPaused = this.poolState === PoolState.paused; + this.poolState = PoolState.paused; + + this.clearMinPoolSizeTimer(); + if (!alreadyPaused) { + this.emitAndLog( + ConnectionPool.CONNECTION_POOL_CLEARED, + new ConnectionPoolClearedEvent(this, { + interruptInUseConnections + }) + ); + } + + if (interruptInUseConnections) { + queueMicrotask(() => this.interruptInUseConnections(oldGeneration)); + } + + this.processWaitQueue(); + } + + /** + * Closes all stale in-use connections in the pool with a resumable PoolClearedOnNetworkError. + * + * Only connections where `connection.generation <= minGeneration` are killed. + */ + private interruptInUseConnections(minGeneration: number) { + for (const connection of this.checkedOut) { + if (connection.generation <= minGeneration) { + connection.onError(new PoolClearedOnNetworkError(this)); + } + } + } + + /** For MongoClient.close() procedures */ + public closeCheckedOutConnections() { + for (const conn of this.checkedOut) { + conn.onError(new MongoClientClosedError()); + } + } + + /** Close the pool */ + close(): void { + if (this.closed) { + return; + } + + // immediately cancel any in-flight connections + this.cancellationToken.emit('cancel'); + + // end the connection counter + if (typeof this.connectionCounter.return === 'function') { + this.connectionCounter.return(undefined); + } + + this.poolState = PoolState.closed; + this.clearMinPoolSizeTimer(); + this.processWaitQueue(); + + for (const conn of this.connections) { + this.emitAndLog( + ConnectionPool.CONNECTION_CLOSED, + new ConnectionClosedEvent(this, conn, 'poolClosed') + ); + conn.destroy(); + } + this.connections.clear(); + this.emitAndLog(ConnectionPool.CONNECTION_POOL_CLOSED, new ConnectionPoolClosedEvent(this)); + } + + /** + * @internal + * Reauthenticate a connection + */ + async reauthenticate(connection: Connection): Promise { + const authContext = connection.authContext; + if (!authContext) { + throw new MongoRuntimeError('No auth context found on connection.'); + } + const credentials = authContext.credentials; + if (!credentials) { + throw new MongoMissingCredentialsError( + 'Connection is missing credentials when asked to reauthenticate' + ); + } + + const resolvedCredentials = credentials.resolveAuthMechanism(connection.hello); + const provider = this.server.topology.client.s.authProviders.getOrCreateProvider( + resolvedCredentials.mechanism, + resolvedCredentials.mechanismProperties + ); + + if (!provider) { + throw new MongoMissingCredentialsError( + `Reauthenticate failed due to no auth provider for ${credentials.mechanism}` + ); + } + + await provider.reauth(authContext); + + return; + } + + /** Clear the min pool size timer */ + private clearMinPoolSizeTimer(): void { + const minPoolSizeTimer = this.minPoolSizeTimer; + if (minPoolSizeTimer) { + clearTimeout(minPoolSizeTimer); + } + } + + private destroyConnection( + connection: Connection, + reason: 'error' | 'idle' | 'stale' | 'poolClosed' + ) { + this.emitAndLog( + ConnectionPool.CONNECTION_CLOSED, + new ConnectionClosedEvent(this, connection, reason) + ); + // destroy the connection + connection.destroy(); + } + + private connectionIsStale(connection: Connection) { + const serviceId = connection.serviceId; + if (this.loadBalanced && serviceId) { + const sid = serviceId.toHexString(); + const generation = this.serviceGenerations.get(sid); + return connection.generation !== generation; + } + + return connection.generation !== this.generation; + } + + private connectionIsIdle(connection: Connection) { + return !!(this.options.maxIdleTimeMS && connection.idleTime > this.options.maxIdleTimeMS); + } + + /** + * Destroys a connection if the connection is perished. + * + * @returns `true` if the connection was destroyed, `false` otherwise. + */ + private destroyConnectionIfPerished(connection: Connection): boolean { + const isStale = this.connectionIsStale(connection); + const isIdle = this.connectionIsIdle(connection); + if (!isStale && !isIdle && !connection.closed) { + return false; + } + const reason = connection.closed ? 'error' : isStale ? 'stale' : 'idle'; + this.destroyConnection(connection, reason); + return true; + } + + private createConnection(callback: Callback) { + // Note that metadata may have changed on the client but have + // been frozen here, so we pull the metadata promise always from the client + // no matter what options were set at the construction of the pool. + const connectOptions: ConnectionOptions = { + ...this.options, + id: this.connectionCounter.next().value, + generation: this.generation, + cancellationToken: this.cancellationToken, + mongoLogger: this.mongoLogger, + authProviders: this.server.topology.client.s.authProviders, + metadata: this.server.topology.client.options.metadata + }; + + this.pending++; + // This is our version of a "virtual" no-I/O connection as the spec requires + const connectionCreatedTime = processTimeMS(); + this.emitAndLog( + ConnectionPool.CONNECTION_CREATED, + new ConnectionCreatedEvent(this, { id: connectOptions.id }) + ); + + connect(connectOptions).then( + connection => { + // The pool might have closed since we started trying to create a connection + if (this.poolState !== PoolState.ready) { + this.pending--; + connection.destroy(); + callback(this.closed ? new PoolClosedError(this) : new PoolClearedError(this)); + return; + } + + // forward all events from the connection to the pool + for (const event of [...APM_EVENTS, Connection.CLUSTER_TIME_RECEIVED]) { + connection.on(event, (e: any) => this.emit(event, e)); + } + + if (this.loadBalanced) { + connection.on(Connection.PINNED, pinType => this.metrics.markPinned(pinType)); + connection.on(Connection.UNPINNED, pinType => this.metrics.markUnpinned(pinType)); + + const serviceId = connection.serviceId; + if (serviceId) { + let generation; + const sid = serviceId.toHexString(); + if ((generation = this.serviceGenerations.get(sid))) { + connection.generation = generation; + } else { + this.serviceGenerations.set(sid, 0); + connection.generation = 0; + } + } + } + + connection.markAvailable(); + this.emitAndLog( + ConnectionPool.CONNECTION_READY, + new ConnectionReadyEvent(this, connection, connectionCreatedTime) + ); + + this.pending--; + callback(undefined, connection); + }, + error => { + this.pending--; + this.server.handleError(error); + this.emitAndLog( + ConnectionPool.CONNECTION_CLOSED, + new ConnectionClosedEvent( + this, + { id: connectOptions.id, serviceId: undefined }, + 'error', + // TODO(NODE-5192): Remove this cast + error as MongoError + ) + ); + if (error instanceof MongoNetworkError || error instanceof MongoServerError) { + error.connectionGeneration = connectOptions.generation; + } + callback(error ?? new MongoRuntimeError('Connection creation failed without error')); + } + ); + } + + private ensureMinPoolSize() { + const minPoolSize = this.options.minPoolSize; + if (this.poolState !== PoolState.ready) { + return; + } + + this.connections.prune(connection => this.destroyConnectionIfPerished(connection)); + + if ( + this.totalConnectionCount < minPoolSize && + this.pendingConnectionCount < this.options.maxConnecting + ) { + // NOTE: ensureMinPoolSize should not try to get all the pending + // connection permits because that potentially delays the availability of + // the connection to a checkout request + this.createConnection((err, connection) => { + if (!err && connection) { + this.connections.push(connection); + queueMicrotask(() => this.processWaitQueue()); + } + if (this.poolState === PoolState.ready) { + clearTimeout(this.minPoolSizeTimer); + this.minPoolSizeTimer = setTimeout( + () => this.ensureMinPoolSize(), + this.options.minPoolSizeCheckFrequencyMS + ); + } + }); + } else { + clearTimeout(this.minPoolSizeTimer); + this.minPoolSizeTimer = setTimeout( + () => this.ensureMinPoolSize(), + this.options.minPoolSizeCheckFrequencyMS + ); + } + } + + private processWaitQueue() { + if (this.processingWaitQueue) { + return; + } + this.processingWaitQueue = true; + + while (this.waitQueueSize) { + const waitQueueMember = this.waitQueue.first(); + if (!waitQueueMember) { + this.waitQueue.shift(); + continue; + } + + if (waitQueueMember.cancelled) { + this.waitQueue.shift(); + continue; + } + + if (this.poolState !== PoolState.ready) { + const reason = this.closed ? 'poolClosed' : 'connectionError'; + const error = this.closed ? new PoolClosedError(this) : new PoolClearedError(this); + this.emitAndLog( + ConnectionPool.CONNECTION_CHECK_OUT_FAILED, + new ConnectionCheckOutFailedEvent(this, reason, waitQueueMember.checkoutTime, error) + ); + this.waitQueue.shift(); + waitQueueMember.reject(error); + continue; + } + + if (!this.availableConnectionCount) { + break; + } + + const connection = this.connections.shift(); + if (!connection) { + break; + } + + if (!this.destroyConnectionIfPerished(connection)) { + this.checkedOut.add(connection); + this.emitAndLog( + ConnectionPool.CONNECTION_CHECKED_OUT, + new ConnectionCheckedOutEvent(this, connection, waitQueueMember.checkoutTime) + ); + + this.waitQueue.shift(); + waitQueueMember.resolve(connection); + } + } + + const { maxPoolSize, maxConnecting } = this.options; + while ( + this.waitQueueSize > 0 && + this.pendingConnectionCount < maxConnecting && + (maxPoolSize === 0 || this.totalConnectionCount < maxPoolSize) + ) { + const waitQueueMember = this.waitQueue.shift(); + if (!waitQueueMember || waitQueueMember.cancelled) { + continue; + } + this.createConnection((err, connection) => { + if (waitQueueMember.cancelled) { + if (!err && connection) { + this.connections.push(connection); + } + } else { + if (err) { + this.emitAndLog( + ConnectionPool.CONNECTION_CHECK_OUT_FAILED, + // TODO(NODE-5192): Remove this cast + new ConnectionCheckOutFailedEvent( + this, + 'connectionError', + waitQueueMember.checkoutTime, + err as MongoError + ) + ); + waitQueueMember.reject(err); + } else if (connection) { + this.checkedOut.add(connection); + this.emitAndLog( + ConnectionPool.CONNECTION_CHECKED_OUT, + new ConnectionCheckedOutEvent(this, connection, waitQueueMember.checkoutTime) + ); + waitQueueMember.resolve(connection); + } + } + queueMicrotask(() => this.processWaitQueue()); + }); + } + this.processingWaitQueue = false; + } +} + +/** + * A callback provided to `withConnection` + * @internal + * + * @param error - An error instance representing the error during the execution. + * @param connection - The managed connection which was checked out of the pool. + * @param callback - A function to call back after connection management is complete + */ +export type WithConnectionCallback = ( + error: MongoError | undefined, + connection: Connection | undefined, + callback: Callback +) => void; diff --git a/gateway/node_modules/mongodb/src/cmap/connection_pool_events.ts b/gateway/node_modules/mongodb/src/cmap/connection_pool_events.ts new file mode 100644 index 0000000000000000000000000000000000000000..fe07536a6dbac32f581d3ddc863a870c22108f46 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/connection_pool_events.ts @@ -0,0 +1,300 @@ +import type { ObjectId } from '../bson'; +import { + CONNECTION_CHECK_OUT_FAILED, + CONNECTION_CHECK_OUT_STARTED, + CONNECTION_CHECKED_IN, + CONNECTION_CHECKED_OUT, + CONNECTION_CLOSED, + CONNECTION_CREATED, + CONNECTION_POOL_CLEARED, + CONNECTION_POOL_CLOSED, + CONNECTION_POOL_CREATED, + CONNECTION_POOL_READY, + CONNECTION_READY +} from '../constants'; +import type { MongoError } from '../error'; +import { processTimeMS } from '../utils'; +import type { Connection } from './connection'; +import type { ConnectionPool, ConnectionPoolOptions } from './connection_pool'; + +/** + * The base export class for all monitoring events published from the connection pool + * @public + * @category Event + */ +export abstract class ConnectionPoolMonitoringEvent { + /** A timestamp when the event was created */ + time: Date; + /** The address (host/port pair) of the pool */ + address: string; + /** @internal */ + abstract name: + | typeof CONNECTION_CHECK_OUT_FAILED + | typeof CONNECTION_CHECK_OUT_STARTED + | typeof CONNECTION_CHECKED_IN + | typeof CONNECTION_CHECKED_OUT + | typeof CONNECTION_CLOSED + | typeof CONNECTION_CREATED + | typeof CONNECTION_POOL_CLEARED + | typeof CONNECTION_POOL_CLOSED + | typeof CONNECTION_POOL_CREATED + | typeof CONNECTION_POOL_READY + | typeof CONNECTION_READY; + + /** @internal */ + constructor(pool: ConnectionPool) { + this.time = new Date(); + this.address = pool.address; + } +} + +/** + * An event published when a connection pool is created + * @public + * @category Event + */ +export class ConnectionPoolCreatedEvent extends ConnectionPoolMonitoringEvent { + /** The options used to create this connection pool */ + options: Pick< + ConnectionPoolOptions, + 'maxPoolSize' | 'minPoolSize' | 'maxConnecting' | 'maxIdleTimeMS' | 'waitQueueTimeoutMS' + >; + /** @internal */ + name = CONNECTION_POOL_CREATED; + + /** @internal */ + constructor(pool: ConnectionPool) { + super(pool); + const { maxConnecting, maxPoolSize, minPoolSize, maxIdleTimeMS, waitQueueTimeoutMS } = + pool.options; + this.options = { maxConnecting, maxPoolSize, minPoolSize, maxIdleTimeMS, waitQueueTimeoutMS }; + } +} + +/** + * An event published when a connection pool is ready + * @public + * @category Event + */ +export class ConnectionPoolReadyEvent extends ConnectionPoolMonitoringEvent { + /** @internal */ + name = CONNECTION_POOL_READY; + + /** @internal */ + constructor(pool: ConnectionPool) { + super(pool); + } +} + +/** + * An event published when a connection pool is closed + * @public + * @category Event + */ +export class ConnectionPoolClosedEvent extends ConnectionPoolMonitoringEvent { + /** @internal */ + name = CONNECTION_POOL_CLOSED; + + /** @internal */ + constructor(pool: ConnectionPool) { + super(pool); + } +} + +/** + * An event published when a connection pool creates a new connection + * @public + * @category Event + */ +export class ConnectionCreatedEvent extends ConnectionPoolMonitoringEvent { + /** A monotonically increasing, per-pool id for the newly created connection */ + connectionId: number | ''; + /** @internal */ + name = CONNECTION_CREATED; + + /** @internal */ + constructor(pool: ConnectionPool, connection: { id: number | '' }) { + super(pool); + this.connectionId = connection.id; + } +} + +/** + * An event published when a connection is ready for use + * @public + * @category Event + */ +export class ConnectionReadyEvent extends ConnectionPoolMonitoringEvent { + /** The id of the connection */ + connectionId: number | ''; + /** + * The time it took to establish the connection. + * In accordance with the definition of establishment of a connection + * specified by `ConnectionPoolOptions.maxConnecting`, + * it is the time elapsed between emitting a `ConnectionCreatedEvent` + * and emitting this event as part of the same checking out. + * + * Naturally, when establishing a connection is part of checking out, + * this duration is not greater than + * `ConnectionCheckedOutEvent.duration`. + */ + durationMS: number; + /** @internal */ + name = CONNECTION_READY; + + /** @internal */ + constructor(pool: ConnectionPool, connection: Connection, connectionCreatedEventTime: number) { + super(pool); + this.durationMS = processTimeMS() - connectionCreatedEventTime; + this.connectionId = connection.id; + } +} + +/** + * An event published when a connection is closed + * @public + * @category Event + */ +export class ConnectionClosedEvent extends ConnectionPoolMonitoringEvent { + /** The id of the connection */ + connectionId: number | ''; + /** The reason the connection was closed */ + reason: string; + serviceId?: ObjectId; + /** @internal */ + name = CONNECTION_CLOSED; + /** @internal */ + error: MongoError | null; + + /** @internal */ + constructor( + pool: ConnectionPool, + connection: Pick, + reason: 'idle' | 'stale' | 'poolClosed' | 'error', + error?: MongoError + ) { + super(pool); + this.connectionId = connection.id; + this.reason = reason; + this.serviceId = connection.serviceId; + this.error = error ?? null; + } +} + +/** + * An event published when a request to check a connection out begins + * @public + * @category Event + */ +export class ConnectionCheckOutStartedEvent extends ConnectionPoolMonitoringEvent { + /** @internal */ + name = CONNECTION_CHECK_OUT_STARTED; + + /** @internal */ + constructor(pool: ConnectionPool) { + super(pool); + } +} + +/** + * An event published when a request to check a connection out fails + * @public + * @category Event + */ +export class ConnectionCheckOutFailedEvent extends ConnectionPoolMonitoringEvent { + /** The reason the attempt to check out failed */ + reason: string; + /** @internal */ + error?: MongoError; + /** @internal */ + name = CONNECTION_CHECK_OUT_FAILED; + /** + * The time it took to check out the connection. + * More specifically, the time elapsed between + * emitting a `ConnectionCheckOutStartedEvent` + * and emitting this event as part of the same check out. + */ + durationMS: number; + + /** @internal */ + constructor( + pool: ConnectionPool, + reason: 'poolClosed' | 'timeout' | 'connectionError', + checkoutTime: number, + error?: MongoError + ) { + super(pool); + this.durationMS = processTimeMS() - checkoutTime; + this.reason = reason; + this.error = error; + } +} + +/** + * An event published when a connection is checked out of the connection pool + * @public + * @category Event + */ +export class ConnectionCheckedOutEvent extends ConnectionPoolMonitoringEvent { + /** The id of the connection */ + connectionId: number | ''; + /** @internal */ + name = CONNECTION_CHECKED_OUT; + /** + * The time it took to check out the connection. + * More specifically, the time elapsed between + * emitting a `ConnectionCheckOutStartedEvent` + * and emitting this event as part of the same checking out. + * + */ + durationMS: number; + + /** @internal */ + constructor(pool: ConnectionPool, connection: Connection, checkoutTime: number) { + super(pool); + this.durationMS = processTimeMS() - checkoutTime; + this.connectionId = connection.id; + } +} + +/** + * An event published when a connection is checked into the connection pool + * @public + * @category Event + */ +export class ConnectionCheckedInEvent extends ConnectionPoolMonitoringEvent { + /** The id of the connection */ + connectionId: number | ''; + /** @internal */ + name = CONNECTION_CHECKED_IN; + + /** @internal */ + constructor(pool: ConnectionPool, connection: Connection) { + super(pool); + this.connectionId = connection.id; + } +} + +/** + * An event published when a connection pool is cleared + * @public + * @category Event + */ +export class ConnectionPoolClearedEvent extends ConnectionPoolMonitoringEvent { + /** @internal */ + serviceId?: ObjectId; + + interruptInUseConnections?: boolean; + /** @internal */ + name = CONNECTION_POOL_CLEARED; + + /** @internal */ + constructor( + pool: ConnectionPool, + options: { serviceId?: ObjectId; interruptInUseConnections?: boolean } = {} + ) { + super(pool); + this.serviceId = options.serviceId; + this.interruptInUseConnections = options.interruptInUseConnections; + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/errors.ts b/gateway/node_modules/mongodb/src/cmap/errors.ts new file mode 100644 index 0000000000000000000000000000000000000000..4481fd69462b37198612204a79995b64303a4c3a --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/errors.ts @@ -0,0 +1,119 @@ +import { MongoDriverError, MongoErrorLabel, MongoNetworkError } from '../error'; +import type { ConnectionPool } from './connection_pool'; + +/** + * An error indicating a connection pool is closed + * @category Error + */ +export class PoolClosedError extends MongoDriverError { + /** The address of the connection pool */ + address: string; + + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(pool: ConnectionPool) { + super('Attempted to check out a connection from closed connection pool'); + this.address = pool.address; + } + + override get name(): string { + return 'MongoPoolClosedError'; + } +} + +/** + * An error indicating a connection pool is currently paused + * @category Error + */ +export class PoolClearedError extends MongoNetworkError { + /** The address of the connection pool */ + address: string; + + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(pool: ConnectionPool, message?: string) { + const errorMessage = message + ? message + : `Connection pool for ${pool.address} was cleared because another operation failed with: "${pool.serverError?.message}"`; + super(errorMessage, pool.serverError ? { cause: pool.serverError } : undefined); + this.address = pool.address; + + this.addErrorLabel(MongoErrorLabel.PoolRequestedRetry); + } + + override get name(): string { + return 'MongoPoolClearedError'; + } +} + +/** + * An error indicating that a connection pool has been cleared after the monitor for that server timed out. + * @category Error + */ +export class PoolClearedOnNetworkError extends PoolClearedError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(pool: ConnectionPool) { + super(pool, `Connection to ${pool.address} interrupted due to server monitor timeout`); + } + + override get name(): string { + return 'PoolClearedOnNetworkError'; + } +} + +/** + * An error thrown when a request to check out a connection times out + * @category Error + */ +export class WaitQueueTimeoutError extends MongoDriverError { + /** The address of the connection pool */ + address: string; + + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, address: string) { + super(message); + this.address = address; + } + + override get name(): string { + return 'MongoWaitQueueTimeoutError'; + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/handshake/client_metadata.ts b/gateway/node_modules/mongodb/src/cmap/handshake/client_metadata.ts new file mode 100644 index 0000000000000000000000000000000000000000..00780fbe0ffd16067b2f98c8d8e456b24e07342f --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/handshake/client_metadata.ts @@ -0,0 +1,353 @@ +import * as process from 'process'; + +import { BSON, ByteUtils, type Document, Int32, NumberUtils } from '../../bson'; +import { MongoInvalidArgumentError } from '../../error'; +import type { DriverInfo, MongoOptions } from '../../mongo_client'; +import { fileIsAccessible } from '../../utils'; + +// eslint-disable-next-line @typescript-eslint/no-require-imports +const NODE_DRIVER_VERSION = require('../../../package.json').version; + +/** @internal */ +export function isDriverInfoEqual(info1: DriverInfo, info2: DriverInfo): boolean { + /** for equality comparison, we consider "" as unset */ + const nonEmptyCmp = (s1: string | undefined, s2: string | undefined): boolean => { + s1 ||= undefined; + s2 ||= undefined; + + return s1 === s2; + }; + + return ( + nonEmptyCmp(info1.name, info2.name) && + nonEmptyCmp(info1.platform, info2.platform) && + nonEmptyCmp(info1.version, info2.version) + ); +} + +/** + * @internal + * @see https://github.com/mongodb/specifications/blob/master/source/mongodb-handshake/handshake.md#hello-command + */ +export interface ClientMetadata { + driver: { + name: string; + version: string; + }; + os: { + type: string; + name?: NodeJS.Platform; + architecture?: string; + version?: string; + }; + platform: string; + application?: { + name: string; + }; + /** FaaS environment information */ + env?: { + name?: 'aws.lambda' | 'gcp.func' | 'azure.func' | 'vercel'; + timeout_sec?: Int32; + memory_mb?: Int32; + region?: string; + container?: { + runtime?: string; + orchestrator?: string; + }; + }; +} + +/** @internal */ +export class LimitedSizeDocument { + private document = new Map(); + /** BSON overhead: Int32 + Null byte */ + private documentSize = 5; + private maxSize: number; + + constructor(maxSize: number) { + this.maxSize = maxSize; + } + + /** Only adds key/value if the bsonByteLength is less than MAX_SIZE */ + public ifItFitsItSits(key: string, value: Record | string): boolean { + // The BSON byteLength of the new element is the same as serializing it to its own document + // subtracting the document size int32 and the null terminator. + const newElementSize = BSON.serialize(new Map().set(key, value)).byteLength - 5; + + if (newElementSize + this.documentSize > this.maxSize) { + return false; + } + + this.documentSize += newElementSize; + + this.document.set(key, value); + + return true; + } + + toObject(): Document { + return BSON.deserialize(BSON.serialize(this.document), { + promoteLongs: false, + promoteBuffers: false, + promoteValues: false, + useBigInt64: false + }); + } +} + +type MakeClientMetadataOptions = Pick; + +/** + * From the specs: + * Implementors SHOULD cumulatively update fields in the following order until the document is under the size limit: + * 1. Omit fields from `env` except `env.name`. + * 2. Omit fields from `os` except `os.type`. + * 3. Omit the `env` document entirely. + * 4. Truncate `platform`. -- special we do not truncate this field + */ +export async function makeClientMetadata( + driverInfoList: DriverInfo[], + { appName = '', runtime: { os } }: MakeClientMetadataOptions +): Promise { + const metadataDocument = new LimitedSizeDocument(512); + + // Add app name first, it must be sent + if (appName.length > 0) { + const name = + ByteUtils.utf8ByteLength(appName) <= 128 + ? appName + : ByteUtils.toUTF8(ByteUtils.fromUTF8(appName), 0, 128, false); + metadataDocument.ifItFitsItSits('application', { name }); + } + + const driverInfo = { + name: 'nodejs', + version: NODE_DRIVER_VERSION + }; + + // This is where we handle additional driver info added after client construction. + for (const { name: n = '', version: v = '' } of driverInfoList) { + if (n.length > 0) { + driverInfo.name = `${driverInfo.name}|${n}`; + } + if (v.length > 0) { + driverInfo.version = `${driverInfo.version}|${v}`; + } + } + + if (!metadataDocument.ifItFitsItSits('driver', driverInfo)) { + throw new MongoInvalidArgumentError( + 'Unable to include driverInfo name and version, metadata cannot exceed 512 bytes' + ); + } + + let runtimeInfo = getRuntimeInfo(); + // This is where we handle additional driver info added after client construction. + for (const { platform = '' } of driverInfoList) { + if (platform.length > 0) { + runtimeInfo = `${runtimeInfo}|${platform}`; + } + } + + if (!metadataDocument.ifItFitsItSits('platform', runtimeInfo)) { + throw new MongoInvalidArgumentError( + 'Unable to include driverInfo platform, metadata cannot exceed 512 bytes' + ); + } + + // Note: order matters, os.type is last so it will be removed last if we're at maxSize + const osInfo = new Map() + .set('name', os.platform()) + .set('architecture', os.arch()) + .set('version', os.release()) + .set('type', os.type()); + + if (!metadataDocument.ifItFitsItSits('os', osInfo)) { + for (const key of osInfo.keys()) { + osInfo.delete(key); + if (osInfo.size === 0) break; + if (metadataDocument.ifItFitsItSits('os', osInfo)) break; + } + } + + const faasEnv = getFAASEnv(); + if (faasEnv != null) { + if (!metadataDocument.ifItFitsItSits('env', faasEnv)) { + for (const key of faasEnv.keys()) { + faasEnv.delete(key); + if (faasEnv.size === 0) break; + if (metadataDocument.ifItFitsItSits('env', faasEnv)) break; + } + } + } + return await addContainerMetadata(metadataDocument.toObject() as ClientMetadata); +} + +let dockerPromise: Promise; +type ContainerMetadata = NonNullable['container']>; +/** @internal */ +async function getContainerMetadata(): Promise { + dockerPromise ??= fileIsAccessible('/.dockerenv'); + const isDocker = await dockerPromise; + + const { KUBERNETES_SERVICE_HOST = '' } = process.env; + const isKubernetes = KUBERNETES_SERVICE_HOST.length > 0 ? true : false; + + const containerMetadata: ContainerMetadata = {}; + + if (isDocker) containerMetadata.runtime = 'docker'; + if (isKubernetes) containerMetadata.orchestrator = 'kubernetes'; + + return containerMetadata; +} + +/** + * @internal + * Re-add each metadata value. + * Attempt to add new env container metadata, but keep old data if it does not fit. + */ +async function addContainerMetadata(originalMetadata: ClientMetadata): Promise { + const containerMetadata = await getContainerMetadata(); + if (Object.keys(containerMetadata).length === 0) return originalMetadata; + + const extendedMetadata = new LimitedSizeDocument(512); + + const extendedEnvMetadata: NonNullable = { + ...originalMetadata?.env, + container: containerMetadata + }; + + for (const [key, val] of Object.entries(originalMetadata)) { + if (key !== 'env') { + extendedMetadata.ifItFitsItSits(key, val); + } else { + if (!extendedMetadata.ifItFitsItSits('env', extendedEnvMetadata)) { + // add in old data if newer / extended metadata does not fit + extendedMetadata.ifItFitsItSits('env', val); + } + } + } + + if (!('env' in originalMetadata)) { + extendedMetadata.ifItFitsItSits('env', extendedEnvMetadata); + } + + return extendedMetadata.toObject() as ClientMetadata; +} + +/** + * Collects FaaS metadata. + * - `name` MUST be the last key in the Map returned. + */ +export function getFAASEnv(): Map | null { + const { + AWS_EXECUTION_ENV = '', + AWS_LAMBDA_RUNTIME_API = '', + FUNCTIONS_WORKER_RUNTIME = '', + K_SERVICE = '', + FUNCTION_NAME = '', + VERCEL = '', + AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '', + AWS_REGION = '', + FUNCTION_MEMORY_MB = '', + FUNCTION_REGION = '', + FUNCTION_TIMEOUT_SEC = '', + VERCEL_REGION = '' + } = process.env; + + const isAWSFaaS = + AWS_EXECUTION_ENV.startsWith('AWS_Lambda_') || AWS_LAMBDA_RUNTIME_API.length > 0; + const isAzureFaaS = FUNCTIONS_WORKER_RUNTIME.length > 0; + const isGCPFaaS = K_SERVICE.length > 0 || FUNCTION_NAME.length > 0; + const isVercelFaaS = VERCEL.length > 0; + + // Note: order matters, name must always be the last key + const faasEnv = new Map(); + + // When isVercelFaaS is true so is isAWSFaaS; Vercel inherits the AWS env + if (isVercelFaaS && !(isAzureFaaS || isGCPFaaS)) { + if (VERCEL_REGION.length > 0) { + faasEnv.set('region', VERCEL_REGION); + } + + faasEnv.set('name', 'vercel'); + return faasEnv; + } + + if (isAWSFaaS && !(isAzureFaaS || isGCPFaaS || isVercelFaaS)) { + if (AWS_REGION.length > 0) { + faasEnv.set('region', AWS_REGION); + } + + if ( + AWS_LAMBDA_FUNCTION_MEMORY_SIZE.length > 0 && + Number.isInteger(+AWS_LAMBDA_FUNCTION_MEMORY_SIZE) + ) { + faasEnv.set('memory_mb', new Int32(AWS_LAMBDA_FUNCTION_MEMORY_SIZE)); + } + + faasEnv.set('name', 'aws.lambda'); + return faasEnv; + } + + if (isAzureFaaS && !(isGCPFaaS || isAWSFaaS || isVercelFaaS)) { + faasEnv.set('name', 'azure.func'); + return faasEnv; + } + + if (isGCPFaaS && !(isAzureFaaS || isAWSFaaS || isVercelFaaS)) { + if (FUNCTION_REGION.length > 0) { + faasEnv.set('region', FUNCTION_REGION); + } + + if (FUNCTION_MEMORY_MB.length > 0 && Number.isInteger(+FUNCTION_MEMORY_MB)) { + faasEnv.set('memory_mb', new Int32(FUNCTION_MEMORY_MB)); + } + + if (FUNCTION_TIMEOUT_SEC.length > 0 && Number.isInteger(+FUNCTION_TIMEOUT_SEC)) { + faasEnv.set('timeout_sec', new Int32(FUNCTION_TIMEOUT_SEC)); + } + + faasEnv.set('name', 'gcp.func'); + return faasEnv; + } + + return null; +} + +/** + * @internal + * This type represents the global Deno object and the minimal type contract we expect it to satisfy. + */ +declare const Deno: { version?: { deno?: string } } | undefined; + +/** + * @internal + * This type represents the global Bun object and the minimal type contract we expect it to satisfy. + */ +declare const Bun: { (): void; version?: string } | undefined; + +/** + * @internal + * Get current JavaScript runtime platform + * + * NOTE: The version information fetching is intentionally written defensively + * to avoid having a released driver version that becomes incompatible + * with a future change to these global objects. + */ +function getRuntimeInfo(): string { + const endianness = NumberUtils.isBigEndian ? 'BE' : 'LE'; + if ('Deno' in globalThis) { + const version = typeof Deno?.version?.deno === 'string' ? Deno?.version?.deno : '0.0.0-unknown'; + + return `Deno v${version}, ${endianness}`; + } + + if ('Bun' in globalThis) { + const version = typeof Bun?.version === 'string' ? Bun?.version : '0.0.0-unknown'; + + return `Bun v${version}, ${endianness}`; + } + + return `Node.js ${process.version}, ${endianness}`; +} diff --git a/gateway/node_modules/mongodb/src/cmap/metrics.ts b/gateway/node_modules/mongodb/src/cmap/metrics.ts new file mode 100644 index 0000000000000000000000000000000000000000..b825b539363e42ecdea1d2f64b0192e1d7132b2c --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/metrics.ts @@ -0,0 +1,58 @@ +/** @internal */ +export class ConnectionPoolMetrics { + static readonly TXN = 'txn' as const; + static readonly CURSOR = 'cursor' as const; + static readonly OTHER = 'other' as const; + + txnConnections = 0; + cursorConnections = 0; + otherConnections = 0; + + /** + * Mark a connection as pinned for a specific operation. + */ + markPinned(pinType: string): void { + if (pinType === ConnectionPoolMetrics.TXN) { + this.txnConnections += 1; + } else if (pinType === ConnectionPoolMetrics.CURSOR) { + this.cursorConnections += 1; + } else { + this.otherConnections += 1; + } + } + + /** + * Unmark a connection as pinned for an operation. + */ + markUnpinned(pinType: string): void { + if (pinType === ConnectionPoolMetrics.TXN) { + this.txnConnections -= 1; + } else if (pinType === ConnectionPoolMetrics.CURSOR) { + this.cursorConnections -= 1; + } else { + this.otherConnections -= 1; + } + } + + /** + * Return information about the cmap metrics as a string. + */ + info(maxPoolSize: number): string { + return ( + 'Timed out while checking out a connection from connection pool: ' + + `maxPoolSize: ${maxPoolSize}, ` + + `connections in use by cursors: ${this.cursorConnections}, ` + + `connections in use by transactions: ${this.txnConnections}, ` + + `connections in use by other operations: ${this.otherConnections}` + ); + } + + /** + * Reset the metrics to the initial values. + */ + reset(): void { + this.txnConnections = 0; + this.cursorConnections = 0; + this.otherConnections = 0; + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/stream_description.ts b/gateway/node_modules/mongodb/src/cmap/stream_description.ts new file mode 100644 index 0000000000000000000000000000000000000000..88411c92c497b74ea5a5eb85a2d2710bc8221e2b --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/stream_description.ts @@ -0,0 +1,96 @@ +import { type Document, type Double, Long } from '../bson'; +import { ServerType } from '../sdam/common'; +import { parseServerType } from '../sdam/server_description'; +import type { CompressorName } from './wire_protocol/compression'; + +const RESPONSE_FIELDS = [ + 'minWireVersion', + 'maxWireVersion', + 'maxBsonObjectSize', + 'maxMessageSizeBytes', + 'maxWriteBatchSize', + 'logicalSessionTimeoutMinutes' +] as const; + +/** @public */ +export interface StreamDescriptionOptions { + compressors?: CompressorName[]; + logicalSessionTimeoutMinutes?: number; + loadBalanced: boolean; +} + +/** @public */ +export class StreamDescription { + address: string; + type: ServerType; + minWireVersion?: number; + maxWireVersion?: number; + maxBsonObjectSize: number; + maxMessageSizeBytes: number; + maxWriteBatchSize: number; + compressors: CompressorName[]; + compressor?: CompressorName; + logicalSessionTimeoutMinutes?: number; + loadBalanced: boolean; + + __nodejs_mock_server__?: boolean; + + zlibCompressionLevel?: number; + serverConnectionId: bigint | null; + + public hello: Document | null = null; + + constructor(address: string, options?: StreamDescriptionOptions) { + this.address = address; + this.type = ServerType.Unknown; + this.minWireVersion = undefined; + this.maxWireVersion = undefined; + this.maxBsonObjectSize = 16777216; + this.maxMessageSizeBytes = 48000000; + this.maxWriteBatchSize = 100000; + this.logicalSessionTimeoutMinutes = options?.logicalSessionTimeoutMinutes; + this.loadBalanced = !!options?.loadBalanced; + this.compressors = + options && options.compressors && Array.isArray(options.compressors) + ? options.compressors + : []; + this.serverConnectionId = null; + } + + receiveResponse(response: Document | null): void { + if (response == null) { + return; + } + this.hello = response; + this.type = parseServerType(response); + if ('connectionId' in response) { + this.serverConnectionId = this.parseServerConnectionID(response.connectionId); + } else { + this.serverConnectionId = null; + } + for (const field of RESPONSE_FIELDS) { + if (response[field] != null) { + this[field] = response[field]; + } + + // testing case + if ('__nodejs_mock_server__' in response) { + this.__nodejs_mock_server__ = response['__nodejs_mock_server__']; + } + } + + if (response.compression) { + this.compressor = this.compressors.filter(c => response.compression?.includes(c))[0]; + } + } + + /* @internal */ + parseServerConnectionID(serverConnectionId: number | Double | bigint | Long): bigint { + // Connection ids are always integral, so it's safe to coerce doubles as well as + // any integral type. + return Long.isLong(serverConnectionId) + ? serverConnectionId.toBigInt() + : // @ts-expect-error: Doubles are coercible to number + BigInt(serverConnectionId); + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/wire_protocol/compression.ts b/gateway/node_modules/mongodb/src/cmap/wire_protocol/compression.ts new file mode 100644 index 0000000000000000000000000000000000000000..e5e46d273129496bf6c1f9b00b3ee908e80823aa --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/wire_protocol/compression.ts @@ -0,0 +1,215 @@ +import * as zlib from 'zlib'; + +import { ByteUtils, readInt32LE } from '../../bson'; +import { LEGACY_HELLO_COMMAND } from '../../constants'; +import { getSnappy, getZstdLibrary, type SnappyLib, type ZStandard } from '../../deps'; +import { MongoDecompressionError, MongoInvalidArgumentError } from '../../error'; +import { + type MessageHeader, + OpCompressedRequest, + type OpCompressesRequestOptions, + OpMsgResponse, + OpReply, + type WriteProtocolMessageType +} from '../commands'; +import { OP_COMPRESSED, OP_MSG } from './constants'; + +/** @public */ +export const Compressor = Object.freeze({ + none: 0, + snappy: 1, + zlib: 2, + zstd: 3 +} as const); + +/** @public */ +export type Compressor = (typeof Compressor)[CompressorName]; + +/** @public */ +export type CompressorName = keyof typeof Compressor; + +export const uncompressibleCommands = new Set([ + LEGACY_HELLO_COMMAND, + 'saslStart', + 'saslContinue', + 'getnonce', + 'authenticate', + 'createUser', + 'updateUser', + 'copydbSaslStart', + 'copydbgetnonce', + 'copydb' +]); + +const ZSTD_COMPRESSION_LEVEL = 3; + +const zlibInflate = (buf: zlib.InputType) => { + return new Promise((resolve, reject) => { + zlib.inflate(buf, (error, result) => { + if (error) return reject(error); + resolve(result); + }); + }); +}; + +const zlibDeflate = (buf: zlib.InputType, options: zlib.ZlibOptions) => { + return new Promise((resolve, reject) => { + zlib.deflate(buf, options, (error, result) => { + if (error) return reject(error); + resolve(result); + }); + }); +}; + +let zstd: ZStandard; +let Snappy: SnappyLib | null = null; +function loadSnappy() { + if (Snappy == null) { + const snappyImport = getSnappy(); + if ('kModuleError' in snappyImport) { + throw snappyImport.kModuleError; + } + Snappy = snappyImport; + } + return Snappy; +} + +// Facilitate compressing a message using an agreed compressor +export async function compress( + options: OpCompressesRequestOptions, + dataToBeCompressed: Uint8Array +): Promise { + const zlibOptions = {} as zlib.ZlibOptions; + switch (options.agreedCompressor) { + case 'snappy': { + Snappy ??= loadSnappy(); + return await Snappy.compress(dataToBeCompressed); + } + case 'zstd': { + loadZstd(); + if ('kModuleError' in zstd) { + throw zstd['kModuleError']; + } + return await zstd.compress(dataToBeCompressed, ZSTD_COMPRESSION_LEVEL); + } + case 'zlib': { + if (options.zlibCompressionLevel) { + zlibOptions.level = options.zlibCompressionLevel; + } + return await zlibDeflate(dataToBeCompressed, zlibOptions); + } + default: { + throw new MongoInvalidArgumentError( + `Unknown compressor ${options.agreedCompressor} failed to compress` + ); + } + } +} + +// Decompress a message using the given compressor +export async function decompress( + compressorID: number, + compressedData: Uint8Array +): Promise { + if ( + compressorID !== Compressor.snappy && + compressorID !== Compressor.zstd && + compressorID !== Compressor.zlib && + compressorID !== Compressor.none + ) { + throw new MongoDecompressionError( + `Server sent message compressed using an unsupported compressor. (Received compressor ID ${compressorID})` + ); + } + + switch (compressorID) { + case Compressor.snappy: { + Snappy ??= loadSnappy(); + return await Snappy.uncompress(compressedData, { asBuffer: true }); + } + case Compressor.zstd: { + loadZstd(); + if ('kModuleError' in zstd) { + throw zstd['kModuleError']; + } + return await zstd.decompress(compressedData); + } + case Compressor.zlib: { + return await zlibInflate(compressedData); + } + default: { + return compressedData; + } + } +} + +/** + * Load ZStandard if it is not already set. + */ +function loadZstd() { + if (!zstd) { + zstd = getZstdLibrary(); + } +} + +const MESSAGE_HEADER_SIZE = 16; + +/** + * @internal + * + * Compresses an OP_MSG or OP_QUERY message, if compression is configured. This method + * also serializes the command to BSON. + */ +export async function compressCommand( + command: WriteProtocolMessageType, + description: { agreedCompressor?: CompressorName; zlibCompressionLevel?: number } +): Promise { + const finalCommand = + description.agreedCompressor === 'none' || !OpCompressedRequest.canCompress(command) + ? command + : new OpCompressedRequest(command, { + agreedCompressor: description.agreedCompressor ?? 'none', + zlibCompressionLevel: description.zlibCompressionLevel ?? 0 + }); + const data = await finalCommand.toBin(); + return ByteUtils.concat(data); +} + +/** + * @internal + * + * Decompresses an OP_MSG or OP_QUERY response from the server, if compression is configured. + * + * This method does not parse the response's BSON. + */ +export async function decompressResponse(message: Uint8Array): Promise { + const messageHeader: MessageHeader = { + length: readInt32LE(message, 0), + requestId: readInt32LE(message, 4), + responseTo: readInt32LE(message, 8), + opCode: readInt32LE(message, 12) + }; + + if (messageHeader.opCode !== OP_COMPRESSED) { + const ResponseType = messageHeader.opCode === OP_MSG ? OpMsgResponse : OpReply; + const messageBody = message.subarray(MESSAGE_HEADER_SIZE); + return new ResponseType(message, messageHeader, messageBody); + } + + const header: MessageHeader = { + ...messageHeader, + fromCompressed: true, + opCode: readInt32LE(message, MESSAGE_HEADER_SIZE), + length: readInt32LE(message, MESSAGE_HEADER_SIZE + 4) + }; + const compressorID = message[MESSAGE_HEADER_SIZE + 8]; + const compressedBuffer = message.slice(MESSAGE_HEADER_SIZE + 9); + + // recalculate based on wrapped opcode + const ResponseType = header.opCode === OP_MSG ? OpMsgResponse : OpReply; + const messageBody = await decompress(compressorID, compressedBuffer); + if (messageBody.length !== header.length) { + throw new MongoDecompressionError('Message body and message header must be the same length'); + } + return new ResponseType(message, header, messageBody); +} diff --git a/gateway/node_modules/mongodb/src/cmap/wire_protocol/constants.ts b/gateway/node_modules/mongodb/src/cmap/wire_protocol/constants.ts new file mode 100644 index 0000000000000000000000000000000000000000..aa689033e18792e86890f662b8ac2d81c6bb9a50 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/wire_protocol/constants.ts @@ -0,0 +1,17 @@ +export const MIN_SUPPORTED_SERVER_VERSION = '4.2'; +export const MAX_SUPPORTED_SERVER_VERSION = '9.0'; +export const MIN_SUPPORTED_SNAPSHOT_READS_WIRE_VERSION = 13; +export const MIN_SUPPORTED_SNAPSHOT_READS_SERVER_VERSION = '5.0'; +export const MIN_SUPPORTED_WIRE_VERSION = 8; +export const MAX_SUPPORTED_WIRE_VERSION = 29; +export const MIN_SUPPORTED_QE_WIRE_VERSION = 21; +export const MIN_SUPPORTED_QE_SERVER_VERSION = '7.0'; +export const MIN_SUPPORTED_RAW_DATA_WIRE_VERSION = 27; +export const MIN_SUPPORTED_RAW_DATA_SERVER_VERSION = '8.2'; +export const OP_REPLY = 1; +export const OP_UPDATE = 2001; +export const OP_INSERT = 2002; +export const OP_QUERY = 2004; +export const OP_DELETE = 2006; +export const OP_COMPRESSED = 2012; +export const OP_MSG = 2013; diff --git a/gateway/node_modules/mongodb/src/cmap/wire_protocol/on_data.ts b/gateway/node_modules/mongodb/src/cmap/wire_protocol/on_data.ts new file mode 100644 index 0000000000000000000000000000000000000000..cf42f7b0234452b3e722606a581ea622993704c4 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/wire_protocol/on_data.ts @@ -0,0 +1,139 @@ +import { type EventEmitter } from 'events'; + +import { type Abortable } from '../../mongo_types'; +import { type TimeoutContext } from '../../timeout'; +import { addAbortListener, kDispose, List, promiseWithResolvers } from '../../utils'; + +/** + * @internal + * An object holding references to a promise's resolve and reject functions. + */ +type PendingPromises = Omit< + ReturnType>>, + 'promise' +>; + +/** + * onData is adapted from Node.js' events.on helper + * https://nodejs.org/api/events.html#eventsonemitter-eventname-options + * + * Returns an AsyncIterator that iterates each 'data' event emitted from emitter. + * It will reject upon an error event. + */ +export function onData( + emitter: EventEmitter, + { timeoutContext, signal }: { timeoutContext?: TimeoutContext } & Abortable +) { + signal?.throwIfAborted(); + + // Setup pending events and pending promise lists + /** + * When the caller has not yet called .next(), we store the + * value from the event in this list. Next time they call .next() + * we pull the first value out of this list and resolve a promise with it. + */ + const unconsumedEvents = new List(); + /** + * When there has not yet been an event, a new promise will be created + * and implicitly stored in this list. When an event occurs we take the first + * promise in this list and resolve it. + */ + const unconsumedPromises = new List(); + + /** + * Stored an error created by an error event. + * This error will turn into a rejection for the subsequent .next() call + */ + let error: Error | null = null; + + /** Set to true only after event listeners have been removed. */ + let finished = false; + + const iterator: AsyncGenerator & AsyncDisposable = { + next() { + // First, we consume all unread events + const value = unconsumedEvents.shift(); + if (value != null) { + return Promise.resolve({ value, done: false }); + } + + // Then we error, if an error happened + // This happens one time if at all, because after 'error' + // we stop listening + if (error != null) { + const p = Promise.reject(error); + // Only the first element errors + error = null; + return p; + } + + // If the iterator is finished, resolve to done + if (finished) return closeHandler(); + + // Wait until an event happens + const { promise, resolve, reject } = promiseWithResolvers>(); + unconsumedPromises.push({ resolve, reject }); + return promise; + }, + + return() { + return closeHandler(); + }, + + throw(err: Error) { + errorHandler(err); + return Promise.resolve({ value: undefined, done: true }); + }, + + [Symbol.asyncIterator]() { + return this; + }, + + async [Symbol.asyncDispose]() { + await closeHandler(); + } + }; + + // Adding event handlers + emitter.on('data', eventHandler); + emitter.on('error', errorHandler); + const abortListener = addAbortListener(signal, function () { + errorHandler(this.reason); + }); + + const timeoutForSocketRead = timeoutContext?.timeoutForSocketRead; + timeoutForSocketRead?.throwIfExpired(); + timeoutForSocketRead?.then(undefined, errorHandler); + + return iterator; + + function eventHandler(value: Uint8Array) { + const promise = unconsumedPromises.shift(); + if (promise != null) promise.resolve({ value, done: false }); + else unconsumedEvents.push(value); + } + + function errorHandler(err: Error) { + const promise = unconsumedPromises.shift(); + + if (promise != null) promise.reject(err); + else error = err; + void closeHandler(); + } + + function closeHandler() { + // Adding event handlers + emitter.off('data', eventHandler); + emitter.off('error', errorHandler); + abortListener?.[kDispose](); + finished = true; + timeoutForSocketRead?.clear(); + const doneResult = { value: undefined, done: finished } as const; + + for (const promise of unconsumedPromises) { + promise.resolve(doneResult); + } + + return Promise.resolve(doneResult); + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/wire_protocol/on_demand/document.ts b/gateway/node_modules/mongodb/src/cmap/wire_protocol/on_demand/document.ts new file mode 100644 index 0000000000000000000000000000000000000000..133c9e78ff9641700ae2857502f9e9d0bce9719e --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/wire_protocol/on_demand/document.ts @@ -0,0 +1,356 @@ +import { + Binary, + type BSONElement, + BSONError, + BSONType, + ByteUtils, + deserialize, + type DeserializeOptions, + NumberUtils, + ObjectId, + parseToElementsToArray, + Timestamp +} from '../../../bson'; + +const BSONElementOffset = { + type: 0, + nameOffset: 1, + nameLength: 2, + offset: 3, + length: 4 +} as const; + +/** @internal */ +export type JSTypeOf = { + [BSONType.null]: null; + [BSONType.undefined]: null; + [BSONType.double]: number; + [BSONType.int]: number; + [BSONType.long]: bigint; + [BSONType.timestamp]: Timestamp; + [BSONType.binData]: Binary; + [BSONType.bool]: boolean; + [BSONType.objectId]: ObjectId; + [BSONType.string]: string; + [BSONType.date]: Date; + [BSONType.object]: OnDemandDocument; + [BSONType.array]: OnDemandDocument; +}; + +/** @internal */ +type CachedBSONElement = { element: BSONElement; value: any | undefined }; + +/** + * @internal + * + * Options for `OnDemandDocument.toObject()`. Validation is required to ensure + * that callers provide utf8 validation options. */ +export type OnDemandDocumentDeserializeOptions = Omit & + Required>; + +/** @internal */ +export class OnDemandDocument { + /** + * Maps JS strings to elements and jsValues for speeding up subsequent lookups. + * - If `false` then name does not exist in the BSON document + * - If `CachedBSONElement` instance name exists + * - If `cache[name].value == null` jsValue has not yet been parsed + * - Null/Undefined values do not get cached because they are zero-length values. + */ + private readonly cache: Record = + Object.create(null); + /** Caches the index of elements that have been named */ + private readonly indexFound: Record = Object.create(null); + + /** All bson elements in this document */ + private readonly elements: ReadonlyArray; + /** BSON bytes, this document begins at offset */ + protected readonly bson: Uint8Array; + /** The start of the document */ + private readonly offset: number; + /** If this is an embedded document, indicates if this was a BSON array */ + public readonly isArray: boolean; + + constructor( + bson: Uint8Array, + offset = 0, + isArray = false, + /** If elements was already calculated */ + elements?: BSONElement[] + ) { + this.bson = bson; + this.offset = offset; + this.isArray = isArray; + this.elements = elements ?? parseToElementsToArray(this.bson, offset); + } + + /** Only supports basic latin strings */ + private isElementName(name: string, element: BSONElement): boolean { + const nameLength = element[BSONElementOffset.nameLength]; + const nameOffset = element[BSONElementOffset.nameOffset]; + + if (name.length !== nameLength) return false; + + const nameEnd = nameOffset + nameLength; + for ( + let byteIndex = nameOffset, charIndex = 0; + charIndex < name.length && byteIndex < nameEnd; + charIndex++, byteIndex++ + ) { + if (this.bson[byteIndex] !== name.charCodeAt(charIndex)) return false; + } + + return true; + } + + /** + * Seeks into the elements array for an element matching the given name. + * + * @remarks + * Caching: + * - Caches the existence of a property making subsequent look ups for non-existent properties return immediately + * - Caches names mapped to elements to avoid reiterating the array and comparing the name again + * - Caches the index at which an element has been found to prevent rechecking against elements already determined to belong to another name + * + * @param name - a basic latin string name of a BSON element + * @returns + */ + private getElement(name: string | number): CachedBSONElement | null { + const cachedElement = this.cache[name]; + if (cachedElement === false) return null; + + if (cachedElement != null) { + return cachedElement; + } + + if (typeof name === 'number') { + if (this.isArray) { + if (name < this.elements.length) { + const element = this.elements[name]; + const cachedElement = { element, value: undefined }; + this.cache[name] = cachedElement; + this.indexFound[name] = true; + return cachedElement; + } else { + return null; + } + } else { + return null; + } + } + + for (let index = 0; index < this.elements.length; index++) { + const element = this.elements[index]; + + // skip this element if it has already been associated with a name + if (!(index in this.indexFound) && this.isElementName(name, element)) { + const cachedElement = { element, value: undefined }; + this.cache[name] = cachedElement; + this.indexFound[index] = true; + return cachedElement; + } + } + + this.cache[name] = false; + return null; + } + + /** + * Translates BSON bytes into a javascript value. Checking `as` against the BSON element's type + * this methods returns the small subset of BSON types that the driver needs to function. + * + * @remarks + * - BSONType.null and BSONType.undefined always return null + * - If the type requested does not match this returns null + * + * @param element - The element to revive to a javascript value + * @param as - A type byte expected to be returned + */ + private toJSValue(element: BSONElement, as: T): JSTypeOf[T]; + private toJSValue(element: BSONElement, as: keyof JSTypeOf): any { + const type = element[BSONElementOffset.type]; + const offset = element[BSONElementOffset.offset]; + const length = element[BSONElementOffset.length]; + + if (as !== type) { + return null; + } + + switch (as) { + case BSONType.null: + case BSONType.undefined: + return null; + case BSONType.double: + return NumberUtils.getFloat64LE(this.bson, offset); + case BSONType.int: + return NumberUtils.getInt32LE(this.bson, offset); + case BSONType.long: + return NumberUtils.getBigInt64LE(this.bson, offset); + case BSONType.bool: + return Boolean(this.bson[offset]); + case BSONType.objectId: + return new ObjectId(this.bson.subarray(offset, offset + 12)); + case BSONType.timestamp: + return new Timestamp(NumberUtils.getBigInt64LE(this.bson, offset)); + case BSONType.string: + return ByteUtils.toUTF8(this.bson, offset + 4, offset + length - 1, false); + case BSONType.binData: { + const totalBinarySize = NumberUtils.getInt32LE(this.bson, offset); + const subType = this.bson[offset + 4]; + + if (subType === 2) { + const subType2BinarySize = NumberUtils.getInt32LE(this.bson, offset + 1 + 4); + if (subType2BinarySize < 0) + throw new BSONError('Negative binary type element size found for subtype 0x02'); + if (subType2BinarySize > totalBinarySize - 4) + throw new BSONError('Binary type with subtype 0x02 contains too long binary size'); + if (subType2BinarySize < totalBinarySize - 4) + throw new BSONError('Binary type with subtype 0x02 contains too short binary size'); + return new Binary( + this.bson.subarray(offset + 1 + 4 + 4, offset + 1 + 4 + 4 + subType2BinarySize), + 2 + ); + } + + return new Binary( + this.bson.subarray(offset + 1 + 4, offset + 1 + 4 + totalBinarySize), + subType + ); + } + case BSONType.date: + // Pretend this is correct. + return new Date(Number(NumberUtils.getBigInt64LE(this.bson, offset))); + + case BSONType.object: + return new OnDemandDocument(this.bson, offset); + case BSONType.array: + return new OnDemandDocument(this.bson, offset, true); + + default: + throw new BSONError(`Unsupported BSON type: ${as}`); + } + } + + /** + * Returns the number of elements in this BSON document + */ + public size() { + return this.elements.length; + } + + /** + * Checks for the existence of an element by name. + * + * @remarks + * Uses `getElement` with the expectation that will populate caches such that a `has` call + * followed by a `getElement` call will not repeat the cost paid by the first look up. + * + * @param name - element name + */ + public has(name: string): boolean { + const cachedElement = this.cache[name]; + if (cachedElement === false) return false; + if (cachedElement != null) return true; + return this.getElement(name) != null; + } + + /** + * Turns BSON element with `name` into a javascript value. + * + * @typeParam T - must be one of the supported BSON types determined by `JSTypeOf` this will determine the return type of this function. + * @param name - the element name + * @param as - the bson type expected + * @param required - whether or not the element is expected to exist, if true this function will throw if it is not present + */ + public get( + name: string | number, + as: T, + required?: boolean + ): JSTypeOf[T] | null; + + /** `required` will make `get` throw if name does not exist or is null/undefined */ + public get( + name: string | number, + as: T, + required: true + ): JSTypeOf[T]; + + public get( + name: string | number, + as: T, + required?: boolean + ): JSTypeOf[T] | null { + const element = this.getElement(name); + if (element == null) { + if (required === true) { + throw new BSONError(`BSON element "${name}" is missing`); + } else { + return null; + } + } + + if (element.value == null) { + const value = this.toJSValue(element.element, as); + if (value == null) { + if (required === true) { + throw new BSONError(`BSON element "${name}" is missing`); + } else { + return null; + } + } + // It is important to never store null + element.value = value; + } + + return element.value; + } + + /** + * Supports returning int, double, long, and bool as javascript numbers + * + * @remarks + * **NOTE:** + * - Use this _only_ when you believe the potential precision loss of an int64 is acceptable + * - This method does not cache the result as Longs or booleans would be stored incorrectly + * + * @param name - element name + * @param required - throws if name does not exist + */ + public getNumber( + name: string, + required?: Req + ): Req extends true ? number : number | null; + public getNumber(name: string, required: boolean): number | null { + const maybeBool = this.get(name, BSONType.bool); + const bool = maybeBool == null ? null : maybeBool ? 1 : 0; + + const maybeLong = this.get(name, BSONType.long); + const long = maybeLong == null ? null : Number(maybeLong); + + const result = bool ?? long ?? this.get(name, BSONType.int) ?? this.get(name, BSONType.double); + + if (required === true && result == null) { + throw new BSONError(`BSON element "${name}" is missing`); + } + + return result; + } + + /** + * Deserialize this object, DOES NOT cache result so avoid multiple invocations + * @param options - BSON deserialization options + */ + public toObject(options?: OnDemandDocumentDeserializeOptions): Record { + return deserialize(this.bson, { + ...options, + index: this.offset, + allowObjectSmallerThanBufferSize: true + }); + } + + /** Returns this document's bytes only */ + toBytes() { + const size = NumberUtils.getInt32LE(this.bson, this.offset); + return this.bson.subarray(this.offset, this.offset + size); + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/wire_protocol/responses.ts b/gateway/node_modules/mongodb/src/cmap/wire_protocol/responses.ts new file mode 100644 index 0000000000000000000000000000000000000000..d5549aea5450471719fd6a87feae2736e588e4fd --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/wire_protocol/responses.ts @@ -0,0 +1,393 @@ +import { + type BSONElement, + type BSONSerializeOptions, + BSONType, + type DeserializeOptions, + type Document, + Long, + parseToElementsToArray, + parseUtf8ValidationOption, + pluckBSONSerializeOptions, + serialize, + type Timestamp +} from '../../bson'; +import { MONGODB_ERROR_CODES, MongoUnexpectedServerResponseError } from '../../error'; +import { type ClusterTime } from '../../sdam/common'; +import { decorateDecryptionResult, ns } from '../../utils'; +import { + type JSTypeOf, + OnDemandDocument, + type OnDemandDocumentDeserializeOptions +} from './on_demand/document'; + +const BSONElementOffset = { + type: 0, + nameOffset: 1, + nameLength: 2, + offset: 3, + length: 4 +} as const; + +/** + * Accepts a BSON payload and checks for na "ok: 0" element. + * This utility is intended to prevent calling response class constructors + * that expect the result to be a success and demand certain properties to exist. + * + * For example, a cursor response always expects a cursor embedded document. + * In order to write the class such that the properties reflect that assertion (non-null) + * we cannot invoke the subclass constructor if the BSON represents an error. + * + * @param bytes - BSON document returned from the server + */ +export function isErrorResponse(bson: Uint8Array, elements: BSONElement[]): boolean { + for (let eIdx = 0; eIdx < elements.length; eIdx++) { + const element = elements[eIdx]; + + if (element[BSONElementOffset.nameLength] === 2) { + const nameOffset = element[BSONElementOffset.nameOffset]; + + // 111 == "o", 107 == "k" + if (bson[nameOffset] === 111 && bson[nameOffset + 1] === 107) { + const valueOffset = element[BSONElementOffset.offset]; + const valueLength = element[BSONElementOffset.length]; + + // If any byte in the length of the ok number (works for any type) is non zero, + // then it is considered "ok: 1" + for (let i = valueOffset; i < valueOffset + valueLength; i++) { + if (bson[i] !== 0x00) return false; + } + + return true; + } + } + } + + return true; +} + +/** @internal */ +export type MongoDBResponseConstructor = { + new (bson: Uint8Array, offset?: number, isArray?: boolean): MongoDBResponse; + make(bson: Uint8Array): MongoDBResponse; +}; + +/** @internal */ +export class MongoDBResponse extends OnDemandDocument { + // Wrap error thrown from BSON + public override get( + name: string | number, + as: T, + required?: false + ): JSTypeOf[T] | null; + public override get( + name: string | number, + as: T, + required: true + ): JSTypeOf[T]; + public override get( + name: string | number, + as: T, + required?: boolean + ): JSTypeOf[T] | null { + try { + return super.get(name, as, required); + } catch (cause) { + throw new MongoUnexpectedServerResponseError(cause.message, { cause }); + } + } + + static is(value: unknown): value is MongoDBResponse { + return value instanceof MongoDBResponse; + } + + static make(bson: Uint8Array) { + const elements = parseToElementsToArray(bson, 0); + const isError = isErrorResponse(bson, elements); + return isError + ? new MongoDBResponse(bson, 0, false, elements) + : new this(bson, 0, false, elements); + } + + // {ok:1} + static empty = new MongoDBResponse(new Uint8Array([13, 0, 0, 0, 16, 111, 107, 0, 1, 0, 0, 0, 0])); + + /** + * Returns true iff: + * - ok is 0 and the top-level code === 50 + * - ok is 1 and the writeErrors array contains a code === 50 + * - ok is 1 and the writeConcern object contains a code === 50 + */ + get isMaxTimeExpiredError() { + // {ok: 0, code: 50 ... } + const isTopLevel = this.ok === 0 && this.code === MONGODB_ERROR_CODES.MaxTimeMSExpired; + if (isTopLevel) return true; + + if (this.ok === 0) return false; + + // {ok: 1, writeConcernError: {code: 50 ... }} + const isWriteConcern = + this.get('writeConcernError', BSONType.object)?.getNumber('code') === + MONGODB_ERROR_CODES.MaxTimeMSExpired; + if (isWriteConcern) return true; + + const writeErrors = this.get('writeErrors', BSONType.array); + if (writeErrors?.size()) { + for (let i = 0; i < writeErrors.size(); i++) { + const isWriteError = + writeErrors.get(i, BSONType.object)?.getNumber('code') === + MONGODB_ERROR_CODES.MaxTimeMSExpired; + + // {ok: 1, writeErrors: [{code: 50 ... }]} + if (isWriteError) return true; + } + } + + return false; + } + + /** + * Drivers can safely assume that the `recoveryToken` field is always a BSON document but drivers MUST NOT modify the + * contents of the document. + */ + get recoveryToken(): Document | null { + return ( + this.get('recoveryToken', BSONType.object)?.toObject({ + promoteValues: false, + promoteLongs: false, + promoteBuffers: false, + validation: { utf8: true } + }) ?? null + ); + } + + /** + * The server creates a cursor in response to a snapshot find/aggregate command and reports atClusterTime within the cursor field in the response. + * For the distinct command the server adds a top-level atClusterTime field to the response. + * The atClusterTime field represents the timestamp of the read and is guaranteed to be majority committed. + */ + public get atClusterTime(): Timestamp | null { + return ( + this.get('cursor', BSONType.object)?.get('atClusterTime', BSONType.timestamp) ?? + this.get('atClusterTime', BSONType.timestamp) + ); + } + + public get operationTime(): Timestamp | null { + return this.get('operationTime', BSONType.timestamp); + } + + /** Normalizes whatever BSON value is "ok" to a JS number 1 or 0. */ + public get ok(): 0 | 1 { + return this.getNumber('ok') ? 1 : 0; + } + + public get $err(): string | null { + return this.get('$err', BSONType.string); + } + + public get errmsg(): string | null { + return this.get('errmsg', BSONType.string); + } + + public get code(): number | null { + return this.getNumber('code'); + } + + private clusterTime?: ClusterTime | null; + public get $clusterTime(): ClusterTime | null { + if (!('clusterTime' in this)) { + const clusterTimeDoc = this.get('$clusterTime', BSONType.object); + if (clusterTimeDoc == null) { + this.clusterTime = null; + return null; + } + const clusterTime = clusterTimeDoc.get('clusterTime', BSONType.timestamp, true); + const signature = clusterTimeDoc.get('signature', BSONType.object)?.toObject(); + // @ts-expect-error: `signature` is incorrectly typed. It is public API. + this.clusterTime = { clusterTime, signature }; + } + return this.clusterTime ?? null; + } + + public override toObject(options?: BSONSerializeOptions): Record { + const exactBSONOptions = { + ...pluckBSONSerializeOptions(options ?? {}), + validation: parseUtf8ValidationOption(options) + }; + return super.toObject(exactBSONOptions); + } +} + +/** @internal */ +export class CursorResponse extends MongoDBResponse { + /** + * Devtools need to know which keys were encrypted before the driver automatically decrypted them. + * If decorating is enabled (`Symbol.for('@@mdb.decorateDecryptionResult')`), this field will be set, + * storing the original encrypted response from the server, so that we can build an object that has + * the list of BSON keys that were encrypted stored at a well known symbol: `Symbol.for('@@mdb.decryptedKeys')`. + */ + encryptedResponse?: MongoDBResponse; + /** + * This supports a feature of the FindCursor. + * It is an optimization to avoid an extra getMore when the limit has been reached + */ + static get emptyGetMore(): CursorResponse { + return new CursorResponse(serialize({ ok: 1, cursor: { id: 0n, nextBatch: [] } })); + } + + static override is(value: unknown): value is CursorResponse { + return value instanceof CursorResponse || value === CursorResponse.emptyGetMore; + } + + private _batch: OnDemandDocument | null = null; + private iterated = 0; + + get cursor() { + return this.get('cursor', BSONType.object, true); + } + + public get id(): Long { + try { + return Long.fromBigInt(this.cursor.get('id', BSONType.long, true)); + } catch (cause) { + throw new MongoUnexpectedServerResponseError(cause.message, { cause }); + } + } + + public get ns() { + const namespace = this.cursor.get('ns', BSONType.string); + if (namespace != null) return ns(namespace); + return null; + } + + public get length() { + return Math.max(this.batchSize - this.iterated, 0); + } + + private _encryptedBatch: OnDemandDocument | null = null; + get encryptedBatch() { + if (this.encryptedResponse == null) return null; + if (this._encryptedBatch != null) return this._encryptedBatch; + + const cursor = this.encryptedResponse?.get('cursor', BSONType.object); + if (cursor?.has('firstBatch')) + this._encryptedBatch = cursor.get('firstBatch', BSONType.array, true); + else if (cursor?.has('nextBatch')) + this._encryptedBatch = cursor.get('nextBatch', BSONType.array, true); + else throw new MongoUnexpectedServerResponseError('Cursor document did not contain a batch'); + + return this._encryptedBatch; + } + + private get batch() { + if (this._batch != null) return this._batch; + const cursor = this.cursor; + if (cursor.has('firstBatch')) this._batch = cursor.get('firstBatch', BSONType.array, true); + else if (cursor.has('nextBatch')) this._batch = cursor.get('nextBatch', BSONType.array, true); + else throw new MongoUnexpectedServerResponseError('Cursor document did not contain a batch'); + return this._batch; + } + + public get batchSize() { + return this.batch?.size(); + } + + public get postBatchResumeToken() { + return ( + this.cursor.get('postBatchResumeToken', BSONType.object)?.toObject({ + promoteValues: false, + promoteLongs: false, + promoteBuffers: false, + validation: { utf8: true } + }) ?? null + ); + } + + public shift(options: OnDemandDocumentDeserializeOptions): any { + if (this.iterated >= this.batchSize) { + return null; + } + + const result = this.batch.get(this.iterated, BSONType.object, true) ?? null; + const encryptedResult = this.encryptedBatch?.get(this.iterated, BSONType.object, true) ?? null; + + this.iterated += 1; + + if (options?.raw) { + return result.toBytes(); + } else { + const object = result.toObject(options); + if (encryptedResult) { + decorateDecryptionResult(object, encryptedResult.toObject(options), true); + } + return object; + } + } + + public clear() { + this.iterated = this.batchSize; + } +} + +/** + * Explain responses have nothing to do with cursor responses + * This class serves to temporarily avoid refactoring how cursors handle + * explain responses which is to detect that the response is not cursor-like and return the explain + * result as the "first and only" document in the "batch" and end the "cursor" + */ +export class ExplainedCursorResponse extends CursorResponse { + isExplain = true; + + override get id(): Long { + return Long.fromBigInt(0n); + } + + override get batchSize() { + return 0; + } + + override get ns() { + return null; + } + + _length = 1; + override get length(): number { + return this._length; + } + + override shift(options?: DeserializeOptions) { + if (this._length === 0) return null; + this._length -= 1; + return this.toObject(options); + } +} + +/** + * Client bulk writes have some extra metadata at the top level that needs to be + * included in the result returned to the user. + */ +export class ClientBulkWriteCursorResponse extends CursorResponse { + get insertedCount() { + return this.get('nInserted', BSONType.int, true); + } + + get upsertedCount() { + return this.get('nUpserted', BSONType.int, true); + } + + get matchedCount() { + return this.get('nMatched', BSONType.int, true); + } + + get modifiedCount() { + return this.get('nModified', BSONType.int, true); + } + + get deletedCount() { + return this.get('nDeleted', BSONType.int, true); + } + + get writeConcernError() { + return this.get('writeConcernError', BSONType.object, false); + } +} diff --git a/gateway/node_modules/mongodb/src/cmap/wire_protocol/shared.ts b/gateway/node_modules/mongodb/src/cmap/wire_protocol/shared.ts new file mode 100644 index 0000000000000000000000000000000000000000..30757da1846161b6ad7ed042a831a43778b28702 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cmap/wire_protocol/shared.ts @@ -0,0 +1,48 @@ +import { MongoInvalidArgumentError } from '../../error'; +import { ReadPreference, type ReadPreferenceLike } from '../../read_preference'; +import { ServerType } from '../../sdam/common'; +import type { Server } from '../../sdam/server'; +import type { ServerDescription } from '../../sdam/server_description'; +import type { Topology } from '../../sdam/topology'; +import { TopologyDescription } from '../../sdam/topology_description'; +import type { Connection } from '../connection'; + +export interface ReadPreferenceOption { + readPreference?: ReadPreferenceLike; +} + +export function getReadPreference(options?: ReadPreferenceOption): ReadPreference { + // Default to command version of the readPreference. + let readPreference = options?.readPreference ?? ReadPreference.primary; + + if (typeof readPreference === 'string') { + readPreference = ReadPreference.fromString(readPreference); + } + + if (!(readPreference instanceof ReadPreference)) { + throw new MongoInvalidArgumentError( + 'Option "readPreference" must be a ReadPreference instance' + ); + } + + return readPreference; +} + +export function isSharded(topologyOrServer?: Topology | Server | Connection): boolean { + if (topologyOrServer == null) { + return false; + } + + if (topologyOrServer.description && topologyOrServer.description.type === ServerType.Mongos) { + return true; + } + + // NOTE: This is incredibly inefficient, and should be removed once command construction + // happens based on `Server` not `Topology`. + if (topologyOrServer.description && topologyOrServer.description instanceof TopologyDescription) { + const servers: ServerDescription[] = Array.from(topologyOrServer.description.servers.values()); + return servers.some((server: ServerDescription) => server.type === ServerType.Mongos); + } + + return false; +} diff --git a/gateway/node_modules/mongodb/src/collection.ts b/gateway/node_modules/mongodb/src/collection.ts new file mode 100644 index 0000000000000000000000000000000000000000..e3a520573635a9203622192e502f064697f4431c --- /dev/null +++ b/gateway/node_modules/mongodb/src/collection.ts @@ -0,0 +1,1314 @@ +import { type BSONSerializeOptions, type Document, resolveBSONOptions } from './bson'; +import type { + AnyBulkWriteOperation, + BulkOperationBase, + BulkWriteOptions, + BulkWriteResult +} from './bulk/common'; +import { OrderedBulkOperation } from './bulk/ordered'; +import { UnorderedBulkOperation } from './bulk/unordered'; +import { ChangeStream, type ChangeStreamDocument, type ChangeStreamOptions } from './change_stream'; +import { AggregationCursor } from './cursor/aggregation_cursor'; +import { FindCursor } from './cursor/find_cursor'; +import { ListIndexesCursor } from './cursor/list_indexes_cursor'; +import { + ListSearchIndexesCursor, + type ListSearchIndexesOptions +} from './cursor/list_search_indexes_cursor'; +import type { Db } from './db'; +import { MongoAPIError, MongoInvalidArgumentError, MongoOperationTimeoutError } from './error'; +import { type ExplainCommandOptions, type ExplainVerbosityLike } from './explain'; +import type { MongoClient, PkFactory } from './mongo_client'; +import type { + Abortable, + Filter, + Flatten, + OptionalUnlessRequiredId, + TODO_NODE_3286, + UpdateFilter, + WithId, + WithoutId +} from './mongo_types'; +import type { AggregateOptions } from './operations/aggregate'; +import { CountOperation, type CountOptions } from './operations/count'; +import { + DeleteManyOperation, + DeleteOneOperation, + type DeleteOptions, + type DeleteResult +} from './operations/delete'; +import { DistinctOperation, type DistinctOptions } from './operations/distinct'; +import { type DropCollectionOptions } from './operations/drop'; +import { + EstimatedDocumentCountOperation, + type EstimatedDocumentCountOptions +} from './operations/estimated_document_count'; +import { autoConnect, executeOperation } from './operations/execute_operation'; +import { type FindOneOptions, type FindOptions } from './operations/find'; +import { + FindOneAndDeleteOperation, + type FindOneAndDeleteOptions, + FindOneAndReplaceOperation, + type FindOneAndReplaceOptions, + FindOneAndUpdateOperation, + type FindOneAndUpdateOptions +} from './operations/find_and_modify'; +import { + CreateIndexesOperation, + type CreateIndexesOptions, + type DropIndexesOptions, + DropIndexOperation, + type IndexDescription, + type IndexDescriptionCompact, + type IndexDescriptionInfo, + type IndexInformationOptions, + type IndexSpecification, + type ListIndexesOptions +} from './operations/indexes'; +import { + type InsertManyResult, + InsertOneOperation, + type InsertOneOptions, + type InsertOneResult +} from './operations/insert'; +import type { Hint, OperationOptions } from './operations/operation'; +import { RenameOperation, type RenameOptions } from './operations/rename'; +import { + CreateSearchIndexesOperation, + type SearchIndexDescription +} from './operations/search_indexes/create'; +import { DropSearchIndexOperation } from './operations/search_indexes/drop'; +import { UpdateSearchIndexOperation } from './operations/search_indexes/update'; +import { + ReplaceOneOperation, + type ReplaceOptions, + UpdateManyOperation, + UpdateOneOperation, + type UpdateOptions, + type UpdateResult +} from './operations/update'; +import { ReadConcern, type ReadConcernLike } from './read_concern'; +import { ReadPreference, type ReadPreferenceLike } from './read_preference'; +import { type Sort } from './sort'; +import { + DEFAULT_PK_FACTORY, + MongoDBCollectionNamespace, + normalizeHintField, + resolveOptions +} from './utils'; +import { WriteConcern, type WriteConcernOptions } from './write_concern'; + +/** @public */ +export interface ModifyResult { + value: WithId | null; + lastErrorObject?: Document; + ok: 0 | 1; +} + +/** @public */ +export interface CountDocumentsOptions extends AggregateOptions { + /** The number of documents to skip. */ + skip?: number; + /** The maximum amount of documents to consider. */ + limit?: number; +} + +/** @public */ +export interface CollectionOptions extends BSONSerializeOptions, WriteConcernOptions { + /** Specify a read concern for the collection. (only MongoDB 3.2 or higher supported) */ + readConcern?: ReadConcernLike; + /** The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). */ + readPreference?: ReadPreferenceLike; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; +} + +/** @internal */ +export interface CollectionPrivate { + pkFactory: PkFactory; + db: Db; + options: any; + namespace: MongoDBCollectionNamespace; + readPreference?: ReadPreference; + bsonOptions: BSONSerializeOptions; + collectionHint?: Hint; + readConcern?: ReadConcern; + writeConcern?: WriteConcern; +} + +/** + * The **Collection** class is an internal class that embodies a MongoDB collection + * allowing for insert/find/update/delete and other command operation on that MongoDB collection. + * + * **COLLECTION Cannot directly be instantiated** + * @public + * + * @example + * ```ts + * import { MongoClient } from 'mongodb'; + * + * interface Pet { + * name: string; + * kind: 'dog' | 'cat' | 'fish'; + * } + * + * const client = new MongoClient('mongodb://localhost:27017'); + * const pets = client.db().collection('pets'); + * + * const petCursor = pets.find(); + * + * for await (const pet of petCursor) { + * console.log(`${pet.name} is a ${pet.kind}!`); + * } + * ``` + */ +export class Collection { + /** @internal */ + s: CollectionPrivate; + + /** @internal */ + client: MongoClient; + + /** + * Get the database object for the collection. + */ + readonly db: Db; + + /** + * Create a new Collection instance + * @internal + */ + constructor(db: Db, name: string, options?: CollectionOptions) { + this.db = db; + // Internal state + this.s = { + db, + options, + namespace: new MongoDBCollectionNamespace(db.databaseName, name), + pkFactory: db.options?.pkFactory ?? DEFAULT_PK_FACTORY, + readPreference: ReadPreference.fromOptions(options), + bsonOptions: resolveBSONOptions(options, db), + readConcern: ReadConcern.fromOptions(options), + writeConcern: WriteConcern.fromOptions(options) + }; + + this.client = db.client; + } + + /** + * The name of the database this collection belongs to + */ + get dbName(): string { + return this.s.namespace.db; + } + + /** + * The name of this collection + */ + get collectionName(): string { + return this.s.namespace.collection; + } + + /** + * The namespace of this collection, in the format `${this.dbName}.${this.collectionName}` + */ + get namespace(): string { + return this.fullNamespace.toString(); + } + + /** + * @internal + * + * The `MongoDBNamespace` for the collection. + */ + get fullNamespace(): MongoDBCollectionNamespace { + return this.s.namespace; + } + + /** + * The current readConcern of the collection. If not explicitly defined for + * this collection, will be inherited from the parent DB + */ + get readConcern(): ReadConcern | undefined { + if (this.s.readConcern == null) { + return this.db.readConcern; + } + return this.s.readConcern; + } + + /** + * The current readPreference of the collection. If not explicitly defined for + * this collection, will be inherited from the parent DB + */ + get readPreference(): ReadPreference | undefined { + if (this.s.readPreference == null) { + return this.db.readPreference; + } + + return this.s.readPreference; + } + + get bsonOptions(): BSONSerializeOptions { + return this.s.bsonOptions; + } + + /** + * The current writeConcern of the collection. If not explicitly defined for + * this collection, will be inherited from the parent DB + */ + get writeConcern(): WriteConcern | undefined { + if (this.s.writeConcern == null) { + return this.db.writeConcern; + } + return this.s.writeConcern; + } + + /** The current index hint for the collection */ + get hint(): Hint | undefined { + return this.s.collectionHint; + } + + set hint(v: Hint | undefined) { + this.s.collectionHint = normalizeHintField(v); + } + + public get timeoutMS(): number | undefined { + return this.s.options.timeoutMS; + } + + /** + * Inserts a single document into MongoDB. If documents passed in do not contain the **_id** field, + * one will be added to each of the documents missing it by the driver, mutating the document. This behavior + * can be overridden by setting the **forceServerObjectId** flag. + * + * @param doc - The document to insert + * @param options - Optional settings for the command + */ + async insertOne( + doc: OptionalUnlessRequiredId, + options?: InsertOneOptions + ): Promise> { + return await executeOperation( + this.client, + new InsertOneOperation( + this as TODO_NODE_3286, + doc, + resolveOptions(this, options) + ) as TODO_NODE_3286 + ); + } + + /** + * Inserts an array of documents into MongoDB. If documents passed in do not contain the **_id** field, + * one will be added to each of the documents missing it by the driver, mutating the document. This behavior + * can be overridden by setting the **forceServerObjectId** flag. + * + * @param docs - The documents to insert + * @param options - Optional settings for the command + */ + async insertMany( + docs: ReadonlyArray>, + options?: BulkWriteOptions + ): Promise> { + if (!Array.isArray(docs)) { + throw new MongoInvalidArgumentError('Argument "docs" must be an array of documents'); + } + options = resolveOptions(this, options ?? {}); + + const acknowledged = WriteConcern.fromOptions(options)?.w !== 0; + + try { + const res = await this.bulkWrite( + docs.map(doc => ({ insertOne: { document: doc } })), + options + ); + return { + acknowledged, + insertedCount: res.insertedCount, + insertedIds: res.insertedIds + }; + } catch (err) { + if (err && err.message === 'Operation must be an object with an operation key') { + throw new MongoInvalidArgumentError( + 'Collection.insertMany() cannot be called with an array that has null/undefined values' + ); + } + throw err; + } + } + + /** + * Perform a bulkWrite operation without a fluent API + * + * Legal operation types are + * - `insertOne` + * - `replaceOne` + * - `updateOne` + * - `updateMany` + * - `deleteOne` + * - `deleteMany` + * + * If documents passed in do not contain the **_id** field, + * one will be added to each of the documents missing it by the driver, mutating the document. This behavior + * can be overridden by setting the **forceServerObjectId** flag. + * + * @param operations - Bulk operations to perform + * @param options - Optional settings for the command + * @throws MongoDriverError if operations is not an array + */ + async bulkWrite( + operations: ReadonlyArray>, + options?: BulkWriteOptions + ): Promise { + if (!Array.isArray(operations)) { + throw new MongoInvalidArgumentError('Argument "operations" must be an array of documents'); + } + + options = resolveOptions(this, options ?? {}); + + // TODO(NODE-7071): remove once the client doesn't need to be connected to construct + // bulk operations + const isConnected = this.client.topology != null; + if (!isConnected) { + await autoConnect(this.client); + } + + // Create the bulk operation + const bulk: BulkOperationBase = + options.ordered === false + ? this.initializeUnorderedBulkOp(options) + : this.initializeOrderedBulkOp(options); + + // for each op go through and add to the bulk + for (const operation of operations) { + bulk.raw(operation); + } + + // Execute the bulk + return await bulk.execute({ ...options }); + } + + /** + * Update a single document in a collection + * + * The value of `update` can be either: + * - UpdateFilter - A document that contains update operator expressions, + * - Document[] - an aggregation pipeline. + * + * @param filter - The filter used to select the document to update + * @param update - The modifications to apply + * @param options - Optional settings for the command + */ + async updateOne( + filter: Filter, + update: UpdateFilter | Document[], + options?: UpdateOptions & { sort?: Sort } + ): Promise> { + return await executeOperation( + this.client, + new UpdateOneOperation(this.s.namespace, filter, update, resolveOptions(this, options)) + ); + } + + /** + * Replace a document in a collection with another document + * + * @param filter - The filter used to select the document to replace + * @param replacement - The Document that replaces the matching document + * @param options - Optional settings for the command + */ + async replaceOne( + filter: Filter, + replacement: WithoutId, + options?: ReplaceOptions + ): Promise> { + return await executeOperation( + this.client, + new ReplaceOneOperation(this.s.namespace, filter, replacement, resolveOptions(this, options)) + ); + } + + /** + * Update multiple documents in a collection + * + * The value of `update` can be either: + * - UpdateFilter - A document that contains update operator expressions, + * - Document[] - an aggregation pipeline. + * + * @param filter - The filter used to select the document to update + * @param update - The modifications to apply + * @param options - Optional settings for the command + */ + async updateMany( + filter: Filter, + update: UpdateFilter | Document[], + options?: UpdateOptions + ): Promise> { + return await executeOperation( + this.client, + new UpdateManyOperation(this.s.namespace, filter, update, resolveOptions(this, options)) + ); + } + + /** + * Delete a document from a collection + * + * @param filter - The filter used to select the document to remove + * @param options - Optional settings for the command + */ + async deleteOne( + filter: Filter = {}, + options: DeleteOptions = {} + ): Promise { + return await executeOperation( + this.client, + new DeleteOneOperation(this.s.namespace, filter, resolveOptions(this, options)) + ); + } + + /** + * Delete multiple documents from a collection + * + * @param filter - The filter used to select the documents to remove + * @param options - Optional settings for the command + */ + async deleteMany( + filter: Filter = {}, + options: DeleteOptions = {} + ): Promise { + return await executeOperation( + this.client, + new DeleteManyOperation(this.s.namespace, filter, resolveOptions(this, options)) + ); + } + + /** + * Rename the collection. + * + * @remarks + * This operation does not inherit options from the Db or MongoClient. + * + * @param newName - New name of of the collection. + * @param options - Optional settings for the command + */ + async rename(newName: string, options?: RenameOptions): Promise { + // Intentionally, we do not inherit options from parent for this operation. + return await executeOperation( + this.client, + new RenameOperation( + this as TODO_NODE_3286, + newName, + resolveOptions(undefined, { + ...options, + readPreference: ReadPreference.PRIMARY + }) + ) + ); + } + + /** + * Drop the collection from the database, removing it permanently. New accesses will create a new collection. + * + * @param options - Optional settings for the command + */ + async drop(options?: DropCollectionOptions): Promise { + return await this.db.dropCollection(this.collectionName, options); + } + + /** + * Fetches the first document that matches the filter + * + * @param filter - Query for find Operation + * @param options - Optional settings for the command + */ + async findOne(): Promise | null>; + async findOne(filter: Filter): Promise | null>; + async findOne( + filter: Filter, + options: Omit & Abortable + ): Promise | null>; + + // allow an override of the schema. + async findOne(): Promise; + async findOne(filter: Filter): Promise; + async findOne( + filter: Filter, + options?: Omit & Abortable + ): Promise; + + async findOne( + filter: Filter = {}, + options: Omit & Abortable = {} + ): Promise | null> { + // Explicitly set the limit to 1 and singleBatch to true for all commands, per the spec. + // noCursorTimeout must be unset as well as batchSize. + // See: https://github.com/mongodb/specifications/blob/master/source/crud/crud.md#findone-api-details + const { ...opts } = options; + opts.singleBatch = true; + const cursor = this.find(filter, opts).limit(1); + const result = await cursor.next(); + await cursor.close(); + return result; + } + + /** + * Creates a cursor for a filter that can be used to iterate over results from MongoDB + * + * @param filter - The filter predicate. If unspecified, then all documents in the collection will match the predicate + */ + find(): FindCursor>; + find(filter: Filter, options?: FindOptions & Abortable): FindCursor>; + find( + filter: Filter, + options?: FindOptions & Abortable + ): FindCursor; + find( + filter: Filter = {}, + options: FindOptions & Abortable = {} + ): FindCursor> { + return new FindCursor>( + this.client, + this.s.namespace, + filter, + resolveOptions(this, options) + ); + } + + /** + * Returns the options of the collection. + * + * @param options - Optional settings for the command + */ + async options(options?: OperationOptions): Promise { + options = resolveOptions(this, options); + const [collection] = await this.db + .listCollections({ name: this.collectionName }, { ...options, nameOnly: false }) + .toArray(); + + if (collection == null || collection.options == null) { + throw new MongoAPIError(`collection ${this.namespace} not found`); + } + + return collection.options; + } + + /** + * Returns if the collection is a capped collection + * + * @param options - Optional settings for the command + */ + async isCapped(options?: OperationOptions): Promise { + const { capped } = await this.options(options); + return Boolean(capped); + } + + /** + * Creates an index on the db and collection collection. + * + * @param indexSpec - The field name or index specification to create an index for + * @param options - Optional settings for the command + * + * @example + * ```ts + * const collection = client.db('foo').collection('bar'); + * + * await collection.createIndex({ a: 1, b: -1 }); + * + * // Alternate syntax for { c: 1, d: -1 } that ensures order of indexes + * await collection.createIndex([ [c, 1], [d, -1] ]); + * + * // Equivalent to { e: 1 } + * await collection.createIndex('e'); + * + * // Equivalent to { f: 1, g: 1 } + * await collection.createIndex(['f', 'g']) + * + * // Equivalent to { h: 1, i: -1 } + * await collection.createIndex([ { h: 1 }, { i: -1 } ]); + * + * // Equivalent to { j: 1, k: -1, l: 2d } + * await collection.createIndex(['j', ['k', -1], { l: '2d' }]) + * ``` + */ + async createIndex( + indexSpec: IndexSpecification, + options?: CreateIndexesOptions + ): Promise { + const indexes = await executeOperation( + this.client, + CreateIndexesOperation.fromIndexSpecification( + this, + this.collectionName, + indexSpec, + resolveOptions(this, options) + ) + ); + + return indexes[0]; + } + + /** + * Creates multiple indexes in the collection, this method is only supported for + * MongoDB 2.6 or higher. Earlier version of MongoDB will throw a command not supported + * error. + * + * **Note**: Unlike {@link Collection#createIndex| createIndex}, this function takes in raw index specifications. + * Index specifications are defined {@link https://www.mongodb.com/docs/manual/reference/command/createIndexes/| here}. + * + * @param indexSpecs - An array of index specifications to be created + * @param options - Optional settings for the command + * + * @example + * ```ts + * const collection = client.db('foo').collection('bar'); + * await collection.createIndexes([ + * // Simple index on field fizz + * { + * key: { fizz: 1 }, + * } + * // wildcard index + * { + * key: { '$**': 1 } + * }, + * // named index on darmok and jalad + * { + * key: { darmok: 1, jalad: -1 } + * name: 'tanagra' + * } + * ]); + * ``` + */ + async createIndexes( + indexSpecs: IndexDescription[], + options?: CreateIndexesOptions + ): Promise { + return await executeOperation( + this.client, + CreateIndexesOperation.fromIndexDescriptionArray( + this, + this.collectionName, + indexSpecs, + resolveOptions(this, { ...options, maxTimeMS: undefined }) + ) + ); + } + + /** + * Drops an index from this collection. + * + * @param indexName - Name of the index to drop. + * @param options - Optional settings for the command + */ + async dropIndex(indexName: string, options?: DropIndexesOptions): Promise { + return await executeOperation( + this.client, + new DropIndexOperation(this as TODO_NODE_3286, indexName, { + ...resolveOptions(this, options), + readPreference: ReadPreference.primary + }) + ); + } + + /** + * Drops all indexes from this collection. + * + * @param options - Optional settings for the command + */ + async dropIndexes(options?: DropIndexesOptions): Promise { + try { + await executeOperation( + this.client, + new DropIndexOperation(this as TODO_NODE_3286, '*', resolveOptions(this, options)) + ); + return true; + } catch (error) { + // TODO(NODE-6517): Driver should only filter for namespace not found error. Other errors should be thrown. + if (error instanceof MongoOperationTimeoutError) throw error; + return false; + } + } + + /** + * Get the list of all indexes information for the collection. + * + * @param options - Optional settings for the command + */ + listIndexes(options?: ListIndexesOptions): ListIndexesCursor { + return new ListIndexesCursor(this as TODO_NODE_3286, resolveOptions(this, options)); + } + + /** + * Checks if one or more indexes exist on the collection, fails on first non-existing index + * + * @param indexes - One or more index names to check. + * @param options - Optional settings for the command + */ + async indexExists(indexes: string | string[], options?: ListIndexesOptions): Promise { + const indexNames: string[] = Array.isArray(indexes) ? indexes : [indexes]; + const allIndexes: Set = new Set( + await this.listIndexes(options) + .map(({ name }) => name) + .toArray() + ); + return indexNames.every(name => allIndexes.has(name)); + } + + /** + * Retrieves this collections index info. + * + * @param options - Optional settings for the command + */ + indexInformation( + options: IndexInformationOptions & { full: true } + ): Promise; + indexInformation( + options: IndexInformationOptions & { full?: false } + ): Promise; + indexInformation( + options: IndexInformationOptions + ): Promise; + indexInformation(): Promise; + async indexInformation( + options?: IndexInformationOptions + ): Promise { + return await this.indexes({ + ...options, + full: options?.full ?? false + }); + } + + /** + * Gets an estimate of the count of documents in a collection using collection metadata. + * This will always run a count command on all server versions. + * + * due to an oversight in versions 5.0.0-5.0.8 of MongoDB, the count command, + * which estimatedDocumentCount uses in its implementation, was not included in v1 of + * the Stable API, and so users of the Stable API with estimatedDocumentCount are + * recommended to upgrade their server version to 5.0.9+ or set apiStrict: false to avoid + * encountering errors. + * + * @see {@link https://www.mongodb.com/docs/manual/reference/command/count/#behavior|Count: Behavior} + * @param options - Optional settings for the command + */ + async estimatedDocumentCount(options?: EstimatedDocumentCountOptions): Promise { + return await executeOperation( + this.client, + new EstimatedDocumentCountOperation(this as TODO_NODE_3286, resolveOptions(this, options)) + ); + } + + /** + * Gets the number of documents matching the filter. + * For a fast count of the total documents in a collection see {@link Collection#estimatedDocumentCount| estimatedDocumentCount}. + * + * Due to countDocuments using the $match aggregation pipeline stage, certain query operators cannot be used in countDocuments. This includes the $where and $near query operators, among others. Details can be found in the documentation for the $match aggregation pipeline stage. + * + * **Note**: When migrating from {@link Collection#count| count} to {@link Collection#countDocuments| countDocuments} + * the following query operators must be replaced: + * + * | Operator | Replacement | + * | -------- | ----------- | + * | `$where` | [`$expr`][1] | + * | `$near` | [`$geoWithin`][2] with [`$center`][3] | + * | `$nearSphere` | [`$geoWithin`][2] with [`$centerSphere`][4] | + * + * [1]: https://www.mongodb.com/docs/manual/reference/operator/query/expr/ + * [2]: https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/ + * [3]: https://www.mongodb.com/docs/manual/reference/operator/query/center/#op._S_center + * [4]: https://www.mongodb.com/docs/manual/reference/operator/query/centerSphere/#op._S_centerSphere + * + * @param filter - The filter for the count + * @param options - Optional settings for the command + * + * @see https://www.mongodb.com/docs/manual/reference/operator/query/expr/ + * @see https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/ + * @see https://www.mongodb.com/docs/manual/reference/operator/query/center/#op._S_center + * @see https://www.mongodb.com/docs/manual/reference/operator/query/centerSphere/#op._S_centerSphere + */ + async countDocuments( + filter: Filter = {}, + options: CountDocumentsOptions & Abortable = {} + ): Promise { + const pipeline = []; + pipeline.push({ $match: filter }); + + if (typeof options.skip === 'number') { + pipeline.push({ $skip: options.skip }); + } + + if (typeof options.limit === 'number') { + pipeline.push({ $limit: options.limit }); + } + + pipeline.push({ $group: { _id: 1, n: { $sum: 1 } } }); + + const cursor = this.aggregate<{ n: number }>(pipeline, options); + const doc = await cursor.next(); + await cursor.close(); + return doc?.n ?? 0; + } + + /** + * The distinct command returns a list of distinct values for the given key across a collection. + * + * @param key - Field of the document to find distinct values for + * @param filter - The filter for filtering the set of documents to which we apply the distinct filter. + * @param options - Optional settings for the command + */ + distinct>( + key: Key + ): Promise[Key]>>>; + distinct>( + key: Key, + filter: Filter + ): Promise[Key]>>>; + distinct>( + key: Key, + filter: Filter, + options: DistinctOptions + ): Promise[Key]>>>; + distinct>( + key: Key, + filter: Filter, + options: DistinctOptions & { explain: ExplainVerbosityLike | ExplainCommandOptions } + ): Promise; + + // Embedded documents overload + distinct(key: string): Promise; + distinct(key: string, filter: Filter): Promise; + distinct(key: string, filter: Filter, options: DistinctOptions): Promise; + + async distinct>( + key: Key, + filter: Filter = {}, + options: DistinctOptions = {} + ): Promise { + return await executeOperation( + this.client, + new DistinctOperation( + this as TODO_NODE_3286, + key as TODO_NODE_3286, + filter, + resolveOptions(this, options) + ) + ); + } + + /** + * Retrieve all the indexes on the collection. + * + * @param options - Optional settings for the command + */ + indexes(options: IndexInformationOptions & { full?: true }): Promise; + indexes(options: IndexInformationOptions & { full: false }): Promise; + indexes( + options: IndexInformationOptions + ): Promise; + indexes(options?: ListIndexesOptions): Promise; + async indexes( + options?: IndexInformationOptions + ): Promise { + const indexes: IndexDescriptionInfo[] = await this.listIndexes(options).toArray(); + const full = options?.full ?? true; + if (full) { + return indexes; + } + + const object: IndexDescriptionCompact = Object.fromEntries( + indexes.map(({ name, key }) => [name, Object.entries(key)]) + ); + + return object; + } + + /** + * Find a document and delete it in one atomic operation. Requires a write lock for the duration of the operation. + * + * @param filter - The filter used to select the document to remove + * @param options - Optional settings for the command + */ + async findOneAndDelete( + filter: Filter, + options: FindOneAndDeleteOptions & { includeResultMetadata: true } + ): Promise>; + async findOneAndDelete( + filter: Filter, + options: FindOneAndDeleteOptions & { includeResultMetadata: false } + ): Promise | null>; + async findOneAndDelete( + filter: Filter, + options: FindOneAndDeleteOptions + ): Promise | null>; + async findOneAndDelete(filter: Filter): Promise | null>; + async findOneAndDelete( + filter: Filter, + options?: FindOneAndDeleteOptions + ): Promise | ModifyResult | null> { + return await executeOperation( + this.client, + new FindOneAndDeleteOperation( + this as TODO_NODE_3286, + filter, + resolveOptions(this, options) + ) as TODO_NODE_3286 + ); + } + + /** + * Find a document and replace it in one atomic operation. Requires a write lock for the duration of the operation. + * + * @param filter - The filter used to select the document to replace + * @param replacement - The Document that replaces the matching document + * @param options - Optional settings for the command + */ + async findOneAndReplace( + filter: Filter, + replacement: WithoutId, + options: FindOneAndReplaceOptions & { includeResultMetadata: true } + ): Promise>; + async findOneAndReplace( + filter: Filter, + replacement: WithoutId, + options: FindOneAndReplaceOptions & { includeResultMetadata: false } + ): Promise | null>; + async findOneAndReplace( + filter: Filter, + replacement: WithoutId, + options: FindOneAndReplaceOptions + ): Promise | null>; + async findOneAndReplace( + filter: Filter, + replacement: WithoutId + ): Promise | null>; + async findOneAndReplace( + filter: Filter, + replacement: WithoutId, + options?: FindOneAndReplaceOptions + ): Promise | ModifyResult | null> { + return await executeOperation( + this.client, + new FindOneAndReplaceOperation( + this as TODO_NODE_3286, + filter, + replacement, + resolveOptions(this, options) + ) as TODO_NODE_3286 + ); + } + + /** + * Find a document and update it in one atomic operation. Requires a write lock for the duration of the operation. + * + * The value of `update` can be either: + * - UpdateFilter - A document that contains update operator expressions, + * - Document[] - an aggregation pipeline consisting of the following stages: + * - $addFields and its alias $set + * - $project and its alias $unset + * - $replaceRoot and its alias $replaceWith. + * See the [findAndModify command documentation](https://www.mongodb.com/docs/manual/reference/command/findAndModify) for details. + * + * @param filter - The filter used to select the document to update + * @param update - The modifications to apply + * @param options - Optional settings for the command + */ + async findOneAndUpdate( + filter: Filter, + update: UpdateFilter | Document[], + options: FindOneAndUpdateOptions & { includeResultMetadata: true } + ): Promise>; + async findOneAndUpdate( + filter: Filter, + update: UpdateFilter | Document[], + options: FindOneAndUpdateOptions & { includeResultMetadata: false } + ): Promise | null>; + async findOneAndUpdate( + filter: Filter, + update: UpdateFilter | Document[], + options: FindOneAndUpdateOptions + ): Promise | null>; + async findOneAndUpdate( + filter: Filter, + update: UpdateFilter | Document[] + ): Promise | null>; + async findOneAndUpdate( + filter: Filter, + update: UpdateFilter | Document[], + options?: FindOneAndUpdateOptions + ): Promise | ModifyResult | null> { + return await executeOperation( + this.client, + new FindOneAndUpdateOperation( + this as TODO_NODE_3286, + filter, + update, + resolveOptions(this, options) + ) as TODO_NODE_3286 + ); + } + + /** + * Execute an aggregation framework pipeline against the collection, needs MongoDB \>= 2.2 + * + * @param pipeline - An array of aggregation pipelines to execute + * @param options - Optional settings for the command + */ + aggregate( + pipeline: Document[] = [], + options?: AggregateOptions & Abortable + ): AggregationCursor { + if (!Array.isArray(pipeline)) { + throw new MongoInvalidArgumentError( + 'Argument "pipeline" must be an array of aggregation stages' + ); + } + + return new AggregationCursor( + this.client, + this.s.namespace, + pipeline, + resolveOptions(this, options) + ); + } + + /** + * Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this collection. + * + * @remarks + * watch() accepts two generic arguments for distinct use cases: + * - The first is to override the schema that may be defined for this specific collection + * - The second is to override the shape of the change stream document entirely, if it is not provided the type will default to ChangeStreamDocument of the first argument + * @example + * By just providing the first argument I can type the change to be `ChangeStreamDocument<{ _id: number }>` + * ```ts + * collection.watch<{ _id: number }>() + * .on('change', change => console.log(change._id.toFixed(4))); + * ``` + * + * @example + * Passing a second argument provides a way to reflect the type changes caused by an advanced pipeline. + * Here, we are using a pipeline to have MongoDB filter for insert changes only and add a comment. + * No need start from scratch on the ChangeStreamInsertDocument type! + * By using an intersection we can save time and ensure defaults remain the same type! + * ```ts + * collection + * .watch & { comment: string }>([ + * { $addFields: { comment: 'big changes' } }, + * { $match: { operationType: 'insert' } } + * ]) + * .on('change', change => { + * change.comment.startsWith('big'); + * change.operationType === 'insert'; + * // No need to narrow in code because the generics did that for us! + * expectType(change.fullDocument); + * }); + * ``` + * + * @remarks + * When `timeoutMS` is configured for a change stream, it will have different behaviour depending + * on whether the change stream is in iterator mode or emitter mode. In both cases, a change + * stream will time out if it does not receive a change event within `timeoutMS` of the last change + * event. + * + * Note that if a change stream is consistently timing out when watching a collection, database or + * client that is being changed, then this may be due to the server timing out before it can finish + * processing the existing oplog. To address this, restart the change stream with a higher + * `timeoutMS`. + * + * If the change stream times out the initial aggregate operation to establish the change stream on + * the server, then the client will close the change stream. If the getMore calls to the server + * time out, then the change stream will be left open, but will throw a MongoOperationTimeoutError + * when in iterator mode and emit an error event that returns a MongoOperationTimeoutError in + * emitter mode. + * + * To determine whether or not the change stream is still open following a timeout, check the + * {@link ChangeStream.closed} getter. + * + * @example + * In iterator mode, if a next() call throws a timeout error, it will attempt to resume the change stream. + * The next call can just be retried after this succeeds. + * ```ts + * const changeStream = collection.watch([], { timeoutMS: 100 }); + * try { + * await changeStream.next(); + * } catch (e) { + * if (e instanceof MongoOperationTimeoutError && !changeStream.closed) { + * await changeStream.next(); + * } + * throw e; + * } + * ``` + * + * @example + * In emitter mode, if the change stream goes `timeoutMS` without emitting a change event, it will + * emit an error event that returns a MongoOperationTimeoutError, but will not close the change + * stream unless the resume attempt fails. There is no need to re-establish change listeners as + * this will automatically continue emitting change events once the resume attempt completes. + * + * ```ts + * const changeStream = collection.watch([], { timeoutMS: 100 }); + * changeStream.on('change', console.log); + * changeStream.on('error', e => { + * if (e instanceof MongoOperationTimeoutError && !changeStream.closed) { + * // do nothing + * } else { + * changeStream.close(); + * } + * }); + * ``` + * + * @param pipeline - An array of {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents. + * @param options - Optional settings for the command + * @typeParam TLocal - Type of the data being detected by the change stream + * @typeParam TChange - Type of the whole change stream document emitted + */ + watch>( + pipeline: Document[] = [], + options: ChangeStreamOptions = {} + ): ChangeStream { + // Allow optionally not specifying a pipeline + if (!Array.isArray(pipeline)) { + options = pipeline; + pipeline = []; + } + + return new ChangeStream(this, pipeline, resolveOptions(this, options)); + } + + /** + * Initiate an Out of order batch write operation. All operations will be buffered into insert/update/remove commands executed out of order. + * + * @throws MongoNotConnectedError + * @remarks + * **NOTE:** MongoClient must be connected prior to calling this method due to a known limitation in this legacy implementation. + * However, `collection.bulkWrite()` provides an equivalent API that does not require prior connecting. + */ + initializeUnorderedBulkOp(options?: BulkWriteOptions): UnorderedBulkOperation { + return new UnorderedBulkOperation(this as TODO_NODE_3286, resolveOptions(this, options)); + } + + /** + * Initiate an In order bulk write operation. Operations will be serially executed in the order they are added, creating a new operation for each switch in types. + * + * @throws MongoNotConnectedError + * @remarks + * **NOTE:** MongoClient must be connected prior to calling this method due to a known limitation in this legacy implementation. + * However, `collection.bulkWrite()` provides an equivalent API that does not require prior connecting. + */ + initializeOrderedBulkOp(options?: BulkWriteOptions): OrderedBulkOperation { + return new OrderedBulkOperation(this as TODO_NODE_3286, resolveOptions(this, options)); + } + + /** + * An estimated count of matching documents in the db to a filter. + * + * **NOTE:** This method has been deprecated, since it does not provide an accurate count of the documents + * in a collection. To obtain an accurate count of documents in the collection, use {@link Collection#countDocuments| countDocuments}. + * To obtain an estimated count of all documents in the collection, use {@link Collection#estimatedDocumentCount| estimatedDocumentCount}. + * + * @deprecated use {@link Collection#countDocuments| countDocuments} or {@link Collection#estimatedDocumentCount| estimatedDocumentCount} instead + * + * @param filter - The filter for the count. + * @param options - Optional settings for the command + */ + async count(filter: Filter = {}, options: CountOptions = {}): Promise { + return await executeOperation( + this.client, + new CountOperation(this.fullNamespace, filter, resolveOptions(this, options)) + ); + } + + /** + * Returns all search indexes for the current collection. + * + * @param options - The options for the list indexes operation. + * + * @remarks Only available when used against a 7.0+ Atlas cluster. + */ + listSearchIndexes(options?: ListSearchIndexesOptions): ListSearchIndexesCursor; + /** + * Returns all search indexes for the current collection. + * + * @param name - The name of the index to search for. Only indexes with matching index names will be returned. + * @param options - The options for the list indexes operation. + * + * @remarks Only available when used against a 7.0+ Atlas cluster. + */ + listSearchIndexes(name: string, options?: ListSearchIndexesOptions): ListSearchIndexesCursor; + listSearchIndexes( + indexNameOrOptions?: string | ListSearchIndexesOptions, + options?: ListSearchIndexesOptions + ): ListSearchIndexesCursor { + options = + typeof indexNameOrOptions === 'object' ? indexNameOrOptions : options == null ? {} : options; + + const indexName = + indexNameOrOptions == null + ? null + : typeof indexNameOrOptions === 'object' + ? null + : indexNameOrOptions; + + return new ListSearchIndexesCursor(this as TODO_NODE_3286, indexName, options); + } + + /** + * Creates a single search index for the collection. + * + * @param description - The index description for the new search index. + * @returns A promise that resolves to the name of the new search index. + * + * @remarks Only available when used against a 7.0+ Atlas cluster. + */ + async createSearchIndex(description: SearchIndexDescription): Promise { + const [index] = await this.createSearchIndexes([description]); + return index; + } + + /** + * Creates multiple search indexes for the current collection. + * + * @param descriptions - An array of `SearchIndexDescription`s for the new search indexes. + * @returns A promise that resolves to an array of the newly created search index names. + * + * @remarks Only available when used against a 7.0+ Atlas cluster. + * @returns + */ + async createSearchIndexes(descriptions: SearchIndexDescription[]): Promise { + return await executeOperation( + this.client, + new CreateSearchIndexesOperation(this as TODO_NODE_3286, descriptions) + ); + } + + /** + * Deletes a search index by index name. + * + * @param name - The name of the search index to be deleted. + * + * @remarks Only available when used against a 7.0+ Atlas cluster. + */ + async dropSearchIndex(name: string): Promise { + return await executeOperation( + this.client, + new DropSearchIndexOperation(this as TODO_NODE_3286, name) + ); + } + + /** + * Updates a search index by replacing the existing index definition with the provided definition. + * + * @param name - The name of the search index to update. + * @param definition - The new search index definition. + * + * @remarks Only available when used against a 7.0+ Atlas cluster. + */ + async updateSearchIndex(name: string, definition: Document): Promise { + return await executeOperation( + this.client, + new UpdateSearchIndexOperation(this as TODO_NODE_3286, name, definition) + ); + } +} diff --git a/gateway/node_modules/mongodb/src/connection_string.ts b/gateway/node_modules/mongodb/src/connection_string.ts new file mode 100644 index 0000000000000000000000000000000000000000..67adcdcec410489798eb9c8c59c7c0d8fe824b6e --- /dev/null +++ b/gateway/node_modules/mongodb/src/connection_string.ts @@ -0,0 +1,1315 @@ +import * as dns from 'dns'; +import ConnectionString from 'mongodb-connection-string-url'; +import * as process from 'process'; +import { URLSearchParams } from 'url'; + +import type { Document } from './bson'; +import { MongoCredentials } from './cmap/auth/mongo_credentials'; +import { AUTH_MECHS_AUTH_SRC_EXTERNAL, AuthMechanism } from './cmap/auth/providers'; +import { Compressor, type CompressorName } from './cmap/wire_protocol/compression'; +import { Encrypter } from './encrypter'; +import { MongoAPIError, MongoInvalidArgumentError, MongoParseError } from './error'; +import { + MongoClient, + type MongoClientOptions, + type MongoOptions, + type PkFactory, + type ServerApi, + ServerApiVersion +} from './mongo_client'; +import { MongoLoggableComponent, MongoLogger, SeverityLevel } from './mongo_logger'; +import { ReadConcern, type ReadConcernLevel } from './read_concern'; +import { ReadPreference, type ReadPreferenceMode } from './read_preference'; +import { resolveRuntimeAdapters } from './runtime_adapters'; +import { ServerMonitoringMode } from './sdam/monitor'; +import type { TagSet } from './sdam/server_description'; +import { + checkParentDomainMatch, + DEFAULT_PK_FACTORY, + emitWarning, + HostAddress, + isRecord, + parseInteger, + setDifference, + squashError +} from './utils'; +import { type W, WriteConcern } from './write_concern'; + +const VALID_TXT_RECORDS = ['authSource', 'replicaSet', 'loadBalanced']; + +const LB_SINGLE_HOST_ERROR = 'loadBalanced option only supported with a single host in the URI'; +const LB_REPLICA_SET_ERROR = 'loadBalanced option not supported with a replicaSet option'; +const LB_DIRECT_CONNECTION_ERROR = + 'loadBalanced option not supported when directConnection is provided'; + +function retryDNSTimeoutFor(rrtype: 'SRV'): (lookupAddress: string) => Promise; +function retryDNSTimeoutFor(rrtype: 'TXT'): (lookupAddress: string) => Promise; +function retryDNSTimeoutFor( + rrtype: 'SRV' | 'TXT' +): (lookupAddress: string) => Promise { + const resolve = + rrtype === 'SRV' + ? (address: string) => dns.promises.resolve(address, 'SRV') + : (address: string) => dns.promises.resolve(address, 'TXT'); + return async function dnsReqRetryTimeout(lookupAddress: string) { + try { + return await resolve(lookupAddress); + } catch (firstDNSError) { + if (firstDNSError.code === dns.TIMEOUT) { + return await resolve(lookupAddress); + } else { + throw firstDNSError; + } + } + }; +} + +const resolveSrv = retryDNSTimeoutFor('SRV'); +const resolveTxt = retryDNSTimeoutFor('TXT'); + +/** + * Lookup a `mongodb+srv` connection string, combine the parts and reparse it as a normal + * connection string. + * + * @param uri - The connection string to parse + * @param options - Optional user provided connection string options + */ +export async function resolveSRVRecord(options: MongoOptions): Promise { + if (typeof options.srvHost !== 'string') { + throw new MongoAPIError('Option "srvHost" must not be empty'); + } + + // Asynchronously start TXT resolution so that we do not have to wait until + // the SRV record is resolved before starting a second DNS query. + const lookupAddress = options.srvHost; + const txtResolutionPromise = resolveTxt(lookupAddress); + + txtResolutionPromise.then(undefined, squashError); // rejections will be handled later + + const hostname = `_${options.srvServiceName}._tcp.${lookupAddress}`; + // Resolve the SRV record and use the result as the list of hosts to connect to. + const addresses = await resolveSrv(hostname); + + if (addresses.length === 0) { + throw new MongoAPIError('No addresses found at host'); + } + + for (const { name } of addresses) { + checkParentDomainMatch(name, lookupAddress); + } + + const hostAddresses = addresses.map(r => HostAddress.fromString(`${r.name}:${r.port ?? 27017}`)); + + validateLoadBalancedOptions(hostAddresses, options, true); + + // Use the result of resolving the TXT record and add options from there if they exist. + let record; + try { + record = await txtResolutionPromise; + } catch (error) { + if (error.code !== 'ENODATA' && error.code !== 'ENOTFOUND') { + throw error; + } + return hostAddresses; + } + + if (record.length > 1) { + throw new MongoParseError('Multiple text records not allowed'); + } + + const txtRecordOptions = new URLSearchParams(record[0].join('')); + const txtRecordOptionKeys = [...txtRecordOptions.keys()]; + if (txtRecordOptionKeys.some(key => !VALID_TXT_RECORDS.includes(key))) { + throw new MongoParseError(`Text record may only set any of: ${VALID_TXT_RECORDS.join(', ')}`); + } + + if (VALID_TXT_RECORDS.some(option => txtRecordOptions.get(option) === '')) { + throw new MongoParseError('Cannot have empty URI params in DNS TXT Record'); + } + + const source = txtRecordOptions.get('authSource') ?? undefined; + const replicaSet = txtRecordOptions.get('replicaSet') ?? undefined; + const loadBalanced = txtRecordOptions.get('loadBalanced') ?? undefined; + + if ( + !options.userSpecifiedAuthSource && + source && + options.credentials && + !AUTH_MECHS_AUTH_SRC_EXTERNAL.has(options.credentials.mechanism) + ) { + options.credentials = MongoCredentials.merge(options.credentials, { source }); + } + + if (!options.userSpecifiedReplicaSet && replicaSet) { + options.replicaSet = replicaSet; + } + + if (loadBalanced === 'true') { + options.loadBalanced = true; + } + + if (options.replicaSet && options.srvMaxHosts > 0) { + throw new MongoParseError('Cannot combine replicaSet option with srvMaxHosts'); + } + + validateLoadBalancedOptions(hostAddresses, options, true); + + return hostAddresses; +} + +/** + * Checks if TLS options are valid + * + * @param allOptions - All options provided by user or included in default options map + * @throws MongoAPIError if TLS options are invalid + */ +function checkTLSOptions(allOptions: CaseInsensitiveMap): void { + if (!allOptions) return; + const check = (a: string, b: string) => { + if (allOptions.has(a) && allOptions.has(b)) { + throw new MongoAPIError(`The '${a}' option cannot be used with the '${b}' option`); + } + }; + check('tlsInsecure', 'tlsAllowInvalidCertificates'); + check('tlsInsecure', 'tlsAllowInvalidHostnames'); +} +function getBoolean(name: string, value: unknown): boolean { + if (typeof value === 'boolean') return value; + switch (value) { + case 'true': + return true; + case 'false': + return false; + default: + throw new MongoParseError(`${name} must be either "true" or "false"`); + } +} + +function getIntFromOptions(name: string, value: unknown): number { + const parsedInt = parseInteger(value); + if (parsedInt != null) { + return parsedInt; + } + throw new MongoParseError(`Expected ${name} to be stringified int value, got: ${value}`); +} + +function getUIntFromOptions(name: string, value: unknown): number { + const parsedValue = getIntFromOptions(name, value); + if (parsedValue < 0) { + throw new MongoParseError(`${name} can only be a positive int value, got: ${value}`); + } + return parsedValue; +} + +function* entriesFromString(value: string): Generator<[string, string]> { + if (value === '') { + return; + } + const keyValuePairs = value.split(','); + for (const keyValue of keyValuePairs) { + const [key, value] = keyValue.split(/:(.*)/); + if (value == null) { + throw new MongoParseError('Cannot have undefined values in key value pairs'); + } + + yield [key, value]; + } +} + +class CaseInsensitiveMap extends Map { + constructor(entries: Array<[string, any]> = []) { + super(entries.map(([k, v]) => [k.toLowerCase(), v])); + } + override has(k: string) { + return super.has(k.toLowerCase()); + } + override get(k: string) { + return super.get(k.toLowerCase()); + } + override set(k: string, v: any) { + return super.set(k.toLowerCase(), v); + } + override delete(k: string): boolean { + return super.delete(k.toLowerCase()); + } +} + +export function parseOptions( + uri: string, + mongoClient: MongoClient | MongoClientOptions | undefined = undefined, + options: MongoClientOptions = {} +): MongoOptions { + if (mongoClient != null && !(mongoClient instanceof MongoClient)) { + options = mongoClient; + mongoClient = undefined; + } + + // validate BSONOptions + if (options.useBigInt64 && typeof options.promoteLongs === 'boolean' && !options.promoteLongs) { + throw new MongoAPIError('Must request either bigint or Long for int64 deserialization'); + } + + if (options.useBigInt64 && typeof options.promoteValues === 'boolean' && !options.promoteValues) { + throw new MongoAPIError('Must request either bigint or Long for int64 deserialization'); + } + + const url = new ConnectionString(uri); + const { hosts, isSRV } = url; + + const mongoOptions = Object.create(null); + + mongoOptions.hosts = isSRV ? [] : hosts.map(HostAddress.fromString); + + const urlOptions = new CaseInsensitiveMap(); + + if (url.pathname !== '/' && url.pathname !== '') { + const dbName = decodeURIComponent( + url.pathname[0] === '/' ? url.pathname.slice(1) : url.pathname + ); + if (dbName) { + urlOptions.set('dbName', [dbName]); + } + } + + if (url.username !== '') { + const auth: Document = { + username: decodeURIComponent(url.username) + }; + + if (typeof url.password === 'string') { + auth.password = decodeURIComponent(url.password); + } + + urlOptions.set('auth', [auth]); + } + + for (const key of url.searchParams.keys()) { + const values = url.searchParams.getAll(key); + + const isReadPreferenceTags = /readPreferenceTags/i.test(key); + + if (!isReadPreferenceTags && values.length > 1) { + throw new MongoInvalidArgumentError( + `URI option "${key}" cannot appear more than once in the connection string` + ); + } + + if (!isReadPreferenceTags && values.includes('')) { + throw new MongoAPIError(`URI option "${key}" cannot be specified with no value`); + } + + if (!urlOptions.has(key)) { + urlOptions.set(key, values); + } + } + + const objectOptions = new CaseInsensitiveMap( + Object.entries(options).filter(([, v]) => v != null) + ); + + // Validate options that can only be provided by one of uri or object + + if (urlOptions.has('serverApi')) { + throw new MongoParseError( + 'URI cannot contain `serverApi`, it can only be passed to the client' + ); + } + + const uriMechanismProperties = urlOptions.get('authMechanismProperties'); + if (uriMechanismProperties) { + for (const property of uriMechanismProperties) { + if (/(^|,)ALLOWED_HOSTS:/.test(property as string)) { + throw new MongoParseError( + 'Auth mechanism property ALLOWED_HOSTS is not allowed in the connection string.' + ); + } + } + } + + if (objectOptions.has('loadBalanced')) { + throw new MongoParseError('loadBalanced is only a valid option in the URI'); + } + + // All option collection + + const allProvidedOptions = new CaseInsensitiveMap(); + + const allProvidedKeys = new Set([...urlOptions.keys(), ...objectOptions.keys()]); + + for (const key of allProvidedKeys) { + const values = []; + const objectOptionValue = objectOptions.get(key); + if (objectOptionValue != null) { + values.push(objectOptionValue); + } + + const urlValues = urlOptions.get(key) ?? []; + values.push(...urlValues); + allProvidedOptions.set(key, values); + } + + if (allProvidedOptions.has('tls') || allProvidedOptions.has('ssl')) { + const tlsAndSslOpts = (allProvidedOptions.get('tls') || []) + .concat(allProvidedOptions.get('ssl') || []) + .map(getBoolean.bind(null, 'tls/ssl')); + if (new Set(tlsAndSslOpts).size !== 1) { + throw new MongoParseError('All values of tls/ssl must be the same.'); + } + } + + checkTLSOptions(allProvidedOptions); + + const unsupportedOptions = setDifference( + allProvidedKeys, + Array.from(Object.keys(OPTIONS)).map(s => s.toLowerCase()) + ); + if (unsupportedOptions.size !== 0) { + const optionWord = unsupportedOptions.size > 1 ? 'options' : 'option'; + const isOrAre = unsupportedOptions.size > 1 ? 'are' : 'is'; + throw new MongoParseError( + `${optionWord} ${Array.from(unsupportedOptions).join(', ')} ${isOrAre} not supported` + ); + } + + // Option parsing and setting + + for (const [key, descriptor] of Object.entries(OPTIONS)) { + const values = allProvidedOptions.get(key); + if (!values || values.length === 0) { + if (DEFAULT_OPTIONS.has(key)) { + setOption(mongoOptions, key, descriptor, [DEFAULT_OPTIONS.get(key)]); + } + } else { + const { deprecated } = descriptor; + if (deprecated) { + const deprecatedMsg = typeof deprecated === 'string' ? `: ${deprecated}` : ''; + emitWarning(`${key} is a deprecated option${deprecatedMsg}`); + } + + setOption(mongoOptions, key, descriptor, values); + } + } + + if (mongoOptions.credentials) { + const isGssapi = mongoOptions.credentials.mechanism === AuthMechanism.MONGODB_GSSAPI; + const isX509 = mongoOptions.credentials.mechanism === AuthMechanism.MONGODB_X509; + const isAws = mongoOptions.credentials.mechanism === AuthMechanism.MONGODB_AWS; + const isOidc = mongoOptions.credentials.mechanism === AuthMechanism.MONGODB_OIDC; + if ( + (isGssapi || isX509) && + allProvidedOptions.has('authSource') && + mongoOptions.credentials.source !== '$external' + ) { + // If authSource was explicitly given and its incorrect, we error + throw new MongoParseError( + `authMechanism ${mongoOptions.credentials.mechanism} requires an authSource of '$external'` + ); + } + + if ( + !(isGssapi || isX509 || isAws || isOidc) && + mongoOptions.dbName && + !allProvidedOptions.has('authSource') + ) { + // inherit the dbName unless GSSAPI or X509, then silently ignore dbName + // and there was no specific authSource given + mongoOptions.credentials = MongoCredentials.merge(mongoOptions.credentials, { + source: mongoOptions.dbName + }); + } + + if (isAws) { + const { username, password } = mongoOptions.credentials; + if (username || password) { + throw new MongoAPIError( + 'username and password cannot be provided when using MONGODB-AWS. Credentials must be provided in a manner that can be read by the AWS SDK.' + ); + } + if (mongoOptions.credentials.mechanismProperties.AWS_SESSION_TOKEN) { + throw new MongoAPIError( + 'AWS_SESSION_TOKEN cannot be provided when using MONGODB-AWS. Credentials must be provided in a manner that can be read by the AWS SDK.' + ); + } + } + + mongoOptions.credentials.validate(); + + // Check if the only auth related option provided was authSource, if so we can remove credentials + if ( + mongoOptions.credentials.password === '' && + mongoOptions.credentials.username === '' && + mongoOptions.credentials.mechanism === AuthMechanism.MONGODB_DEFAULT && + Object.keys(mongoOptions.credentials.mechanismProperties).length === 0 + ) { + delete mongoOptions.credentials; + } + } + + if (!mongoOptions.dbName) { + // dbName default is applied here because of the credential validation above + mongoOptions.dbName = 'test'; + } + + validateLoadBalancedOptions(hosts, mongoOptions, isSRV); + + if (mongoClient && mongoOptions.autoEncryption) { + Encrypter.checkForMongoCrypt(); + mongoOptions.encrypter = new Encrypter(mongoClient, uri, options); + mongoOptions.autoEncrypter = mongoOptions.encrypter.autoEncrypter; + } + + // Potential SRV Overrides and SRV connection string validations + + mongoOptions.userSpecifiedAuthSource = + objectOptions.has('authSource') || urlOptions.has('authSource'); + mongoOptions.userSpecifiedReplicaSet = + objectOptions.has('replicaSet') || urlOptions.has('replicaSet'); + + if (isSRV) { + // SRV Record is resolved upon connecting + mongoOptions.srvHost = hosts[0]; + + if (mongoOptions.directConnection) { + throw new MongoAPIError('SRV URI does not support directConnection'); + } + + if (mongoOptions.srvMaxHosts > 0 && typeof mongoOptions.replicaSet === 'string') { + throw new MongoParseError('Cannot use srvMaxHosts option with replicaSet'); + } + + // SRV turns on TLS by default, but users can override and turn it off + const noUserSpecifiedTLS = !objectOptions.has('tls') && !urlOptions.has('tls'); + const noUserSpecifiedSSL = !objectOptions.has('ssl') && !urlOptions.has('ssl'); + if (noUserSpecifiedTLS && noUserSpecifiedSSL) { + mongoOptions.tls = true; + } + } else { + const userSpecifiedSrvOptions = + urlOptions.has('srvMaxHosts') || + objectOptions.has('srvMaxHosts') || + urlOptions.has('srvServiceName') || + objectOptions.has('srvServiceName'); + + if (userSpecifiedSrvOptions) { + throw new MongoParseError( + 'Cannot use srvMaxHosts or srvServiceName with a non-srv connection string' + ); + } + } + + if (mongoOptions.directConnection && mongoOptions.hosts.length !== 1) { + throw new MongoParseError('directConnection option requires exactly one host'); + } + + if ( + !mongoOptions.proxyHost && + (mongoOptions.proxyPort || mongoOptions.proxyUsername || mongoOptions.proxyPassword) + ) { + throw new MongoParseError('Must specify proxyHost if other proxy options are passed'); + } + + if ( + (mongoOptions.proxyUsername && !mongoOptions.proxyPassword) || + (!mongoOptions.proxyUsername && mongoOptions.proxyPassword) + ) { + throw new MongoParseError('Can only specify both of proxy username/password or neither'); + } + + const proxyOptions = ['proxyHost', 'proxyPort', 'proxyUsername', 'proxyPassword'].map( + key => urlOptions.get(key) ?? [] + ); + + if (proxyOptions.some(options => options.length > 1)) { + throw new MongoParseError( + 'Proxy options cannot be specified multiple times in the connection string' + ); + } + + mongoOptions.mongoLoggerOptions = MongoLogger.resolveOptions( + { + MONGODB_LOG_COMMAND: process.env.MONGODB_LOG_COMMAND, + MONGODB_LOG_TOPOLOGY: process.env.MONGODB_LOG_TOPOLOGY, + MONGODB_LOG_SERVER_SELECTION: process.env.MONGODB_LOG_SERVER_SELECTION, + MONGODB_LOG_CONNECTION: process.env.MONGODB_LOG_CONNECTION, + MONGODB_LOG_CLIENT: process.env.MONGODB_LOG_CLIENT, + MONGODB_LOG_ALL: process.env.MONGODB_LOG_ALL, + MONGODB_LOG_MAX_DOCUMENT_LENGTH: process.env.MONGODB_LOG_MAX_DOCUMENT_LENGTH, + MONGODB_LOG_PATH: process.env.MONGODB_LOG_PATH + }, + { + mongodbLogPath: mongoOptions.mongodbLogPath, + mongodbLogComponentSeverities: mongoOptions.mongodbLogComponentSeverities, + mongodbLogMaxDocumentLength: mongoOptions.mongodbLogMaxDocumentLength + } + ); + + mongoOptions.runtime = resolveRuntimeAdapters(options); + + return mongoOptions; +} + +/** + * #### Throws if LB mode is true: + * - hosts contains more than one host + * - there is a replicaSet name set + * - directConnection is set + * - if srvMaxHosts is used when an srv connection string is passed in + * + * @throws MongoParseError + */ +function validateLoadBalancedOptions( + hosts: HostAddress[] | string[], + mongoOptions: MongoOptions, + isSrv: boolean +): void { + if (mongoOptions.loadBalanced) { + if (hosts.length > 1) { + throw new MongoParseError(LB_SINGLE_HOST_ERROR); + } + if (mongoOptions.replicaSet) { + throw new MongoParseError(LB_REPLICA_SET_ERROR); + } + if (mongoOptions.directConnection) { + throw new MongoParseError(LB_DIRECT_CONNECTION_ERROR); + } + + if (isSrv && mongoOptions.srvMaxHosts > 0) { + throw new MongoParseError('Cannot limit srv hosts with loadBalanced enabled'); + } + } + return; +} + +function setOption( + mongoOptions: any, + key: string, + descriptor: OptionDescriptor, + values: unknown[] +) { + const { target, type, transform } = descriptor; + const name = target ?? key; + + switch (type) { + case 'boolean': + mongoOptions[name] = getBoolean(name, values[0]); + break; + case 'int': + mongoOptions[name] = getIntFromOptions(name, values[0]); + break; + case 'uint': + mongoOptions[name] = getUIntFromOptions(name, values[0]); + break; + case 'string': + if (values[0] == null) { + break; + } + // The value should always be a string here, but since the array is typed as unknown + // there still needs to be an explicit cast. + // eslint-disable-next-line @typescript-eslint/no-base-to-string + mongoOptions[name] = String(values[0]); + break; + case 'record': + if (!isRecord(values[0])) { + throw new MongoParseError(`${name} must be an object`); + } + mongoOptions[name] = values[0]; + break; + case 'any': + mongoOptions[name] = values[0]; + break; + default: { + if (!transform) { + throw new MongoParseError('Descriptors missing a type must define a transform'); + } + const transformValue = transform({ name, options: mongoOptions, values }); + mongoOptions[name] = transformValue; + break; + } + } +} + +interface OptionDescriptor { + target?: string; + type?: 'boolean' | 'int' | 'uint' | 'record' | 'string' | 'any'; + default?: any; + + deprecated?: boolean | string; + /** + * @param name - the original option name + * @param options - the options so far for resolution + * @param values - the possible values in precedence order + */ + transform?: (args: { name: string; options: MongoOptions; values: unknown[] }) => unknown; +} + +export const OPTIONS = { + enableOverloadRetargeting: { + default: false, + type: 'boolean' + }, + appName: { + type: 'string' + }, + auth: { + target: 'credentials', + transform({ name, options, values: [value] }): MongoCredentials { + if (!isRecord(value, ['username', 'password'] as const)) { + throw new MongoParseError( + `${name} must be an object with 'username' and 'password' properties` + ); + } + return MongoCredentials.merge(options.credentials, { + username: value.username, + password: value.password + }); + } + }, + authMechanism: { + target: 'credentials', + transform({ options, values: [value] }): MongoCredentials { + const mechanisms = Object.values(AuthMechanism); + const [mechanism] = mechanisms.filter(m => m.match(RegExp(String.raw`\b${value}\b`, 'i'))); + if (!mechanism) { + throw new MongoParseError(`authMechanism one of ${mechanisms}, got ${value}`); + } + let source = options.credentials?.source; + if ( + mechanism === AuthMechanism.MONGODB_PLAIN || + AUTH_MECHS_AUTH_SRC_EXTERNAL.has(mechanism) + ) { + // some mechanisms have '$external' as the Auth Source + source = '$external'; + } + + let password = options.credentials?.password; + if (mechanism === AuthMechanism.MONGODB_X509 && password === '') { + password = undefined; + } + return MongoCredentials.merge(options.credentials, { + mechanism, + source, + password + }); + } + }, + // Note that if the authMechanismProperties contain a TOKEN_RESOURCE that has a + // comma in it, it MUST be supplied as a MongoClient option instead of in the + // connection string. + authMechanismProperties: { + target: 'credentials', + transform({ options, values }): MongoCredentials { + // We can have a combination of options passed in the URI and options passed + // as an object to the MongoClient. So we must transform the string options + // as well as merge them together with a potentially provided object. + let mechanismProperties = Object.create(null); + + for (const optionValue of values) { + if (typeof optionValue === 'string') { + for (const [key, value] of entriesFromString(optionValue)) { + try { + mechanismProperties[key] = getBoolean(key, value); + } catch { + mechanismProperties[key] = value; + } + } + } else { + if (!isRecord(optionValue)) { + throw new MongoParseError('AuthMechanismProperties must be an object'); + } + mechanismProperties = { ...optionValue }; + } + } + return MongoCredentials.merge(options.credentials, { + mechanismProperties + }); + } + }, + authSource: { + target: 'credentials', + transform({ options, values: [value] }): MongoCredentials { + const source = String(value); + return MongoCredentials.merge(options.credentials, { source }); + } + }, + autoEncryption: { + type: 'record' + }, + autoSelectFamily: { + type: 'boolean', + default: true + }, + autoSelectFamilyAttemptTimeout: { + type: 'uint' + }, + bsonRegExp: { + type: 'boolean' + }, + serverApi: { + target: 'serverApi', + transform({ values: [version] }): ServerApi { + const serverApiToValidate = + typeof version === 'string' ? ({ version } as ServerApi) : (version as ServerApi); + const versionToValidate = serverApiToValidate && serverApiToValidate.version; + if (!versionToValidate) { + throw new MongoParseError( + `Invalid \`serverApi\` property; must specify a version from the following enum: ["${Object.values( + ServerApiVersion + ).join('", "')}"]` + ); + } + if (!Object.values(ServerApiVersion).some(v => v === versionToValidate)) { + throw new MongoParseError( + `Invalid server API version=${versionToValidate}; must be in the following enum: ["${Object.values( + ServerApiVersion + ).join('", "')}"]` + ); + } + return serverApiToValidate; + } + }, + checkKeys: { + type: 'boolean' + }, + compressors: { + default: 'none', + target: 'compressors', + transform({ values }) { + const compressionList = new Set(); + for (const compVal of values as (CompressorName[] | string)[]) { + const compValArray = typeof compVal === 'string' ? compVal.split(',') : compVal; + if (!Array.isArray(compValArray)) { + throw new MongoInvalidArgumentError( + 'compressors must be an array or a comma-delimited list of strings' + ); + } + for (const c of compValArray) { + if (Object.keys(Compressor).includes(String(c))) { + compressionList.add(String(c)); + } else { + throw new MongoInvalidArgumentError( + `${c} is not a valid compression mechanism. Must be one of: ${Object.keys( + Compressor + )}.` + ); + } + } + } + return [...compressionList]; + } + }, + connectTimeoutMS: { + default: 30000, + type: 'uint' + }, + dbName: { + type: 'string' + }, + directConnection: { + default: false, + type: 'boolean' + }, + driverInfo: { + default: {}, + type: 'record' + }, + enableUtf8Validation: { type: 'boolean', default: true }, + family: { + transform({ name, values: [value] }): 4 | 6 { + const transformValue = getIntFromOptions(name, value); + if (transformValue === 4 || transformValue === 6) { + return transformValue; + } + throw new MongoParseError(`Option 'family' must be 4 or 6 got ${transformValue}.`); + } + }, + fieldsAsRaw: { + type: 'record' + }, + forceServerObjectId: { + default: false, + type: 'boolean' + }, + fsync: { + deprecated: 'Please use journal instead', + target: 'writeConcern', + transform({ name, options, values: [value] }): WriteConcern { + const wc = WriteConcern.fromOptions({ + writeConcern: { + ...options.writeConcern, + fsync: getBoolean(name, value) + } + }); + if (!wc) throw new MongoParseError(`Unable to make a writeConcern from fsync=${value}`); + return wc; + } + } as OptionDescriptor, + heartbeatFrequencyMS: { + default: 10000, + type: 'uint' + }, + ignoreUndefined: { + type: 'boolean' + }, + j: { + deprecated: 'Please use journal instead', + target: 'writeConcern', + transform({ name, options, values: [value] }): WriteConcern { + const wc = WriteConcern.fromOptions({ + writeConcern: { + ...options.writeConcern, + journal: getBoolean(name, value) + } + }); + if (!wc) throw new MongoParseError(`Unable to make a writeConcern from journal=${value}`); + return wc; + } + } as OptionDescriptor, + journal: { + target: 'writeConcern', + transform({ name, options, values: [value] }): WriteConcern { + const wc = WriteConcern.fromOptions({ + writeConcern: { + ...options.writeConcern, + journal: getBoolean(name, value) + } + }); + if (!wc) throw new MongoParseError(`Unable to make a writeConcern from journal=${value}`); + return wc; + } + }, + loadBalanced: { + default: false, + type: 'boolean' + }, + localThresholdMS: { + default: 15, + type: 'uint' + }, + maxAdaptiveRetries: { + default: 2, + type: 'uint' + }, + maxConnecting: { + default: 2, + transform({ name, values: [value] }): number { + const maxConnecting = getUIntFromOptions(name, value); + if (maxConnecting === 0) { + throw new MongoInvalidArgumentError('maxConnecting must be > 0 if specified'); + } + return maxConnecting; + } + }, + maxIdleTimeMS: { + default: 0, + type: 'uint' + }, + maxPoolSize: { + default: 100, + type: 'uint' + }, + maxStalenessSeconds: { + target: 'readPreference', + transform({ name, options, values: [value] }) { + const maxStalenessSeconds = getUIntFromOptions(name, value); + if (options.readPreference) { + return ReadPreference.fromOptions({ + readPreference: { ...options.readPreference, maxStalenessSeconds } + }); + } else { + return new ReadPreference('secondary', undefined, { maxStalenessSeconds }); + } + } + }, + minInternalBufferSize: { + type: 'uint' + }, + minPoolSize: { + default: 0, + type: 'uint' + }, + minHeartbeatFrequencyMS: { + default: 500, + type: 'uint' + }, + monitorCommands: { + default: false, + type: 'boolean' + }, + name: { + target: 'driverInfo', + transform({ values: [value], options }) { + return { ...options.driverInfo, name: String(value) }; + } + } as OptionDescriptor, + noDelay: { + default: true, + type: 'boolean' + }, + pkFactory: { + default: DEFAULT_PK_FACTORY, + transform({ values: [value] }): PkFactory { + if (isRecord(value, ['createPk'] as const) && typeof value.createPk === 'function') { + return value as PkFactory; + } + throw new MongoParseError( + `Option pkFactory must be an object with a createPk function, got ${value}` + ); + } + }, + promoteBuffers: { + type: 'boolean' + }, + promoteLongs: { + type: 'boolean' + }, + promoteValues: { + type: 'boolean' + }, + useBigInt64: { + type: 'boolean' + }, + proxyHost: { + type: 'string' + }, + proxyPassword: { + type: 'string' + }, + proxyPort: { + type: 'uint' + }, + proxyUsername: { + type: 'string' + }, + raw: { + default: false, + type: 'boolean' + }, + readConcern: { + transform({ values: [value], options }) { + if (value instanceof ReadConcern || isRecord(value, ['level'] as const)) { + return ReadConcern.fromOptions({ ...options.readConcern, ...value }); + } + throw new MongoParseError(`ReadConcern must be an object, got ${JSON.stringify(value)}`); + } + }, + readConcernLevel: { + target: 'readConcern', + transform({ values: [level], options }) { + return ReadConcern.fromOptions({ + ...options.readConcern, + level: level as ReadConcernLevel + }); + } + }, + readPreference: { + default: ReadPreference.primary, + transform({ values: [value], options }) { + if (value instanceof ReadPreference) { + return ReadPreference.fromOptions({ + readPreference: { ...options.readPreference, ...value }, + ...value + }); + } + if (isRecord(value, ['mode'] as const)) { + const rp = ReadPreference.fromOptions({ + readPreference: { ...options.readPreference, ...value }, + ...value + }); + if (rp) return rp; + else throw new MongoParseError(`Cannot make read preference from ${JSON.stringify(value)}`); + } + if (typeof value === 'string') { + const rpOpts = { + hedge: options.readPreference?.hedge, + maxStalenessSeconds: options.readPreference?.maxStalenessSeconds + }; + return new ReadPreference( + value as ReadPreferenceMode, + options.readPreference?.tags, + rpOpts + ); + } + throw new MongoParseError(`Unknown ReadPreference value: ${value}`); + } + }, + readPreferenceTags: { + target: 'readPreference', + transform({ + values, + options + }: { + values: Array[]>; + options: MongoClientOptions; + }) { + const tags: Array> = Array.isArray(values[0]) + ? values[0] + : (values as Array); + const readPreferenceTags = []; + for (const tag of tags) { + const readPreferenceTag: TagSet = Object.create(null); + if (typeof tag === 'string') { + for (const [k, v] of entriesFromString(tag)) { + readPreferenceTag[k] = v; + } + } + if (isRecord(tag)) { + for (const [k, v] of Object.entries(tag)) { + readPreferenceTag[k] = v; + } + } + readPreferenceTags.push(readPreferenceTag); + } + return ReadPreference.fromOptions({ + readPreference: options.readPreference, + readPreferenceTags + }); + } + }, + replicaSet: { + type: 'string' + }, + retryReads: { + default: true, + type: 'boolean' + }, + retryWrites: { + default: true, + type: 'boolean' + }, + runtimeAdapters: { + type: 'record' + }, + serializeFunctions: { + type: 'boolean' + }, + serverMonitoringMode: { + default: 'auto', + transform({ values: [value] }) { + if (!Object.values(ServerMonitoringMode).includes(value as any)) { + throw new MongoParseError( + 'serverMonitoringMode must be one of `auto`, `poll`, or `stream`' + ); + } + return value; + } + }, + serverSelectionTimeoutMS: { + default: 30000, + type: 'uint' + }, + servername: { + type: 'string' + }, + socketTimeoutMS: { + // TODO(NODE-6491): deprecated: 'Please use timeoutMS instead', + default: 0, + type: 'uint' + }, + srvMaxHosts: { + type: 'uint', + default: 0 + }, + srvServiceName: { + type: 'string', + default: 'mongodb' + }, + ssl: { + target: 'tls', + type: 'boolean' + }, + timeoutMS: { + type: 'uint' + }, + tls: { + type: 'boolean' + }, + tlsAllowInvalidCertificates: { + target: 'rejectUnauthorized', + transform({ name, values: [value] }) { + // allowInvalidCertificates is the inverse of rejectUnauthorized + return !getBoolean(name, value); + } + }, + tlsAllowInvalidHostnames: { + target: 'checkServerIdentity', + transform({ name, values: [value] }) { + // tlsAllowInvalidHostnames means setting the checkServerIdentity function to a noop + return getBoolean(name, value) ? () => undefined : undefined; + } + }, + tlsCAFile: { + type: 'string' + }, + tlsCRLFile: { + type: 'string' + }, + tlsCertificateKeyFile: { + type: 'string' + }, + tlsCertificateKeyFilePassword: { + target: 'passphrase', + type: 'any' + }, + tlsInsecure: { + transform({ name, options, values: [value] }) { + const tlsInsecure = getBoolean(name, value); + if (tlsInsecure) { + options.checkServerIdentity = () => undefined; + options.rejectUnauthorized = false; + } else { + options.checkServerIdentity = options.tlsAllowInvalidHostnames + ? () => undefined + : undefined; + options.rejectUnauthorized = options.tlsAllowInvalidCertificates ? false : true; + } + return tlsInsecure; + } + }, + w: { + target: 'writeConcern', + transform({ values: [value], options }) { + return WriteConcern.fromOptions({ writeConcern: { ...options.writeConcern, w: value as W } }); + } + }, + waitQueueTimeoutMS: { + // TODO(NODE-6491): deprecated: 'Please use timeoutMS instead', + default: 0, + type: 'uint' + }, + writeConcern: { + target: 'writeConcern', + transform({ values: [value], options }) { + if (isRecord(value) || value instanceof WriteConcern) { + return WriteConcern.fromOptions({ + writeConcern: { + ...options.writeConcern, + ...value + } + }); + } else if (value === 'majority' || typeof value === 'number') { + return WriteConcern.fromOptions({ + writeConcern: { + ...options.writeConcern, + w: value + } + }); + } + + throw new MongoParseError(`Invalid WriteConcern cannot parse: ${JSON.stringify(value)}`); + } + }, + wtimeout: { + deprecated: 'Please use wtimeoutMS instead', + target: 'writeConcern', + transform({ values: [value], options }) { + const wc = WriteConcern.fromOptions({ + writeConcern: { + ...options.writeConcern, + wtimeout: getUIntFromOptions('wtimeout', value) + } + }); + if (wc) return wc; + throw new MongoParseError(`Cannot make WriteConcern from wtimeout`); + } + } as OptionDescriptor, + wtimeoutMS: { + target: 'writeConcern', + transform({ values: [value], options }) { + const wc = WriteConcern.fromOptions({ + writeConcern: { + ...options.writeConcern, + wtimeoutMS: getUIntFromOptions('wtimeoutMS', value) + } + }); + if (wc) return wc; + throw new MongoParseError(`Cannot make WriteConcern from wtimeout`); + } + }, + zlibCompressionLevel: { + default: 0, + type: 'int' + }, + mongodbLogPath: { + transform({ values: [value] }) { + if ( + !( + (typeof value === 'string' && ['stderr', 'stdout'].includes(value)) || + (value && + typeof value === 'object' && + 'write' in value && + typeof value.write === 'function') + ) + ) { + throw new MongoAPIError( + `Option 'mongodbLogPath' must be of type 'stderr' | 'stdout' | MongoDBLogWritable` + ); + } + return value; + } + }, + mongodbLogComponentSeverities: { + transform({ values: [value] }) { + if (typeof value !== 'object' || !value) { + throw new MongoAPIError(`Option 'mongodbLogComponentSeverities' must be a non-null object`); + } + for (const [k, v] of Object.entries(value)) { + if (typeof v !== 'string' || typeof k !== 'string') { + throw new MongoAPIError( + `User input for option 'mongodbLogComponentSeverities' object cannot include a non-string key or value` + ); + } + if (!Object.values(MongoLoggableComponent).some(val => val === k) && k !== 'default') { + throw new MongoAPIError( + `User input for option 'mongodbLogComponentSeverities' contains invalid key: ${k}` + ); + } + if (!Object.values(SeverityLevel).some(val => val === v)) { + throw new MongoAPIError( + `Option 'mongodbLogComponentSeverities' does not support ${v} as a value for ${k}` + ); + } + } + return value; + } + }, + mongodbLogMaxDocumentLength: { type: 'uint' }, + // Custom types for modifying core behavior + connectionType: { type: 'any' }, + srvPoller: { type: 'any' }, + // Accepted Node.js Options + allowPartialTrustChain: { type: 'any' }, + minDHSize: { type: 'any' }, + pskCallback: { type: 'any' }, + secureContext: { type: 'any' }, + enableTrace: { type: 'any' }, + requestCert: { type: 'any' }, + rejectUnauthorized: { type: 'any' }, + checkServerIdentity: { type: 'any' }, + keepAliveInitialDelay: { type: 'any' }, + ALPNProtocols: { type: 'any' }, + SNICallback: { type: 'any' }, + session: { type: 'any' }, + requestOCSP: { type: 'any' }, + localAddress: { type: 'any' }, + localPort: { type: 'any' }, + hints: { type: 'any' }, + lookup: { type: 'any' }, + ca: { type: 'any' }, + cert: { type: 'any' }, + ciphers: { type: 'any' }, + crl: { type: 'any' }, + ecdhCurve: { type: 'any' }, + key: { type: 'any' }, + passphrase: { type: 'any' }, + pfx: { type: 'any' }, + secureProtocol: { type: 'any' }, + index: { type: 'any' }, + // Legacy options from v3 era + __skipPingOnConnect: { type: 'boolean' } +} as Record; + +export const DEFAULT_OPTIONS = new CaseInsensitiveMap( + Object.entries(OPTIONS) + .filter(([, descriptor]) => descriptor.default != null) + .map(([k, d]) => [k, d.default]) +); diff --git a/gateway/node_modules/mongodb/src/constants.ts b/gateway/node_modules/mongodb/src/constants.ts new file mode 100644 index 0000000000000000000000000000000000000000..abb69509f94e95230a6ed272925c61f77f52c013 --- /dev/null +++ b/gateway/node_modules/mongodb/src/constants.ts @@ -0,0 +1,176 @@ +export const SYSTEM_NAMESPACE_COLLECTION = 'system.namespaces'; +export const SYSTEM_INDEX_COLLECTION = 'system.indexes'; +export const SYSTEM_PROFILE_COLLECTION = 'system.profile'; +export const SYSTEM_USER_COLLECTION = 'system.users'; +export const SYSTEM_COMMAND_COLLECTION = '$cmd'; +export const SYSTEM_JS_COLLECTION = 'system.js'; + +// events +export const ERROR = 'error' as const; +export const TIMEOUT = 'timeout' as const; +export const CLOSE = 'close' as const; +export const OPEN = 'open' as const; +export const CONNECT = 'connect' as const; +export const CLOSED = 'closed' as const; +export const ENDED = 'ended' as const; +export const MESSAGE = 'message' as const; +export const PINNED = 'pinned' as const; +export const UNPINNED = 'unpinned' as const; +export const DESCRIPTION_RECEIVED = 'descriptionReceived'; +/** @internal */ +export const SERVER_OPENING = 'serverOpening' as const; +/** @internal */ +export const SERVER_CLOSED = 'serverClosed' as const; +/** @internal */ +export const SERVER_DESCRIPTION_CHANGED = 'serverDescriptionChanged' as const; +/** @internal */ +export const TOPOLOGY_OPENING = 'topologyOpening' as const; +/** @internal */ +export const TOPOLOGY_CLOSED = 'topologyClosed' as const; +/** @internal */ +export const TOPOLOGY_DESCRIPTION_CHANGED = 'topologyDescriptionChanged' as const; +/** @internal */ +export const SERVER_SELECTION_STARTED = 'serverSelectionStarted' as const; +/** @internal */ +export const SERVER_SELECTION_FAILED = 'serverSelectionFailed' as const; +/** @internal */ +export const SERVER_SELECTION_SUCCEEDED = 'serverSelectionSucceeded' as const; +/** @internal */ +export const WAITING_FOR_SUITABLE_SERVER = 'waitingForSuitableServer' as const; +/** @internal */ +export const CONNECTION_POOL_CREATED = 'connectionPoolCreated' as const; +/** @internal */ +export const CONNECTION_POOL_CLOSED = 'connectionPoolClosed' as const; +/** @internal */ +export const CONNECTION_POOL_CLEARED = 'connectionPoolCleared' as const; +/** @internal */ +export const CONNECTION_POOL_READY = 'connectionPoolReady' as const; +/** @internal */ +export const CONNECTION_CREATED = 'connectionCreated' as const; +/** @internal */ +export const CONNECTION_READY = 'connectionReady' as const; +/** @internal */ +export const CONNECTION_CLOSED = 'connectionClosed' as const; +/** @internal */ +export const CONNECTION_CHECK_OUT_STARTED = 'connectionCheckOutStarted' as const; +/** @internal */ +export const CONNECTION_CHECK_OUT_FAILED = 'connectionCheckOutFailed' as const; +/** @internal */ +export const CONNECTION_CHECKED_OUT = 'connectionCheckedOut' as const; +/** @internal */ +export const CONNECTION_CHECKED_IN = 'connectionCheckedIn' as const; +export const CLUSTER_TIME_RECEIVED = 'clusterTimeReceived' as const; +/** @internal */ +export const COMMAND_STARTED = 'commandStarted' as const; +/** @internal */ +export const COMMAND_SUCCEEDED = 'commandSucceeded' as const; +/** @internal */ +export const COMMAND_FAILED = 'commandFailed' as const; +/** @internal */ +export const SERVER_HEARTBEAT_STARTED = 'serverHeartbeatStarted' as const; +/** @internal */ +export const SERVER_HEARTBEAT_SUCCEEDED = 'serverHeartbeatSucceeded' as const; +/** @internal */ +export const SERVER_HEARTBEAT_FAILED = 'serverHeartbeatFailed' as const; +export const RESPONSE = 'response' as const; +export const MORE = 'more' as const; +export const INIT = 'init' as const; +export const CHANGE = 'change' as const; +export const END = 'end' as const; +export const RESUME_TOKEN_CHANGED = 'resumeTokenChanged' as const; + +/** @public */ +export const HEARTBEAT_EVENTS = Object.freeze([ + SERVER_HEARTBEAT_STARTED, + SERVER_HEARTBEAT_SUCCEEDED, + SERVER_HEARTBEAT_FAILED +] as const); + +/** @public */ +export const CMAP_EVENTS = Object.freeze([ + CONNECTION_POOL_CREATED, + CONNECTION_POOL_READY, + CONNECTION_POOL_CLEARED, + CONNECTION_POOL_CLOSED, + CONNECTION_CREATED, + CONNECTION_READY, + CONNECTION_CLOSED, + CONNECTION_CHECK_OUT_STARTED, + CONNECTION_CHECK_OUT_FAILED, + CONNECTION_CHECKED_OUT, + CONNECTION_CHECKED_IN +] as const); + +/** @public */ +export const TOPOLOGY_EVENTS = Object.freeze([ + SERVER_OPENING, + SERVER_CLOSED, + SERVER_DESCRIPTION_CHANGED, + TOPOLOGY_OPENING, + TOPOLOGY_CLOSED, + TOPOLOGY_DESCRIPTION_CHANGED, + ERROR, + TIMEOUT, + CLOSE +] as const); + +/** @public */ +export const APM_EVENTS = Object.freeze([ + COMMAND_STARTED, + COMMAND_SUCCEEDED, + COMMAND_FAILED +] as const); + +/** + * All events that we relay to the `Topology` + * @internal + */ +export const SERVER_RELAY_EVENTS = Object.freeze([ + SERVER_HEARTBEAT_STARTED, + SERVER_HEARTBEAT_SUCCEEDED, + SERVER_HEARTBEAT_FAILED, + COMMAND_STARTED, + COMMAND_SUCCEEDED, + COMMAND_FAILED, + ...CMAP_EVENTS +] as const); + +/** + * All events we listen to from `Server` instances, but do not forward to the client + * @internal + */ +export const LOCAL_SERVER_EVENTS = Object.freeze([ + CONNECT, + DESCRIPTION_RECEIVED, + CLOSED, + ENDED +] as const); + +/** @public */ +export const MONGO_CLIENT_EVENTS = Object.freeze([ + ...CMAP_EVENTS, + ...APM_EVENTS, + ...TOPOLOGY_EVENTS, + ...HEARTBEAT_EVENTS +] as const); + +/** + * @internal + * The legacy hello command that was deprecated in MongoDB 5.0. + */ +export const LEGACY_HELLO_COMMAND = 'ismaster'; + +/** + * @internal + * The legacy hello command that was deprecated in MongoDB 5.0. + */ +export const LEGACY_HELLO_COMMAND_CAMEL_CASE = 'isMaster'; + +// Typescript errors if we index objects with `Symbol.for(...)`, so +// to avoid TS errors we pull them out into variables. Then we can type +// the objects (and class) that we expect to see them on and prevent TS +// errors. +/** @internal */ +export const kDecorateResult = Symbol.for('@@mdb.decorateDecryptionResult'); +/** @internal */ +export const kDecoratedKeys = Symbol.for('@@mdb.decryptedKeys'); diff --git a/gateway/node_modules/mongodb/src/cursor/abstract_cursor.ts b/gateway/node_modules/mongodb/src/cursor/abstract_cursor.ts new file mode 100644 index 0000000000000000000000000000000000000000..693e1204d81ba52fc112351869a8a6b288370872 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cursor/abstract_cursor.ts @@ -0,0 +1,1246 @@ +import { Readable } from 'stream'; + +import { type BSONSerializeOptions, type Document, Long, pluckBSONSerializeOptions } from '../bson'; +import { type OnDemandDocumentDeserializeOptions } from '../cmap/wire_protocol/on_demand/document'; +import { type CursorResponse } from '../cmap/wire_protocol/responses'; +import { + MongoAPIError, + MongoCursorExhaustedError, + MongoCursorInUseError, + MongoInvalidArgumentError, + MongoRuntimeError, + MongoTailableCursorError +} from '../error'; +import type { MongoClient } from '../mongo_client'; +import { type Abortable, TypedEventEmitter } from '../mongo_types'; +import { executeOperation } from '../operations/execute_operation'; +import { GetMoreOperation } from '../operations/get_more'; +import { KillCursorsOperation } from '../operations/kill_cursors'; +import { ReadConcern, type ReadConcernLike } from '../read_concern'; +import { ReadPreference, type ReadPreferenceLike } from '../read_preference'; +import type { Server } from '../sdam/server'; +import { type ClientSession, maybeClearPinnedConnection } from '../sessions'; +import { type CSOTTimeoutContext, type Timeout, TimeoutContext } from '../timeout'; +import { + addAbortListener, + type Disposable, + kDispose, + type MongoDBNamespace, + noop, + squashError +} from '../utils'; + +/** + * @internal + * TODO(NODE-2882): A cursor's getMore commands must be run on the same server it was started on + * and the same session must be used for the lifetime of the cursor. This object serves to get the + * server and session (along with the response) out of executeOperation back to the AbstractCursor. + * + * There may be a better design for communicating these values back to the cursor, currently an operation + * MUST store the selected server on itself so it can be read after executeOperation has returned. + */ +export interface InitialCursorResponse { + /** The server selected for the operation */ + server: Server; + /** The session used for this operation, may be implicitly created */ + session?: ClientSession; + /** The raw server response for the operation */ + response: CursorResponse; +} + +/** @public */ +export const CURSOR_FLAGS = [ + 'tailable', + 'oplogReplay', + 'noCursorTimeout', + 'awaitData', + 'exhaust', + 'partial' +] as const; + +/** @public */ +export type CursorFlag = (typeof CURSOR_FLAGS)[number]; + +function removeActiveCursor(this: AbstractCursor) { + this.client.s.activeCursors.delete(this); +} + +/** + * @public + * @experimental + * Specifies how `timeoutMS` is applied to the cursor. Can be either `'cursorLifeTime'` or `'iteration'` + * When set to `'iteration'`, the deadline specified by `timeoutMS` applies to each call of + * `cursor.next()`. + * When set to `'cursorLifetime'`, the deadline applies to the life of the entire cursor. + * + * Depending on the type of cursor being used, this option has different default values. + * For non-tailable cursors, this value defaults to `'cursorLifetime'` + * For tailable cursors, this value defaults to `'iteration'` since tailable cursors, by + * definition can have an arbitrarily long lifetime. + * + * @example + * ```ts + * const cursor = collection.find({}, {timeoutMS: 100, timeoutMode: 'iteration'}); + * for await (const doc of cursor) { + * // process doc + * // This will throw a timeout error if any of the iterator's `next()` calls takes more than 100ms, but + * // will continue to iterate successfully otherwise, regardless of the number of batches. + * } + * ``` + * + * @example + * ```ts + * const cursor = collection.find({}, { timeoutMS: 1000, timeoutMode: 'cursorLifetime' }); + * const docs = await cursor.toArray(); // This entire line will throw a timeout error if all batches are not fetched and returned within 1000ms. + * ``` + */ +export const CursorTimeoutMode = Object.freeze({ + ITERATION: 'iteration', + LIFETIME: 'cursorLifetime' +} as const); + +/** + * @public + * @experimental + */ +export type CursorTimeoutMode = (typeof CursorTimeoutMode)[keyof typeof CursorTimeoutMode]; + +/** @public */ +export interface AbstractCursorOptions extends BSONSerializeOptions { + session?: ClientSession; + readPreference?: ReadPreferenceLike; + readConcern?: ReadConcernLike; + /** + * Specifies the number of documents to return in each response from MongoDB + */ + batchSize?: number; + /** + * When applicable `maxTimeMS` controls the amount of time the initial command + * that constructs a cursor should take. (ex. find, aggregate, listCollections) + */ + maxTimeMS?: number; + /** + * When applicable `maxAwaitTimeMS` controls the amount of time subsequent getMores + * that a cursor uses to fetch more data should take. (ex. cursor.next()) + */ + maxAwaitTimeMS?: number; + /** + * Comment to apply to the operation. + * + * In server versions pre-4.4, 'comment' must be string. A server + * error will be thrown if any other type is provided. + * + * In server versions 4.4 and above, 'comment' can be any valid BSON type. + */ + comment?: unknown; + /** + * By default, MongoDB will automatically close a cursor when the + * client has exhausted all results in the cursor. However, for [capped collections](https://www.mongodb.com/docs/manual/core/capped-collections) + * you may use a Tailable Cursor that remains open after the client exhausts + * the results in the initial cursor. + */ + tailable?: boolean; + /** + * If awaitData is set to true, when the cursor reaches the end of the capped collection, + * MongoDB blocks the query thread for a period of time waiting for new data to arrive. + * When new data is inserted into the capped collection, the blocked thread is signaled + * to wake up and return the next batch to the client. + */ + awaitData?: boolean; + noCursorTimeout?: boolean; + /** Specifies the time an operation will run until it throws a timeout error. See {@link AbstractCursorOptions.timeoutMode} for more details on how this option applies to cursors. */ + timeoutMS?: number; + /** + * @public + * @experimental + * Specifies how `timeoutMS` is applied to the cursor. Can be either `'cursorLifeTime'` or `'iteration'` + * When set to `'iteration'`, the deadline specified by `timeoutMS` applies to each call of + * `cursor.next()`. + * When set to `'cursorLifetime'`, the deadline applies to the life of the entire cursor. + * + * Depending on the type of cursor being used, this option has different default values. + * For non-tailable cursors, this value defaults to `'cursorLifetime'` + * For tailable cursors, this value defaults to `'iteration'` since tailable cursors, by + * definition can have an arbitrarily long lifetime. + * + * @example + * ```ts + * const cursor = collection.find({}, {timeoutMS: 100, timeoutMode: 'iteration'}); + * for await (const doc of cursor) { + * // process doc + * // This will throw a timeout error if any of the iterator's `next()` calls takes more than 100ms, but + * // will continue to iterate successfully otherwise, regardless of the number of batches. + * } + * ``` + * + * @example + * ```ts + * const cursor = collection.find({}, { timeoutMS: 1000, timeoutMode: 'cursorLifetime' }); + * const docs = await cursor.toArray(); // This entire line will throw a timeout error if all batches are not fetched and returned within 1000ms. + * ``` + */ + timeoutMode?: CursorTimeoutMode; + + /** + * @internal + * + * A timeout context to govern the total time the cursor can live. If provided, the cursor + * cannot be used in ITERATION mode. + */ + timeoutContext?: CursorTimeoutContext; +} + +/** @internal */ +export type InternalAbstractCursorOptions = Omit & { + // resolved + readPreference: ReadPreference; + readConcern?: ReadConcern; + + // cursor flags, some are deprecated + oplogReplay?: boolean; + exhaust?: boolean; + partial?: boolean; + + omitMaxTimeMS?: boolean; +}; + +/** @public */ +export type AbstractCursorEvents = { + [AbstractCursor.CLOSE](): void; +}; + +/** @public */ +export abstract class AbstractCursor< + TSchema = any, + CursorEvents extends AbstractCursorEvents = AbstractCursorEvents + > + extends TypedEventEmitter + implements AsyncDisposable +{ + /** @internal */ + private cursorId: Long | null; + /** @internal */ + private cursorSession: ClientSession | null; + /** @internal */ + private selectedServer?: Server; + /** @internal */ + private cursorNamespace: MongoDBNamespace; + /** @internal */ + private documents: CursorResponse | null = null; + /** @internal */ + private cursorClient: MongoClient; + /** @internal */ + private transform?: (doc: TSchema) => any; + /** + * @internal + * This is true whether or not the first command fails. It only indicates whether or not the first + * command has been run. + */ + private initialized: boolean; + /** @internal */ + private isClosed: boolean; + /** @internal */ + private isKilled: boolean; + /** @internal */ + protected readonly cursorOptions: InternalAbstractCursorOptions; + /** @internal */ + protected timeoutContext?: CursorTimeoutContext; + + /** @event */ + static readonly CLOSE = 'close' as const; + + /** @internal */ + protected deserializationOptions: OnDemandDocumentDeserializeOptions; + protected signal: AbortSignal | undefined; + private abortListener: Disposable | undefined; + + /** @internal */ + protected constructor( + client: MongoClient, + namespace: MongoDBNamespace, + options: AbstractCursorOptions & Abortable = {} + ) { + super(); + this.on('error', noop); + + if (!client.s.isMongoClient) { + throw new MongoRuntimeError('Cursor must be constructed with MongoClient'); + } + this.cursorClient = client; + this.cursorNamespace = namespace; + this.cursorId = null; + this.initialized = false; + this.isClosed = false; + this.isKilled = false; + this.cursorOptions = { + readPreference: + options.readPreference && options.readPreference instanceof ReadPreference + ? options.readPreference + : ReadPreference.primary, + ...pluckBSONSerializeOptions(options), + timeoutMS: options?.timeoutContext?.csotEnabled() + ? options.timeoutContext.timeoutMS + : options.timeoutMS, + tailable: options.tailable, + awaitData: options.awaitData + }; + + if (this.cursorOptions.timeoutMS != null) { + if (options.timeoutMode == null) { + if (options.tailable) { + if (options.awaitData) { + if ( + options.maxAwaitTimeMS != null && + options.maxAwaitTimeMS >= this.cursorOptions.timeoutMS + ) + throw new MongoInvalidArgumentError( + 'Cannot specify maxAwaitTimeMS >= timeoutMS for a tailable awaitData cursor' + ); + } + + this.cursorOptions.timeoutMode = CursorTimeoutMode.ITERATION; + } else { + this.cursorOptions.timeoutMode = CursorTimeoutMode.LIFETIME; + } + } else { + if (options.tailable && options.timeoutMode === CursorTimeoutMode.LIFETIME) { + throw new MongoInvalidArgumentError( + "Cannot set tailable cursor's timeoutMode to LIFETIME" + ); + } + this.cursorOptions.timeoutMode = options.timeoutMode; + } + } else { + if (options.timeoutMode != null) + throw new MongoInvalidArgumentError('Cannot set timeoutMode without setting timeoutMS'); + } + + // Set for initial command + this.cursorOptions.omitMaxTimeMS = + this.cursorOptions.timeoutMS != null && + ((this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION && + !this.cursorOptions.tailable) || + (this.cursorOptions.tailable && !this.cursorOptions.awaitData)); + + const readConcern = ReadConcern.fromOptions(options); + if (readConcern) { + this.cursorOptions.readConcern = readConcern; + } + + if (typeof options.batchSize === 'number') { + this.cursorOptions.batchSize = options.batchSize; + } + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (options.comment !== undefined) { + this.cursorOptions.comment = options.comment; + } + + if (typeof options.maxTimeMS === 'number') { + this.cursorOptions.maxTimeMS = options.maxTimeMS; + } + + if (typeof options.maxAwaitTimeMS === 'number') { + this.cursorOptions.maxAwaitTimeMS = options.maxAwaitTimeMS; + } + + this.cursorSession = options.session ?? null; + + this.deserializationOptions = { + ...this.cursorOptions, + validation: { + utf8: options?.enableUtf8Validation === false ? false : true + } + }; + + this.timeoutContext = options.timeoutContext; + this.signal = options.signal; + this.abortListener = addAbortListener( + this.signal, + () => void this.close().then(undefined, squashError) + ); + this.trackCursor(); + } + + /** + * The cursor has no id until it receives a response from the initial cursor creating command. + * + * It is non-zero for as long as the database has an open cursor. + * + * The initiating command may receive a zero id if the entire result is in the `firstBatch`. + */ + get id(): Long | undefined { + return this.cursorId ?? undefined; + } + + /** @internal */ + get isDead() { + return (this.cursorId?.isZero() ?? false) || this.isClosed || this.isKilled; + } + + /** @internal */ + get client(): MongoClient { + return this.cursorClient; + } + + /** @internal */ + get server(): Server | undefined { + return this.selectedServer; + } + + get namespace(): MongoDBNamespace { + return this.cursorNamespace; + } + + get readPreference(): ReadPreference { + return this.cursorOptions.readPreference; + } + + get readConcern(): ReadConcern | undefined { + return this.cursorOptions.readConcern; + } + + /** @internal */ + get session(): ClientSession | null { + return this.cursorSession; + } + + set session(clientSession: ClientSession) { + this.cursorSession = clientSession; + } + + /** + * The cursor is closed and all remaining locally buffered documents have been iterated. + */ + get closed(): boolean { + return this.isClosed && (this.documents?.length ?? 0) === 0; + } + + /** + * A `killCursors` command was attempted on this cursor. + * This is performed if the cursor id is non zero. + */ + get killed(): boolean { + return this.isKilled; + } + + get loadBalanced(): boolean { + return !!this.cursorClient.topology?.loadBalanced; + } + + /** + * An alias for {@link AbstractCursor.close|AbstractCursor.close()}. + */ + async [Symbol.asyncDispose]() { + await this.close(); + } + + /** Adds cursor to client's tracking so it will be closed by MongoClient.close() */ + private trackCursor() { + this.cursorClient.s.activeCursors.add(this); + if (!this.listeners('close').includes(removeActiveCursor)) { + this.once('close', removeActiveCursor); + } + } + + /** Returns current buffered documents length */ + bufferedCount(): number { + return this.documents?.length ?? 0; + } + + /** Returns current buffered documents */ + readBufferedDocuments(number?: number): NonNullable[] { + const bufferedDocs: NonNullable[] = []; + const documentsToRead = Math.min( + number ?? this.documents?.length ?? 0, + this.documents?.length ?? 0 + ); + + for (let count = 0; count < documentsToRead; count++) { + const document = this.documents?.shift(this.deserializationOptions); + if (document != null) { + bufferedDocs.push(document); + } + } + + return bufferedDocs; + } + + async *[Symbol.asyncIterator](): AsyncGenerator { + this.signal?.throwIfAborted(); + + if (this.closed) { + return; + } + + try { + while (true) { + if (this.isKilled) { + return; + } + + if (this.closed) { + return; + } + + if (this.cursorId != null && this.isDead && (this.documents?.length ?? 0) === 0) { + return; + } + + const document = await this.next(); + + // eslint-disable-next-line no-restricted-syntax + if (document === null) { + return; + } + + yield document; + + this.signal?.throwIfAborted(); + } + } finally { + // Only close the cursor if it has not already been closed. This finally clause handles + // the case when a user would break out of a for await of loop early. + if (!this.isClosed) { + try { + await this.close(); + } catch (error) { + squashError(error); + } + } + } + } + + stream(): Readable & AsyncIterable { + const readable = new ReadableCursorStream(this); + const abortListener = addAbortListener(this.signal, function () { + readable.destroy(this.reason); + }); + readable.once('end', () => { + abortListener?.[kDispose](); + }); + + return readable; + } + + async hasNext(): Promise { + this.signal?.throwIfAborted(); + + if (this.cursorId === Long.ZERO) { + return false; + } + + if (this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION && this.cursorId != null) { + this.timeoutContext?.refresh(); + } + try { + do { + if ((this.documents?.length ?? 0) !== 0) { + return true; + } + await this.fetchBatch(); + } while (!this.isDead || (this.documents?.length ?? 0) !== 0); + } finally { + if (this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION) { + this.timeoutContext?.clear(); + } + } + + return false; + } + + /** Get the next available document from the cursor, returns null if no more documents are available. */ + async next(): Promise { + this.signal?.throwIfAborted(); + + if (this.cursorId === Long.ZERO) { + throw new MongoCursorExhaustedError(); + } + + if (this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION && this.cursorId != null) { + this.timeoutContext?.refresh(); + } + + try { + do { + const doc = this.documents?.shift(this.deserializationOptions); + if (doc != null) { + if (this.transform != null) return await this.transformDocument(doc); + return doc; + } + await this.fetchBatch(); + } while (!this.isDead || (this.documents?.length ?? 0) !== 0); + } finally { + if (this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION) { + this.timeoutContext?.clear(); + } + } + + return null; + } + + /** + * Try to get the next available document from the cursor or `null` if an empty batch is returned + */ + async tryNext(): Promise { + this.signal?.throwIfAborted(); + + if (this.cursorId === Long.ZERO) { + throw new MongoCursorExhaustedError(); + } + + if (this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION && this.cursorId != null) { + this.timeoutContext?.refresh(); + } + try { + let doc = this.documents?.shift(this.deserializationOptions); + if (doc != null) { + if (this.transform != null) return await this.transformDocument(doc); + return doc; + } + + await this.fetchBatch(); + + doc = this.documents?.shift(this.deserializationOptions); + if (doc != null) { + if (this.transform != null) return await this.transformDocument(doc); + return doc; + } + } finally { + if (this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION) { + this.timeoutContext?.clear(); + } + } + + return null; + } + + /** + * Iterates over all the documents for this cursor using the iterator, callback pattern. + * + * If the iterator returns `false`, iteration will stop. + * + * @param iterator - The iteration callback. + * @deprecated - Will be removed in a future release. Use for await...of instead. + */ + async forEach(iterator: (doc: TSchema) => boolean | void): Promise { + this.signal?.throwIfAborted(); + + if (typeof iterator !== 'function') { + throw new MongoInvalidArgumentError('Argument "iterator" must be a function'); + } + for await (const document of this) { + const result = iterator(document); + if (result === false) { + break; + } + } + } + + /** + * Frees any client-side resources used by the cursor. + */ + async close(options?: { timeoutMS?: number }): Promise { + await this.cleanup(options?.timeoutMS); + } + + /** + * Returns an array of documents. The caller is responsible for making sure that there + * is enough memory to store the results. Note that the array only contains partial + * results when this cursor had been previously accessed. In that case, + * cursor.rewind() can be used to reset the cursor. + */ + async toArray(): Promise { + this.signal?.throwIfAborted(); + + const array: TSchema[] = []; + // at the end of the loop (since readBufferedDocuments is called) the buffer will be empty + // then, the 'await of' syntax will run a getMore call + for await (const document of this) { + array.push(document); + const docs = this.readBufferedDocuments(); + if (this.transform != null) { + for (const doc of docs) { + array.push(await this.transformDocument(doc)); + } + } else { + // Note: previous versions of this logic used `array.push(...)`, which adds each item + // to the callstack. For large arrays, this can exceed the maximum call size. + for (const doc of docs) { + array.push(doc); + } + } + } + return array; + } + /** + * Add a cursor flag to the cursor + * + * @param flag - The flag to set, must be one of following ['tailable', 'oplogReplay', 'noCursorTimeout', 'awaitData', 'partial' -. + * @param value - The flag boolean value. + */ + addCursorFlag(flag: CursorFlag, value: boolean): this { + this.throwIfInitialized(); + if (!CURSOR_FLAGS.includes(flag)) { + throw new MongoInvalidArgumentError(`Flag ${flag} is not one of ${CURSOR_FLAGS}`); + } + + if (typeof value !== 'boolean') { + throw new MongoInvalidArgumentError(`Flag ${flag} must be a boolean value`); + } + + this.cursorOptions[flag] = value; + return this; + } + + /** + * Map all documents using the provided function + * If there is a transform set on the cursor, that will be called first and the result passed to + * this function's transform. + * + * @remarks + * + * **Note** Cursors use `null` internally to indicate that there are no more documents in the cursor. Providing a mapping + * function that maps values to `null` will result in the cursor closing itself before it has finished iterating + * all documents. This will **not** result in a memory leak, just surprising behavior. For example: + * + * ```typescript + * const cursor = collection.find({}); + * cursor.map(() => null); + * + * const documents = await cursor.toArray(); + * // documents is always [], regardless of how many documents are in the collection. + * ``` + * + * Other falsey values are allowed: + * + * ```typescript + * const cursor = collection.find({}); + * cursor.map(() => ''); + * + * const documents = await cursor.toArray(); + * // documents is now an array of empty strings + * ``` + * + * **Note for Typescript Users:** adding a transform changes the return type of the iteration of this cursor, + * it **does not** return a new instance of a cursor. This means when calling map, + * you should always assign the result to a new variable in order to get a correctly typed cursor variable. + * Take note of the following example: + * + * @example + * ```typescript + * const cursor: FindCursor = coll.find(); + * const mappedCursor: FindCursor = cursor.map(doc => Object.keys(doc).length); + * const keyCounts: number[] = await mappedCursor.toArray(); // cursor.toArray() still returns Document[] + * ``` + * @param transform - The mapping transformation method. + */ + map(transform: (doc: TSchema) => T): AbstractCursor { + this.throwIfInitialized(); + const oldTransform = this.transform; + if (oldTransform) { + this.transform = doc => { + return transform(oldTransform(doc)); + }; + } else { + this.transform = transform; + } + + return this as unknown as AbstractCursor; + } + + /** + * Set the ReadPreference for the cursor. + * + * @param readPreference - The new read preference for the cursor. + */ + withReadPreference(readPreference: ReadPreferenceLike): this { + this.throwIfInitialized(); + if (readPreference instanceof ReadPreference) { + this.cursorOptions.readPreference = readPreference; + } else if (typeof readPreference === 'string') { + this.cursorOptions.readPreference = ReadPreference.fromString(readPreference); + } else { + throw new MongoInvalidArgumentError(`Invalid read preference: ${readPreference}`); + } + + return this; + } + + /** + * Set the ReadPreference for the cursor. + * + * @param readPreference - The new read preference for the cursor. + */ + withReadConcern(readConcern: ReadConcernLike): this { + this.throwIfInitialized(); + const resolvedReadConcern = ReadConcern.fromOptions({ readConcern }); + if (resolvedReadConcern) { + this.cursorOptions.readConcern = resolvedReadConcern; + } + + return this; + } + + /** + * Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher) + * + * @param value - Number of milliseconds to wait before aborting the query. + */ + maxTimeMS(value: number): this { + this.throwIfInitialized(); + if (typeof value !== 'number') { + throw new MongoInvalidArgumentError('Argument for maxTimeMS must be a number'); + } + + this.cursorOptions.maxTimeMS = value; + return this; + } + + /** + * Set the batch size for the cursor. + * + * @param value - The number of documents to return per batch. See {@link https://www.mongodb.com/docs/manual/reference/command/find/|find command documentation}. + */ + batchSize(value: number): this { + this.throwIfInitialized(); + if (this.cursorOptions.tailable) { + throw new MongoTailableCursorError('Tailable cursor does not support batchSize'); + } + + if (typeof value !== 'number') { + throw new MongoInvalidArgumentError('Operation "batchSize" requires an integer'); + } + + this.cursorOptions.batchSize = value; + return this; + } + + /** + * Rewind this cursor to its uninitialized state. Any options that are present on the cursor will + * remain in effect. Iterating this cursor will cause new queries to be sent to the server, even + * if the resultant data has already been retrieved by this cursor. + */ + rewind(): void { + if (this.timeoutContext && this.timeoutContext.owner !== this) { + throw new MongoAPIError(`Cannot rewind cursor that does not own its timeout context.`); + } + if (!this.initialized) { + return; + } + + this.cursorId = null; + this.documents?.clear(); + this.timeoutContext?.clear(); + this.timeoutContext = undefined; + this.isClosed = false; + this.isKilled = false; + this.initialized = false; + this.hasEmittedClose = false; + this.trackCursor(); + + // We only want to end this session if we created it, and it hasn't ended yet + if (this.cursorSession?.explicit === false) { + if (!this.cursorSession.hasEnded) { + this.cursorSession.endSession().then(undefined, squashError); + } + + this.cursorSession = null; + } + } + + /** + * Returns a new uninitialized copy of this cursor, with options matching those that have been set on the current instance + */ + abstract clone(): AbstractCursor; + + /** @internal */ + protected abstract _initialize( + session: ClientSession | undefined + ): Promise; + + /** @internal */ + async getMore(): Promise { + if (this.cursorId == null) { + throw new MongoRuntimeError( + 'Unexpected null cursor id. A cursor creating command should have set this' + ); + } + if (this.selectedServer == null) { + throw new MongoRuntimeError( + 'Unexpected null selectedServer. A cursor creating command should have set this' + ); + } + + if (this.cursorSession == null) { + throw new MongoRuntimeError( + 'Unexpected null session. A cursor creating command should have set this' + ); + } + const getMoreOptions = { + ...this.cursorOptions, + session: this.cursorSession, + batchSize: this.cursorOptions.batchSize + }; + + const getMoreOperation = new GetMoreOperation( + this.cursorNamespace, + this.cursorId, + this.selectedServer, + getMoreOptions + ); + + return await executeOperation(this.cursorClient, getMoreOperation, this.timeoutContext); + } + + /** + * @internal + * + * This function is exposed for the unified test runner's createChangeStream + * operation. We cannot refactor to use the abstract _initialize method without + * a significant refactor. + */ + private async cursorInit(): Promise { + if (this.cursorOptions.timeoutMS != null) { + this.timeoutContext ??= new CursorTimeoutContext( + TimeoutContext.create({ + serverSelectionTimeoutMS: this.client.s.options.serverSelectionTimeoutMS, + timeoutMS: this.cursorOptions.timeoutMS + }), + this + ); + } + try { + this.cursorSession ??= this.cursorClient.startSession({ owner: this, explicit: false }); + const state = await this._initialize(this.cursorSession); + // Set omitMaxTimeMS to the value needed for subsequent getMore calls + this.cursorOptions.omitMaxTimeMS = this.cursorOptions.timeoutMS != null; + const response = state.response; + this.selectedServer = state.server; + this.cursorId = response.id; + this.cursorNamespace = response.ns ?? this.namespace; + this.documents = response; + this.initialized = true; // the cursor is now initialized, even if it is dead + } catch (error) { + // the cursor is now initialized, even if an error occurred + this.initialized = true; + await this.cleanup(undefined, error); + throw error; + } + + if (this.isDead) { + await this.cleanup(); + } + + return; + } + + /** @internal Attempt to obtain more documents */ + private async fetchBatch(): Promise { + if (this.isClosed) { + return; + } + + if (this.isDead) { + // if the cursor is dead, we clean it up + // cleanupCursor should never throw, but if it does it indicates a bug in the driver + // and we should surface the error + await this.cleanup(); + return; + } + + if (this.cursorId == null) { + await this.cursorInit(); + // If the cursor died or returned documents, return + if ((this.documents?.length ?? 0) !== 0 || this.isDead) return; + } + + // Otherwise, run a getMore + try { + const response = await this.getMore(); + this.cursorId = response.id; + this.documents = response; + } catch (error) { + try { + await this.cleanup(undefined, error); + } catch (cleanupError) { + // `cleanupCursor` should never throw, squash and throw the original error + squashError(cleanupError); + } + throw error; + } + + if (this.isDead) { + // If we successfully received a response from a cursor BUT the cursor indicates that it is exhausted, + // we intentionally clean up the cursor to release its session back into the pool before the cursor + // is iterated. This prevents a cursor that is exhausted on the server from holding + // onto a session indefinitely until the AbstractCursor is iterated. + // + // cleanupCursorAsync should never throw, but if it does it indicates a bug in the driver + // and we should surface the error + await this.cleanup(); + } + } + + /** @internal */ + private async cleanup(timeoutMS?: number, error?: Error) { + this.abortListener?.[kDispose](); + this.isClosed = true; + const timeoutContextForKillCursors = (): CursorTimeoutContext | undefined => { + if (timeoutMS != null) { + this.timeoutContext?.clear(); + return new CursorTimeoutContext( + TimeoutContext.create({ + serverSelectionTimeoutMS: this.client.s.options.serverSelectionTimeoutMS, + timeoutMS + }), + this + ); + } else { + return this.timeoutContext?.refreshed(); + } + }; + + const withEmitClose = async (fn: () => Promise) => { + try { + await fn(); + } finally { + this.emitClose(); + } + }; + + const close = async () => { + // if no session has been defined on the cursor, the cursor was never initialized + // or the cursor was re-wound and never re-iterated. In either case, we + // 1. do not need to end the session (there is no session after all) + // 2. do not need to kill the cursor server-side + const session = this.cursorSession; + if (!session) return; + + try { + if ( + !this.isKilled && + this.cursorId && + !this.cursorId.isZero() && + this.cursorNamespace && + this.selectedServer && + !session.hasEnded + ) { + this.isKilled = true; + const cursorId = this.cursorId; + this.cursorId = Long.ZERO; + + await executeOperation( + this.cursorClient, + new KillCursorsOperation(cursorId, this.cursorNamespace, this.selectedServer, { + session + }), + timeoutContextForKillCursors() + ); + } + } catch (error) { + squashError(error); + } finally { + if (session.owner === this) { + await session.endSession({ error }); + } + if (!session.inTransaction()) { + maybeClearPinnedConnection(session, { error }); + } + } + }; + + await withEmitClose(close); + } + + /** @internal */ + private hasEmittedClose = false; + /** @internal */ + private emitClose() { + try { + if (!this.hasEmittedClose && ((this.documents?.length ?? 0) === 0 || this.isClosed)) { + // @ts-expect-error: CursorEvents is generic so Parameters may not be assignable to `[]`. Not sure how to require extenders do not add parameters. + this.emit('close'); + } + } finally { + this.hasEmittedClose = true; + } + } + + /** @internal */ + private async transformDocument(document: NonNullable): Promise> { + if (this.transform == null) return document; + + try { + const transformedDocument = this.transform(document); + // eslint-disable-next-line no-restricted-syntax + if (transformedDocument === null) { + const TRANSFORM_TO_NULL_ERROR = + 'Cursor returned a `null` document, but the cursor is not exhausted. Mapping documents to `null` is not supported in the cursor transform.'; + throw new MongoAPIError(TRANSFORM_TO_NULL_ERROR); + } + return transformedDocument; + } catch (transformError) { + try { + await this.close(); + } catch (closeError) { + squashError(closeError); + } + throw transformError; + } + } + + /** @internal */ + protected throwIfInitialized() { + if (this.initialized) throw new MongoCursorInUseError(); + } +} + +class ReadableCursorStream extends Readable { + private _cursor: AbstractCursor; + private _readInProgress = false; + + constructor(cursor: AbstractCursor) { + super({ + objectMode: true, + autoDestroy: false, + highWaterMark: 1 + }); + this._cursor = cursor; + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + override _read(size: number): void { + if (!this._readInProgress) { + this._readInProgress = true; + this._readNext(); + } + } + + override _destroy(error: Error | null, callback: (error?: Error | null) => void): void { + this._cursor.close().then( + () => callback(error), + closeError => callback(closeError) + ); + } + + private _readNext() { + if (this._cursor.id === Long.ZERO) { + this.push(null); + return; + } + + this._cursor + .next() + .then( + // result from next() + result => { + if (result == null) { + this.push(null); + } else if (this.destroyed) { + this._cursor.close().then(undefined, squashError); + } else { + if (this.push(result)) { + return this._readNext(); + } + + this._readInProgress = false; + } + }, + // error from next() + err => { + // NOTE: This is questionable, but we have a test backing the behavior. It seems the + // desired behavior is that a stream ends cleanly when a user explicitly closes + // a client during iteration. Alternatively, we could do the "right" thing and + // propagate the error message by removing this special case. + if (err.message.match(/server is closed/)) { + this._cursor.close().then(undefined, squashError); + return this.push(null); + } + + // NOTE: This is also perhaps questionable. The rationale here is that these errors tend + // to be "operation was interrupted", where a cursor has been closed but there is an + // active getMore in-flight. This used to check if the cursor was killed but once + // that changed to happen in cleanup legitimate errors would not destroy the + // stream. There are change streams test specifically test these cases. + if (err.message.match(/operation was interrupted/)) { + return this.push(null); + } + + // NOTE: The two above checks on the message of the error will cause a null to be pushed + // to the stream, thus closing the stream before the destroy call happens. This means + // that either of those error messages on a change stream will not get a proper + // 'error' event to be emitted (the error passed to destroy). Change stream resumability + // relies on that error event to be emitted to create its new cursor and thus was not + // working on 4.4 servers because the error emitted on failover was "interrupted at + // shutdown" while on 5.0+ it is "The server is in quiesce mode and will shut down". + // See NODE-4475. + return this.destroy(err); + } + ) + // if either of the above handlers throw + .catch(error => { + this._readInProgress = false; + this.destroy(error); + }); + } +} + +/** + * @internal + * The cursor timeout context is a wrapper around a timeout context + * that keeps track of the "owner" of the cursor. For timeout contexts + * instantiated inside a cursor, the owner will be the cursor. + * + * All timeout behavior is exactly the same as the wrapped timeout context's. + */ +export class CursorTimeoutContext extends TimeoutContext { + timeoutContext: TimeoutContext; + owner: symbol | AbstractCursor; + + constructor(timeoutContext: TimeoutContext, owner: symbol | AbstractCursor) { + super(); + this.timeoutContext = timeoutContext; + this.owner = owner; + } + override get serverSelectionTimeout(): Timeout | null { + return this.timeoutContext.serverSelectionTimeout; + } + override get connectionCheckoutTimeout(): Timeout | null { + return this.timeoutContext.connectionCheckoutTimeout; + } + override get clearServerSelectionTimeout(): boolean { + return this.timeoutContext.clearServerSelectionTimeout; + } + override get timeoutForSocketWrite(): Timeout | null { + return this.timeoutContext.timeoutForSocketWrite; + } + override get timeoutForSocketRead(): Timeout | null { + return this.timeoutContext.timeoutForSocketRead; + } + override csotEnabled(): this is CSOTTimeoutContext { + return this.timeoutContext.csotEnabled(); + } + override refresh(): void { + if (typeof this.owner !== 'symbol') return this.timeoutContext.refresh(); + } + override clear(): void { + if (typeof this.owner !== 'symbol') return this.timeoutContext.clear(); + } + override get maxTimeMS(): number | null { + return this.timeoutContext.maxTimeMS; + } + get timeoutMS(): number | null { + return this.timeoutContext.csotEnabled() ? this.timeoutContext.timeoutMS : null; + } + override refreshed(): CursorTimeoutContext { + return new CursorTimeoutContext(this.timeoutContext.refreshed(), this.owner); + } + override addMaxTimeMSToCommand(command: Document, options: { omitMaxTimeMS?: boolean }): void { + this.timeoutContext.addMaxTimeMSToCommand(command, options); + } + override getSocketTimeoutMS(): number | undefined { + return this.timeoutContext.getSocketTimeoutMS(); + } +} diff --git a/gateway/node_modules/mongodb/src/cursor/aggregation_cursor.ts b/gateway/node_modules/mongodb/src/cursor/aggregation_cursor.ts new file mode 100644 index 0000000000000000000000000000000000000000..07201e1c3041869934dcb8a9078d448aab1c62fc --- /dev/null +++ b/gateway/node_modules/mongodb/src/cursor/aggregation_cursor.ts @@ -0,0 +1,245 @@ +import type { Document } from '../bson'; +import { MongoAPIError } from '../error'; +import { + Explain, + type ExplainCommandOptions, + type ExplainVerbosityLike, + validateExplainTimeoutOptions +} from '../explain'; +import type { MongoClient } from '../mongo_client'; +import { type Abortable } from '../mongo_types'; +import { AggregateOperation, type AggregateOptions } from '../operations/aggregate'; +import { executeOperation } from '../operations/execute_operation'; +import type { ClientSession } from '../sessions'; +import type { Sort } from '../sort'; +import { mergeOptions, type MongoDBNamespace } from '../utils'; +import { + type AbstractCursorOptions, + CursorTimeoutMode, + type InitialCursorResponse +} from './abstract_cursor'; +import { ExplainableCursor } from './explainable_cursor'; + +/** @public */ +export interface AggregationCursorOptions extends AbstractCursorOptions, AggregateOptions {} + +/** + * The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB + * allowing for iteration over the results returned from the underlying query. It supports + * one by one document iteration, conversion to an array or can be iterated as a Node 4.X + * or higher stream + * @public + */ +export class AggregationCursor extends ExplainableCursor { + public readonly pipeline: Document[]; + /** @internal */ + private aggregateOptions: AggregateOptions & Abortable; + + /** @internal */ + constructor( + client: MongoClient, + namespace: MongoDBNamespace, + pipeline: Document[] = [], + options: AggregateOptions & Abortable = {} + ) { + super(client, namespace, options); + + this.pipeline = pipeline; + this.aggregateOptions = options; + + const lastStage: Document | undefined = this.pipeline[this.pipeline.length - 1]; + + if ( + this.cursorOptions.timeoutMS != null && + this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION && + (lastStage?.$merge != null || lastStage?.$out != null) + ) + throw new MongoAPIError('Cannot use $out or $merge stage with ITERATION timeoutMode'); + } + + clone(): AggregationCursor { + const clonedOptions = mergeOptions({}, this.aggregateOptions); + delete clonedOptions.session; + return new AggregationCursor(this.client, this.namespace, this.pipeline, { + ...clonedOptions + }); + } + + override map(transform: (doc: TSchema) => T): AggregationCursor { + return super.map(transform) as AggregationCursor; + } + + /** @internal */ + async _initialize(session: ClientSession): Promise { + const options = { + ...this.aggregateOptions, + ...this.cursorOptions, + session, + signal: this.signal + }; + if (options.explain) { + try { + validateExplainTimeoutOptions(options, Explain.fromOptions(options)); + } catch { + throw new MongoAPIError( + 'timeoutMS cannot be used with explain when explain is specified in aggregateOptions' + ); + } + } + + const aggregateOperation = new AggregateOperation(this.namespace, this.pipeline, options); + + const response = await executeOperation(this.client, aggregateOperation, this.timeoutContext); + + return { server: aggregateOperation.server, session, response }; + } + + /** Execute the explain for the cursor */ + async explain(): Promise; + async explain(verbosity: ExplainVerbosityLike | ExplainCommandOptions): Promise; + async explain(options: { timeoutMS?: number }): Promise; + async explain( + verbosity: ExplainVerbosityLike | ExplainCommandOptions, + options: { timeoutMS?: number } + ): Promise; + async explain( + verbosity?: ExplainVerbosityLike | ExplainCommandOptions | { timeoutMS?: number }, + options?: { timeoutMS?: number } + ): Promise { + const { explain, timeout } = this.resolveExplainTimeoutOptions(verbosity, options); + return ( + await executeOperation( + this.client, + new AggregateOperation(this.namespace, this.pipeline, { + ...this.aggregateOptions, // NOTE: order matters here, we may need to refine this + ...this.cursorOptions, + ...timeout, + explain: explain ?? true + }) + ) + ).shift(this.deserializationOptions); + } + + /** Add a stage to the aggregation pipeline + * @example + * ``` + * const documents = await users.aggregate().addStage({ $match: { name: /Mike/ } }).toArray(); + * ``` + * @example + * ``` + * const documents = await users.aggregate() + * .addStage<{ name: string }>({ $project: { name: true } }) + * .toArray(); // type of documents is { name: string }[] + * ``` + */ + addStage(stage: Document): this; + addStage(stage: Document): AggregationCursor; + addStage(stage: Document): AggregationCursor { + this.throwIfInitialized(); + if ( + this.cursorOptions.timeoutMS != null && + this.cursorOptions.timeoutMode === CursorTimeoutMode.ITERATION && + (stage.$out != null || stage.$merge != null) + ) { + throw new MongoAPIError('Cannot use $out or $merge stage with ITERATION timeoutMode'); + } + this.pipeline.push(stage); + return this as unknown as AggregationCursor; + } + + /** Add a group stage to the aggregation pipeline */ + group($group: Document): AggregationCursor; + group($group: Document): this { + return this.addStage({ $group }); + } + + /** Add a limit stage to the aggregation pipeline */ + limit($limit: number): this { + return this.addStage({ $limit }); + } + + /** Add a match stage to the aggregation pipeline */ + match($match: Document): this { + return this.addStage({ $match }); + } + + /** Add an out stage to the aggregation pipeline */ + out($out: { db: string; coll: string } | string): this { + return this.addStage({ $out }); + } + + /** + * Add a project stage to the aggregation pipeline + * + * @remarks + * In order to strictly type this function you must provide an interface + * that represents the effect of your projection on the result documents. + * + * By default chaining a projection to your cursor changes the returned type to the generic {@link Document} type. + * You should specify a parameterized type to have assertions on your final results. + * + * @example + * ```typescript + * // Best way + * const docs: AggregationCursor<{ a: number }> = cursor.project<{ a: number }>({ _id: 0, a: true }); + * // Flexible way + * const docs: AggregationCursor = cursor.project({ _id: 0, a: true }); + * ``` + * + * @remarks + * In order to strictly type this function you must provide an interface + * that represents the effect of your projection on the result documents. + * + * **Note for Typescript Users:** adding a transform changes the return type of the iteration of this cursor, + * it **does not** return a new instance of a cursor. This means when calling project, + * you should always assign the result to a new variable in order to get a correctly typed cursor variable. + * Take note of the following example: + * + * @example + * ```typescript + * const cursor: AggregationCursor<{ a: number; b: string }> = coll.aggregate([]); + * const projectCursor = cursor.project<{ a: number }>({ _id: 0, a: true }); + * const aPropOnlyArray: {a: number}[] = await projectCursor.toArray(); + * + * // or always use chaining and save the final cursor + * + * const cursor = coll.aggregate().project<{ a: string }>({ + * _id: 0, + * a: { $convert: { input: '$a', to: 'string' } + * }}); + * ``` + */ + project($project: Document): AggregationCursor { + return this.addStage({ $project }); + } + + /** Add a lookup stage to the aggregation pipeline */ + lookup($lookup: Document): this { + return this.addStage({ $lookup }); + } + + /** Add a redact stage to the aggregation pipeline */ + redact($redact: Document): this { + return this.addStage({ $redact }); + } + + /** Add a skip stage to the aggregation pipeline */ + skip($skip: number): this { + return this.addStage({ $skip }); + } + + /** Add a sort stage to the aggregation pipeline */ + sort($sort: Sort): this { + return this.addStage({ $sort }); + } + + /** Add a unwind stage to the aggregation pipeline */ + unwind($unwind: Document | string): this { + return this.addStage({ $unwind }); + } + + /** Add a geoNear stage to the aggregation pipeline */ + geoNear($geoNear: Document): this { + return this.addStage({ $geoNear }); + } +} diff --git a/gateway/node_modules/mongodb/src/cursor/change_stream_cursor.ts b/gateway/node_modules/mongodb/src/cursor/change_stream_cursor.ts new file mode 100644 index 0000000000000000000000000000000000000000..7b4db301069d86b59c8170f4dbf7a654b58ebf06 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cursor/change_stream_cursor.ts @@ -0,0 +1,171 @@ +import type { Document } from '../bson'; +import { + ChangeStream, + type ChangeStreamDocument, + type ChangeStreamEvents, + type OperationTime, + type ResumeToken +} from '../change_stream'; +import { type CursorResponse } from '../cmap/wire_protocol/responses'; +import { INIT, RESPONSE } from '../constants'; +import type { MongoClient } from '../mongo_client'; +import { AggregateOperation } from '../operations/aggregate'; +import type { CollationOptions } from '../operations/command'; +import { executeOperation } from '../operations/execute_operation'; +import type { ClientSession } from '../sessions'; +import { maxWireVersion, type MongoDBNamespace } from '../utils'; +import { + AbstractCursor, + type AbstractCursorOptions, + type InitialCursorResponse +} from './abstract_cursor'; + +/** @internal */ +export interface ChangeStreamCursorOptions extends AbstractCursorOptions { + startAtOperationTime?: OperationTime; + resumeAfter?: ResumeToken; + startAfter?: ResumeToken; + maxAwaitTimeMS?: number; + collation?: CollationOptions; + fullDocument?: string; +} + +/** @internal */ +export class ChangeStreamCursor< + TSchema extends Document = Document, + TChange extends Document = ChangeStreamDocument +> extends AbstractCursor { + private _resumeToken: ResumeToken; + private startAtOperationTime: OperationTime | null; + private hasReceived?: boolean; + private readonly changeStreamCursorOptions: ChangeStreamCursorOptions; + private postBatchResumeToken?: ResumeToken; + private readonly pipeline: Document[]; + + /** + * @internal + * + * used to determine change stream resumability + */ + maxWireVersion: number | undefined; + + constructor( + client: MongoClient, + namespace: MongoDBNamespace, + pipeline: Document[] = [], + options: ChangeStreamCursorOptions = {} + ) { + super(client, namespace, { ...options, tailable: true, awaitData: true }); + + this.pipeline = pipeline; + this.changeStreamCursorOptions = options; + this._resumeToken = null; + this.startAtOperationTime = options.startAtOperationTime ?? null; + + if (options.startAfter) { + this.resumeToken = options.startAfter; + } else if (options.resumeAfter) { + this.resumeToken = options.resumeAfter; + } + } + + set resumeToken(token: ResumeToken) { + this._resumeToken = token; + this.emit(ChangeStream.RESUME_TOKEN_CHANGED, token); + } + + get resumeToken(): ResumeToken { + return this._resumeToken; + } + + get resumeOptions(): ChangeStreamCursorOptions { + const options: ChangeStreamCursorOptions = { + ...this.changeStreamCursorOptions + }; + + for (const key of ['resumeAfter', 'startAfter', 'startAtOperationTime'] as const) { + delete options[key]; + } + + if (this.resumeToken != null) { + if (this.changeStreamCursorOptions.startAfter && !this.hasReceived) { + options.startAfter = this.resumeToken; + } else { + options.resumeAfter = this.resumeToken; + } + } else if (this.startAtOperationTime != null) { + options.startAtOperationTime = this.startAtOperationTime; + } + + return options; + } + + cacheResumeToken(resumeToken: ResumeToken): void { + if (this.bufferedCount() === 0 && this.postBatchResumeToken) { + this.resumeToken = this.postBatchResumeToken; + } else { + this.resumeToken = resumeToken; + } + this.hasReceived = true; + } + + _processBatch(response: CursorResponse): void { + const { postBatchResumeToken } = response; + if (postBatchResumeToken) { + this.postBatchResumeToken = postBatchResumeToken; + + if (response.batchSize === 0) { + this.resumeToken = postBatchResumeToken; + } + } + } + + clone(): AbstractCursor { + return new ChangeStreamCursor(this.client, this.namespace, this.pipeline, { + ...this.cursorOptions + }); + } + + async _initialize(session: ClientSession): Promise { + const aggregateOperation = new AggregateOperation(this.namespace, this.pipeline, { + ...this.cursorOptions, + ...this.changeStreamCursorOptions, + session + }); + + const response = await executeOperation( + session.client, + aggregateOperation, + this.timeoutContext + ); + + const server = aggregateOperation.server; + this.maxWireVersion = maxWireVersion(server); + + if ( + this.startAtOperationTime == null && + this.changeStreamCursorOptions.resumeAfter == null && + this.changeStreamCursorOptions.startAfter == null + ) { + this.startAtOperationTime = response.operationTime; + } + + this._processBatch(response); + + this.emit(INIT, response); + this.emit(RESPONSE); + + return { server, session, response }; + } + + override async getMore(): Promise { + const response = await super.getMore(); + + this.maxWireVersion = maxWireVersion(this.server); + this._processBatch(response); + + this.emit(ChangeStream.MORE, response); + this.emit(ChangeStream.RESPONSE); + return response; + } +} diff --git a/gateway/node_modules/mongodb/src/cursor/client_bulk_write_cursor.ts b/gateway/node_modules/mongodb/src/cursor/client_bulk_write_cursor.ts new file mode 100644 index 0000000000000000000000000000000000000000..d9da82d367b64299088234a1f298d5bdb3eca1c2 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cursor/client_bulk_write_cursor.ts @@ -0,0 +1,83 @@ +import { type Document } from '../bson'; +import { type ClientBulkWriteCursorResponse } from '../cmap/wire_protocol/responses'; +import type { MongoClient } from '../mongo_client'; +import { ClientBulkWriteOperation } from '../operations/client_bulk_write/client_bulk_write'; +import { type ClientBulkWriteCommandBuilder } from '../operations/client_bulk_write/command_builder'; +import { type ClientBulkWriteOptions } from '../operations/client_bulk_write/common'; +import { executeOperation } from '../operations/execute_operation'; +import type { ClientSession } from '../sessions'; +import { mergeOptions, MongoDBNamespace } from '../utils'; +import { + AbstractCursor, + type AbstractCursorOptions, + type InitialCursorResponse +} from './abstract_cursor'; + +/** @public */ +export interface ClientBulkWriteCursorOptions + extends Omit, + ClientBulkWriteOptions {} + +/** + * This is the cursor that handles client bulk write operations. Note this is never + * exposed directly to the user and is always immediately exhausted. + * @internal + */ +export class ClientBulkWriteCursor extends AbstractCursor { + commandBuilder: ClientBulkWriteCommandBuilder; + /** @internal */ + private cursorResponse?: ClientBulkWriteCursorResponse; + /** @internal */ + private clientBulkWriteOptions: ClientBulkWriteOptions; + + /** @internal */ + constructor( + client: MongoClient, + commandBuilder: ClientBulkWriteCommandBuilder, + options: ClientBulkWriteCursorOptions = {} + ) { + super(client, new MongoDBNamespace('admin', '$cmd'), options); + + this.commandBuilder = commandBuilder; + this.clientBulkWriteOptions = options; + } + + /** + * We need a way to get the top level cursor response fields for + * generating the bulk write result, so we expose this here. + */ + get response(): ClientBulkWriteCursorResponse | null { + if (this.cursorResponse) return this.cursorResponse; + return null; + } + + get operations(): Document[] { + return this.commandBuilder.lastOperations; + } + + clone(): ClientBulkWriteCursor { + const clonedOptions = mergeOptions({}, this.clientBulkWriteOptions); + delete clonedOptions.session; + return new ClientBulkWriteCursor(this.client, this.commandBuilder, { + ...clonedOptions + }); + } + + /** @internal */ + async _initialize(session: ClientSession): Promise { + const clientBulkWriteOperation = new ClientBulkWriteOperation(this.commandBuilder, { + ...this.clientBulkWriteOptions, + ...this.cursorOptions, + session + }); + + const response = await executeOperation( + this.client, + clientBulkWriteOperation, + this.timeoutContext + ); + this.cursorResponse = response; + + return { server: clientBulkWriteOperation.server, session, response }; + } +} diff --git a/gateway/node_modules/mongodb/src/cursor/explainable_cursor.ts b/gateway/node_modules/mongodb/src/cursor/explainable_cursor.ts new file mode 100644 index 0000000000000000000000000000000000000000..6becb0bb373b8a3d57f68d98e51aba4fdc60c5ec --- /dev/null +++ b/gateway/node_modules/mongodb/src/cursor/explainable_cursor.ts @@ -0,0 +1,51 @@ +import { type Document } from '../bson'; +import { type ExplainCommandOptions, type ExplainVerbosityLike } from '../explain'; +import { AbstractCursor } from './abstract_cursor'; + +/** + * @public + * + * A base class for any cursors that have `explain()` methods. + */ +export abstract class ExplainableCursor extends AbstractCursor { + /** Execute the explain for the cursor */ + abstract explain(): Promise; + abstract explain(verbosity: ExplainVerbosityLike | ExplainCommandOptions): Promise; + abstract explain(options: { timeoutMS?: number }): Promise; + abstract explain( + verbosity: ExplainVerbosityLike | ExplainCommandOptions, + options: { timeoutMS?: number } + ): Promise; + abstract explain( + verbosity?: ExplainVerbosityLike | ExplainCommandOptions | { timeoutMS?: number }, + options?: { timeoutMS?: number } + ): Promise; + + protected resolveExplainTimeoutOptions( + verbosity?: ExplainVerbosityLike | ExplainCommandOptions | { timeoutMS?: number }, + options?: { timeoutMS?: number } + ): { timeout?: { timeoutMS?: number }; explain?: ExplainVerbosityLike | ExplainCommandOptions } { + let explain: ExplainVerbosityLike | ExplainCommandOptions | undefined; + let timeout: { timeoutMS?: number } | undefined; + + if (verbosity == null && options == null) { + explain = undefined; + timeout = undefined; + } else if (verbosity != null && options == null) { + explain = + typeof verbosity !== 'object' + ? verbosity + : 'verbosity' in verbosity + ? verbosity + : undefined; + + timeout = typeof verbosity === 'object' && 'timeoutMS' in verbosity ? verbosity : undefined; + } else { + // @ts-expect-error TS isn't smart enough to determine that if both options are provided, the first is explain options + explain = verbosity; + timeout = options; + } + + return { timeout, explain }; + } +} diff --git a/gateway/node_modules/mongodb/src/cursor/find_cursor.ts b/gateway/node_modules/mongodb/src/cursor/find_cursor.ts new file mode 100644 index 0000000000000000000000000000000000000000..c66a2a3e45f648e495c42ff86ca260730a843c8c --- /dev/null +++ b/gateway/node_modules/mongodb/src/cursor/find_cursor.ts @@ -0,0 +1,497 @@ +import { type Document } from '../bson'; +import { CursorResponse } from '../cmap/wire_protocol/responses'; +import { MongoAPIError, MongoInvalidArgumentError, MongoTailableCursorError } from '../error'; +import { + Explain, + type ExplainCommandOptions, + type ExplainVerbosityLike, + validateExplainTimeoutOptions +} from '../explain'; +import type { MongoClient } from '../mongo_client'; +import { type Abortable } from '../mongo_types'; +import type { CollationOptions } from '../operations/command'; +import { CountOperation, type CountOptions } from '../operations/count'; +import { executeOperation } from '../operations/execute_operation'; +import { FindOperation, type FindOptions } from '../operations/find'; +import type { Hint } from '../operations/operation'; +import type { ClientSession } from '../sessions'; +import { formatSort, type Sort, type SortDirection } from '../sort'; +import { emitWarningOnce, mergeOptions, type MongoDBNamespace, noop, squashError } from '../utils'; +import { type InitialCursorResponse } from './abstract_cursor'; +import { ExplainableCursor } from './explainable_cursor'; + +/** @public Flags allowed for cursor */ +export const FLAGS = [ + 'tailable', + 'oplogReplay', + 'noCursorTimeout', + 'awaitData', + 'exhaust', + 'partial' +] as const; + +/** @public */ +export class FindCursor extends ExplainableCursor { + /** @internal */ + private cursorFilter: Document; + /** @internal */ + private numReturned = 0; + /** @internal */ + private readonly findOptions: FindOptions & Abortable; + + /** @internal */ + constructor( + client: MongoClient, + namespace: MongoDBNamespace, + filter: Document = {}, + options: FindOptions & Abortable = {} + ) { + super(client, namespace, options); + + this.cursorFilter = filter; + this.findOptions = options; + + if (options.sort != null) { + this.findOptions.sort = formatSort(options.sort); + } + } + + clone(): FindCursor { + const clonedOptions = mergeOptions({}, this.findOptions); + delete clonedOptions.session; + return new FindCursor(this.client, this.namespace, this.cursorFilter, { + ...clonedOptions + }); + } + + override map(transform: (doc: TSchema) => T): FindCursor { + return super.map(transform) as FindCursor; + } + + /** @internal */ + async _initialize(session: ClientSession): Promise { + const options = { + ...this.findOptions, // NOTE: order matters here, we may need to refine this + ...this.cursorOptions, + session, + signal: this.signal + }; + + if (options.explain) { + try { + validateExplainTimeoutOptions(options, Explain.fromOptions(options)); + } catch { + throw new MongoAPIError( + 'timeoutMS cannot be used with explain when explain is specified in findOptions' + ); + } + } + + const findOperation = new FindOperation(this.namespace, this.cursorFilter, options); + + const response = await executeOperation(this.client, findOperation, this.timeoutContext); + + // the response is not a cursor when `explain` is enabled + this.numReturned = response.batchSize; + + return { server: findOperation.server, session, response }; + } + + /** @internal */ + override async getMore(): Promise { + const numReturned = this.numReturned; + const limit = this.findOptions.limit ?? Infinity; + const remaining = limit - numReturned; + + if (numReturned === limit && !this.id?.isZero()) { + // this is an optimization for the special case of a limit for a find command to avoid an + // extra getMore when the limit has been reached and the limit is a multiple of the batchSize. + // This is a consequence of the new query engine in 5.0 having no knowledge of the limit as it + // produces results for the find command. Once a batch is filled up, it is returned and only + // on the subsequent getMore will the query framework consider the limit, determine the cursor + // is exhausted and return a cursorId of zero. + // instead, if we determine there are no more documents to request from the server, we preemptively + // close the cursor + try { + await this.close(); + } catch (error) { + squashError(error); + } + return CursorResponse.emptyGetMore; + } + + // TODO(DRIVERS-1448): Remove logic to enforce `limit` in the driver + let cleanup: () => void = noop; + const { batchSize } = this.cursorOptions; + if (batchSize != null && batchSize > remaining) { + this.cursorOptions.batchSize = remaining; + + // After executing the final getMore, re-assign the batchSize back to its original value so that + // if the cursor is rewound and executed, the batchSize is still correct. + cleanup = () => { + this.cursorOptions.batchSize = batchSize; + }; + } + + try { + const response = await super.getMore(); + + this.numReturned = this.numReturned + response.batchSize; + + return response; + } finally { + cleanup?.(); + } + } + + /** + * Get the count of documents for this cursor + * @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead + */ + async count(options?: CountOptions): Promise { + emitWarningOnce( + 'cursor.count is deprecated and will be removed in the next major version, please use `collection.estimatedDocumentCount` or `collection.countDocuments` instead ' + ); + if (typeof options === 'boolean') { + throw new MongoInvalidArgumentError('Invalid first parameter to count'); + } + return await executeOperation( + this.client, + new CountOperation(this.namespace, this.cursorFilter, { + ...this.findOptions, // NOTE: order matters here, we may need to refine this + ...this.cursorOptions, + ...options + }) + ); + } + + /** Execute the explain for the cursor */ + async explain(): Promise; + async explain(verbosity: ExplainVerbosityLike | ExplainCommandOptions): Promise; + async explain(options: { timeoutMS?: number }): Promise; + async explain( + verbosity: ExplainVerbosityLike | ExplainCommandOptions, + options: { timeoutMS?: number } + ): Promise; + async explain( + verbosity?: ExplainVerbosityLike | ExplainCommandOptions | { timeoutMS?: number }, + options?: { timeoutMS?: number } + ): Promise { + const { explain, timeout } = this.resolveExplainTimeoutOptions(verbosity, options); + + return ( + await executeOperation( + this.client, + new FindOperation(this.namespace, this.cursorFilter, { + ...this.findOptions, // NOTE: order matters here, we may need to refine this + ...this.cursorOptions, + ...timeout, + explain: explain ?? true + }) + ) + ).shift(this.deserializationOptions); + } + + /** Set the cursor query */ + filter(filter: Document): this { + this.throwIfInitialized(); + this.cursorFilter = filter; + return this; + } + + /** + * Set the cursor hint + * + * @param hint - If specified, then the query system will only consider plans using the hinted index. + */ + hint(hint: Hint): this { + this.throwIfInitialized(); + this.findOptions.hint = hint; + return this; + } + + /** + * Set the cursor min + * + * @param min - Specify a $min value to specify the inclusive lower bound for a specific index in order to constrain the results of find(). The $min specifies the lower bound for all keys of a specific index in order. + */ + min(min: Document): this { + this.throwIfInitialized(); + this.findOptions.min = min; + return this; + } + + /** + * Set the cursor max + * + * @param max - Specify a $max value to specify the exclusive upper bound for a specific index in order to constrain the results of find(). The $max specifies the upper bound for all keys of a specific index in order. + */ + max(max: Document): this { + this.throwIfInitialized(); + this.findOptions.max = max; + return this; + } + + /** + * Set the cursor returnKey. + * If set to true, modifies the cursor to only return the index field or fields for the results of the query, rather than documents. + * If set to true and the query does not use an index to perform the read operation, the returned documents will not contain any fields. + * + * @param value - the returnKey value. + */ + returnKey(value: boolean): this { + this.throwIfInitialized(); + this.findOptions.returnKey = value; + return this; + } + + /** + * Modifies the output of a query by adding a field $recordId to matching documents. $recordId is the internal key which uniquely identifies a document in a collection. + * + * @param value - The $showDiskLoc option has now been deprecated and replaced with the showRecordId field. $showDiskLoc will still be accepted for OP_QUERY stye find. + */ + showRecordId(value: boolean): this { + this.throwIfInitialized(); + this.findOptions.showRecordId = value; + return this; + } + + /** + * Add a query modifier to the cursor query + * + * @param name - The query modifier (must start with $, such as $orderby etc) + * @param value - The modifier value. + */ + addQueryModifier(name: string, value: string | boolean | number | Document): this { + this.throwIfInitialized(); + if (name[0] !== '$') { + throw new MongoInvalidArgumentError(`${name} is not a valid query modifier`); + } + + // Strip of the $ + const field = name.substr(1); + + // NOTE: consider some TS magic for this + switch (field) { + case 'comment': + this.findOptions.comment = value; + break; + + case 'explain': + this.findOptions.explain = value as boolean; + break; + + case 'hint': + this.findOptions.hint = value as string | Document; + break; + + case 'max': + this.findOptions.max = value as Document; + break; + + case 'maxTimeMS': + this.findOptions.maxTimeMS = value as number; + break; + + case 'min': + this.findOptions.min = value as Document; + break; + + case 'orderby': + this.findOptions.sort = formatSort(value as string | Document); + break; + + case 'query': + this.cursorFilter = value as Document; + break; + + case 'returnKey': + this.findOptions.returnKey = value as boolean; + break; + + case 'showDiskLoc': + this.findOptions.showRecordId = value as boolean; + break; + + default: + throw new MongoInvalidArgumentError(`Invalid query modifier: ${name}`); + } + + return this; + } + + /** + * Add a comment to the cursor query allowing for tracking the comment in the log. + * + * @param value - The comment attached to this query. + */ + comment(value: string): this { + this.throwIfInitialized(); + this.findOptions.comment = value; + return this; + } + + /** + * Set a maxAwaitTimeMS on a tailing cursor query to allow to customize the timeout value for the option awaitData (Only supported on MongoDB 3.2 or higher, ignored otherwise) + * + * @param value - Number of milliseconds to wait before aborting the tailed query. + */ + maxAwaitTimeMS(value: number): this { + this.throwIfInitialized(); + if (typeof value !== 'number') { + throw new MongoInvalidArgumentError('Argument for maxAwaitTimeMS must be a number'); + } + + this.findOptions.maxAwaitTimeMS = value; + return this; + } + + /** + * Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher) + * + * @param value - Number of milliseconds to wait before aborting the query. + */ + override maxTimeMS(value: number): this { + this.throwIfInitialized(); + if (typeof value !== 'number') { + throw new MongoInvalidArgumentError('Argument for maxTimeMS must be a number'); + } + + this.findOptions.maxTimeMS = value; + return this; + } + + /** + * Add a project stage to the aggregation pipeline + * + * @remarks + * In order to strictly type this function you must provide an interface + * that represents the effect of your projection on the result documents. + * + * By default chaining a projection to your cursor changes the returned type to the generic + * {@link Document} type. + * You should specify a parameterized type to have assertions on your final results. + * + * @example + * ```typescript + * // Best way + * const docs: FindCursor<{ a: number }> = cursor.project<{ a: number }>({ _id: 0, a: true }); + * // Flexible way + * const docs: FindCursor = cursor.project({ _id: 0, a: true }); + * ``` + * + * @remarks + * + * **Note for Typescript Users:** adding a transform changes the return type of the iteration of this cursor, + * it **does not** return a new instance of a cursor. This means when calling project, + * you should always assign the result to a new variable in order to get a correctly typed cursor variable. + * Take note of the following example: + * + * @example + * ```typescript + * const cursor: FindCursor<{ a: number; b: string }> = coll.find(); + * const projectCursor = cursor.project<{ a: number }>({ _id: 0, a: true }); + * const aPropOnlyArray: {a: number}[] = await projectCursor.toArray(); + * + * // or always use chaining and save the final cursor + * + * const cursor = coll.find().project<{ a: string }>({ + * _id: 0, + * a: { $convert: { input: '$a', to: 'string' } + * }}); + * ``` + */ + project(value: Document): FindCursor { + this.throwIfInitialized(); + this.findOptions.projection = value; + return this as unknown as FindCursor; + } + + /** + * Sets the sort order of the cursor query. + * + * @param sort - The key or keys set for the sort. + * @param direction - The direction of the sorting (1 or -1). + */ + sort(sort: Sort | string, direction?: SortDirection): this { + this.throwIfInitialized(); + if (this.findOptions.tailable) { + throw new MongoTailableCursorError('Tailable cursor does not support sorting'); + } + + this.findOptions.sort = formatSort(sort, direction); + return this; + } + + /** + * Allows disk use for blocking sort operations exceeding 100MB memory. (MongoDB 3.2 or higher) + * + * @remarks + * {@link https://www.mongodb.com/docs/manual/reference/command/find/#find-cmd-allowdiskuse | find command allowDiskUse documentation} + */ + allowDiskUse(allow = true): this { + this.throwIfInitialized(); + + if (!this.findOptions.sort) { + throw new MongoInvalidArgumentError('Option "allowDiskUse" requires a sort specification'); + } + + // As of 6.0 the default is true. This allows users to get back to the old behavior. + if (!allow) { + this.findOptions.allowDiskUse = false; + return this; + } + + this.findOptions.allowDiskUse = true; + return this; + } + + /** + * Set the collation options for the cursor. + * + * @param value - The cursor collation options (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields). + */ + collation(value: CollationOptions): this { + this.throwIfInitialized(); + this.findOptions.collation = value; + return this; + } + + /** + * Set the limit for the cursor. + * + * @param value - The limit for the cursor query. + */ + limit(value: number): this { + this.throwIfInitialized(); + if (this.findOptions.tailable) { + throw new MongoTailableCursorError('Tailable cursor does not support limit'); + } + + if (typeof value !== 'number') { + throw new MongoInvalidArgumentError('Operation "limit" requires an integer'); + } + + this.findOptions.limit = value; + return this; + } + + /** + * Set the skip for the cursor. + * + * @param value - The skip for the cursor query. + */ + skip(value: number): this { + this.throwIfInitialized(); + if (this.findOptions.tailable) { + throw new MongoTailableCursorError('Tailable cursor does not support skip'); + } + + if (typeof value !== 'number') { + throw new MongoInvalidArgumentError('Operation "skip" requires an integer'); + } + + this.findOptions.skip = value; + return this; + } +} diff --git a/gateway/node_modules/mongodb/src/cursor/list_collections_cursor.ts b/gateway/node_modules/mongodb/src/cursor/list_collections_cursor.ts new file mode 100644 index 0000000000000000000000000000000000000000..bbe0a2536b954e5ec688742a855888f194f18152 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cursor/list_collections_cursor.ts @@ -0,0 +1,50 @@ +import type { Document } from '../bson'; +import type { Db } from '../db'; +import { type Abortable } from '../mongo_types'; +import { executeOperation } from '../operations/execute_operation'; +import { + type CollectionInfo, + ListCollectionsOperation, + type ListCollectionsOptions +} from '../operations/list_collections'; +import type { ClientSession } from '../sessions'; +import { AbstractCursor, type InitialCursorResponse } from './abstract_cursor'; + +/** @public */ +export class ListCollectionsCursor< + T extends Pick | CollectionInfo = + | Pick + | CollectionInfo +> extends AbstractCursor { + parent: Db; + filter: Document; + options?: ListCollectionsOptions & Abortable; + + constructor(db: Db, filter: Document, options?: ListCollectionsOptions & Abortable) { + super(db.client, db.s.namespace, options); + this.parent = db; + this.filter = filter; + this.options = options; + } + + clone(): ListCollectionsCursor { + return new ListCollectionsCursor(this.parent, this.filter, { + ...this.options, + ...this.cursorOptions + }); + } + + /** @internal */ + async _initialize(session: ClientSession | undefined): Promise { + const operation = new ListCollectionsOperation(this.parent, this.filter, { + ...this.cursorOptions, + ...this.options, + session, + signal: this.signal + }); + + const response = await executeOperation(this.parent.client, operation, this.timeoutContext); + + return { server: operation.server, session, response }; + } +} diff --git a/gateway/node_modules/mongodb/src/cursor/list_indexes_cursor.ts b/gateway/node_modules/mongodb/src/cursor/list_indexes_cursor.ts new file mode 100644 index 0000000000000000000000000000000000000000..0f768f3b699c2e191eff84f50365b1f5353a1e6a --- /dev/null +++ b/gateway/node_modules/mongodb/src/cursor/list_indexes_cursor.ts @@ -0,0 +1,37 @@ +import type { Collection } from '../collection'; +import { executeOperation } from '../operations/execute_operation'; +import { ListIndexesOperation, type ListIndexesOptions } from '../operations/indexes'; +import type { ClientSession } from '../sessions'; +import { AbstractCursor, type InitialCursorResponse } from './abstract_cursor'; + +/** @public */ +export class ListIndexesCursor extends AbstractCursor { + parent: Collection; + options?: ListIndexesOptions; + + constructor(collection: Collection, options?: ListIndexesOptions) { + super(collection.client, collection.s.namespace, options); + this.parent = collection; + this.options = options; + } + + clone(): ListIndexesCursor { + return new ListIndexesCursor(this.parent, { + ...this.options, + ...this.cursorOptions + }); + } + + /** @internal */ + async _initialize(session: ClientSession | undefined): Promise { + const operation = new ListIndexesOperation(this.parent, { + ...this.cursorOptions, + ...this.options, + session + }); + + const response = await executeOperation(this.parent.client, operation, this.timeoutContext); + + return { server: operation.server, session, response }; + } +} diff --git a/gateway/node_modules/mongodb/src/cursor/list_search_indexes_cursor.ts b/gateway/node_modules/mongodb/src/cursor/list_search_indexes_cursor.ts new file mode 100644 index 0000000000000000000000000000000000000000..cfe1358f72574070eb620423ae9663ea15dcbef0 --- /dev/null +++ b/gateway/node_modules/mongodb/src/cursor/list_search_indexes_cursor.ts @@ -0,0 +1,20 @@ +import type { Collection } from '../collection'; +import type { AggregateOptions } from '../operations/aggregate'; +import { AggregationCursor } from './aggregation_cursor'; + +/** @public */ +export type ListSearchIndexesOptions = Omit; + +/** @public */ +export class ListSearchIndexesCursor extends AggregationCursor<{ name: string }> { + /** @internal */ + constructor( + { fullNamespace: ns, client }: Collection, + name: string | null, + options: ListSearchIndexesOptions = {} + ) { + const pipeline = + name == null ? [{ $listSearchIndexes: {} }] : [{ $listSearchIndexes: { name } }]; + super(client, ns, pipeline, options); + } +} diff --git a/gateway/node_modules/mongodb/src/cursor/run_command_cursor.ts b/gateway/node_modules/mongodb/src/cursor/run_command_cursor.ts new file mode 100644 index 0000000000000000000000000000000000000000..997fe1ffebc602078d4b4d95ebd0a536337d387b --- /dev/null +++ b/gateway/node_modules/mongodb/src/cursor/run_command_cursor.ts @@ -0,0 +1,178 @@ +import type { BSONSerializeOptions, Document } from '../bson'; +import { type CursorResponse } from '../cmap/wire_protocol/responses'; +import type { Db } from '../db'; +import { MongoAPIError, MongoRuntimeError } from '../error'; +import { executeOperation } from '../operations/execute_operation'; +import { GetMoreOperation } from '../operations/get_more'; +import { RunCursorCommandOperation } from '../operations/run_command'; +import type { ReadConcernLike } from '../read_concern'; +import type { ReadPreferenceLike } from '../read_preference'; +import type { ClientSession } from '../sessions'; +import { ns } from '../utils'; +import { + AbstractCursor, + type CursorTimeoutMode, + type InitialCursorResponse +} from './abstract_cursor'; + +/** @public */ +export type RunCursorCommandOptions = { + readPreference?: ReadPreferenceLike; + session?: ClientSession; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error. Note that if + * `maxTimeMS` is provided in the command in addition to setting `timeoutMS` in the options, then + * the original value of `maxTimeMS` will be overwritten. + */ + timeoutMS?: number; + /** + * @public + * @experimental + * Specifies how `timeoutMS` is applied to the cursor. Can be either `'cursorLifeTime'` or `'iteration'` + * When set to `'iteration'`, the deadline specified by `timeoutMS` applies to each call of + * `cursor.next()`. + * When set to `'cursorLifetime'`, the deadline applies to the life of the entire cursor. + * + * Depending on the type of cursor being used, this option has different default values. + * For non-tailable cursors, this value defaults to `'cursorLifetime'` + * For tailable cursors, this value defaults to `'iteration'` since tailable cursors, by + * definition can have an arbitrarily long lifetime. + * + * @example + * ```ts + * const cursor = collection.find({}, {timeoutMS: 100, timeoutMode: 'iteration'}); + * for await (const doc of cursor) { + * // process doc + * // This will throw a timeout error if any of the iterator's `next()` calls takes more than 100ms, but + * // will continue to iterate successfully otherwise, regardless of the number of batches. + * } + * ``` + * + * @example + * ```ts + * const cursor = collection.find({}, { timeoutMS: 1000, timeoutMode: 'cursorLifetime' }); + * const docs = await cursor.toArray(); // This entire line will throw a timeout error if all batches are not fetched and returned within 1000ms. + * ``` + */ + timeoutMode?: CursorTimeoutMode; + tailable?: boolean; + awaitData?: boolean; +} & BSONSerializeOptions; + +/** @public */ +export class RunCommandCursor extends AbstractCursor { + public readonly command: Readonly>; + public readonly getMoreOptions: { + comment?: any; + maxAwaitTimeMS?: number; + batchSize?: number; + } = {}; + + /** + * Controls the `getMore.comment` field + * @param comment - any BSON value + */ + public setComment(comment: any): this { + this.getMoreOptions.comment = comment; + return this; + } + + /** + * Controls the `getMore.maxTimeMS` field. Only valid when cursor is tailable await + * @param maxTimeMS - the number of milliseconds to wait for new data + */ + public setMaxTimeMS(maxTimeMS: number): this { + this.getMoreOptions.maxAwaitTimeMS = maxTimeMS; + return this; + } + + /** + * Controls the `getMore.batchSize` field + * @param batchSize - the number documents to return in the `nextBatch` + */ + public setBatchSize(batchSize: number): this { + this.getMoreOptions.batchSize = batchSize; + return this; + } + + /** Unsupported for RunCommandCursor */ + public override clone(): never { + throw new MongoAPIError('Clone not supported, create a new cursor with db.runCursorCommand'); + } + + /** Unsupported for RunCommandCursor: readConcern must be configured directly on command document */ + public override withReadConcern(_: ReadConcernLike): never { + throw new MongoAPIError( + 'RunCommandCursor does not support readConcern it must be attached to the command being run' + ); + } + + /** Unsupported for RunCommandCursor: various cursor flags must be configured directly on command document */ + public override addCursorFlag(_: string, __: boolean): never { + throw new MongoAPIError( + 'RunCommandCursor does not support cursor flags, they must be attached to the command being run' + ); + } + + /** + * Unsupported for RunCommandCursor: maxTimeMS must be configured directly on command document + */ + public override maxTimeMS(_: number): never { + throw new MongoAPIError( + 'maxTimeMS must be configured on the command document directly, to configure getMore.maxTimeMS use cursor.setMaxTimeMS()' + ); + } + + /** Unsupported for RunCommandCursor: batchSize must be configured directly on command document */ + public override batchSize(_: number): never { + throw new MongoAPIError( + 'batchSize must be configured on the command document directly, to configure getMore.batchSize use cursor.setBatchSize()' + ); + } + + /** @internal */ + private db: Db; + + /** @internal */ + constructor(db: Db, command: Document, options: RunCursorCommandOptions = {}) { + super(db.client, ns(db.namespace), options); + this.db = db; + this.command = Object.freeze({ ...command }); + } + + /** @internal */ + protected async _initialize(session: ClientSession): Promise { + const operation = new RunCursorCommandOperation(this.db.s.namespace, this.command, { + ...this.cursorOptions, + session: session, + readPreference: this.cursorOptions.readPreference + }); + + const response = await executeOperation(this.client, operation, this.timeoutContext); + + return { + server: operation.server, + session, + response + }; + } + + /** @internal */ + override async getMore(): Promise { + if (!this.session) { + throw new MongoRuntimeError( + 'Unexpected null session. A cursor creating command should have set this' + ); + } + + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const getMoreOperation = new GetMoreOperation(this.namespace, this.id!, this.server!, { + ...this.cursorOptions, + session: this.session, + ...this.getMoreOptions + }); + + return await executeOperation(this.client, getMoreOperation, this.timeoutContext); + } +} diff --git a/gateway/node_modules/mongodb/src/db.ts b/gateway/node_modules/mongodb/src/db.ts new file mode 100644 index 0000000000000000000000000000000000000000..0907b6f0a015f4f8907fdd53322353bbcddb4013 --- /dev/null +++ b/gateway/node_modules/mongodb/src/db.ts @@ -0,0 +1,621 @@ +import { Admin } from './admin'; +import { type BSONSerializeOptions, type Document, resolveBSONOptions } from './bson'; +import { ChangeStream, type ChangeStreamDocument, type ChangeStreamOptions } from './change_stream'; +import { Collection, type CollectionOptions } from './collection'; +import * as CONSTANTS from './constants'; +import { AggregationCursor } from './cursor/aggregation_cursor'; +import { ListCollectionsCursor } from './cursor/list_collections_cursor'; +import { RunCommandCursor, type RunCursorCommandOptions } from './cursor/run_command_cursor'; +import { MongoInvalidArgumentError } from './error'; +import type { MongoClient, PkFactory } from './mongo_client'; +import type { Abortable, TODO_NODE_3286 } from './mongo_types'; +import type { AggregateOptions } from './operations/aggregate'; +import { type CreateCollectionOptions, createCollections } from './operations/create_collection'; +import { + type DropCollectionOptions, + dropCollections, + DropDatabaseOperation, + type DropDatabaseOptions +} from './operations/drop'; +import { executeOperation } from './operations/execute_operation'; +import { + CreateIndexesOperation, + type CreateIndexesOptions, + type IndexDescriptionCompact, + type IndexDescriptionInfo, + type IndexInformationOptions, + type IndexSpecification +} from './operations/indexes'; +import type { CollectionInfo, ListCollectionsOptions } from './operations/list_collections'; +import { ProfilingLevelOperation, type ProfilingLevelOptions } from './operations/profiling_level'; +import { RemoveUserOperation, type RemoveUserOptions } from './operations/remove_user'; +import { RenameOperation, type RenameOptions } from './operations/rename'; +import { RunCommandOperation, type RunCommandOptions } from './operations/run_command'; +import { + type ProfilingLevel, + SetProfilingLevelOperation, + type SetProfilingLevelOptions +} from './operations/set_profiling_level'; +import { DbStatsOperation, type DbStatsOptions } from './operations/stats'; +import { ReadConcern } from './read_concern'; +import { ReadPreference, type ReadPreferenceLike } from './read_preference'; +import { DEFAULT_PK_FACTORY, filterOptions, MongoDBNamespace, resolveOptions } from './utils'; +import { WriteConcern, type WriteConcernOptions } from './write_concern'; + +// Allowed parameters +const DB_OPTIONS_ALLOW_LIST = [ + 'writeConcern', + 'readPreference', + 'readPreferenceTags', + 'native_parser', + 'forceServerObjectId', + 'pkFactory', + 'serializeFunctions', + 'raw', + 'authSource', + 'ignoreUndefined', + 'readConcern', + 'retryMiliSeconds', + 'numberOfRetries', + 'useBigInt64', + 'promoteBuffers', + 'promoteLongs', + 'bsonRegExp', + 'enableUtf8Validation', + 'promoteValues', + 'compression', + 'retryWrites', + 'timeoutMS' +]; + +/** @internal */ +export interface DbPrivate { + options?: DbOptions; + readPreference?: ReadPreference; + pkFactory: PkFactory; + readConcern?: ReadConcern; + bsonOptions: BSONSerializeOptions; + writeConcern?: WriteConcern; + namespace: MongoDBNamespace; +} + +/** @public */ +export interface DbOptions extends BSONSerializeOptions, WriteConcernOptions { + /** If the database authentication is dependent on another databaseName. */ + authSource?: string; + /** Force server to assign _id values instead of driver. */ + forceServerObjectId?: boolean; + /** The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). */ + readPreference?: ReadPreferenceLike; + /** A primary key factory object for generation of custom _id keys. */ + pkFactory?: PkFactory; + /** Specify a read concern for the collection. (only MongoDB 3.2 or higher supported) */ + readConcern?: ReadConcern; + /** Should retry failed writes */ + retryWrites?: boolean; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; +} + +/** + * The **Db** class is a class that represents a MongoDB Database. + * @public + * + * @example + * ```ts + * import { MongoClient } from 'mongodb'; + * + * interface Pet { + * name: string; + * kind: 'dog' | 'cat' | 'fish'; + * } + * + * const client = new MongoClient('mongodb://localhost:27017'); + * const db = client.db(); + * + * // Create a collection that validates our union + * await db.createCollection('pets', { + * validator: { $expr: { $in: ['$kind', ['dog', 'cat', 'fish']] } } + * }) + * ``` + */ +export class Db { + /** @internal */ + s: DbPrivate; + + /** + * Gets the MongoClient associated with the Db. + * @public + */ + readonly client: MongoClient; + + public static SYSTEM_NAMESPACE_COLLECTION = CONSTANTS.SYSTEM_NAMESPACE_COLLECTION; + public static SYSTEM_INDEX_COLLECTION = CONSTANTS.SYSTEM_INDEX_COLLECTION; + public static SYSTEM_PROFILE_COLLECTION = CONSTANTS.SYSTEM_PROFILE_COLLECTION; + public static SYSTEM_USER_COLLECTION = CONSTANTS.SYSTEM_USER_COLLECTION; + public static SYSTEM_COMMAND_COLLECTION = CONSTANTS.SYSTEM_COMMAND_COLLECTION; + public static SYSTEM_JS_COLLECTION = CONSTANTS.SYSTEM_JS_COLLECTION; + + /** + * Creates a new Db instance. + * + * Db name cannot contain a dot, the server may apply more restrictions when an operation is run. + * + * @param client - The MongoClient for the database. + * @param databaseName - The name of the database this instance represents. + * @param options - Optional settings for Db construction. + */ + constructor(client: MongoClient, databaseName: string, options?: DbOptions) { + options = options ?? {}; + + // Filter the options + options = filterOptions(options, DB_OPTIONS_ALLOW_LIST); + + // Ensure there are no dots in database name + if (typeof databaseName === 'string' && databaseName.includes('.')) { + throw new MongoInvalidArgumentError(`Database names cannot contain the character '.'`); + } + + // Internal state of the db object + this.s = { + // Options + options, + // Unpack read preference + readPreference: ReadPreference.fromOptions(options), + // Merge bson options + bsonOptions: resolveBSONOptions(options, client), + // Set up the primary key factory or fallback to ObjectId + pkFactory: options?.pkFactory ?? DEFAULT_PK_FACTORY, + // ReadConcern + readConcern: ReadConcern.fromOptions(options), + writeConcern: WriteConcern.fromOptions(options), + // Namespace + namespace: new MongoDBNamespace(databaseName) + }; + + this.client = client; + } + + get databaseName(): string { + return this.s.namespace.db; + } + + // Options + get options(): DbOptions | undefined { + return this.s.options; + } + + /** + * Check if a secondary can be used (because the read preference is *not* set to primary) + */ + get secondaryOk(): boolean { + return this.s.readPreference?.preference !== 'primary' || false; + } + + get readConcern(): ReadConcern | undefined { + return this.s.readConcern; + } + + /** + * The current readPreference of the Db. If not explicitly defined for + * this Db, will be inherited from the parent MongoClient + */ + get readPreference(): ReadPreference { + if (this.s.readPreference == null) { + return this.client.readPreference; + } + + return this.s.readPreference; + } + + get bsonOptions(): BSONSerializeOptions { + return this.s.bsonOptions; + } + + // get the write Concern + get writeConcern(): WriteConcern | undefined { + return this.s.writeConcern; + } + + get namespace(): string { + return this.s.namespace.toString(); + } + + public get timeoutMS(): number | undefined { + return this.s.options?.timeoutMS; + } + + /** + * Create a new collection on a server with the specified options. Use this to create capped collections. + * More information about command options available at https://www.mongodb.com/docs/manual/reference/command/create/ + * + * Collection namespace validation is performed server-side. + * + * @param name - The name of the collection to create + * @param options - Optional settings for the command + */ + async createCollection( + name: string, + options?: CreateCollectionOptions + ): Promise> { + options = resolveOptions(this, options); + return await createCollections(this, name, options); + } + + /** + * Execute a command + * + * @remarks + * This command does not inherit options from the MongoClient. + * + * The driver will ensure the following fields are attached to the command sent to the server: + * - `lsid` - sourced from an implicit session or options.session + * - `$readPreference` - defaults to primary or can be configured by options.readPreference + * - `$db` - sourced from the name of this database + * + * If the client has a serverApi setting: + * - `apiVersion` + * - `apiStrict` + * - `apiDeprecationErrors` + * + * When in a transaction: + * - `readConcern` - sourced from readConcern set on the TransactionOptions + * - `writeConcern` - sourced from writeConcern set on the TransactionOptions + * + * Attaching any of the above fields to the command will have no effect as the driver will overwrite the value. + * + * @param command - The command to run + * @param options - Optional settings for the command + */ + async command(command: Document, options?: RunCommandOptions & Abortable): Promise { + // Intentionally, we do not inherit options from parent for this operation. + return await executeOperation( + this.client, + new RunCommandOperation( + this.s.namespace, + command, + resolveOptions(undefined, { + ...resolveBSONOptions(options), + timeoutMS: options?.timeoutMS ?? this.timeoutMS, + session: options?.session, + readPreference: options?.readPreference, + signal: options?.signal + }) + ) + ); + } + + /** + * Execute an aggregation framework pipeline against the database. + * + * @param pipeline - An array of aggregation stages to be executed + * @param options - Optional settings for the command + */ + aggregate( + pipeline: Document[] = [], + options?: AggregateOptions + ): AggregationCursor { + return new AggregationCursor( + this.client, + this.s.namespace, + pipeline, + resolveOptions(this, options) + ); + } + + /** Return the Admin db instance */ + admin(): Admin { + return new Admin(this); + } + + /** + * Returns a reference to a MongoDB Collection. If it does not exist it will be created implicitly. + * + * Collection namespace validation is performed server-side. + * + * @param name - the collection name we wish to access. + * @returns return the new Collection instance + */ + collection( + name: string, + options: CollectionOptions = {} + ): Collection { + if (typeof options === 'function') { + throw new MongoInvalidArgumentError('The callback form of this helper has been removed.'); + } + return new Collection(this, name, resolveOptions(this, options)); + } + + /** + * Get all the db statistics. + * + * @param options - Optional settings for the command + */ + async stats(options?: DbStatsOptions): Promise { + return await executeOperation( + this.client, + new DbStatsOperation(this, resolveOptions(this, options)) + ); + } + + /** + * List all collections of this database with optional filter + * + * @param filter - Query to filter collections by + * @param options - Optional settings for the command + */ + listCollections( + filter: Document, + options: Exclude & { nameOnly: true } & Abortable + ): ListCollectionsCursor>; + listCollections( + filter: Document, + options: Exclude & { nameOnly: false } & Abortable + ): ListCollectionsCursor; + listCollections< + T extends Pick | CollectionInfo = + | Pick + | CollectionInfo + >(filter?: Document, options?: ListCollectionsOptions & Abortable): ListCollectionsCursor; + listCollections< + T extends Pick | CollectionInfo = + | Pick + | CollectionInfo + >( + filter: Document = {}, + options: ListCollectionsOptions & Abortable = {} + ): ListCollectionsCursor { + return new ListCollectionsCursor(this, filter, resolveOptions(this, options)); + } + + /** + * Rename a collection. + * + * @remarks + * This operation does not inherit options from the MongoClient. + * + * @param fromCollection - Name of current collection to rename + * @param toCollection - New name of of the collection + * @param options - Optional settings for the command + */ + async renameCollection( + fromCollection: string, + toCollection: string, + options?: RenameOptions + ): Promise> { + // Intentionally, we do not inherit options from parent for this operation. + return await executeOperation( + this.client, + new RenameOperation( + this.collection(fromCollection) as TODO_NODE_3286, + toCollection, + resolveOptions(undefined, { + ...options, + readPreference: ReadPreference.primary + }) + ) as TODO_NODE_3286 + ); + } + + /** + * Drop a collection from the database, removing it permanently. New accesses will create a new collection. + * + * @param name - Name of collection to drop + * @param options - Optional settings for the command + */ + async dropCollection(name: string, options?: DropCollectionOptions): Promise { + options = resolveOptions(this, options); + return await dropCollections(this, name, options); + } + + /** + * Drop a database, removing it permanently from the server. + * + * @param options - Optional settings for the command + */ + async dropDatabase(options?: DropDatabaseOptions): Promise { + return await executeOperation( + this.client, + new DropDatabaseOperation(this, resolveOptions(this, options)) + ); + } + + /** + * Fetch all collections for the current db. + * + * @param options - Optional settings for the command + */ + async collections(options?: ListCollectionsOptions): Promise { + options = resolveOptions(this, options); + const collections = await this.listCollections({}, { ...options, nameOnly: true }).toArray(); + + return collections + .filter( + // Filter collections removing any illegal ones + ({ name }) => !name.includes('$') + ) + .map(({ name }) => new Collection(this, name, this.s.options)); + } + + /** + * Creates an index on the db and collection. + * + * @param name - Name of the collection to create the index on. + * @param indexSpec - Specify the field to index, or an index specification + * @param options - Optional settings for the command + */ + async createIndex( + name: string, + indexSpec: IndexSpecification, + options?: CreateIndexesOptions + ): Promise { + const indexes = await executeOperation( + this.client, + CreateIndexesOperation.fromIndexSpecification(this, name, indexSpec, options) + ); + return indexes[0]; + } + + /** + * Remove a user from a database + * + * @param username - The username to remove + * @param options - Optional settings for the command + */ + async removeUser(username: string, options?: RemoveUserOptions): Promise { + return await executeOperation( + this.client, + new RemoveUserOperation(this, username, resolveOptions(this, options)) + ); + } + + /** + * Set the current profiling level of MongoDB + * + * @param level - The new profiling level (off, slow_only, all). + * @param options - Optional settings for the command + */ + async setProfilingLevel( + level: ProfilingLevel, + options?: SetProfilingLevelOptions + ): Promise { + return await executeOperation( + this.client, + new SetProfilingLevelOperation(this, level, resolveOptions(this, options)) + ); + } + + /** + * Retrieve the current profiling Level for MongoDB + * + * @param options - Optional settings for the command + */ + async profilingLevel(options?: ProfilingLevelOptions): Promise { + return await executeOperation( + this.client, + new ProfilingLevelOperation(this, resolveOptions(this, options)) + ); + } + + /** + * Retrieves this collections index info. + * + * @param name - The name of the collection. + * @param options - Optional settings for the command + */ + indexInformation( + name: string, + options: IndexInformationOptions & { full: true } + ): Promise; + indexInformation( + name: string, + options: IndexInformationOptions & { full?: false } + ): Promise; + indexInformation( + name: string, + options: IndexInformationOptions + ): Promise; + indexInformation(name: string): Promise; + async indexInformation( + name: string, + options?: IndexInformationOptions + ): Promise { + return await this.collection(name).indexInformation(resolveOptions(this, options)); + } + + /** + * Create a new Change Stream, watching for new changes (insertions, updates, + * replacements, deletions, and invalidations) in this database. Will ignore all + * changes to system collections. + * + * @remarks + * watch() accepts two generic arguments for distinct use cases: + * - The first is to provide the schema that may be defined for all the collections within this database + * - The second is to override the shape of the change stream document entirely, if it is not provided the type will default to ChangeStreamDocument of the first argument + * + * @remarks + * When `timeoutMS` is configured for a change stream, it will have different behaviour depending + * on whether the change stream is in iterator mode or emitter mode. In both cases, a change + * stream will time out if it does not receive a change event within `timeoutMS` of the last change + * event. + * + * Note that if a change stream is consistently timing out when watching a collection, database or + * client that is being changed, then this may be due to the server timing out before it can finish + * processing the existing oplog. To address this, restart the change stream with a higher + * `timeoutMS`. + * + * If the change stream times out the initial aggregate operation to establish the change stream on + * the server, then the client will close the change stream. If the getMore calls to the server + * time out, then the change stream will be left open, but will throw a MongoOperationTimeoutError + * when in iterator mode and emit an error event that returns a MongoOperationTimeoutError in + * emitter mode. + * + * To determine whether or not the change stream is still open following a timeout, check the + * {@link ChangeStream.closed} getter. + * + * @example + * In iterator mode, if a next() call throws a timeout error, it will attempt to resume the change stream. + * The next call can just be retried after this succeeds. + * ```ts + * const changeStream = collection.watch([], { timeoutMS: 100 }); + * try { + * await changeStream.next(); + * } catch (e) { + * if (e instanceof MongoOperationTimeoutError && !changeStream.closed) { + * await changeStream.next(); + * } + * throw e; + * } + * ``` + * + * @example + * In emitter mode, if the change stream goes `timeoutMS` without emitting a change event, it will + * emit an error event that returns a MongoOperationTimeoutError, but will not close the change + * stream unless the resume attempt fails. There is no need to re-establish change listeners as + * this will automatically continue emitting change events once the resume attempt completes. + * + * ```ts + * const changeStream = collection.watch([], { timeoutMS: 100 }); + * changeStream.on('change', console.log); + * changeStream.on('error', e => { + * if (e instanceof MongoOperationTimeoutError && !changeStream.closed) { + * // do nothing + * } else { + * changeStream.close(); + * } + * }); + * ``` + * @param pipeline - An array of {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents. + * @param options - Optional settings for the command + * @typeParam TSchema - Type of the data being detected by the change stream + * @typeParam TChange - Type of the whole change stream document emitted + */ + watch< + TSchema extends Document = Document, + TChange extends Document = ChangeStreamDocument + >(pipeline: Document[] = [], options: ChangeStreamOptions = {}): ChangeStream { + // Allow optionally not specifying a pipeline + if (!Array.isArray(pipeline)) { + options = pipeline; + pipeline = []; + } + + return new ChangeStream(this, pipeline, resolveOptions(this, options)); + } + + /** + * A low level cursor API providing basic driver functionality: + * - ClientSession management + * - ReadPreference for server selection + * - Running getMores automatically when a local batch is exhausted + * + * @param command - The command that will start a cursor on the server. + * @param options - Configurations for running the command, bson options will apply to getMores + */ + runCursorCommand(command: Document, options?: RunCursorCommandOptions): RunCommandCursor { + return new RunCommandCursor(this, command, options); + } +} diff --git a/gateway/node_modules/mongodb/src/deps.ts b/gateway/node_modules/mongodb/src/deps.ts new file mode 100644 index 0000000000000000000000000000000000000000..fc92beda8bd5e10db1f58868d60b415c6ae39bea --- /dev/null +++ b/gateway/node_modules/mongodb/src/deps.ts @@ -0,0 +1,227 @@ +import { type Stream } from './cmap/connect'; +import { MongoMissingDependencyError } from './error'; +import type { Callback } from './utils'; + +function makeErrorModule(error: any) { + const props = error ? { kModuleError: error } : {}; + return new Proxy(props, { + get: (_: any, key: any) => { + if (key === 'kModuleError') { + return error; + } + throw error; + }, + set: () => { + throw error; + } + }); +} + +export type Kerberos = typeof import('kerberos') | { kModuleError: MongoMissingDependencyError }; + +export function getKerberos(): Kerberos { + let kerberos: Kerberos; + try { + // Ensure you always wrap an optional require in the try block NODE-3199 + // eslint-disable-next-line @typescript-eslint/no-require-imports + kerberos = require('kerberos'); + } catch (error) { + kerberos = makeErrorModule( + new MongoMissingDependencyError( + 'Optional module `kerberos` not found. Please install it to enable kerberos authentication', + { cause: error, dependencyName: 'kerberos' } + ) + ); + } + return kerberos; +} + +export interface KerberosClient { + step(challenge: string): Promise; + step(challenge: string, callback: Callback): void; + wrap(challenge: string, options: { user: string }): Promise; + wrap(challenge: string, options: { user: string }, callback: Callback): void; + unwrap(challenge: string): Promise; + unwrap(challenge: string, callback: Callback): void; +} + +type ZStandardLib = { + /** + * Compress using zstd. + * @param buf - Buffer to be compressed. + */ + compress(buf: Uint8Array, level?: number): Promise; + + /** + * Decompress using zstd. + */ + decompress(buf: Uint8Array): Promise; +}; + +export type ZStandard = ZStandardLib | { kModuleError: MongoMissingDependencyError }; + +export function getZstdLibrary(): ZStandardLib | { kModuleError: MongoMissingDependencyError } { + let ZStandard: ZStandardLib | { kModuleError: MongoMissingDependencyError }; + try { + // eslint-disable-next-line @typescript-eslint/no-require-imports + ZStandard = require('@mongodb-js/zstd'); + } catch (error) { + ZStandard = makeErrorModule( + new MongoMissingDependencyError( + 'Optional module `@mongodb-js/zstd` not found. Please install it to enable zstd compression', + { cause: error, dependencyName: 'zstd' } + ) + ); + } + + return ZStandard; +} + +/** + * @public + * Copy of the AwsCredentialIdentityProvider interface from [`smithy/types`](https://socket.dev/npm/package/\@smithy/types/files/1.1.1/dist-types/identity/awsCredentialIdentity.d.ts), + * the return type of the aws-sdk's `fromNodeProviderChain().provider()`. + */ +export interface AWSCredentials { + accessKeyId: string; + secretAccessKey: string; + sessionToken?: string; + expiration?: Date; +} + +type CredentialProvider = { + fromNodeProviderChain( + this: void, + options: { clientConfig: { region: string } } + ): () => Promise; + fromNodeProviderChain(this: void): () => Promise; +}; + +export function getAwsCredentialProvider(): + | CredentialProvider + | { kModuleError: MongoMissingDependencyError } { + try { + // Ensure you always wrap an optional require in the try block NODE-3199 + // eslint-disable-next-line @typescript-eslint/no-require-imports + const credentialProvider = require('@aws-sdk/credential-providers'); + return credentialProvider; + } catch (error) { + return makeErrorModule( + new MongoMissingDependencyError( + 'Optional module `@aws-sdk/credential-providers` not found.' + + ' Please install it to enable getting aws credentials via the official sdk.', + { cause: error, dependencyName: '@aws-sdk/credential-providers' } + ) + ); + } +} + +/** @internal */ +export type GcpMetadata = + | typeof import('gcp-metadata') + | { kModuleError: MongoMissingDependencyError }; + +export function getGcpMetadata(): GcpMetadata { + try { + // Ensure you always wrap an optional require in the try block NODE-3199 + // eslint-disable-next-line @typescript-eslint/no-require-imports + const credentialProvider = require('gcp-metadata'); + return credentialProvider; + } catch (error) { + return makeErrorModule( + new MongoMissingDependencyError( + 'Optional module `gcp-metadata` not found.' + + ' Please install it to enable getting gcp credentials via the official sdk.', + { cause: error, dependencyName: 'gcp-metadata' } + ) + ); + } +} + +/** @internal */ +export type SnappyLib = { + /** + * In order to support both we must check the return value of the function + * @param buf - Buffer to be compressed + */ + compress(buf: Uint8Array): Promise; + + /** + * In order to support both we must check the return value of the function + * @param buf - Buffer to be compressed + */ + uncompress(buf: Uint8Array, opt: { asBuffer: true }): Promise; +}; + +export function getSnappy(): SnappyLib | { kModuleError: MongoMissingDependencyError } { + try { + // Ensure you always wrap an optional require in the try block NODE-3199 + // eslint-disable-next-line @typescript-eslint/no-require-imports + const value = require('snappy'); + return value; + } catch (error) { + const kModuleError = new MongoMissingDependencyError( + 'Optional module `snappy` not found. Please install it to enable snappy compression', + { cause: error, dependencyName: 'snappy' } + ); + return { kModuleError }; + } +} + +export type SocksLib = { + SocksClient: { + createConnection(options: { + command: 'connect'; + destination: { host: string; port: number }; + proxy: { + /** host and port are ignored because we pass existing_socket */ + host: 'iLoveJavaScript'; + port: 0; + type: 5; + userId?: string; + password?: string; + }; + timeout?: number; + /** We always create our own socket, and pass it to this API for proxy negotiation */ + existing_socket: Stream; + }): Promise<{ socket: Stream }>; + }; +}; + +export function getSocks(): SocksLib | { kModuleError: MongoMissingDependencyError } { + try { + // Ensure you always wrap an optional require in the try block NODE-3199 + // eslint-disable-next-line @typescript-eslint/no-require-imports + const value = require('socks'); + return value; + } catch (error) { + const kModuleError = new MongoMissingDependencyError( + 'Optional module `socks` not found. Please install it to connections over a SOCKS5 proxy', + { cause: error, dependencyName: 'socks' } + ); + return { kModuleError }; + } +} + +/** A utility function to get the instance of mongodb-client-encryption, if it exists. */ +export function getMongoDBClientEncryption(): + | typeof import('mongodb-client-encryption') + | { kModuleError: MongoMissingDependencyError } { + let mongodbClientEncryption = null; + + try { + // NOTE(NODE-3199): Ensure you always wrap an optional require literally in the try block + // Cannot be moved to helper utility function, bundlers search and replace the actual require call + // in a way that makes this line throw at bundle time, not runtime, catching here will make bundling succeed + // eslint-disable-next-line @typescript-eslint/no-require-imports + mongodbClientEncryption = require('mongodb-client-encryption'); + } catch (error) { + const kModuleError = new MongoMissingDependencyError( + 'Optional module `mongodb-client-encryption` not found. Please install it to use auto encryption or ClientEncryption.', + { cause: error, dependencyName: 'mongodb-client-encryption' } + ); + return { kModuleError }; + } + + return mongodbClientEncryption; +} diff --git a/gateway/node_modules/mongodb/src/encrypter.ts b/gateway/node_modules/mongodb/src/encrypter.ts new file mode 100644 index 0000000000000000000000000000000000000000..5a627ea67e981917a860006089e22a760acb5dff --- /dev/null +++ b/gateway/node_modules/mongodb/src/encrypter.ts @@ -0,0 +1,127 @@ +import { AutoEncrypter, type AutoEncryptionOptions } from './client-side-encryption/auto_encrypter'; +import { MONGO_CLIENT_EVENTS } from './constants'; +import { getMongoDBClientEncryption } from './deps'; +import { MongoInvalidArgumentError, MongoMissingDependencyError } from './error'; +import { MongoClient, type MongoClientOptions } from './mongo_client'; + +/** @internal */ +export interface EncrypterOptions { + autoEncryption: AutoEncryptionOptions; + maxPoolSize?: number; +} + +/** @internal */ +export class Encrypter { + private internalClient: MongoClient | null; + bypassAutoEncryption: boolean; + needsConnecting: boolean; + autoEncrypter: AutoEncrypter; + + constructor(client: MongoClient, uri: string, options: MongoClientOptions) { + if (typeof options.autoEncryption !== 'object') { + throw new MongoInvalidArgumentError('Option "autoEncryption" must be specified'); + } + // initialize to null, if we call getInternalClient, we may set this it is important to not overwrite those function calls. + this.internalClient = null; + + this.bypassAutoEncryption = !!options.autoEncryption.bypassAutoEncryption; + this.needsConnecting = false; + + if (options.maxPoolSize === 0 && options.autoEncryption.keyVaultClient == null) { + options.autoEncryption.keyVaultClient = client; + } else if (options.autoEncryption.keyVaultClient == null) { + options.autoEncryption.keyVaultClient = this.getInternalClient(client, uri, options); + } + + if (this.bypassAutoEncryption) { + options.autoEncryption.metadataClient = undefined; + } else if (options.maxPoolSize === 0) { + options.autoEncryption.metadataClient = client; + } else { + options.autoEncryption.metadataClient = this.getInternalClient(client, uri, options); + } + + if (options.proxyHost) { + options.autoEncryption.proxyOptions = { + proxyHost: options.proxyHost, + proxyPort: options.proxyPort, + proxyUsername: options.proxyUsername, + proxyPassword: options.proxyPassword + }; + } + + this.autoEncrypter = new AutoEncrypter(client, options.autoEncryption); + } + + getInternalClient(client: MongoClient, uri: string, options: MongoClientOptions): MongoClient { + let internalClient = this.internalClient; + if (internalClient == null) { + const clonedOptions: MongoClientOptions = {}; + + for (const key of [ + ...Object.getOwnPropertyNames(options), + ...Object.getOwnPropertySymbols(options) + ] as string[]) { + if (['autoEncryption', 'minPoolSize', 'servers', 'caseTranslate', 'dbName'].includes(key)) + continue; + Reflect.set(clonedOptions, key, Reflect.get(options, key)); + } + + clonedOptions.minPoolSize = 0; + + internalClient = new MongoClient(uri, clonedOptions); + this.internalClient = internalClient; + + for (const eventName of MONGO_CLIENT_EVENTS) { + for (const listener of client.listeners(eventName)) { + internalClient.on(eventName, listener); + } + } + + client.on('newListener', (eventName, listener) => { + internalClient?.on(eventName, listener); + }); + + this.needsConnecting = true; + } + return internalClient; + } + + async connectInternalClient(): Promise { + const internalClient = this.internalClient; + if (this.needsConnecting && internalClient != null) { + this.needsConnecting = false; + await internalClient.connect(); + } + } + + async close(client: MongoClient): Promise { + let error; + try { + await this.autoEncrypter.close(); + } catch (autoEncrypterError) { + error = autoEncrypterError; + } + const internalClient = this.internalClient; + if (internalClient != null && client !== internalClient) { + return await internalClient.close(); + } + if (error != null) { + throw error; + } + } + + static checkForMongoCrypt(): void { + const mongodbClientEncryption = getMongoDBClientEncryption(); + if ('kModuleError' in mongodbClientEncryption) { + throw new MongoMissingDependencyError( + 'Auto-encryption requested, but the module is not installed. ' + + 'Please add `mongodb-client-encryption` as a dependency of your project', + { + cause: mongodbClientEncryption['kModuleError'], + dependencyName: 'mongodb-client-encryption' + } + ); + } + } +} diff --git a/gateway/node_modules/mongodb/src/error.ts b/gateway/node_modules/mongodb/src/error.ts new file mode 100644 index 0000000000000000000000000000000000000000..e17e3e71ebb687e8653dda0d9187e90eb01961a6 --- /dev/null +++ b/gateway/node_modules/mongodb/src/error.ts @@ -0,0 +1,1567 @@ +import type { Document } from './bson'; +import { + type ClientBulkWriteError, + type ClientBulkWriteResult +} from './operations/client_bulk_write/common'; +import type { ServerType } from './sdam/common'; +import type { TopologyVersion } from './sdam/server_description'; +import type { TopologyDescription } from './sdam/topology_description'; + +/** @public */ +export type AnyError = MongoError | Error; + +/** + * @internal + * The legacy error message from the server that indicates the node is not a writable primary + * https://github.com/mongodb/specifications/blob/921232976f9913cf17415b5ef937ee772e45e6ae/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md#not-writable-primary-and-node-is-recovering + */ +export const LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE = new RegExp('not master', 'i'); + +/** + * @internal + * The legacy error message from the server that indicates the node is not a primary or secondary + * https://github.com/mongodb/specifications/blob/921232976f9913cf17415b5ef937ee772e45e6ae/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md#not-writable-primary-and-node-is-recovering + */ +export const LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE = new RegExp( + 'not master or secondary', + 'i' +); + +/** + * @internal + * The error message from the server that indicates the node is recovering + * https://github.com/mongodb/specifications/blob/921232976f9913cf17415b5ef937ee772e45e6ae/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md#not-writable-primary-and-node-is-recovering + */ +export const NODE_IS_RECOVERING_ERROR_MESSAGE = new RegExp('node is recovering', 'i'); + +/** @internal MongoDB Error Codes */ +export const MONGODB_ERROR_CODES = Object.freeze({ + HostUnreachable: 6, + HostNotFound: 7, + AuthenticationFailed: 18, + NetworkTimeout: 89, + ShutdownInProgress: 91, + PrimarySteppedDown: 189, + ExceededTimeLimit: 262, + SocketException: 9001, + NotWritablePrimary: 10107, + InterruptedAtShutdown: 11600, + InterruptedDueToReplStateChange: 11602, + NotPrimaryNoSecondaryOk: 13435, + NotPrimaryOrSecondary: 13436, + StaleShardVersion: 63, + StaleEpoch: 150, + StaleConfig: 13388, + RetryChangeStream: 234, + FailedToSatisfyReadPreference: 133, + CursorNotFound: 43, + LegacyNotPrimary: 10058, + // WriteConcernTimeout is WriteConcernFailed on pre-8.1 servers + WriteConcernTimeout: 64, + NamespaceNotFound: 26, + IllegalOperation: 20, + MaxTimeMSExpired: 50, + UnknownReplWriteConcern: 79, + UnsatisfiableWriteConcern: 100, + Reauthenticate: 391, + ReadConcernMajorityNotAvailableYet: 134 +} as const); + +// From spec https://github.com/mongodb/specifications/blob/921232976f9913cf17415b5ef937ee772e45e6ae/source/change-streams/change-streams.md#resumable-error +export const GET_MORE_RESUMABLE_CODES = new Set([ + MONGODB_ERROR_CODES.HostUnreachable, + MONGODB_ERROR_CODES.HostNotFound, + MONGODB_ERROR_CODES.NetworkTimeout, + MONGODB_ERROR_CODES.ShutdownInProgress, + MONGODB_ERROR_CODES.PrimarySteppedDown, + MONGODB_ERROR_CODES.ExceededTimeLimit, + MONGODB_ERROR_CODES.SocketException, + MONGODB_ERROR_CODES.NotWritablePrimary, + MONGODB_ERROR_CODES.InterruptedAtShutdown, + MONGODB_ERROR_CODES.InterruptedDueToReplStateChange, + MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk, + MONGODB_ERROR_CODES.NotPrimaryOrSecondary, + MONGODB_ERROR_CODES.StaleShardVersion, + MONGODB_ERROR_CODES.StaleEpoch, + MONGODB_ERROR_CODES.StaleConfig, + MONGODB_ERROR_CODES.RetryChangeStream, + MONGODB_ERROR_CODES.FailedToSatisfyReadPreference, + MONGODB_ERROR_CODES.CursorNotFound +]); + +/** @public */ +export const MongoErrorLabel = Object.freeze({ + RetryableWriteError: 'RetryableWriteError', + TransientTransactionError: 'TransientTransactionError', + UnknownTransactionCommitResult: 'UnknownTransactionCommitResult', + ResumableChangeStreamError: 'ResumableChangeStreamError', + HandshakeError: 'HandshakeError', + ResetPool: 'ResetPool', + PoolRequestedRetry: 'PoolRequestedRetry', + InterruptInUseConnections: 'InterruptInUseConnections', + NoWritesPerformed: 'NoWritesPerformed', + RetryableError: 'RetryableError', + SystemOverloadedError: 'SystemOverloadedError' +} as const); + +/** @public */ +export type MongoErrorLabel = (typeof MongoErrorLabel)[keyof typeof MongoErrorLabel]; + +/** @public */ +export interface ErrorDescription extends Document { + message?: string; + errmsg?: string; + $err?: string; + errorLabels?: string[]; + errInfo?: Document; +} + +function isAggregateError(e: unknown): e is Error & { errors: Error[] } { + return e != null && typeof e === 'object' && 'errors' in e && Array.isArray(e.errors); +} + +/** + * @public + * @category Error + * + * @privateRemarks + * mongodb-client-encryption has a dependency on this error, it uses the constructor with a string argument + */ +export class MongoError extends Error { + /** @internal */ + private readonly errorLabelSet: Set = new Set(); + public get errorLabels(): string[] { + return Array.from(this.errorLabelSet); + } + + /** + * This is a number in MongoServerError and a string in MongoDriverError + * @privateRemarks + * Define the type override on the subclasses when we can use the override keyword + */ + code?: number | string; + topologyVersion?: TopologyVersion; + connectionGeneration?: number; + override cause?: Error; + + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { cause?: Error }) { + super(message, options); + } + + /** @internal */ + static buildErrorMessage(e: unknown): string { + if (typeof e === 'string') { + return e; + } + if (isAggregateError(e) && e.message.length === 0) { + return e.errors.length === 0 + ? 'AggregateError has an empty errors array. Please check the `cause` property for more information.' + : e.errors.map(({ message }) => message).join(', '); + } + + return e != null && typeof e === 'object' && 'message' in e && typeof e.message === 'string' + ? e.message + : 'empty error message'; + } + + override get name(): string { + return 'MongoError'; + } + + /** Legacy name for server error responses */ + get errmsg(): string { + return this.message; + } + + /** + * Checks the error to see if it has an error label + * + * @param label - The error label to check for + * @returns returns true if the error has the provided error label + */ + hasErrorLabel(label: string): boolean { + return this.errorLabelSet.has(label); + } + + addErrorLabel(label: string): void { + this.errorLabelSet.add(label); + } +} + +/** + * An error coming from the mongo server + * + * @public + * @category Error + */ +export class MongoServerError extends MongoError { + /** Raw error result document returned by server. */ + errorResponse: ErrorDescription; + codeName?: string; + writeConcernError?: Document; + errInfo?: Document; + ok?: number; + [key: string]: any; + + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: ErrorDescription) { + super(message.message || message.errmsg || message.$err || 'n/a'); + + if (message.errorLabels) { + for (const label of message.errorLabels) this.addErrorLabel(label); + } + + this.errorResponse = message; + + for (const name in message) { + if ( + name !== 'errorLabels' && + name !== 'errmsg' && + name !== 'message' && + name !== 'errorResponse' + ) { + this[name] = message[name]; + } + } + } + + override get name(): string { + return 'MongoServerError'; + } +} + +/** + * An error generated by the driver + * + * @public + * @category Error + */ +export class MongoDriverError extends MongoError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { cause?: Error }) { + super(message, options); + } + + override get name(): string { + return 'MongoDriverError'; + } +} + +/** + * An error generated when the driver API is used incorrectly + * + * @privateRemarks + * Should **never** be directly instantiated + * + * @public + * @category Error + */ + +export class MongoAPIError extends MongoDriverError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { cause?: Error }) { + super(message, options); + } + + override get name(): string { + return 'MongoAPIError'; + } +} + +/** + * An error generated when the driver encounters unexpected input + * or reaches an unexpected/invalid internal state. + * + * @privateRemarks + * Should **never** be directly instantiated. + * + * @public + * @category Error + */ +export class MongoRuntimeError extends MongoDriverError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { cause?: Error }) { + super(message, options); + } + + override get name(): string { + return 'MongoRuntimeError'; + } +} + +/** + * An error generated when a primary server is marked stale, never directly thrown + * + * @public + * @category Error + */ +export class MongoStalePrimaryError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { cause?: Error }) { + super(message, options); + } + + override get name(): string { + return 'MongoStalePrimaryError'; + } +} + +/** + * An error generated when a batch command is re-executed after one of the commands in the batch + * has failed + * + * @public + * @category Error + */ +export class MongoBatchReExecutionError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message = 'This batch has already been executed, create new batch to execute') { + super(message); + } + + override get name(): string { + return 'MongoBatchReExecutionError'; + } +} + +/** + * An error generated when the driver fails to decompress + * data received from the server. + * + * @public + * @category Error + */ +export class MongoDecompressionError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoDecompressionError'; + } +} + +/** + * An error thrown when the user attempts to operate on a database or collection through a MongoClient + * that has not yet successfully called the "connect" method + * + * @public + * @category Error + */ +export class MongoNotConnectedError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoNotConnectedError'; + } +} + +/** + * An error generated when the user makes a mistake in the usage of transactions. + * (e.g. attempting to commit a transaction with a readPreference other than primary) + * + * @public + * @category Error + */ +export class MongoTransactionError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoTransactionError'; + } +} + +/** + * An error generated when the user attempts to operate + * on a session that has expired or has been closed. + * + * @public + * @category Error + */ +export class MongoExpiredSessionError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message = 'Cannot use a session that has ended') { + super(message); + } + + override get name(): string { + return 'MongoExpiredSessionError'; + } +} + +/** + * A error generated when the user attempts to authenticate + * via Kerberos, but fails to connect to the Kerberos client. + * + * @public + * @category Error + */ +export class MongoKerberosError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoKerberosError'; + } +} + +/** + * A error generated when the user attempts to authenticate + * via AWS, but fails + * + * @public + * @category Error + */ +export class MongoAWSError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { cause?: Error }) { + super(message, options); + } + + override get name(): string { + return 'MongoAWSError'; + } +} + +/** + * A error generated when the user attempts to authenticate + * via OIDC callbacks, but fails. + * + * @public + * @category Error + */ +export class MongoOIDCError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoOIDCError'; + } +} + +/** + * A error generated when the user attempts to authenticate + * via Azure, but fails. + * + * @public + * @category Error + */ +export class MongoAzureError extends MongoOIDCError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoAzureError'; + } +} + +/** + * A error generated when the user attempts to authenticate + * via GCP, but fails. + * + * @public + * @category Error + */ +export class MongoGCPError extends MongoOIDCError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoGCPError'; + } +} + +/** + * An error indicating that an error occurred when executing the bulk write. + * + * @public + * @category Error + */ +export class MongoClientBulkWriteError extends MongoServerError { + /** + * Write concern errors that occurred while executing the bulk write. This list may have + * multiple items if more than one server command was required to execute the bulk write. + */ + writeConcernErrors: Document[]; + /** + * Errors that occurred during the execution of individual write operations. This map will + * contain at most one entry if the bulk write was ordered. + */ + writeErrors: Map; + /** + * The results of any successful operations that were performed before the error was + * encountered. + */ + partialResult?: ClientBulkWriteResult; + + /** + * Initialize the client bulk write error. + * @param message - The error message. + */ + constructor(message: ErrorDescription) { + super(message); + this.writeConcernErrors = []; + this.writeErrors = new Map(); + } + + override get name(): string { + return 'MongoClientBulkWriteError'; + } +} + +/** + * An error indicating that an error occurred when processing bulk write results. + * + * @public + * @category Error + */ +export class MongoClientBulkWriteCursorError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoClientBulkWriteCursorError'; + } +} + +/** + * An error indicating that an error occurred on the client when executing a client bulk write. + * + * @public + * @category Error + */ +export class MongoClientBulkWriteExecutionError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoClientBulkWriteExecutionError'; + } +} + +/** + * An error generated when a ChangeStream operation fails to execute. + * + * @public + * @category Error + */ +export class MongoChangeStreamError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoChangeStreamError'; + } +} + +/** + * An error thrown when the user calls a function or method not supported on a tailable cursor + * + * @public + * @category Error + */ +export class MongoTailableCursorError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message = 'Tailable cursor does not support this operation') { + super(message); + } + + override get name(): string { + return 'MongoTailableCursorError'; + } +} + +/** An error generated when a GridFSStream operation fails to execute. + * + * @public + * @category Error + */ +export class MongoGridFSStreamError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoGridFSStreamError'; + } +} + +/** + * An error generated when a malformed or invalid chunk is + * encountered when reading from a GridFSStream. + * + * @public + * @category Error + */ +export class MongoGridFSChunkError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoGridFSChunkError'; + } +} + +/** + * An error generated when a **parsable** unexpected response comes from the server. + * This is generally an error where the driver in a state expecting a certain behavior to occur in + * the next message from MongoDB but it receives something else. + * This error **does not** represent an issue with wire message formatting. + * + * #### Example + * When an operation fails, it is the driver's job to retry it. It must perform serverSelection + * again to make sure that it attempts the operation against a server in a good state. If server + * selection returns a server that does not support retryable operations, this error is used. + * This scenario is unlikely as retryable support would also have been determined on the first attempt + * but it is possible the state change could report a selectable server that does not support retries. + * + * @public + * @category Error + */ +export class MongoUnexpectedServerResponseError extends MongoRuntimeError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { cause?: Error }) { + super(message, options); + } + + override get name(): string { + return 'MongoUnexpectedServerResponseError'; + } +} + +/** + * @public + * @category Error + * + * The `MongoOperationTimeoutError` class represents an error that occurs when an operation could not be completed within the specified `timeoutMS`. + * It is generated by the driver in support of the "client side operation timeout" feature so inherits from `MongoDriverError`. + * When `timeoutMS` is enabled `MongoServerError`s relating to `MaxTimeExpired` errors will be converted to `MongoOperationTimeoutError` + * + * @example + * ```ts + * try { + * await blogs.insertOne(blogPost, { timeoutMS: 60_000 }) + * } catch (error) { + * if (error instanceof MongoOperationTimeoutError) { + * console.log(`Oh no! writer's block!`, error); + * } + * } + * ``` + */ +export class MongoOperationTimeoutError extends MongoDriverError { + override get name(): string { + return 'MongoOperationTimeoutError'; + } +} + +/** + * An error thrown when the user attempts to add options to a cursor that has already been + * initialized + * + * @public + * @category Error + */ +export class MongoCursorInUseError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message = 'Cursor is already initialized') { + super(message); + } + + override get name(): string { + return 'MongoCursorInUseError'; + } +} + +/** + * An error generated when an attempt is made to operate + * on a closed/closing server. + * + * @public + * @category Error + */ +export class MongoServerClosedError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message = 'Server is closed') { + super(message); + } + + override get name(): string { + return 'MongoServerClosedError'; + } +} + +/** + * An error thrown when an attempt is made to read from a cursor that has been exhausted + * + * @public + * @category Error + */ +export class MongoCursorExhaustedError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message?: string) { + super(message || 'Cursor is exhausted'); + } + + override get name(): string { + return 'MongoCursorExhaustedError'; + } +} + +/** + * An error generated when an attempt is made to operate on a + * dropped, or otherwise unavailable, database. + * + * @public + * @category Error + */ +export class MongoTopologyClosedError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message = 'Topology is closed') { + super(message); + } + + override get name(): string { + return 'MongoTopologyClosedError'; + } +} + +/** + * An error generated when the MongoClient is closed and async + * operations are interrupted. + * + * @public + * @category Error + */ +export class MongoClientClosedError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor() { + super('Operation interrupted because client was closed'); + } + + override get name(): string { + return 'MongoClientClosedError'; + } +} + +/** @public */ +export interface MongoNetworkErrorOptions { + /** Indicates the timeout happened before a connection handshake completed */ + beforeHandshake?: boolean; + cause?: Error; +} + +/** + * An error indicating an issue with the network, including TCP errors and timeouts. + * @public + * @category Error + */ +export class MongoNetworkError extends MongoError { + /** @internal */ + public readonly beforeHandshake: boolean; + + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: MongoNetworkErrorOptions) { + super(message, { cause: options?.cause }); + this.beforeHandshake = !!options?.beforeHandshake; + } + + override get name(): string { + return 'MongoNetworkError'; + } +} + +/** + * An error indicating a network timeout occurred + * @public + * @category Error + * + * @privateRemarks + * mongodb-client-encryption has a dependency on this error with an instanceof check + */ +export class MongoNetworkTimeoutError extends MongoNetworkError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: MongoNetworkErrorOptions) { + super(message, options); + } + + override get name(): string { + return 'MongoNetworkTimeoutError'; + } +} + +/** + * An error used when attempting to parse a value (like a connection string) + * @public + * @category Error + */ +export class MongoParseError extends MongoDriverError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoParseError'; + } +} + +/** + * An error generated when the user supplies malformed or unexpected arguments + * or when a required argument or field is not provided. + * + * + * @public + * @category Error + */ +export class MongoInvalidArgumentError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options?: { cause?: Error }) { + super(message, options); + } + + override get name(): string { + return 'MongoInvalidArgumentError'; + } +} + +/** + * An error generated when a feature that is not enabled or allowed for the current server + * configuration is used + * + * + * @public + * @category Error + */ +export class MongoCompatibilityError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoCompatibilityError'; + } +} + +/** + * An error generated when the user fails to provide authentication credentials before attempting + * to connect to a mongo server instance. + * + * + * @public + * @category Error + */ +export class MongoMissingCredentialsError extends MongoAPIError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string) { + super(message); + } + + override get name(): string { + return 'MongoMissingCredentialsError'; + } +} + +/** + * An error generated when a required module or dependency is not present in the local environment + * + * @public + * @category Error + */ +export class MongoMissingDependencyError extends MongoAPIError { + dependencyName: string; + + /** @remarks This property is assigned in the `Error` constructor. */ + declare cause: Error; + + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, options: { cause: Error; dependencyName: string }) { + super(message, options); + this.dependencyName = options.dependencyName; + } + + override get name(): string { + return 'MongoMissingDependencyError'; + } +} +/** + * An error signifying a general system issue + * @public + * @category Error + */ +export class MongoSystemError extends MongoError { + /** An optional reason context, such as an error saved during flow of monitoring and selecting servers */ + reason?: TopologyDescription; + + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, reason: TopologyDescription) { + if (reason && reason.error) { + super(MongoError.buildErrorMessage(reason.error.message || reason.error), { + cause: reason.error + }); + } else { + super(message); + } + + if (reason) { + this.reason = reason; + } + + this.code = reason.error?.code; + } + + override get name(): string { + return 'MongoSystemError'; + } +} + +/** + * An error signifying a client-side server selection error + * @public + * @category Error + */ +export class MongoServerSelectionError extends MongoSystemError { + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(message: string, reason: TopologyDescription) { + super(message, reason); + } + + override get name(): string { + return 'MongoServerSelectionError'; + } +} + +/** + * The type of the result property of MongoWriteConcernError + * @public + */ +export interface WriteConcernErrorResult { + writeConcernError: { + code: number; + errmsg: string; + codeName?: string; + errInfo?: Document; + }; + ok: number; + code?: number; + errorLabels?: string[]; + [x: string | number]: unknown; +} + +/** + * An error thrown when the server reports a writeConcernError + * @public + * @category Error + */ +export class MongoWriteConcernError extends MongoServerError { + /** The result document */ + result: Document; + + /** + * **Do not use this constructor!** + * + * Meant for internal use only. + * + * @remarks + * This class is only meant to be constructed within the driver. This constructor is + * not subject to semantic versioning compatibility guarantees and may change at any time. + * + * @public + **/ + constructor(result: WriteConcernErrorResult) { + super({ ...result.writeConcernError, ...result }); + this.errInfo = result.writeConcernError.errInfo; + this.result = result; + } + + override get name(): string { + return 'MongoWriteConcernError'; + } +} + +// https://github.com/mongodb/specifications/blob/master/source/retryable-reads/retryable-reads.md#retryable-error +const RETRYABLE_READ_ERROR_CODES = new Set([ + MONGODB_ERROR_CODES.HostUnreachable, + MONGODB_ERROR_CODES.HostNotFound, + MONGODB_ERROR_CODES.NetworkTimeout, + MONGODB_ERROR_CODES.ShutdownInProgress, + MONGODB_ERROR_CODES.PrimarySteppedDown, + MONGODB_ERROR_CODES.SocketException, + MONGODB_ERROR_CODES.NotWritablePrimary, + MONGODB_ERROR_CODES.InterruptedAtShutdown, + MONGODB_ERROR_CODES.InterruptedDueToReplStateChange, + MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk, + MONGODB_ERROR_CODES.NotPrimaryOrSecondary, + MONGODB_ERROR_CODES.ExceededTimeLimit, + MONGODB_ERROR_CODES.ReadConcernMajorityNotAvailableYet +]); + +// see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.md#terms +const RETRYABLE_WRITE_ERROR_CODES = RETRYABLE_READ_ERROR_CODES; + +export function needsRetryableWriteLabel( + error: Error, + maxWireVersion: number, + serverType: ServerType +): boolean { + // pre-4.4 server, then the driver adds an error label for every valid case + // execute operation will only inspect the label, code/message logic is handled here + if (error instanceof MongoNetworkError) { + return true; + } + + if (error instanceof MongoError) { + if ( + (maxWireVersion >= 9 || isRetryableWriteError(error)) && + !error.hasErrorLabel(MongoErrorLabel.HandshakeError) + ) { + // If we already have the error label no need to add it again. 4.4+ servers add the label. + // In the case where we have a handshake error, need to fall down to the logic checking + // the codes. + return false; + } + } + + if (error instanceof MongoWriteConcernError) { + if (serverType === 'Mongos' && maxWireVersion < 9) { + // use original top-level code from server response + return RETRYABLE_WRITE_ERROR_CODES.has(error.result.code ?? 0); + } + const code = error.result.writeConcernError.code ?? Number(error.code); + return RETRYABLE_WRITE_ERROR_CODES.has(Number.isNaN(code) ? 0 : code); + } + + if (error instanceof MongoError) { + return RETRYABLE_WRITE_ERROR_CODES.has(Number(error.code)); + } + + const isNotWritablePrimaryError = LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE.test(error.message); + if (isNotWritablePrimaryError) { + return true; + } + + const isNodeIsRecoveringError = NODE_IS_RECOVERING_ERROR_MESSAGE.test(error.message); + if (isNodeIsRecoveringError) { + return true; + } + + return false; +} + +export function isRetryableWriteError(error: MongoError): boolean { + return ( + error.hasErrorLabel(MongoErrorLabel.RetryableWriteError) || + error.hasErrorLabel(MongoErrorLabel.PoolRequestedRetry) + ); +} + +/** Determines whether an error is something the driver should attempt to retry */ +export function isRetryableReadError(error: MongoError): boolean { + const hasRetryableErrorCode = + typeof error.code === 'number' ? RETRYABLE_READ_ERROR_CODES.has(error.code) : false; + if (hasRetryableErrorCode) { + return true; + } + + if (error instanceof MongoNetworkError) { + return true; + } + + const isNotWritablePrimaryError = LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE.test(error.message); + if (isNotWritablePrimaryError) { + return true; + } + + const isNodeIsRecoveringError = NODE_IS_RECOVERING_ERROR_MESSAGE.test(error.message); + if (isNodeIsRecoveringError) { + return true; + } + + return false; +} + +const SDAM_RECOVERING_CODES = new Set([ + MONGODB_ERROR_CODES.ShutdownInProgress, + MONGODB_ERROR_CODES.PrimarySteppedDown, + MONGODB_ERROR_CODES.InterruptedAtShutdown, + MONGODB_ERROR_CODES.InterruptedDueToReplStateChange, + MONGODB_ERROR_CODES.NotPrimaryOrSecondary +]); + +const SDAM_NOT_PRIMARY_CODES = new Set([ + MONGODB_ERROR_CODES.NotWritablePrimary, + MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk, + MONGODB_ERROR_CODES.LegacyNotPrimary +]); + +const SDAM_NODE_SHUTTING_DOWN_ERROR_CODES = new Set([ + MONGODB_ERROR_CODES.InterruptedAtShutdown, + MONGODB_ERROR_CODES.ShutdownInProgress +]); + +function isRecoveringError(err: MongoError) { + if (typeof err.code === 'number') { + // If any error code exists, we ignore the error.message + return SDAM_RECOVERING_CODES.has(err.code); + } + + return ( + LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE.test(err.message) || + NODE_IS_RECOVERING_ERROR_MESSAGE.test(err.message) + ); +} + +function isNotWritablePrimaryError(err: MongoError) { + if (typeof err.code === 'number') { + // If any error code exists, we ignore the error.message + return SDAM_NOT_PRIMARY_CODES.has(err.code); + } + + if (isRecoveringError(err)) { + return false; + } + + return LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE.test(err.message); +} + +export function isNodeShuttingDownError(err: MongoError): boolean { + return !!(typeof err.code === 'number' && SDAM_NODE_SHUTTING_DOWN_ERROR_CODES.has(err.code)); +} + +/** + * Determines whether SDAM can recover from a given error. If it cannot + * then the pool will be cleared, and server state will completely reset + * locally. + * + * @see https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md#not-writable-primary-and-node-is-recovering + */ +export function isStateChangeError(error: MongoError): boolean { + return isRecoveringError(error) || isNotWritablePrimaryError(error); +} + +export function isNetworkTimeoutError(err: MongoError): err is MongoNetworkError { + return !!(err instanceof MongoNetworkError && err.message.match(/timed out/)); +} + +export function isResumableError(error?: Error, wireVersion?: number): boolean { + if (error == null || !(error instanceof MongoError)) { + return false; + } + + if (error instanceof MongoNetworkError) { + return true; + } + + if (error instanceof MongoServerSelectionError) { + return true; + } + + if (wireVersion != null && wireVersion >= 9) { + // DRIVERS-1308: For 4.4 drivers running against 4.4 servers, drivers will add a special case to treat the CursorNotFound error code as resumable + if (error.code === MONGODB_ERROR_CODES.CursorNotFound) { + return true; + } + return error.hasErrorLabel(MongoErrorLabel.ResumableChangeStreamError); + } + + if (typeof error.code === 'number') { + return GET_MORE_RESUMABLE_CODES.has(error.code); + } + + return false; +} diff --git a/gateway/node_modules/mongodb/src/explain.ts b/gateway/node_modules/mongodb/src/explain.ts new file mode 100644 index 0000000000000000000000000000000000000000..6100e1ae11a5c3bed5e594e6460fac4968e0ff4c --- /dev/null +++ b/gateway/node_modules/mongodb/src/explain.ts @@ -0,0 +1,124 @@ +import { type Document } from './bson'; +import { MongoAPIError } from './error'; + +/** @public */ +export const ExplainVerbosity = Object.freeze({ + queryPlanner: 'queryPlanner', + queryPlannerExtended: 'queryPlannerExtended', + executionStats: 'executionStats', + allPlansExecution: 'allPlansExecution' +} as const); + +/** @public */ +export type ExplainVerbosity = string; + +/** + * For backwards compatibility, true is interpreted as "allPlansExecution" + * and false as "queryPlanner". + * @public + */ +export type ExplainVerbosityLike = ExplainVerbosity | boolean; + +/** @public */ +export interface ExplainCommandOptions { + /** The explain verbosity for the command. */ + verbosity: ExplainVerbosity; + /** The maxTimeMS setting for the command. */ + maxTimeMS?: number; +} + +/** + * @public + * + * When set, this configures an explain command. Valid values are boolean (for legacy compatibility, + * see {@link ExplainVerbosityLike}), a string containing the explain verbosity, or an object containing the verbosity and + * an optional maxTimeMS. + * + * Examples of valid usage: + * + * ```typescript + * collection.find({ name: 'john doe' }, { explain: true }); + * collection.find({ name: 'john doe' }, { explain: false }); + * collection.find({ name: 'john doe' }, { explain: 'queryPlanner' }); + * collection.find({ name: 'john doe' }, { explain: { verbosity: 'queryPlanner' } }); + * ``` + * + * maxTimeMS can be configured to limit the amount of time the server + * spends executing an explain by providing an object: + * + * ```typescript + * // limits the `explain` command to no more than 2 seconds + * collection.find({ name: 'john doe' }, { + * explain: { + * verbosity: 'queryPlanner', + * maxTimeMS: 2000 + * } + * }); + * ``` + */ +export interface ExplainOptions { + /** Specifies the verbosity mode for the explain output. */ + explain?: ExplainVerbosityLike | ExplainCommandOptions; +} + +/** @internal */ +export class Explain { + readonly verbosity: ExplainVerbosity; + readonly maxTimeMS?: number; + + private constructor(verbosity: ExplainVerbosityLike, maxTimeMS?: number) { + if (typeof verbosity === 'boolean') { + this.verbosity = verbosity + ? ExplainVerbosity.allPlansExecution + : ExplainVerbosity.queryPlanner; + } else { + this.verbosity = verbosity; + } + + this.maxTimeMS = maxTimeMS; + } + + static fromOptions({ explain }: ExplainOptions = {}): Explain | undefined { + if (explain == null) return; + + if (typeof explain === 'boolean' || typeof explain === 'string') { + return new Explain(explain); + } + + const { verbosity, maxTimeMS } = explain; + return new Explain(verbosity, maxTimeMS); + } +} + +export function validateExplainTimeoutOptions(options: Document, explain?: Explain) { + const { maxTimeMS, timeoutMS } = options; + if (timeoutMS != null && (maxTimeMS != null || explain?.maxTimeMS != null)) { + throw new MongoAPIError('Cannot use maxTimeMS with timeoutMS for explain commands.'); + } +} + +/** + * Applies an explain to a given command. + * @internal + * + * @param command - the command on which to apply the explain + * @param options - the options containing the explain verbosity + */ +export function decorateWithExplain( + command: Document, + explain: Explain +): { + explain: Document; + verbosity: ExplainVerbosity; + maxTimeMS?: number; +} { + type ExplainCommand = ReturnType; + const { verbosity, maxTimeMS } = explain; + const baseCommand: ExplainCommand = { explain: command, verbosity }; + + if (typeof maxTimeMS === 'number') { + baseCommand.maxTimeMS = maxTimeMS; + } + + return baseCommand; +} diff --git a/gateway/node_modules/mongodb/src/gridfs/download.ts b/gateway/node_modules/mongodb/src/gridfs/download.ts new file mode 100644 index 0000000000000000000000000000000000000000..fb6691cb8cd6774640051bd9ca90fab8b171ea63 --- /dev/null +++ b/gateway/node_modules/mongodb/src/gridfs/download.ts @@ -0,0 +1,483 @@ +import { Readable } from 'stream'; + +import { ByteUtils, type Document, type ObjectId } from '../bson'; +import type { Collection } from '../collection'; +import { CursorTimeoutMode } from '../cursor/abstract_cursor'; +import type { FindCursor } from '../cursor/find_cursor'; +import { + MongoGridFSChunkError, + MongoGridFSStreamError, + MongoInvalidArgumentError, + MongoRuntimeError +} from '../error'; +import type { FindOptions } from '../operations/find'; +import type { ReadPreference } from '../read_preference'; +import type { Sort } from '../sort'; +import { CSOTTimeoutContext } from '../timeout'; +import type { Callback } from '../utils'; +import type { GridFSChunk } from './upload'; + +/** @public */ +export interface GridFSBucketReadStreamOptions { + sort?: Sort; + skip?: number; + /** + * 0-indexed non-negative byte offset from the beginning of the file + */ + start?: number; + /** + * 0-indexed non-negative byte offset to the end of the file contents + * to be returned by the stream. `end` is non-inclusive + */ + end?: number; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; +} + +/** @public */ +export interface GridFSBucketReadStreamOptionsWithRevision extends GridFSBucketReadStreamOptions { + /** The revision number relative to the oldest file with the given filename. 0 + * gets you the oldest file, 1 gets you the 2nd oldest, -1 gets you the + * newest. */ + revision?: number; +} + +/** @public */ +export interface GridFSFile { + _id: ObjectId; + length: number; + chunkSize: number; + filename: string; + metadata?: Document; + uploadDate: Date; +} + +/** @internal */ +export interface GridFSBucketReadStreamPrivate { + /** + * The running total number of bytes read from the chunks collection. + */ + bytesRead: number; + /** + * The number of bytes to remove from the last chunk read in the file. This is non-zero + * if `end` is not equal to the length of the document and `end` is not a multiple + * of the chunkSize. + */ + bytesToTrim: number; + + /** + * The number of bytes to remove from the first chunk read in the file. This is non-zero + * if `start` is not equal to the 0 and `start` is not a multiple + * of the chunkSize. + */ + bytesToSkip: number; + + files: Collection; + chunks: Collection; + cursor?: FindCursor; + + /** The running total number of chunks read from the chunks collection. */ + expected: number; + + /** + * The filter used to search in the _files_ collection (i.e., `{ _id: <> }`) + * This is not the same filter used when reading chunks from the chunks collection. + */ + filter: Document; + + /** Indicates whether or not download has started. */ + init: boolean; + + /** The expected number of chunks to read, calculated from start, end, chunkSize and file length. */ + expectedEnd: number; + file?: GridFSFile; + options: { + sort?: Sort; + skip?: number; + start: number; + end: number; + timeoutMS?: number; + }; + readPreference?: ReadPreference; + timeoutContext?: CSOTTimeoutContext; +} + +/** + * A readable stream that enables you to read buffers from GridFS. + * + * Do not instantiate this class directly. Use `openDownloadStream()` instead. + * @public + */ +export class GridFSBucketReadStream extends Readable { + /** @internal */ + s: GridFSBucketReadStreamPrivate; + + /** + * Fires when the stream loaded the file document corresponding to the provided id. + * @event + */ + static readonly FILE = 'file' as const; + + /** + * @param chunks - Handle for chunks collection + * @param files - Handle for files collection + * @param readPreference - The read preference to use + * @param filter - The filter to use to find the file document + * @internal + */ + constructor( + chunks: Collection, + files: Collection, + readPreference: ReadPreference | undefined, + filter: Document, + options?: GridFSBucketReadStreamOptions + ) { + super({ emitClose: true }); + this.s = { + bytesToTrim: 0, + bytesToSkip: 0, + bytesRead: 0, + chunks, + expected: 0, + files, + filter, + init: false, + expectedEnd: 0, + options: { + start: 0, + end: 0, + ...options + }, + readPreference, + timeoutContext: + options?.timeoutMS != null + ? new CSOTTimeoutContext({ timeoutMS: options.timeoutMS, serverSelectionTimeoutMS: 0 }) + : undefined + }; + } + + /** + * Reads from the cursor and pushes to the stream. + * Private Impl, do not call directly + * @internal + */ + override _read(): void { + if (this.destroyed) return; + waitForFile(this, () => doRead(this)); + } + + /** + * Sets the 0-based offset in bytes to start streaming from. Throws + * an error if this stream has entered flowing mode + * (e.g. if you've already called `on('data')`) + * + * @param start - 0-based offset in bytes to start streaming from + */ + start(start = 0): this { + throwIfInitialized(this); + this.s.options.start = start; + return this; + } + + /** + * Sets the 0-based offset in bytes to start streaming from. Throws + * an error if this stream has entered flowing mode + * (e.g. if you've already called `on('data')`) + * + * @param end - Offset in bytes to stop reading at + */ + end(end = 0): this { + throwIfInitialized(this); + this.s.options.end = end; + return this; + } + + /** + * Marks this stream as aborted (will never push another `data` event) + * and kills the underlying cursor. Will emit the 'end' event, and then + * the 'close' event once the cursor is successfully killed. + */ + async abort(): Promise { + this.push(null); + this.destroy(); + const remainingTimeMS = this.s.timeoutContext?.getRemainingTimeMSOrThrow(); + await this.s.cursor?.close({ timeoutMS: remainingTimeMS }); + } +} + +function throwIfInitialized(stream: GridFSBucketReadStream): void { + if (stream.s.init) { + throw new MongoGridFSStreamError('Options cannot be changed after the stream is initialized'); + } +} + +function doRead(stream: GridFSBucketReadStream): void { + if (stream.destroyed) return; + if (!stream.s.cursor) return; + if (!stream.s.file) return; + + const handleReadResult = (doc: Document | null) => { + if (stream.destroyed) return; + + if (!doc) { + stream.push(null); + + stream.s.cursor?.close().then(undefined, error => stream.destroy(error)); + return; + } + + if (!stream.s.file) return; + + const bytesRemaining = stream.s.file.length - stream.s.bytesRead; + const expectedN = stream.s.expected++; + const expectedLength = Math.min(stream.s.file.chunkSize, bytesRemaining); + if (doc.n > expectedN) { + return stream.destroy( + new MongoGridFSChunkError( + `ChunkIsMissing: Got unexpected n: ${doc.n}, expected: ${expectedN}` + ) + ); + } + + if (doc.n < expectedN) { + return stream.destroy( + new MongoGridFSChunkError(`ExtraChunk: Got unexpected n: ${doc.n}, expected: ${expectedN}`) + ); + } + + let buf = ByteUtils.isUint8Array(doc.data) ? doc.data : doc.data.buffer; + + if (buf.byteLength !== expectedLength) { + if (bytesRemaining <= 0) { + return stream.destroy( + new MongoGridFSChunkError( + `ExtraChunk: Got unexpected n: ${doc.n}, expected file length ${stream.s.file.length} bytes but already read ${stream.s.bytesRead} bytes` + ) + ); + } + + return stream.destroy( + new MongoGridFSChunkError( + `ChunkIsWrongSize: Got unexpected length: ${buf.byteLength}, expected: ${expectedLength}` + ) + ); + } + + stream.s.bytesRead += buf.byteLength; + + if (buf.byteLength === 0) { + return stream.push(null); + } + + let sliceStart = null; + let sliceEnd = null; + + if (stream.s.bytesToSkip != null) { + sliceStart = stream.s.bytesToSkip; + stream.s.bytesToSkip = 0; + } + + const atEndOfStream = expectedN === stream.s.expectedEnd - 1; + const bytesLeftToRead = stream.s.options.end - stream.s.bytesToSkip; + if (atEndOfStream && stream.s.bytesToTrim != null) { + sliceEnd = stream.s.file.chunkSize - stream.s.bytesToTrim; + } else if (stream.s.options.end && bytesLeftToRead < doc.data.byteLength) { + sliceEnd = bytesLeftToRead; + } + + if (sliceStart != null || sliceEnd != null) { + buf = buf.slice(sliceStart || 0, sliceEnd || buf.byteLength); + } + + stream.push(buf); + return; + }; + + stream.s.cursor.next().then(handleReadResult, error => { + if (stream.destroyed) return; + stream.destroy(error); + }); +} + +function init(stream: GridFSBucketReadStream): void { + const findOneOptions: FindOptions = {}; + if (stream.s.readPreference) { + findOneOptions.readPreference = stream.s.readPreference; + } + if (stream.s.options && stream.s.options.sort) { + findOneOptions.sort = stream.s.options.sort; + } + if (stream.s.options && stream.s.options.skip) { + findOneOptions.skip = stream.s.options.skip; + } + + const handleReadResult = (doc: Document | null) => { + if (stream.destroyed) return; + + if (!doc) { + const identifier = stream.s.filter._id + ? stream.s.filter._id.toString() + : stream.s.filter.filename; + const errmsg = `FileNotFound: file ${identifier} was not found`; + // TODO(NODE-3483) + const err = new MongoRuntimeError(errmsg); + err.code = 'ENOENT'; // TODO: NODE-3338 set property as part of constructor + return stream.destroy(err); + } + + // If document is empty, kill the stream immediately and don't + // execute any reads + if (doc.length <= 0) { + stream.push(null); + return; + } + + if (stream.destroyed) { + // If user destroys the stream before we have a cursor, wait + // until the query is done to say we're 'closed' because we can't + // cancel a query. + stream.destroy(); + return; + } + + try { + stream.s.bytesToSkip = handleStartOption(stream, doc, stream.s.options); + } catch (error) { + return stream.destroy(error); + } + + const filter: Document = { files_id: doc._id }; + + // Currently (MongoDB 3.4.4) skip function does not support the index, + // it needs to retrieve all the documents first and then skip them. (CS-25811) + // As work around we use $gte on the "n" field. + if (stream.s.options && stream.s.options.start != null) { + const skip = Math.floor(stream.s.options.start / doc.chunkSize); + if (skip > 0) { + filter['n'] = { $gte: skip }; + } + } + + let remainingTimeMS: number | undefined; + try { + remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow( + `Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms` + ); + } catch (error) { + return stream.destroy(error); + } + + stream.s.cursor = stream.s.chunks + .find(filter, { + timeoutMode: stream.s.options.timeoutMS != null ? CursorTimeoutMode.LIFETIME : undefined, + timeoutMS: remainingTimeMS + }) + .sort({ n: 1 }); + + if (stream.s.readPreference) { + stream.s.cursor.withReadPreference(stream.s.readPreference); + } + + stream.s.expectedEnd = Math.ceil(doc.length / doc.chunkSize); + stream.s.file = doc as GridFSFile; + + try { + stream.s.bytesToTrim = handleEndOption(stream, doc, stream.s.cursor, stream.s.options); + } catch (error) { + return stream.destroy(error); + } + + stream.emit(GridFSBucketReadStream.FILE, doc); + return; + }; + + let remainingTimeMS: number | undefined; + try { + remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow( + `Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms` + ); + } catch (error) { + if (!stream.destroyed) stream.destroy(error); + return; + } + + findOneOptions.timeoutMS = remainingTimeMS; + + stream.s.files.findOne(stream.s.filter, findOneOptions).then(handleReadResult, error => { + if (stream.destroyed) return; + stream.destroy(error); + }); +} + +function waitForFile(stream: GridFSBucketReadStream, callback: Callback): void { + if (stream.s.file) { + return callback(); + } + + if (!stream.s.init) { + init(stream); + stream.s.init = true; + } + + stream.once('file', () => { + callback(); + }); +} + +function handleStartOption( + stream: GridFSBucketReadStream, + doc: Document, + options: GridFSBucketReadStreamOptions +): number { + if (options && options.start != null) { + if (options.start > doc.length) { + throw new MongoInvalidArgumentError( + `Stream start (${options.start}) must not be more than the length of the file (${doc.length})` + ); + } + if (options.start < 0) { + throw new MongoInvalidArgumentError(`Stream start (${options.start}) must not be negative`); + } + if (options.end != null && options.end < options.start) { + throw new MongoInvalidArgumentError( + `Stream start (${options.start}) must not be greater than stream end (${options.end})` + ); + } + + stream.s.bytesRead = Math.floor(options.start / doc.chunkSize) * doc.chunkSize; + stream.s.expected = Math.floor(options.start / doc.chunkSize); + + return options.start - stream.s.bytesRead; + } + throw new MongoInvalidArgumentError('Start option must be defined'); +} + +function handleEndOption( + stream: GridFSBucketReadStream, + doc: Document, + cursor: FindCursor, + options: GridFSBucketReadStreamOptions +) { + if (options && options.end != null) { + if (options.end > doc.length) { + throw new MongoInvalidArgumentError( + `Stream end (${options.end}) must not be more than the length of the file (${doc.length})` + ); + } + if (options.start == null || options.start < 0) { + throw new MongoInvalidArgumentError(`Stream end (${options.end}) must not be negative`); + } + + const start = options.start != null ? Math.floor(options.start / doc.chunkSize) : 0; + + cursor.limit(Math.ceil(options.end / doc.chunkSize) - start); + + stream.s.expectedEnd = Math.ceil(options.end / doc.chunkSize); + + return Math.ceil(options.end / doc.chunkSize) * doc.chunkSize - options.end; + } + throw new MongoInvalidArgumentError('End option must be defined'); +} diff --git a/gateway/node_modules/mongodb/src/gridfs/index.ts b/gateway/node_modules/mongodb/src/gridfs/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..f62267b0a990e0fce6ef42b9cab145af87a2a68e --- /dev/null +++ b/gateway/node_modules/mongodb/src/gridfs/index.ts @@ -0,0 +1,264 @@ +import type { ObjectId } from '../bson'; +import type { Collection } from '../collection'; +import type { FindCursor } from '../cursor/find_cursor'; +import type { Db } from '../db'; +import { MongoOperationTimeoutError, MongoRuntimeError } from '../error'; +import { type Filter, TypedEventEmitter } from '../mongo_types'; +import type { ReadPreference } from '../read_preference'; +import type { Sort } from '../sort'; +import { CSOTTimeoutContext } from '../timeout'; +import { noop, resolveOptions } from '../utils'; +import { WriteConcern, type WriteConcernOptions } from '../write_concern'; +import type { FindOptions } from './../operations/find'; +import { + GridFSBucketReadStream, + type GridFSBucketReadStreamOptions, + type GridFSBucketReadStreamOptionsWithRevision, + type GridFSFile +} from './download'; +import { + GridFSBucketWriteStream, + type GridFSBucketWriteStreamOptions, + type GridFSChunk +} from './upload'; + +const DEFAULT_GRIDFS_BUCKET_OPTIONS: { + bucketName: string; + chunkSizeBytes: number; +} = { + bucketName: 'fs', + chunkSizeBytes: 255 * 1024 +}; + +/** @public */ +export interface GridFSBucketOptions extends WriteConcernOptions { + /** The 'files' and 'chunks' collections will be prefixed with the bucket name followed by a dot. */ + bucketName?: string; + /** Number of bytes stored in each chunk. Defaults to 255KB */ + chunkSizeBytes?: number; + /** Read preference to be passed to read operations */ + readPreference?: ReadPreference; + /** + * @experimental + * Specifies the lifetime duration of a gridFS stream. If any async operations are in progress + * when this timeout expires, the stream will throw a timeout error. + */ + timeoutMS?: number; +} + +/** @internal */ +export interface GridFSBucketPrivate { + db: Db; + options: { + bucketName: string; + chunkSizeBytes: number; + readPreference?: ReadPreference; + writeConcern: WriteConcern | undefined; + timeoutMS?: number; + }; + _chunksCollection: Collection; + _filesCollection: Collection; + checkedIndexes: boolean; + calledOpenUploadStream: boolean; +} + +/** @public */ +export type GridFSBucketEvents = { + index(): void; +}; + +/** + * Constructor for a streaming GridFS interface + * @public + */ +export class GridFSBucket extends TypedEventEmitter { + /** @internal */ + s: GridFSBucketPrivate; + + /** + * When the first call to openUploadStream is made, the upload stream will + * check to see if it needs to create the proper indexes on the chunks and + * files collections. This event is fired either when 1) it determines that + * no index creation is necessary, 2) when it successfully creates the + * necessary indexes. + * @event + */ + static readonly INDEX = 'index' as const; + + constructor(db: Db, options?: GridFSBucketOptions) { + super(); + this.on('error', noop); + this.setMaxListeners(0); + const privateOptions = resolveOptions(db, { + ...DEFAULT_GRIDFS_BUCKET_OPTIONS, + ...options, + writeConcern: WriteConcern.fromOptions(options) + }); + this.s = { + db, + options: privateOptions, + _chunksCollection: db.collection(privateOptions.bucketName + '.chunks'), + _filesCollection: db.collection(privateOptions.bucketName + '.files'), + checkedIndexes: false, + calledOpenUploadStream: false + }; + } + + /** + * Returns a writable stream (GridFSBucketWriteStream) for writing + * buffers to GridFS. The stream's 'id' property contains the resulting + * file's id. + * + * @param filename - The value of the 'filename' key in the files doc + * @param options - Optional settings. + */ + + openUploadStream( + filename: string, + options?: GridFSBucketWriteStreamOptions + ): GridFSBucketWriteStream { + return new GridFSBucketWriteStream(this, filename, { + timeoutMS: this.s.options.timeoutMS, + ...options + }); + } + + /** + * Returns a writable stream (GridFSBucketWriteStream) for writing + * buffers to GridFS for a custom file id. The stream's 'id' property contains the resulting + * file's id. + */ + openUploadStreamWithId( + id: ObjectId, + filename: string, + options?: GridFSBucketWriteStreamOptions + ): GridFSBucketWriteStream { + return new GridFSBucketWriteStream(this, filename, { + timeoutMS: this.s.options.timeoutMS, + ...options, + id + }); + } + + /** Returns a readable stream (GridFSBucketReadStream) for streaming file data from GridFS. */ + openDownloadStream( + id: ObjectId, + options?: GridFSBucketReadStreamOptions + ): GridFSBucketReadStream { + return new GridFSBucketReadStream( + this.s._chunksCollection, + this.s._filesCollection, + this.s.options.readPreference, + { _id: id }, + { timeoutMS: this.s.options.timeoutMS, ...options } + ); + } + + /** + * Deletes a file with the given id + * + * @param id - The id of the file doc + */ + async delete(id: ObjectId, options?: { timeoutMS: number }): Promise { + const { timeoutMS } = resolveOptions(this.s.db, options); + let timeoutContext: CSOTTimeoutContext | undefined = undefined; + + if (timeoutMS) { + timeoutContext = new CSOTTimeoutContext({ + timeoutMS, + serverSelectionTimeoutMS: this.s.db.client.s.options.serverSelectionTimeoutMS + }); + } + + const { deletedCount } = await this.s._filesCollection.deleteOne( + { _id: id }, + { timeoutMS: timeoutContext?.remainingTimeMS } + ); + + const remainingTimeMS = timeoutContext?.remainingTimeMS; + if (remainingTimeMS != null && remainingTimeMS <= 0) + throw new MongoOperationTimeoutError(`Timed out after ${timeoutMS}ms`); + // Delete orphaned chunks before returning FileNotFound + await this.s._chunksCollection.deleteMany({ files_id: id }, { timeoutMS: remainingTimeMS }); + + if (deletedCount === 0) { + // TODO(NODE-3483): Replace with more appropriate error + // Consider creating new error MongoGridFSFileNotFoundError + throw new MongoRuntimeError(`File not found for id ${id}`); + } + } + + /** Convenience wrapper around find on the files collection */ + find(filter: Filter = {}, options: FindOptions = {}): FindCursor { + return this.s._filesCollection.find(filter, options); + } + + /** + * Returns a readable stream (GridFSBucketReadStream) for streaming the + * file with the given name from GridFS. If there are multiple files with + * the same name, this will stream the most recent file with the given name + * (as determined by the `uploadDate` field). You can set the `revision` + * option to change this behavior. + */ + openDownloadStreamByName( + filename: string, + options?: GridFSBucketReadStreamOptionsWithRevision + ): GridFSBucketReadStream { + let sort: Sort = { uploadDate: -1 }; + let skip = undefined; + if (options && options.revision != null) { + if (options.revision >= 0) { + sort = { uploadDate: 1 }; + skip = options.revision; + } else { + skip = -options.revision - 1; + } + } + return new GridFSBucketReadStream( + this.s._chunksCollection, + this.s._filesCollection, + this.s.options.readPreference, + { filename }, + { timeoutMS: this.s.options.timeoutMS, ...options, sort, skip } + ); + } + + /** + * Renames the file with the given _id to the given string + * + * @param id - the id of the file to rename + * @param filename - new name for the file + */ + async rename(id: ObjectId, filename: string, options?: { timeoutMS: number }): Promise { + const filter = { _id: id }; + const update = { $set: { filename } }; + const { matchedCount } = await this.s._filesCollection.updateOne(filter, update, options); + if (matchedCount === 0) { + throw new MongoRuntimeError(`File with id ${id} not found`); + } + } + + /** Removes this bucket's files collection, followed by its chunks collection. */ + async drop(options?: { timeoutMS: number }): Promise { + const { timeoutMS } = resolveOptions(this.s.db, options); + let timeoutContext: CSOTTimeoutContext | undefined = undefined; + + if (timeoutMS) { + timeoutContext = new CSOTTimeoutContext({ + timeoutMS, + serverSelectionTimeoutMS: this.s.db.client.s.options.serverSelectionTimeoutMS + }); + } + + if (timeoutContext) { + await this.s._filesCollection.drop({ timeoutMS: timeoutContext.remainingTimeMS }); + const remainingTimeMS = timeoutContext.getRemainingTimeMSOrThrow( + `Timed out after ${timeoutMS}ms` + ); + await this.s._chunksCollection.drop({ timeoutMS: remainingTimeMS }); + } else { + await this.s._filesCollection.drop(); + await this.s._chunksCollection.drop(); + } + } +} diff --git a/gateway/node_modules/mongodb/src/gridfs/upload.ts b/gateway/node_modules/mongodb/src/gridfs/upload.ts new file mode 100644 index 0000000000000000000000000000000000000000..13359cad4fb08a7465ee04dffd6ba82200b7118c --- /dev/null +++ b/gateway/node_modules/mongodb/src/gridfs/upload.ts @@ -0,0 +1,538 @@ +import { Writable } from 'stream'; + +import { ByteUtils, type Document, ObjectId } from '../bson'; +import type { Collection } from '../collection'; +import { CursorTimeoutMode } from '../cursor/abstract_cursor'; +import { + MongoAPIError, + MONGODB_ERROR_CODES, + MongoError, + MongoOperationTimeoutError +} from '../error'; +import { CSOTTimeoutContext } from '../timeout'; +import { type Callback, resolveTimeoutOptions, squashError } from '../utils'; +import type { WriteConcernOptions } from '../write_concern'; +import { WriteConcern } from './../write_concern'; +import type { GridFSFile } from './download'; +import type { GridFSBucket } from './index'; + +/** @public */ +export interface GridFSChunk { + _id: ObjectId; + files_id: ObjectId; + n: number; + data: Buffer | Uint8Array; +} + +/** @public */ +export interface GridFSBucketWriteStreamOptions extends WriteConcernOptions { + /** Overwrite this bucket's chunkSizeBytes for this file */ + chunkSizeBytes?: number; + /** Custom file id for the GridFS file. */ + id?: ObjectId; + /** Object to store in the file document's `metadata` field */ + metadata?: Document; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; +} + +/** + * A writable stream that enables you to write buffers to GridFS. + * + * Do not instantiate this class directly. Use `openUploadStream()` instead. + * @public + */ +export class GridFSBucketWriteStream extends Writable { + bucket: GridFSBucket; + /** A Collection instance where the file's chunks are stored */ + chunks: Collection; + /** A Collection instance where the file's GridFSFile document is stored */ + files: Collection; + /** The name of the file */ + filename: string; + /** Options controlling the metadata inserted along with the file */ + options: GridFSBucketWriteStreamOptions; + /** Indicates the stream is finished uploading */ + done: boolean; + /** The ObjectId used for the `_id` field on the GridFSFile document */ + id: ObjectId; + /** The number of bytes that each chunk will be limited to */ + chunkSizeBytes: number; + /** Space used to store a chunk currently being inserted */ + bufToStore: Uint8Array; + /** Accumulates the number of bytes inserted as the stream uploads chunks */ + length: number; + /** Accumulates the number of chunks inserted as the stream uploads file contents */ + n: number; + /** Tracks the current offset into the buffered bytes being uploaded */ + pos: number; + /** Contains a number of properties indicating the current state of the stream */ + state: { + /** If set the stream has ended */ + streamEnd: boolean; + /** Indicates the number of chunks that still need to be inserted to exhaust the current buffered data */ + outstandingRequests: number; + /** If set an error occurred during insertion */ + errored: boolean; + /** If set the stream was intentionally aborted */ + aborted: boolean; + }; + /** The write concern setting to be used with every insert operation */ + writeConcern?: WriteConcern; + /** + * The document containing information about the inserted file. + * This property is defined _after_ the finish event has been emitted. + * It will remain `null` if an error occurs. + * + * @example + * ```ts + * fs.createReadStream('file.txt') + * .pipe(bucket.openUploadStream('file.txt')) + * .on('finish', function () { + * console.log(this.gridFSFile) + * }) + * ``` + */ + gridFSFile: GridFSFile | null = null; + /** @internal */ + timeoutContext?: CSOTTimeoutContext; + + /** + * @param bucket - Handle for this stream's corresponding bucket + * @param filename - The value of the 'filename' key in the files doc + * @param options - Optional settings. + * @internal + */ + constructor(bucket: GridFSBucket, filename: string, options?: GridFSBucketWriteStreamOptions) { + super(); + + options = options ?? {}; + this.bucket = bucket; + this.chunks = bucket.s._chunksCollection; + this.filename = filename; + this.files = bucket.s._filesCollection; + this.options = options; + this.writeConcern = WriteConcern.fromOptions(options) || bucket.s.options.writeConcern; + // Signals the write is all done + this.done = false; + + this.id = options.id ? options.id : new ObjectId(); + // properly inherit the default chunksize from parent + this.chunkSizeBytes = options.chunkSizeBytes || this.bucket.s.options.chunkSizeBytes; + this.bufToStore = ByteUtils.allocate(this.chunkSizeBytes); + this.length = 0; + this.n = 0; + this.pos = 0; + this.state = { + streamEnd: false, + outstandingRequests: 0, + errored: false, + aborted: false + }; + + if (options.timeoutMS != null) + this.timeoutContext = new CSOTTimeoutContext({ + timeoutMS: options.timeoutMS, + serverSelectionTimeoutMS: resolveTimeoutOptions(this.bucket.s.db.client, {}) + .serverSelectionTimeoutMS + }); + } + + /** + * @internal + * + * The stream is considered constructed when the indexes are done being created + */ + override _construct(callback: (error?: Error | null) => void): void { + if (!this.bucket.s.calledOpenUploadStream) { + this.bucket.s.calledOpenUploadStream = true; + + checkIndexes(this).then( + () => { + this.bucket.s.checkedIndexes = true; + this.bucket.emit('index'); + callback(); + }, + error => { + if (error instanceof MongoOperationTimeoutError) { + return handleError(this, error, callback); + } + squashError(error); + callback(); + } + ); + } else { + return queueMicrotask(callback); + } + } + + /** + * @internal + * Write a buffer to the stream. + * + * @param chunk - Buffer to write + * @param encoding - Optional encoding for the buffer + * @param callback - Function to call when the chunk was added to the buffer, or if the entire chunk was persisted to MongoDB if this chunk caused a flush. + */ + override _write( + chunk: Uint8Array | string, + encoding: BufferEncoding, + callback: Callback + ): void { + doWrite(this, chunk, encoding, callback); + } + + /** @internal */ + override _final(callback: (error?: Error | null) => void): void { + if (this.state.streamEnd) { + return queueMicrotask(callback); + } + this.state.streamEnd = true; + writeRemnant(this, callback); + } + + /** + * Places this write stream into an aborted state (all future writes fail) + * and deletes all chunks that have already been written. + */ + async abort(): Promise { + if (this.state.streamEnd) { + // TODO(NODE-3485): Replace with MongoGridFSStreamClosed + throw new MongoAPIError('Cannot abort a stream that has already completed'); + } + + if (this.state.aborted) { + // TODO(NODE-3485): Replace with MongoGridFSStreamClosed + throw new MongoAPIError('Cannot call abort() on a stream twice'); + } + + this.state.aborted = true; + const remainingTimeMS = this.timeoutContext?.getRemainingTimeMSOrThrow( + `Upload timed out after ${this.timeoutContext?.timeoutMS}ms` + ); + + await this.chunks.deleteMany({ files_id: this.id }, { timeoutMS: remainingTimeMS }); + } +} + +function handleError(stream: GridFSBucketWriteStream, error: Error, callback: Callback): void { + if (stream.state.errored) { + queueMicrotask(callback); + return; + } + stream.state.errored = true; + queueMicrotask(() => callback(error)); +} + +function createChunkDoc(filesId: ObjectId, n: number, data: Uint8Array): GridFSChunk { + return { + _id: new ObjectId(), + files_id: filesId, + n, + data + }; +} + +async function checkChunksIndex(stream: GridFSBucketWriteStream): Promise { + const index = { files_id: 1, n: 1 }; + + let remainingTimeMS; + remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow( + `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` + ); + + let indexes; + try { + indexes = await stream.chunks + .listIndexes({ + timeoutMode: remainingTimeMS != null ? CursorTimeoutMode.LIFETIME : undefined, + timeoutMS: remainingTimeMS + }) + .toArray(); + } catch (error) { + if (error instanceof MongoError && error.code === MONGODB_ERROR_CODES.NamespaceNotFound) { + indexes = []; + } else { + throw error; + } + } + + const hasChunksIndex = !!indexes.find(index => { + const keys = Object.keys(index.key); + if (keys.length === 2 && index.key.files_id === 1 && index.key.n === 1) { + return true; + } + return false; + }); + + if (!hasChunksIndex) { + remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow( + `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` + ); + await stream.chunks.createIndex(index, { + ...stream.writeConcern, + background: true, + unique: true, + timeoutMS: remainingTimeMS + }); + } +} + +function checkDone(stream: GridFSBucketWriteStream, callback: Callback): void { + if (stream.done) { + return queueMicrotask(callback); + } + + if (stream.state.streamEnd && stream.state.outstandingRequests === 0 && !stream.state.errored) { + // Set done so we do not trigger duplicate createFilesDoc + stream.done = true; + // Create a new files doc + const gridFSFile = createFilesDoc( + stream.id, + stream.length, + stream.chunkSizeBytes, + stream.filename, + stream.options.metadata + ); + + if (isAborted(stream, callback)) { + return; + } + + const remainingTimeMS = stream.timeoutContext?.remainingTimeMS; + if (remainingTimeMS != null && remainingTimeMS <= 0) { + return handleError( + stream, + new MongoOperationTimeoutError( + `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` + ), + callback + ); + } + + stream.files + .insertOne(gridFSFile, { writeConcern: stream.writeConcern, timeoutMS: remainingTimeMS }) + .then( + () => { + stream.gridFSFile = gridFSFile; + callback(); + }, + error => { + return handleError(stream, error, callback); + } + ); + return; + } + + queueMicrotask(callback); +} + +async function checkIndexes(stream: GridFSBucketWriteStream): Promise { + let remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow( + `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` + ); + const doc = await stream.files.findOne( + {}, + { + projection: { _id: 1 }, + timeoutMS: remainingTimeMS + } + ); + if (doc != null) { + // If at least one document exists assume the collection has the required index + return; + } + + const index = { filename: 1, uploadDate: 1 }; + + let indexes; + remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow( + `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` + ); + const listIndexesOptions = { + timeoutMode: remainingTimeMS != null ? CursorTimeoutMode.LIFETIME : undefined, + timeoutMS: remainingTimeMS + }; + try { + indexes = await stream.files.listIndexes(listIndexesOptions).toArray(); + } catch (error) { + if (error instanceof MongoError && error.code === MONGODB_ERROR_CODES.NamespaceNotFound) { + indexes = []; + } else { + throw error; + } + } + + const hasFileIndex = !!indexes.find(index => { + const keys = Object.keys(index.key); + if (keys.length === 2 && index.key.filename === 1 && index.key.uploadDate === 1) { + return true; + } + return false; + }); + + if (!hasFileIndex) { + remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow( + `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` + ); + + await stream.files.createIndex(index, { background: false, timeoutMS: remainingTimeMS }); + } + + await checkChunksIndex(stream); +} + +function createFilesDoc( + _id: ObjectId, + length: number, + chunkSize: number, + filename: string, + metadata?: Document +): GridFSFile { + const ret: GridFSFile = { + _id, + length, + chunkSize, + uploadDate: new Date(), + filename + }; + + if (metadata) { + ret.metadata = metadata; + } + + return ret; +} + +function doWrite( + stream: GridFSBucketWriteStream, + chunk: Uint8Array | string, + encoding: BufferEncoding, + callback: Callback +): void { + if (isAborted(stream, callback)) { + return; + } + + const inputBuf = + typeof chunk === 'string' ? ByteUtils.fromUTF8(chunk) : ByteUtils.toLocalBufferType(chunk); + + stream.length += inputBuf.length; + + // Input is small enough to fit in our buffer + if (stream.pos + inputBuf.length < stream.chunkSizeBytes) { + ByteUtils.copy(inputBuf, stream.bufToStore, stream.pos); + stream.pos += inputBuf.length; + queueMicrotask(callback); + return; + } + + // Otherwise, buffer is too big for current chunk, so we need to flush + // to MongoDB. + let inputBufRemaining = inputBuf.length; + let spaceRemaining: number = stream.chunkSizeBytes - stream.pos; + let numToCopy = Math.min(spaceRemaining, inputBuf.length); + let outstandingRequests = 0; + while (inputBufRemaining > 0) { + const inputBufPos = inputBuf.length - inputBufRemaining; + ByteUtils.copy(inputBuf, stream.bufToStore, stream.pos, inputBufPos, inputBufPos + numToCopy); + stream.pos += numToCopy; + spaceRemaining -= numToCopy; + let doc: GridFSChunk; + if (spaceRemaining === 0) { + doc = createChunkDoc(stream.id, stream.n, new Uint8Array(stream.bufToStore)); + + const remainingTimeMS = stream.timeoutContext?.remainingTimeMS; + if (remainingTimeMS != null && remainingTimeMS <= 0) { + return handleError( + stream, + new MongoOperationTimeoutError( + `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` + ), + callback + ); + } + + ++stream.state.outstandingRequests; + ++outstandingRequests; + + if (isAborted(stream, callback)) { + return; + } + + stream.chunks + .insertOne(doc, { writeConcern: stream.writeConcern, timeoutMS: remainingTimeMS }) + .then( + () => { + --stream.state.outstandingRequests; + --outstandingRequests; + + if (!outstandingRequests) { + checkDone(stream, callback); + } + }, + error => { + return handleError(stream, error, callback); + } + ); + + spaceRemaining = stream.chunkSizeBytes; + stream.pos = 0; + ++stream.n; + } + inputBufRemaining -= numToCopy; + numToCopy = Math.min(spaceRemaining, inputBufRemaining); + } +} + +function writeRemnant(stream: GridFSBucketWriteStream, callback: Callback): void { + // Buffer is empty, so don't bother to insert + if (stream.pos === 0) { + return checkDone(stream, callback); + } + + // Create a new buffer to make sure the buffer isn't bigger than it needs + // to be. + const remnant = ByteUtils.allocate(stream.pos); + ByteUtils.copy(stream.bufToStore, remnant, 0, 0, stream.pos); + const doc = createChunkDoc(stream.id, stream.n, remnant); + + // If the stream was aborted, do not write remnant + if (isAborted(stream, callback)) { + return; + } + + const remainingTimeMS = stream.timeoutContext?.remainingTimeMS; + if (remainingTimeMS != null && remainingTimeMS <= 0) { + return handleError( + stream, + new MongoOperationTimeoutError( + `Upload timed out after ${stream.timeoutContext?.timeoutMS}ms` + ), + callback + ); + } + ++stream.state.outstandingRequests; + stream.chunks + .insertOne(doc, { writeConcern: stream.writeConcern, timeoutMS: remainingTimeMS }) + .then( + () => { + --stream.state.outstandingRequests; + checkDone(stream, callback); + }, + error => { + return handleError(stream, error, callback); + } + ); +} + +function isAborted(stream: GridFSBucketWriteStream, callback: Callback): boolean { + if (stream.state.aborted) { + queueMicrotask(() => callback(new MongoAPIError('Stream has been aborted'))); + return true; + } + return false; +} diff --git a/gateway/node_modules/mongodb/src/index.ts b/gateway/node_modules/mongodb/src/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..542a0c1740661798a04c998888fadea2b049f940 --- /dev/null +++ b/gateway/node_modules/mongodb/src/index.ts @@ -0,0 +1,635 @@ +import { Admin } from './admin'; +import { OrderedBulkOperation } from './bulk/ordered'; +import { UnorderedBulkOperation } from './bulk/unordered'; +import { ChangeStream } from './change_stream'; +import { Collection } from './collection'; +import { AbstractCursor } from './cursor/abstract_cursor'; +import { AggregationCursor } from './cursor/aggregation_cursor'; +import { FindCursor } from './cursor/find_cursor'; +import { ListCollectionsCursor } from './cursor/list_collections_cursor'; +import { ListIndexesCursor } from './cursor/list_indexes_cursor'; +import type { RunCommandCursor } from './cursor/run_command_cursor'; +import { Db } from './db'; +import { GridFSBucket } from './gridfs'; +import { GridFSBucketReadStream } from './gridfs/download'; +import { GridFSBucketWriteStream } from './gridfs/upload'; +import { MongoClient } from './mongo_client'; +import { CancellationToken } from './mongo_types'; +import { ClientSession } from './sessions'; + +/** @public */ +export { BSON } from './bson'; +export { + Binary, + BSONRegExp, + BSONSymbol, + BSONType, + Code, + DBRef, + Decimal128, + Double, + Int32, + Long, + MaxKey, + MinKey, + ObjectId, + Timestamp, + UUID +} from './bson'; +export { + type AnyBulkWriteOperation, + type BulkWriteOptions, + MongoBulkWriteError +} from './bulk/common'; +export { ClientEncryption } from './client-side-encryption/client_encryption'; +export { ChangeStreamCursor } from './cursor/change_stream_cursor'; +export { ExplainableCursor } from './cursor/explainable_cursor'; +export { + MongoAPIError, + MongoAWSError, + MongoAzureError, + MongoBatchReExecutionError, + MongoChangeStreamError, + MongoClientBulkWriteCursorError, + MongoClientBulkWriteError, + MongoClientBulkWriteExecutionError, + MongoClientClosedError, + MongoCompatibilityError, + MongoCursorExhaustedError, + MongoCursorInUseError, + MongoDecompressionError, + MongoDriverError, + MongoError, + MongoExpiredSessionError, + MongoGCPError, + MongoGridFSChunkError, + MongoGridFSStreamError, + MongoInvalidArgumentError, + MongoKerberosError, + MongoMissingCredentialsError, + MongoMissingDependencyError, + MongoNetworkError, + MongoNetworkTimeoutError, + MongoNotConnectedError, + MongoOIDCError, + MongoOperationTimeoutError, + MongoParseError, + MongoRuntimeError, + MongoServerClosedError, + MongoServerError, + MongoServerSelectionError, + MongoStalePrimaryError, + MongoSystemError, + MongoTailableCursorError, + MongoTopologyClosedError, + MongoTransactionError, + MongoUnexpectedServerResponseError, + MongoWriteConcernError, + WriteConcernErrorResult +} from './error'; +export { + AbstractCursor, + // Actual driver classes exported + Admin, + AggregationCursor, + CancellationToken, + ChangeStream, + ClientSession, + Collection, + Db, + FindCursor, + GridFSBucket, + GridFSBucketReadStream, + GridFSBucketWriteStream, + ListCollectionsCursor, + ListIndexesCursor, + MongoClient, + OrderedBulkOperation, + RunCommandCursor, + UnorderedBulkOperation +}; + +// enums +export { BatchType } from './bulk/common'; +export { AutoEncryptionLoggerLevel } from './client-side-encryption/auto_encrypter'; +export { GSSAPICanonicalizationValue } from './cmap/auth/gssapi'; +export { AuthMechanism } from './cmap/auth/providers'; +export { Compressor } from './cmap/wire_protocol/compression'; +export { CURSOR_FLAGS, CursorTimeoutMode } from './cursor/abstract_cursor'; +export { MongoErrorLabel } from './error'; +export { ExplainVerbosity } from './explain'; +export { ServerApiVersion } from './mongo_client'; +export { MongoLoggableComponent, SeverityLevel } from './mongo_logger'; +export { ReturnDocument } from './operations/find_and_modify'; +export { ProfilingLevel } from './operations/set_profiling_level'; +export { ReadConcernLevel } from './read_concern'; +export { ReadPreferenceMode } from './read_preference'; +export { ServerType, TopologyType } from './sdam/common'; + +// Helper classes +export type { AWSCredentialProvider } from './cmap/auth/aws_temporary_credentials'; +export type { AWSCredentials } from './deps'; +export { ReadConcern } from './read_concern'; +export { ReadPreference } from './read_preference'; +export { WriteConcern } from './write_concern'; +// events +export { + CommandFailedEvent, + CommandStartedEvent, + CommandSucceededEvent +} from './cmap/command_monitoring_events'; +export { + ConnectionCheckedInEvent, + ConnectionCheckedOutEvent, + ConnectionCheckOutFailedEvent, + ConnectionCheckOutStartedEvent, + ConnectionClosedEvent, + ConnectionCreatedEvent, + ConnectionPoolClearedEvent, + ConnectionPoolClosedEvent, + ConnectionPoolCreatedEvent, + ConnectionPoolMonitoringEvent, + ConnectionPoolReadyEvent, + ConnectionReadyEvent +} from './cmap/connection_pool_events'; +export { + ServerClosedEvent, + ServerDescriptionChangedEvent, + ServerHeartbeatFailedEvent, + ServerHeartbeatStartedEvent, + ServerHeartbeatSucceededEvent, + ServerOpeningEvent, + TopologyClosedEvent, + TopologyDescriptionChangedEvent, + TopologyOpeningEvent +} from './sdam/events'; +export { + ServerSelectionEvent, + ServerSelectionFailedEvent, + ServerSelectionStartedEvent, + ServerSelectionSucceededEvent, + WaitingForSuitableServerEvent +} from './sdam/server_selection_events'; +export { SrvPollingEvent } from './sdam/srv_polling'; + +// type only exports below, these are removed from emitted JS +export type { AdminPrivate } from './admin'; +export type { BSONElement, BSONSerializeOptions, Document } from './bson'; +export type { deserialize, serialize } from './bson'; +export type { + BulkResult, + BulkWriteOperationError, + BulkWriteResult, + DeleteManyModel, + DeleteOneModel, + InsertOneModel, + ReplaceOneModel, + UpdateManyModel, + UpdateOneModel, + WriteConcernError, + WriteError +} from './bulk/common'; +export type { + Batch, + BulkOperationBase, + BulkOperationPrivate, + FindOperators, + WriteConcernErrorData +} from './bulk/common'; +export type { + ChangeStreamCollModDocument, + ChangeStreamCreateDocument, + ChangeStreamCreateIndexDocument, + ChangeStreamDeleteDocument, + ChangeStreamDocument, + ChangeStreamDocumentCollectionUUID, + ChangeStreamDocumentCommon, + ChangeStreamDocumentKey, + ChangeStreamDocumentOperationDescription, + ChangeStreamDocumentWallTime, + ChangeStreamDropDatabaseDocument, + ChangeStreamDropDocument, + ChangeStreamDropIndexDocument, + ChangeStreamEvents, + ChangeStreamInsertDocument, + ChangeStreamInvalidateDocument, + ChangeStreamNameSpace, + ChangeStreamOptions, + ChangeStreamRefineCollectionShardKeyDocument, + ChangeStreamRenameDocument, + ChangeStreamReplaceDocument, + ChangeStreamReshardCollectionDocument, + ChangeStreamShardCollectionDocument, + ChangeStreamSplitEvent, + ChangeStreamUpdateDocument, + OperationTime, + ResumeToken, + UpdateDescription +} from './change_stream'; +export type { AutoEncrypter } from './client-side-encryption/auto_encrypter'; +export type { AutoEncryptionOptions } from './client-side-encryption/auto_encrypter'; +export type { AutoEncryptionExtraOptions } from './client-side-encryption/auto_encrypter'; +export type { + AWSEncryptionKeyOptions, + AzureEncryptionKeyOptions, + ClientEncryptionCreateDataKeyProviderOptions, + ClientEncryptionEncryptOptions, + ClientEncryptionOptions, + ClientEncryptionRewrapManyDataKeyProviderOptions, + ClientEncryptionRewrapManyDataKeyResult, + DataKey, + GCPEncryptionKeyOptions, + KMIPEncryptionKeyOptions, + RangeOptions, + StringQueryOptions, + TextQueryOptions +} from './client-side-encryption/client_encryption'; +export { + MongoCryptAzureKMSRequestError, + MongoCryptCreateDataKeyError, + MongoCryptCreateEncryptedCollectionError, + MongoCryptError, + MongoCryptInvalidArgumentError, + MongoCryptKMSRequestNetworkTimeoutError +} from './client-side-encryption/errors'; +export type { MongocryptdManager } from './client-side-encryption/mongocryptd_manager'; +export type { + AWSKMSProviderConfiguration, + AzureKMSProviderConfiguration, + ClientEncryptionDataKeyProvider, + CredentialProviders, + GCPKMSProviderConfiguration, + KMIPKMSProviderConfiguration, + KMSProviders, + LocalKMSProviderConfiguration +} from './client-side-encryption/providers/index'; +export type { + ClientEncryptionSocketOptions, + ClientEncryptionTlsOptions, + CSFLEKMSTlsOptions, + StateMachineExecutable +} from './client-side-encryption/state_machine'; +export type { AuthContext, AuthProvider } from './cmap/auth/auth_provider'; +export type { + AuthMechanismProperties, + MongoCredentials, + MongoCredentialsOptions +} from './cmap/auth/mongo_credentials'; +export type { + IdPInfo, + IdPServerResponse, + OIDCCallbackFunction, + OIDCCallbackParams, + OIDCResponse +} from './cmap/auth/mongodb_oidc'; +export type { Workflow } from './cmap/auth/mongodb_oidc'; +export type { TokenCache } from './cmap/auth/mongodb_oidc/token_cache'; +export type { + MessageHeader, + OpCompressedRequest, + OpCompressesRequestOptions, + OpMsgOptions, + OpMsgRequest, + OpMsgResponse, + OpQueryOptions, + OpQueryRequest, + OpReply, + WriteProtocolMessageType +} from './cmap/commands'; +export type { HandshakeDocument } from './cmap/connect'; +export type { LEGAL_TCP_SOCKET_OPTIONS, LEGAL_TLS_SOCKET_OPTIONS, Stream } from './cmap/connect'; +export type { + CommandOptions, + Connection, + ConnectionEvents, + ConnectionOptions, + ProxyOptions +} from './cmap/connection'; +export type { + ConnectionPool, + ConnectionPoolEvents, + ConnectionPoolOptions, + PoolState, + WaitQueueMember, + WithConnectionCallback +} from './cmap/connection_pool'; +export type { ClientMetadata } from './cmap/handshake/client_metadata'; +export type { ConnectionPoolMetrics } from './cmap/metrics'; +export type { StreamDescription, StreamDescriptionOptions } from './cmap/stream_description'; +export type { CompressorName } from './cmap/wire_protocol/compression'; +export type { + JSTypeOf, + OnDemandDocument, + OnDemandDocumentDeserializeOptions +} from './cmap/wire_protocol/on_demand/document'; +export type { + CursorResponse, + MongoDBResponse, + MongoDBResponseConstructor +} from './cmap/wire_protocol/responses'; +export type { + CollectionOptions, + CollectionPrivate, + CountDocumentsOptions, + ModifyResult +} from './collection'; +export type { + COMMAND_FAILED, + COMMAND_STARTED, + COMMAND_SUCCEEDED, + CONNECTION_CHECK_OUT_FAILED, + CONNECTION_CHECK_OUT_STARTED, + CONNECTION_CHECKED_IN, + CONNECTION_CHECKED_OUT, + CONNECTION_CLOSED, + CONNECTION_CREATED, + CONNECTION_POOL_CLEARED, + CONNECTION_POOL_CLOSED, + CONNECTION_POOL_CREATED, + CONNECTION_POOL_READY, + CONNECTION_READY, + MONGO_CLIENT_EVENTS, + SERVER_CLOSED, + SERVER_DESCRIPTION_CHANGED, + SERVER_HEARTBEAT_FAILED, + SERVER_HEARTBEAT_STARTED, + SERVER_HEARTBEAT_SUCCEEDED, + SERVER_OPENING, + SERVER_SELECTION_FAILED, + SERVER_SELECTION_STARTED, + SERVER_SELECTION_SUCCEEDED, + TOPOLOGY_CLOSED, + TOPOLOGY_DESCRIPTION_CHANGED, + TOPOLOGY_OPENING, + WAITING_FOR_SUITABLE_SERVER +} from './constants'; +export type { + AbstractCursorEvents, + AbstractCursorOptions, + CursorFlag +} from './cursor/abstract_cursor'; +export type { + CursorTimeoutContext, + InitialCursorResponse, + InternalAbstractCursorOptions +} from './cursor/abstract_cursor'; +export type { AggregationCursorOptions } from './cursor/aggregation_cursor'; +export type { ChangeStreamCursorOptions } from './cursor/change_stream_cursor'; +export type { + ListSearchIndexesCursor, + ListSearchIndexesOptions +} from './cursor/list_search_indexes_cursor'; +export type { RunCursorCommandOptions } from './cursor/run_command_cursor'; +export type { DbOptions, DbPrivate } from './db'; +export type { Encrypter, EncrypterOptions } from './encrypter'; +export type { AnyError, ErrorDescription, MongoNetworkErrorOptions } from './error'; +export type { + Explain, + ExplainCommandOptions, + ExplainOptions, + ExplainVerbosityLike +} from './explain'; +export type { + GridFSBucketReadStreamOptions, + GridFSBucketReadStreamOptionsWithRevision, + GridFSBucketReadStreamPrivate, + GridFSFile +} from './gridfs/download'; +export type { GridFSBucketEvents, GridFSBucketOptions, GridFSBucketPrivate } from './gridfs/index'; +export type { GridFSBucketWriteStreamOptions, GridFSChunk } from './gridfs/upload'; +export type { + Auth, + DriverInfo, + MongoClientEvents, + MongoClientOptions, + MongoClientPrivate, + MongoOptions, + PkFactory, + ServerApi, + SupportedNodeConnectionOptions, + SupportedSocketOptions, + SupportedTLSConnectionOptions, + SupportedTLSSocketOptions, + WithSessionCallback +} from './mongo_client'; +export { MongoClientAuthProviders } from './mongo_client_auth_providers'; +export type { + Log, + LogComponentSeveritiesClientOptions, + LogConvertible, + Loggable, + LoggableCommandFailedEvent, + LoggableCommandSucceededEvent, + LoggableEvent, + LoggableServerHeartbeatFailedEvent, + LoggableServerHeartbeatStartedEvent, + LoggableServerHeartbeatSucceededEvent, + MongoDBLogWritable, + MongoLogger, + MongoLoggerEnvOptions, + MongoLoggerMongoClientOptions, + MongoLoggerOptions +} from './mongo_logger'; +export type { + Abortable, + CommonEvents, + EventsDescription, + GenericListener, + TypedEventEmitter +} from './mongo_types'; +export type { + AcceptedFields, + AddToSetOperators, + AlternativeType, + ArrayElement, + ArrayOperator, + BitwiseFilter, + BSONTypeAlias, + Condition, + EnhancedOmit, + Filter, + FilterOperations, + FilterOperators, + Flatten, + InferIdType, + IntegerType, + IsAny, + Join, + KeysOfAType, + KeysOfOtherType, + MatchKeysAndValues, + NestedPaths, + NestedPathsOfType, + NonObjectIdLikeDocument, + NotAcceptedFields, + NumericType, + OneOrMore, + OnlyFieldsOfType, + OptionalId, + OptionalUnlessRequiredId, + PropertyType, + PullAllOperator, + PullOperator, + PushOperator, + RegExpOrString, + RootFilterOperators, + SchemaMember, + SetFields, + StrictFilter, + StrictMatchKeysAndValues, + StrictUpdateFilter, + UpdateFilter, + WithId, + WithoutId +} from './mongo_types'; +export type { + AggregateOperation, + AggregateOptions, + DB_AGGREGATE_COLLECTION +} from './operations/aggregate'; +export type { + AnyClientBulkWriteModel, + ClientBulkWriteError, + ClientBulkWriteModel, + ClientBulkWriteOptions, + ClientBulkWriteResult, + ClientDeleteManyModel, + ClientDeleteOneModel, + ClientDeleteResult, + ClientInsertOneModel, + ClientInsertOneResult, + ClientReplaceOneModel, + ClientUpdateManyModel, + ClientUpdateOneModel, + ClientUpdateResult, + ClientWriteModel +} from './operations/client_bulk_write/common'; +export type { + CollationOptions, + CommandOperation, + CommandOperationOptions, + OperationParent +} from './operations/command'; +export type { CountOptions } from './operations/count'; +export type { + ClusteredCollectionOptions, + CreateCollectionOptions, + TimeSeriesCollectionOptions +} from './operations/create_collection'; +export type { DeleteOptions, DeleteResult, DeleteStatement } from './operations/delete'; +export type { DistinctOptions } from './operations/distinct'; +export type { DropCollectionOptions, DropDatabaseOptions } from './operations/drop'; +export type { EstimatedDocumentCountOptions } from './operations/estimated_document_count'; +export type { FindOneOptions, FindOptions } from './operations/find'; +export type { + FindOneAndDeleteOptions, + FindOneAndReplaceOptions, + FindOneAndUpdateOptions +} from './operations/find_and_modify'; +export type { IndexInformationOptions } from './operations/indexes'; +export type { + CreateIndexesOptions, + DropIndexesOptions, + IndexDescription, + IndexDescriptionCompact, + IndexDescriptionInfo, + IndexDirection, + IndexSpecification, + ListIndexesOptions +} from './operations/indexes'; +export type { InsertManyResult, InsertOneOptions, InsertOneResult } from './operations/insert'; +export type { CollectionInfo, ListCollectionsOptions } from './operations/list_collections'; +export type { ListDatabasesOptions, ListDatabasesResult } from './operations/list_databases'; +export type { AbstractOperation, Hint, OperationOptions } from './operations/operation'; +export type { ProfilingLevelOptions } from './operations/profiling_level'; +export type { RemoveUserOptions } from './operations/remove_user'; +export type { RenameOptions } from './operations/rename'; +export type { RunCommandOptions } from './operations/run_command'; +export type { SearchIndexDescription } from './operations/search_indexes/create'; +export type { SetProfilingLevelOptions } from './operations/set_profiling_level'; +export type { DbStatsOptions } from './operations/stats'; +export type { + ReplaceOptions, + UpdateOptions, + UpdateResult, + UpdateStatement +} from './operations/update'; +export type { ValidateCollectionOptions } from './operations/validate_collection'; +export type { ReadConcernLike } from './read_concern'; +export type { + HedgeOptions, + ReadPreferenceFromOptions, + ReadPreferenceLike, + ReadPreferenceLikeOptions, + ReadPreferenceOptions +} from './read_preference'; +export type { OsAdapter, Runtime, RuntimeAdapters } from './runtime_adapters'; +export type { ClusterTime } from './sdam/common'; +export type { + Monitor, + MonitorEvents, + MonitorInterval, + MonitorIntervalOptions, + MonitorOptions, + MonitorPrivate, + RTTPinger, + RTTPingerOptions, + RTTSampler, + ServerMonitoringMode +} from './sdam/monitor'; +export type { + Server, + ServerCommandOptions, + ServerEvents, + ServerOptions, + ServerPrivate +} from './sdam/server'; +export type { + ServerDescription, + ServerDescriptionOptions, + TagSet, + TopologyVersion +} from './sdam/server_description'; +export type { DeprioritizedServers, ServerSelector } from './sdam/server_selection'; +export type { SrvPoller, SrvPollerEvents, SrvPollerOptions } from './sdam/srv_polling'; +export type { + ConnectOptions, + SelectServerOptions, + ServerSelectionCallback, + ServerSelectionRequest, + Topology, + TopologyEvents, + TopologyOptions, + TopologyPrivate +} from './sdam/topology'; +export type { TopologyDescription, TopologyDescriptionOptions } from './sdam/topology_description'; +export type { + ClientSessionEvents, + ClientSessionOptions, + EndSessionOptions, + ServerSession, + ServerSessionId, + ServerSessionPool, + WithTransactionCallback +} from './sessions'; +export type { Sort, SortDirection, SortDirectionForCmd, SortForCmd } from './sort'; +export type { + CSOTTimeoutContext, + CSOTTimeoutContextOptions, + LegacyTimeoutContext, + LegacyTimeoutContextOptions, + Timeout, + TimeoutContext, + TimeoutContextOptions +} from './timeout'; +export type { Transaction, TransactionOptions, TxnState } from './transactions'; +export type { + BufferPool, + Callback, + EventEmitterWithState, + HostAddress, + List, + MongoDBCollectionNamespace, + MongoDBNamespace +} from './utils'; +export type { W, WriteConcernOptions, WriteConcernSettings } from './write_concern'; diff --git a/gateway/node_modules/mongodb/src/mongo_client.ts b/gateway/node_modules/mongodb/src/mongo_client.ts new file mode 100644 index 0000000000000000000000000000000000000000..944c1c5f2efb749d8cd99aba5a68ae6fbff477aa --- /dev/null +++ b/gateway/node_modules/mongodb/src/mongo_client.ts @@ -0,0 +1,1182 @@ +import { promises as fs } from 'fs'; +import type { TcpNetConnectOpts } from 'net'; +import type { ConnectionOptions as TLSConnectionOptions, TLSSocketOptions } from 'tls'; + +import { TopologyType } from '.'; +import { type BSONSerializeOptions, type Document, resolveBSONOptions } from './bson'; +import { ChangeStream, type ChangeStreamDocument, type ChangeStreamOptions } from './change_stream'; +import type { AutoEncrypter, AutoEncryptionOptions } from './client-side-encryption/auto_encrypter'; +import { + type AuthMechanismProperties, + DEFAULT_ALLOWED_HOSTS, + type MongoCredentials +} from './cmap/auth/mongo_credentials'; +import { type TokenCache } from './cmap/auth/mongodb_oidc/token_cache'; +import { AuthMechanism } from './cmap/auth/providers'; +import type { LEGAL_TCP_SOCKET_OPTIONS, LEGAL_TLS_SOCKET_OPTIONS } from './cmap/connect'; +import type { Connection } from './cmap/connection'; +import { + type ClientMetadata, + isDriverInfoEqual, + makeClientMetadata +} from './cmap/handshake/client_metadata'; +import type { CompressorName } from './cmap/wire_protocol/compression'; +import { parseOptions, resolveSRVRecord } from './connection_string'; +import { MONGO_CLIENT_EVENTS } from './constants'; +import { type AbstractCursor } from './cursor/abstract_cursor'; +import { Db, type DbOptions } from './db'; +import type { Encrypter } from './encrypter'; +import { MongoInvalidArgumentError } from './error'; +import { MongoClientAuthProviders } from './mongo_client_auth_providers'; +import { + type LogComponentSeveritiesClientOptions, + type MongoDBLogWritable, + MongoLogger, + type MongoLoggerOptions, + SeverityLevel +} from './mongo_logger'; +import { TypedEventEmitter } from './mongo_types'; +import { + type ClientBulkWriteModel, + type ClientBulkWriteOptions, + type ClientBulkWriteResult +} from './operations/client_bulk_write/common'; +import { ClientBulkWriteExecutor } from './operations/client_bulk_write/executor'; +import { EndSessionsOperation } from './operations/end_sessions'; +import { executeOperation } from './operations/execute_operation'; +import type { ReadConcern, ReadConcernLevel, ReadConcernLike } from './read_concern'; +import { ReadPreference, type ReadPreferenceMode } from './read_preference'; +import { type Runtime, type RuntimeAdapters } from './runtime_adapters'; +import type { ServerMonitoringMode } from './sdam/monitor'; +import type { TagSet } from './sdam/server_description'; +import { DeprioritizedServers, readPreferenceServerSelector } from './sdam/server_selection'; +import type { SrvPoller } from './sdam/srv_polling'; +import { Topology, type TopologyEvents } from './sdam/topology'; +import { ClientSession, type ClientSessionOptions, ServerSessionPool } from './sessions'; +import { + COSMOS_DB_CHECK, + COSMOS_DB_MSG, + DOCUMENT_DB_CHECK, + DOCUMENT_DB_MSG, + type HostAddress, + hostMatchesWildcards, + isHostMatch, + type MongoDBNamespace, + noop, + ns, + resolveOptions, + squashError +} from './utils'; +import type { W, WriteConcern, WriteConcernSettings } from './write_concern'; + +/** @public */ +export const ServerApiVersion = Object.freeze({ + v1: '1' +} as const); + +/** @public */ +export type ServerApiVersion = (typeof ServerApiVersion)[keyof typeof ServerApiVersion]; + +/** @public */ +export interface ServerApi { + version: ServerApiVersion; + strict?: boolean; + deprecationErrors?: boolean; +} + +/** @public */ +export interface DriverInfo { + name?: string; + version?: string; + platform?: string; +} + +/** @public */ +export interface Auth { + /** The username for auth */ + username?: string; + /** The password for auth */ + password?: string; +} + +/** @public */ +export interface PkFactory { + createPk(): any; +} + +/** @public */ +export type SupportedTLSConnectionOptions = Pick< + TLSConnectionOptions & { + allowPartialTrustChain?: boolean; + }, + (typeof LEGAL_TLS_SOCKET_OPTIONS)[number] +>; + +/** @public */ +export type SupportedTLSSocketOptions = Pick< + TLSSocketOptions, + Extract +>; + +/** @public */ +export type SupportedSocketOptions = Pick< + TcpNetConnectOpts & { + autoSelectFamily?: boolean; + autoSelectFamilyAttemptTimeout?: number; + /** Node.JS socket option to set the time the first keepalive probe is sent on an idle socket. Defaults to 120000ms */ + keepAliveInitialDelay?: number; + }, + (typeof LEGAL_TCP_SOCKET_OPTIONS)[number] +>; + +/** @public */ +export type SupportedNodeConnectionOptions = SupportedTLSConnectionOptions & + SupportedTLSSocketOptions & + SupportedSocketOptions; + +/** + * Describes all possible URI query options for the mongo client + * @public + * @see https://www.mongodb.com/docs/manual/reference/connection-string + */ +export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeConnectionOptions { + /** Specifies the name of the replica set, if the mongod is a member of a replica set. */ + replicaSet?: string; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; + /** Enables or disables TLS/SSL for the connection. */ + tls?: boolean; + /** A boolean to enable or disables TLS/SSL for the connection. (The ssl option is equivalent to the tls option.) */ + ssl?: boolean; + /** Specifies the location of a local .pem file that contains either the client's TLS/SSL certificate and key. */ + tlsCertificateKeyFile?: string; + /** Specifies the password to de-crypt the tlsCertificateKeyFile. */ + tlsCertificateKeyFilePassword?: string; + /** Specifies the location of a local .pem file that contains the root certificate chain from the Certificate Authority. This file is used to validate the certificate presented by the mongod/mongos instance. */ + tlsCAFile?: string; + /** Specifies the location of a local CRL .pem file that contains the client revokation list. */ + tlsCRLFile?: string; + /** Bypasses validation of the certificates presented by the mongod/mongos instance */ + tlsAllowInvalidCertificates?: boolean; + /** Disables hostname validation of the certificate presented by the mongod/mongos instance. */ + tlsAllowInvalidHostnames?: boolean; + /** Disables various certificate validations. */ + tlsInsecure?: boolean; + /** The time in milliseconds to attempt a connection before timing out. */ + connectTimeoutMS?: number; + /** The time in milliseconds to attempt a send or receive on a socket before the attempt times out. */ + socketTimeoutMS?: number; + /** An array or comma-delimited string of compressors to enable network compression for communication between this client and a mongod/mongos instance. */ + compressors?: CompressorName[] | string; + /** An integer that specifies the compression level if using zlib for network compression. */ + zlibCompressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | undefined; + /** The maximum number of hosts to connect to when using an srv connection string, a setting of `0` means unlimited hosts */ + srvMaxHosts?: number; + /** + * Modifies the srv URI to look like: + * + * `_{srvServiceName}._tcp.{hostname}.{domainname}` + * + * Querying this DNS URI is expected to respond with SRV records + */ + srvServiceName?: string; + /** The maximum number of connections in the connection pool. */ + maxPoolSize?: number; + /** The minimum number of connections in the connection pool. */ + minPoolSize?: number; + /** The maximum number of connections that may be in the process of being established concurrently by the connection pool. */ + maxConnecting?: number; + /** + * The maximum amount of time a connection should remain idle in the connection pool before being marked idle, in milliseconds. + * If specified, this must be a number greater than or equal to 0, where 0 means there is no limit. Defaults to 0. After this + * time passes, the idle collection can be automatically cleaned up in the background. + */ + maxIdleTimeMS?: number; + /** The maximum time in milliseconds that a thread can wait for a connection to become available. */ + waitQueueTimeoutMS?: number; + /** Specify a read concern for the collection (only MongoDB 3.2 or higher supported) */ + readConcern?: ReadConcernLike; + /** The level of isolation */ + readConcernLevel?: ReadConcernLevel; + /** Specifies the read preferences for this connection */ + readPreference?: ReadPreferenceMode | ReadPreference; + /** Specifies, in seconds, how stale a secondary can be before the client stops using it for read operations. */ + maxStalenessSeconds?: number; + /** Specifies the tags document as a comma-separated list of colon-separated key-value pairs. */ + readPreferenceTags?: TagSet[]; + /** The auth settings for when connection to server. */ + auth?: Auth; + /** Specify the database name associated with the user’s credentials. */ + authSource?: string; + /** Specify the authentication mechanism that MongoDB will use to authenticate the connection. */ + authMechanism?: AuthMechanism; + /** Specify properties for the specified authMechanism as a comma-separated list of colon-separated key-value pairs. */ + authMechanismProperties?: AuthMechanismProperties; + /** The size (in milliseconds) of the latency window for selecting among multiple suitable MongoDB instances. */ + localThresholdMS?: number; + /** Specifies how long (in milliseconds) to block for server selection before throwing an exception. */ + serverSelectionTimeoutMS?: number; + /** heartbeatFrequencyMS controls when the driver checks the state of the MongoDB deployment. Specify the interval (in milliseconds) between checks, counted from the end of the previous check until the beginning of the next one. */ + heartbeatFrequencyMS?: number; + /** Sets the minimum heartbeat frequency. In the event that the driver has to frequently re-check a server's availability, it will wait at least this long since the previous check to avoid wasted effort. */ + minHeartbeatFrequencyMS?: number; + /** The name of the application that created this MongoClient instance. MongoDB 3.4 and newer will print this value in the server log upon establishing each connection. It is also recorded in the slow query log and profile collections */ + appName?: string; + /** Enables retryable reads. */ + retryReads?: boolean; + /** Enable retryable writes. */ + retryWrites?: boolean; + /** + * The maximum number of retries during server overload. Set to 0 to disable overload retries. Defaults to 2. + * @see https://www.mongodb.com/docs/atlas/overload-errors + * + * This option works with MongoDB Atlas Server Version 9.0 and above. + * */ + maxAdaptiveRetries?: number; + /** + * Whether or not to enable overload retargeting. Defaults to false. + * @see https://www.mongodb.com/docs/atlas/overload-errors + * More information about the overload policy in drivers: + * @see https://github.com/mongodb/specifications/blob/master/source/client-backpressure/client-backpressure.md#overload-retry-policy + * + * This option works with MongoDB Atlas Server Version 9.0 and above. + * */ + enableOverloadRetargeting?: boolean; + /** Allow a driver to force a Single topology type with a connection string containing one host */ + directConnection?: boolean; + /** Instruct the driver it is connecting to a load balancer fronting a mongos like service */ + loadBalanced?: boolean; + /** + * The write concern w value + * @deprecated Please use the `writeConcern` option instead + */ + w?: W; + /** + * The write concern timeout + * @deprecated Please use the `writeConcern` option instead + */ + wtimeoutMS?: number; + /** + * The journal write concern + * @deprecated Please use the `writeConcern` option instead + */ + journal?: boolean; + /** + * A MongoDB WriteConcern, which describes the level of acknowledgement + * requested from MongoDB for write operations. + * + * @see https://www.mongodb.com/docs/manual/reference/write-concern/ + */ + writeConcern?: WriteConcern | WriteConcernSettings; + /** TCP Connection no delay */ + noDelay?: boolean; + /** Force server to assign `_id` values instead of driver */ + forceServerObjectId?: boolean; + /** A primary key factory function for generation of custom `_id` keys */ + pkFactory?: PkFactory; + /** Enable command monitoring for this client */ + monitorCommands?: boolean; + /** Server API version */ + serverApi?: ServerApi | ServerApiVersion; + /** + * Optionally enable in-use auto encryption + * + * @remarks + * Automatic encryption is an enterprise only feature that only applies to operations on a collection. Automatic encryption is not supported for operations on a database or view, and operations that are not bypassed will result in error + * (see [libmongocrypt: Auto Encryption Allow-List](https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/client-side-encryption.md#libmongocrypt-auto-encryption-allow-list)). To bypass automatic encryption for all operations, set bypassAutoEncryption=true in AutoEncryptionOpts. + * + * Automatic encryption requires the authenticated user to have the [listCollections privilege action](https://www.mongodb.com/docs/manual/reference/command/listCollections/#dbcmd.listCollections). + * + * If a MongoClient with a limited connection pool size (i.e a non-zero maxPoolSize) is configured with AutoEncryptionOptions, a separate internal MongoClient is created if any of the following are true: + * - AutoEncryptionOptions.keyVaultClient is not passed. + * - AutoEncryptionOptions.bypassAutomaticEncryption is false. + * + * If an internal MongoClient is created, it is configured with the same options as the parent MongoClient except minPoolSize is set to 0 and AutoEncryptionOptions is omitted. + */ + autoEncryption?: AutoEncryptionOptions; + /** + * Allows a wrapping driver to amend the client metadata generated by the driver to include information about the wrapping driver + /* @deprecated - Will be made internal in a future major release. + */ + driverInfo?: DriverInfo; + /** Configures a Socks5 proxy host used for creating TCP connections. */ + proxyHost?: string; + /** Configures a Socks5 proxy port used for creating TCP connections. */ + proxyPort?: number; + /** Configures a Socks5 proxy username when the proxy in proxyHost requires username/password authentication. */ + proxyUsername?: string; + /** Configures a Socks5 proxy password when the proxy in proxyHost requires username/password authentication. */ + proxyPassword?: string; + /** Instructs the driver monitors to use a specific monitoring mode */ + serverMonitoringMode?: ServerMonitoringMode; + /** + * @public + * Specifies the destination of the driver's logging. The default is stderr. + */ + mongodbLogPath?: 'stderr' | 'stdout' | MongoDBLogWritable; + /** + * @public + * Enable logging level per component or use `default` to control any unset components. + */ + mongodbLogComponentSeverities?: LogComponentSeveritiesClientOptions; + /** + * @public + * All BSON documents are stringified to EJSON. This controls the maximum length of those strings. + * It is defaulted to 1000. + */ + mongodbLogMaxDocumentLength?: number; + + /** @internal */ + srvPoller?: SrvPoller; + /** @internal */ + connectionType?: typeof Connection; + /** @internal */ + __skipPingOnConnect?: boolean; + /** + * @experimental + * + * If provided, any adapters provided will be used in place of the corresponding Node.js module. + */ + runtimeAdapters?: RuntimeAdapters; +} + +/** @public */ +export type WithSessionCallback = (session: ClientSession) => Promise; + +/** @internal */ +export interface MongoClientPrivate { + url: string; + bsonOptions: BSONSerializeOptions; + namespace: MongoDBNamespace; + hasBeenClosed: boolean; + authProviders: MongoClientAuthProviders; + /** + * We keep a reference to the sessions that are acquired from the pool. + * - used to track and close all sessions in client.close() (which is non-standard behavior) + * - used to notify the leak checker in our tests if test author forgot to clean up explicit sessions + */ + readonly activeSessions: Set; + /** + * We keep a reference to the cursors that are created from this client. + * - used to track and close all cursors in client.close(). + * Cursors in this set are ones that still need to have their close method invoked (no other conditions are considered) + */ + readonly activeCursors: Set; + readonly sessionPool: ServerSessionPool; + readonly options: MongoOptions; + readonly readConcern?: ReadConcern; + readonly writeConcern?: WriteConcern; + readonly readPreference: ReadPreference; + readonly isMongoClient: true; +} + +/** @public */ +export type MongoClientEvents = Pick & { + // In previous versions the open event emitted a topology, in an effort to no longer + // expose internals but continue to expose this useful event API, it now emits a mongoClient + open(mongoClient: MongoClient): void; +}; + +/** + * @public + * + * The **MongoClient** class is a class that allows for making Connections to MongoDB. + * + * **NOTE:** The programmatically provided options take precedence over the URI options. + * + * @remarks + * + * A MongoClient is the entry point to connecting to a MongoDB server. + * + * It handles a multitude of features on your application's behalf: + * - **Server Host Connection Configuration**: A MongoClient is responsible for reading TLS cert, ca, and crl files if provided. + * - **SRV Record Polling**: A "`mongodb+srv`" style connection string is used to have the MongoClient resolve DNS SRV records of all server hostnames which the driver periodically monitors for changes and adjusts its current view of hosts correspondingly. + * - **Server Monitoring**: The MongoClient automatically keeps monitoring the health of server nodes in your cluster to reach out to the correct and lowest latency one available. + * - **Connection Pooling**: To avoid paying the cost of rebuilding a connection to the server on every operation the MongoClient keeps idle connections preserved for reuse. + * - **Session Pooling**: The MongoClient creates logical sessions that enable retryable writes, causal consistency, and transactions. It handles pooling these sessions for reuse in subsequent operations. + * - **Cursor Operations**: A MongoClient's cursors use the health monitoring system to send the request for more documents to the same server the query began on. + * - **Mongocryptd process**: When using auto encryption, a MongoClient will launch a `mongocryptd` instance for handling encryption if the mongocrypt shared library isn't in use. + * + * There are many more features of a MongoClient that are not listed above. + * + * In order to enable these features, a number of asynchronous Node.js resources are established by the driver: Timers, FS Requests, Sockets, etc. + * For details on cleanup, please refer to the MongoClient `close()` documentation. + * + * @example + * ```ts + * import { MongoClient } from 'mongodb'; + * // Enable command monitoring for debugging + * const client = new MongoClient('mongodb://localhost:27017?appName=mflix', { monitorCommands: true }); + * ``` + */ +export class MongoClient extends TypedEventEmitter implements AsyncDisposable { + /** @internal */ + s: MongoClientPrivate; + /** @internal */ + topology?: Topology; + /** @internal */ + override readonly mongoLogger: MongoLogger | undefined; + /** @internal */ + private connectionLock?: Promise; + /** @internal */ + private closeLock?: Promise; + + /** + * The consolidate, parsed, transformed and merged options. + */ + public readonly options: Readonly< + Omit + > & + Pick & { + /** @internal */ + metadata: Promise; + }; + + private driverInfoList: DriverInfo[] = []; + + constructor(url: string, options?: MongoClientOptions) { + super(); + this.on('error', noop); + + this.options = parseOptions(url, this, options); + + this.appendMetadata(this.options.driverInfo); + + const shouldSetLogger = Object.values(this.options.mongoLoggerOptions.componentSeverities).some( + value => value !== SeverityLevel.OFF + ); + this.mongoLogger = shouldSetLogger + ? new MongoLogger(this.options.mongoLoggerOptions) + : undefined; + + // eslint-disable-next-line @typescript-eslint/no-this-alias + const client = this; + + // The internal state + this.s = { + url, + bsonOptions: resolveBSONOptions(this.options), + namespace: ns('admin'), + hasBeenClosed: false, + sessionPool: new ServerSessionPool(this), + activeSessions: new Set(), + activeCursors: new Set(), + authProviders: new MongoClientAuthProviders(), + + get options() { + return client.options; + }, + get readConcern() { + return client.options.readConcern; + }, + get writeConcern() { + return client.options.writeConcern; + }, + get readPreference() { + return client.options.readPreference; + }, + get isMongoClient(): true { + return true; + } + }; + this.checkForNonGenuineHosts(); + } + + /** + * An alias for {@link MongoClient.close|MongoClient.close()}. + */ + async [Symbol.asyncDispose]() { + await this.close(); + } + + /** + * Append metadata to the client metadata after instantiation. + * @param driverInfo - Information about the application or library. + */ + appendMetadata(driverInfo: DriverInfo) { + const isDuplicateDriverInfo = this.driverInfoList.some(info => + isDriverInfoEqual(info, driverInfo) + ); + if (isDuplicateDriverInfo) return; + + this.driverInfoList.push(driverInfo); + this.options.metadata = makeClientMetadata(this.driverInfoList, this.options) + .then(undefined, squashError) + .then(result => result ?? ({} as ClientMetadata)); // ensure Promise + } + + /** @internal */ + private checkForNonGenuineHosts() { + const documentDBHostnames = this.options.hosts.filter((hostAddress: HostAddress) => + isHostMatch(DOCUMENT_DB_CHECK, hostAddress.host) + ); + const srvHostIsDocumentDB = isHostMatch(DOCUMENT_DB_CHECK, this.options.srvHost); + + const cosmosDBHostnames = this.options.hosts.filter((hostAddress: HostAddress) => + isHostMatch(COSMOS_DB_CHECK, hostAddress.host) + ); + const srvHostIsCosmosDB = isHostMatch(COSMOS_DB_CHECK, this.options.srvHost); + + if (documentDBHostnames.length !== 0 || srvHostIsDocumentDB) { + this.mongoLogger?.info('client', DOCUMENT_DB_MSG); + } else if (cosmosDBHostnames.length !== 0 || srvHostIsCosmosDB) { + this.mongoLogger?.info('client', COSMOS_DB_MSG); + } + } + + get serverApi(): Readonly { + return this.options.serverApi && Object.freeze({ ...this.options.serverApi }); + } + /** + * Intended for APM use only + * @internal + */ + get monitorCommands(): boolean { + return this.options.monitorCommands; + } + set monitorCommands(value: boolean) { + this.options.monitorCommands = value; + } + + /** @internal */ + get autoEncrypter(): AutoEncrypter | undefined { + return this.options.autoEncrypter; + } + + get readConcern(): ReadConcern | undefined { + return this.s.readConcern; + } + + get writeConcern(): WriteConcern | undefined { + return this.s.writeConcern; + } + + get readPreference(): ReadPreference { + return this.s.readPreference; + } + + get bsonOptions(): BSONSerializeOptions { + return this.s.bsonOptions; + } + + get timeoutMS(): number | undefined { + return this.s.options.timeoutMS; + } + + /** + * Executes a client bulk write operation, available on server 8.0+. + * @param models - The client bulk write models. + * @param options - The client bulk write options. + * @returns A ClientBulkWriteResult for acknowledged writes and ok: 1 for unacknowledged writes. + */ + async bulkWrite = Record>( + models: ReadonlyArray>, + options?: ClientBulkWriteOptions + ): Promise { + if (this.autoEncrypter) { + throw new MongoInvalidArgumentError( + 'MongoClient bulkWrite does not currently support automatic encryption.' + ); + } + // We do not need schema type information past this point ("as any" is fine) + return await new ClientBulkWriteExecutor( + this, + models as any, + resolveOptions(this, options) + ).execute(); + } + + /** + * An optional method to verify a handful of assumptions that are generally useful at application boot-time before using a MongoClient. + * For detailed information about the connect process see the MongoClient.connect static method documentation. + * + * @param url - The MongoDB connection string (supports `mongodb://` and `mongodb+srv://` schemes) + * @param options - Optional configuration options for the client + * + * @see https://www.mongodb.com/docs/manual/reference/connection-string/ + */ + async connect(): Promise { + if (this.connectionLock) { + return await this.connectionLock; + } + + try { + this.connectionLock = this._connect(); + await this.connectionLock; + } finally { + // release + this.connectionLock = undefined; + } + + return this; + } + + /** + * Create a topology to open the connection, must be locked to avoid topology leaks in concurrency scenario. + * Locking is enforced by the connect method. + * + * @internal + */ + private async _connect(): Promise { + if (this.topology && this.topology.isConnected()) { + return this; + } + + const options = this.options; + + if (options.tls) { + if (typeof options.tlsCAFile === 'string') { + options.ca ??= await fs.readFile(options.tlsCAFile); + } + if (typeof options.tlsCRLFile === 'string') { + options.crl ??= await fs.readFile(options.tlsCRLFile); + } + if (typeof options.tlsCertificateKeyFile === 'string') { + if (!options.key || !options.cert) { + const contents = await fs.readFile(options.tlsCertificateKeyFile); + options.key ??= contents; + options.cert ??= contents; + } + } + } + if (typeof options.srvHost === 'string') { + const hosts = await resolveSRVRecord(options); + + for (const [index, host] of hosts.entries()) { + options.hosts[index] = host; + } + } + + // It is important to perform validation of hosts AFTER SRV resolution, to check the real hostname, + // but BEFORE we even attempt connecting with a potentially not allowed hostname + if (options.credentials?.mechanism === AuthMechanism.MONGODB_OIDC) { + const allowedHosts = + options.credentials?.mechanismProperties?.ALLOWED_HOSTS || DEFAULT_ALLOWED_HOSTS; + const isServiceAuth = !!options.credentials?.mechanismProperties?.ENVIRONMENT; + if (!isServiceAuth) { + for (const host of options.hosts) { + if (!hostMatchesWildcards(host.toHostPort().host, allowedHosts)) { + throw new MongoInvalidArgumentError( + `Host '${host}' is not valid for OIDC authentication with ALLOWED_HOSTS of '${allowedHosts.join( + ',' + )}'` + ); + } + } + } + } + + this.topology = new Topology(this, options.hosts, options); + // Events can be emitted before initialization is complete so we have to + // save the reference to the topology on the client ASAP if the event handlers need to access it + + this.topology.once(Topology.OPEN, () => this.emit('open', this)); + + for (const event of MONGO_CLIENT_EVENTS) { + this.topology.on(event, (...args: any[]) => this.emit(event, ...(args as any))); + } + + const topologyConnect = async () => { + try { + await this.topology?.connect(options); + } catch (error) { + this.topology?.close(); + throw error; + } + }; + + if (this.autoEncrypter) { + await this.autoEncrypter?.init(); + await topologyConnect(); + await options.encrypter.connectInternalClient(); + } else { + await topologyConnect(); + } + + return this; + } + + /** + * Cleans up resources managed by the MongoClient. + * + * The close method clears and closes all resources whose lifetimes are managed by the MongoClient. + * Please refer to the `MongoClient` class documentation for a high level overview of the client's key features and responsibilities. + * + * **However,** the close method does not handle the cleanup of resources explicitly created by the user. + * Any user-created driver resource with its own `close()` method should be explicitly closed by the user before calling MongoClient.close(). + * This method is written as a "best effort" attempt to leave behind the least amount of resources server-side when possible. + * + * The following list defines ideal preconditions and consequent pitfalls if they are not met. + * The MongoClient, ClientSession, Cursors and ChangeStreams all support [explicit resource management](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html). + * By using explicit resource management to manage the lifetime of driver resources instead of manually managing their lifetimes, the pitfalls outlined below can be avoided. + * + * The close method performs the following in the order listed: + * - Client-side: + * - **Close in-use connections**: Any connections that are currently waiting on a response from the server will be closed. + * This is performed _first_ to avoid reaching the next step (server-side clean up) and having no available connections to check out. + * - _Ideal_: All operations have been awaited or cancelled, and the outcomes, regardless of success or failure, have been processed before closing the client servicing the operation. + * - _Pitfall_: When `client.close()` is called and all connections are in use, after closing them, the client must create new connections for cleanup operations, which comes at the cost of new TLS/TCP handshakes and authentication steps. + * - Server-side: + * - **Close active cursors**: All cursors that haven't been completed will have a `killCursor` operation sent to the server they were initialized on, freeing the server-side resource. + * - _Ideal_: Cursors are explicitly closed or completed before `client.close()` is called. + * - _Pitfall_: `killCursors` may have to build a new connection if the in-use closure ended all pooled connections. + * - **End active sessions**: In-use sessions created with `client.startSession()` or `client.withSession()` or implicitly by the driver will have their `.endSession()` method called. + * Contrary to the name of the method, `endSession()` returns the session to the client's pool of sessions rather than end them on the server. + * - _Ideal_: Transaction outcomes are awaited and their corresponding explicit sessions are ended before `client.close()` is called. + * - _Pitfall_: **This step aborts in-progress transactions**. It is advisable to observe the outcome of a transaction before closing your client. + * - **End all pooled sessions**: The `endSessions` command with all session IDs the client has pooled is sent to the server to inform the cluster it can clean them up. + * - _Ideal_: No user intervention is expected. + * - _Pitfall_: None. + * + * The remaining shutdown is of the MongoClient resources that are intended to be entirely internal but is documented here as their existence relates to the JS event loop. + * + * - Client-side (again): + * - **Stop all server monitoring**: Connections kept live for detecting cluster changes and roundtrip time measurements are shutdown. + * - **Close all pooled connections**: Each server node in the cluster has a corresponding connection pool and all connections in the pool are closed. Any operations waiting to check out a connection will have an error thrown instead of a connection returned. + * - **Clear out server selection queue**: Any operations that are in the process of waiting for a server to be selected will have an error thrown instead of a server returned. + * - **Close encryption-related resources**: An internal MongoClient created for communicating with `mongocryptd` or other encryption purposes is closed. (Using this same method of course!) + * + * After the close method completes there should be no MongoClient related resources [ref-ed in Node.js' event loop](https://docs.libuv.org/en/v1.x/handle.html#reference-counting). + * This should allow Node.js to exit gracefully if MongoClient resources were the only active handles in the event loop. + * + * @param _force - currently an unused flag that has no effect. Defaults to `false`. + */ + async close(_force = false): Promise { + if (this.closeLock) { + return await this.closeLock; + } + + try { + this.closeLock = this._close(); + await this.closeLock; + } finally { + // release + this.closeLock = undefined; + } + } + + /* @internal */ + private async _close(): Promise { + // There's no way to set hasBeenClosed back to false + Object.defineProperty(this.s, 'hasBeenClosed', { + value: true, + enumerable: true, + configurable: false, + writable: false + }); + + this.topology?.closeCheckedOutConnections(); + + const activeCursorCloses = Array.from(this.s.activeCursors, cursor => cursor.close()); + this.s.activeCursors.clear(); + + await Promise.all(activeCursorCloses); + + const activeSessionEnds = Array.from(this.s.activeSessions, session => session.endSession()); + this.s.activeSessions.clear(); + + await Promise.all(activeSessionEnds); + + if (this.topology == null) { + return; + } + + const supportsSessions = + this.topology.description.type === TopologyType.LoadBalanced || + this.topology.description.logicalSessionTimeoutMinutes != null; + + if (supportsSessions) { + await endSessions(this, this.topology); + } + + // clear out references to old topology + const topology = this.topology; + this.topology = undefined; + + topology.close(); + + const { encrypter } = this.options; + if (encrypter) { + await encrypter.close(this); + } + + async function endSessions( + client: MongoClient, + { description: topologyDescription }: Topology + ) { + // If we would attempt to select a server and get nothing back we short circuit + // to avoid the server selection timeout. + const selector = readPreferenceServerSelector(ReadPreference.primaryPreferred); + const serverDescriptions = Array.from(topologyDescription.servers.values()); + const servers = selector(topologyDescription, serverDescriptions, new DeprioritizedServers()); + if (servers.length !== 0) { + const endSessions = Array.from(client.s.sessionPool.sessions, ({ id }) => id); + if (endSessions.length !== 0) { + try { + await executeOperation(client, new EndSessionsOperation(endSessions)); + } catch (error) { + squashError(error); + } + } + } + } + } + + /** + * Create a new Db instance sharing the current socket connections. + * + * @param dbName - The name of the database we want to use. If not provided, use database name from connection string. + * @param options - Optional settings for Db construction + */ + db(dbName?: string, options?: DbOptions): Db { + options = options ?? {}; + + // Default to db from connection string if not provided + if (!dbName) { + dbName = this.s.options.dbName; + } + + // Copy the options and add out internal override of the not shared flag + const finalOptions = Object.assign({}, this.options, options); + + // Return the db object + const db = new Db(this, dbName, finalOptions); + + // Return the database + return db; + } + + /** + * Creates a new MongoClient instance and immediately connects it to MongoDB. + * This convenience method combines `new MongoClient(url, options)` and `client.connect()` in a single step. + * + * Connect can be helpful to detect configuration issues early by validating: + * - **DNS Resolution**: Verifies that SRV records and hostnames in the connection string resolve DNS entries + * - **Network Connectivity**: Confirms that host addresses are reachable and ports are open + * - **TLS Configuration**: Validates SSL/TLS certificates, CA files, and encryption settings are correct + * - **Authentication**: Verifies that provided credentials are valid + * - **Server Compatibility**: Ensures the MongoDB server version is supported by this driver version + * - **Load Balancer Setup**: For load-balanced deployments, confirms the service is properly configured + * + * @returns A promise that resolves to the same MongoClient instance once connected + * + * @remarks + * **Connection is Optional:** Calling `connect` is optional since any operation method (`find`, `insertOne`, etc.) + * will automatically perform these same validation steps if the client is not already connected. + * However, explicitly calling `connect` can make sense for: + * - **Fail-fast Error Detection**: Non-transient connection issues (hostname unresolved, port refused connection) are discovered immediately rather than during your first operation + * - **Predictable Performance**: Eliminates first connection overhead from your first database operation + * + * @remarks + * **Connection Pooling Impact:** Calling `connect` will populate the connection pool with one connection + * to a server selected by the client's configured `readPreference` (defaults to primary). + * + * @remarks + * **Timeout Behavior:** When using `timeoutMS`, the connection establishment time does not count against + * the timeout for subsequent operations. This means `connect` runs without a `timeoutMS` limit, while + * your database operations will still respect the configured timeout. If you need predictable operation + * timing with `timeoutMS`, call `connect` explicitly before performing operations. + * + * @see https://www.mongodb.com/docs/manual/reference/connection-string/ + */ + static async connect(url: string, options?: MongoClientOptions): Promise { + const client = new this(url, options); + return await client.connect(); + } + + /** + * Creates a new ClientSession. When using the returned session in an operation + * a corresponding ServerSession will be created. + * + * @remarks + * A ClientSession instance may only be passed to operations being performed on the same + * MongoClient it was started from. + */ + startSession(options?: ClientSessionOptions): ClientSession { + const session = new ClientSession( + this, + this.s.sessionPool, + { explicit: true, ...options }, + this.options + ); + this.s.activeSessions.add(session); + session.once('ended', () => { + this.s.activeSessions.delete(session); + }); + return session; + } + + /** + * A convenience method for creating and handling the clean up of a ClientSession. + * The session will always be ended when the executor finishes. + * + * @param executor - An executor function that all operations using the provided session must be invoked in + * @param options - optional settings for the session + */ + async withSession(executor: WithSessionCallback): Promise; + async withSession( + options: ClientSessionOptions, + executor: WithSessionCallback + ): Promise; + async withSession( + optionsOrExecutor: ClientSessionOptions | WithSessionCallback, + executor?: WithSessionCallback + ): Promise { + const options = { + // Always define an owner + owner: Symbol(), + // If it's an object inherit the options + ...(typeof optionsOrExecutor === 'object' ? optionsOrExecutor : {}) + }; + + const withSessionCallback = + typeof optionsOrExecutor === 'function' ? optionsOrExecutor : executor; + + if (withSessionCallback == null) { + throw new MongoInvalidArgumentError('Missing required callback parameter'); + } + + const session = this.startSession(options); + + try { + return await withSessionCallback(session); + } finally { + try { + await session.endSession(); + } catch (error) { + squashError(error); + } + } + } + + /** + * Create a new Change Stream, watching for new changes (insertions, updates, + * replacements, deletions, and invalidations) in this cluster. Will ignore all + * changes to system collections, as well as the local, admin, and config databases. + * + * @remarks + * watch() accepts two generic arguments for distinct use cases: + * - The first is to provide the schema that may be defined for all the data within the current cluster + * - The second is to override the shape of the change stream document entirely, if it is not provided the type will default to ChangeStreamDocument of the first argument + * + * @remarks + * When `timeoutMS` is configured for a change stream, it will have different behaviour depending + * on whether the change stream is in iterator mode or emitter mode. In both cases, a change + * stream will time out if it does not receive a change event within `timeoutMS` of the last change + * event. + * + * Note that if a change stream is consistently timing out when watching a collection, database or + * client that is being changed, then this may be due to the server timing out before it can finish + * processing the existing oplog. To address this, restart the change stream with a higher + * `timeoutMS`. + * + * If the change stream times out the initial aggregate operation to establish the change stream on + * the server, then the client will close the change stream. If the getMore calls to the server + * time out, then the change stream will be left open, but will throw a MongoOperationTimeoutError + * when in iterator mode and emit an error event that returns a MongoOperationTimeoutError in + * emitter mode. + * + * To determine whether or not the change stream is still open following a timeout, check the + * {@link ChangeStream.closed} getter. + * + * @example + * In iterator mode, if a next() call throws a timeout error, it will attempt to resume the change stream. + * The next call can just be retried after this succeeds. + * ```ts + * const changeStream = collection.watch([], { timeoutMS: 100 }); + * try { + * await changeStream.next(); + * } catch (e) { + * if (e instanceof MongoOperationTimeoutError && !changeStream.closed) { + * await changeStream.next(); + * } + * throw e; + * } + * ``` + * + * @example + * In emitter mode, if the change stream goes `timeoutMS` without emitting a change event, it will + * emit an error event that returns a MongoOperationTimeoutError, but will not close the change + * stream unless the resume attempt fails. There is no need to re-establish change listeners as + * this will automatically continue emitting change events once the resume attempt completes. + * + * ```ts + * const changeStream = collection.watch([], { timeoutMS: 100 }); + * changeStream.on('change', console.log); + * changeStream.on('error', e => { + * if (e instanceof MongoOperationTimeoutError && !changeStream.closed) { + * // do nothing + * } else { + * changeStream.close(); + * } + * }); + * ``` + * @param pipeline - An array of {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents. + * @param options - Optional settings for the command + * @typeParam TSchema - Type of the data being detected by the change stream + * @typeParam TChange - Type of the whole change stream document emitted + */ + watch< + TSchema extends Document = Document, + TChange extends Document = ChangeStreamDocument + >(pipeline: Document[] = [], options: ChangeStreamOptions = {}): ChangeStream { + // Allow optionally not specifying a pipeline + if (!Array.isArray(pipeline)) { + options = pipeline; + pipeline = []; + } + + return new ChangeStream(this, pipeline, resolveOptions(this, options)); + } +} + +/** + * Parsed Mongo Client Options. + * + * User supplied options are documented by `MongoClientOptions`. + * + * **NOTE:** The client's options parsing is subject to change to support new features. + * This type is provided to aid with inspection of options after parsing, it should not be relied upon programmatically. + * + * Options are sourced from: + * - connection string + * - options object passed to the MongoClient constructor + * - file system (ex. tls settings) + * - environment variables + * - DNS SRV records and TXT records + * + * Not all options may be present after client construction as some are obtained from asynchronous operations. + * + * @public + */ +export interface MongoOptions + extends Required< + Pick< + MongoClientOptions, + | 'maxAdaptiveRetries' + | 'enableOverloadRetargeting' + | 'autoEncryption' + | 'connectTimeoutMS' + | 'directConnection' + | 'driverInfo' + | 'forceServerObjectId' + | 'minHeartbeatFrequencyMS' + | 'heartbeatFrequencyMS' + | 'localThresholdMS' + | 'maxConnecting' + | 'maxIdleTimeMS' + | 'maxPoolSize' + | 'minPoolSize' + | 'monitorCommands' + | 'noDelay' + | 'pkFactory' + | 'raw' + | 'replicaSet' + | 'retryReads' + | 'retryWrites' + | 'serverSelectionTimeoutMS' + | 'socketTimeoutMS' + | 'srvMaxHosts' + | 'srvServiceName' + | 'tlsAllowInvalidCertificates' + | 'tlsAllowInvalidHostnames' + | 'tlsInsecure' + | 'waitQueueTimeoutMS' + | 'zlibCompressionLevel' + > + >, + SupportedNodeConnectionOptions { + appName?: string; + hosts: HostAddress[]; + srvHost?: string; + credentials?: MongoCredentials; + readPreference: ReadPreference; + readConcern: ReadConcern; + loadBalanced: boolean; + directConnection: boolean; + serverApi: ServerApi; + compressors: CompressorName[]; + writeConcern: WriteConcern; + dbName: string; + /** @internal */ + metadata: Promise; + /** @internal */ + autoEncrypter?: AutoEncrypter; + /** @internal */ + tokenCache?: TokenCache; + proxyHost?: string; + proxyPort?: number; + proxyUsername?: string; + proxyPassword?: string; + serverMonitoringMode: ServerMonitoringMode; + /** @internal */ + connectionType?: typeof Connection; + /** @internal */ + authProviders: MongoClientAuthProviders; + /** @internal */ + encrypter: Encrypter; + /** @internal */ + userSpecifiedAuthSource: boolean; + /** @internal */ + userSpecifiedReplicaSet: boolean; + + /** + * # NOTE ABOUT TLS Options + * + * If `tls` is provided as an option, it is equivalent to setting the `ssl` option. + * + * NodeJS native TLS options are passed through to the socket and retain their original types. + * + * ### Additional options: + * + * | nodejs native option | driver spec equivalent option name | driver option type | + * |:----------------------|:----------------------------------------------|:-------------------| + * | `ca` | `tlsCAFile` | `string` | + * | `crl` | `tlsCRLFile` | `string` | + * | `cert` | `tlsCertificateKeyFile` | `string` | + * | `key` | `tlsCertificateKeyFile` | `string` | + * | `passphrase` | `tlsCertificateKeyFilePassword` | `string` | + * | `rejectUnauthorized` | `tlsAllowInvalidCertificates` | `boolean` | + * | `checkServerIdentity` | `tlsAllowInvalidHostnames` | `boolean` | + * | see note below | `tlsInsecure` | `boolean` | + * + * If `tlsInsecure` is set to `true`, then it will set the node native options `checkServerIdentity` + * to a no-op and `rejectUnauthorized` to `false`. + * + * If `tlsInsecure` is set to `false`, then it will set the node native options `checkServerIdentity` + * to a no-op and `rejectUnauthorized` to the inverse value of `tlsAllowInvalidCertificates`. If + * `tlsAllowInvalidCertificates` is not set, then `rejectUnauthorized` will be set to `true`. + * + * ### Note on `tlsCAFile`, `tlsCertificateKeyFile` and `tlsCRLFile` + * + * The files specified by the paths passed in to the `tlsCAFile`, `tlsCertificateKeyFile` and `tlsCRLFile` + * fields are read lazily on the first call to `MongoClient.connect`. Once these files have been read and + * the `ca`, `cert`, `crl` and `key` fields are populated, they will not be read again on subsequent calls to + * `MongoClient.connect`. As a result, until the first call to `MongoClient.connect`, the `ca`, + * `cert`, `crl` and `key` fields will be undefined. + */ + tls: boolean; + tlsCAFile?: string; + tlsCRLFile?: string; + tlsCertificateKeyFile?: string; + + /** + * @internal + * TODO: NODE-5671 - remove internal flag + */ + mongoLoggerOptions: MongoLoggerOptions; + /** + * @internal + * TODO: NODE-5671 - remove internal flag + */ + mongodbLogPath?: 'stderr' | 'stdout' | MongoDBLogWritable; + timeoutMS?: number; + /** @internal */ + __skipPingOnConnect?: boolean; + + /** @internal */ + runtime: Runtime; +} diff --git a/gateway/node_modules/mongodb/src/mongo_client_auth_providers.ts b/gateway/node_modules/mongodb/src/mongo_client_auth_providers.ts new file mode 100644 index 0000000000000000000000000000000000000000..f458726db762f05ff11aa593ba05a09f5771ca78 --- /dev/null +++ b/gateway/node_modules/mongodb/src/mongo_client_auth_providers.ts @@ -0,0 +1,87 @@ +import { type AuthProvider } from './cmap/auth/auth_provider'; +import { GSSAPI } from './cmap/auth/gssapi'; +import { type AuthMechanismProperties } from './cmap/auth/mongo_credentials'; +import { MongoDBAWS } from './cmap/auth/mongodb_aws'; +import { MongoDBOIDC, OIDC_WORKFLOWS, type Workflow } from './cmap/auth/mongodb_oidc'; +import { AutomatedCallbackWorkflow } from './cmap/auth/mongodb_oidc/automated_callback_workflow'; +import { HumanCallbackWorkflow } from './cmap/auth/mongodb_oidc/human_callback_workflow'; +import { TokenCache } from './cmap/auth/mongodb_oidc/token_cache'; +import { Plain } from './cmap/auth/plain'; +import { AuthMechanism } from './cmap/auth/providers'; +import { ScramSHA1, ScramSHA256 } from './cmap/auth/scram'; +import { X509 } from './cmap/auth/x509'; +import { MongoInvalidArgumentError } from './error'; + +/** @internal */ +const AUTH_PROVIDERS = new Map< + AuthMechanism | string, + (authMechanismProperties: AuthMechanismProperties) => AuthProvider +>([ + [ + AuthMechanism.MONGODB_AWS, + ({ AWS_CREDENTIAL_PROVIDER }) => new MongoDBAWS(AWS_CREDENTIAL_PROVIDER) + ], + [AuthMechanism.MONGODB_GSSAPI, () => new GSSAPI()], + [AuthMechanism.MONGODB_OIDC, properties => new MongoDBOIDC(getWorkflow(properties))], + [AuthMechanism.MONGODB_PLAIN, () => new Plain()], + [AuthMechanism.MONGODB_SCRAM_SHA1, () => new ScramSHA1()], + [AuthMechanism.MONGODB_SCRAM_SHA256, () => new ScramSHA256()], + [AuthMechanism.MONGODB_X509, () => new X509()] +]); + +/** + * Create a set of providers per client + * to avoid sharing the provider's cache between different clients. + * @internal + */ +export class MongoClientAuthProviders { + private existingProviders: Map = new Map(); + + /** + * Get or create an authentication provider based on the provided mechanism. + * We don't want to create all providers at once, as some providers may not be used. + * @param name - The name of the provider to get or create. + * @param credentials - The credentials. + * @returns The provider. + * @throws MongoInvalidArgumentError if the mechanism is not supported. + * @internal + */ + getOrCreateProvider( + name: AuthMechanism | string, + authMechanismProperties: AuthMechanismProperties + ): AuthProvider { + const authProvider = this.existingProviders.get(name); + if (authProvider) { + return authProvider; + } + + const providerFunction = AUTH_PROVIDERS.get(name); + if (!providerFunction) { + throw new MongoInvalidArgumentError(`authMechanism ${name} not supported`); + } + + const provider = providerFunction(authMechanismProperties); + this.existingProviders.set(name, provider); + return provider; + } +} + +/** + * Gets either a device workflow or callback workflow. + */ +function getWorkflow(authMechanismProperties: AuthMechanismProperties): Workflow { + if (authMechanismProperties.OIDC_HUMAN_CALLBACK) { + return new HumanCallbackWorkflow(new TokenCache(), authMechanismProperties.OIDC_HUMAN_CALLBACK); + } else if (authMechanismProperties.OIDC_CALLBACK) { + return new AutomatedCallbackWorkflow(new TokenCache(), authMechanismProperties.OIDC_CALLBACK); + } else { + const environment = authMechanismProperties.ENVIRONMENT; + const workflow = OIDC_WORKFLOWS.get(environment)?.(); + if (!workflow) { + throw new MongoInvalidArgumentError( + `Could not load workflow for environment ${authMechanismProperties.ENVIRONMENT}` + ); + } + return workflow; + } +} diff --git a/gateway/node_modules/mongodb/src/mongo_logger.ts b/gateway/node_modules/mongodb/src/mongo_logger.ts new file mode 100644 index 0000000000000000000000000000000000000000..bb54896df225c26385c0eb8eab2d837e85533da2 --- /dev/null +++ b/gateway/node_modules/mongodb/src/mongo_logger.ts @@ -0,0 +1,1081 @@ +import * as process from 'process'; +import { inspect } from 'util'; + +import { + type Binary, + type BSONRegExp, + type BSONSymbol, + type Code, + type DBRef, + type Decimal128, + type Document, + type Double, + EJSON, + type EJSONOptions, + type Int32, + type Long, + type MaxKey, + type MinKey, + type ObjectId, + type Timestamp +} from './bson'; +import type { CommandStartedEvent } from './cmap/command_monitoring_events'; +import type { + ConnectionCheckedInEvent, + ConnectionCheckedOutEvent, + ConnectionCheckOutFailedEvent, + ConnectionCheckOutStartedEvent, + ConnectionClosedEvent, + ConnectionCreatedEvent, + ConnectionPoolClearedEvent, + ConnectionPoolClosedEvent, + ConnectionPoolCreatedEvent, + ConnectionPoolReadyEvent, + ConnectionReadyEvent +} from './cmap/connection_pool_events'; +import { + COMMAND_FAILED, + COMMAND_STARTED, + COMMAND_SUCCEEDED, + CONNECTION_CHECK_OUT_FAILED, + CONNECTION_CHECK_OUT_STARTED, + CONNECTION_CHECKED_IN, + CONNECTION_CHECKED_OUT, + CONNECTION_CLOSED, + CONNECTION_CREATED, + CONNECTION_POOL_CLEARED, + CONNECTION_POOL_CLOSED, + CONNECTION_POOL_CREATED, + CONNECTION_POOL_READY, + CONNECTION_READY, + SERVER_CLOSED, + SERVER_HEARTBEAT_FAILED, + SERVER_HEARTBEAT_STARTED, + SERVER_HEARTBEAT_SUCCEEDED, + SERVER_OPENING, + SERVER_SELECTION_FAILED, + SERVER_SELECTION_STARTED, + SERVER_SELECTION_SUCCEEDED, + TOPOLOGY_CLOSED, + TOPOLOGY_DESCRIPTION_CHANGED, + TOPOLOGY_OPENING, + WAITING_FOR_SUITABLE_SERVER +} from './constants'; +import type { + ServerClosedEvent, + ServerOpeningEvent, + TopologyClosedEvent, + TopologyDescriptionChangedEvent, + TopologyOpeningEvent +} from './sdam/events'; +import type { + ServerSelectionEvent, + ServerSelectionFailedEvent, + ServerSelectionStartedEvent, + ServerSelectionSucceededEvent, + WaitingForSuitableServerEvent +} from './sdam/server_selection_events'; +import { HostAddress, isPromiseLike, isUint8Array, parseUnsignedInteger } from './utils'; + +/** + * @public + * Severity levels align with unix syslog. + * Most typical driver functions will log to debug. + */ +export const SeverityLevel = Object.freeze({ + EMERGENCY: 'emergency', + ALERT: 'alert', + CRITICAL: 'critical', + ERROR: 'error', + WARNING: 'warn', + NOTICE: 'notice', + INFORMATIONAL: 'info', + DEBUG: 'debug', + TRACE: 'trace', + OFF: 'off' +} as const); + +/** @internal */ +export const DEFAULT_MAX_DOCUMENT_LENGTH = 1000; +/** @public */ +export type SeverityLevel = (typeof SeverityLevel)[keyof typeof SeverityLevel]; + +/** @internal */ +class SeverityLevelMap extends Map { + constructor(entries: [SeverityLevel | number, SeverityLevel | number][]) { + const newEntries: [number | SeverityLevel, SeverityLevel | number][] = []; + for (const [level, value] of entries) { + newEntries.push([value, level]); + } + + newEntries.push(...entries); + super(newEntries); + } + + getNumericSeverityLevel(severity: SeverityLevel): number { + return this.get(severity) as number; + } + + getSeverityLevelName(level: number): SeverityLevel | undefined { + return this.get(level) as SeverityLevel | undefined; + } +} + +/** @internal */ +export const SEVERITY_LEVEL_MAP = new SeverityLevelMap([ + [SeverityLevel.OFF, -Infinity], + [SeverityLevel.EMERGENCY, 0], + [SeverityLevel.ALERT, 1], + [SeverityLevel.CRITICAL, 2], + [SeverityLevel.ERROR, 3], + [SeverityLevel.WARNING, 4], + [SeverityLevel.NOTICE, 5], + [SeverityLevel.INFORMATIONAL, 6], + [SeverityLevel.DEBUG, 7], + [SeverityLevel.TRACE, 8] +]); + +/** @public */ +export const MongoLoggableComponent = Object.freeze({ + COMMAND: 'command', + TOPOLOGY: 'topology', + SERVER_SELECTION: 'serverSelection', + CONNECTION: 'connection', + CLIENT: 'client' +} as const); + +/** @public */ +export type MongoLoggableComponent = + (typeof MongoLoggableComponent)[keyof typeof MongoLoggableComponent]; + +/** @internal */ +export interface MongoLoggerEnvOptions { + /** Severity level for command component */ + MONGODB_LOG_COMMAND?: string; + /** Severity level for topology component */ + MONGODB_LOG_TOPOLOGY?: string; + /** Severity level for server selection component */ + MONGODB_LOG_SERVER_SELECTION?: string; + /** Severity level for CMAP */ + MONGODB_LOG_CONNECTION?: string; + /** Severity level for client */ + MONGODB_LOG_CLIENT?: string; + /** Default severity level to be if any of the above are unset */ + MONGODB_LOG_ALL?: string; + /** Max length of embedded EJSON docs. Setting to 0 disables truncation. Defaults to 1000. */ + MONGODB_LOG_MAX_DOCUMENT_LENGTH?: string; + /** Destination for log messages. Must be 'stderr', 'stdout'. Defaults to 'stderr'. */ + MONGODB_LOG_PATH?: string; +} + +/** @public */ +export interface LogComponentSeveritiesClientOptions { + /** Optional severity level for command component */ + command?: SeverityLevel; + /** Optional severity level for topology component */ + topology?: SeverityLevel; + /** Optional severity level for server selection component */ + serverSelection?: SeverityLevel; + /** Optional severity level for connection component */ + connection?: SeverityLevel; + /** Optional severity level for client component */ + client?: SeverityLevel; + /** Optional default severity level to be used if any of the above are unset */ + default?: SeverityLevel; +} + +/** @internal */ +export interface MongoLoggerMongoClientOptions { + /** Destination for log messages */ + mongodbLogPath?: 'stdout' | 'stderr' | MongoDBLogWritable; + /** Severity levels for logger components */ + mongodbLogComponentSeverities?: LogComponentSeveritiesClientOptions; + /** Max length of embedded EJSON docs. Setting to 0 disables truncation. Defaults to 1000. */ + mongodbLogMaxDocumentLength?: number; +} + +/** @internal */ +export interface MongoLoggerOptions { + componentSeverities: { + /** Severity level for command component */ + command: SeverityLevel; + /** Severity level for topology component */ + topology: SeverityLevel; + /** Severity level for server selection component */ + serverSelection: SeverityLevel; + /** Severity level for connection component */ + connection: SeverityLevel; + /** Severity level for client component */ + client: SeverityLevel; + /** Default severity level to be used if any of the above are unset */ + default: SeverityLevel; + }; + /** Max length of embedded EJSON docs. Setting to 0 disables truncation. Defaults to 1000. */ + maxDocumentLength: number; + /** Destination for log messages. */ + logDestination: MongoDBLogWritable; + /** For internal check to see if error should stop logging. */ + logDestinationIsStdErr: boolean; +} + +/** + * Parses a string as one of SeverityLevel + * @internal + * + * @param s - the value to be parsed + * @returns one of SeverityLevel if value can be parsed as such, otherwise null + */ +export function parseSeverityFromString(s?: string): SeverityLevel | null { + const validSeverities: string[] = Object.values(SeverityLevel); + const lowerSeverity = s?.toLowerCase(); + + if (lowerSeverity != null && validSeverities.includes(lowerSeverity)) { + return lowerSeverity as SeverityLevel; + } + + return null; +} + +/** @internal */ +export function createStdioLogger(stream: { + write: NodeJS.WriteStream['write']; +}): MongoDBLogWritable { + return { + write: (log: Log): Promise => { + return new Promise((resolve, reject) => { + const logLine = inspect(log, { compact: true, breakLength: Infinity }); + stream.write(`${logLine}\n`, 'utf-8', error => { + if (error) return reject(error); + resolve(true); + }); + }); + } + }; +} + +/** + * resolves the MONGODB_LOG_PATH and mongodbLogPath options from the environment and the + * mongo client options respectively. The mongodbLogPath can be either 'stdout', 'stderr', a NodeJS + * Writable or an object which has a `write` method with the signature: + * ```ts + * write(log: Log): void + * ``` + * + * @returns the MongoDBLogWritable object to write logs to + */ +function resolveLogPath( + { MONGODB_LOG_PATH }: MongoLoggerEnvOptions, + { mongodbLogPath }: MongoLoggerMongoClientOptions +): { mongodbLogPath: MongoDBLogWritable; mongodbLogPathIsStdErr: boolean } { + if (typeof mongodbLogPath === 'string' && /^stderr$/i.test(mongodbLogPath)) { + return { mongodbLogPath: createStdioLogger(process.stderr), mongodbLogPathIsStdErr: true }; + } + if (typeof mongodbLogPath === 'string' && /^stdout$/i.test(mongodbLogPath)) { + return { mongodbLogPath: createStdioLogger(process.stdout), mongodbLogPathIsStdErr: false }; + } + + if (typeof mongodbLogPath === 'object' && typeof mongodbLogPath?.write === 'function') { + return { mongodbLogPath: mongodbLogPath, mongodbLogPathIsStdErr: false }; + } + + if (MONGODB_LOG_PATH && /^stderr$/i.test(MONGODB_LOG_PATH)) { + return { mongodbLogPath: createStdioLogger(process.stderr), mongodbLogPathIsStdErr: true }; + } + if (MONGODB_LOG_PATH && /^stdout$/i.test(MONGODB_LOG_PATH)) { + return { mongodbLogPath: createStdioLogger(process.stdout), mongodbLogPathIsStdErr: false }; + } + + return { mongodbLogPath: createStdioLogger(process.stderr), mongodbLogPathIsStdErr: true }; +} + +function resolveSeverityConfiguration( + clientOption: string | undefined, + environmentOption: string | undefined, + defaultSeverity: SeverityLevel +): SeverityLevel { + return ( + parseSeverityFromString(clientOption) ?? + parseSeverityFromString(environmentOption) ?? + defaultSeverity + ); +} + +/** @public */ +export interface Log extends Record { + t: Date; + c: MongoLoggableComponent; + s: SeverityLevel; + message?: string; +} + +/** + * @public + * + * A custom destination for structured logging messages. + */ +export interface MongoDBLogWritable { + /** + * This function will be called for every enabled log message. + * + * It can be sync or async: + * - If it is synchronous it will block the driver from proceeding until this method returns. + * - If it is asynchronous the driver will not await the returned promise. It will attach fulfillment handling (`.then`). + * If the promise rejects the logger will write an error message to stderr and stop functioning. + * If the promise resolves the driver proceeds to the next log message (or waits for new ones to occur). + * + * Tips: + * - We recommend writing an async `write` function that _never_ rejects. + * Instead handle logging errors as necessary to your use case and make the write function a noop, until it can be recovered. + * - The Log messages are structured but **subject to change** since the intended purpose is informational. + * Program against this defensively and err on the side of stringifying whatever is passed in to write in some form or another. + * + */ + write(log: Log): PromiseLike | unknown; +} + +function compareSeverity(s0: SeverityLevel, s1: SeverityLevel): 1 | 0 | -1 { + const s0Num = SEVERITY_LEVEL_MAP.getNumericSeverityLevel(s0); + const s1Num = SEVERITY_LEVEL_MAP.getNumericSeverityLevel(s1); + + return s0Num < s1Num ? -1 : s0Num > s1Num ? 1 : 0; +} + +/** + * @internal + * Must be separate from Events API due to differences in spec requirements for logging a command success + */ +export type LoggableCommandSucceededEvent = { + address: string; + connectionId?: string | number; + requestId: number; + duration: number; + commandName: string; + reply: Document | undefined; + serviceId?: ObjectId; + name: typeof COMMAND_SUCCEEDED; + serverConnectionId: bigint | null; + databaseName: string; +}; + +/** + * @internal + * Must be separate from Events API due to differences in spec requirements for logging a command failure + */ +export type LoggableCommandFailedEvent = { + address: string; + connectionId?: string | number; + requestId: number; + duration: number; + commandName: string; + failure: Error; + serviceId?: ObjectId; + name: typeof COMMAND_FAILED; + serverConnectionId: bigint | null; + databaseName: string; +}; + +/** + * @internal + * Must be separate from Events API due to differences in spec requirements for logging server heartbeat beginning + */ +export type LoggableServerHeartbeatStartedEvent = { + topologyId: number; + awaited: boolean; + connectionId: string; + name: typeof SERVER_HEARTBEAT_STARTED; +}; + +/** + * @internal + * Must be separate from Events API due to differences in spec requirements for logging server heartbeat success + */ +export type LoggableServerHeartbeatSucceededEvent = { + topologyId: number; + awaited: boolean; + connectionId: string; + reply: Document; + serverConnectionId: number | ''; + duration: number; + name: typeof SERVER_HEARTBEAT_SUCCEEDED; +}; + +/** + * @internal + * Must be separate from Events API due to differences in spec requirements for logging server heartbeat failure + */ +export type LoggableServerHeartbeatFailedEvent = { + topologyId: number; + awaited: boolean; + connectionId: string; + failure: Error; + duration: number; + name: typeof SERVER_HEARTBEAT_FAILED; +}; + +type SDAMLoggableEvent = + | ServerClosedEvent + | LoggableServerHeartbeatFailedEvent + | LoggableServerHeartbeatStartedEvent + | LoggableServerHeartbeatSucceededEvent + | ServerOpeningEvent + | TopologyClosedEvent + | TopologyDescriptionChangedEvent + | TopologyOpeningEvent; + +/** @internal */ +export type LoggableEvent = + | ServerSelectionStartedEvent + | ServerSelectionFailedEvent + | ServerSelectionSucceededEvent + | WaitingForSuitableServerEvent + | CommandStartedEvent + | LoggableCommandSucceededEvent + | LoggableCommandFailedEvent + | ConnectionPoolCreatedEvent + | ConnectionPoolReadyEvent + | ConnectionPoolClosedEvent + | ConnectionPoolClearedEvent + | ConnectionCreatedEvent + | ConnectionReadyEvent + | ConnectionClosedEvent + | ConnectionCheckedInEvent + | ConnectionCheckedOutEvent + | ConnectionCheckOutStartedEvent + | ConnectionCheckOutFailedEvent + | ServerClosedEvent + | LoggableServerHeartbeatFailedEvent + | LoggableServerHeartbeatStartedEvent + | LoggableServerHeartbeatSucceededEvent + | ServerOpeningEvent + | TopologyClosedEvent + | TopologyDescriptionChangedEvent + | TopologyOpeningEvent; + +/** @internal */ +export interface LogConvertible extends Record { + toLog(): Record; +} + +type BSONObject = + | BSONRegExp + | BSONSymbol + | Code + | DBRef + | Decimal128 + | Double + | Int32 + | Long + | MaxKey + | MinKey + | ObjectId + | Timestamp + | Binary; +/** @internal */ +export function stringifyWithMaxLen( + value: any, + maxDocumentLength: number, + options: EJSONOptions = {} +): string { + let strToTruncate = ''; + + let currentLength = 0; + const maxDocumentLengthEnsurer = function maxDocumentLengthEnsurer(key: string, value: any) { + if (currentLength >= maxDocumentLength) { + return undefined; + } + // Account for root document + if (key === '') { + // Account for starting brace + currentLength += 1; + return value; + } + + // +4 accounts for 2 quotation marks, colon and comma after value + // Note that this potentially undercounts since it does not account for escape sequences which + // will have an additional backslash added to them once passed through JSON.stringify. + currentLength += key.length + 4; + + if (value == null) return value; + + switch (typeof value) { + case 'string': + // +2 accounts for quotes + // Note that this potentially undercounts similarly to the key length calculation + currentLength += value.length + 2; + break; + case 'number': + case 'bigint': + currentLength += String(value).length; + break; + case 'boolean': + currentLength += value ? 4 : 5; + break; + case 'object': + if (isUint8Array(value)) { + // '{"$binary":{"base64":"","subType":"XX"}}' + // This is an estimate based on the fact that the base64 is approximately 1.33x the length of + // the actual binary sequence https://en.wikipedia.org/wiki/Base64 + currentLength += (22 + value.byteLength + value.byteLength * 0.33 + 18) | 0; + } else if ('_bsontype' in value) { + const v = value as BSONObject; + switch (v._bsontype) { + case 'Int32': + currentLength += String(v.value).length; + break; + case 'Double': + // Account for representing integers as .0 + currentLength += + (v.value | 0) === v.value ? String(v.value).length + 2 : String(v.value).length; + break; + case 'Long': + currentLength += v.toString().length; + break; + case 'ObjectId': + // '{"$oid":"XXXXXXXXXXXXXXXXXXXXXXXX"}' + currentLength += 35; + break; + case 'MaxKey': + case 'MinKey': + // '{"$maxKey":1}' or '{"$minKey":1}' + currentLength += 13; + break; + case 'Binary': + // '{"$binary":{"base64":"","subType":"XX"}}' + // This is an estimate based on the fact that the base64 is approximately 1.33x the length of + // the actual binary sequence https://en.wikipedia.org/wiki/Base64 + currentLength += (22 + value.position + value.position * 0.33 + 18) | 0; + break; + case 'Timestamp': + // '{"$timestamp":{"t":,"i":}}' + currentLength += 19 + String(v.t).length + 5 + String(v.i).length + 2; + break; + case 'Code': + // '{"$code":""}' or '{"$code":"","$scope":}' + if (v.scope == null) { + currentLength += v.code.length + 10 + 2; + } else { + // Ignoring actual scope object, so this undercounts by a significant amount + currentLength += v.code.length + 10 + 11; + } + break; + case 'BSONRegExp': + // '{"$regularExpression":{"pattern":"","options":""}}' + currentLength += 34 + v.pattern.length + 13 + v.options.length + 3; + break; + } + } + } + return value; + }; + + if (typeof value === 'string') { + strToTruncate = value; + } else if (typeof value === 'function') { + strToTruncate = value.name; + } else { + try { + if (maxDocumentLength !== 0) { + strToTruncate = EJSON.stringify(value, maxDocumentLengthEnsurer, 0, options); + } else { + strToTruncate = EJSON.stringify(value, options); + } + } catch (e) { + strToTruncate = `Extended JSON serialization failed with: ${e.message}`; + } + } + + // handle truncation that occurs in the middle of multi-byte codepoints + if ( + maxDocumentLength !== 0 && + strToTruncate.length > maxDocumentLength && + strToTruncate.charCodeAt(maxDocumentLength - 1) !== + strToTruncate.codePointAt(maxDocumentLength - 1) + ) { + maxDocumentLength--; + if (maxDocumentLength === 0) { + return ''; + } + } + + return maxDocumentLength !== 0 && strToTruncate.length > maxDocumentLength + ? `${strToTruncate.slice(0, maxDocumentLength)}...` + : strToTruncate; +} + +/** @internal */ +export type Loggable = LoggableEvent | LogConvertible; + +function isLogConvertible(obj: Loggable): obj is LogConvertible { + const objAsLogConvertible = obj as LogConvertible; + // eslint-disable-next-line no-restricted-syntax + return objAsLogConvertible.toLog !== undefined && typeof objAsLogConvertible.toLog === 'function'; +} + +function attachServerSelectionFields( + log: Record, + serverSelectionEvent: ServerSelectionEvent, + maxDocumentLength: number = DEFAULT_MAX_DOCUMENT_LENGTH +) { + const { selector, operation, topologyDescription, message } = serverSelectionEvent; + log.selector = stringifyWithMaxLen(selector, maxDocumentLength); + log.operation = operation; + log.topologyDescription = stringifyWithMaxLen(topologyDescription, maxDocumentLength); + log.message = message; + + return log; +} + +function attachCommandFields( + log: Record, + commandEvent: CommandStartedEvent | LoggableCommandSucceededEvent | LoggableCommandFailedEvent +) { + log.commandName = commandEvent.commandName; + log.requestId = commandEvent.requestId; + log.driverConnectionId = commandEvent.connectionId; + const { host, port } = HostAddress.fromString(commandEvent.address).toHostPort(); + log.serverHost = host; + log.serverPort = port; + if (commandEvent?.serviceId) { + log.serviceId = commandEvent.serviceId.toHexString(); + } + log.databaseName = commandEvent.databaseName; + log.serverConnectionId = commandEvent.serverConnectionId; + + return log; +} + +function attachConnectionFields(log: Record, event: any) { + const { host, port } = HostAddress.fromString(event.address).toHostPort(); + log.serverHost = host; + log.serverPort = port; + + return log; +} + +function attachSDAMFields(log: Record, sdamEvent: SDAMLoggableEvent) { + log.topologyId = sdamEvent.topologyId; + return log; +} + +function attachServerHeartbeatFields( + log: Record, + serverHeartbeatEvent: + | LoggableServerHeartbeatFailedEvent + | LoggableServerHeartbeatStartedEvent + | LoggableServerHeartbeatSucceededEvent +) { + const { awaited, connectionId } = serverHeartbeatEvent; + log.awaited = awaited; + log.driverConnectionId = serverHeartbeatEvent.connectionId; + const { host, port } = HostAddress.fromString(connectionId).toHostPort(); + log.serverHost = host; + log.serverPort = port; + return log; +} + +/** @internal */ +export function defaultLogTransform( + logObject: LoggableEvent | Record, + maxDocumentLength: number = DEFAULT_MAX_DOCUMENT_LENGTH +): Omit { + let log: Omit = Object.create(null); + + switch (logObject.name) { + case SERVER_SELECTION_STARTED: + log = attachServerSelectionFields(log, logObject, maxDocumentLength); + return log; + case SERVER_SELECTION_FAILED: + log = attachServerSelectionFields(log, logObject, maxDocumentLength); + log.failure = logObject.failure?.message; + return log; + case SERVER_SELECTION_SUCCEEDED: + log = attachServerSelectionFields(log, logObject, maxDocumentLength); + log.serverHost = logObject.serverHost; + log.serverPort = logObject.serverPort; + return log; + case WAITING_FOR_SUITABLE_SERVER: + log = attachServerSelectionFields(log, logObject, maxDocumentLength); + log.remainingTimeMS = logObject.remainingTimeMS; + return log; + case COMMAND_STARTED: + log = attachCommandFields(log, logObject); + log.message = 'Command started'; + log.command = stringifyWithMaxLen(logObject.command, maxDocumentLength, { relaxed: true }); + log.databaseName = logObject.databaseName; + return log; + case COMMAND_SUCCEEDED: + log = attachCommandFields(log, logObject); + log.message = 'Command succeeded'; + log.durationMS = logObject.duration; + log.reply = stringifyWithMaxLen(logObject.reply, maxDocumentLength, { relaxed: true }); + return log; + case COMMAND_FAILED: + log = attachCommandFields(log, logObject); + log.message = 'Command failed'; + log.durationMS = logObject.duration; + log.failure = logObject.failure?.message ?? '(redacted)'; + return log; + case CONNECTION_POOL_CREATED: + log = attachConnectionFields(log, logObject); + log.message = 'Connection pool created'; + if (logObject.options) { + const { maxIdleTimeMS, minPoolSize, maxPoolSize, maxConnecting, waitQueueTimeoutMS } = + logObject.options; + log = { + ...log, + maxIdleTimeMS, + minPoolSize, + maxPoolSize, + maxConnecting, + waitQueueTimeoutMS + }; + } + return log; + case CONNECTION_POOL_READY: + log = attachConnectionFields(log, logObject); + log.message = 'Connection pool ready'; + return log; + case CONNECTION_POOL_CLEARED: + log = attachConnectionFields(log, logObject); + log.message = 'Connection pool cleared'; + if (logObject.serviceId?._bsontype === 'ObjectId') { + log.serviceId = logObject.serviceId?.toHexString(); + } + return log; + case CONNECTION_POOL_CLOSED: + log = attachConnectionFields(log, logObject); + log.message = 'Connection pool closed'; + return log; + case CONNECTION_CREATED: + log = attachConnectionFields(log, logObject); + log.message = 'Connection created'; + log.driverConnectionId = logObject.connectionId; + return log; + case CONNECTION_READY: + log = attachConnectionFields(log, logObject); + log.message = 'Connection ready'; + log.driverConnectionId = logObject.connectionId; + log.durationMS = logObject.durationMS; + return log; + case CONNECTION_CLOSED: + log = attachConnectionFields(log, logObject); + log.message = 'Connection closed'; + log.driverConnectionId = logObject.connectionId; + switch (logObject.reason) { + case 'stale': + log.reason = 'Connection became stale because the pool was cleared'; + break; + case 'idle': + log.reason = + 'Connection has been available but unused for longer than the configured max idle time'; + break; + case 'error': + log.reason = 'An error occurred while using the connection'; + if (logObject.error) { + log.error = logObject.error; + } + break; + case 'poolClosed': + log.reason = 'Connection pool was closed'; + break; + default: + log.reason = `Unknown close reason: ${logObject.reason}`; + } + return log; + case CONNECTION_CHECK_OUT_STARTED: + log = attachConnectionFields(log, logObject); + log.message = 'Connection checkout started'; + return log; + case CONNECTION_CHECK_OUT_FAILED: + log = attachConnectionFields(log, logObject); + log.message = 'Connection checkout failed'; + switch (logObject.reason) { + case 'poolClosed': + log.reason = 'Connection pool was closed'; + break; + case 'timeout': + log.reason = 'Wait queue timeout elapsed without a connection becoming available'; + break; + case 'connectionError': + log.reason = 'An error occurred while trying to establish a new connection'; + if (logObject.error) { + log.error = logObject.error; + } + break; + default: + log.reason = `Unknown close reason: ${logObject.reason}`; + } + log.durationMS = logObject.durationMS; + return log; + case CONNECTION_CHECKED_OUT: + log = attachConnectionFields(log, logObject); + log.message = 'Connection checked out'; + log.driverConnectionId = logObject.connectionId; + log.durationMS = logObject.durationMS; + return log; + case CONNECTION_CHECKED_IN: + log = attachConnectionFields(log, logObject); + log.message = 'Connection checked in'; + log.driverConnectionId = logObject.connectionId; + return log; + case SERVER_OPENING: + log = attachSDAMFields(log, logObject); + log = attachConnectionFields(log, logObject); + log.message = 'Starting server monitoring'; + return log; + case SERVER_CLOSED: + log = attachSDAMFields(log, logObject); + log = attachConnectionFields(log, logObject); + log.message = 'Stopped server monitoring'; + return log; + case SERVER_HEARTBEAT_STARTED: + log = attachSDAMFields(log, logObject); + log = attachServerHeartbeatFields(log, logObject); + log.message = 'Server heartbeat started'; + return log; + case SERVER_HEARTBEAT_SUCCEEDED: + log = attachSDAMFields(log, logObject); + log = attachServerHeartbeatFields(log, logObject); + log.message = 'Server heartbeat succeeded'; + log.durationMS = logObject.duration; + log.serverConnectionId = logObject.serverConnectionId; + log.reply = stringifyWithMaxLen(logObject.reply, maxDocumentLength, { relaxed: true }); + return log; + case SERVER_HEARTBEAT_FAILED: + log = attachSDAMFields(log, logObject); + log = attachServerHeartbeatFields(log, logObject); + log.message = 'Server heartbeat failed'; + log.durationMS = logObject.duration; + log.failure = logObject.failure?.message; + return log; + case TOPOLOGY_OPENING: + log = attachSDAMFields(log, logObject); + log.message = 'Starting topology monitoring'; + return log; + case TOPOLOGY_CLOSED: + log = attachSDAMFields(log, logObject); + log.message = 'Stopped topology monitoring'; + return log; + case TOPOLOGY_DESCRIPTION_CHANGED: + log = attachSDAMFields(log, logObject); + log.message = 'Topology description changed'; + log.previousDescription = log.reply = stringifyWithMaxLen( + logObject.previousDescription, + maxDocumentLength + ); + log.newDescription = log.reply = stringifyWithMaxLen( + logObject.newDescription, + maxDocumentLength + ); + return log; + default: + for (const [key, value] of Object.entries(logObject)) { + if (value != null) log[key] = value; + } + } + return log; +} + +/** @internal */ +export class MongoLogger { + componentSeverities: Record; + maxDocumentLength: number; + logDestination: MongoDBLogWritable; + logDestinationIsStdErr: boolean; + pendingLog: PromiseLike | unknown = null; + private severities: Record>; + + /** + * This method should be used when logging errors that do not have a public driver API for + * reporting errors. + */ + error = this.log.bind(this, 'error'); + /** + * This method should be used to log situations where undesirable application behaviour might + * occur. For example, failing to end sessions on `MongoClient.close`. + */ + warn = this.log.bind(this, 'warn'); + /** + * This method should be used to report high-level information about normal driver behaviour. + * For example, the creation of a `MongoClient`. + */ + info = this.log.bind(this, 'info'); + /** + * This method should be used to report information that would be helpful when debugging an + * application. For example, a command starting, succeeding or failing. + */ + debug = this.log.bind(this, 'debug'); + /** + * This method should be used to report fine-grained details related to logic flow. For example, + * entering and exiting a function body. + */ + trace = this.log.bind(this, 'trace'); + + constructor(options: MongoLoggerOptions) { + this.componentSeverities = options.componentSeverities; + this.maxDocumentLength = options.maxDocumentLength; + this.logDestination = options.logDestination; + this.logDestinationIsStdErr = options.logDestinationIsStdErr; + this.severities = this.createLoggingSeverities(); + } + + createLoggingSeverities(): Record> { + const severities = Object(); + for (const component of Object.values(MongoLoggableComponent)) { + severities[component] = {}; + for (const severityLevel of Object.values(SeverityLevel)) { + severities[component][severityLevel] = + compareSeverity(severityLevel, this.componentSeverities[component]) <= 0; + } + } + return severities; + } + + turnOffSeverities() { + for (const component of Object.values(MongoLoggableComponent)) { + this.componentSeverities[component] = SeverityLevel.OFF; + for (const severityLevel of Object.values(SeverityLevel)) { + this.severities[component][severityLevel] = false; + } + } + } + + private logWriteFailureHandler(error: Error) { + if (this.logDestinationIsStdErr) { + this.turnOffSeverities(); + this.clearPendingLog(); + return; + } + this.logDestination = createStdioLogger(process.stderr); + this.logDestinationIsStdErr = true; + this.clearPendingLog(); + this.error(MongoLoggableComponent.CLIENT, { + toLog: function () { + return { + message: 'User input for mongodbLogPath is now invalid. Logging is halted.', + error: error.message + }; + } + }); + this.turnOffSeverities(); + this.clearPendingLog(); + } + + private clearPendingLog() { + this.pendingLog = null; + } + + willLog(component: MongoLoggableComponent, severity: SeverityLevel): boolean { + if (severity === SeverityLevel.OFF) return false; + return this.severities[component][severity]; + } + + private log( + severity: SeverityLevel, + component: MongoLoggableComponent, + message: Loggable | string + ): void { + if (!this.willLog(component, severity)) return; + + let logMessage: Log = { t: new Date(), c: component, s: severity }; + if (typeof message === 'string') { + logMessage.message = message; + } else if (typeof message === 'object') { + if (isLogConvertible(message)) { + logMessage = { ...logMessage, ...message.toLog() }; + } else { + logMessage = { ...logMessage, ...defaultLogTransform(message, this.maxDocumentLength) }; + } + } + + if (isPromiseLike(this.pendingLog)) { + this.pendingLog = this.pendingLog + + .then(() => this.logDestination.write(logMessage)) + + .then(this.clearPendingLog.bind(this), this.logWriteFailureHandler.bind(this)); + return; + } + + try { + const logResult = this.logDestination.write(logMessage); + if (isPromiseLike(logResult)) { + this.pendingLog = logResult.then( + this.clearPendingLog.bind(this), + this.logWriteFailureHandler.bind(this) + ); + } + } catch (error) { + this.logWriteFailureHandler(error); + } + } + + /** + * Merges options set through environment variables and the MongoClient, preferring environment + * variables when both are set, and substituting defaults for values not set. Options set in + * constructor take precedence over both environment variables and MongoClient options. + * + * @remarks + * When parsing component severity levels, invalid values are treated as unset and replaced with + * the default severity. + * + * @param envOptions - options set for the logger from the environment + * @param clientOptions - options set for the logger in the MongoClient options + * @returns a MongoLoggerOptions object to be used when instantiating a new MongoLogger + */ + static resolveOptions( + envOptions: MongoLoggerEnvOptions, + clientOptions: MongoLoggerMongoClientOptions + ): MongoLoggerOptions { + // client options take precedence over env options + const resolvedLogPath = resolveLogPath(envOptions, clientOptions); + const combinedOptions = { + ...envOptions, + ...clientOptions, + mongodbLogPath: resolvedLogPath.mongodbLogPath, + mongodbLogPathIsStdErr: resolvedLogPath.mongodbLogPathIsStdErr + }; + const defaultSeverity = resolveSeverityConfiguration( + combinedOptions.mongodbLogComponentSeverities?.default, + combinedOptions.MONGODB_LOG_ALL, + SeverityLevel.OFF + ); + + return { + componentSeverities: { + command: resolveSeverityConfiguration( + combinedOptions.mongodbLogComponentSeverities?.command, + combinedOptions.MONGODB_LOG_COMMAND, + defaultSeverity + ), + topology: resolveSeverityConfiguration( + combinedOptions.mongodbLogComponentSeverities?.topology, + combinedOptions.MONGODB_LOG_TOPOLOGY, + defaultSeverity + ), + serverSelection: resolveSeverityConfiguration( + combinedOptions.mongodbLogComponentSeverities?.serverSelection, + combinedOptions.MONGODB_LOG_SERVER_SELECTION, + defaultSeverity + ), + connection: resolveSeverityConfiguration( + combinedOptions.mongodbLogComponentSeverities?.connection, + combinedOptions.MONGODB_LOG_CONNECTION, + defaultSeverity + ), + client: resolveSeverityConfiguration( + combinedOptions.mongodbLogComponentSeverities?.client, + combinedOptions.MONGODB_LOG_CLIENT, + defaultSeverity + ), + default: defaultSeverity + }, + maxDocumentLength: + combinedOptions.mongodbLogMaxDocumentLength ?? + parseUnsignedInteger(combinedOptions.MONGODB_LOG_MAX_DOCUMENT_LENGTH) ?? + 1000, + logDestination: combinedOptions.mongodbLogPath, + logDestinationIsStdErr: combinedOptions.mongodbLogPathIsStdErr + }; + } +} diff --git a/gateway/node_modules/mongodb/src/mongo_types.ts b/gateway/node_modules/mongodb/src/mongo_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..3c56585a73129b7d1f5b5528fc6bb3d77ae3adbe --- /dev/null +++ b/gateway/node_modules/mongodb/src/mongo_types.ts @@ -0,0 +1,678 @@ +import { EventEmitter } from 'events'; + +import type { + Binary, + BSONRegExp, + BSONType, + Decimal128, + Document, + Double, + Int32, + Long, + ObjectId, + ObjectIdLike, + Timestamp +} from './bson'; +import { type CommandStartedEvent } from './cmap/command_monitoring_events'; +import { + type LoggableCommandFailedEvent, + type LoggableCommandSucceededEvent, + type LoggableServerHeartbeatFailedEvent, + type LoggableServerHeartbeatStartedEvent, + type LoggableServerHeartbeatSucceededEvent, + MongoLoggableComponent, + type MongoLogger +} from './mongo_logger'; +import type { Sort } from './sort'; +import { noop } from './utils'; + +/** @internal */ +export type TODO_NODE_3286 = any; + +/** Given an object shaped type, return the type of the _id field or default to ObjectId @public */ +export type InferIdType = TSchema extends { _id: infer IdType } + ? // user has defined a type for _id + Record extends IdType + ? never // explicitly forbid empty objects as the type of _id + : IdType + : TSchema extends { _id?: infer IdType } + ? // optional _id defined - return ObjectId | IdType + unknown extends IdType + ? ObjectId // infer the _id type as ObjectId if the type of _id is unknown + : IdType + : ObjectId; // user has not defined _id on schema + +/** Add an _id field to an object shaped type @public */ +export type WithId = EnhancedOmit & { _id: InferIdType }; + +/** + * Add an optional _id field to an object shaped type + * @public + */ +export type OptionalId = EnhancedOmit & { _id?: InferIdType }; + +/** + * Adds an optional _id field to an object shaped type, unless the _id field is required on that type. + * In the case _id is required, this method continues to require_id. + * + * @public + * + * @privateRemarks + * `ObjectId extends TSchema['_id']` is a confusing ordering at first glance. Rather than ask + * `TSchema['_id'] extends ObjectId` which translated to "Is the _id property ObjectId?" + * we instead ask "Does ObjectId look like (have the same shape) as the _id?" + */ +export type OptionalUnlessRequiredId = TSchema extends { _id: any } + ? TSchema + : OptionalId; + +/** TypeScript Omit (Exclude to be specific) does not work for objects with an "any" indexed type, and breaks discriminated unions @public */ +export type EnhancedOmit = string extends keyof TRecordOrUnion + ? TRecordOrUnion // TRecordOrUnion has indexed type e.g. { _id: string; [k: string]: any; } or it is "any" + : TRecordOrUnion extends any + ? Pick> // discriminated unions + : never; + +/** Remove the _id field from an object shaped type @public */ +export type WithoutId = Omit; + +/** A MongoDB filter can be some portion of the schema or a set of operators @public */ +export type Filter = { + [P in keyof WithId]?: Condition[P]>; +} & RootFilterOperators>; + +/** @public */ +export type Condition = AlternativeType | FilterOperators>; + +/** + * It is possible to search using alternative types in mongodb e.g. + * string types can be searched using a regex in mongo + * array types can be searched using their element type + * @public + */ +export type AlternativeType = + T extends ReadonlyArray ? T | RegExpOrString : RegExpOrString; + +/** @public */ +export type RegExpOrString = T extends string ? BSONRegExp | RegExp | T : T; + +/** @public */ +export interface RootFilterOperators extends Document { + $and?: Filter[]; + $nor?: Filter[]; + $or?: Filter[]; + $text?: { + $search: string; + $language?: string; + $caseSensitive?: boolean; + $diacriticSensitive?: boolean; + }; + $where?: string | ((this: TSchema) => boolean); + $comment?: string | Document; +} + +/** + * @public + * A type that extends Document but forbids anything that "looks like" an object id. + */ +export type NonObjectIdLikeDocument = { + [key in keyof ObjectIdLike]?: never; +} & Document; + +/** @public */ +export interface FilterOperators extends NonObjectIdLikeDocument { + // Comparison + $eq?: TValue; + $gt?: TValue; + $gte?: TValue; + $in?: ReadonlyArray; + $lt?: TValue; + $lte?: TValue; + $ne?: TValue; + $nin?: ReadonlyArray; + // Logical + $not?: TValue extends string ? FilterOperators | RegExp : FilterOperators; + // Element + /** + * When `true`, `$exists` matches the documents that contain the field, + * including documents where the field value is null. + */ + $exists?: boolean; + $type?: BSONType | BSONTypeAlias; + // Evaluation + $expr?: Record; + $jsonSchema?: Record; + $mod?: TValue extends number ? [number, number] : never; + $regex?: TValue extends string ? RegExp | BSONRegExp | string : never; + $options?: TValue extends string ? string : never; + // Geospatial + $geoIntersects?: { $geometry: Document }; + $geoWithin?: Document; + $near?: Document; + $nearSphere?: Document; + $maxDistance?: number; + // Array + $all?: ReadonlyArray; + $elemMatch?: Document; + $size?: TValue extends ReadonlyArray ? number : never; + // Bitwise + $bitsAllClear?: BitwiseFilter; + $bitsAllSet?: BitwiseFilter; + $bitsAnyClear?: BitwiseFilter; + $bitsAnySet?: BitwiseFilter; + $rand?: Record; +} + +/** @public */ +export type BitwiseFilter = + | number /** numeric bit mask */ + | Binary /** BinData bit mask */ + | ReadonlyArray; /** `[ , , ... ]` */ + +/** @public */ +export type BSONTypeAlias = keyof typeof BSONType; + +/** @public */ +export type IsAny = true extends false & Type + ? ResultIfAny + : ResultIfNotAny; + +/** @public */ +export type Flatten = Type extends ReadonlyArray ? Item : Type; + +/** @public */ +export type ArrayElement = Type extends ReadonlyArray ? Item : never; + +/** @public */ +export type SchemaMember = { [P in keyof T]?: V } | { [key: string]: V }; + +/** @public */ +export type IntegerType = number | Int32 | Long | bigint; + +/** @public */ +export type NumericType = IntegerType | Decimal128 | Double; + +/** @public */ +export type FilterOperations = + T extends Record + ? { [key in keyof T]?: FilterOperators } + : FilterOperators; + +/** @public */ +export type KeysOfAType = { + [key in keyof TSchema]: NonNullable extends Type ? key : never; +}[keyof TSchema]; + +/** @public */ +export type KeysOfOtherType = { + [key in keyof TSchema]: NonNullable extends Type ? never : key; +}[keyof TSchema]; + +/** @public */ +export type AcceptedFields = { + readonly [key in KeysOfAType]?: AssignableType; +}; + +/** It avoids using fields with not acceptable types @public */ +export type NotAcceptedFields = { + readonly [key in KeysOfOtherType]?: never; +}; + +/** @public */ +export type OnlyFieldsOfType = IsAny< + TSchema[keyof TSchema], + AssignableType extends FieldType ? Record : Record, + AcceptedFields & + NotAcceptedFields & + Record +>; + +/** @public */ +export type MatchKeysAndValues = Readonly> & Record; + +/** @public */ +export type AddToSetOperators = { + $each?: Array>; +}; + +/** @public */ +export type ArrayOperator = { + $each?: Array>; + $slice?: number; + $position?: number; + $sort?: Sort; +}; + +/** @public */ +export type SetFields = ({ + readonly [key in KeysOfAType | undefined>]?: + | OptionalId> + | AddToSetOperators>>>; +} & IsAny< + TSchema[keyof TSchema], + object, + NotAcceptedFields | undefined> +>) & { + readonly [key: string]: AddToSetOperators | any; +}; + +/** @public */ +export type PushOperator = ({ + readonly [key in KeysOfAType>]?: + | Flatten + | ArrayOperator>>; +} & NotAcceptedFields>) & { + readonly [key: string]: ArrayOperator | any; +}; + +/** @public */ +export type PullOperator = ({ + readonly [key in KeysOfAType>]?: + | Partial> + | FilterOperations>; +} & NotAcceptedFields>) & { + readonly [key: string]: FilterOperators | any; +}; + +/** @public */ +export type PullAllOperator = ({ + readonly [key in KeysOfAType>]?: TSchema[key]; +} & NotAcceptedFields>) & { + readonly [key: string]: ReadonlyArray; +}; + +/** @public */ +export type UpdateFilter = { + $currentDate?: OnlyFieldsOfType< + TSchema, + Date | Timestamp, + true | { $type: 'date' | 'timestamp' } + >; + $inc?: OnlyFieldsOfType; + $min?: MatchKeysAndValues; + $max?: MatchKeysAndValues; + $mul?: OnlyFieldsOfType; + $rename?: Record; + $set?: MatchKeysAndValues; + $setOnInsert?: MatchKeysAndValues; + $unset?: OnlyFieldsOfType; + $addToSet?: SetFields; + $pop?: OnlyFieldsOfType, 1 | -1>; + $pull?: PullOperator; + $push?: PushOperator; + $pullAll?: PullAllOperator; + $bit?: OnlyFieldsOfType< + TSchema, + NumericType | undefined, + { and: IntegerType } | { or: IntegerType } | { xor: IntegerType } + >; +} & Document; + +/** @public */ +export type Nullable = AnyType | null | undefined; + +/** @public */ +export type OneOrMore = T | ReadonlyArray; + +/** @public */ +export type GenericListener = (...args: any[]) => void; + +/** + * Event description type + * @public + */ +export type EventsDescription = Record; + +/** @public */ +export type CommonEvents = 'newListener' | 'removeListener'; + +/** + * Typescript type safe event emitter + * @public + */ +export declare interface TypedEventEmitter extends EventEmitter { + addListener(event: EventKey, listener: Events[EventKey]): this; + addListener( + event: CommonEvents, + listener: (eventName: string | symbol, listener: GenericListener) => void + ): this; + addListener(event: string | symbol, listener: GenericListener): this; + + on(event: EventKey, listener: Events[EventKey]): this; + on( + event: CommonEvents, + listener: (eventName: string | symbol, listener: GenericListener) => void + ): this; + on(event: string | symbol, listener: GenericListener): this; + + once(event: EventKey, listener: Events[EventKey]): this; + once( + event: CommonEvents, + listener: (eventName: string | symbol, listener: GenericListener) => void + ): this; + once(event: string | symbol, listener: GenericListener): this; + + removeListener(event: EventKey, listener: Events[EventKey]): this; + removeListener( + event: CommonEvents, + listener: (eventName: string | symbol, listener: GenericListener) => void + ): this; + removeListener(event: string | symbol, listener: GenericListener): this; + + off(event: EventKey, listener: Events[EventKey]): this; + off( + event: CommonEvents, + listener: (eventName: string | symbol, listener: GenericListener) => void + ): this; + off(event: string | symbol, listener: GenericListener): this; + + removeAllListeners( + event?: EventKey | CommonEvents | symbol | string + ): this; + + listeners( + event: EventKey | CommonEvents | symbol | string + ): Events[EventKey][]; + + rawListeners( + event: EventKey | CommonEvents | symbol | string + ): Events[EventKey][]; + + emit( + event: EventKey | symbol, + ...args: Parameters + ): boolean; + + listenerCount( + type: EventKey | CommonEvents | symbol | string + ): number; + + prependListener(event: EventKey, listener: Events[EventKey]): this; + prependListener( + event: CommonEvents, + listener: (eventName: string | symbol, listener: GenericListener) => void + ): this; + prependListener(event: string | symbol, listener: GenericListener): this; + + prependOnceListener( + event: EventKey, + listener: Events[EventKey] + ): this; + prependOnceListener( + event: CommonEvents, + listener: (eventName: string | symbol, listener: GenericListener) => void + ): this; + prependOnceListener(event: string | symbol, listener: GenericListener): this; + + eventNames(): string[]; + getMaxListeners(): number; + setMaxListeners(n: number): this; +} + +/** + * Typescript type safe event emitter + * @public + */ + +// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging +export class TypedEventEmitter extends EventEmitter { + /** @internal */ + protected mongoLogger?: MongoLogger; + /** @internal */ + protected component?: MongoLoggableComponent; + /** @internal */ + emitAndLog( + event: EventKey | symbol, + ...args: Parameters + ): void { + this.emit(event, ...args); + if (this.component) this.mongoLogger?.debug(this.component, args[0]); + } + /** @internal */ + emitAndLogHeartbeat( + event: EventKey | symbol, + topologyId: number, + serverConnectionId?: number | '', + ...args: Parameters + ): void { + this.emit(event, ...args); + if (this.component) { + const loggableHeartbeatEvent: + | LoggableServerHeartbeatFailedEvent + | LoggableServerHeartbeatSucceededEvent + | LoggableServerHeartbeatStartedEvent = { + topologyId: topologyId, + serverConnectionId: serverConnectionId ?? null, + ...args[0] + }; + this.mongoLogger?.debug(this.component, loggableHeartbeatEvent); + } + } + /** @internal */ + emitAndLogCommand( + monitorCommands: boolean, + event: EventKey | symbol, + databaseName: string, + connectionEstablished: boolean, + ...args: Parameters + ): void { + if (monitorCommands) { + this.emit(event, ...args); + } + if (connectionEstablished) { + const loggableCommandEvent: + | CommandStartedEvent + | LoggableCommandFailedEvent + | LoggableCommandSucceededEvent = { + databaseName: databaseName, + ...args[0] + }; + this.mongoLogger?.debug(MongoLoggableComponent.COMMAND, loggableCommandEvent); + } + } +} + +/** + * @internal + */ +export class CancellationToken extends TypedEventEmitter<{ cancel(): void }> { + constructor(...args: any[]) { + super(...args); + this.on('error', noop); + } +} + +/** @public */ +export type Abortable = { + /** + * @experimental + * When provided, the corresponding `AbortController` can be used to abort an asynchronous action. + * + * The `signal.reason` value is used as the error thrown. + * + * @remarks + * **NOTE:** If an abort signal aborts an operation while the driver is writing to the underlying + * socket or reading the response from the server, the socket will be closed. + * If signals are aborted at a high rate during socket read/writes this can lead to a high rate of connection reestablishment. + * + * We plan to mitigate this in a future release, please follow NODE-6062 (`timeoutMS` expiration suffers the same limitation). + * + * AbortSignals are likely a best fit for human interactive interruption (ex. ctrl-C) where the frequency + * of cancellation is reasonably low. If a signal is programmatically aborted for 100s of operations you can empty + * the driver's connection pool. + * + * @example + * ```js + * const controller = new AbortController(); + * const { signal } = controller; + * process.on('SIGINT', () => controller.abort(new Error('^C pressed'))); + * + * try { + * const res = await fetch('...', { signal }); + * await collection.findOne(await res.json(), { signal }); + * catch (error) { + * if (error === signal.reason) { + * // signal abort error handling + * } + * } + * ``` + */ + signal?: AbortSignal | undefined; +}; + +/** + * Helper types for dot-notation filter attributes + */ + +/** @public */ +export type Join = T extends [] + ? '' + : T extends [string | number] + ? `${T[0]}` + : T extends [string | number, ...infer R] + ? `${T[0]}${D}${Join}` + : string; + +/** @public */ +export type PropertyType = string extends Property + ? unknown + : Property extends keyof Type + ? Type[Property] + : Property extends `${number}` + ? Type extends ReadonlyArray + ? ArrayType + : unknown + : Property extends `${infer Key}.${infer Rest}` + ? Key extends `${number}` + ? Type extends ReadonlyArray + ? PropertyType + : unknown + : Key extends keyof Type + ? Type[Key] extends Map + ? MapType + : PropertyType + : unknown + : unknown; + +/** + * @public + * returns tuple of strings (keys to be joined on '.') that represent every path into a schema + * https://www.mongodb.com/docs/manual/tutorial/query-embedded-documents/ + * + * @remarks + * Through testing we determined that a depth of 8 is safe for the typescript compiler + * and provides reasonable compilation times. This number is otherwise not special and + * should be changed if issues are found with this level of checking. Beyond this + * depth any helpers that make use of NestedPaths should devolve to not asserting any + * type safety on the input. + */ +export type NestedPaths = Depth['length'] extends 8 + ? [] + : Type extends + | string + | number + | bigint + | boolean + | Date + | RegExp + | Buffer + | Uint8Array + | ((...args: any[]) => any) + | { _bsontype: string } + ? [] + : Type extends ReadonlyArray + ? [] | [number, ...NestedPaths] + : Type extends Map + ? [string] + : Type extends object + ? { + [Key in Extract]: Type[Key] extends Type // type of value extends the parent + ? [Key] + : // for a recursive union type, the child will never extend the parent type. + // but the parent will still extend the child + Type extends Type[Key] + ? [Key] + : Type[Key] extends ReadonlyArray // handling recursive types with arrays + ? Type extends ArrayType // is the type of the parent the same as the type of the array? + ? [Key] // yes, it's a recursive array type + : // for unions, the child type extends the parent + ArrayType extends Type + ? [Key] // we have a recursive array union + : // child is an array, but it's not a recursive array + [Key, ...NestedPaths] + : // child is not structured the same as the parent + [Key, ...NestedPaths] | [Key]; + }[Extract] + : []; + +/** + * @public + * returns keys (strings) for every path into a schema with a value of type + * https://www.mongodb.com/docs/manual/tutorial/query-embedded-documents/ + */ +export type NestedPathsOfType = KeysOfAType< + { + [Property in Join, '.'>]: PropertyType; + }, + Type +>; + +/** + * @public + * @experimental + */ +export type StrictFilter = + | Partial + | ({ + [Property in Join, []>, '.'>]?: Condition< + PropertyType, Property> + >; + } & RootFilterOperators>); + +/** + * @public + * @experimental + */ +export type StrictUpdateFilter = { + $currentDate?: OnlyFieldsOfType< + TSchema, + Date | Timestamp, + true | { $type: 'date' | 'timestamp' } + >; + $inc?: OnlyFieldsOfType; + $min?: StrictMatchKeysAndValues; + $max?: StrictMatchKeysAndValues; + $mul?: OnlyFieldsOfType; + $rename?: Record; + $set?: StrictMatchKeysAndValues; + $setOnInsert?: StrictMatchKeysAndValues; + $unset?: OnlyFieldsOfType; + $addToSet?: SetFields; + $pop?: OnlyFieldsOfType, 1 | -1>; + $pull?: PullOperator; + $push?: PushOperator; + $pullAll?: PullAllOperator; + $bit?: OnlyFieldsOfType< + TSchema, + NumericType | undefined, + { and: IntegerType } | { or: IntegerType } | { xor: IntegerType } + >; +} & Document; + +/** + * @public + * @experimental + */ +export type StrictMatchKeysAndValues = Readonly< + { + [Property in Join, '.'>]?: PropertyType; + } & { + [Property in `${NestedPathsOfType}.$${`[${string}]` | ''}`]?: ArrayElement< + PropertyType + >; + } & { + [Property in `${NestedPathsOfType[]>}.$${ + | `[${string}]` + | ''}.${string}`]?: any; // Could be further narrowed + } & Document +>; diff --git a/gateway/node_modules/mongodb/src/operations/aggregate.ts b/gateway/node_modules/mongodb/src/operations/aggregate.ts new file mode 100644 index 0000000000000000000000000000000000000000..e1ae2bbfcdda417817c16a33233ea57127d21e51 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/aggregate.ts @@ -0,0 +1,153 @@ +import type { Document } from '../bson'; +import { CursorResponse, ExplainedCursorResponse } from '../cmap/wire_protocol/responses'; +import { type CursorTimeoutMode } from '../cursor/abstract_cursor'; +import { MongoInvalidArgumentError } from '../error'; +import { type ExplainOptions } from '../explain'; +import { type MongoDBNamespace } from '../utils'; +import { WriteConcern } from '../write_concern'; +import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects, type Hint } from './operation'; + +/** @internal */ +export const DB_AGGREGATE_COLLECTION = 1 as const; + +/** @public */ +export interface AggregateOptions extends Omit { + /** allowDiskUse lets the server know if it can use disk to store temporary results for the aggregation (requires mongodb 2.6 \>). */ + allowDiskUse?: boolean; + /** The number of documents to return per batch. See [aggregation documentation](https://www.mongodb.com/docs/manual/reference/command/aggregate). */ + batchSize?: number; + /** Allow driver to bypass schema validation. */ + bypassDocumentValidation?: boolean; + /** Return the query as cursor, on 2.6 \> it returns as a real cursor on pre 2.6 it returns as an emulated cursor. */ + cursor?: Document; + /** + * Specifies a cumulative time limit in milliseconds for processing operations on the cursor. MongoDB interrupts the operation at the earliest following interrupt point. + */ + maxTimeMS?: number; + /** The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query. */ + maxAwaitTimeMS?: number; + /** Specify collation. */ + collation?: CollationOptions; + /** Add an index selection hint to an aggregation command */ + hint?: Hint; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + + out?: string; + + /** + * Specifies the verbosity mode for the explain output. + * @deprecated This API is deprecated in favor of `collection.aggregate().explain()` + * or `db.aggregate().explain()`. + */ + explain?: ExplainOptions['explain']; + /** @internal */ + timeoutMode?: CursorTimeoutMode; +} + +/** @internal */ +export class AggregateOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = CursorResponse; + override options: AggregateOptions; + target: string | typeof DB_AGGREGATE_COLLECTION; + pipeline: Document[]; + hasWriteStage: boolean; + + constructor(ns: MongoDBNamespace, pipeline: Document[], options?: AggregateOptions) { + super(undefined, { ...options, dbName: ns.db }); + + this.options = { ...options }; + + // Covers when ns.collection is null, undefined or the empty string, use DB_AGGREGATE_COLLECTION + this.target = ns.collection || DB_AGGREGATE_COLLECTION; + + this.pipeline = pipeline; + + // determine if we have a write stage, override read preference if so + this.hasWriteStage = false; + if (typeof options?.out === 'string') { + this.pipeline = this.pipeline.concat({ $out: options.out }); + this.hasWriteStage = true; + } else if (pipeline.length > 0) { + const finalStage = pipeline[pipeline.length - 1]; + if (finalStage.$out || finalStage.$merge) { + this.hasWriteStage = true; + } + } + + if (!this.hasWriteStage) { + delete this.options.writeConcern; + } + + if (options?.cursor != null && typeof options.cursor !== 'object') { + throw new MongoInvalidArgumentError('Cursor options must be an object'); + } + + this.SERVER_COMMAND_RESPONSE_TYPE = this.explain ? ExplainedCursorResponse : CursorResponse; + } + + override get commandName() { + return 'aggregate' as const; + } + + override get canRetryRead(): boolean { + return !this.hasWriteStage; + } + + addToPipeline(stage: Document): void { + this.pipeline.push(stage); + } + + override buildCommandDocument(): Document { + const options = this.options; + const command: Document = { aggregate: this.target, pipeline: this.pipeline }; + + if (this.hasWriteStage && this.writeConcern) { + WriteConcern.apply(command, this.writeConcern); + } + + if (options.bypassDocumentValidation === true) { + command.bypassDocumentValidation = options.bypassDocumentValidation; + } + + if (typeof options.allowDiskUse === 'boolean') { + command.allowDiskUse = options.allowDiskUse; + } + + if (options.hint) { + command.hint = options.hint; + } + + if (options.let) { + command.let = options.let; + } + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (options.comment !== undefined) { + command.comment = options.comment; + } + + command.cursor = options.cursor || {}; + if (options.batchSize && !this.hasWriteStage) { + command.cursor.batchSize = options.batchSize; + } + + return command; + } + + override handleOk( + response: InstanceType + ): CursorResponse { + return response; + } +} + +defineAspects(AggregateOperation, [ + Aspect.READ_OPERATION, + Aspect.RETRYABLE, + Aspect.EXPLAINABLE, + Aspect.CURSOR_CREATING, + Aspect.SUPPORTS_RAW_DATA +]); diff --git a/gateway/node_modules/mongodb/src/operations/client_bulk_write/client_bulk_write.ts b/gateway/node_modules/mongodb/src/operations/client_bulk_write/client_bulk_write.ts new file mode 100644 index 0000000000000000000000000000000000000000..0c78cb7cdedd7a9252b22035017137091987e41e --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/client_bulk_write/client_bulk_write.ts @@ -0,0 +1,72 @@ +import { type Connection } from '../../cmap/connection'; +import { ClientBulkWriteCursorResponse } from '../../cmap/wire_protocol/responses'; +import type { ClientSession } from '../../sessions'; +import { MongoDBNamespace } from '../../utils'; +import { CommandOperation } from '../command'; +import { Aspect, defineAspects } from '../operation'; +import { type ClientBulkWriteCommand, type ClientBulkWriteCommandBuilder } from './command_builder'; +import { type ClientBulkWriteOptions } from './common'; + +/** + * Executes a single client bulk write operation within a potential batch. + * @internal + */ +export class ClientBulkWriteOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = ClientBulkWriteCursorResponse; + + commandBuilder: ClientBulkWriteCommandBuilder; + override options: ClientBulkWriteOptions; + + override get commandName() { + return 'bulkWrite' as const; + } + + constructor(commandBuilder: ClientBulkWriteCommandBuilder, options: ClientBulkWriteOptions) { + super(undefined, options); + this.commandBuilder = commandBuilder; + this.options = options; + this.ns = new MongoDBNamespace('admin', '$cmd'); + } + + override resetBatch(): boolean { + return this.commandBuilder.resetBatch(); + } + + override get canRetryWrite(): boolean { + return this.commandBuilder.isBatchRetryable; + } + + override handleOk( + response: InstanceType + ): ClientBulkWriteCursorResponse { + return response; + } + + override buildCommandDocument( + connection: Connection, + _session?: ClientSession + ): ClientBulkWriteCommand { + const command = this.commandBuilder.buildBatch( + connection.description.maxMessageSizeBytes, + connection.description.maxWriteBatchSize, + connection.description.maxBsonObjectSize + ); + + // Check _after_ the batch is built if we cannot retry it and override the option. + if (!this.canRetryWrite) { + this.options.willRetryWrite = false; + } + + return command; + } +} + +// Skipping the collation as it goes on the individual ops. +defineAspects(ClientBulkWriteOperation, [ + Aspect.WRITE_OPERATION, + Aspect.SKIP_COLLATION, + Aspect.CURSOR_CREATING, + Aspect.RETRYABLE, + Aspect.COMMAND_BATCHING, + Aspect.SUPPORTS_RAW_DATA +]); diff --git a/gateway/node_modules/mongodb/src/operations/client_bulk_write/command_builder.ts b/gateway/node_modules/mongodb/src/operations/client_bulk_write/command_builder.ts new file mode 100644 index 0000000000000000000000000000000000000000..dee80facca9fcfb01fa34037ace8892f31fa06a4 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/client_bulk_write/command_builder.ts @@ -0,0 +1,487 @@ +import { BSON, type BSONSerializeOptions, type Document } from '../../bson'; +import { DocumentSequence } from '../../cmap/commands'; +import { MongoAPIError, MongoInvalidArgumentError } from '../../error'; +import { type PkFactory } from '../../mongo_client'; +import type { Filter, OptionalId, UpdateFilter, WithoutId } from '../../mongo_types'; +import { formatSort, type SortForCmd } from '../../sort'; +import { DEFAULT_PK_FACTORY, hasAtomicOperators } from '../../utils'; +import { type CollationOptions } from '../command'; +import { type Hint } from '../operation'; +import type { + AnyClientBulkWriteModel, + ClientBulkWriteOptions, + ClientDeleteManyModel, + ClientDeleteOneModel, + ClientInsertOneModel, + ClientReplaceOneModel, + ClientUpdateManyModel, + ClientUpdateOneModel +} from './common'; + +/** @internal */ +export interface ClientBulkWriteCommand { + bulkWrite: 1; + errorsOnly: boolean; + ordered: boolean; + ops: DocumentSequence; + nsInfo: DocumentSequence; + bypassDocumentValidation?: boolean; + let?: Document; + comment?: any; +} + +/** + * The bytes overhead for the extra fields added post command generation. + */ +const MESSAGE_OVERHEAD_BYTES = 1000; + +/** @internal */ +export class ClientBulkWriteCommandBuilder { + models: ReadonlyArray>; + options: ClientBulkWriteOptions; + pkFactory: PkFactory; + /** The current index in the models array that is being processed. */ + currentModelIndex: number; + /** The model index that the builder was on when it finished the previous batch. Used for resets when retrying. */ + previousModelIndex: number; + /** The last array of operations that were created. Used by the results merger for indexing results. */ + lastOperations: Document[]; + /** Returns true if the current batch being created has no multi-updates. */ + isBatchRetryable: boolean; + + /** + * Create the command builder. + * @param models - The client write models. + */ + constructor( + models: ReadonlyArray>, + options: ClientBulkWriteOptions, + pkFactory?: PkFactory + ) { + this.models = models; + this.options = options; + this.pkFactory = pkFactory ?? DEFAULT_PK_FACTORY; + this.currentModelIndex = 0; + this.previousModelIndex = 0; + this.lastOperations = []; + this.isBatchRetryable = true; + } + + /** + * Gets the errorsOnly value for the command, which is the inverse of the + * user provided verboseResults option. Defaults to true. + */ + get errorsOnly(): boolean { + if ('verboseResults' in this.options) { + return !this.options.verboseResults; + } + return true; + } + + /** + * Determines if there is another batch to process. + * @returns True if not all batches have been built. + */ + hasNextBatch(): boolean { + return this.currentModelIndex < this.models.length; + } + + /** + * When we need to retry a command we need to set the current + * model index back to its previous value. + */ + resetBatch(): boolean { + this.currentModelIndex = this.previousModelIndex; + return true; + } + + /** + * Build a single batch of a client bulk write command. + * @param maxMessageSizeBytes - The max message size in bytes. + * @param maxWriteBatchSize - The max write batch size. + * @returns The client bulk write command. + */ + buildBatch( + maxMessageSizeBytes: number, + maxWriteBatchSize: number, + maxBsonObjectSize: number + ): ClientBulkWriteCommand { + // We start by assuming the batch has no multi-updates, so it is retryable + // until we find them. + this.isBatchRetryable = true; + let commandLength = 0; + let currentNamespaceIndex = 0; + const command: ClientBulkWriteCommand = this.baseCommand(); + const namespaces = new Map(); + // In the case of retries we need to mark where we started this batch. + this.previousModelIndex = this.currentModelIndex; + + while (this.currentModelIndex < this.models.length) { + const model = this.models[this.currentModelIndex]; + const ns = model.namespace; + const nsIndex = namespaces.get(ns); + + // Multi updates are not retryable. + if (model.name === 'deleteMany' || model.name === 'updateMany') { + this.isBatchRetryable = false; + } + + if (nsIndex != null) { + // Build the operation and serialize it to get the bytes buffer. + const operation = buildOperation(model, nsIndex, this.pkFactory, this.options); + let operationBuffer; + try { + operationBuffer = BSON.serialize(operation); + } catch (cause) { + throw new MongoInvalidArgumentError(`Could not serialize operation to BSON`, { cause }); + } + + validateBufferSize('ops', operationBuffer, maxBsonObjectSize); + + // Check if the operation buffer can fit in the command. If it can, + // then add the operation to the document sequence and increment the + // current length as long as the ops don't exceed the maxWriteBatchSize. + if ( + commandLength + operationBuffer.length < maxMessageSizeBytes && + command.ops.documents.length < maxWriteBatchSize + ) { + // Pushing to the ops document sequence returns the total byte length of the document sequence. + commandLength = MESSAGE_OVERHEAD_BYTES + command.ops.push(operation, operationBuffer); + // Increment the builder's current model index. + this.currentModelIndex++; + } else { + // The operation cannot fit in the current command and will need to + // go in the next batch. Exit the loop. + break; + } + } else { + // The namespace is not already in the nsInfo so we will set it in the map, and + // construct our nsInfo and ops documents and buffers. + namespaces.set(ns, currentNamespaceIndex); + const nsInfo = { ns: ns }; + const operation = buildOperation( + model, + currentNamespaceIndex, + this.pkFactory, + this.options + ); + let nsInfoBuffer; + let operationBuffer; + try { + nsInfoBuffer = BSON.serialize(nsInfo); + operationBuffer = BSON.serialize(operation); + } catch (cause) { + throw new MongoInvalidArgumentError(`Could not serialize ns info to BSON`, { cause }); + } + + validateBufferSize('nsInfo', nsInfoBuffer, maxBsonObjectSize); + validateBufferSize('ops', operationBuffer, maxBsonObjectSize); + + // Check if the operation and nsInfo buffers can fit in the command. If they + // can, then add the operation and nsInfo to their respective document + // sequences and increment the current length as long as the ops don't exceed + // the maxWriteBatchSize. + if ( + commandLength + nsInfoBuffer.length + operationBuffer.length < maxMessageSizeBytes && + command.ops.documents.length < maxWriteBatchSize + ) { + // Pushing to the ops document sequence returns the total byte length of the document sequence. + commandLength = + MESSAGE_OVERHEAD_BYTES + + command.nsInfo.push(nsInfo, nsInfoBuffer) + + command.ops.push(operation, operationBuffer); + // We've added a new namespace, increment the namespace index. + currentNamespaceIndex++; + // Increment the builder's current model index. + this.currentModelIndex++; + } else { + // The operation cannot fit in the current command and will need to + // go in the next batch. Exit the loop. + break; + } + } + } + // Set the last operations and return the command. + this.lastOperations = command.ops.documents; + return command; + } + + private baseCommand(): ClientBulkWriteCommand { + const command: ClientBulkWriteCommand = { + bulkWrite: 1, + errorsOnly: this.errorsOnly, + ordered: this.options.ordered ?? true, + ops: new DocumentSequence('ops'), + nsInfo: new DocumentSequence('nsInfo') + }; + // Add bypassDocumentValidation if it was present in the options. + if (this.options.bypassDocumentValidation != null) { + command.bypassDocumentValidation = this.options.bypassDocumentValidation; + } + // Add let if it was present in the options. + if (this.options.let) { + command.let = this.options.let; + } + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (this.options.comment !== undefined) { + command.comment = this.options.comment; + } + + return command; + } +} + +function validateBufferSize(name: string, buffer: Uint8Array, maxBsonObjectSize: number) { + if (buffer.length > maxBsonObjectSize) { + throw new MongoInvalidArgumentError( + `Client bulk write operation ${name} of length ${buffer.length} exceeds the max bson object size of ${maxBsonObjectSize}` + ); + } +} + +/** @internal */ +export interface ClientInsertOperation { + insert: number; + document: OptionalId; +} + +/** + * Build the insert one operation. + * @param model - The insert one model. + * @param index - The namespace index. + * @returns the operation. + */ +export const buildInsertOneOperation = ( + model: ClientInsertOneModel, + index: number, + pkFactory: PkFactory +): ClientInsertOperation => { + const document: ClientInsertOperation = { + insert: index, + document: model.document + }; + document.document._id = model.document._id ?? pkFactory.createPk(); + return document; +}; + +/** @internal */ +export interface ClientDeleteOperation { + delete: number; + multi: boolean; + filter: Filter; + hint?: Hint; + collation?: CollationOptions; +} + +/** + * Build the delete one operation. + * @param model - The insert many model. + * @param index - The namespace index. + * @returns the operation. + */ +export const buildDeleteOneOperation = ( + model: ClientDeleteOneModel, + index: number +): Document => { + return createDeleteOperation(model, index, false); +}; + +/** + * Build the delete many operation. + * @param model - The delete many model. + * @param index - The namespace index. + * @returns the operation. + */ +export const buildDeleteManyOperation = ( + model: ClientDeleteManyModel, + index: number +): Document => { + return createDeleteOperation(model, index, true); +}; + +/** + * Creates a delete operation based on the parameters. + */ +function createDeleteOperation( + model: ClientDeleteOneModel | ClientDeleteManyModel, + index: number, + multi: boolean +): ClientDeleteOperation { + const document: ClientDeleteOperation = { + delete: index, + multi: multi, + filter: model.filter + }; + if (model.hint) { + document.hint = model.hint; + } + if (model.collation) { + document.collation = model.collation; + } + return document; +} + +/** @internal */ +export interface ClientUpdateOperation { + update: number; + multi: boolean; + filter: Filter; + updateMods: UpdateFilter | Document[]; + hint?: Hint; + upsert?: boolean; + arrayFilters?: Document[]; + collation?: CollationOptions; + sort?: SortForCmd; +} + +/** + * Build the update one operation. + * @param model - The update one model. + * @param index - The namespace index. + * @returns the operation. + */ +export const buildUpdateOneOperation = ( + model: ClientUpdateOneModel, + index: number, + options: BSONSerializeOptions +): ClientUpdateOperation => { + return createUpdateOperation(model, index, false, options); +}; + +/** + * Build the update many operation. + * @param model - The update many model. + * @param index - The namespace index. + * @returns the operation. + */ +export const buildUpdateManyOperation = ( + model: ClientUpdateManyModel, + index: number, + options: BSONSerializeOptions +): ClientUpdateOperation => { + return createUpdateOperation(model, index, true, options); +}; + +/** + * Validate the update document. + * @param update - The update document. + */ +function validateUpdate(update: Document, options: BSONSerializeOptions) { + if (!hasAtomicOperators(update, options)) { + throw new MongoAPIError( + 'Client bulk write update models must only contain atomic modifiers (start with $) and must not be empty.' + ); + } +} + +/** + * Creates a delete operation based on the parameters. + */ +function createUpdateOperation( + model: ClientUpdateOneModel | ClientUpdateManyModel, + index: number, + multi: boolean, + options: BSONSerializeOptions +): ClientUpdateOperation { + // Update documents provided in UpdateOne and UpdateMany write models are + // required only to contain atomic modifiers (i.e. keys that start with "$"). + // Drivers MUST throw an error if an update document is empty or if the + // document's first key does not start with "$". + validateUpdate(model.update, options); + const document: ClientUpdateOperation = { + update: index, + multi: multi, + filter: model.filter, + updateMods: model.update + }; + if (model.hint) { + document.hint = model.hint; + } + if (model.upsert) { + document.upsert = model.upsert; + } + if (model.arrayFilters) { + document.arrayFilters = model.arrayFilters; + } + if (model.collation) { + document.collation = model.collation; + } + if (!multi && 'sort' in model && model.sort != null) { + document.sort = formatSort(model.sort); + } + return document; +} + +/** @internal */ +export interface ClientReplaceOneOperation { + update: number; + multi: boolean; + filter: Filter; + updateMods: WithoutId; + hint?: Hint; + upsert?: boolean; + collation?: CollationOptions; + sort?: SortForCmd; +} + +/** + * Build the replace one operation. + * @param model - The replace one model. + * @param index - The namespace index. + * @returns the operation. + */ +export const buildReplaceOneOperation = ( + model: ClientReplaceOneModel, + index: number +): ClientReplaceOneOperation => { + if (hasAtomicOperators(model.replacement)) { + throw new MongoAPIError( + 'Client bulk write replace models must not contain atomic modifiers (start with $) and must not be empty.' + ); + } + + const document: ClientReplaceOneOperation = { + update: index, + multi: false, + filter: model.filter, + updateMods: model.replacement + }; + if (model.hint) { + document.hint = model.hint; + } + if (model.upsert) { + document.upsert = model.upsert; + } + if (model.collation) { + document.collation = model.collation; + } + if (model.sort != null) { + document.sort = formatSort(model.sort); + } + return document; +}; + +/** @internal */ +export function buildOperation( + model: AnyClientBulkWriteModel, + index: number, + pkFactory: PkFactory, + options: BSONSerializeOptions +): Document { + switch (model.name) { + case 'insertOne': + return buildInsertOneOperation(model, index, pkFactory); + case 'deleteOne': + return buildDeleteOneOperation(model, index); + case 'deleteMany': + return buildDeleteManyOperation(model, index); + case 'updateOne': + return buildUpdateOneOperation(model, index, options); + case 'updateMany': + return buildUpdateManyOperation(model, index, options); + case 'replaceOne': + return buildReplaceOneOperation(model, index); + } +} diff --git a/gateway/node_modules/mongodb/src/operations/client_bulk_write/common.ts b/gateway/node_modules/mongodb/src/operations/client_bulk_write/common.ts new file mode 100644 index 0000000000000000000000000000000000000000..b08725bedaec839dfdd1db104143ebbcda5df07b --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/client_bulk_write/common.ts @@ -0,0 +1,276 @@ +import { type Document } from '../../bson'; +import type { Filter, OptionalId, UpdateFilter, WithoutId } from '../../mongo_types'; +import type { CollationOptions, CommandOperationOptions } from '../../operations/command'; +import type { Hint } from '../../operations/operation'; +import { type Sort } from '../../sort'; + +/** @public */ +export interface ClientBulkWriteOptions extends CommandOperationOptions { + /** + * If true, when an insert fails, don't execute the remaining writes. + * If false, continue with remaining inserts when one fails. + * @defaultValue `true` - inserts are ordered by default + */ + ordered?: boolean; + /** + * Allow driver to bypass schema validation. + * @defaultValue `false` - documents will be validated by default + **/ + bypassDocumentValidation?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /** + * Whether detailed results for each successful operation should be included in the returned + * BulkWriteResult. + */ + verboseResults?: boolean; +} + +/** @public */ +export interface ClientWriteModel { + /** + * The namespace for the write. + * + * A namespace is a combination of the database name and the name of the collection: `.`. + * All documents belong to a namespace. + * + * @see https://www.mongodb.com/docs/manual/reference/limits/#std-label-faq-dev-namespace + */ + namespace: string; +} + +/** @public */ +export interface ClientInsertOneModel extends ClientWriteModel { + name: 'insertOne'; + /** The document to insert. */ + document: OptionalId; +} + +/** @public */ +export interface ClientDeleteOneModel extends ClientWriteModel { + name: 'deleteOne'; + /** + * The filter used to determine if a document should be deleted. + * For a deleteOne operation, the first match is removed. + */ + filter: Filter; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; +} + +/** @public */ +export interface ClientDeleteManyModel extends ClientWriteModel { + name: 'deleteMany'; + /** + * The filter used to determine if a document should be deleted. + * For a deleteMany operation, all matches are removed. + */ + filter: Filter; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; +} + +/** @public */ +export interface ClientReplaceOneModel extends ClientWriteModel { + name: 'replaceOne'; + /** + * The filter used to determine if a document should be replaced. + * For a replaceOne operation, the first match is replaced. + */ + filter: Filter; + /** The document with which to replace the matched document. */ + replacement: WithoutId; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; + /** When true, creates a new document if no document matches the query. */ + upsert?: boolean; + /** Specifies the sort order for the documents matched by the filter. */ + sort?: Sort; +} + +/** @public */ +export interface ClientUpdateOneModel extends ClientWriteModel { + name: 'updateOne'; + /** + * The filter used to determine if a document should be updated. + * For an updateOne operation, the first match is updated. + */ + filter: Filter; + /** + * The modifications to apply. The value can be either: + * UpdateFilter - A document that contains update operator expressions, + * Document[] - an aggregation pipeline. + */ + update: UpdateFilter | Document[]; + /** A set of filters specifying to which array elements an update should apply. */ + arrayFilters?: Document[]; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; + /** When true, creates a new document if no document matches the query. */ + upsert?: boolean; + /** Specifies the sort order for the documents matched by the filter. */ + sort?: Sort; +} + +/** @public */ +export interface ClientUpdateManyModel extends ClientWriteModel { + name: 'updateMany'; + /** + * The filter used to determine if a document should be updated. + * For an updateMany operation, all matches are updated. + */ + filter: Filter; + /** + * The modifications to apply. The value can be either: + * UpdateFilter - A document that contains update operator expressions, + * Document[] - an aggregation pipeline. + */ + update: UpdateFilter | Document[]; + /** A set of filters specifying to which array elements an update should apply. */ + arrayFilters?: Document[]; + /** Specifies a collation. */ + collation?: CollationOptions; + /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ + hint?: Hint; + /** When true, creates a new document if no document matches the query. */ + upsert?: boolean; +} + +/** + * Used to represent any of the client bulk write models that can be passed as an array + * to MongoClient#bulkWrite. + * @public + */ +export type AnyClientBulkWriteModel = + | ClientInsertOneModel + | ClientReplaceOneModel + | ClientUpdateOneModel + | ClientUpdateManyModel + | ClientDeleteOneModel + | ClientDeleteManyModel; + +/** + * A mapping of namespace strings to collections schemas. + * @public + * + * @example + * ```ts + * type MongoDBSchemas = { + * 'db.books': Book; + * 'db.authors': Author; + * } + * + * const model: ClientBulkWriteModel = { + * namespace: 'db.books' + * name: 'insertOne', + * document: { title: 'Practical MongoDB Aggregations', authorName: 3 } // error `authorName` cannot be number + * }; + * ``` + * + * The type of the `namespace` field narrows other parts of the BulkWriteModel to use the correct schema for type assertions. + * + */ +export type ClientBulkWriteModel< + SchemaMap extends Record = Record +> = { + [Namespace in keyof SchemaMap]: AnyClientBulkWriteModel & { + namespace: Namespace; + }; +}[keyof SchemaMap]; + +/** @public */ +export interface ClientBulkWriteResult { + /** + * Whether the bulk write was acknowledged. + */ + readonly acknowledged: boolean; + /** + * The total number of documents inserted across all insert operations. + */ + readonly insertedCount: number; + /** + * The total number of documents upserted across all update operations. + */ + readonly upsertedCount: number; + /** + * The total number of documents matched across all update operations. + */ + readonly matchedCount: number; + /** + * The total number of documents modified across all update operations. + */ + readonly modifiedCount: number; + /** + * The total number of documents deleted across all delete operations. + */ + readonly deletedCount: number; + /** + * The results of each individual insert operation that was successfully performed. + */ + readonly insertResults?: ReadonlyMap; + /** + * The results of each individual update operation that was successfully performed. + */ + readonly updateResults?: ReadonlyMap; + /** + * The results of each individual delete operation that was successfully performed. + */ + readonly deleteResults?: ReadonlyMap; +} + +/** @public */ +export interface ClientBulkWriteError { + code: number; + message: string; +} + +/** @public */ +export interface ClientInsertOneResult { + /** + * The _id of the inserted document. + */ + insertedId: any; +} + +/** @public */ +export interface ClientUpdateResult { + /** + * The number of documents that matched the filter. + */ + matchedCount: number; + + /** + * The number of documents that were modified. + */ + modifiedCount: number; + + /** + * The _id field of the upserted document if an upsert occurred. + * + * It MUST be possible to discern between a BSON Null upserted ID value and this field being + * unset. If necessary, drivers MAY add a didUpsert boolean field to differentiate between + * these two cases. + */ + upsertedId?: any; + + /** + * Determines if the upsert did include an _id, which includes the case of the _id being null. + */ + didUpsert: boolean; +} + +/** @public */ +export interface ClientDeleteResult { + /** + * The number of documents that were deleted. + */ + deletedCount: number; +} diff --git a/gateway/node_modules/mongodb/src/operations/client_bulk_write/executor.ts b/gateway/node_modules/mongodb/src/operations/client_bulk_write/executor.ts new file mode 100644 index 0000000000000000000000000000000000000000..6d12c79c25396fb1f422755709eefafca1ce898d --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/client_bulk_write/executor.ts @@ -0,0 +1,149 @@ +import { type Document } from '../../bson'; +import { CursorTimeoutContext, CursorTimeoutMode } from '../../cursor/abstract_cursor'; +import { ClientBulkWriteCursor } from '../../cursor/client_bulk_write_cursor'; +import { + MongoClientBulkWriteError, + MongoClientBulkWriteExecutionError, + MongoInvalidArgumentError, + MongoServerError +} from '../../error'; +import { type MongoClient } from '../../mongo_client'; +import { TimeoutContext } from '../../timeout'; +import { resolveTimeoutOptions } from '../../utils'; +import { WriteConcern } from '../../write_concern'; +import { executeOperation } from '../execute_operation'; +import { ClientBulkWriteOperation } from './client_bulk_write'; +import { ClientBulkWriteCommandBuilder } from './command_builder'; +import { + type AnyClientBulkWriteModel, + type ClientBulkWriteOptions, + type ClientBulkWriteResult +} from './common'; +import { ClientBulkWriteResultsMerger } from './results_merger'; + +/** + * Responsible for executing a client bulk write. + * @internal + */ +export class ClientBulkWriteExecutor { + private readonly client: MongoClient; + private readonly options: ClientBulkWriteOptions; + private readonly operations: ReadonlyArray>; + + /** + * Instantiate the executor. + * @param client - The mongo client. + * @param operations - The user supplied bulk write models. + * @param options - The bulk write options. + */ + constructor( + client: MongoClient, + operations: ReadonlyArray>, + options?: ClientBulkWriteOptions + ) { + if (operations.length === 0) { + throw new MongoClientBulkWriteExecutionError('No client bulk write models were provided.'); + } + + this.client = client; + this.operations = operations; + this.options = { + ordered: true, + bypassDocumentValidation: false, + verboseResults: false, + ...options + }; + + // If no write concern was provided, we inherit one from the client. + if (!this.options.writeConcern) { + this.options.writeConcern = WriteConcern.fromOptions(this.client.s.options); + } + + if (this.options.writeConcern?.w === 0) { + if (this.options.verboseResults) { + throw new MongoInvalidArgumentError( + 'Cannot request unacknowledged write concern and verbose results' + ); + } + + if (this.options.ordered) { + throw new MongoInvalidArgumentError( + 'Cannot request unacknowledged write concern and ordered writes' + ); + } + } + } + + /** + * Execute the client bulk write. Will split commands into batches and exhaust the cursors + * for each, then merge the results into one. + * @returns The result. + */ + async execute(): Promise { + // The command builder will take the user provided models and potential split the batch + // into multiple commands due to size. + const pkFactory = this.client.s.options.pkFactory; + const commandBuilder = new ClientBulkWriteCommandBuilder( + this.operations, + this.options, + pkFactory + ); + // Unacknowledged writes need to execute all batches and return { ok: 1} + const resolvedOptions = resolveTimeoutOptions(this.client, this.options); + const context = TimeoutContext.create(resolvedOptions); + + if (this.options.writeConcern?.w === 0) { + while (commandBuilder.hasNextBatch()) { + const operation = new ClientBulkWriteOperation(commandBuilder, this.options); + await executeOperation(this.client, operation, context); + } + return ClientBulkWriteResultsMerger.unacknowledged(); + } else { + const resultsMerger = new ClientBulkWriteResultsMerger(this.options); + // For each command will will create and exhaust a cursor for the results. + while (commandBuilder.hasNextBatch()) { + const cursorContext = new CursorTimeoutContext(context, Symbol()); + const options = { + ...this.options, + timeoutContext: cursorContext, + ...(resolvedOptions.timeoutMS != null && { timeoutMode: CursorTimeoutMode.LIFETIME }) + }; + const cursor = new ClientBulkWriteCursor(this.client, commandBuilder, options); + try { + await resultsMerger.merge(cursor); + } catch (error) { + // Write concern errors are recorded in the writeConcernErrors field on MongoClientBulkWriteError. + // When a write concern error is encountered, it should not terminate execution of the bulk write + // for either ordered or unordered bulk writes. However, drivers MUST throw an exception at the end + // of execution if any write concern errors were observed. + if (error instanceof MongoServerError && !(error instanceof MongoClientBulkWriteError)) { + // Server side errors need to be wrapped inside a MongoClientBulkWriteError, where the root + // cause is the error property and a partial result is to be included. + const bulkWriteError = new MongoClientBulkWriteError({ + message: 'Mongo client bulk write encountered an error during execution' + }); + bulkWriteError.cause = error; + bulkWriteError.partialResult = resultsMerger.bulkWriteResult; + throw bulkWriteError; + } else { + // Client side errors are just thrown. + throw error; + } + } + } + + // If we have write concern errors or unordered write errors at the end we throw. + if (resultsMerger.writeConcernErrors.length > 0 || resultsMerger.writeErrors.size > 0) { + const error = new MongoClientBulkWriteError({ + message: 'Mongo client bulk write encountered errors during execution.' + }); + error.writeConcernErrors = resultsMerger.writeConcernErrors; + error.writeErrors = resultsMerger.writeErrors; + error.partialResult = resultsMerger.bulkWriteResult; + throw error; + } + + return resultsMerger.bulkWriteResult; + } + } +} diff --git a/gateway/node_modules/mongodb/src/operations/client_bulk_write/results_merger.ts b/gateway/node_modules/mongodb/src/operations/client_bulk_write/results_merger.ts new file mode 100644 index 0000000000000000000000000000000000000000..1a0f0a9f36ba4dff11106c63b37b2446e7abeb1a --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/client_bulk_write/results_merger.ts @@ -0,0 +1,260 @@ +import { MongoWriteConcernError } from '../..'; +import { type Document } from '../../bson'; +import { type ClientBulkWriteCursor } from '../../cursor/client_bulk_write_cursor'; +import { MongoClientBulkWriteError } from '../../error'; +import { + type ClientBulkWriteError, + type ClientBulkWriteOptions, + type ClientBulkWriteResult, + type ClientDeleteResult, + type ClientInsertOneResult, + type ClientUpdateResult +} from './common'; + +/** + * Unacknowledged bulk writes are always the same. + */ +const UNACKNOWLEDGED = { + acknowledged: false, + insertedCount: 0, + upsertedCount: 0, + matchedCount: 0, + modifiedCount: 0, + deletedCount: 0, + insertResults: undefined, + updateResults: undefined, + deleteResults: undefined +}; + +interface ClientBulkWriteResultAccumulation { + /** + * Whether the bulk write was acknowledged. + */ + acknowledged: boolean; + /** + * The total number of documents inserted across all insert operations. + */ + insertedCount: number; + /** + * The total number of documents upserted across all update operations. + */ + upsertedCount: number; + /** + * The total number of documents matched across all update operations. + */ + matchedCount: number; + /** + * The total number of documents modified across all update operations. + */ + modifiedCount: number; + /** + * The total number of documents deleted across all delete operations. + */ + deletedCount: number; + /** + * The results of each individual insert operation that was successfully performed. + */ + insertResults?: Map; + /** + * The results of each individual update operation that was successfully performed. + */ + updateResults?: Map; + /** + * The results of each individual delete operation that was successfully performed. + */ + deleteResults?: Map; +} + +/** + * Merges client bulk write cursor responses together into a single result. + * @internal + */ +export class ClientBulkWriteResultsMerger { + private result: ClientBulkWriteResultAccumulation; + private options: ClientBulkWriteOptions; + private currentBatchOffset: number; + writeConcernErrors: Document[]; + writeErrors: Map; + + /** + * @returns The standard unacknowledged bulk write result. + */ + static unacknowledged(): ClientBulkWriteResult { + return UNACKNOWLEDGED; + } + + /** + * Instantiate the merger. + * @param options - The options. + */ + constructor(options: ClientBulkWriteOptions) { + this.options = options; + this.currentBatchOffset = 0; + this.writeConcernErrors = []; + this.writeErrors = new Map(); + this.result = { + acknowledged: true, + insertedCount: 0, + upsertedCount: 0, + matchedCount: 0, + modifiedCount: 0, + deletedCount: 0, + insertResults: undefined, + updateResults: undefined, + deleteResults: undefined + }; + + if (options.verboseResults) { + this.result.insertResults = new Map(); + this.result.updateResults = new Map(); + this.result.deleteResults = new Map(); + } + } + + /** + * Get the bulk write result object. + */ + get bulkWriteResult(): ClientBulkWriteResult { + return { + acknowledged: this.result.acknowledged, + insertedCount: this.result.insertedCount, + upsertedCount: this.result.upsertedCount, + matchedCount: this.result.matchedCount, + modifiedCount: this.result.modifiedCount, + deletedCount: this.result.deletedCount, + insertResults: this.result.insertResults, + updateResults: this.result.updateResults, + deleteResults: this.result.deleteResults + }; + } + + /** + * Merge the results in the cursor to the existing result. + * @param currentBatchOffset - The offset index to the original models. + * @param response - The cursor response. + * @param documents - The documents in the cursor. + * @returns The current result. + */ + async merge(cursor: ClientBulkWriteCursor): Promise { + let writeConcernErrorResult; + try { + for await (const document of cursor) { + // Only add to maps if ok: 1 + if (document.ok === 1) { + if (this.options.verboseResults) { + this.processDocument(cursor, document); + } + } else { + // If an individual write error is encountered during an ordered bulk write, drivers MUST + // record the error in writeErrors and immediately throw the exception. Otherwise, drivers + // MUST continue to iterate the results cursor and execute any further bulkWrite batches. + if (this.options.ordered) { + const error = new MongoClientBulkWriteError({ + message: 'Mongo client ordered bulk write encountered a write error.' + }); + error.writeErrors.set(document.idx + this.currentBatchOffset, { + code: document.code, + message: document.errmsg + }); + error.partialResult = this.result; + throw error; + } else { + this.writeErrors.set(document.idx + this.currentBatchOffset, { + code: document.code, + message: document.errmsg + }); + } + } + } + } catch (error) { + if (error instanceof MongoWriteConcernError) { + const result = error.result; + writeConcernErrorResult = { + insertedCount: result.nInserted, + upsertedCount: result.nUpserted, + matchedCount: result.nMatched, + modifiedCount: result.nModified, + deletedCount: result.nDeleted, + writeConcernError: result.writeConcernError + }; + if (this.options.verboseResults && result.cursor.firstBatch) { + for (const document of result.cursor.firstBatch) { + if (document.ok === 1) { + this.processDocument(cursor, document); + } + } + } + } else { + throw error; + } + } finally { + // Update the counts from the cursor response. + if (cursor.response) { + const response = cursor.response; + this.incrementCounts(response); + } + + // Increment the batch offset. + this.currentBatchOffset += cursor.operations.length; + } + + // If we have write concern errors ensure they are added. + if (writeConcernErrorResult) { + const writeConcernError = writeConcernErrorResult.writeConcernError as Document; + this.incrementCounts(writeConcernErrorResult); + this.writeConcernErrors.push({ + code: writeConcernError.code, + message: writeConcernError.errmsg + }); + } + + return this.result; + } + + /** + * Process an individual document in the results. + * @param cursor - The cursor. + * @param document - The document to process. + */ + private processDocument(cursor: ClientBulkWriteCursor, document: Document) { + // Get the corresponding operation from the command. + const operation = cursor.operations[document.idx]; + // Handle insert results. + if ('insert' in operation) { + this.result.insertResults?.set(document.idx + this.currentBatchOffset, { + insertedId: operation.document._id + }); + } + // Handle update results. + if ('update' in operation) { + const result: ClientUpdateResult = { + matchedCount: document.n, + modifiedCount: document.nModified ?? 0, + // Check if the bulk did actually upsert. + didUpsert: document.upserted != null + }; + if (document.upserted) { + result.upsertedId = document.upserted._id; + } + this.result.updateResults?.set(document.idx + this.currentBatchOffset, result); + } + // Handle delete results. + if ('delete' in operation) { + this.result.deleteResults?.set(document.idx + this.currentBatchOffset, { + deletedCount: document.n + }); + } + } + + /** + * Increment the result counts. + * @param document - The document with the results. + */ + private incrementCounts(document: Document) { + this.result.insertedCount += document.insertedCount; + this.result.upsertedCount += document.upsertedCount; + this.result.matchedCount += document.matchedCount; + this.result.modifiedCount += document.modifiedCount; + this.result.deletedCount += document.deletedCount; + } +} diff --git a/gateway/node_modules/mongodb/src/operations/command.ts b/gateway/node_modules/mongodb/src/operations/command.ts new file mode 100644 index 0000000000000000000000000000000000000000..da499fa685b7f2f445978fa604f10030139e89a0 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/command.ts @@ -0,0 +1,171 @@ +import { type Connection } from '..'; +import type { BSONSerializeOptions, Document } from '../bson'; +import { MIN_SUPPORTED_RAW_DATA_WIRE_VERSION } from '../cmap/wire_protocol/constants'; +import { MongoInvalidArgumentError } from '../error'; +import { + decorateWithExplain, + Explain, + type ExplainOptions, + validateExplainTimeoutOptions +} from '../explain'; +import { ReadConcern } from '../read_concern'; +import type { ReadPreference } from '../read_preference'; +import type { ServerCommandOptions } from '../sdam/server'; +import type { ClientSession } from '../sessions'; +import { type TimeoutContext } from '../timeout'; +import { commandSupportsReadConcern, maxWireVersion, MongoDBNamespace } from '../utils'; +import { WriteConcern, type WriteConcernOptions } from '../write_concern'; +import type { ReadConcernLike } from './../read_concern'; +import { AbstractOperation, Aspect, type OperationOptions } from './operation'; + +/** @public */ +export interface CollationOptions { + locale: string; + caseLevel?: boolean; + caseFirst?: string; + strength?: number; + numericOrdering?: boolean; + alternate?: string; + maxVariable?: string; + backwards?: boolean; + normalization?: boolean; +} + +/** @public */ +export interface CommandOperationOptions + extends OperationOptions, + WriteConcernOptions, + ExplainOptions { + /** Specify a read concern and level for the collection. (only MongoDB 3.2 or higher supported) */ + readConcern?: ReadConcernLike; + /** Collation */ + collation?: CollationOptions; + /** + * maxTimeMS is a server-side time limit in milliseconds for processing an operation. + */ + maxTimeMS?: number; + /** + * Comment to apply to the operation. + * + * In server versions pre-4.4, 'comment' must be string. A server + * error will be thrown if any other type is provided. + * + * In server versions 4.4 and above, 'comment' can be any valid BSON type. + */ + comment?: unknown; + // Admin command overrides. + dbName?: string; + authdb?: string; + + /** + * Used when the command needs to grant access to the underlying namespaces for time series collections. + * Only available on server versions 8.2 and above and is not meant for public use. + * @internal + * @sinceServerVersion 8.2 + **/ + rawData?: boolean; +} + +/** @internal */ +export interface OperationParent { + s: { namespace: MongoDBNamespace }; + readConcern?: ReadConcern; + writeConcern?: WriteConcern; + readPreference?: ReadPreference; + bsonOptions?: BSONSerializeOptions; + timeoutMS?: number; +} + +/** @internal */ +export abstract class CommandOperation extends AbstractOperation { + override options: CommandOperationOptions; + readConcern?: ReadConcern; + writeConcern?: WriteConcern; + explain?: Explain; + + constructor(parent?: OperationParent, options?: CommandOperationOptions) { + super(options); + this.options = options ?? {}; + + // NOTE: this was explicitly added for the add/remove user operations, it's likely + // something we'd want to reconsider. Perhaps those commands can use `Admin` + // as a parent? + const dbNameOverride = options?.dbName || options?.authdb; + if (dbNameOverride) { + this.ns = new MongoDBNamespace(dbNameOverride, '$cmd'); + } else { + this.ns = parent + ? parent.s.namespace.withCollection('$cmd') + : new MongoDBNamespace('admin', '$cmd'); + } + + this.readConcern = ReadConcern.fromOptions(options); + this.writeConcern = WriteConcern.fromOptions(options); + + if (this.hasAspect(Aspect.EXPLAINABLE)) { + this.explain = Explain.fromOptions(options); + if (this.explain) validateExplainTimeoutOptions(this.options, this.explain); + } else if (options?.explain != null) { + throw new MongoInvalidArgumentError(`Option "explain" is not supported on this command`); + } + } + + override get canRetryWrite(): boolean { + if (this.hasAspect(Aspect.EXPLAINABLE)) { + return this.explain == null; + } + return super.canRetryWrite; + } + + abstract buildCommandDocument(connection: Connection, session?: ClientSession): Document; + + override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions { + return { + ...this.options, + ...this.bsonOptions, + timeoutContext, + readPreference: this.readPreference, + session: this.session + }; + } + + override buildCommand(connection: Connection, session?: ClientSession): Document { + const command = this.buildCommandDocument(connection, session); + + const inTransaction = this.session && this.session.inTransaction(); + + if (this.readConcern && commandSupportsReadConcern(command) && !inTransaction) { + Object.assign(command, { readConcern: this.readConcern }); + } + + if (this.writeConcern && this.hasAspect(Aspect.WRITE_OPERATION) && !inTransaction) { + WriteConcern.apply(command, this.writeConcern); + } + + if ( + this.options.collation && + typeof this.options.collation === 'object' && + !this.hasAspect(Aspect.SKIP_COLLATION) + ) { + Object.assign(command, { collation: this.options.collation }); + } + + if (typeof this.options.maxTimeMS === 'number') { + command.maxTimeMS = this.options.maxTimeMS; + } + + if ( + this.options.rawData != null && + this.hasAspect(Aspect.SUPPORTS_RAW_DATA) && + maxWireVersion(connection) >= MIN_SUPPORTED_RAW_DATA_WIRE_VERSION + ) { + command.rawData = this.options.rawData; + } + + if (this.hasAspect(Aspect.EXPLAINABLE) && this.explain) { + return decorateWithExplain(command, this.explain); + } + + return command; + } +} diff --git a/gateway/node_modules/mongodb/src/operations/count.ts b/gateway/node_modules/mongodb/src/operations/count.ts new file mode 100644 index 0000000000000000000000000000000000000000..500588e1e5cece9e531a3cf4ba4d38bf140a1370 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/count.ts @@ -0,0 +1,73 @@ +import { type Connection } from '..'; +import type { Document } from '../bson'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import type { ClientSession } from '../sessions'; +import type { MongoDBNamespace } from '../utils'; +import { CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects } from './operation'; + +/** @public */ +export interface CountOptions extends CommandOperationOptions { + /** The number of documents to skip. */ + skip?: number; + /** The maximum amounts to count before aborting. */ + limit?: number; + /** + * Number of milliseconds to wait before aborting the query. + */ + maxTimeMS?: number; + /** An index name hint for the query. */ + hint?: string | Document; +} + +/** @internal */ +export class CountOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: CountOptions; + collectionName?: string; + query: Document; + + constructor(namespace: MongoDBNamespace, filter: Document, options: CountOptions) { + super({ s: { namespace: namespace } }, options); + + this.options = options; + this.collectionName = namespace.collection; + this.query = filter; + } + + override get commandName() { + return 'count' as const; + } + + override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document { + const options = this.options; + const cmd: Document = { + count: this.collectionName, + query: this.query + }; + + if (typeof options.limit === 'number') { + cmd.limit = options.limit; + } + + if (typeof options.skip === 'number') { + cmd.skip = options.skip; + } + + if (options.hint != null) { + cmd.hint = options.hint; + } + + if (typeof options.maxTimeMS === 'number') { + cmd.maxTimeMS = options.maxTimeMS; + } + + return cmd; + } + + override handleOk(response: InstanceType): number { + return response.getNumber('n') ?? 0; + } +} + +defineAspects(CountOperation, [Aspect.READ_OPERATION, Aspect.RETRYABLE, Aspect.SUPPORTS_RAW_DATA]); diff --git a/gateway/node_modules/mongodb/src/operations/create_collection.ts b/gateway/node_modules/mongodb/src/operations/create_collection.ts new file mode 100644 index 0000000000000000000000000000000000000000..15733c911760666c6c0a9de0098c4c1e7e6931a8 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/create_collection.ts @@ -0,0 +1,218 @@ +import { type Connection } from '..'; +import type { Document } from '../bson'; +import { + MIN_SUPPORTED_QE_SERVER_VERSION, + MIN_SUPPORTED_QE_WIRE_VERSION +} from '../cmap/wire_protocol/constants'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import { Collection } from '../collection'; +import type { Db } from '../db'; +import { MongoCompatibilityError } from '../error'; +import type { PkFactory } from '../mongo_client'; +import type { ClientSession } from '../sessions'; +import { TimeoutContext } from '../timeout'; +import { maxWireVersion } from '../utils'; +import { CommandOperation, type CommandOperationOptions } from './command'; +import { executeOperation } from './execute_operation'; +import { CreateIndexesOperation } from './indexes'; +import { Aspect, defineAspects } from './operation'; + +const ILLEGAL_COMMAND_FIELDS = new Set([ + 'w', + 'wtimeout', + 'timeoutMS', + 'j', + 'fsync', + 'pkFactory', + 'raw', + 'readPreference', + 'session', + 'readConcern', + 'writeConcern', + 'raw', + 'fieldsAsRaw', + 'useBigInt64', + 'promoteLongs', + 'promoteValues', + 'promoteBuffers', + 'bsonRegExp', + 'serializeFunctions', + 'ignoreUndefined', + 'enableUtf8Validation' +]); + +/** @public + * Configuration options for timeseries collections + * @see https://www.mongodb.com/docs/manual/core/timeseries-collections/ + */ +export interface TimeSeriesCollectionOptions extends Document { + timeField: string; + metaField?: string; + granularity?: 'seconds' | 'minutes' | 'hours' | string; + bucketMaxSpanSeconds?: number; + bucketRoundingSeconds?: number; +} + +/** @public + * Configuration options for clustered collections + * @see https://www.mongodb.com/docs/manual/core/clustered-collections/ + */ +export interface ClusteredCollectionOptions extends Document { + name?: string; + key: Document; + unique: boolean; +} + +/** @public */ +export interface CreateCollectionOptions extends Omit { + /** Create a capped collection */ + capped?: boolean; + /** The size of the capped collection in bytes */ + size?: number; + /** The maximum number of documents in the capped collection */ + max?: number; + /** Available for the MMAPv1 storage engine only to set the usePowerOf2Sizes and the noPadding flag */ + flags?: number; + /** Allows users to specify configuration to the storage engine on a per-collection basis when creating a collection */ + storageEngine?: Document; + /** Allows users to specify validation rules or expressions for the collection. For more information, see Document Validation */ + validator?: Document; + /** Determines how strictly MongoDB applies the validation rules to existing documents during an update */ + validationLevel?: string; + /** Determines whether to error on invalid documents or just warn about the violations but allow invalid documents to be inserted */ + validationAction?: string; + /** Allows users to specify a default configuration for indexes when creating a collection */ + indexOptionDefaults?: Document; + /** The name of the source collection or view from which to create the view. The name is not the full namespace of the collection or view (i.e., does not include the database name and implies the same database as the view to create) */ + viewOn?: string; + /** An array that consists of the aggregation pipeline stage. Creates the view by applying the specified pipeline to the viewOn collection or view */ + pipeline?: Document[]; + /** A primary key factory function for generation of custom _id keys. */ + pkFactory?: PkFactory; + /** A document specifying configuration options for timeseries collections. */ + timeseries?: TimeSeriesCollectionOptions; + /** A document specifying configuration options for clustered collections. For MongoDB 5.3 and above. */ + clusteredIndex?: ClusteredCollectionOptions; + /** The number of seconds after which a document in a timeseries or clustered collection expires. */ + expireAfterSeconds?: number; + /** @experimental */ + encryptedFields?: Document; + /** + * If set, enables pre-update and post-update document events to be included for any + * change streams that listen on this collection. + */ + changeStreamPreAndPostImages?: { enabled: boolean }; +} + +/* @internal */ +const INVALID_QE_VERSION = + 'Driver support of Queryable Encryption is incompatible with server. Upgrade server to use Queryable Encryption.'; + +/** @internal */ +export class CreateCollectionOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: CreateCollectionOptions; + db: Db; + name: string; + + constructor(db: Db, name: string, options: CreateCollectionOptions = {}) { + super(db, options); + + this.options = options; + this.db = db; + this.name = name; + } + + override get commandName() { + return 'create' as const; + } + + override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document { + const isOptionValid = ([k, v]: [k: string, v: unknown]) => + v != null && typeof v !== 'function' && !ILLEGAL_COMMAND_FIELDS.has(k); + return { + create: this.name, + ...Object.fromEntries(Object.entries(this.options).filter(isOptionValid)) + }; + } + + override handleOk( + _response: InstanceType + ): Collection { + return new Collection(this.db, this.name, this.options); + } +} + +export async function createCollections( + db: Db, + name: string, + options: CreateCollectionOptions +): Promise> { + const timeoutContext = TimeoutContext.create({ + session: options.session, + serverSelectionTimeoutMS: db.client.s.options.serverSelectionTimeoutMS, + waitQueueTimeoutMS: db.client.s.options.waitQueueTimeoutMS, + timeoutMS: options.timeoutMS + }); + + const encryptedFields: Document | undefined = + options.encryptedFields ?? + db.client.s.options.autoEncryption?.encryptedFieldsMap?.[`${db.databaseName}.${name}`]; + + if (encryptedFields) { + class CreateSupportingFLEv2CollectionOperation extends CreateCollectionOperation { + override buildCommandDocument(connection: Connection, session?: ClientSession): Document { + if ( + !connection.description.loadBalanced && + maxWireVersion(connection) < MIN_SUPPORTED_QE_WIRE_VERSION + ) { + throw new MongoCompatibilityError( + `${INVALID_QE_VERSION} The minimum server version required is ${MIN_SUPPORTED_QE_SERVER_VERSION}` + ); + } + + return super.buildCommandDocument(connection, session); + } + } + + // Create auxilliary collections for queryable encryption support. + const escCollection = encryptedFields.escCollection ?? `enxcol_.${name}.esc`; + const ecocCollection = encryptedFields.ecocCollection ?? `enxcol_.${name}.ecoc`; + + for (const collectionName of [escCollection, ecocCollection]) { + const createOp = new CreateSupportingFLEv2CollectionOperation(db, collectionName, { + clusteredIndex: { + key: { _id: 1 }, + unique: true + }, + session: options.session + }); + await executeOperation(db.client, createOp, timeoutContext); + } + + if (!options.encryptedFields) { + options = { ...options, encryptedFields }; + } + } + + const coll = await executeOperation( + db.client, + new CreateCollectionOperation(db, name, options), + timeoutContext + ); + + if (encryptedFields) { + // Create the required index for queryable encryption support. + const createIndexOp = CreateIndexesOperation.fromIndexSpecification( + db, + name, + { __safeContent__: 1 }, + { session: options.session } + ); + await executeOperation(db.client, createIndexOp, timeoutContext); + } + + return coll as unknown as Collection; +} + +defineAspects(CreateCollectionOperation, [Aspect.WRITE_OPERATION]); diff --git a/gateway/node_modules/mongodb/src/operations/delete.ts b/gateway/node_modules/mongodb/src/operations/delete.ts new file mode 100644 index 0000000000000000000000000000000000000000..94b6435e1bbfd684c67b5ec2439450e272e207b7 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/delete.ts @@ -0,0 +1,183 @@ +import type { Document } from '../bson'; +import { type Connection } from '../cmap/connection'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import { MongoCompatibilityError, MongoServerError } from '../error'; +import type { ClientSession } from '../sessions'; +import { maxWireVersion, type MongoDBCollectionNamespace, type MongoDBNamespace } from '../utils'; +import { type WriteConcernOptions } from '../write_concern'; +import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects, type Hint } from './operation'; + +/** @public */ +export interface DeleteOptions extends CommandOperationOptions, WriteConcernOptions { + /** If true, when an insert fails, don't execute the remaining writes. If false, continue with remaining inserts when one fails. */ + ordered?: boolean; + /** Specifies the collation to use for the operation */ + collation?: CollationOptions; + /** Specify that the update query should only consider plans using the hinted index */ + hint?: string | Document; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; +} + +/** @public */ +export interface DeleteResult { + /** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined. */ + acknowledged: boolean; + /** The number of documents that were deleted */ + deletedCount: number; +} + +/** @public */ +export interface DeleteStatement { + /** The query that matches documents to delete. */ + q: Document; + /** The number of matching documents to delete. */ + limit: number; + /** Specifies the collation to use for the operation. */ + collation?: CollationOptions; + /** A document or string that specifies the index to use to support the query predicate. */ + hint?: Hint; +} + +/** @internal */ +export class DeleteOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: DeleteOptions; + statements: DeleteStatement[]; + + constructor(ns: MongoDBNamespace, statements: DeleteStatement[], options: DeleteOptions) { + super(undefined, options); + this.options = options; + this.ns = ns; + this.statements = statements; + } + + override get commandName() { + return 'delete' as const; + } + + override get canRetryWrite(): boolean { + if (super.canRetryWrite === false) { + return false; + } + + return this.statements.every(op => (op.limit != null ? op.limit > 0 : true)); + } + + override buildCommandDocument(connection: Connection, _session?: ClientSession): Document { + const options = this.options; + + const ordered = typeof options.ordered === 'boolean' ? options.ordered : true; + const command: Document = { + delete: this.ns.collection, + deletes: this.statements, + ordered + }; + + if (options.let) { + command.let = options.let; + } + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (options.comment !== undefined) { + command.comment = options.comment; + } + + const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0; + if (unacknowledgedWrite && maxWireVersion(connection) < 9) { + if (this.statements.find((o: Document) => o.hint)) { + throw new MongoCompatibilityError( + `hint for the delete command is only supported on MongoDB 4.4+` + ); + } + } + + return command; + } +} + +export class DeleteOneOperation extends DeleteOperation { + constructor(ns: MongoDBCollectionNamespace, filter: Document, options: DeleteOptions) { + super(ns, [makeDeleteStatement(filter, { ...options, limit: 1 })], options); + } + + override handleOk( + response: InstanceType + ): DeleteResult { + const res = super.handleOk(response); + + // @ts-expect-error Explain commands have broken TS + if (this.explain) return res; + + if (res.code) throw new MongoServerError(res); + if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]); + + return { + acknowledged: this.writeConcern?.w !== 0, + deletedCount: res.n + }; + } +} +export class DeleteManyOperation extends DeleteOperation { + constructor(ns: MongoDBCollectionNamespace, filter: Document, options: DeleteOptions) { + super(ns, [makeDeleteStatement(filter, options)], options); + } + + override handleOk( + response: InstanceType + ): DeleteResult { + const res = super.handleOk(response); + + // @ts-expect-error Explain commands have broken TS + if (this.explain) return res; + + if (res.code) throw new MongoServerError(res); + if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]); + + return { + acknowledged: this.writeConcern?.w !== 0, + deletedCount: res.n + }; + } +} + +export function makeDeleteStatement( + filter: Document, + options: DeleteOptions & { limit?: number } +): DeleteStatement { + const op: DeleteStatement = { + q: filter, + limit: typeof options.limit === 'number' ? options.limit : 0 + }; + + if (options.collation) { + op.collation = options.collation; + } + + if (options.hint) { + op.hint = options.hint; + } + + return op; +} + +defineAspects(DeleteOperation, [ + Aspect.RETRYABLE, + Aspect.WRITE_OPERATION, + Aspect.SUPPORTS_RAW_DATA +]); +defineAspects(DeleteOneOperation, [ + Aspect.RETRYABLE, + Aspect.WRITE_OPERATION, + Aspect.EXPLAINABLE, + Aspect.SKIP_COLLATION, + Aspect.SUPPORTS_RAW_DATA +]); +defineAspects(DeleteManyOperation, [ + Aspect.WRITE_OPERATION, + Aspect.EXPLAINABLE, + Aspect.SKIP_COLLATION, + Aspect.SUPPORTS_RAW_DATA +]); diff --git a/gateway/node_modules/mongodb/src/operations/distinct.ts b/gateway/node_modules/mongodb/src/operations/distinct.ts new file mode 100644 index 0000000000000000000000000000000000000000..1ed3f80de1fad6e2f43090d4d70bb046fa0a8495 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/distinct.ts @@ -0,0 +1,92 @@ +import { type Document } from '../bson'; +import { type Connection } from '../cmap/connection'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import type { Collection } from '../collection'; +import { CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects } from './operation'; + +/** @public */ +export type DistinctOptions = CommandOperationOptions & { + /** + * @sinceServerVersion 7.1 + * + * The index to use. Specify either the index name as a string or the index key pattern. + * If specified, then the query system will only consider plans using the hinted index. + * + * If provided as a string, `hint` must be index name for an index on the collection. + * If provided as an object, `hint` must be an index description for an index defined on the collection. + * + * See https://www.mongodb.com/docs/manual/reference/command/distinct/#command-fields. + */ + hint?: Document | string; +}; + +/** + * Return a list of distinct values for the given key across a collection. + * @internal + */ +export class DistinctOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: DistinctOptions; + collection: Collection; + /** Field of the document to find distinct values for. */ + key: string; + /** The query for filtering the set of documents to which we apply the distinct filter. */ + query: Document; + + /** + * Construct a Distinct operation. + * + * @param collection - Collection instance. + * @param key - Field of the document to find distinct values for. + * @param query - The query for filtering the set of documents to which we apply the distinct filter. + * @param options - Optional settings. See Collection.prototype.distinct for a list of options. + */ + constructor(collection: Collection, key: string, query: Document, options?: DistinctOptions) { + super(collection, options); + + this.options = options ?? {}; + this.collection = collection; + this.key = key; + this.query = query; + } + + override get commandName() { + return 'distinct' as const; + } + + override buildCommandDocument(_connection: Connection): Document { + const command: Document = { + distinct: this.collection.collectionName, + key: this.key, + query: this.query + }; + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (this.options.comment !== undefined) { + command.comment = this.options.comment; + } + + if (this.options.hint != null) { + command.hint = this.options.hint; + } + + return command; + } + + override handleOk( + response: InstanceType + ): any[] | Document { + if (this.explain) { + return response.toObject(this.bsonOptions); + } + return response.toObject(this.bsonOptions).values; + } +} + +defineAspects(DistinctOperation, [ + Aspect.READ_OPERATION, + Aspect.RETRYABLE, + Aspect.EXPLAINABLE, + Aspect.SUPPORTS_RAW_DATA +]); diff --git a/gateway/node_modules/mongodb/src/operations/drop.ts b/gateway/node_modules/mongodb/src/operations/drop.ts new file mode 100644 index 0000000000000000000000000000000000000000..5733b6b956d8a3b4f1a7ebf7dcbd39a00a48adfa --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/drop.ts @@ -0,0 +1,130 @@ +import { type Connection, type MongoError, MongoServerError } from '..'; +import type { Document } from '../bson'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import { CursorTimeoutContext } from '../cursor/abstract_cursor'; +import type { Db } from '../db'; +import { MONGODB_ERROR_CODES } from '../error'; +import type { ClientSession } from '../sessions'; +import { TimeoutContext } from '../timeout'; +import { CommandOperation, type CommandOperationOptions } from './command'; +import { executeOperation } from './execute_operation'; +import { Aspect, defineAspects } from './operation'; + +/** @public */ +export interface DropCollectionOptions extends Omit { + /** @experimental */ + encryptedFields?: Document; +} + +/** @internal */ +export class DropCollectionOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + + override options: DropCollectionOptions; + name: string; + + constructor(db: Db, name: string, options: DropCollectionOptions = {}) { + super(db, options); + this.options = options; + this.name = name; + } + + override get commandName() { + return 'drop' as const; + } + + override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document { + return { drop: this.name }; + } + + override handleOk(_response: InstanceType): boolean { + return true; + } + + override handleError(error: MongoError): boolean { + if (!(error instanceof MongoServerError)) throw error; + if (Number(error.code) !== MONGODB_ERROR_CODES.NamespaceNotFound) throw error; + + return false; + } +} + +export async function dropCollections( + db: Db, + name: string, + options: DropCollectionOptions +): Promise { + const timeoutContext = TimeoutContext.create({ + session: options.session, + serverSelectionTimeoutMS: db.client.s.options.serverSelectionTimeoutMS, + waitQueueTimeoutMS: db.client.s.options.waitQueueTimeoutMS, + timeoutMS: options.timeoutMS + }); + + const encryptedFieldsMap = db.client.s.options.autoEncryption?.encryptedFieldsMap; + let encryptedFields: Document | undefined = + options.encryptedFields ?? encryptedFieldsMap?.[`${db.databaseName}.${name}`]; + + if (!encryptedFields && encryptedFieldsMap) { + // If the MongoClient was configured with an encryptedFieldsMap, + // and no encryptedFields config was available in it or explicitly + // passed as an argument, the spec tells us to look one up using + // listCollections(). + const listCollectionsResult = await db + .listCollections( + { name }, + { + nameOnly: false, + session: options.session, + timeoutContext: new CursorTimeoutContext(timeoutContext, Symbol()) + } + ) + .toArray(); + encryptedFields = listCollectionsResult?.[0]?.options?.encryptedFields; + } + + if (encryptedFields) { + const escCollection = encryptedFields.escCollection || `enxcol_.${name}.esc`; + const ecocCollection = encryptedFields.ecocCollection || `enxcol_.${name}.ecoc`; + + for (const collectionName of [escCollection, ecocCollection]) { + // Drop auxilliary collections, ignoring potential NamespaceNotFound errors. + const dropOp = new DropCollectionOperation(db, collectionName, options); + await executeOperation(db.client, dropOp, timeoutContext); + } + } + + return await executeOperation( + db.client, + new DropCollectionOperation(db, name, options), + timeoutContext + ); +} + +/** @public */ +export type DropDatabaseOptions = CommandOperationOptions; + +/** @internal */ +export class DropDatabaseOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: DropDatabaseOptions; + + constructor(db: Db, options: DropDatabaseOptions) { + super(db, options); + this.options = options; + } + override get commandName() { + return 'dropDatabase' as const; + } + + override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document { + return { dropDatabase: 1 }; + } + + override handleOk(_response: InstanceType): boolean { + return true; + } +} + +defineAspects(DropCollectionOperation, [Aspect.WRITE_OPERATION]); +defineAspects(DropDatabaseOperation, [Aspect.WRITE_OPERATION]); diff --git a/gateway/node_modules/mongodb/src/operations/end_sessions.ts b/gateway/node_modules/mongodb/src/operations/end_sessions.ts new file mode 100644 index 0000000000000000000000000000000000000000..564dd596ba3d0cd1ef65976bc23a2a82f43c8280 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/end_sessions.ts @@ -0,0 +1,44 @@ +import { + type ClientSession, + type Connection, + type ServerCommandOptions, + type ServerSessionId, + type TimeoutContext, + type WriteConcern +} from '..'; +import { type Document } from '../bson'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import { CommandOperation } from '../operations/command'; +import { ReadPreference } from '../read_preference'; +import { MongoDBNamespace } from '../utils'; +import { Aspect, defineAspects } from './operation'; + +export class EndSessionsOperation extends CommandOperation { + override writeConcern: WriteConcern = { w: 0 }; + override ns = MongoDBNamespace.fromString('admin.$cmd'); + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + + private sessions: Array; + + constructor(sessions: Array) { + super(); + this.sessions = sessions; + } + + override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document { + return { + endSessions: this.sessions + }; + } + override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions { + return { + timeoutContext, + readPreference: ReadPreference.primaryPreferred + }; + } + override get commandName(): string { + return 'endSessions'; + } +} + +defineAspects(EndSessionsOperation, Aspect.WRITE_OPERATION); diff --git a/gateway/node_modules/mongodb/src/operations/estimated_document_count.ts b/gateway/node_modules/mongodb/src/operations/estimated_document_count.ts new file mode 100644 index 0000000000000000000000000000000000000000..2541b69b2af08625e848d1e6d29fe4fbf0621878 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/estimated_document_count.ts @@ -0,0 +1,61 @@ +import { type Connection } from '..'; +import type { Document } from '../bson'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import type { Collection } from '../collection'; +import type { ClientSession } from '../sessions'; +import { CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects } from './operation'; + +/** @public */ +export interface EstimatedDocumentCountOptions extends CommandOperationOptions { + /** + * The maximum amount of time to allow the operation to run. + * + * This option is sent only if the caller explicitly provides a value. The default is to not send a value. + */ + maxTimeMS?: number; +} + +/** @internal */ +export class EstimatedDocumentCountOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: EstimatedDocumentCountOptions; + collectionName: string; + + constructor(collection: Collection, options: EstimatedDocumentCountOptions = {}) { + super(collection, options); + this.options = options; + this.collectionName = collection.collectionName; + } + + override get commandName() { + return 'count' as const; + } + + override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document { + const cmd: Document = { count: this.collectionName }; + + if (typeof this.options.maxTimeMS === 'number') { + cmd.maxTimeMS = this.options.maxTimeMS; + } + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (this.options.comment !== undefined) { + cmd.comment = this.options.comment; + } + + return cmd; + } + + override handleOk(response: InstanceType): number { + return response.getNumber('n') ?? 0; + } +} + +defineAspects(EstimatedDocumentCountOperation, [ + Aspect.READ_OPERATION, + Aspect.RETRYABLE, + Aspect.CURSOR_CREATING, + Aspect.SUPPORTS_RAW_DATA +]); diff --git a/gateway/node_modules/mongodb/src/operations/execute_operation.ts b/gateway/node_modules/mongodb/src/operations/execute_operation.ts new file mode 100644 index 0000000000000000000000000000000000000000..64ce92d52b2dbdad4db920bf1c290c5284b3fc90 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/execute_operation.ts @@ -0,0 +1,420 @@ +import { setTimeout } from 'timers/promises'; + +import { MIN_SUPPORTED_SNAPSHOT_READS_WIRE_VERSION } from '../cmap/wire_protocol/constants'; +import { + isRetryableReadError, + isRetryableWriteError, + MongoCompatibilityError, + MONGODB_ERROR_CODES, + MongoError, + MongoErrorLabel, + MongoExpiredSessionError, + MongoInvalidArgumentError, + MongoNetworkError, + MongoNotConnectedError, + MongoRuntimeError, + MongoServerError, + MongoTransactionError, + MongoUnexpectedServerResponseError +} from '../error'; +import type { MongoClient } from '../mongo_client'; +import { ReadPreference } from '../read_preference'; +import { TopologyType } from '../sdam/common'; +import { + DeprioritizedServers, + sameServerSelector, + secondaryWritableServerSelector, + type ServerSelector +} from '../sdam/server_selection'; +import type { Topology } from '../sdam/topology'; +import type { ClientSession } from '../sessions'; +import { TimeoutContext } from '../timeout'; +import { abortable, maxWireVersion, supportsRetryableWrites } from '../utils'; +import { AggregateOperation } from './aggregate'; +import { AbstractOperation, Aspect } from './operation'; +import { RunCommandOperation } from './run_command'; + +const MMAPv1_RETRY_WRITES_ERROR_CODE = MONGODB_ERROR_CODES.IllegalOperation; +const MMAPv1_RETRY_WRITES_ERROR_MESSAGE = + 'This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.'; + +type ResultTypeFromOperation = ReturnType< + TOperation['handleOk'] +>; + +/** + * Executes the given operation with provided arguments. + * @internal + * + * @remarks + * Allows for a single point of entry to provide features such as implicit sessions, which + * are required by the Driver Sessions specification in the event that a ClientSession is + * not provided. + * + * The expectation is that this function: + * - Connects the MongoClient if it has not already been connected, see {@link autoConnect} + * - Creates a session if none is provided and cleans up the session it creates + * - Tries an operation and retries under certain conditions, see {@link executeOperationWithRetries} + * + * @typeParam T - The operation's type + * @typeParam TResult - The type of the operation's result, calculated from T + * + * @param client - The MongoClient to execute this operation with + * @param operation - The operation to execute + */ +export async function executeOperation< + T extends AbstractOperation, + TResult = ResultTypeFromOperation +>(client: MongoClient, operation: T, timeoutContext?: TimeoutContext | null): Promise { + if (!(operation instanceof AbstractOperation)) { + // TODO(NODE-3483): Extend MongoRuntimeError + throw new MongoRuntimeError('This method requires a valid operation instance'); + } + + const topology = + client.topology == null + ? await abortable(autoConnect(client), operation.options) + : client.topology; + + // The driver sessions spec mandates that we implicitly create sessions for operations + // that are not explicitly provided with a session. + let session = operation.session; + let owner: symbol | undefined; + + if (session == null) { + owner = Symbol(); + session = client.startSession({ owner, explicit: false }); + } else if (session.hasEnded) { + throw new MongoExpiredSessionError('Use of expired sessions is not permitted'); + } else if ( + session.snapshotEnabled && + maxWireVersion(topology) < MIN_SUPPORTED_SNAPSHOT_READS_WIRE_VERSION + ) { + throw new MongoCompatibilityError('Snapshot reads require MongoDB 5.0 or later'); + } else if (session.client !== client) { + throw new MongoInvalidArgumentError('ClientSession must be from the same MongoClient'); + } + + operation.session ??= session; + + const readPreference = operation.readPreference ?? ReadPreference.primary; + const inTransaction = !!session?.inTransaction(); + + const hasReadAspect = operation.hasAspect(Aspect.READ_OPERATION); + + if ( + inTransaction && + !readPreference.equals(ReadPreference.primary) && + (hasReadAspect || operation.commandName === 'runCommand') + ) { + throw new MongoTransactionError( + `Read preference in a transaction must be primary, not: ${readPreference.mode}` + ); + } + + if (session?.isPinned && session.transaction.isCommitted && !operation.bypassPinningCheck) { + session.unpin(); + } + + timeoutContext ??= TimeoutContext.create({ + session, + serverSelectionTimeoutMS: client.s.options.serverSelectionTimeoutMS, + waitQueueTimeoutMS: client.s.options.waitQueueTimeoutMS, + timeoutMS: operation.options.timeoutMS + }); + + try { + return await executeOperationWithRetries(operation, { + topology, + timeoutContext, + session, + readPreference + }); + } finally { + if (session?.owner != null && session.owner === owner) { + await session.endSession(); + } + } +} + +/** + * Connects a client if it has not yet been connected + * @internal + */ +export async function autoConnect(client: MongoClient): Promise { + if (client.topology == null) { + if (client.s.hasBeenClosed) { + throw new MongoNotConnectedError('Client must be connected before running operations'); + } + client.s.options.__skipPingOnConnect = true; + try { + await client.connect(); + if (client.topology == null) { + throw new MongoRuntimeError( + 'client.connect did not create a topology but also did not throw' + ); + } + return client.topology; + } finally { + delete client.s.options.__skipPingOnConnect; + } + } + return client.topology; +} + +/** @internal */ +type RetryOptions = { + session: ClientSession | undefined; + readPreference: ReadPreference; + topology: Topology; + timeoutContext: TimeoutContext; +}; +/** @internal The base backoff duration in milliseconds */ +const BASE_BACKOFF_MS = 100; +/** @internal The maximum backoff duration in milliseconds */ +const MAX_BACKOFF_MS = 10_000; + +/** + * Executes an operation and retries as appropriate + * @internal + * + * @remarks + * Implements behaviour described in [Retryable Reads](https://github.com/mongodb/specifications/blob/master/source/retryable-reads/retryable-reads.md) and [Retryable + * Writes](https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.md) specification + * + * This function: + * - performs initial server selection + * - attempts to execute an operation + * - retries the operation if it meets the criteria for a retryable read or a retryable write + * + * @typeParam T - The operation's type + * @typeParam TResult - The type of the operation's result, calculated from T + * + * @param operation - The operation to execute + */ +async function executeOperationWithRetries< + T extends AbstractOperation, + TResult = ResultTypeFromOperation +>( + operation: T, + { topology, timeoutContext, session, readPreference }: RetryOptions +): Promise { + let selector: ReadPreference | ServerSelector; + + if (operation.hasAspect(Aspect.MUST_SELECT_SAME_SERVER)) { + // GetMore and KillCursor operations must always select the same server, but run through + // server selection to potentially force monitor checks if the server is + // in an unknown state. + selector = sameServerSelector(operation.server?.description); + } else if (operation instanceof AggregateOperation && operation.hasWriteStage) { + // If operation should try to write to secondary use the custom server selector + // otherwise provide the read preference. + selector = secondaryWritableServerSelector(topology.commonWireVersion, readPreference); + } else { + selector = readPreference; + } + + let server = await topology.selectServer(selector, { + session, + operationName: operation.commandName, + timeoutContext, + signal: operation.options.signal, + deprioritizedServers: new DeprioritizedServers() + }); + + const hasReadAspect = operation.hasAspect(Aspect.READ_OPERATION); + const hasWriteAspect = operation.hasAspect(Aspect.WRITE_OPERATION); + const inTransaction = session?.inTransaction() ?? false; + + const willRetryRead = topology.s.options.retryReads && !inTransaction && operation.canRetryRead; + + const willRetryWrite = + topology.s.options.retryWrites && + !inTransaction && + supportsRetryableWrites(server) && + operation.canRetryWrite; + + const willRetry = + operation.hasAspect(Aspect.RETRYABLE) && + session != null && + ((hasReadAspect && willRetryRead) || (hasWriteAspect && willRetryWrite)); + + if (hasWriteAspect && willRetryWrite && session != null) { + operation.options.willRetryWrite = true; + session.incrementTransactionNumber(); + } + + const deprioritizedServers = new DeprioritizedServers(); + + let maxAttempts = + typeof operation.maxAttempts === 'number' + ? operation.maxAttempts + : willRetry + ? timeoutContext.csotEnabled() + ? Infinity + : 2 + : 1; + + let error: MongoError | null = null; + + for (let attempt = 0; attempt < maxAttempts; attempt++) { + operation.attemptsMade = attempt + 1; + operation.server = server; + + try { + try { + const result = await server.command(operation, timeoutContext); + return operation.handleOk(result); + } catch (error) { + return operation.handleError(error); + } + } catch (operationError) { + // Should never happen but if it does - propagate the error. + if (!(operationError instanceof MongoError)) throw operationError; + + // Preserve the original error once a write has been performed. + // Only update to the latest error if no writes were performed. + if (error == null) { + error = operationError; + } else { + if (!operationError.hasErrorLabel(MongoErrorLabel.NoWritesPerformed)) { + error = operationError; + } + } + + // Reset timeouts + timeoutContext.clear(); + + if (hasWriteAspect && operationError.code === MMAPv1_RETRY_WRITES_ERROR_CODE) { + throw new MongoServerError({ + message: MMAPv1_RETRY_WRITES_ERROR_MESSAGE, + errmsg: MMAPv1_RETRY_WRITES_ERROR_MESSAGE, + originalError: operationError + }); + } + + if (!canRetry(operation, operationError)) { + throw error; + } + + if (operationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError)) { + const maxOverloadAttempts = topology.s.options.maxAdaptiveRetries + 1; + maxAttempts = Math.min(maxOverloadAttempts, operation.maxAttempts ?? maxOverloadAttempts); + } + + if (attempt + 1 >= maxAttempts) { + throw error; + } + + if ( + operationError instanceof MongoNetworkError && + operation.hasAspect(Aspect.CURSOR_CREATING) && + session != null && + session.isPinned && + !session.inTransaction() + ) { + session.unpin({ force: true, forceClear: true }); + } + + if ( + operationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError) && + operation.hasAspect(Aspect.CURSOR_CREATING) && + session != null && + session.isPinned && + !session.inTransaction() + ) { + session.unpin({ force: true }); + } + + if (operationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError)) { + const backoffMS = Math.random() * Math.min(MAX_BACKOFF_MS, BASE_BACKOFF_MS * 2 ** attempt); + + // if the backoff would exhaust the CSOT timeout, short-circuit. + if (timeoutContext.csotEnabled() && backoffMS > timeoutContext.remainingTimeMS) { + throw error; + } + + await setTimeout(backoffMS); + } + + if ( + topology.description.type === TopologyType.Sharded || + (operationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError) && + topology.s.options.enableOverloadRetargeting) + ) { + deprioritizedServers.add(server.description); + } + + server = await topology.selectServer(selector, { + session, + operationName: operation.commandName, + deprioritizedServers, + signal: operation.options.signal + }); + + if ( + hasWriteAspect && + !supportsRetryableWrites(server) && + !operationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError) + ) { + throw new MongoUnexpectedServerResponseError( + 'Selected server does not support retryable writes' + ); + } + + // Batched operations must reset the batch before retry, + // otherwise building a command will build the _next_ batch, not the current batch. + if (operation.hasAspect(Aspect.COMMAND_BATCHING)) { + operation.resetBatch(); + } + } + } + + throw ( + error ?? + new MongoRuntimeError( + 'Should never happen: operation execution loop terminated but no error was recorded.' + ) + ); + + function canRetry(operation: AbstractOperation, error: MongoError) { + // SystemOverloadedError is retryable, but must respect retryReads/retryWrites settings + // Check topology options directly (not operation.canRetryRead/Write) because backpressure + // expands retry support beyond traditional retryable reads/writes + // NOTE: Unlike traditional retries, backpressure retries ARE allowed inside transactions + if ( + error.hasErrorLabel(MongoErrorLabel.SystemOverloadedError) && + error.hasErrorLabel(MongoErrorLabel.RetryableError) + ) { + // runCommand requires BOTH retryReads and retryWrites to be enabled (per spec step 2.4) + if (operation instanceof RunCommandOperation) { + return topology.s.options.retryReads && topology.s.options.retryWrites; + } + + // Write-stage aggregates ($out/$merge) require retryWrites + if (operation instanceof AggregateOperation && operation.hasWriteStage) { + return topology.s.options.retryWrites; + } + + // For other operations, check if retries are enabled based on operation type + const canRetryAsRead = hasReadAspect && topology.s.options.retryReads; + const canRetryAsWrite = hasWriteAspect && topology.s.options.retryWrites; + return canRetryAsRead || canRetryAsWrite; + } + + // run command is only retryable if we get retryable overload errors + if (operation instanceof RunCommandOperation) { + return false; + } + + // batch operations are only retryable if the batch is retryable + if (operation.hasAspect(Aspect.COMMAND_BATCHING)) { + return operation.canRetryWrite && isRetryableWriteError(error); + } + + return ( + (hasWriteAspect && willRetryWrite && isRetryableWriteError(error)) || + (hasReadAspect && willRetryRead && isRetryableReadError(error)) + ); + } +} diff --git a/gateway/node_modules/mongodb/src/operations/find.ts b/gateway/node_modules/mongodb/src/operations/find.ts new file mode 100644 index 0000000000000000000000000000000000000000..6c9f8dafe3a87d0e6973aa8402f3f33af4d697e7 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/find.ts @@ -0,0 +1,254 @@ +import type { Document } from '../bson'; +import { CursorResponse, ExplainedCursorResponse } from '../cmap/wire_protocol/responses'; +import { type AbstractCursorOptions, type CursorTimeoutMode } from '../cursor/abstract_cursor'; +import { MongoInvalidArgumentError } from '../error'; +import { type ExplainOptions } from '../explain'; +import type { ServerCommandOptions } from '../sdam/server'; +import { formatSort, type Sort } from '../sort'; +import { type TimeoutContext } from '../timeout'; +import { type MongoDBNamespace, normalizeHintField } from '../utils'; +import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects, type Hint } from './operation'; + +/** + * @public + */ +export interface FindOptions + extends Omit, + AbstractCursorOptions { + /** Sets the limit of documents returned in the query. */ + limit?: number; + /** Set to sort the documents coming back from the query. Array of indexes, `[['a', 1]]` etc. */ + sort?: Sort; + /** The fields to return in the query. Object of fields to either include or exclude (one of, not both), `{'a':1, 'b': 1}` **or** `{'a': 0, 'b': 0}` */ + projection?: Document; + /** Set to skip N documents ahead in your query (useful for pagination). */ + skip?: number; + /** Tell the query to use specific indexes in the query. Object of indexes to use, `{'_id':1}` */ + hint?: Hint; + /** Specify if the cursor can timeout. */ + timeout?: boolean; + /** Specify if the cursor is tailable. */ + tailable?: boolean; + /** Specify if the cursor is a tailable-await cursor. Requires `tailable` to be true */ + awaitData?: boolean; + /** Set the batchSize for the getMoreCommand when iterating over the query results. */ + batchSize?: number; + /** If true, returns only the index keys in the resulting documents. */ + returnKey?: boolean; + /** The inclusive lower bound for a specific index */ + min?: Document; + /** The exclusive upper bound for a specific index */ + max?: Document; + /** Number of milliseconds to wait before aborting the query. */ + maxTimeMS?: number; + /** The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query. Requires `tailable` and `awaitData` to be true */ + maxAwaitTimeMS?: number; + /** The server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use. Set this option to prevent that. */ + noCursorTimeout?: boolean; + /** Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields). */ + collation?: CollationOptions; + /** Allows disk use for blocking sort operations exceeding 100MB memory. (MongoDB 3.2 or higher) */ + allowDiskUse?: boolean; + /** Determines whether to close the cursor after the first batch. Defaults to false. */ + singleBatch?: boolean; + /** For queries against a sharded collection, allows the command (or subsequent getMore commands) to return partial results, rather than an error, if one or more queried shards are unavailable. */ + allowPartialResults?: boolean; + /** Determines whether to return the record identifier for each document. If true, adds a field $recordId to the returned documents. */ + showRecordId?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /** + * Option to enable an optimized code path for queries looking for a particular range of `ts` values in the oplog. Requires `tailable` to be true. + * @deprecated Starting from MongoDB 4.4 this flag is not needed and will be ignored. + */ + oplogReplay?: boolean; + + /** + * Specifies the verbosity mode for the explain output. + * @deprecated This API is deprecated in favor of `collection.find().explain()`. + */ + explain?: ExplainOptions['explain']; + /** @internal*/ + timeoutMode?: CursorTimeoutMode; +} + +/** @public */ +export type FindOneOptions = Omit; + +/** @internal */ +export class FindOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = CursorResponse; + + /** + * @remarks WriteConcern can still be present on the options because + * we inherit options from the client/db/collection. The + * key must be present on the options in order to delete it. + * This allows typescript to delete the key but will + * not allow a writeConcern to be assigned as a property on options. + */ + override options: FindOptions & { writeConcern?: never }; + filter: Document; + + constructor(ns: MongoDBNamespace, filter: Document = {}, options: FindOptions = {}) { + super(undefined, options); + + this.options = { ...options }; + delete this.options.writeConcern; + this.ns = ns; + + if (typeof filter !== 'object' || Array.isArray(filter)) { + throw new MongoInvalidArgumentError('Query filter must be a plain object or ObjectId'); + } + + // special case passing in an ObjectId as a filter + this.filter = filter != null && filter._bsontype === 'ObjectId' ? { _id: filter } : filter; + + this.SERVER_COMMAND_RESPONSE_TYPE = this.explain ? ExplainedCursorResponse : CursorResponse; + } + + override get commandName() { + return 'find' as const; + } + + override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions { + return { + ...this.options, + ...this.bsonOptions, + documentsReturnedIn: 'firstBatch', + session: this.session, + timeoutContext + }; + } + + override handleOk( + response: InstanceType + ): CursorResponse { + return response; + } + + override buildCommandDocument(): Document { + return makeFindCommand(this.ns, this.filter, this.options); + } +} + +function makeFindCommand(ns: MongoDBNamespace, filter: Document, options: FindOptions): Document { + const findCommand: Document = { + find: ns.collection, + filter + }; + + if (options.sort) { + findCommand.sort = formatSort(options.sort); + } + + if (options.projection) { + let projection = options.projection; + if (projection && Array.isArray(projection)) { + projection = projection.length + ? projection.reduce((result, field) => { + result[field] = 1; + return result; + }, {}) + : { _id: 1 }; + } + + findCommand.projection = projection; + } + + if (options.hint) { + findCommand.hint = normalizeHintField(options.hint); + } + + if (typeof options.skip === 'number') { + findCommand.skip = options.skip; + } + + if (typeof options.limit === 'number') { + if (options.limit < 0) { + findCommand.limit = -options.limit; + findCommand.singleBatch = true; + } else { + findCommand.limit = options.limit; + } + } + + if (typeof options.batchSize === 'number') { + if (options.batchSize < 0) { + findCommand.limit = -options.batchSize; + } else { + if (options.batchSize === options.limit) { + // Spec dictates that if these are equal the batchSize should be one more than the + // limit to avoid leaving the cursor open. + findCommand.batchSize = options.batchSize + 1; + } else { + findCommand.batchSize = options.batchSize; + } + } + } + + if (typeof options.singleBatch === 'boolean') { + findCommand.singleBatch = options.singleBatch; + } + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (options.comment !== undefined) { + findCommand.comment = options.comment; + } + + if (options.max) { + findCommand.max = options.max; + } + + if (options.min) { + findCommand.min = options.min; + } + + if (typeof options.returnKey === 'boolean') { + findCommand.returnKey = options.returnKey; + } + + if (typeof options.showRecordId === 'boolean') { + findCommand.showRecordId = options.showRecordId; + } + + if (typeof options.tailable === 'boolean') { + findCommand.tailable = options.tailable; + } + + if (typeof options.oplogReplay === 'boolean') { + findCommand.oplogReplay = options.oplogReplay; + } + + if (typeof options.timeout === 'boolean') { + findCommand.noCursorTimeout = !options.timeout; + } else if (typeof options.noCursorTimeout === 'boolean') { + findCommand.noCursorTimeout = options.noCursorTimeout; + } + + if (typeof options.awaitData === 'boolean') { + findCommand.awaitData = options.awaitData; + } + + if (typeof options.allowPartialResults === 'boolean') { + findCommand.allowPartialResults = options.allowPartialResults; + } + if (typeof options.allowDiskUse === 'boolean') { + findCommand.allowDiskUse = options.allowDiskUse; + } + + if (options.let) { + findCommand.let = options.let; + } + + return findCommand; +} + +defineAspects(FindOperation, [ + Aspect.READ_OPERATION, + Aspect.RETRYABLE, + Aspect.EXPLAINABLE, + Aspect.CURSOR_CREATING, + Aspect.SUPPORTS_RAW_DATA +]); diff --git a/gateway/node_modules/mongodb/src/operations/find_and_modify.ts b/gateway/node_modules/mongodb/src/operations/find_and_modify.ts new file mode 100644 index 0000000000000000000000000000000000000000..a35e288dd47212ee38f70955d69cd306b7b4d453 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/find_and_modify.ts @@ -0,0 +1,319 @@ +import { type Connection } from '..'; +import type { Document } from '../bson'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import type { Collection } from '../collection'; +import { MongoCompatibilityError, MongoInvalidArgumentError } from '../error'; +import { ReadPreference } from '../read_preference'; +import type { ClientSession } from '../sessions'; +import { formatSort, type Sort, type SortForCmd } from '../sort'; +import { decorateWithCollation, hasAtomicOperators, maxWireVersion } from '../utils'; +import { type WriteConcern, type WriteConcernSettings } from '../write_concern'; +import { CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects } from './operation'; + +/** @public */ +export const ReturnDocument = Object.freeze({ + BEFORE: 'before', + AFTER: 'after' +} as const); + +/** @public */ +export type ReturnDocument = (typeof ReturnDocument)[keyof typeof ReturnDocument]; + +/** @public */ +export interface FindOneAndDeleteOptions extends CommandOperationOptions { + /** An optional hint for query optimization. See the {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/ + hint?: Document; + /** Limits the fields to return for all matching documents. */ + projection?: Document; + /** Determines which document the operation modifies if the query selects multiple documents. */ + sort?: Sort; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /** + * Return the ModifyResult instead of the modified document. Defaults to false + */ + includeResultMetadata?: boolean; +} + +/** @public */ +export interface FindOneAndReplaceOptions extends CommandOperationOptions { + /** Allow driver to bypass schema validation. */ + bypassDocumentValidation?: boolean; + /** An optional hint for query optimization. See the {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/ + hint?: Document; + /** Limits the fields to return for all matching documents. */ + projection?: Document; + /** When set to 'after', returns the updated document rather than the original. The default is 'before'. */ + returnDocument?: ReturnDocument; + /** Determines which document the operation modifies if the query selects multiple documents. */ + sort?: Sort; + /** Upsert the document if it does not exist. */ + upsert?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /** + * Return the ModifyResult instead of the modified document. Defaults to false + */ + includeResultMetadata?: boolean; +} + +/** @public */ +export interface FindOneAndUpdateOptions extends CommandOperationOptions { + /** Optional list of array filters referenced in filtered positional operators */ + arrayFilters?: Document[]; + /** Allow driver to bypass schema validation. */ + bypassDocumentValidation?: boolean; + /** An optional hint for query optimization. See the {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/ + hint?: Document; + /** Limits the fields to return for all matching documents. */ + projection?: Document; + /** When set to 'after', returns the updated document rather than the original. The default is 'before'. */ + returnDocument?: ReturnDocument; + /** Determines which document the operation modifies if the query selects multiple documents. */ + sort?: Sort; + /** Upsert the document if it does not exist. */ + upsert?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /** + * Return the ModifyResult instead of the modified document. Defaults to false + */ + includeResultMetadata?: boolean; +} + +/** @internal */ +interface FindAndModifyCmdBase { + remove: boolean; + new: boolean; + upsert: boolean; + update?: Document; + sort?: SortForCmd; + fields?: Document; + bypassDocumentValidation?: boolean; + arrayFilters?: Document[]; + maxTimeMS?: number; + let?: Document; + writeConcern?: WriteConcern | WriteConcernSettings; + /** + * Comment to apply to the operation. + * + * In server versions pre-4.4, 'comment' must be string. A server + * error will be thrown if any other type is provided. + * + * In server versions 4.4 and above, 'comment' can be any valid BSON type. + */ + comment?: unknown; +} + +function configureFindAndModifyCmdBaseUpdateOpts( + cmdBase: FindAndModifyCmdBase, + options: FindOneAndReplaceOptions | FindOneAndUpdateOptions +): FindAndModifyCmdBase { + cmdBase.new = options.returnDocument === ReturnDocument.AFTER; + cmdBase.upsert = options.upsert === true; + + if (options.bypassDocumentValidation === true) { + cmdBase.bypassDocumentValidation = options.bypassDocumentValidation; + } + return cmdBase; +} + +/** @internal */ +export class FindAndModifyOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: FindOneAndReplaceOptions | FindOneAndUpdateOptions | FindOneAndDeleteOptions; + collection: Collection; + query: Document; + doc?: Document; + + constructor( + collection: Collection, + query: Document, + options: FindOneAndReplaceOptions | FindOneAndUpdateOptions | FindOneAndDeleteOptions + ) { + super(collection, options); + this.options = options; + // force primary read preference + this.readPreference = ReadPreference.primary; + + this.collection = collection; + this.query = query; + } + + override get commandName() { + return 'findAndModify' as const; + } + + override buildCommandDocument( + connection: Connection, + _session?: ClientSession + ): Document & FindAndModifyCmdBase { + const options = this.options; + const command: Document & FindAndModifyCmdBase = { + findAndModify: this.collection.collectionName, + query: this.query, + remove: false, + new: false, + upsert: false + }; + + options.includeResultMetadata ??= false; + + const sort = formatSort(options.sort); + if (sort) { + command.sort = sort; + } + + if (options.projection) { + command.fields = options.projection; + } + + if (options.maxTimeMS) { + command.maxTimeMS = options.maxTimeMS; + } + + // Decorate the findAndModify command with the write Concern + if (options.writeConcern) { + command.writeConcern = options.writeConcern; + } + + if (options.let) { + command.let = options.let; + } + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (options.comment !== undefined) { + command.comment = options.comment; + } + + decorateWithCollation(command, options); + + if (options.hint) { + const unacknowledgedWrite = this.writeConcern?.w === 0; + if (unacknowledgedWrite && maxWireVersion(connection) < 9) { + throw new MongoCompatibilityError( + 'hint for the findAndModify command is only supported on MongoDB 4.4+' + ); + } + + command.hint = options.hint; + } + + return command; + } + + override handleOk(response: InstanceType): Document { + const result = super.handleOk(response); + return this.options.includeResultMetadata ? result : (result.value ?? null); + } +} + +/** @internal */ +export class FindOneAndDeleteOperation extends FindAndModifyOperation { + constructor(collection: Collection, filter: Document, options: FindOneAndDeleteOptions) { + // Basic validation + if (filter == null || typeof filter !== 'object') { + throw new MongoInvalidArgumentError('Argument "filter" must be an object'); + } + + super(collection, filter, options); + } + + override buildCommandDocument( + connection: Connection, + session?: ClientSession + ): Document & FindAndModifyCmdBase { + const document = super.buildCommandDocument(connection, session); + document.remove = true; + return document; + } +} + +/** @internal */ +export class FindOneAndReplaceOperation extends FindAndModifyOperation { + private replacement: Document; + constructor( + collection: Collection, + filter: Document, + replacement: Document, + options: FindOneAndReplaceOptions + ) { + if (filter == null || typeof filter !== 'object') { + throw new MongoInvalidArgumentError('Argument "filter" must be an object'); + } + + if (replacement == null || typeof replacement !== 'object') { + throw new MongoInvalidArgumentError('Argument "replacement" must be an object'); + } + + if (hasAtomicOperators(replacement)) { + throw new MongoInvalidArgumentError('Replacement document must not contain atomic operators'); + } + + super(collection, filter, options); + this.replacement = replacement; + } + + override buildCommandDocument( + connection: Connection, + session?: ClientSession + ): Document & FindAndModifyCmdBase { + const document = super.buildCommandDocument(connection, session); + document.update = this.replacement; + configureFindAndModifyCmdBaseUpdateOpts(document, this.options); + return document; + } +} + +/** @internal */ +export class FindOneAndUpdateOperation extends FindAndModifyOperation { + override options: FindOneAndUpdateOptions; + + private update: Document; + constructor( + collection: Collection, + filter: Document, + update: Document, + options: FindOneAndUpdateOptions + ) { + if (filter == null || typeof filter !== 'object') { + throw new MongoInvalidArgumentError('Argument "filter" must be an object'); + } + + if (update == null || typeof update !== 'object') { + throw new MongoInvalidArgumentError('Argument "update" must be an object'); + } + + if (!hasAtomicOperators(update, options)) { + throw new MongoInvalidArgumentError('Update document requires atomic operators'); + } + + super(collection, filter, options); + this.update = update; + this.options = options; + } + + override buildCommandDocument( + connection: Connection, + session?: ClientSession + ): Document & FindAndModifyCmdBase { + const document = super.buildCommandDocument(connection, session); + document.update = this.update; + configureFindAndModifyCmdBaseUpdateOpts(document, this.options); + + if (this.options.arrayFilters) { + document.arrayFilters = this.options.arrayFilters; + } + + return document; + } +} + +defineAspects(FindAndModifyOperation, [ + Aspect.WRITE_OPERATION, + Aspect.RETRYABLE, + Aspect.EXPLAINABLE, + Aspect.SUPPORTS_RAW_DATA +]); diff --git a/gateway/node_modules/mongodb/src/operations/get_more.ts b/gateway/node_modules/mongodb/src/operations/get_more.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e94020cd7ecf8c7d29da8199477b6f2a5fd4609 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/get_more.ts @@ -0,0 +1,108 @@ +import type { Document, Long } from '../bson'; +import { type Connection } from '../cmap/connection'; +import { CursorResponse } from '../cmap/wire_protocol/responses'; +import { MongoRuntimeError } from '../error'; +import type { Server, ServerCommandOptions } from '../sdam/server'; +import { type TimeoutContext } from '../timeout'; +import { maxWireVersion, type MongoDBNamespace } from '../utils'; +import { AbstractOperation, Aspect, defineAspects, type OperationOptions } from './operation'; + +/** @internal */ +export interface GetMoreOptions extends OperationOptions { + /** Set the batchSize for the getMoreCommand when iterating over the query results. */ + batchSize?: number; + /** + * Comment to apply to the operation. + * + * getMore only supports 'comment' in server versions 4.4 and above. + */ + comment?: unknown; + /** Number of milliseconds to wait before aborting the query. */ + maxTimeMS?: number; + /** TODO(NODE-4413): Address bug with maxAwaitTimeMS not being passed in from the cursor correctly */ + maxAwaitTimeMS?: number; +} + +/** + * GetMore command: https://www.mongodb.com/docs/manual/reference/command/getMore/ + * @internal + */ +export interface GetMoreCommand { + getMore: Long; + collection: string; + batchSize?: number; + maxTimeMS?: number; + /** Only supported on wire versions 10 or greater */ + comment?: unknown; +} + +/** @internal */ +export class GetMoreOperation extends AbstractOperation { + override SERVER_COMMAND_RESPONSE_TYPE = CursorResponse; + cursorId: Long; + override options: GetMoreOptions; + + constructor(ns: MongoDBNamespace, cursorId: Long, server: Server, options: GetMoreOptions) { + super(options); + + this.options = options; + this.ns = ns; + this.cursorId = cursorId; + this.server = server; + } + + override get commandName() { + return 'getMore' as const; + } + + override buildCommand(connection: Connection): Document { + if (this.cursorId == null || this.cursorId.isZero()) { + throw new MongoRuntimeError('Unable to iterate cursor with no id'); + } + + const collection = this.ns.collection; + if (collection == null) { + // Cursors should have adopted the namespace returned by MongoDB + // which should always defined a collection name (even a pseudo one, ex. db.aggregate()) + throw new MongoRuntimeError('A collection name must be determined before getMore'); + } + + const getMoreCmd: GetMoreCommand = { + getMore: this.cursorId, + collection + }; + + if (typeof this.options.batchSize === 'number') { + getMoreCmd.batchSize = Math.abs(this.options.batchSize); + } + + if (typeof this.options.maxAwaitTimeMS === 'number') { + getMoreCmd.maxTimeMS = this.options.maxAwaitTimeMS; + } + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (this.options.comment !== undefined && maxWireVersion(connection) >= 9) { + getMoreCmd.comment = this.options.comment; + } + + return getMoreCmd; + } + + override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions { + return { + returnFieldSelector: null, + documentsReturnedIn: 'nextBatch', + timeoutContext, + ...this.options + }; + } + + override handleOk( + response: InstanceType + ): CursorResponse { + return response; + } +} + +defineAspects(GetMoreOperation, [Aspect.READ_OPERATION, Aspect.MUST_SELECT_SAME_SERVER]); diff --git a/gateway/node_modules/mongodb/src/operations/indexes.ts b/gateway/node_modules/mongodb/src/operations/indexes.ts new file mode 100644 index 0000000000000000000000000000000000000000..f6354dcf1adaa5024b27bba271c1315f9740a3c6 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/indexes.ts @@ -0,0 +1,418 @@ +import type { Document } from '../bson'; +import { type Connection } from '../cmap/connection'; +import { CursorResponse, MongoDBResponse } from '../cmap/wire_protocol/responses'; +import type { Collection } from '../collection'; +import { type AbstractCursorOptions } from '../cursor/abstract_cursor'; +import { MongoCompatibilityError } from '../error'; +import { type OneOrMore } from '../mongo_types'; +import { isObject, maxWireVersion, type MongoDBNamespace } from '../utils'; +import { + type CollationOptions, + CommandOperation, + type CommandOperationOptions, + type OperationParent +} from './command'; +import { Aspect, defineAspects } from './operation'; + +const VALID_INDEX_OPTIONS = new Set([ + 'background', + 'unique', + 'name', + 'partialFilterExpression', + 'sparse', + 'hidden', + 'expireAfterSeconds', + 'storageEngine', + 'collation', + 'version', + + // text indexes + 'weights', + 'default_language', + 'language_override', + 'textIndexVersion', + + // 2d-sphere indexes + '2dsphereIndexVersion', + + // 2d indexes + 'bits', + 'min', + 'max', + + // geoHaystack Indexes + 'bucketSize', + + // wildcard indexes + 'wildcardProjection' +]); + +/** @public */ +export type IndexDirection = + | -1 + | 1 + | '2d' + | '2dsphere' + | 'text' + | 'geoHaystack' + | 'hashed' + | number; + +function isIndexDirection(x: unknown): x is IndexDirection { + return ( + typeof x === 'number' || x === '2d' || x === '2dsphere' || x === 'text' || x === 'geoHaystack' + ); +} +/** @public */ +export type IndexSpecification = OneOrMore< + | string + | [string, IndexDirection] + | { [key: string]: IndexDirection } + | Map +>; + +/** @public */ +export interface IndexInformationOptions extends ListIndexesOptions { + /** + * When `true`, an array of index descriptions is returned. + * When `false`, the driver returns an object that with keys corresponding to index names with values + * corresponding to the entries of the indexes' key. + * + * For example, the given the following indexes: + * ``` + * [ { name: 'a_1', key: { a: 1 } }, { name: 'b_1_c_1' , key: { b: 1, c: 1 } }] + * ``` + * + * When `full` is `true`, the above array is returned. When `full` is `false`, the following is returned: + * ``` + * { + * 'a_1': [['a', 1]], + * 'b_1_c_1': [['b', 1], ['c', 1]], + * } + * ``` + */ + full?: boolean; +} + +/** @public */ +export interface IndexDescription + extends Pick< + CreateIndexesOptions, + | 'background' + | 'unique' + | 'partialFilterExpression' + | 'sparse' + | 'hidden' + | 'expireAfterSeconds' + | 'storageEngine' + | 'version' + | 'weights' + | 'default_language' + | 'language_override' + | 'textIndexVersion' + | '2dsphereIndexVersion' + | 'bits' + | 'min' + | 'max' + | 'bucketSize' + | 'wildcardProjection' + > { + collation?: CollationOptions; + name?: string; + key: { [key: string]: IndexDirection } | Map; +} + +/** @public */ +export interface CreateIndexesOptions extends Omit { + /** Creates the index in the background, yielding whenever possible. */ + background?: boolean; + /** Creates an unique index. */ + unique?: boolean; + /** Override the autogenerated index name (useful if the resulting name is larger than 128 bytes) */ + name?: string; + /** Creates a partial index based on the given filter object (MongoDB 3.2 or higher) */ + partialFilterExpression?: Document; + /** Creates a sparse index. */ + sparse?: boolean; + /** Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher) */ + expireAfterSeconds?: number; + /** Allows users to configure the storage engine on a per-index basis when creating an index. (MongoDB 3.0 or higher) */ + storageEngine?: Document; + /** (MongoDB 4.4. or higher) Specifies how many data-bearing members of a replica set, including the primary, must complete the index builds successfully before the primary marks the indexes as ready. This option accepts the same values for the "w" field in a write concern plus "votingMembers", which indicates all voting data-bearing nodes. */ + commitQuorum?: number | string; + /** Specifies the index version number, either 0 or 1. */ + version?: number; + // text indexes + weights?: Document; + default_language?: string; + language_override?: string; + textIndexVersion?: number; + // 2d-sphere indexes + '2dsphereIndexVersion'?: number; + // 2d indexes + bits?: number; + /** For geospatial indexes set the lower bound for the co-ordinates. */ + min?: number; + /** For geospatial indexes set the high bound for the co-ordinates. */ + max?: number; + // geoHaystack Indexes + bucketSize?: number; + // wildcard indexes + wildcardProjection?: Document; + /** Specifies that the index should exist on the target collection but should not be used by the query planner when executing operations. (MongoDB 4.4 or higher) */ + hidden?: boolean; +} + +function isSingleIndexTuple(t: unknown): t is [string, IndexDirection] { + return Array.isArray(t) && t.length === 2 && isIndexDirection(t[1]); +} + +/** + * Converts an `IndexSpecification`, which can be specified in multiple formats, into a + * valid `key` for the createIndexes command. + */ +function constructIndexDescriptionMap(indexSpec: IndexSpecification): Map { + const key: Map = new Map(); + + const indexSpecs = + !Array.isArray(indexSpec) || isSingleIndexTuple(indexSpec) ? [indexSpec] : indexSpec; + + // Iterate through array and handle different types + for (const spec of indexSpecs) { + if (typeof spec === 'string') { + key.set(spec, 1); + } else if (Array.isArray(spec)) { + key.set(spec[0], spec[1] ?? 1); + } else if (spec instanceof Map) { + for (const [property, value] of spec) { + key.set(property, value); + } + } else if (isObject(spec)) { + for (const [property, value] of Object.entries(spec)) { + key.set(property, value); + } + } + } + + return key; +} + +/** + * Receives an index description and returns a modified index description which has had invalid options removed + * from the description and has mapped the `version` option to the `v` option. + */ +function resolveIndexDescription( + description: IndexDescription +): Omit { + const validProvidedOptions = Object.entries(description).filter(([optionName]) => + VALID_INDEX_OPTIONS.has(optionName) + ); + + return Object.fromEntries( + // we support the `version` option, but the `createIndexes` command expects it to be the `v` + validProvidedOptions.map(([name, value]) => (name === 'version' ? ['v', value] : [name, value])) + ); +} + +/** + * @public + * The index information returned by the listIndexes command. https://www.mongodb.com/docs/manual/reference/command/listIndexes/#mongodb-dbcommand-dbcmd.listIndexes + */ +export type IndexDescriptionInfo = Omit & { + key: { [key: string]: IndexDirection }; + v?: IndexDescription['version']; +} & Document; + +/** @public */ +export type IndexDescriptionCompact = Record; + +/** + * @internal + * + * Internally, the driver represents index description keys with `Map`s to preserve key ordering. + * We don't require users to specify maps, so we transform user provided descriptions into + * "resolved" by converting the `key` into a JS `Map`, if it isn't already a map. + * + * Additionally, we support the `version` option, but the `createIndexes` command uses the field `v` + * to specify the index version so we map the value of `version` to `v`, if provided. + */ +type ResolvedIndexDescription = Omit & { + key: Map; + v?: IndexDescription['version']; +}; + +/** @internal */ +export class CreateIndexesOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: CreateIndexesOptions; + collectionName: string; + indexes: ReadonlyArray; + + private constructor( + parent: OperationParent, + collectionName: string, + indexes: IndexDescription[], + options?: CreateIndexesOptions + ) { + super(parent, options); + + this.options = options ?? {}; + // collation is set on each index, it should not be defined at the root + this.options.collation = undefined; + this.collectionName = collectionName; + this.indexes = indexes.map((userIndex: IndexDescription): ResolvedIndexDescription => { + // Ensure the key is a Map to preserve index key ordering + const key = + userIndex.key instanceof Map ? userIndex.key : new Map(Object.entries(userIndex.key)); + const name = userIndex.name ?? Array.from(key).flat().join('_'); + const validIndexOptions = resolveIndexDescription(userIndex); + return { + ...validIndexOptions, + name, + key + }; + }); + this.ns = parent.s.namespace; + } + + static fromIndexDescriptionArray( + parent: OperationParent, + collectionName: string, + indexes: IndexDescription[], + options?: CreateIndexesOptions + ): CreateIndexesOperation { + return new CreateIndexesOperation(parent, collectionName, indexes, options); + } + + static fromIndexSpecification( + parent: OperationParent, + collectionName: string, + indexSpec: IndexSpecification, + options: CreateIndexesOptions = {} + ): CreateIndexesOperation { + const key = constructIndexDescriptionMap(indexSpec); + const description: IndexDescription = { ...options, key }; + return new CreateIndexesOperation(parent, collectionName, [description], options); + } + + override get commandName() { + return 'createIndexes'; + } + + override buildCommandDocument(connection: Connection): Document { + const options = this.options; + const indexes = this.indexes; + + const serverWireVersion = maxWireVersion(connection); + + const cmd: Document = { createIndexes: this.collectionName, indexes }; + + if (options.commitQuorum != null) { + if (serverWireVersion < 9) { + throw new MongoCompatibilityError( + 'Option `commitQuorum` for `createIndexes` not supported on servers < 4.4' + ); + } + cmd.commitQuorum = options.commitQuorum; + } + return cmd; + } + + override handleOk(_response: InstanceType): string[] { + const indexNames = this.indexes.map(index => index.name || ''); + return indexNames; + } +} + +/** @public */ +export type DropIndexesOptions = CommandOperationOptions; + +/** @internal */ +export class DropIndexOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: DropIndexesOptions; + collection: Collection; + indexName: string; + + constructor(collection: Collection, indexName: string, options?: DropIndexesOptions) { + super(collection, options); + + this.options = options ?? {}; + this.collection = collection; + this.indexName = indexName; + this.ns = collection.fullNamespace; + } + + override get commandName() { + return 'dropIndexes' as const; + } + + override buildCommandDocument(_connection: Connection): Document { + return { dropIndexes: this.collection.collectionName, index: this.indexName }; + } +} + +/** @public */ +export type ListIndexesOptions = AbstractCursorOptions & { + /** @internal */ + omitMaxTimeMS?: boolean; + /** @internal */ + rawData?: boolean; +}; + +/** @internal */ +export class ListIndexesOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = CursorResponse; + /** + * @remarks WriteConcern can still be present on the options because + * we inherit options from the client/db/collection. The + * key must be present on the options in order to delete it. + * This allows typescript to delete the key but will + * not allow a writeConcern to be assigned as a property on options. + */ + override options: ListIndexesOptions & { writeConcern?: never }; + collectionNamespace: MongoDBNamespace; + + constructor(collection: Collection, options?: ListIndexesOptions) { + super(collection, options); + + this.options = { ...options }; + delete this.options.writeConcern; + this.collectionNamespace = collection.s.namespace; + } + + override get commandName() { + return 'listIndexes' as const; + } + + override buildCommandDocument(connection: Connection): Document { + const serverWireVersion = maxWireVersion(connection); + + const cursor = this.options.batchSize ? { batchSize: this.options.batchSize } : {}; + + const command: Document = { listIndexes: this.collectionNamespace.collection, cursor }; + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (serverWireVersion >= 9 && this.options.comment !== undefined) { + command.comment = this.options.comment; + } + + return command; + } + + override handleOk( + response: InstanceType + ): CursorResponse { + return response; + } +} + +defineAspects(ListIndexesOperation, [ + Aspect.READ_OPERATION, + Aspect.RETRYABLE, + Aspect.CURSOR_CREATING, + Aspect.SUPPORTS_RAW_DATA +]); +defineAspects(CreateIndexesOperation, [Aspect.WRITE_OPERATION, Aspect.SUPPORTS_RAW_DATA]); +defineAspects(DropIndexOperation, [Aspect.WRITE_OPERATION, Aspect.SUPPORTS_RAW_DATA]); diff --git a/gateway/node_modules/mongodb/src/operations/insert.ts b/gateway/node_modules/mongodb/src/operations/insert.ts new file mode 100644 index 0000000000000000000000000000000000000000..45d470bf05c44954807395fa1860779be01245aa --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/insert.ts @@ -0,0 +1,108 @@ +import { type Connection } from '..'; +import type { Document } from '../bson'; +import type { BulkWriteOptions } from '../bulk/common'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import type { Collection } from '../collection'; +import { MongoServerError } from '../error'; +import type { InferIdType } from '../mongo_types'; +import type { ClientSession } from '../sessions'; +import { maybeAddIdToDocuments, type MongoDBNamespace } from '../utils'; +import { CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects } from './operation'; +/** @internal */ +export class InsertOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: BulkWriteOptions; + + documents: Document[]; + + constructor(ns: MongoDBNamespace, documents: Document[], options: BulkWriteOptions) { + super(undefined, options); + this.options = { ...options, checkKeys: options.checkKeys ?? false }; + this.ns = ns; + this.documents = documents; + } + + override get commandName() { + return 'insert' as const; + } + + override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document { + const options = this.options ?? {}; + const ordered = typeof options.ordered === 'boolean' ? options.ordered : true; + const command: Document = { + insert: this.ns.collection, + documents: this.documents, + ordered + }; + + if (typeof options.bypassDocumentValidation === 'boolean') { + command.bypassDocumentValidation = options.bypassDocumentValidation; + } + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (options.comment !== undefined) { + command.comment = options.comment; + } + + return command; + } +} + +/** @public */ +export interface InsertOneOptions extends CommandOperationOptions { + /** Allow driver to bypass schema validation. */ + bypassDocumentValidation?: boolean; + /** Force server to assign _id values instead of driver. */ + forceServerObjectId?: boolean; +} + +/** @public */ +export interface InsertOneResult { + /** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */ + acknowledged: boolean; + /** The identifier that was inserted. If the server generated the identifier, this value will be null as the driver does not have access to that data */ + insertedId: InferIdType; +} + +export class InsertOneOperation extends InsertOperation { + constructor(collection: Collection, doc: Document, options: InsertOneOptions) { + super(collection.s.namespace, [maybeAddIdToDocuments(collection, doc, options)], options); + } + + override handleOk(response: InstanceType): Document { + const res = super.handleOk(response); + if (res.code) throw new MongoServerError(res); + if (res.writeErrors) { + // This should be a WriteError but we can't change it now because of error hierarchy + throw new MongoServerError(res.writeErrors[0]); + } + + return { + acknowledged: this.writeConcern?.w !== 0, + insertedId: this.documents[0]._id + }; + } +} + +/** @public */ +export interface InsertManyResult { + /** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */ + acknowledged: boolean; + /** The number of inserted documents for this operations */ + insertedCount: number; + /** Map of the index of the inserted document to the id of the inserted document */ + insertedIds: { [key: number]: InferIdType }; +} + +defineAspects(InsertOperation, [ + Aspect.RETRYABLE, + Aspect.WRITE_OPERATION, + Aspect.SUPPORTS_RAW_DATA +]); +defineAspects(InsertOneOperation, [ + Aspect.RETRYABLE, + Aspect.WRITE_OPERATION, + Aspect.SUPPORTS_RAW_DATA +]); diff --git a/gateway/node_modules/mongodb/src/operations/kill_cursors.ts b/gateway/node_modules/mongodb/src/operations/kill_cursors.ts new file mode 100644 index 0000000000000000000000000000000000000000..87184e4da0de085104bcb9c3f495a753342c102b --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/kill_cursors.ts @@ -0,0 +1,64 @@ +import type { Long } from '../bson'; +import { type Connection } from '../cmap/connection'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import { type MongoError, MongoRuntimeError } from '../error'; +import type { Server, ServerCommandOptions } from '../sdam/server'; +import type { ClientSession } from '../sessions'; +import { type TimeoutContext } from '../timeout'; +import { type MongoDBNamespace } from '../utils'; +import { AbstractOperation, Aspect, defineAspects, type OperationOptions } from './operation'; + +/** + * https://www.mongodb.com/docs/manual/reference/command/killCursors/ + * @internal + */ +interface KillCursorsCommand { + killCursors: string; + cursors: Long[]; + comment?: unknown; +} + +export class KillCursorsOperation extends AbstractOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + cursorId: Long; + + constructor(cursorId: Long, ns: MongoDBNamespace, server: Server, options: OperationOptions) { + super(options); + this.ns = ns; + this.cursorId = cursorId; + this.server = server; + } + + override get commandName() { + return 'killCursors' as const; + } + + override buildCommand(_connection: Connection, _session?: ClientSession): KillCursorsCommand { + const killCursors = this.ns.collection; + if (killCursors == null) { + // Cursors should have adopted the namespace returned by MongoDB + // which should always defined a collection name (even a pseudo one, ex. db.aggregate()) + throw new MongoRuntimeError('A collection name must be determined before killCursors'); + } + + const killCursorsCommand: KillCursorsCommand = { + killCursors, + cursors: [this.cursorId] + }; + + return killCursorsCommand; + } + + override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions { + return { + session: this.session, + timeoutContext + }; + } + + override handleError(_error: MongoError): void { + // The driver should never emit errors from killCursors, this is spec-ed behavior + } +} + +defineAspects(KillCursorsOperation, [Aspect.MUST_SELECT_SAME_SERVER]); diff --git a/gateway/node_modules/mongodb/src/operations/list_collections.ts b/gateway/node_modules/mongodb/src/operations/list_collections.ts new file mode 100644 index 0000000000000000000000000000000000000000..df38c954cb95a09947ff1ce7be13c9260e4c0154 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/list_collections.ts @@ -0,0 +1,108 @@ +import { type Connection } from '..'; +import type { Binary, Document } from '../bson'; +import { CursorResponse, ExplainedCursorResponse } from '../cmap/wire_protocol/responses'; +import { type CursorTimeoutContext, type CursorTimeoutMode } from '../cursor/abstract_cursor'; +import type { Db } from '../db'; +import { type Abortable } from '../mongo_types'; +import { maxWireVersion } from '../utils'; +import { CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects } from './operation'; + +/** @public */ +export interface ListCollectionsOptions + extends Omit, + Abortable { + /** Since 4.0: If true, will only return the collection name in the response, and will omit additional info */ + nameOnly?: boolean; + /** Since 4.0: If true and nameOnly is true, allows a user without the required privilege (i.e. listCollections action on the database) to run the command when access control is enforced. */ + authorizedCollections?: boolean; + /** The batchSize for the returned command cursor or if pre 2.8 the systems batch collection */ + batchSize?: number; + /** @internal */ + timeoutMode?: CursorTimeoutMode; + + /** @internal */ + timeoutContext?: CursorTimeoutContext; +} + +/** @internal */ +export class ListCollectionsOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = CursorResponse; + /** + * @remarks WriteConcern can still be present on the options because + * we inherit options from the client/db/collection. The + * key must be present on the options in order to delete it. + * This allows typescript to delete the key but will + * not allow a writeConcern to be assigned as a property on options. + */ + override options: ListCollectionsOptions & { writeConcern?: never }; + db: Db; + filter: Document; + nameOnly: boolean; + authorizedCollections: boolean; + batchSize?: number; + + constructor(db: Db, filter: Document, options?: ListCollectionsOptions) { + super(db, options); + + this.options = { ...options }; + delete this.options.writeConcern; + this.db = db; + this.filter = filter; + this.nameOnly = !!this.options.nameOnly; + this.authorizedCollections = !!this.options.authorizedCollections; + + if (typeof this.options.batchSize === 'number') { + this.batchSize = this.options.batchSize; + } + + this.SERVER_COMMAND_RESPONSE_TYPE = this.explain ? ExplainedCursorResponse : CursorResponse; + } + + override get commandName() { + return 'listCollections' as const; + } + + override buildCommandDocument(connection: Connection): Document { + const command: Document = { + listCollections: 1, + filter: this.filter, + cursor: this.batchSize ? { batchSize: this.batchSize } : {}, + nameOnly: this.nameOnly, + authorizedCollections: this.authorizedCollections + }; + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (maxWireVersion(connection) >= 9 && this.options.comment !== undefined) { + command.comment = this.options.comment; + } + + return command; + } + + override handleOk( + response: InstanceType + ): CursorResponse { + return response; + } +} + +/** @public */ +export interface CollectionInfo extends Document { + name: string; + type?: string; + options?: Document; + info?: { + readOnly?: false; + uuid?: Binary; + }; + idIndex?: Document; +} + +defineAspects(ListCollectionsOperation, [ + Aspect.READ_OPERATION, + Aspect.RETRYABLE, + Aspect.CURSOR_CREATING, + Aspect.SUPPORTS_RAW_DATA +]); diff --git a/gateway/node_modules/mongodb/src/operations/list_databases.ts b/gateway/node_modules/mongodb/src/operations/list_databases.ts new file mode 100644 index 0000000000000000000000000000000000000000..b9f4e97dda74bc2db0ee04592358b7063a1ccb99 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/list_databases.ts @@ -0,0 +1,68 @@ +import { type Connection } from '..'; +import type { Document } from '../bson'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import type { Db } from '../db'; +import type { ClientSession } from '../sessions'; +import { maxWireVersion, MongoDBNamespace } from '../utils'; +import { CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects } from './operation'; + +/** @public */ +export interface ListDatabasesResult { + databases: ({ name: string; sizeOnDisk?: number; empty?: boolean } & Document)[]; + totalSize?: number; + totalSizeMb?: number; + ok: 1 | 0; +} + +/** @public */ +export interface ListDatabasesOptions extends Omit { + /** A query predicate that determines which databases are listed */ + filter?: Document; + /** A flag to indicate whether the command should return just the database names, or return both database names and size information */ + nameOnly?: boolean; + /** A flag that determines which databases are returned based on the user privileges when access control is enabled */ + authorizedDatabases?: boolean; +} + +/** @internal */ +export class ListDatabasesOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: ListDatabasesOptions; + + constructor(db: Db, options?: ListDatabasesOptions) { + super(db, options); + this.options = options ?? {}; + this.ns = new MongoDBNamespace('admin', '$cmd'); + } + + override get commandName() { + return 'listDatabases' as const; + } + + override buildCommandDocument(connection: Connection, _session?: ClientSession): Document { + const cmd: Document = { listDatabases: 1 }; + + if (typeof this.options.nameOnly === 'boolean') { + cmd.nameOnly = this.options.nameOnly; + } + + if (this.options.filter) { + cmd.filter = this.options.filter; + } + + if (typeof this.options.authorizedDatabases === 'boolean') { + cmd.authorizedDatabases = this.options.authorizedDatabases; + } + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (maxWireVersion(connection) >= 9 && this.options.comment !== undefined) { + cmd.comment = this.options.comment; + } + + return cmd; + } +} + +defineAspects(ListDatabasesOperation, [Aspect.READ_OPERATION, Aspect.RETRYABLE]); diff --git a/gateway/node_modules/mongodb/src/operations/operation.ts b/gateway/node_modules/mongodb/src/operations/operation.ts new file mode 100644 index 0000000000000000000000000000000000000000..488afc96a883361ac9d16be13d076ca85bdca66b --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/operation.ts @@ -0,0 +1,189 @@ +import { type Connection, type MongoError } from '..'; +import { type BSONSerializeOptions, type Document, resolveBSONOptions } from '../bson'; +import { type MongoDBResponse } from '../cmap/wire_protocol/responses'; +import { type Abortable } from '../mongo_types'; +import { ReadPreference, type ReadPreferenceLike } from '../read_preference'; +import type { Server, ServerCommandOptions } from '../sdam/server'; +import type { ClientSession } from '../sessions'; +import { type TimeoutContext } from '../timeout'; +import type { MongoDBNamespace } from '../utils'; + +export const Aspect = { + READ_OPERATION: Symbol('READ_OPERATION'), + WRITE_OPERATION: Symbol('WRITE_OPERATION'), + RETRYABLE: Symbol('RETRYABLE'), + EXPLAINABLE: Symbol('EXPLAINABLE'), + SKIP_COLLATION: Symbol('SKIP_COLLATION'), + CURSOR_CREATING: Symbol('CURSOR_CREATING'), + MUST_SELECT_SAME_SERVER: Symbol('MUST_SELECT_SAME_SERVER'), + COMMAND_BATCHING: Symbol('COMMAND_BATCHING'), + SUPPORTS_RAW_DATA: Symbol('SUPPORTS_RAW_DATA') +} as const; + +/** @public */ +export type Hint = string | Document; + +/** @public */ +export interface OperationOptions extends BSONSerializeOptions { + /** Specify ClientSession for this command */ + session?: ClientSession; + willRetryWrite?: boolean; + + /** The preferred read preference (ReadPreference.primary, ReadPreference.primary_preferred, ReadPreference.secondary, ReadPreference.secondary_preferred, ReadPreference.nearest). */ + readPreference?: ReadPreferenceLike; + + /** @internal Hints to `executeOperation` that this operation should not unpin on an ended transaction */ + bypassPinningCheck?: boolean; + + /** @internal Hint to `executeOperation` to omit maxTimeMS */ + omitMaxTimeMS?: boolean; + + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; +} + +/** + * This class acts as a parent class for any operation and is responsible for setting this.options, + * as well as setting and getting a session. + * Additionally, this class implements `hasAspect`, which determines whether an operation has + * a specific aspect. + * @internal + */ +export abstract class AbstractOperation { + ns!: MongoDBNamespace; + readPreference: ReadPreference; + server!: Server; + bypassPinningCheck: boolean; + + // BSON serialization options + bsonOptions?: BSONSerializeOptions; + + options: OperationOptions & Abortable; + + /** Specifies the time an operation will run until it throws a timeout error. */ + timeoutMS?: number; + + /** Used by commitTransaction to share the retry budget across two executeOperation calls. */ + maxAttempts?: number; + + /** Tracks how many attempts were made in the last executeOperation call. */ + attemptsMade: number; + + private _session: ClientSession | undefined; + + static aspects?: Set; + + constructor(options: OperationOptions & Abortable = {}) { + this.readPreference = this.hasAspect(Aspect.WRITE_OPERATION) + ? ReadPreference.primary + : (ReadPreference.fromOptions(options) ?? ReadPreference.primary); + + // Pull the BSON serialize options from the already-resolved options + this.bsonOptions = resolveBSONOptions(options); + + this._session = options.session != null ? options.session : undefined; + + this.options = options; + this.bypassPinningCheck = !!options.bypassPinningCheck; + + this.attemptsMade = 0; + } + + /** Must match the first key of the command object sent to the server. + Command name should be stateless (should not use 'this' keyword) */ + abstract get commandName(): string; + + hasAspect(aspect: symbol): boolean { + const ctor = this.constructor as { aspects?: Set }; + if (ctor.aspects == null) { + return false; + } + + return ctor.aspects.has(aspect); + } + + // Make sure the session is not writable from outside this class. + get session(): ClientSession | undefined { + return this._session; + } + + set session(session: ClientSession) { + this._session = session; + } + + clearSession() { + this._session = undefined; + } + + resetBatch(): boolean { + return true; + } + + get canRetryRead(): boolean { + return this.hasAspect(Aspect.RETRYABLE) && this.hasAspect(Aspect.READ_OPERATION); + } + + get canRetryWrite(): boolean { + return this.hasAspect(Aspect.RETRYABLE) && this.hasAspect(Aspect.WRITE_OPERATION); + } + abstract SERVER_COMMAND_RESPONSE_TYPE: typeof MongoDBResponse; + + /** + * Build a raw command document. + */ + abstract buildCommand(connection: Connection, session?: ClientSession): Document; + + /** + * Builds an instance of `ServerCommandOptions` to be used for operation execution. + */ + abstract buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions; + + /** + * Given an instance of a MongoDBResponse, map the response to the correct result type. For + * example, a `CountOperation` might map the response as follows: + * + * ```typescript + * override handleOk(response: InstanceType): TResult { + * return response.toObject(this.bsonOptions).n ?? 0; + * } + * + * // or, with type safety: + * override handleOk(response: InstanceType): TResult { + * return response.getNumber('n') ?? 0; + * } + * ``` + */ + handleOk(response: InstanceType): TResult { + return response.toObject(this.bsonOptions) as TResult; + } + + /** + * Optional. + * + * If the operation performs error handling, such as wrapping, renaming the error, or squashing errors + * this method can be overridden. + */ + handleError(error: MongoError): TResult | never { + throw error; + } +} + +export function defineAspects( + operation: { aspects?: Set }, + aspects: symbol | symbol[] | Set +): Set { + if (!Array.isArray(aspects) && !(aspects instanceof Set)) { + aspects = [aspects]; + } + + aspects = new Set(aspects); + Object.defineProperty(operation, 'aspects', { + value: aspects, + writable: false + }); + + return aspects; +} diff --git a/gateway/node_modules/mongodb/src/operations/profiling_level.ts b/gateway/node_modules/mongodb/src/operations/profiling_level.ts new file mode 100644 index 0000000000000000000000000000000000000000..8308db81812f1bb567492a9d97af558374223311 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/profiling_level.ts @@ -0,0 +1,46 @@ +import { BSONType, type Document } from '../bson'; +import { type Connection } from '../cmap/connection'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import type { Db } from '../db'; +import { MongoUnexpectedServerResponseError } from '../error'; +import { CommandOperation, type CommandOperationOptions } from './command'; + +/** @public */ +export type ProfilingLevelOptions = Omit; + +class ProfilingLevelResponse extends MongoDBResponse { + get was() { + return this.get('was', BSONType.int, true); + } +} + +/** @internal */ +export class ProfilingLevelOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = ProfilingLevelResponse; + override options: ProfilingLevelOptions; + + constructor(db: Db, options: ProfilingLevelOptions) { + super(db, options); + this.options = options; + } + + override get commandName() { + return 'profile' as const; + } + + override buildCommandDocument(_connection: Connection): Document { + return { profile: -1 }; + } + + override handleOk(response: InstanceType): string { + if (response.ok === 1) { + const was = response.was; + if (was === 0) return 'off'; + if (was === 1) return 'slow_only'; + if (was === 2) return 'all'; + throw new MongoUnexpectedServerResponseError(`Illegal profiling level value ${was}`); + } else { + throw new MongoUnexpectedServerResponseError('Error with profile command'); + } + } +} diff --git a/gateway/node_modules/mongodb/src/operations/remove_user.ts b/gateway/node_modules/mongodb/src/operations/remove_user.ts new file mode 100644 index 0000000000000000000000000000000000000000..20e92ad0d9dc5db3d5aad0e8db97f45c1a70e934 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/remove_user.ts @@ -0,0 +1,36 @@ +import { type Document } from '../bson'; +import { type Connection } from '../cmap/connection'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import type { Db } from '../db'; +import { CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects } from './operation'; + +/** @public */ +export type RemoveUserOptions = Omit; + +/** @internal */ +export class RemoveUserOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: RemoveUserOptions; + username: string; + + constructor(db: Db, username: string, options: RemoveUserOptions) { + super(db, options); + this.options = options; + this.username = username; + } + + override get commandName() { + return 'dropUser' as const; + } + + override buildCommandDocument(_connection: Connection): Document { + return { dropUser: this.username }; + } + + override handleOk(_response: InstanceType): boolean { + return true; + } +} + +defineAspects(RemoveUserOperation, [Aspect.WRITE_OPERATION]); diff --git a/gateway/node_modules/mongodb/src/operations/rename.ts b/gateway/node_modules/mongodb/src/operations/rename.ts new file mode 100644 index 0000000000000000000000000000000000000000..9def98527066c8640297ec54aaa2e0fd8a351092 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/rename.ts @@ -0,0 +1,60 @@ +import { type Connection } from '..'; +import type { Document } from '../bson'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import { Collection } from '../collection'; +import type { ClientSession } from '../sessions'; +import { MongoDBNamespace } from '../utils'; +import { CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects } from './operation'; + +/** @public */ +export interface RenameOptions extends Omit { + /** Drop the target name collection if it previously exists. */ + dropTarget?: boolean; + /** + * @deprecated + * + * This option has been dead code since at least Node driver version 4.x. It will + * be removed in a future major release. + */ + new_collection?: boolean; +} + +/** @internal */ +export class RenameOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + collection: Collection; + newName: string; + override options: RenameOptions; + + constructor(collection: Collection, newName: string, options: RenameOptions) { + super(collection, options); + this.collection = collection; + this.newName = newName; + this.options = options; + this.ns = new MongoDBNamespace('admin', '$cmd'); + } + + override get commandName(): string { + return 'renameCollection' as const; + } + + override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document { + const renameCollection = this.collection.namespace; + const to = this.collection.s.namespace.withCollection(this.newName).toString(); + const dropTarget = + typeof this.options.dropTarget === 'boolean' ? this.options.dropTarget : false; + + return { + renameCollection, + to, + dropTarget + }; + } + + override handleOk(_response: InstanceType): Document { + return new Collection(this.collection.db, this.newName, this.collection.s.options); + } +} + +defineAspects(RenameOperation, [Aspect.WRITE_OPERATION]); diff --git a/gateway/node_modules/mongodb/src/operations/run_command.ts b/gateway/node_modules/mongodb/src/operations/run_command.ts new file mode 100644 index 0000000000000000000000000000000000000000..9f3e505f7e8f71a02a13509a367fed5cdf94c974 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/run_command.ts @@ -0,0 +1,79 @@ +import { type Abortable } from '..'; +import type { BSONSerializeOptions, Document } from '../bson'; +import { type Connection } from '../cmap/connection'; +import { CursorResponse, MongoDBResponse } from '../cmap/wire_protocol/responses'; +import { AbstractOperation } from '../operations/operation'; +import type { ReadPreferenceLike } from '../read_preference'; +import type { ServerCommandOptions } from '../sdam/server'; +import type { ClientSession } from '../sessions'; +import { type TimeoutContext } from '../timeout'; +import { type MongoDBNamespace } from '../utils'; + +/** @public */ +export type RunCommandOptions = { + /** Specify ClientSession for this command */ + session?: ClientSession; + /** The read preference */ + readPreference?: ReadPreferenceLike; + /** + * @experimental + * Specifies the time an operation will run until it throws a timeout error + */ + timeoutMS?: number; + /** @internal */ + omitMaxTimeMS?: boolean; + + /** + * @internal Hints to `executeOperation` that this operation should not unpin on an ended transaction + * This is only used by the driver for transaction commands + */ + bypassPinningCheck?: boolean; +} & BSONSerializeOptions & + Abortable; + +/** @internal */ +export class RunCommandOperation extends AbstractOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + command: Document; + override options: RunCommandOptions; + + constructor(namespace: MongoDBNamespace, command: Document, options: RunCommandOptions) { + super(options); + this.command = command; + this.options = options; + this.ns = namespace.withCollection('$cmd'); + } + + override get commandName() { + return 'runCommand' as const; + } + + override buildCommand(_connection: Connection, _session?: ClientSession): Document { + return this.command; + } + + override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions { + return { + ...this.options, + session: this.session, + timeoutContext, + signal: this.options.signal, + readPreference: this.options.readPreference + }; + } +} + +/** + * @internal + * + * A specialized subclass of RunCommandOperation for cursor-creating commands. + */ +export class RunCursorCommandOperation extends RunCommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = CursorResponse; + + override handleOk( + response: InstanceType + ): CursorResponse { + return response; + } +} diff --git a/gateway/node_modules/mongodb/src/operations/search_indexes/create.ts b/gateway/node_modules/mongodb/src/operations/search_indexes/create.ts new file mode 100644 index 0000000000000000000000000000000000000000..6d91ec3f35e37f6d28a198af7c6f441ab600a86a --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/search_indexes/create.ts @@ -0,0 +1,56 @@ +import { type Document } from '../../bson'; +import { type Connection } from '../../cmap/connection'; +import { MongoDBResponse } from '../../cmap/wire_protocol/responses'; +import type { Collection } from '../../collection'; +import type { ServerCommandOptions } from '../../sdam/server'; +import type { ClientSession } from '../../sessions'; +import { type TimeoutContext } from '../../timeout'; +import { AbstractOperation } from '../operation'; + +/** + * @public + */ +export interface SearchIndexDescription extends Document { + /** The name of the index. */ + name?: string; + + /** The index definition. */ + definition: Document; + + /** The type of the index. Currently `search` or `vectorSearch` are supported. */ + type?: string; +} + +/** @internal */ +export class CreateSearchIndexesOperation extends AbstractOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + private readonly collection: Collection; + private readonly descriptions: ReadonlyArray; + + constructor(collection: Collection, descriptions: ReadonlyArray) { + super(); + this.collection = collection; + this.descriptions = descriptions; + this.ns = collection.fullNamespace; + } + + override get commandName() { + return 'createSearchIndexes' as const; + } + + override buildCommand(_connection: Connection, _session?: ClientSession): Document { + const namespace = this.collection.fullNamespace; + return { + createSearchIndexes: namespace.collection, + indexes: this.descriptions + }; + } + + override handleOk(response: InstanceType): string[] { + return super.handleOk(response).indexesCreated.map((val: { name: string }) => val.name); + } + + override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions { + return { session: this.session, timeoutContext }; + } +} diff --git a/gateway/node_modules/mongodb/src/operations/search_indexes/drop.ts b/gateway/node_modules/mongodb/src/operations/search_indexes/drop.ts new file mode 100644 index 0000000000000000000000000000000000000000..e9132a7a7440e269adc39ee022e149515e510509 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/search_indexes/drop.ts @@ -0,0 +1,58 @@ +import { type Connection, type MongoError } from '../..'; +import type { Document } from '../../bson'; +import { MongoDBResponse } from '../../cmap/wire_protocol/responses'; +import type { Collection } from '../../collection'; +import { MONGODB_ERROR_CODES, MongoServerError } from '../../error'; +import type { ServerCommandOptions } from '../../sdam/server'; +import type { ClientSession } from '../../sessions'; +import { type TimeoutContext } from '../../timeout'; +import { AbstractOperation } from '../operation'; + +/** @internal */ +export class DropSearchIndexOperation extends AbstractOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + + private readonly collection: Collection; + private readonly name: string; + + constructor(collection: Collection, name: string) { + super(); + this.collection = collection; + this.name = name; + this.ns = collection.fullNamespace; + } + + override get commandName() { + return 'dropSearchIndex' as const; + } + + override buildCommand(_connection: Connection, _session?: ClientSession): Document { + const namespace = this.collection.fullNamespace; + + const command: Document = { + dropSearchIndex: namespace.collection + }; + + if (typeof this.name === 'string') { + command.name = this.name; + } + + return command; + } + + override handleOk(_response: MongoDBResponse): void { + // do nothing + } + + override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions { + return { session: this.session, timeoutContext }; + } + + override handleError(error: MongoError): void { + const isNamespaceNotFoundError = + error instanceof MongoServerError && error.code === MONGODB_ERROR_CODES.NamespaceNotFound; + if (!isNamespaceNotFoundError) { + throw error; + } + } +} diff --git a/gateway/node_modules/mongodb/src/operations/search_indexes/update.ts b/gateway/node_modules/mongodb/src/operations/search_indexes/update.ts new file mode 100644 index 0000000000000000000000000000000000000000..af9d8810821db3776fc5d686399ec3d90d878afe --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/search_indexes/update.ts @@ -0,0 +1,45 @@ +import type { Document } from '../../bson'; +import { type Connection } from '../../cmap/connection'; +import { MongoDBResponse } from '../../cmap/wire_protocol/responses'; +import type { Collection } from '../../collection'; +import type { ServerCommandOptions } from '../../sdam/server'; +import type { ClientSession } from '../../sessions'; +import { type TimeoutContext } from '../../timeout'; +import { AbstractOperation } from '../operation'; + +/** @internal */ +export class UpdateSearchIndexOperation extends AbstractOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + private readonly collection: Collection; + private readonly name: string; + private readonly definition: Document; + + constructor(collection: Collection, name: string, definition: Document) { + super(); + this.collection = collection; + this.name = name; + this.definition = definition; + this.ns = collection.fullNamespace; + } + + override get commandName() { + return 'updateSearchIndex' as const; + } + + override buildCommand(_connection: Connection, _session?: ClientSession): Document { + const namespace = this.collection.fullNamespace; + return { + updateSearchIndex: namespace.collection, + name: this.name, + definition: this.definition + }; + } + + override handleOk(_response: MongoDBResponse): void { + // no response. + } + + override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions { + return { session: this.session, timeoutContext }; + } +} diff --git a/gateway/node_modules/mongodb/src/operations/set_profiling_level.ts b/gateway/node_modules/mongodb/src/operations/set_profiling_level.ts new file mode 100644 index 0000000000000000000000000000000000000000..bf97747cd94a3082565ace06e286eb675c85f048 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/set_profiling_level.ts @@ -0,0 +1,74 @@ +import { type Document } from '../bson'; +import { type Connection } from '../cmap/connection'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import type { Db } from '../db'; +import { MongoInvalidArgumentError } from '../error'; +import { enumToString } from '../utils'; +import { CommandOperation, type CommandOperationOptions } from './command'; + +const levelValues = new Set(['off', 'slow_only', 'all']); + +/** @public */ +export const ProfilingLevel = Object.freeze({ + off: 'off', + slowOnly: 'slow_only', + all: 'all' +} as const); + +/** @public */ +export type ProfilingLevel = (typeof ProfilingLevel)[keyof typeof ProfilingLevel]; + +/** @public */ +export type SetProfilingLevelOptions = Omit; + +/** @internal */ +export class SetProfilingLevelOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: SetProfilingLevelOptions; + level: ProfilingLevel; + profile: 0 | 1 | 2; + + constructor(db: Db, level: ProfilingLevel, options: SetProfilingLevelOptions) { + super(db, options); + this.options = options; + switch (level) { + case ProfilingLevel.off: + this.profile = 0; + break; + case ProfilingLevel.slowOnly: + this.profile = 1; + break; + case ProfilingLevel.all: + this.profile = 2; + break; + default: + this.profile = 0; + break; + } + + this.level = level; + } + + override get commandName() { + return 'profile' as const; + } + + override buildCommandDocument(_connection: Connection): Document { + const level = this.level; + + if (!levelValues.has(level)) { + // TODO(NODE-3483): Determine error to put here + throw new MongoInvalidArgumentError( + `Profiling level must be one of "${enumToString(ProfilingLevel)}"` + ); + } + + return { profile: this.profile }; + } + + override handleOk( + _response: InstanceType + ): ProfilingLevel { + return this.level; + } +} diff --git a/gateway/node_modules/mongodb/src/operations/stats.ts b/gateway/node_modules/mongodb/src/operations/stats.ts new file mode 100644 index 0000000000000000000000000000000000000000..e5f915015969ce0e4047e9858cf048f6e755ae28 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/stats.ts @@ -0,0 +1,37 @@ +import type { Document } from '../bson'; +import { type Connection } from '../cmap/connection'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import type { Db } from '../db'; +import { CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects } from './operation'; + +/** @public */ +export interface DbStatsOptions extends Omit { + /** Divide the returned sizes by scale value. */ + scale?: number; +} + +/** @internal */ +export class DbStatsOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: DbStatsOptions; + + constructor(db: Db, options: DbStatsOptions) { + super(db, options); + this.options = options; + } + + override get commandName() { + return 'dbStats' as const; + } + + override buildCommandDocument(_connection: Connection): Document { + const command: Document = { dbStats: true }; + if (this.options.scale != null) { + command.scale = this.options.scale; + } + return command; + } +} + +defineAspects(DbStatsOperation, [Aspect.READ_OPERATION]); diff --git a/gateway/node_modules/mongodb/src/operations/update.ts b/gateway/node_modules/mongodb/src/operations/update.ts new file mode 100644 index 0000000000000000000000000000000000000000..36d7a5e3c786515c25d9de54d490355899a39134 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/update.ts @@ -0,0 +1,318 @@ +import type { Document } from '../bson'; +import { type Connection } from '../cmap/connection'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import { MongoInvalidArgumentError, MongoServerError } from '../error'; +import type { InferIdType } from '../mongo_types'; +import type { ClientSession } from '../sessions'; +import { formatSort, type Sort, type SortForCmd } from '../sort'; +import { + hasAtomicOperators, + type MongoDBCollectionNamespace, + type MongoDBNamespace +} from '../utils'; +import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command'; +import { Aspect, defineAspects, type Hint } from './operation'; + +/** @public */ +export interface UpdateOptions extends CommandOperationOptions { + /** A set of filters specifying to which array elements an update should apply */ + arrayFilters?: Document[]; + /** If true, allows the write to opt-out of document level validation */ + bypassDocumentValidation?: boolean; + /** Specifies a collation */ + collation?: CollationOptions; + /** Specify that the update query should only consider plans using the hinted index */ + hint?: Hint; + /** When true, creates a new document if no document matches the query */ + upsert?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; +} + +/** + * @public + * `TSchema` is the schema of the collection + */ +export interface UpdateResult { + /** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */ + acknowledged: boolean; + /** The number of documents that matched the filter */ + matchedCount: number; + /** The number of documents that were modified */ + modifiedCount: number; + /** The number of documents that were upserted */ + upsertedCount: number; + /** The identifier of the inserted document if an upsert took place */ + upsertedId: InferIdType | null; +} + +/** @public */ +export interface UpdateStatement { + /** The query that matches documents to update. */ + q: Document; + /** The modifications to apply. */ + u: Document | Document[]; + /** If true, perform an insert if no documents match the query. */ + upsert?: boolean; + /** If true, updates all documents that meet the query criteria. */ + multi?: boolean; + /** Specifies the collation to use for the operation. */ + collation?: CollationOptions; + /** An array of filter documents that determines which array elements to modify for an update operation on an array field. */ + arrayFilters?: Document[]; + /** A document or string that specifies the index to use to support the query predicate. */ + hint?: Hint; + /** Specifies the sort order for the documents matched by the filter. */ + sort?: SortForCmd; +} + +/** + * @internal + * UpdateOperation is used in bulk write, while UpdateOneOperation and UpdateManyOperation are only used in the collections API + */ +export class UpdateOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: UpdateOptions & { ordered?: boolean }; + statements: UpdateStatement[]; + + constructor( + ns: MongoDBNamespace, + statements: UpdateStatement[], + options: UpdateOptions & { ordered?: boolean } + ) { + super(undefined, options); + this.options = options; + this.ns = ns; + + this.statements = statements; + } + + override get commandName() { + return 'update' as const; + } + + override get canRetryWrite(): boolean { + if (super.canRetryWrite === false) { + return false; + } + + return this.statements.every(op => op.multi == null || op.multi === false); + } + + override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document { + const options = this.options; + const command: Document = { + update: this.ns.collection, + updates: this.statements, + ordered: options.ordered ?? true + }; + + if (typeof options.bypassDocumentValidation === 'boolean') { + command.bypassDocumentValidation = options.bypassDocumentValidation; + } + + if (options.let) { + command.let = options.let; + } + + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (options.comment !== undefined) { + command.comment = options.comment; + } + + return command; + } +} + +/** @internal */ +export class UpdateOneOperation extends UpdateOperation { + constructor( + ns: MongoDBCollectionNamespace, + filter: Document, + update: Document, + options: UpdateOptions + ) { + super(ns, [makeUpdateStatement(filter, update, { ...options, multi: false })], options); + + if (!hasAtomicOperators(update, options)) { + throw new MongoInvalidArgumentError('Update document requires atomic operators'); + } + } + + override handleOk( + response: InstanceType + ): UpdateResult { + const res = super.handleOk(response); + + // @ts-expect-error Explain typing is broken + if (this.explain != null) return res; + + if (res.code) throw new MongoServerError(res); + if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]); + + return { + acknowledged: this.writeConcern?.w !== 0, + modifiedCount: res.nModified ?? res.n, + upsertedId: + Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null, + upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0, + matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n + }; + } +} + +/** @internal */ +export class UpdateManyOperation extends UpdateOperation { + constructor( + ns: MongoDBCollectionNamespace, + filter: Document, + update: Document, + options: UpdateOptions + ) { + super(ns, [makeUpdateStatement(filter, update, { ...options, multi: true })], options); + + if (!hasAtomicOperators(update, options)) { + throw new MongoInvalidArgumentError('Update document requires atomic operators'); + } + } + + override handleOk( + response: InstanceType + ): UpdateResult { + const res = super.handleOk(response); + + // @ts-expect-error Explain typing is broken + if (this.explain != null) return res; + if (res.code) throw new MongoServerError(res); + if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]); + + return { + acknowledged: this.writeConcern?.w !== 0, + modifiedCount: res.nModified ?? res.n, + upsertedId: + Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null, + upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0, + matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n + }; + } +} + +/** @public */ +export interface ReplaceOptions extends CommandOperationOptions { + /** If true, allows the write to opt-out of document level validation */ + bypassDocumentValidation?: boolean; + /** Specifies a collation */ + collation?: CollationOptions; + /** Specify that the update query should only consider plans using the hinted index */ + hint?: string | Document; + /** When true, creates a new document if no document matches the query */ + upsert?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; + /** Specifies the sort order for the documents matched by the filter. */ + sort?: Sort; +} + +/** @internal */ +export class ReplaceOneOperation extends UpdateOperation { + constructor( + ns: MongoDBCollectionNamespace, + filter: Document, + replacement: Document, + options: ReplaceOptions + ) { + super(ns, [makeUpdateStatement(filter, replacement, { ...options, multi: false })], options); + + if (hasAtomicOperators(replacement)) { + throw new MongoInvalidArgumentError('Replacement document must not contain atomic operators'); + } + } + + override handleOk( + response: InstanceType + ): UpdateResult { + const res = super.handleOk(response); + + // @ts-expect-error Explain typing is broken + if (this.explain != null) return res; + if (res.code) throw new MongoServerError(res); + if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]); + + return { + acknowledged: this.writeConcern?.w !== 0, + modifiedCount: res.nModified ?? res.n, + upsertedId: + Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null, + upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0, + matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n + }; + } +} + +export function makeUpdateStatement( + filter: Document, + update: Document | Document[], + options: UpdateOptions & { multi?: boolean } & { sort?: Sort } +): UpdateStatement { + if (filter == null || typeof filter !== 'object') { + throw new MongoInvalidArgumentError('Selector must be a valid JavaScript object'); + } + + if (update == null || typeof update !== 'object') { + throw new MongoInvalidArgumentError('Document must be a valid JavaScript object'); + } + + const op: UpdateStatement = { q: filter, u: update }; + if (typeof options.upsert === 'boolean') { + op.upsert = options.upsert; + } + + if (options.multi) { + op.multi = options.multi; + } + + if (options.hint) { + op.hint = options.hint; + } + + if (options.arrayFilters) { + op.arrayFilters = options.arrayFilters; + } + + if (options.collation) { + op.collation = options.collation; + } + + if (!options.multi && options.sort != null) { + op.sort = formatSort(options.sort); + } + + return op; +} + +defineAspects(UpdateOperation, [ + Aspect.RETRYABLE, + Aspect.WRITE_OPERATION, + Aspect.SKIP_COLLATION, + Aspect.SUPPORTS_RAW_DATA +]); +defineAspects(UpdateOneOperation, [ + Aspect.RETRYABLE, + Aspect.WRITE_OPERATION, + Aspect.EXPLAINABLE, + Aspect.SKIP_COLLATION, + Aspect.SUPPORTS_RAW_DATA +]); +defineAspects(UpdateManyOperation, [ + Aspect.WRITE_OPERATION, + Aspect.EXPLAINABLE, + Aspect.SKIP_COLLATION, + Aspect.SUPPORTS_RAW_DATA +]); +defineAspects(ReplaceOneOperation, [ + Aspect.RETRYABLE, + Aspect.WRITE_OPERATION, + Aspect.SKIP_COLLATION, + Aspect.SUPPORTS_RAW_DATA +]); diff --git a/gateway/node_modules/mongodb/src/operations/validate_collection.ts b/gateway/node_modules/mongodb/src/operations/validate_collection.ts new file mode 100644 index 0000000000000000000000000000000000000000..fb5fec8dc4ea34469bbeb19683ce09369c3c46d6 --- /dev/null +++ b/gateway/node_modules/mongodb/src/operations/validate_collection.ts @@ -0,0 +1,50 @@ +import { type Connection } from '..'; +import type { Admin } from '../admin'; +import { type Document } from '../bson'; +import { MongoDBResponse } from '../cmap/wire_protocol/responses'; +import { MongoUnexpectedServerResponseError } from '../error'; +import type { ClientSession } from '../sessions'; +import { CommandOperation, type CommandOperationOptions } from './command'; + +/** @public */ +export interface ValidateCollectionOptions extends Omit { + /** Validates a collection in the background, without interrupting read or write traffic (only in MongoDB 4.4+) */ + background?: boolean; +} + +/** @internal */ +export class ValidateCollectionOperation extends CommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; + override options: ValidateCollectionOptions; + collectionName: string; + + constructor(admin: Admin, collectionName: string, options: ValidateCollectionOptions) { + super(admin.s.db, options); + this.options = options; + this.collectionName = collectionName; + } + + override get commandName() { + return 'validate' as const; + } + + override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document { + // Decorate command with extra options + return { + validate: this.collectionName, + ...Object.fromEntries(Object.entries(this.options).filter(entry => entry[0] !== 'session')) + }; + } + + override handleOk(response: InstanceType): Document { + const result = super.handleOk(response); + if (result.result != null && typeof result.result !== 'string') + throw new MongoUnexpectedServerResponseError('Error with validation data'); + if (result.result != null && result.result.match(/exception|corrupt/) != null) + throw new MongoUnexpectedServerResponseError(`Invalid collection ${this.collectionName}`); + if (result.valid != null && !result.valid) + throw new MongoUnexpectedServerResponseError(`Invalid collection ${this.collectionName}`); + + return response; + } +} diff --git a/gateway/node_modules/mongodb/src/read_concern.ts b/gateway/node_modules/mongodb/src/read_concern.ts new file mode 100644 index 0000000000000000000000000000000000000000..1118c377f5f3992a4d8eb8ecad1d810e717c2800 --- /dev/null +++ b/gateway/node_modules/mongodb/src/read_concern.ts @@ -0,0 +1,88 @@ +import type { Document } from './bson'; + +/** @public */ +export const ReadConcernLevel = Object.freeze({ + local: 'local', + majority: 'majority', + linearizable: 'linearizable', + available: 'available', + snapshot: 'snapshot' +} as const); + +/** @public */ +export type ReadConcernLevel = (typeof ReadConcernLevel)[keyof typeof ReadConcernLevel]; + +/** @public */ +export type ReadConcernLike = ReadConcern | { level: ReadConcernLevel } | ReadConcernLevel; + +/** + * The MongoDB ReadConcern, which allows for control of the consistency and isolation properties + * of the data read from replica sets and replica set shards. + * @public + * + * @see https://www.mongodb.com/docs/manual/reference/read-concern/index.html + */ +export class ReadConcern { + level: ReadConcernLevel | string; + + /** Constructs a ReadConcern from the read concern level.*/ + constructor(level: ReadConcernLevel) { + /** + * A spec test exists that allows level to be any string. + * "invalid readConcern with out stage" + * @see ./test/spec/crud/v2/aggregate-out-readConcern.json + * @see https://github.com/mongodb/specifications/blob/master/source/read-write-concern/read-write-concern.md#unknown-levels-and-additional-options-for-string-based-readconcerns + */ + this.level = ReadConcernLevel[level] ?? level; + } + + /** + * Construct a ReadConcern given an options object. + * + * @param options - The options object from which to extract the write concern. + */ + static fromOptions(options?: { + readConcern?: ReadConcernLike; + level?: ReadConcernLevel; + }): ReadConcern | undefined { + if (options == null) { + return; + } + + if (options.readConcern) { + const { readConcern } = options; + if (readConcern instanceof ReadConcern) { + return readConcern; + } else if (typeof readConcern === 'string') { + return new ReadConcern(readConcern); + } else if ('level' in readConcern && readConcern.level) { + return new ReadConcern(readConcern.level); + } + } + + if (options.level) { + return new ReadConcern(options.level); + } + return; + } + + static get MAJORITY(): 'majority' { + return ReadConcernLevel.majority; + } + + static get AVAILABLE(): 'available' { + return ReadConcernLevel.available; + } + + static get LINEARIZABLE(): 'linearizable' { + return ReadConcernLevel.linearizable; + } + + static get SNAPSHOT(): 'snapshot' { + return ReadConcernLevel.snapshot; + } + + toJSON(): Document { + return { level: this.level }; + } +} diff --git a/gateway/node_modules/mongodb/src/read_preference.ts b/gateway/node_modules/mongodb/src/read_preference.ts new file mode 100644 index 0000000000000000000000000000000000000000..0ca9c6758d0716b07e0feb083f76c0a151fe295e --- /dev/null +++ b/gateway/node_modules/mongodb/src/read_preference.ts @@ -0,0 +1,256 @@ +import type { Document } from './bson'; +import { MongoInvalidArgumentError } from './error'; +import type { TagSet } from './sdam/server_description'; +import type { ClientSession } from './sessions'; + +/** @public */ +export type ReadPreferenceLike = ReadPreference | ReadPreferenceMode; + +/** @public */ +export const ReadPreferenceMode = Object.freeze({ + primary: 'primary', + primaryPreferred: 'primaryPreferred', + secondary: 'secondary', + secondaryPreferred: 'secondaryPreferred', + nearest: 'nearest' +} as const); + +/** @public */ +export type ReadPreferenceMode = (typeof ReadPreferenceMode)[keyof typeof ReadPreferenceMode]; + +/** @public */ +export interface HedgeOptions { + /** Explicitly enable or disable hedged reads. */ + enabled?: boolean; +} + +/** @public */ +export interface ReadPreferenceOptions { + /** Max secondary read staleness in seconds, Minimum value is 90 seconds.*/ + maxStalenessSeconds?: number; + /** Server mode in which the same query is dispatched in parallel to multiple replica set members. */ + hedge?: HedgeOptions; +} + +/** @public */ +export interface ReadPreferenceLikeOptions extends ReadPreferenceOptions { + readPreference?: + | ReadPreferenceLike + | { + mode?: ReadPreferenceMode; + preference?: ReadPreferenceMode; + tags?: TagSet[]; + maxStalenessSeconds?: number; + }; +} + +/** @public */ +export interface ReadPreferenceFromOptions extends ReadPreferenceLikeOptions { + session?: ClientSession; + readPreferenceTags?: TagSet[]; + hedge?: HedgeOptions; +} + +/** + * The **ReadPreference** class is a class that represents a MongoDB ReadPreference and is + * used to construct connections. + * @public + * + * @see https://www.mongodb.com/docs/manual/core/read-preference/ + */ +export class ReadPreference { + mode: ReadPreferenceMode; + tags?: TagSet[]; + hedge?: HedgeOptions; + maxStalenessSeconds?: number; + + public static PRIMARY = ReadPreferenceMode.primary; + public static PRIMARY_PREFERRED = ReadPreferenceMode.primaryPreferred; + public static SECONDARY = ReadPreferenceMode.secondary; + public static SECONDARY_PREFERRED = ReadPreferenceMode.secondaryPreferred; + public static NEAREST = ReadPreferenceMode.nearest; + + public static primary = new ReadPreference(ReadPreferenceMode.primary); + public static primaryPreferred = new ReadPreference(ReadPreferenceMode.primaryPreferred); + public static secondary = new ReadPreference(ReadPreferenceMode.secondary); + public static secondaryPreferred = new ReadPreference(ReadPreferenceMode.secondaryPreferred); + public static nearest = new ReadPreference(ReadPreferenceMode.nearest); + + /** + * @param mode - A string describing the read preference mode (primary|primaryPreferred|secondary|secondaryPreferred|nearest) + * @param tags - A tag set used to target reads to members with the specified tag(s). tagSet is not available if using read preference mode primary. + * @param options - Additional read preference options + */ + constructor(mode: ReadPreferenceMode, tags?: TagSet[], options?: ReadPreferenceOptions) { + if (!ReadPreference.isValid(mode)) { + throw new MongoInvalidArgumentError(`Invalid read preference mode ${JSON.stringify(mode)}`); + } + if (options == null && typeof tags === 'object' && !Array.isArray(tags)) { + options = tags; + tags = undefined; + } else if (tags && !Array.isArray(tags)) { + throw new MongoInvalidArgumentError('ReadPreference tags must be an array'); + } + + this.mode = mode; + this.tags = tags; + this.hedge = options?.hedge; + this.maxStalenessSeconds = undefined; + + options = options ?? {}; + if (options.maxStalenessSeconds != null) { + if (options.maxStalenessSeconds <= 0) { + throw new MongoInvalidArgumentError('maxStalenessSeconds must be a positive integer'); + } + + this.maxStalenessSeconds = options.maxStalenessSeconds; + } + + if (this.mode === ReadPreference.PRIMARY) { + if (this.tags && Array.isArray(this.tags) && this.tags.length > 0) { + throw new MongoInvalidArgumentError('Primary read preference cannot be combined with tags'); + } + + if (this.maxStalenessSeconds) { + throw new MongoInvalidArgumentError( + 'Primary read preference cannot be combined with maxStalenessSeconds' + ); + } + + if (this.hedge) { + throw new MongoInvalidArgumentError( + 'Primary read preference cannot be combined with hedge' + ); + } + } + } + + // Support the deprecated `preference` property introduced in the porcelain layer + get preference(): ReadPreferenceMode { + return this.mode; + } + + static fromString(mode: string): ReadPreference { + return new ReadPreference(mode as ReadPreferenceMode); + } + + /** + * Construct a ReadPreference given an options object. + * + * @param options - The options object from which to extract the read preference. + */ + static fromOptions(options?: ReadPreferenceFromOptions): ReadPreference | undefined { + if (!options) return; + const readPreference = + options.readPreference ?? options.session?.transaction.options.readPreference; + const readPreferenceTags = options.readPreferenceTags; + + if (readPreference == null) { + return; + } + + if (typeof readPreference === 'string') { + return new ReadPreference(readPreference, readPreferenceTags, { + maxStalenessSeconds: options.maxStalenessSeconds, + hedge: options.hedge + }); + } else if (!(readPreference instanceof ReadPreference) && typeof readPreference === 'object') { + const mode = readPreference.mode || readPreference.preference; + if (mode && typeof mode === 'string') { + return new ReadPreference(mode, readPreference.tags ?? readPreferenceTags, { + maxStalenessSeconds: readPreference.maxStalenessSeconds, + hedge: options.hedge + }); + } + } + + if (readPreferenceTags) { + readPreference.tags = readPreferenceTags; + } + + return readPreference as ReadPreference; + } + + /** + * Replaces options.readPreference with a ReadPreference instance + */ + static translate(options: ReadPreferenceLikeOptions): ReadPreferenceLikeOptions { + if (options.readPreference == null) return options; + const r = options.readPreference; + + if (typeof r === 'string') { + options.readPreference = new ReadPreference(r); + } else if (r && !(r instanceof ReadPreference) && typeof r === 'object') { + const mode = r.mode || r.preference; + if (mode && typeof mode === 'string') { + options.readPreference = new ReadPreference(mode, r.tags, { + maxStalenessSeconds: r.maxStalenessSeconds + }); + } + } else if (!(r instanceof ReadPreference)) { + throw new MongoInvalidArgumentError(`Invalid read preference: ${r}`); + } + + return options; + } + + /** + * Validate if a mode is legal + * + * @param mode - The string representing the read preference mode. + */ + static isValid(mode: string): boolean { + const VALID_MODES = new Set([ + ReadPreference.PRIMARY, + ReadPreference.PRIMARY_PREFERRED, + ReadPreference.SECONDARY, + ReadPreference.SECONDARY_PREFERRED, + ReadPreference.NEAREST, + null + ]); + + return VALID_MODES.has(mode as ReadPreferenceMode); + } + + /** + * Validate if a mode is legal + * + * @param mode - The string representing the read preference mode. + */ + isValid(mode?: string): boolean { + return ReadPreference.isValid(typeof mode === 'string' ? mode : this.mode); + } + + /** + * Indicates that this readPreference needs the "SecondaryOk" bit when sent over the wire + * @see https://www.mongodb.com/docs/manual/reference/mongodb-wire-protocol/#op-query + */ + secondaryOk(): boolean { + const NEEDS_SECONDARYOK = new Set([ + ReadPreference.PRIMARY_PREFERRED, + ReadPreference.SECONDARY, + ReadPreference.SECONDARY_PREFERRED, + ReadPreference.NEAREST + ]); + + return NEEDS_SECONDARYOK.has(this.mode); + } + + /** + * Check if the two ReadPreferences are equivalent + * + * @param readPreference - The read preference with which to check equality + */ + equals(readPreference: ReadPreference): boolean { + return readPreference.mode === this.mode; + } + + /** Return JSON representation */ + toJSON(): Document { + const readPreference = { mode: this.mode } as Document; + if (Array.isArray(this.tags)) readPreference.tags = this.tags; + if (this.maxStalenessSeconds) readPreference.maxStalenessSeconds = this.maxStalenessSeconds; + if (this.hedge) readPreference.hedge = this.hedge; + return readPreference; + } +} diff --git a/gateway/node_modules/mongodb/src/runtime_adapters.ts b/gateway/node_modules/mongodb/src/runtime_adapters.ts new file mode 100644 index 0000000000000000000000000000000000000000..3969e0fc049e8b1ae5c01e2ee168a389ad601677 --- /dev/null +++ b/gateway/node_modules/mongodb/src/runtime_adapters.ts @@ -0,0 +1,64 @@ +/* eslint-disable no-restricted-imports*/ + +// We squash the restricted import errors here because we are using type-only imports, which +// do not impact the driver's actual runtime dependencies. +// We also allow restricted imports in this file, because we expect this file to be the only place actually importing restricted Node APIs. + +import type * as os from 'os'; + +import { type MongoClientOptions } from './mongo_client'; + +/** + * @internal + * + * This propery can be set on the global object to allow the driver to require otherwise blocked modules. + * This is used by our test suite to allow tests to access the `os` module without allowing user code to do so. + */ +export const ALLOWED_DRIVER_REQUIRE_PROPERTY_NAME = 'allowedDriverRequire'; + +/** + * @public + * @experimental + * + * Represents the set of dependencies that the driver uses from the [Node.js OS module](https://nodejs.org/api/os.html). + */ +export type OsAdapter = Pick; + +/** + * @public + * @experimental + * + * This type represents the set of dependencies that the driver needs from the Javascript runtime in order to function. + */ +export interface RuntimeAdapters { + os?: OsAdapter; +} + +/** + * @internal + * + * Represents a complete, parsed set of runtime adapters. After options parsing, all adapters + * are always present (either using the user's provided adapter, or defaulting to the Node.js module). + */ +export interface Runtime { + os: OsAdapter; +} + +/** + * @internal + * + * Given a MongoClientOptions, this function resolves the set of runtime options, providing Nodejs implementations if + * not provided by in `options`, and returns a `Runtime`. + */ +export function resolveRuntimeAdapters(options: MongoClientOptions): Runtime { + (globalThis as any)[ALLOWED_DRIVER_REQUIRE_PROPERTY_NAME] = true; + try { + const runtime = { + // eslint-disable-next-line @typescript-eslint/no-require-imports + os: options.runtimeAdapters?.os ?? require('os') + }; + return runtime; + } finally { + (globalThis as any)[ALLOWED_DRIVER_REQUIRE_PROPERTY_NAME] = false; + } +} diff --git a/gateway/node_modules/mongodb/src/sdam/common.ts b/gateway/node_modules/mongodb/src/sdam/common.ts new file mode 100644 index 0000000000000000000000000000000000000000..974114856762103e7656832ed8ce4bc04738fe6d --- /dev/null +++ b/gateway/node_modules/mongodb/src/sdam/common.ts @@ -0,0 +1,74 @@ +import type { Binary, Long, Timestamp } from '../bson'; +import type { ClientSession } from '../sessions'; +import type { Topology } from './topology'; + +// shared state names +export const STATE_CLOSING = 'closing'; +export const STATE_CLOSED = 'closed'; +export const STATE_CONNECTING = 'connecting'; +export const STATE_CONNECTED = 'connected'; + +/** + * An enumeration of topology types we know about + * @public + */ +export const TopologyType = Object.freeze({ + Single: 'Single', + ReplicaSetNoPrimary: 'ReplicaSetNoPrimary', + ReplicaSetWithPrimary: 'ReplicaSetWithPrimary', + Sharded: 'Sharded', + Unknown: 'Unknown', + LoadBalanced: 'LoadBalanced' +} as const); + +/** @public */ +export type TopologyType = (typeof TopologyType)[keyof typeof TopologyType]; + +/** + * An enumeration of server types we know about + * @public + */ +export const ServerType = Object.freeze({ + Standalone: 'Standalone', + Mongos: 'Mongos', + PossiblePrimary: 'PossiblePrimary', + RSPrimary: 'RSPrimary', + RSSecondary: 'RSSecondary', + RSArbiter: 'RSArbiter', + RSOther: 'RSOther', + RSGhost: 'RSGhost', + Unknown: 'Unknown', + LoadBalancer: 'LoadBalancer' +} as const); + +/** @public */ +export type ServerType = (typeof ServerType)[keyof typeof ServerType]; + +/** + * @public + * Gossiped in component for the cluster time tracking the state of user databases + * across the cluster. It may optionally include a signature identifying the process that + * generated such a value. + */ +export interface ClusterTime { + clusterTime: Timestamp; + /** Used to validate the identity of a request or response's ClusterTime. */ + signature?: { + hash: Binary; + keyId: Long; + }; +} + +/** Shared function to determine clusterTime for a given topology or session */ +export function _advanceClusterTime( + entity: Topology | ClientSession, + $clusterTime: ClusterTime +): void { + if (entity.clusterTime == null) { + entity.clusterTime = $clusterTime; + } else { + if ($clusterTime.clusterTime.greaterThan(entity.clusterTime.clusterTime)) { + entity.clusterTime = $clusterTime; + } + } +} diff --git a/gateway/node_modules/mongodb/src/sdam/events.ts b/gateway/node_modules/mongodb/src/sdam/events.ts new file mode 100644 index 0000000000000000000000000000000000000000..64cb6ffb376cdd45960913c1772a6dde12af903b --- /dev/null +++ b/gateway/node_modules/mongodb/src/sdam/events.ts @@ -0,0 +1,219 @@ +import type { Document } from '../bson'; +import { + SERVER_CLOSED, + SERVER_DESCRIPTION_CHANGED, + SERVER_HEARTBEAT_FAILED, + SERVER_HEARTBEAT_STARTED, + SERVER_HEARTBEAT_SUCCEEDED, + SERVER_OPENING, + TOPOLOGY_CLOSED, + TOPOLOGY_DESCRIPTION_CHANGED, + TOPOLOGY_OPENING +} from '../constants'; +import type { ServerDescription } from './server_description'; +import type { TopologyDescription } from './topology_description'; + +/** + * Emitted when server description changes, but does NOT include changes to the RTT. + * @public + * @category Event + */ +export class ServerDescriptionChangedEvent { + /** A unique identifier for the topology */ + topologyId: number; + /** The address (host/port pair) of the server */ + address: string; + /** The previous server description */ + previousDescription: ServerDescription; + /** The new server description */ + newDescription: ServerDescription; + name = SERVER_DESCRIPTION_CHANGED; + + /** @internal */ + constructor( + topologyId: number, + address: string, + previousDescription: ServerDescription, + newDescription: ServerDescription + ) { + this.topologyId = topologyId; + this.address = address; + this.previousDescription = previousDescription; + this.newDescription = newDescription; + } +} + +/** + * Emitted when server is initialized. + * @public + * @category Event + */ +export class ServerOpeningEvent { + /** A unique identifier for the topology */ + topologyId: number; + /** The address (host/port pair) of the server */ + address: string; + /** @internal */ + name = SERVER_OPENING; + + /** @internal */ + constructor(topologyId: number, address: string) { + this.topologyId = topologyId; + this.address = address; + } +} + +/** + * Emitted when server is closed. + * @public + * @category Event + */ +export class ServerClosedEvent { + /** A unique identifier for the topology */ + topologyId: number; + /** The address (host/port pair) of the server */ + address: string; + /** @internal */ + name = SERVER_CLOSED; + + /** @internal */ + constructor(topologyId: number, address: string) { + this.topologyId = topologyId; + this.address = address; + } +} + +/** + * Emitted when topology description changes. + * @public + * @category Event + */ +export class TopologyDescriptionChangedEvent { + /** A unique identifier for the topology */ + topologyId: number; + /** The old topology description */ + previousDescription: TopologyDescription; + /** The new topology description */ + newDescription: TopologyDescription; + /** @internal */ + name = TOPOLOGY_DESCRIPTION_CHANGED; + + /** @internal */ + constructor( + topologyId: number, + previousDescription: TopologyDescription, + newDescription: TopologyDescription + ) { + this.topologyId = topologyId; + this.previousDescription = previousDescription; + this.newDescription = newDescription; + } +} + +/** + * Emitted when topology is initialized. + * @public + * @category Event + */ +export class TopologyOpeningEvent { + /** A unique identifier for the topology */ + topologyId: number; + /** @internal */ + name = TOPOLOGY_OPENING; + + /** @internal */ + constructor(topologyId: number) { + this.topologyId = topologyId; + } +} + +/** + * Emitted when topology is closed. + * @public + * @category Event + */ +export class TopologyClosedEvent { + /** A unique identifier for the topology */ + topologyId: number; + /** @internal */ + name = TOPOLOGY_CLOSED; + + /** @internal */ + constructor(topologyId: number) { + this.topologyId = topologyId; + } +} + +/** + * Emitted when the server monitor’s hello command is started - immediately before + * the hello command is serialized into raw BSON and written to the socket. + * + * @public + * @category Event + */ +export class ServerHeartbeatStartedEvent { + /** The connection id for the command */ + connectionId: string; + /** Is true when using the streaming protocol */ + awaited: boolean; + /** @internal */ + name = SERVER_HEARTBEAT_STARTED; + + /** @internal */ + constructor(connectionId: string, awaited: boolean) { + this.connectionId = connectionId; + this.awaited = awaited; + } +} + +/** + * Emitted when the server monitor’s hello succeeds. + * @public + * @category Event + */ +export class ServerHeartbeatSucceededEvent { + /** The connection id for the command */ + connectionId: string; + /** The execution time of the event in ms */ + duration: number; + /** The command reply */ + reply: Document; + /** Is true when using the streaming protocol */ + awaited: boolean; + /** @internal */ + name = SERVER_HEARTBEAT_SUCCEEDED; + + /** @internal */ + constructor(connectionId: string, duration: number, reply: Document | null, awaited: boolean) { + this.connectionId = connectionId; + this.duration = duration; + this.reply = reply ?? {}; + this.awaited = awaited; + } +} + +/** + * Emitted when the server monitor’s hello fails, either with an “ok: 0” or a socket exception. + * @public + * @category Event + */ +export class ServerHeartbeatFailedEvent { + /** The connection id for the command */ + connectionId: string; + /** The execution time of the event in ms */ + duration: number; + /** The command failure */ + failure: Error; + /** Is true when using the streaming protocol */ + awaited: boolean; + /** @internal */ + name = SERVER_HEARTBEAT_FAILED; + + /** @internal */ + constructor(connectionId: string, duration: number, failure: Error, awaited: boolean) { + this.connectionId = connectionId; + this.duration = duration; + this.failure = failure; + this.awaited = awaited; + } +} diff --git a/gateway/node_modules/mongodb/src/sdam/monitor.ts b/gateway/node_modules/mongodb/src/sdam/monitor.ts new file mode 100644 index 0000000000000000000000000000000000000000..e5b4a32a8490d3e77bea744c3c58a05a0ed86e59 --- /dev/null +++ b/gateway/node_modules/mongodb/src/sdam/monitor.ts @@ -0,0 +1,771 @@ +import { clearTimeout, setTimeout } from 'timers'; + +import { type Document, Long } from '../bson'; +import { connect, makeConnection, makeSocket, performInitialHandshake } from '../cmap/connect'; +import type { Connection, ConnectionOptions } from '../cmap/connection'; +import { getFAASEnv } from '../cmap/handshake/client_metadata'; +import { LEGACY_HELLO_COMMAND } from '../constants'; +import { MongoError, MongoErrorLabel, MongoNetworkTimeoutError } from '../error'; +import { MongoLoggableComponent } from '../mongo_logger'; +import { CancellationToken, TypedEventEmitter } from '../mongo_types'; +import { + calculateDurationInMs, + type Callback, + type EventEmitterWithState, + makeStateMachine, + noop, + ns, + processTimeMS +} from '../utils'; +import { ServerType, STATE_CLOSED, STATE_CLOSING } from './common'; +import { + ServerHeartbeatFailedEvent, + ServerHeartbeatStartedEvent, + ServerHeartbeatSucceededEvent +} from './events'; +import { Server } from './server'; +import type { TopologyVersion } from './server_description'; + +const STATE_IDLE = 'idle'; +const STATE_MONITORING = 'monitoring'; +const stateTransition = makeStateMachine({ + [STATE_CLOSING]: [STATE_CLOSING, STATE_IDLE, STATE_CLOSED], + [STATE_CLOSED]: [STATE_CLOSED, STATE_MONITORING], + [STATE_IDLE]: [STATE_IDLE, STATE_MONITORING, STATE_CLOSING], + [STATE_MONITORING]: [STATE_MONITORING, STATE_IDLE, STATE_CLOSING] +}); + +const INVALID_REQUEST_CHECK_STATES = new Set([STATE_CLOSING, STATE_CLOSED, STATE_MONITORING]); +function isInCloseState(monitor: Monitor) { + return monitor.s.state === STATE_CLOSED || monitor.s.state === STATE_CLOSING; +} + +/** @public */ +export const ServerMonitoringMode = Object.freeze({ + auto: 'auto', + poll: 'poll', + stream: 'stream' +} as const); + +/** @public */ +export type ServerMonitoringMode = (typeof ServerMonitoringMode)[keyof typeof ServerMonitoringMode]; + +/** @internal */ +export interface MonitorPrivate { + state: string; +} + +/** @public */ +export interface MonitorOptions + extends Omit { + connectTimeoutMS: number; + heartbeatFrequencyMS: number; + minHeartbeatFrequencyMS: number; + serverMonitoringMode: ServerMonitoringMode; +} + +/** @public */ +export type MonitorEvents = { + serverHeartbeatStarted(event: ServerHeartbeatStartedEvent): void; + serverHeartbeatSucceeded(event: ServerHeartbeatSucceededEvent): void; + serverHeartbeatFailed(event: ServerHeartbeatFailedEvent): void; + resetServer(error?: MongoError): void; + resetConnectionPool(): void; + close(): void; +} & EventEmitterWithState; + +/** @internal */ +export class Monitor extends TypedEventEmitter { + /** @internal */ + s: MonitorPrivate; + address: string; + options: Readonly< + Pick< + MonitorOptions, + | 'connectTimeoutMS' + | 'heartbeatFrequencyMS' + | 'minHeartbeatFrequencyMS' + | 'serverMonitoringMode' + > + >; + connectOptions: ConnectionOptions; + isRunningInFaasEnv: boolean; + server: Server; + connection: Connection | null; + cancellationToken: CancellationToken; + /** @internal */ + monitorId?: MonitorInterval; + rttPinger?: RTTPinger; + /** @internal */ + override component = MongoLoggableComponent.TOPOLOGY; + /** @internal */ + private rttSampler: RTTSampler; + + constructor(server: Server, options: MonitorOptions) { + super(); + this.on('error', noop); + + this.server = server; + this.connection = null; + this.cancellationToken = new CancellationToken(); + this.cancellationToken.setMaxListeners(Infinity); + this.monitorId = undefined; + this.s = { + state: STATE_CLOSED + }; + this.address = server.description.address; + this.options = Object.freeze({ + connectTimeoutMS: options.connectTimeoutMS ?? 10000, + heartbeatFrequencyMS: options.heartbeatFrequencyMS ?? 10000, + minHeartbeatFrequencyMS: options.minHeartbeatFrequencyMS ?? 500, + serverMonitoringMode: options.serverMonitoringMode + }); + this.isRunningInFaasEnv = getFAASEnv() != null; + this.mongoLogger = this.server.topology.client?.mongoLogger; + this.rttSampler = new RTTSampler(10); + + const cancellationToken = this.cancellationToken; + // TODO: refactor this to pull it directly from the pool, requires new ConnectionPool integration + const connectOptions = { + id: '' as const, + generation: server.pool.generation, + cancellationToken, + hostAddress: server.description.hostAddress, + ...options, + // force BSON serialization options + raw: false, + useBigInt64: false, + promoteLongs: true, + promoteValues: true, + promoteBuffers: true + }; + + // ensure no authentication is used for monitoring + delete connectOptions.credentials; + if (connectOptions.autoEncrypter) { + delete connectOptions.autoEncrypter; + } + + this.connectOptions = Object.freeze(connectOptions); + } + + connect(): void { + if (this.s.state !== STATE_CLOSED) { + return; + } + + // start + const heartbeatFrequencyMS = this.options.heartbeatFrequencyMS; + const minHeartbeatFrequencyMS = this.options.minHeartbeatFrequencyMS; + this.monitorId = new MonitorInterval(monitorServer(this), { + heartbeatFrequencyMS: heartbeatFrequencyMS, + minHeartbeatFrequencyMS: minHeartbeatFrequencyMS, + immediate: true + }); + } + + requestCheck(): void { + if (INVALID_REQUEST_CHECK_STATES.has(this.s.state)) { + return; + } + + this.monitorId?.wake(); + } + + reset(): void { + const topologyVersion = this.server.description.topologyVersion; + if (isInCloseState(this) || topologyVersion == null) { + return; + } + + stateTransition(this, STATE_CLOSING); + resetMonitorState(this); + + // restart monitor + stateTransition(this, STATE_IDLE); + + // restart monitoring + const heartbeatFrequencyMS = this.options.heartbeatFrequencyMS; + const minHeartbeatFrequencyMS = this.options.minHeartbeatFrequencyMS; + this.monitorId = new MonitorInterval(monitorServer(this), { + heartbeatFrequencyMS: heartbeatFrequencyMS, + minHeartbeatFrequencyMS: minHeartbeatFrequencyMS + }); + } + + close(): void { + if (isInCloseState(this)) { + return; + } + + stateTransition(this, STATE_CLOSING); + resetMonitorState(this); + + // close monitor + this.emit('close'); + stateTransition(this, STATE_CLOSED); + } + + get roundTripTime(): number { + return this.rttSampler.average(); + } + + get minRoundTripTime(): number { + return this.rttSampler.min(); + } + + get latestRtt(): number | null { + return this.rttSampler.last; + } + + addRttSample(rtt: number) { + this.rttSampler.addSample(rtt); + } + + clearRttSamples() { + this.rttSampler.clear(); + } +} + +function resetMonitorState(monitor: Monitor) { + monitor.monitorId?.stop(); + monitor.monitorId = undefined; + + monitor.rttPinger?.close(); + monitor.rttPinger = undefined; + + monitor.cancellationToken.emit('cancel'); + + monitor.connection?.destroy(); + monitor.connection = null; + + monitor.clearRttSamples(); +} + +function useStreamingProtocol(monitor: Monitor, topologyVersion: TopologyVersion | null): boolean { + // If we have no topology version we always poll no matter + // what the user provided, since the server does not support + // the streaming protocol. + if (topologyVersion == null) return false; + + const serverMonitoringMode = monitor.options.serverMonitoringMode; + if (serverMonitoringMode === ServerMonitoringMode.poll) return false; + if (serverMonitoringMode === ServerMonitoringMode.stream) return true; + + // If we are in auto mode, we need to figure out if we're in a FaaS + // environment or not and choose the appropriate mode. + if (monitor.isRunningInFaasEnv) return false; + return true; +} + +function checkServer(monitor: Monitor, callback: Callback) { + let start: number; + let awaited: boolean; + const topologyVersion = monitor.server.description.topologyVersion; + const isAwaitable = useStreamingProtocol(monitor, topologyVersion); + monitor.emitAndLogHeartbeat( + Server.SERVER_HEARTBEAT_STARTED, + monitor.server.topology.s.id, + undefined, + new ServerHeartbeatStartedEvent(monitor.address, isAwaitable) + ); + + function onHeartbeatFailed(err: Error) { + monitor.connection?.destroy(); + monitor.connection = null; + monitor.emitAndLogHeartbeat( + Server.SERVER_HEARTBEAT_FAILED, + monitor.server.topology.s.id, + undefined, + new ServerHeartbeatFailedEvent(monitor.address, calculateDurationInMs(start), err, awaited) + ); + + const error = !(err instanceof MongoError) + ? new MongoError(MongoError.buildErrorMessage(err), { cause: err }) + : err; + error.addErrorLabel(MongoErrorLabel.ResetPool); + if (error instanceof MongoNetworkTimeoutError) { + error.addErrorLabel(MongoErrorLabel.InterruptInUseConnections); + } + + monitor.emit('resetServer', error); + callback(err); + } + + function onHeartbeatSucceeded(hello: Document) { + if (!('isWritablePrimary' in hello)) { + // Provide hello-style response document. + hello.isWritablePrimary = hello[LEGACY_HELLO_COMMAND]; + } + + // NOTE: here we use the latestRtt as this measurement corresponds with the value + // obtained for this successful heartbeat, if there is no latestRtt, then we calculate the + // duration + const duration = + isAwaitable && monitor.rttPinger + ? (monitor.rttPinger.latestRtt ?? calculateDurationInMs(start)) + : calculateDurationInMs(start); + + monitor.addRttSample(duration); + + monitor.emitAndLogHeartbeat( + Server.SERVER_HEARTBEAT_SUCCEEDED, + monitor.server.topology.s.id, + hello.connectionId, + new ServerHeartbeatSucceededEvent(monitor.address, duration, hello, isAwaitable) + ); + + if (isAwaitable) { + // If we are using the streaming protocol then we immediately issue another 'started' + // event, otherwise the "check" is complete and return to the main monitor loop + monitor.emitAndLogHeartbeat( + Server.SERVER_HEARTBEAT_STARTED, + monitor.server.topology.s.id, + undefined, + new ServerHeartbeatStartedEvent(monitor.address, true) + ); + // We have not actually sent an outgoing handshake, but when we get the next response we + // want the duration to reflect the time since we last heard from the server + start = processTimeMS(); + } else { + monitor.rttPinger?.close(); + monitor.rttPinger = undefined; + + callback(undefined, hello); + } + } + + const { connection } = monitor; + if (connection && !connection.closed) { + const { serverApi, helloOk } = connection; + const connectTimeoutMS = monitor.options.connectTimeoutMS; + const maxAwaitTimeMS = monitor.options.heartbeatFrequencyMS; + + const cmd = { + [serverApi?.version || helloOk ? 'hello' : LEGACY_HELLO_COMMAND]: 1, + ...(isAwaitable && topologyVersion + ? { maxAwaitTimeMS, topologyVersion: makeTopologyVersion(topologyVersion) } + : {}) + }; + + const options = isAwaitable + ? { + socketTimeoutMS: connectTimeoutMS ? connectTimeoutMS + maxAwaitTimeMS : 0, + exhaustAllowed: true + } + : { socketTimeoutMS: connectTimeoutMS }; + + if (isAwaitable && monitor.rttPinger == null) { + monitor.rttPinger = new RTTPinger(monitor); + } + + // Record new start time before sending handshake + start = processTimeMS(); + + if (isAwaitable) { + awaited = true; + return connection.exhaustCommand(ns('admin.$cmd'), cmd, options, (error, hello) => { + if (error) return onHeartbeatFailed(error); + return onHeartbeatSucceeded(hello); + }); + } + + awaited = false; + connection + .command(ns('admin.$cmd'), cmd, options) + .then(onHeartbeatSucceeded, onHeartbeatFailed); + + return; + } + + // connecting does an implicit `hello` + (async () => { + const socket = await makeSocket(monitor.connectOptions); + const connection = makeConnection(monitor.connectOptions, socket); + // The start time is after socket creation but before the handshake + start = processTimeMS(); + try { + await performInitialHandshake(connection, monitor.connectOptions); + return connection; + } catch (error) { + connection.destroy(); + throw error; + } + })().then( + connection => { + if (isInCloseState(monitor)) { + connection.destroy(); + return; + } + const duration = calculateDurationInMs(start); + monitor.addRttSample(duration); + + monitor.connection = connection; + monitor.emitAndLogHeartbeat( + Server.SERVER_HEARTBEAT_SUCCEEDED, + monitor.server.topology.s.id, + connection.hello?.connectionId, + new ServerHeartbeatSucceededEvent( + monitor.address, + duration, + connection.hello, + useStreamingProtocol(monitor, connection.hello?.topologyVersion) + ) + ); + + callback(undefined, connection.hello); + }, + error => { + monitor.connection = null; + awaited = false; + onHeartbeatFailed(error); + } + ); +} + +function monitorServer(monitor: Monitor) { + return (callback: Callback) => { + if (monitor.s.state === STATE_MONITORING) { + queueMicrotask(callback); + return; + } + stateTransition(monitor, STATE_MONITORING); + function done() { + if (!isInCloseState(monitor)) { + stateTransition(monitor, STATE_IDLE); + } + + callback(); + } + + checkServer(monitor, (err, hello) => { + if (err) { + // otherwise an error occurred on initial discovery, also bail + if (monitor.server.description.type === ServerType.Unknown) { + return done(); + } + } + + // if the check indicates streaming is supported, immediately reschedule monitoring + if (useStreamingProtocol(monitor, hello?.topologyVersion)) { + setTimeout(() => { + if (!isInCloseState(monitor)) { + monitor.monitorId?.wake(); + } + }, 0); + } + + done(); + }); + }; +} + +function makeTopologyVersion(tv: TopologyVersion) { + return { + processId: tv.processId, + // tests mock counter as just number, but in a real situation counter should always be a Long + // TODO(NODE-2674): Preserve int64 sent from MongoDB + counter: Long.isLong(tv.counter) ? tv.counter : Long.fromNumber(tv.counter) + }; +} + +/** @internal */ +export interface RTTPingerOptions extends ConnectionOptions { + heartbeatFrequencyMS: number; +} + +/** @internal */ +export class RTTPinger { + connection?: Connection; + /** @internal */ + cancellationToken: CancellationToken; + /** @internal */ + monitorId: NodeJS.Timeout; + /** @internal */ + monitor: Monitor; + closed: boolean; + /** @internal */ + latestRtt?: number; + + constructor(monitor: Monitor) { + this.connection = undefined; + this.cancellationToken = monitor.cancellationToken; + this.closed = false; + this.monitor = monitor; + this.latestRtt = monitor.latestRtt ?? undefined; + + const heartbeatFrequencyMS = monitor.options.heartbeatFrequencyMS; + this.monitorId = setTimeout(() => this.measureRoundTripTime(), heartbeatFrequencyMS); + } + + get roundTripTime(): number { + return this.monitor.roundTripTime; + } + + get minRoundTripTime(): number { + return this.monitor.minRoundTripTime; + } + + close(): void { + this.closed = true; + clearTimeout(this.monitorId); + + this.connection?.destroy(); + this.connection = undefined; + } + + private measureAndReschedule(start: number, conn?: Connection) { + if (this.closed) { + conn?.destroy(); + return; + } + + if (this.connection == null) { + this.connection = conn; + } + + this.latestRtt = calculateDurationInMs(start); + this.monitorId = setTimeout( + () => this.measureRoundTripTime(), + this.monitor.options.heartbeatFrequencyMS + ); + } + + private measureRoundTripTime() { + const start = processTimeMS(); + + if (this.closed) { + return; + } + + const connection = this.connection; + if (connection == null) { + connect(this.monitor.connectOptions).then( + connection => { + this.measureAndReschedule(start, connection); + }, + () => { + this.connection = undefined; + } + ); + return; + } + + const commandName = + connection.serverApi?.version || connection.helloOk ? 'hello' : LEGACY_HELLO_COMMAND; + + connection.command(ns('admin.$cmd'), { [commandName]: 1 }, undefined).then( + () => this.measureAndReschedule(start), + () => { + this.connection?.destroy(); + this.connection = undefined; + return; + } + ); + } +} + +/** + * @internal + */ +export interface MonitorIntervalOptions { + /** The interval to execute a method on */ + heartbeatFrequencyMS: number; + /** A minimum interval that must elapse before the method is called */ + minHeartbeatFrequencyMS: number; + /** Whether the method should be called immediately when the interval is started */ + immediate: boolean; +} + +/** + * @internal + */ +export class MonitorInterval { + fn: (callback: Callback) => void; + timerId: NodeJS.Timeout | undefined; + lastExecutionEnded: number; + isExpeditedCallToFnScheduled = false; + stopped = false; + isExecutionInProgress = false; + hasExecutedOnce = false; + + heartbeatFrequencyMS: number; + minHeartbeatFrequencyMS: number; + + constructor(fn: (callback: Callback) => void, options: Partial = {}) { + this.fn = fn; + this.lastExecutionEnded = -Infinity; + + this.heartbeatFrequencyMS = options.heartbeatFrequencyMS ?? 1000; + this.minHeartbeatFrequencyMS = options.minHeartbeatFrequencyMS ?? 500; + + if (options.immediate) { + this._executeAndReschedule(); + } else { + this._reschedule(undefined); + } + } + + wake() { + const currentTime = processTimeMS(); + const timeSinceLastCall = currentTime - this.lastExecutionEnded; + + // TODO(NODE-4674): Add error handling and logging to the monitor + if (timeSinceLastCall < 0) { + return this._executeAndReschedule(); + } + + if (this.isExecutionInProgress) { + return; + } + + // debounce multiple calls to wake within the `minInterval` + if (this.isExpeditedCallToFnScheduled) { + return; + } + + // reschedule a call as soon as possible, ensuring the call never happens + // faster than the `minInterval` + if (timeSinceLastCall < this.minHeartbeatFrequencyMS) { + this.isExpeditedCallToFnScheduled = true; + this._reschedule(this.minHeartbeatFrequencyMS - timeSinceLastCall); + return; + } + + this._executeAndReschedule(); + } + + stop() { + this.stopped = true; + if (this.timerId) { + clearTimeout(this.timerId); + this.timerId = undefined; + } + + this.lastExecutionEnded = -Infinity; + this.isExpeditedCallToFnScheduled = false; + } + + toString() { + return JSON.stringify(this); + } + + toJSON() { + const currentTime = processTimeMS(); + const timeSinceLastCall = currentTime - this.lastExecutionEnded; + return { + timerId: this.timerId != null ? 'set' : 'cleared', + lastCallTime: this.lastExecutionEnded, + isExpeditedCheckScheduled: this.isExpeditedCallToFnScheduled, + stopped: this.stopped, + heartbeatFrequencyMS: this.heartbeatFrequencyMS, + minHeartbeatFrequencyMS: this.minHeartbeatFrequencyMS, + currentTime, + timeSinceLastCall + }; + } + + private _reschedule(ms?: number) { + if (this.stopped) return; + if (this.timerId) { + clearTimeout(this.timerId); + } + + this.timerId = setTimeout(this._executeAndReschedule, ms || this.heartbeatFrequencyMS); + } + + private _executeAndReschedule = () => { + if (this.stopped) return; + if (this.timerId) { + clearTimeout(this.timerId); + } + + this.isExpeditedCallToFnScheduled = false; + this.isExecutionInProgress = true; + + this.fn(() => { + this.lastExecutionEnded = processTimeMS(); + this.isExecutionInProgress = false; + this._reschedule(this.heartbeatFrequencyMS); + }); + }; +} + +/** @internal + * This class implements the RTT sampling logic specified for [CSOT](https://github.com/mongodb/specifications/blob/bbb335e60cd7ea1e0f7cd9a9443cb95fc9d3b64d/source/client-side-operations-timeout/client-side-operations-timeout.md#drivers-use-minimum-rtt-to-short-circuit-operations) + * + * This is implemented as a [circular buffer](https://en.wikipedia.org/wiki/Circular_buffer) keeping + * the most recent `windowSize` samples + * */ +export class RTTSampler { + /** Index of the next slot to be overwritten */ + private writeIndex: number; + private length: number; + private rttSamples: Float64Array; + + constructor(windowSize = 10) { + this.rttSamples = new Float64Array(windowSize); + this.length = 0; + this.writeIndex = 0; + } + + /** + * Adds an rtt sample to the end of the circular buffer + * When `windowSize` samples have been collected, `addSample` overwrites the least recently added + * sample + */ + addSample(sample: number) { + this.rttSamples[this.writeIndex++] = sample; + if (this.length < this.rttSamples.length) { + this.length++; + } + + this.writeIndex %= this.rttSamples.length; + } + + /** + * When \< 2 samples have been collected, returns 0 + * Otherwise computes the minimum value samples contained in the buffer + */ + min(): number { + if (this.length < 2) return 0; + let min = this.rttSamples[0]; + for (let i = 1; i < this.length; i++) { + if (this.rttSamples[i] < min) min = this.rttSamples[i]; + } + + return min; + } + + /** + * Returns mean of samples contained in the buffer + */ + average(): number { + if (this.length === 0) return 0; + let sum = 0; + for (let i = 0; i < this.length; i++) { + sum += this.rttSamples[i]; + } + + return sum / this.length; + } + + /** + * Returns most recently inserted element in the buffer + * Returns null if the buffer is empty + * */ + get last(): number | null { + if (this.length === 0) return null; + return this.rttSamples[this.writeIndex === 0 ? this.length - 1 : this.writeIndex - 1]; + } + + /** + * Clear the buffer + * NOTE: this does not overwrite the data held in the internal array, just the pointers into + * this array + */ + clear() { + this.length = 0; + this.writeIndex = 0; + } +} diff --git a/gateway/node_modules/mongodb/src/sdam/server.ts b/gateway/node_modules/mongodb/src/sdam/server.ts new file mode 100644 index 0000000000000000000000000000000000000000..14b6455f1f189b00885aad7d9004e44bce7dfab2 --- /dev/null +++ b/gateway/node_modules/mongodb/src/sdam/server.ts @@ -0,0 +1,595 @@ +import type { Document } from '../bson'; +import { type AutoEncrypter } from '../client-side-encryption/auto_encrypter'; +import { type CommandOptions, Connection } from '../cmap/connection'; +import { + ConnectionPool, + type ConnectionPoolEvents, + type ConnectionPoolOptions +} from '../cmap/connection_pool'; +import { PoolClearedError } from '../cmap/errors'; +import { + APM_EVENTS, + CLOSED, + CMAP_EVENTS, + CONNECT, + DESCRIPTION_RECEIVED, + ENDED, + HEARTBEAT_EVENTS, + SERVER_HEARTBEAT_FAILED, + SERVER_HEARTBEAT_STARTED, + SERVER_HEARTBEAT_SUCCEEDED +} from '../constants'; +import { + type AnyError, + isNodeShuttingDownError, + isStateChangeError, + MONGODB_ERROR_CODES, + MongoError, + MongoErrorLabel, + MongoNetworkError, + MongoNetworkTimeoutError, + MongoParseError, + MongoRuntimeError, + MongoServerClosedError, + type MongoServerError, + needsRetryableWriteLabel +} from '../error'; +import type { ServerApi } from '../mongo_client'; +import { type Abortable, TypedEventEmitter } from '../mongo_types'; +import { AggregateOperation } from '../operations/aggregate'; +import type { GetMoreOptions } from '../operations/get_more'; +import { type AbstractOperation } from '../operations/operation'; +import type { ClientSession } from '../sessions'; +import { type TimeoutContext } from '../timeout'; +import { isTransactionCommand } from '../transactions'; +import { + abortable, + type EventEmitterWithState, + makeStateMachine, + maxWireVersion, + noop, + squashError, + supportsRetryableWrites +} from '../utils'; +import { throwIfWriteConcernError } from '../write_concern'; +import { + type ClusterTime, + STATE_CLOSED, + STATE_CLOSING, + STATE_CONNECTED, + STATE_CONNECTING, + TopologyType +} from './common'; +import type { + ServerHeartbeatFailedEvent, + ServerHeartbeatStartedEvent, + ServerHeartbeatSucceededEvent +} from './events'; +import { Monitor, type MonitorOptions } from './monitor'; +import { compareTopologyVersion, ServerDescription } from './server_description'; +import { MIN_SECONDARY_WRITE_WIRE_VERSION } from './server_selection'; +import type { Topology } from './topology'; + +const stateTransition = makeStateMachine({ + [STATE_CLOSED]: [STATE_CLOSED, STATE_CONNECTING], + [STATE_CONNECTING]: [STATE_CONNECTING, STATE_CLOSING, STATE_CONNECTED, STATE_CLOSED], + [STATE_CONNECTED]: [STATE_CONNECTED, STATE_CLOSING, STATE_CLOSED], + [STATE_CLOSING]: [STATE_CLOSING, STATE_CLOSED] +}); + +/** @internal */ +export type ServerOptions = Omit & + MonitorOptions; + +/** @internal */ +export interface ServerPrivate { + /** The server description for this server */ + description: ServerDescription; + /** A copy of the options used to construct this instance */ + options: ServerOptions; + /** The current state of the Server */ + state: string; + /** MongoDB server API version */ + serverApi?: ServerApi; + /** A count of the operations currently running against the server. */ + operationCount: number; +} + +/** @public */ +export type ServerEvents = { + serverHeartbeatStarted(event: ServerHeartbeatStartedEvent): void; + serverHeartbeatSucceeded(event: ServerHeartbeatSucceededEvent): void; + serverHeartbeatFailed(event: ServerHeartbeatFailedEvent): void; + /** Top level MongoClient doesn't emit this so it is marked: @internal */ + connect(server: Server): void; + descriptionReceived(description: ServerDescription): void; + closed(): void; + ended(): void; +} & ConnectionPoolEvents & + EventEmitterWithState; + +/** @internal */ +export type ServerCommandOptions = Omit & { + timeoutContext: TimeoutContext; + returnFieldSelector?: Document | null; +} & Abortable; + +/** @internal */ +export class Server extends TypedEventEmitter { + /** @internal */ + s: ServerPrivate; + /** @internal */ + topology: Topology; + /** @internal */ + pool: ConnectionPool; + serverApi?: ServerApi; + hello?: Document; + monitor: Monitor | null; + + /** @event */ + static readonly SERVER_HEARTBEAT_STARTED = SERVER_HEARTBEAT_STARTED; + /** @event */ + static readonly SERVER_HEARTBEAT_SUCCEEDED = SERVER_HEARTBEAT_SUCCEEDED; + /** @event */ + static readonly SERVER_HEARTBEAT_FAILED = SERVER_HEARTBEAT_FAILED; + /** @event */ + static readonly CONNECT = CONNECT; + /** @event */ + static readonly DESCRIPTION_RECEIVED = DESCRIPTION_RECEIVED; + /** @event */ + static readonly CLOSED = CLOSED; + /** @event */ + static readonly ENDED = ENDED; + + /** + * Create a server + */ + constructor(topology: Topology, description: ServerDescription, options: ServerOptions) { + super(); + this.on('error', noop); + + this.serverApi = options.serverApi; + + const poolOptions = { hostAddress: description.hostAddress, ...options }; + + this.topology = topology; + this.pool = new ConnectionPool(this, poolOptions); + + this.s = { + description, + options, + state: STATE_CLOSED, + operationCount: 0 + }; + + for (const event of [...CMAP_EVENTS, ...APM_EVENTS]) { + this.pool.on(event, (e: any) => this.emit(event, e)); + } + + this.pool.on(Connection.CLUSTER_TIME_RECEIVED, (clusterTime: ClusterTime) => { + this.clusterTime = clusterTime; + }); + + if (this.loadBalanced) { + this.monitor = null; + // monitoring is disabled in load balancing mode + return; + } + + // create the monitor + this.monitor = new Monitor(this, this.s.options); + + for (const event of HEARTBEAT_EVENTS) { + this.monitor.on(event, (e: any) => this.emit(event, e)); + } + + this.monitor.on('resetServer', (error: MongoServerError) => markServerUnknown(this, error)); + this.monitor.on(Server.SERVER_HEARTBEAT_SUCCEEDED, (event: ServerHeartbeatSucceededEvent) => { + this.emit( + Server.DESCRIPTION_RECEIVED, + new ServerDescription(this.description.hostAddress, event.reply, { + roundTripTime: this.monitor?.roundTripTime, + minRoundTripTime: this.monitor?.minRoundTripTime + }) + ); + + if (this.s.state === STATE_CONNECTING) { + stateTransition(this, STATE_CONNECTED); + this.emit(Server.CONNECT, this); + } + }); + } + + get clusterTime(): ClusterTime | undefined { + return this.topology.clusterTime; + } + + set clusterTime(clusterTime: ClusterTime | undefined) { + this.topology.clusterTime = clusterTime; + } + + get description(): ServerDescription { + return this.s.description; + } + + get name(): string { + return this.s.description.address; + } + + get autoEncrypter(): AutoEncrypter | undefined { + if (this.s.options && this.s.options.autoEncrypter) { + return this.s.options.autoEncrypter; + } + return; + } + + get loadBalanced(): boolean { + return this.topology.description.type === TopologyType.LoadBalanced; + } + + /** + * Initiate server connect + */ + connect(): void { + if (this.s.state !== STATE_CLOSED) { + return; + } + + stateTransition(this, STATE_CONNECTING); + + // If in load balancer mode we automatically set the server to + // a load balancer. It never transitions out of this state and + // has no monitor. + if (!this.loadBalanced) { + this.monitor?.connect(); + } else { + stateTransition(this, STATE_CONNECTED); + this.emit(Server.CONNECT, this); + } + } + + closeCheckedOutConnections() { + return this.pool.closeCheckedOutConnections(); + } + + /** Destroy the server connection */ + close(): void { + if (this.s.state === STATE_CLOSED) { + return; + } + + stateTransition(this, STATE_CLOSING); + + if (!this.loadBalanced) { + this.monitor?.close(); + } + + this.pool.close(); + stateTransition(this, STATE_CLOSED); + this.emit('closed'); + } + + /** + * Immediately schedule monitoring of this server. If there already an attempt being made + * this will be a no-op. + */ + requestCheck(): void { + if (!this.loadBalanced) { + this.monitor?.requestCheck(); + } + } + + public async command( + operation: AbstractOperation, + timeoutContext: TimeoutContext + ): Promise> { + if (this.s.state === STATE_CLOSING || this.s.state === STATE_CLOSED) { + throw new MongoServerClosedError(); + } + const session = operation.session; + + let conn = session?.pinnedConnection; + + this.incrementOperationCount(); + if (conn == null) { + try { + conn = await this.pool.checkOut({ timeoutContext, signal: operation.options.signal }); + } catch (checkoutError) { + this.decrementOperationCount(); + if (!(checkoutError instanceof PoolClearedError)) this.handleError(checkoutError); + throw checkoutError; + } + } + + let reauthPromise: Promise | null = null; + const cleanup = () => { + this.decrementOperationCount(); + if (session?.pinnedConnection !== conn) { + if (reauthPromise != null) { + // The reauth promise only exists if it hasn't thrown. + const checkBackIn = () => { + this.pool.checkIn(conn); + }; + void reauthPromise.then(checkBackIn, checkBackIn); + } else { + this.pool.checkIn(conn); + } + } + }; + + let cmd; + try { + cmd = operation.buildCommand(conn, session); + } catch (e) { + cleanup(); + throw e; + } + + const options = operation.buildOptions(timeoutContext); + const ns = operation.ns; + + if (this.loadBalanced && isPinnableCommand(cmd, session) && !session?.pinnedConnection) { + session?.pin(conn); + } + + options.directConnection = this.topology.s.options.directConnection; + + const omitReadPreference = + operation instanceof AggregateOperation && + operation.hasWriteStage && + maxWireVersion(conn) < MIN_SECONDARY_WRITE_WIRE_VERSION; + if (omitReadPreference) { + delete options.readPreference; + } + + if (this.description.iscryptd) { + options.omitMaxTimeMS = true; + } + + try { + try { + const res = await conn.command(ns, cmd, options, operation.SERVER_COMMAND_RESPONSE_TYPE); + throwIfWriteConcernError(res); + return res; + } catch (commandError) { + throw this.decorateCommandError(conn, cmd, options, commandError); + } + } catch (operationError) { + if ( + operationError instanceof MongoError && + operationError.code?.valueOf() === MONGODB_ERROR_CODES.Reauthenticate + ) { + reauthPromise = this.pool.reauthenticate(conn); + reauthPromise.then(undefined, error => { + reauthPromise = null; + squashError(error); + }); + + await abortable(reauthPromise, options); + reauthPromise = null; // only reachable if reauth succeeds + + try { + const res = await conn.command(ns, cmd, options, operation.SERVER_COMMAND_RESPONSE_TYPE); + throwIfWriteConcernError(res); + return res; + } catch (commandError) { + throw this.decorateCommandError(conn, cmd, options, commandError); + } + } else { + throw operationError; + } + } finally { + cleanup(); + } + } + + /** + * Handle SDAM error + * @internal + */ + handleError(error: AnyError, connection?: Connection) { + if (!(error instanceof MongoError)) { + return; + } + + if (isStaleError(this, error)) { + return; + } + + const isNetworkNonTimeoutError = + error instanceof MongoNetworkError && !(error instanceof MongoNetworkTimeoutError); + const isNetworkTimeoutBeforeHandshakeError = + error instanceof MongoNetworkError && error.beforeHandshake; + const isAuthOrEstablishmentHandshakeError = error.hasErrorLabel(MongoErrorLabel.HandshakeError); + const isSystemOverloadError = error.hasErrorLabel(MongoErrorLabel.SystemOverloadedError); + + // Perhaps questionable and divergent from the spec, but considering MongoParseErrors like state change errors was legacy behavior. + if (isStateChangeError(error) || error instanceof MongoParseError) { + const shouldClearPool = isNodeShuttingDownError(error); + + // from the SDAM spec: The driver MUST synchronize clearing the pool with updating the topology. + // In load balanced mode: there is no monitoring, so there is no topology to update. We simply clear the pool. + // For other topologies: the `ResetPool` label instructs the topology to clear the server's pool in `updateServer()`. + if (!this.loadBalanced) { + if (shouldClearPool) { + error.addErrorLabel(MongoErrorLabel.ResetPool); + } + markServerUnknown(this, error); + queueMicrotask(() => this.requestCheck()); + return; + } + + if (connection && shouldClearPool) { + this.pool.clear({ serviceId: connection.serviceId }); + } + } else if ( + isNetworkNonTimeoutError || + isNetworkTimeoutBeforeHandshakeError || + isAuthOrEstablishmentHandshakeError + ) { + // Do NOT clear the pool if we encounter a system overloaded error. + if (isSystemOverloadError) { + return; + } + // from the SDAM spec: The driver MUST synchronize clearing the pool with updating the topology. + // In load balanced mode: there is no monitoring, so there is no topology to update. We simply clear the pool. + // For other topologies: the `ResetPool` label instructs the topology to clear the server's pool in `updateServer()`. + if (!this.loadBalanced) { + error.addErrorLabel(MongoErrorLabel.ResetPool); + markServerUnknown(this, error); + } else if (connection) { + this.pool.clear({ serviceId: connection.serviceId }); + } + } + } + + /** + * Ensure that error is properly decorated and internal state is updated before throwing + * @internal + */ + private decorateCommandError( + connection: Connection, + cmd: Document, + options: CommandOptions | GetMoreOptions | undefined, + error: unknown + ): Error { + if (typeof error !== 'object' || error == null || !('name' in error)) { + throw new MongoRuntimeError('An unexpected error type: ' + typeof error); + } + + if (error.name === 'AbortError' && 'cause' in error && error.cause instanceof MongoError) { + error = error.cause; + } + + if (!(error instanceof MongoError)) { + // Node.js or some other error we have not special handling for + return error as Error; + } + + if (connectionIsStale(this.pool, connection)) { + return error; + } + + const session = options?.session; + if (error instanceof MongoNetworkError) { + if (session && !session.hasEnded && session.serverSession) { + session.serverSession.isDirty = true; + } + + // inActiveTransaction check handles commit and abort. + if ( + inActiveTransaction(session, cmd) && + !error.hasErrorLabel(MongoErrorLabel.TransientTransactionError) + ) { + error.addErrorLabel(MongoErrorLabel.TransientTransactionError); + } + + if ( + (isRetryableWritesEnabled(this.topology) || isTransactionCommand(cmd)) && + supportsRetryableWrites(this) && + !inActiveTransaction(session, cmd) + ) { + error.addErrorLabel(MongoErrorLabel.RetryableWriteError); + } + } else { + if ( + (isRetryableWritesEnabled(this.topology) || isTransactionCommand(cmd)) && + needsRetryableWriteLabel(error, maxWireVersion(this), this.description.type) && + !inActiveTransaction(session, cmd) + ) { + error.addErrorLabel(MongoErrorLabel.RetryableWriteError); + } + } + + if ( + session && + session.isPinned && + error.hasErrorLabel(MongoErrorLabel.TransientTransactionError) + ) { + session.unpin({ force: true }); + } + + this.handleError(error, connection); + + return error; + } + + /** + * Decrement the operation count, returning the new count. + */ + private decrementOperationCount(): number { + return (this.s.operationCount -= 1); + } + + /** + * Increment the operation count, returning the new count. + */ + private incrementOperationCount(): number { + return (this.s.operationCount += 1); + } +} + +function markServerUnknown(server: Server, error?: MongoError) { + // Load balancer servers can never be marked unknown. + if (server.loadBalanced) { + return; + } + + if (error instanceof MongoNetworkError && !(error instanceof MongoNetworkTimeoutError)) { + server.monitor?.reset(); + } + + server.emit( + Server.DESCRIPTION_RECEIVED, + new ServerDescription(server.description.hostAddress, undefined, { error }) + ); +} + +function isPinnableCommand(cmd: Document, session?: ClientSession): boolean { + if (session) { + return ( + session.inTransaction() || + (session.transaction.isCommitted && 'commitTransaction' in cmd) || + 'aggregate' in cmd || + 'find' in cmd || + 'getMore' in cmd || + 'listCollections' in cmd || + 'listIndexes' in cmd || + 'bulkWrite' in cmd + ); + } + + return false; +} + +function connectionIsStale(pool: ConnectionPool, connection: Connection) { + if (connection.serviceId) { + return ( + connection.generation !== pool.serviceGenerations.get(connection.serviceId.toHexString()) + ); + } + + return connection.generation !== pool.generation; +} + +function inActiveTransaction(session: ClientSession | undefined, cmd: Document) { + return session && session.inTransaction() && !isTransactionCommand(cmd); +} + +/** this checks the retryWrites option passed down from the client options, it + * does not check if the server supports retryable writes */ +function isRetryableWritesEnabled(topology: Topology) { + return topology.s.options.retryWrites !== false; +} + +function isStaleError(server: Server, error: MongoError): boolean { + const currentGeneration = server.pool.generation; + const generation = error.connectionGeneration; + + if (generation && generation < currentGeneration) { + return true; + } + + const currentTopologyVersion = server.description.topologyVersion; + return compareTopologyVersion(currentTopologyVersion, error.topologyVersion) >= 0; +} diff --git a/gateway/node_modules/mongodb/src/sdam/server_description.ts b/gateway/node_modules/mongodb/src/sdam/server_description.ts new file mode 100644 index 0000000000000000000000000000000000000000..d7f16fe2877993e81492d6f7a7091ee5bd7364f9 --- /dev/null +++ b/gateway/node_modules/mongodb/src/sdam/server_description.ts @@ -0,0 +1,297 @@ +import { type Document, Long, type ObjectId } from '../bson'; +import { type MongoError, MongoRuntimeError } from '../error'; +import { + arrayStrictEqual, + compareObjectId, + errorStrictEqual, + HostAddress, + processTimeMS +} from '../utils'; +import { type ClusterTime, ServerType } from './common'; + +const WRITABLE_SERVER_TYPES = new Set([ + ServerType.RSPrimary, + ServerType.Standalone, + ServerType.Mongos, + ServerType.LoadBalancer +]); + +const DATA_BEARING_SERVER_TYPES = new Set([ + ServerType.RSPrimary, + ServerType.RSSecondary, + ServerType.Mongos, + ServerType.Standalone, + ServerType.LoadBalancer +]); + +/** @public */ +export interface TopologyVersion { + processId: ObjectId; + counter: Long; +} + +/** @public */ +export type TagSet = { [key: string]: string }; + +/** @internal */ +export interface ServerDescriptionOptions { + /** An Error used for better reporting debugging */ + error?: MongoError; + + /** The average round trip time to ping this server (in ms) */ + roundTripTime?: number; + /** The minimum round trip time to ping this server over the past 10 samples(in ms) */ + minRoundTripTime?: number; + + /** If the client is in load balancing mode. */ + loadBalanced?: boolean; +} + +/** + * The client's view of a single server, based on the most recent hello outcome. + * + * Internal type, not meant to be directly instantiated + * @public + */ +export class ServerDescription { + address: string; + type: ServerType; + hosts: string[]; + passives: string[]; + arbiters: string[]; + tags: TagSet; + error: MongoError | null; + topologyVersion: TopologyVersion | null; + minWireVersion: number; + maxWireVersion: number; + roundTripTime: number; + /** The minimum measurement of the last 10 measurements of roundTripTime that have been collected */ + minRoundTripTime: number; + lastUpdateTime: number; + lastWriteDate: number; + me: string | null; + primary: string | null; + setName: string | null; + setVersion: number | null; + electionId: ObjectId | null; + logicalSessionTimeoutMinutes: number | null; + /** The max message size in bytes for the server. */ + maxMessageSizeBytes: number | null; + /** The max number of writes in a bulk write command. */ + maxWriteBatchSize: number | null; + /** The max bson object size. */ + maxBsonObjectSize: number | null; + /** Indicates server is a mongocryptd instance. */ + iscryptd: boolean; + + // NOTE: does this belong here? It seems we should gossip the cluster time at the CMAP level + $clusterTime?: ClusterTime; + + /** + * Create a ServerDescription + * @internal + * + * @param address - The address of the server + * @param hello - An optional hello response for this server + */ + constructor( + address: HostAddress | string, + hello?: Document, + options: ServerDescriptionOptions = {} + ) { + if (address == null || address === '') { + throw new MongoRuntimeError('ServerDescription must be provided with a non-empty address'); + } + + this.address = + typeof address === 'string' + ? HostAddress.fromString(address).toString() // Use HostAddress to normalize + : address.toString(); + this.type = parseServerType(hello, options); + this.hosts = hello?.hosts?.map((host: string) => host.toLowerCase()) ?? []; + this.passives = hello?.passives?.map((host: string) => host.toLowerCase()) ?? []; + this.arbiters = hello?.arbiters?.map((host: string) => host.toLowerCase()) ?? []; + this.tags = hello?.tags ?? {}; + this.minWireVersion = hello?.minWireVersion ?? 0; + this.maxWireVersion = hello?.maxWireVersion ?? 0; + this.roundTripTime = options?.roundTripTime ?? -1; + this.minRoundTripTime = options?.minRoundTripTime ?? 0; + this.lastUpdateTime = processTimeMS(); + this.lastWriteDate = hello?.lastWrite?.lastWriteDate ?? 0; + // NOTE: This actually builds the stack string instead of holding onto the getter and all its + // associated references. This is done to prevent a memory leak. + this.error = options.error ?? null; + this.error?.stack; + // TODO(NODE-2674): Preserve int64 sent from MongoDB + this.topologyVersion = this.error?.topologyVersion ?? hello?.topologyVersion ?? null; + this.setName = hello?.setName ?? null; + this.setVersion = hello?.setVersion ?? null; + this.electionId = hello?.electionId ?? null; + this.logicalSessionTimeoutMinutes = hello?.logicalSessionTimeoutMinutes ?? null; + this.maxMessageSizeBytes = hello?.maxMessageSizeBytes ?? null; + this.maxWriteBatchSize = hello?.maxWriteBatchSize ?? null; + this.maxBsonObjectSize = hello?.maxBsonObjectSize ?? null; + this.primary = hello?.primary ?? null; + this.me = hello?.me?.toLowerCase() ?? null; + this.$clusterTime = hello?.$clusterTime ?? null; + this.iscryptd = Boolean(hello?.iscryptd); + } + + get hostAddress(): HostAddress { + return HostAddress.fromString(this.address); + } + + get allHosts(): string[] { + return this.hosts.concat(this.arbiters).concat(this.passives); + } + + /** Is this server available for reads*/ + get isReadable(): boolean { + return this.type === ServerType.RSSecondary || this.isWritable; + } + + /** Is this server data bearing */ + get isDataBearing(): boolean { + return DATA_BEARING_SERVER_TYPES.has(this.type); + } + + /** Is this server available for writes */ + get isWritable(): boolean { + return WRITABLE_SERVER_TYPES.has(this.type); + } + + get host(): string { + const chopLength = `:${this.port}`.length; + return this.address.slice(0, -chopLength); + } + + get port(): number { + const port = this.address.split(':').pop(); + return port ? Number.parseInt(port, 10) : 27017; + } + + /** + * Determines if another `ServerDescription` is equal to this one per the rules defined in the SDAM specification. + * @see https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md + */ + equals(other?: ServerDescription | null): boolean { + // Despite using the comparator that would determine a nullish topologyVersion as greater than + // for equality we should only always perform direct equality comparison + const topologyVersionsEqual = + this.topologyVersion === other?.topologyVersion || + compareTopologyVersion(this.topologyVersion, other?.topologyVersion) === 0; + + const electionIdsEqual = + this.electionId != null && other?.electionId != null + ? compareObjectId(this.electionId, other.electionId) === 0 + : this.electionId === other?.electionId; + + return ( + other != null && + other.iscryptd === this.iscryptd && + errorStrictEqual(this.error, other.error) && + this.type === other.type && + this.minWireVersion === other.minWireVersion && + arrayStrictEqual(this.hosts, other.hosts) && + tagsStrictEqual(this.tags, other.tags) && + this.setName === other.setName && + this.setVersion === other.setVersion && + electionIdsEqual && + this.primary === other.primary && + this.logicalSessionTimeoutMinutes === other.logicalSessionTimeoutMinutes && + topologyVersionsEqual + ); + } +} + +// Parses a `hello` message and determines the server type +export function parseServerType(hello?: Document, options?: ServerDescriptionOptions): ServerType { + if (options?.loadBalanced) { + return ServerType.LoadBalancer; + } + + if (!hello || !hello.ok) { + return ServerType.Unknown; + } + + if (hello.isreplicaset) { + return ServerType.RSGhost; + } + + if (hello.msg && hello.msg === 'isdbgrid') { + return ServerType.Mongos; + } + + if (hello.setName) { + if (hello.hidden) { + return ServerType.RSOther; + } else if (hello.isWritablePrimary) { + return ServerType.RSPrimary; + } else if (hello.secondary) { + return ServerType.RSSecondary; + } else if (hello.arbiterOnly) { + return ServerType.RSArbiter; + } else { + return ServerType.RSOther; + } + } + + return ServerType.Standalone; +} + +function tagsStrictEqual(tags: TagSet, tags2: TagSet): boolean { + const tagsKeys = Object.keys(tags); + const tags2Keys = Object.keys(tags2); + + return ( + tagsKeys.length === tags2Keys.length && + tagsKeys.every((key: string) => tags2[key] === tags[key]) + ); +} + +/** + * Compares two topology versions. + * + * 1. If the response topologyVersion is unset or the ServerDescription's + * topologyVersion is null, the client MUST assume the response is more recent. + * 1. If the response's topologyVersion.processId is not equal to the + * ServerDescription's, the client MUST assume the response is more recent. + * 1. If the response's topologyVersion.processId is equal to the + * ServerDescription's, the client MUST use the counter field to determine + * which topologyVersion is more recent. + * + * ```ts + * currentTv < newTv === -1 + * currentTv === newTv === 0 + * currentTv > newTv === 1 + * ``` + */ +export function compareTopologyVersion( + currentTv?: TopologyVersion | null, + newTv?: TopologyVersion | null +): 0 | -1 | 1 { + if (currentTv == null || newTv == null) { + return -1; + } + + if (!currentTv.processId.equals(newTv.processId)) { + return -1; + } + + // TODO(NODE-2674): Preserve int64 sent from MongoDB + const currentCounter = + typeof currentTv.counter === 'bigint' + ? Long.fromBigInt(currentTv.counter) + : Long.isLong(currentTv.counter) + ? currentTv.counter + : Long.fromNumber(currentTv.counter); + + const newCounter = + typeof newTv.counter === 'bigint' + ? Long.fromBigInt(newTv.counter) + : Long.isLong(newTv.counter) + ? newTv.counter + : Long.fromNumber(newTv.counter); + + return currentCounter.compare(newCounter); +} diff --git a/gateway/node_modules/mongodb/src/sdam/server_selection.ts b/gateway/node_modules/mongodb/src/sdam/server_selection.ts new file mode 100644 index 0000000000000000000000000000000000000000..8f01beaa948f61018fa89e4c898b2323383548cd --- /dev/null +++ b/gateway/node_modules/mongodb/src/sdam/server_selection.ts @@ -0,0 +1,415 @@ +import { MongoInvalidArgumentError, MongoRuntimeError } from '../error'; +import { ReadPreference } from '../read_preference'; +import { ServerType, TopologyType } from './common'; +import type { ServerDescription, TagSet } from './server_description'; +import type { TopologyDescription } from './topology_description'; + +// max staleness constants +const IDLE_WRITE_PERIOD = 10000; +const SMALLEST_MAX_STALENESS_SECONDS = 90; + +// Minimum version to try writes on secondaries. +export const MIN_SECONDARY_WRITE_WIRE_VERSION = 13; + +/** @internal */ +export type ServerSelector = ( + topologyDescription: TopologyDescription, + servers: ServerDescription[], + deprioritized: DeprioritizedServers +) => ServerDescription[]; + +/** @internal */ +export class DeprioritizedServers { + private deprioritized: Set = new Set(); + + constructor(descriptions?: Iterable) { + for (const description of descriptions ?? []) { + this.add(description); + } + } + + add({ address }: ServerDescription) { + this.deprioritized.add(address); + } + + has({ address }: ServerDescription): boolean { + return this.deprioritized.has(address); + } +} + +function filterDeprioritized( + candidates: ServerDescription[], + deprioritized: DeprioritizedServers +): ServerDescription[] { + const filtered = candidates.filter(candidate => !deprioritized.has(candidate)); + + return filtered.length ? filtered : candidates; +} + +/** + * Returns a server selector that selects for writable servers + */ +export function writableServerSelector(): ServerSelector { + return function writableServer( + topologyDescription: TopologyDescription, + servers: ServerDescription[], + deprioritized: DeprioritizedServers + ): ServerDescription[] { + const eligibleServers = filterDeprioritized( + servers.filter(({ isWritable }) => isWritable), + deprioritized + ); + + return latencyWindowReducer(topologyDescription, eligibleServers); + }; +} + +/** + * The purpose of this selector is to select the same server, only + * if it is in a state that it can have commands sent to it. + */ +export function sameServerSelector(description?: ServerDescription): ServerSelector { + return function sameServerSelector( + _topologyDescription: TopologyDescription, + servers: ServerDescription[], + _deprioritized: DeprioritizedServers + ): ServerDescription[] { + if (!description) return []; + // Filter the servers to match the provided description only if + // the type is not unknown. + return servers.filter(sd => { + return sd.address === description.address && sd.type !== ServerType.Unknown; + }); + }; +} + +/** + * Returns a server selector that uses a read preference to select a + * server potentially for a write on a secondary. + */ +export function secondaryWritableServerSelector( + wireVersion: number, + readPreference?: ReadPreference +): ServerSelector { + // If server version < 5.0, read preference always primary. + // If server version >= 5.0... + // - If read preference is supplied, use that. + // - If no read preference is supplied, use primary. + if ( + !readPreference || + !wireVersion || + (wireVersion && wireVersion < MIN_SECONDARY_WRITE_WIRE_VERSION) + ) { + return readPreferenceServerSelector(ReadPreference.primary); + } + return readPreferenceServerSelector(readPreference); +} + +/** + * Reduces the passed in array of servers by the rules of the "Max Staleness" specification + * found here: + * + * @see https://github.com/mongodb/specifications/blob/master/source/max-staleness/max-staleness.md + * + * @param readPreference - The read preference providing max staleness guidance + * @param topologyDescription - The topology description + * @param servers - The list of server descriptions to be reduced + * @returns The list of servers that satisfy the requirements of max staleness + */ +function maxStalenessReducer( + readPreference: ReadPreference, + topologyDescription: TopologyDescription, + servers: ServerDescription[] +): ServerDescription[] { + if (readPreference.maxStalenessSeconds == null || readPreference.maxStalenessSeconds < 0) { + return servers; + } + + const maxStaleness = readPreference.maxStalenessSeconds; + const maxStalenessVariance = + (topologyDescription.heartbeatFrequencyMS + IDLE_WRITE_PERIOD) / 1000; + if (maxStaleness < maxStalenessVariance) { + throw new MongoInvalidArgumentError( + `Option "maxStalenessSeconds" must be at least ${maxStalenessVariance} seconds` + ); + } + + if (maxStaleness < SMALLEST_MAX_STALENESS_SECONDS) { + throw new MongoInvalidArgumentError( + `Option "maxStalenessSeconds" must be at least ${SMALLEST_MAX_STALENESS_SECONDS} seconds` + ); + } + + if (topologyDescription.type === TopologyType.ReplicaSetWithPrimary) { + const primary: ServerDescription = Array.from(topologyDescription.servers.values()).filter( + primaryFilter + )[0]; + + return servers.filter((server: ServerDescription) => { + const stalenessMS = + server.lastUpdateTime - + server.lastWriteDate - + (primary.lastUpdateTime - primary.lastWriteDate) + + topologyDescription.heartbeatFrequencyMS; + + const staleness = stalenessMS / 1000; + const maxStalenessSeconds = readPreference.maxStalenessSeconds ?? 0; + return staleness <= maxStalenessSeconds; + }); + } + + if (topologyDescription.type === TopologyType.ReplicaSetNoPrimary) { + if (servers.length === 0) { + return servers; + } + + const sMax = servers.reduce((max: ServerDescription, s: ServerDescription) => + s.lastWriteDate > max.lastWriteDate ? s : max + ); + + return servers.filter((server: ServerDescription) => { + const stalenessMS = + sMax.lastWriteDate - server.lastWriteDate + topologyDescription.heartbeatFrequencyMS; + + const staleness = stalenessMS / 1000; + const maxStalenessSeconds = readPreference.maxStalenessSeconds ?? 0; + return staleness <= maxStalenessSeconds; + }); + } + + return servers; +} + +/** + * Determines whether a server's tags match a given set of tags. + * + * A tagset matches the server's tags if every k-v pair in the tagset + * is also in the server's tagset. + * + * Note that this does not requires that every k-v pair in the server's tagset is also + * in the client's tagset. The server's tagset is required only to be a superset of the + * client's tags. + * + * @see https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.md#tag_sets + * + * @param tagSet - The requested tag set to match + * @param serverTags - The server's tags + */ +function tagSetMatch(tagSet: TagSet, serverTags: TagSet) { + return Object.entries(tagSet).every( + ([key, value]) => serverTags[key] != null && serverTags[key] === value + ); +} + +/** + * Reduces a set of server descriptions based on tags requested by the read preference + * + * @param readPreference - The read preference providing the requested tags + * @param servers - The list of server descriptions to reduce + * @returns The list of servers matching the requested tags + */ +function tagSetReducer( + { tags }: ReadPreference, + servers: ServerDescription[] +): ServerDescription[] { + if (tags == null || tags.length === 0) { + // empty tag sets match all servers + return servers; + } + + for (const tagSet of tags) { + const serversMatchingTagset = servers.filter((s: ServerDescription) => + tagSetMatch(tagSet, s.tags) + ); + + if (serversMatchingTagset.length) { + return serversMatchingTagset; + } + } + + return []; +} + +/** + * Reduces a list of servers to ensure they fall within an acceptable latency window. This is + * further specified in the "Server Selection" specification, found here: + * + * @see https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.md + * + * @param topologyDescription - The topology description + * @param servers - The list of servers to reduce + * @returns The servers which fall within an acceptable latency window + */ +function latencyWindowReducer( + topologyDescription: TopologyDescription, + servers: ServerDescription[] +): ServerDescription[] { + const low = servers.reduce( + (min: number, server: ServerDescription) => Math.min(server.roundTripTime, min), + Infinity + ); + + const high = low + topologyDescription.localThresholdMS; + return servers.filter(server => server.roundTripTime <= high && server.roundTripTime >= low); +} + +// filters +function primaryFilter(server: ServerDescription): boolean { + return server.type === ServerType.RSPrimary; +} + +function secondaryFilter(server: ServerDescription): boolean { + return server.type === ServerType.RSSecondary; +} + +function nearestFilter(server: ServerDescription): boolean { + return server.type === ServerType.RSSecondary || server.type === ServerType.RSPrimary; +} + +function knownFilter(server: ServerDescription): boolean { + return server.type !== ServerType.Unknown; +} + +function loadBalancerFilter(server: ServerDescription): boolean { + return server.type === ServerType.LoadBalancer; +} + +function isDeprioritizedFactory( + deprioritized: DeprioritizedServers +): (server: ServerDescription) => boolean { + return server => + // if any deprioritized servers equal the server, here we are. + !deprioritized.has(server); +} + +function secondarySelector( + readPreference: ReadPreference, + topologyDescription: TopologyDescription, + servers: ServerDescription[], + deprioritized: DeprioritizedServers +) { + const mode = readPreference.mode; + switch (mode) { + case 'primary': + // Note: no need to filter for deprioritized servers. A replica set has only one primary; that means that + // we are in one of two scenarios: + // 1. deprioritized servers is empty - return the primary. + // 2. deprioritized servers contains the primary - return the primary. + return servers.filter(primaryFilter); + case 'primaryPreferred': { + const primary = servers.filter(primaryFilter); + + // If there is a primary and it is not deprioritized, use the primary. Otherwise, + // check for secondaries. + const eligiblePrimary = primary.filter(isDeprioritizedFactory(deprioritized)); + if (eligiblePrimary.length) { + return eligiblePrimary; + } + + // If we make it here, we either have: + // 1. a deprioritized primary + // 2. no eligible primary + // secondaries take precedence of deprioritized primaries. + const secondaries = tagSetReducer( + readPreference, + maxStalenessReducer(readPreference, topologyDescription, servers.filter(secondaryFilter)) + ); + + const eligibleSecondaries = secondaries.filter(isDeprioritizedFactory(deprioritized)); + if (eligibleSecondaries.length) { + return latencyWindowReducer(topologyDescription, eligibleSecondaries); + } + + // if we make it here, we have no primaries or secondaries that not deprioritized. + // prefer the primary (which may not exist, if the topology has no primary). + // otherwise, return the secondaries (which also may not exist, but there is nothing else to check here). + return primary.length ? primary : latencyWindowReducer(topologyDescription, secondaries); + } + case 'nearest': { + const eligible = filterDeprioritized( + tagSetReducer( + readPreference, + maxStalenessReducer(readPreference, topologyDescription, servers.filter(nearestFilter)) + ), + deprioritized + ); + return latencyWindowReducer(topologyDescription, eligible); + } + case 'secondary': + case 'secondaryPreferred': { + const secondaries = tagSetReducer( + readPreference, + maxStalenessReducer(readPreference, topologyDescription, servers.filter(secondaryFilter)) + ); + const eligibleSecondaries = secondaries.filter(isDeprioritizedFactory(deprioritized)); + + if (eligibleSecondaries.length) { + return latencyWindowReducer(topologyDescription, eligibleSecondaries); + } + + // we have no eligible secondaries, try for a primary if we can. + if (mode === ReadPreference.SECONDARY_PREFERRED) { + const primary = servers.filter(primaryFilter); + + // unlike readPreference=primary, here we do filter for deprioritized servers. + // if the primary is deprioritized, deprioritized secondaries take precedence. + const eligiblePrimary = primary.filter(isDeprioritizedFactory(deprioritized)); + if (eligiblePrimary.length) return eligiblePrimary; + + // we have no eligible primary nor secondaries that have not been deprioritized + return secondaries.length + ? latencyWindowReducer(topologyDescription, secondaries) + : primary; + } + + // return all secondaries in the latency window. + return latencyWindowReducer(topologyDescription, secondaries); + } + + default: { + const _exhaustiveCheck: never = mode; + throw new MongoRuntimeError( + `unexpected readPreference=${mode} (should never happen). Please report a bug in the Node driver Jira project.` + ); + } + } +} + +/** + * Returns a function which selects servers based on a provided read preference + * + * @param readPreference - The read preference to select with + */ +export function readPreferenceServerSelector(readPreference: ReadPreference): ServerSelector { + if (!readPreference.isValid()) { + throw new MongoInvalidArgumentError('Invalid read preference specified'); + } + + return function readPreferenceServers( + topologyDescription: TopologyDescription, + servers: ServerDescription[], + deprioritized: DeprioritizedServers + ): ServerDescription[] { + switch (topologyDescription.type) { + case 'Single': + return latencyWindowReducer(topologyDescription, servers.filter(knownFilter)); + case 'ReplicaSetNoPrimary': + case 'ReplicaSetWithPrimary': + return secondarySelector(readPreference, topologyDescription, servers, deprioritized); + case 'Sharded': { + const selectable = filterDeprioritized(servers, deprioritized); + return latencyWindowReducer(topologyDescription, selectable.filter(knownFilter)); + } + case 'Unknown': + return []; + case 'LoadBalanced': + return servers.filter(loadBalancerFilter); + default: { + const _exhaustiveCheck: never = topologyDescription.type; + throw new MongoRuntimeError( + `unexpected topology type: ${topologyDescription.type} (this should never happen). Please file a bug in the Node driver Jira project.` + ); + } + } + }; +} diff --git a/gateway/node_modules/mongodb/src/sdam/server_selection_events.ts b/gateway/node_modules/mongodb/src/sdam/server_selection_events.ts new file mode 100644 index 0000000000000000000000000000000000000000..9b171661792e9a7e276d19820a26c41348d0bfd4 --- /dev/null +++ b/gateway/node_modules/mongodb/src/sdam/server_selection_events.ts @@ -0,0 +1,142 @@ +import { HostAddress } from '.././utils'; +import { + SERVER_SELECTION_FAILED, + SERVER_SELECTION_STARTED, + SERVER_SELECTION_SUCCEEDED, + WAITING_FOR_SUITABLE_SERVER +} from '../constants'; +import { type ReadPreference } from '../read_preference'; +import { type ServerSelector } from './server_selection'; +import type { TopologyDescription } from './topology_description'; + +/** + * The base export class for all logs published from server selection + * @internal + * @category Log Type + */ +export abstract class ServerSelectionEvent { + /** String representation of the selector being used to select the server. + * Defaults to 'custom selector' for application-provided custom selector case. + */ + selector: string | ReadPreference | ServerSelector; + /** The name of the operation for which a server is being selected. */ + operation: string; + /** The current topology description. */ + topologyDescription: TopologyDescription; + + /** @internal */ + abstract name: + | typeof SERVER_SELECTION_STARTED + | typeof SERVER_SELECTION_SUCCEEDED + | typeof SERVER_SELECTION_FAILED + | typeof WAITING_FOR_SUITABLE_SERVER; + + abstract message: string; + + /** @internal */ + constructor( + selector: string | ReadPreference | ServerSelector, + topologyDescription: TopologyDescription, + operation: string + ) { + this.selector = selector; + this.operation = operation; + this.topologyDescription = topologyDescription; + } +} + +/** + * An event published when server selection starts + * @internal + * @category Event + */ +export class ServerSelectionStartedEvent extends ServerSelectionEvent { + /** @internal */ + name = SERVER_SELECTION_STARTED; + message = 'Server selection started'; + + /** @internal */ + constructor( + selector: string | ReadPreference | ServerSelector, + topologyDescription: TopologyDescription, + operation: string + ) { + super(selector, topologyDescription, operation); + } +} + +/** + * An event published when a server selection fails + * @internal + * @category Event + */ +export class ServerSelectionFailedEvent extends ServerSelectionEvent { + /** @internal */ + name = SERVER_SELECTION_FAILED; + message = 'Server selection failed'; + /** Representation of the error the driver will throw regarding server selection failing. */ + failure: Error; + + /** @internal */ + constructor( + selector: string | ReadPreference | ServerSelector, + topologyDescription: TopologyDescription, + error: Error, + operation: string + ) { + super(selector, topologyDescription, operation); + this.failure = error; + } +} + +/** + * An event published when server selection succeeds + * @internal + * @category Event + */ +export class ServerSelectionSucceededEvent extends ServerSelectionEvent { + /** @internal */ + name = SERVER_SELECTION_SUCCEEDED; + message = 'Server selection succeeded'; + /** The hostname, IP address, or Unix domain socket path for the selected server. */ + serverHost: string; + /** The port for the selected server. Optional; not present for Unix domain sockets. When the user does not specify a port and the default (27017) is used, the driver SHOULD include it here. */ + serverPort: number | undefined; + + /** @internal */ + constructor( + selector: string | ReadPreference | ServerSelector, + topologyDescription: TopologyDescription, + address: string, + operation: string + ) { + super(selector, topologyDescription, operation); + const { host, port } = HostAddress.fromString(address).toHostPort(); + this.serverHost = host; + this.serverPort = port; + } +} + +/** + * An event published when server selection is waiting for a suitable server to become available + * @internal + * @category Event + */ +export class WaitingForSuitableServerEvent extends ServerSelectionEvent { + /** @internal */ + name = WAITING_FOR_SUITABLE_SERVER; + message = 'Waiting for suitable server to become available'; + /** The remaining time left until server selection will time out. */ + remainingTimeMS: number; + + /** @internal */ + constructor( + selector: string | ReadPreference | ServerSelector, + topologyDescription: TopologyDescription, + remainingTimeMS: number, + operation: string + ) { + super(selector, topologyDescription, operation); + this.remainingTimeMS = remainingTimeMS; + } +} diff --git a/gateway/node_modules/mongodb/src/sdam/srv_polling.ts b/gateway/node_modules/mongodb/src/sdam/srv_polling.ts new file mode 100644 index 0000000000000000000000000000000000000000..cfc4779cf256c7579c3e250b70a9f75318d26746 --- /dev/null +++ b/gateway/node_modules/mongodb/src/sdam/srv_polling.ts @@ -0,0 +1,146 @@ +import * as dns from 'dns'; +import { clearTimeout, setTimeout } from 'timers'; + +import { MongoRuntimeError } from '../error'; +import { TypedEventEmitter } from '../mongo_types'; +import { checkParentDomainMatch, HostAddress, noop, squashError } from '../utils'; + +/** + * @internal + * @category Event + */ +export class SrvPollingEvent { + srvRecords: dns.SrvRecord[]; + constructor(srvRecords: dns.SrvRecord[]) { + this.srvRecords = srvRecords; + } + + hostnames(): Set { + return new Set(this.srvRecords.map(r => HostAddress.fromSrvRecord(r).toString())); + } +} + +/** @internal */ +export interface SrvPollerOptions { + srvServiceName: string; + srvMaxHosts: number; + srvHost: string; + heartbeatFrequencyMS: number; +} + +/** @internal */ +export type SrvPollerEvents = { + srvRecordDiscovery(event: SrvPollingEvent): void; +}; + +/** @internal */ +export class SrvPoller extends TypedEventEmitter { + srvHost: string; + rescanSrvIntervalMS: number; + heartbeatFrequencyMS: number; + haMode: boolean; + generation: number; + srvMaxHosts: number; + srvServiceName: string; + _timeout?: NodeJS.Timeout; + + /** @event */ + static readonly SRV_RECORD_DISCOVERY = 'srvRecordDiscovery' as const; + + constructor(options: SrvPollerOptions) { + super(); + this.on('error', noop); + + if (!options || !options.srvHost) { + throw new MongoRuntimeError('Options for SrvPoller must exist and include srvHost'); + } + + this.srvHost = options.srvHost; + this.srvMaxHosts = options.srvMaxHosts ?? 0; + this.srvServiceName = options.srvServiceName ?? 'mongodb'; + this.rescanSrvIntervalMS = 60000; + this.heartbeatFrequencyMS = options.heartbeatFrequencyMS ?? 10000; + + this.haMode = false; + this.generation = 0; + + this._timeout = undefined; + } + + get srvAddress(): string { + return `_${this.srvServiceName}._tcp.${this.srvHost}`; + } + + get intervalMS(): number { + return this.haMode ? this.heartbeatFrequencyMS : this.rescanSrvIntervalMS; + } + + start(): void { + if (!this._timeout) { + this.schedule(); + } + } + + stop(): void { + if (this._timeout) { + clearTimeout(this._timeout); + this.generation += 1; + this._timeout = undefined; + } + } + + // TODO(NODE-4994): implement new logging logic for SrvPoller failures + schedule(): void { + if (this._timeout) { + clearTimeout(this._timeout); + } + + this._timeout = setTimeout(() => { + this._poll().then(undefined, squashError); + }, this.intervalMS); + } + + success(srvRecords: dns.SrvRecord[]): void { + this.haMode = false; + this.schedule(); + this.emit(SrvPoller.SRV_RECORD_DISCOVERY, new SrvPollingEvent(srvRecords)); + } + + failure(): void { + this.haMode = true; + this.schedule(); + } + + async _poll(): Promise { + const generation = this.generation; + let srvRecords; + + try { + srvRecords = await dns.promises.resolve(this.srvAddress, 'SRV'); + } catch { + this.failure(); + return; + } + + if (generation !== this.generation) { + return; + } + + const finalAddresses: dns.SrvRecord[] = []; + for (const record of srvRecords) { + try { + checkParentDomainMatch(record.name, this.srvHost); + finalAddresses.push(record); + } catch (error) { + squashError(error); + } + } + + if (!finalAddresses.length) { + this.failure(); + return; + } + + this.success(finalAddresses); + } +} diff --git a/gateway/node_modules/mongodb/src/sdam/topology.ts b/gateway/node_modules/mongodb/src/sdam/topology.ts new file mode 100644 index 0000000000000000000000000000000000000000..36228f75947aeb2deaba4d5170dd7bedca551897 --- /dev/null +++ b/gateway/node_modules/mongodb/src/sdam/topology.ts @@ -0,0 +1,1103 @@ +import type { BSONSerializeOptions, Document } from '../bson'; +import type { MongoCredentials } from '../cmap/auth/mongo_credentials'; +import type { ConnectionEvents } from '../cmap/connection'; +import type { ConnectionPoolEvents } from '../cmap/connection_pool'; +import type { ClientMetadata } from '../cmap/handshake/client_metadata'; +import { DEFAULT_OPTIONS } from '../connection_string'; +import { + CLOSE, + CONNECT, + ERROR, + LOCAL_SERVER_EVENTS, + OPEN, + SERVER_CLOSED, + SERVER_DESCRIPTION_CHANGED, + SERVER_OPENING, + SERVER_RELAY_EVENTS, + TIMEOUT, + TOPOLOGY_CLOSED, + TOPOLOGY_DESCRIPTION_CHANGED, + TOPOLOGY_OPENING +} from '../constants'; +import { + MongoCompatibilityError, + type MongoDriverError, + MongoError, + MongoErrorLabel, + MongoOperationTimeoutError, + MongoRuntimeError, + MongoServerSelectionError, + MongoTopologyClosedError +} from '../error'; +import type { MongoClient, ServerApi } from '../mongo_client'; +import { MongoLoggableComponent, type MongoLogger, SeverityLevel } from '../mongo_logger'; +import { type Abortable, TypedEventEmitter } from '../mongo_types'; +import { ReadPreference, type ReadPreferenceLike } from '../read_preference'; +import type { ClientSession } from '../sessions'; +import { Timeout, TimeoutContext, TimeoutError } from '../timeout'; +import type { Transaction } from '../transactions'; +import { + addAbortListener, + type Callback, + type EventEmitterWithState, + HostAddress, + kDispose, + List, + makeStateMachine, + noop, + processTimeMS, + promiseWithResolvers, + shuffle +} from '../utils'; +import { + _advanceClusterTime, + type ClusterTime, + ServerType, + STATE_CLOSED, + STATE_CLOSING, + STATE_CONNECTED, + STATE_CONNECTING, + TopologyType +} from './common'; +import { + ServerClosedEvent, + ServerDescriptionChangedEvent, + ServerOpeningEvent, + TopologyClosedEvent, + TopologyDescriptionChangedEvent, + TopologyOpeningEvent +} from './events'; +import type { ServerMonitoringMode } from './monitor'; +import { Server, type ServerEvents, type ServerOptions } from './server'; +import { compareTopologyVersion, ServerDescription } from './server_description'; +import { + DeprioritizedServers, + readPreferenceServerSelector, + type ServerSelector +} from './server_selection'; +import { + ServerSelectionFailedEvent, + ServerSelectionStartedEvent, + ServerSelectionSucceededEvent, + WaitingForSuitableServerEvent +} from './server_selection_events'; +import { SrvPoller, type SrvPollingEvent } from './srv_polling'; +import { TopologyDescription } from './topology_description'; + +// Global state +let globalTopologyCounter = 0; + +const stateTransition = makeStateMachine({ + [STATE_CLOSED]: [STATE_CLOSED, STATE_CONNECTING], + [STATE_CONNECTING]: [STATE_CONNECTING, STATE_CLOSING, STATE_CONNECTED, STATE_CLOSED], + [STATE_CONNECTED]: [STATE_CONNECTED, STATE_CLOSING, STATE_CLOSED], + [STATE_CLOSING]: [STATE_CLOSING, STATE_CLOSED] +}); + +/** @internal */ +export type ServerSelectionCallback = Callback; + +/** @internal */ +export interface ServerSelectionRequest { + serverSelector: ServerSelector; + topologyDescription: TopologyDescription; + mongoLogger: MongoLogger | undefined; + transaction?: Transaction; + startTime: number; + resolve: (server: Server) => void; + reject: (error: MongoError) => void; + cancelled: boolean; + operationName: string; + waitingLogged: boolean; + deprioritizedServers: DeprioritizedServers; +} + +/** @internal */ +export interface TopologyPrivate { + /** the id of this topology */ + id: number; + /** passed in options */ + options: TopologyOptions; + /** initial seedlist of servers to connect to */ + seedlist: HostAddress[]; + /** initial state */ + state: string; + /** the topology description */ + description: TopologyDescription; + serverSelectionTimeoutMS: number; + heartbeatFrequencyMS: number; + minHeartbeatFrequencyMS: number; + /** A map of server instances to normalized addresses */ + servers: Map; + credentials?: MongoCredentials; + clusterTime?: ClusterTime; + + /** related to srv polling */ + srvPoller?: SrvPoller; + detectShardedTopology: (event: TopologyDescriptionChangedEvent) => void; + detectSrvRecords: (event: SrvPollingEvent) => void; +} + +/** @internal */ +export interface TopologyOptions extends BSONSerializeOptions, ServerOptions { + srvMaxHosts: number; + srvServiceName: string; + hosts: HostAddress[]; + retryWrites: boolean; + retryReads: boolean; + maxAdaptiveRetries: number; + enableOverloadRetargeting: boolean; + /** How long to block for server selection before throwing an error */ + serverSelectionTimeoutMS: number; + /** The name of the replica set to connect to */ + replicaSet?: string; + srvHost?: string; + srvPoller?: SrvPoller; + /** Indicates that a client should directly connect to a node without attempting to discover its topology type */ + directConnection: boolean; + loadBalanced: boolean; + metadata: Promise; + serverMonitoringMode: ServerMonitoringMode; + /** MongoDB server API version */ + serverApi?: ServerApi; + __skipPingOnConnect?: boolean; +} + +/** @public */ +export interface ConnectOptions { + readPreference?: ReadPreference; +} + +/** @public */ +export interface SelectServerOptions { + readPreference?: ReadPreferenceLike; + /** How long to block for server selection before throwing an error */ + serverSelectionTimeoutMS?: number; + session?: ClientSession; + operationName: string; + + /** @internal */ + deprioritizedServers: DeprioritizedServers; + /** + * @internal + * TODO(NODE-6496): Make this required by making ChangeStream use LegacyTimeoutContext + * */ + timeoutContext?: TimeoutContext; +} + +/** @public */ +export type TopologyEvents = { + /** Top level MongoClient doesn't emit this so it is marked: @internal */ + connect(topology: Topology): void; + serverOpening(event: ServerOpeningEvent): void; + serverClosed(event: ServerClosedEvent): void; + serverDescriptionChanged(event: ServerDescriptionChangedEvent): void; + topologyClosed(event: TopologyClosedEvent): void; + topologyOpening(event: TopologyOpeningEvent): void; + topologyDescriptionChanged(event: TopologyDescriptionChangedEvent): void; + error(error: Error): void; + /** @internal */ + open(topology: Topology): void; + close(): void; + timeout(): void; +} & Omit & + ConnectionPoolEvents & + ConnectionEvents & + EventEmitterWithState; +/** + * A container of server instances representing a connection to a MongoDB topology. + * @internal + */ +export class Topology extends TypedEventEmitter { + s: TopologyPrivate; + waitQueue: List; + hello?: Document; + _type?: string; + + client!: MongoClient; + + private connectionLock?: Promise; + + /** @event */ + static readonly SERVER_OPENING = SERVER_OPENING; + /** @event */ + static readonly SERVER_CLOSED = SERVER_CLOSED; + /** @event */ + static readonly SERVER_DESCRIPTION_CHANGED = SERVER_DESCRIPTION_CHANGED; + /** @event */ + static readonly TOPOLOGY_OPENING = TOPOLOGY_OPENING; + /** @event */ + static readonly TOPOLOGY_CLOSED = TOPOLOGY_CLOSED; + /** @event */ + static readonly TOPOLOGY_DESCRIPTION_CHANGED = TOPOLOGY_DESCRIPTION_CHANGED; + /** @event */ + static readonly ERROR = ERROR; + /** @event */ + static readonly OPEN = OPEN; + /** @event */ + static readonly CONNECT = CONNECT; + /** @event */ + static readonly CLOSE = CLOSE; + /** @event */ + static readonly TIMEOUT = TIMEOUT; + + /** + * @param seedlist - a list of HostAddress instances to connect to + */ + constructor( + client: MongoClient, + seeds: string | string[] | HostAddress | HostAddress[], + options: TopologyOptions + ) { + super(); + this.on('error', noop); + + this.client = client; + // Options should only be undefined in tests, MongoClient will always have defined options + options = options ?? { + hosts: [HostAddress.fromString('localhost:27017')], + ...Object.fromEntries(DEFAULT_OPTIONS.entries()) + }; + + if (typeof seeds === 'string') { + seeds = [HostAddress.fromString(seeds)]; + } else if (!Array.isArray(seeds)) { + seeds = [seeds]; + } + + const seedlist: HostAddress[] = []; + for (const seed of seeds) { + if (typeof seed === 'string') { + seedlist.push(HostAddress.fromString(seed)); + } else if (seed instanceof HostAddress) { + seedlist.push(seed); + } else { + // FIXME(NODE-3483): May need to be a MongoParseError + throw new MongoRuntimeError(`Topology cannot be constructed from ${JSON.stringify(seed)}`); + } + } + + const topologyType = topologyTypeFromOptions(options); + const topologyId = globalTopologyCounter++; + + const selectedHosts = + options.srvMaxHosts == null || + options.srvMaxHosts === 0 || + options.srvMaxHosts >= seedlist.length + ? seedlist + : shuffle(seedlist, options.srvMaxHosts); + + const serverDescriptions = new Map(); + for (const hostAddress of selectedHosts) { + serverDescriptions.set(hostAddress.toString(), new ServerDescription(hostAddress)); + } + + this.waitQueue = new List(); + this.s = { + // the id of this topology + id: topologyId, + // passed in options + options, + // initial seedlist of servers to connect to + seedlist, + // initial state + state: STATE_CLOSED, + // the topology description + description: new TopologyDescription( + topologyType, + serverDescriptions, + options.replicaSet, + undefined, + undefined, + undefined, + options + ), + serverSelectionTimeoutMS: options.serverSelectionTimeoutMS, + heartbeatFrequencyMS: options.heartbeatFrequencyMS, + minHeartbeatFrequencyMS: options.minHeartbeatFrequencyMS, + // a map of server instances to normalized addresses + servers: new Map(), + credentials: options?.credentials, + clusterTime: undefined, + + detectShardedTopology: ev => this.detectShardedTopology(ev), + detectSrvRecords: ev => this.detectSrvRecords(ev) + }; + + this.mongoLogger = client.mongoLogger; + this.component = 'topology'; + + if (options.srvHost && !options.loadBalanced) { + this.s.srvPoller = + options.srvPoller ?? + new SrvPoller({ + heartbeatFrequencyMS: this.s.heartbeatFrequencyMS, + srvHost: options.srvHost, + srvMaxHosts: options.srvMaxHosts, + srvServiceName: options.srvServiceName + }); + + this.on(Topology.TOPOLOGY_DESCRIPTION_CHANGED, this.s.detectShardedTopology); + } + this.connectionLock = undefined; + } + + private detectShardedTopology(event: TopologyDescriptionChangedEvent) { + const previousType = event.previousDescription.type; + const newType = event.newDescription.type; + + const transitionToSharded = + previousType !== TopologyType.Sharded && newType === TopologyType.Sharded; + const srvListeners = this.s.srvPoller?.listeners(SrvPoller.SRV_RECORD_DISCOVERY); + const listeningToSrvPolling = !!srvListeners?.includes(this.s.detectSrvRecords); + + if (transitionToSharded && !listeningToSrvPolling) { + this.s.srvPoller?.on(SrvPoller.SRV_RECORD_DISCOVERY, this.s.detectSrvRecords); + this.s.srvPoller?.start(); + } + } + + private detectSrvRecords(ev: SrvPollingEvent) { + const previousTopologyDescription = this.s.description; + this.s.description = this.s.description.updateFromSrvPollingEvent( + ev, + this.s.options.srvMaxHosts + ); + if (this.s.description === previousTopologyDescription) { + // Nothing changed, so return + return; + } + + updateServers(this); + + this.emitAndLog( + Topology.TOPOLOGY_DESCRIPTION_CHANGED, + new TopologyDescriptionChangedEvent( + this.s.id, + previousTopologyDescription, + this.s.description + ) + ); + } + + /** + * @returns A `TopologyDescription` for this topology + */ + get description(): TopologyDescription { + return this.s.description; + } + + get loadBalanced(): boolean { + return this.s.options.loadBalanced; + } + + get serverApi(): ServerApi | undefined { + return this.s.options.serverApi; + } + + /** Initiate server connect */ + async connect(options?: ConnectOptions): Promise { + this.connectionLock ??= this._connect(options); + try { + await this.connectionLock; + return this; + } finally { + this.connectionLock = undefined; + } + } + + private async _connect(options?: ConnectOptions): Promise { + options = options ?? {}; + if (this.s.state === STATE_CONNECTED) { + return this; + } + + stateTransition(this, STATE_CONNECTING); + + // emit SDAM monitoring events + this.emitAndLog(Topology.TOPOLOGY_OPENING, new TopologyOpeningEvent(this.s.id)); + + // emit an event for the topology change + this.emitAndLog( + Topology.TOPOLOGY_DESCRIPTION_CHANGED, + new TopologyDescriptionChangedEvent( + this.s.id, + new TopologyDescription(TopologyType.Unknown), // initial is always Unknown + this.s.description + ) + ); + + // connect all known servers, then attempt server selection to connect + const serverDescriptions = Array.from(this.s.description.servers.values()); + this.s.servers = new Map( + serverDescriptions.map(serverDescription => [ + serverDescription.address, + createAndConnectServer(this, serverDescription) + ]) + ); + + // In load balancer mode we need to fake a server description getting + // emitted from the monitor, since the monitor doesn't exist. + if (this.s.options.loadBalanced) { + for (const description of serverDescriptions) { + const newDescription = new ServerDescription(description.hostAddress, undefined, { + loadBalanced: this.s.options.loadBalanced + }); + this.serverUpdateHandler(newDescription); + } + } + + const serverSelectionTimeoutMS = this.client.s.options.serverSelectionTimeoutMS; + const readPreference = options.readPreference ?? ReadPreference.primary; + const timeoutContext = TimeoutContext.create({ + // TODO(NODE-6448): auto-connect ignores timeoutMS; potential future feature + timeoutMS: undefined, + serverSelectionTimeoutMS, + waitQueueTimeoutMS: this.client.s.options.waitQueueTimeoutMS + }); + const selectServerOptions = { + operationName: 'handshake', + ...options, + timeoutContext, + deprioritizedServers: new DeprioritizedServers() + }; + + try { + const server = await this.selectServer( + readPreferenceServerSelector(readPreference), + selectServerOptions + ); + + const skipPingOnConnect = this.s.options.__skipPingOnConnect === true; + if (!skipPingOnConnect) { + const connection = await server.pool.checkOut({ timeoutContext: timeoutContext }); + server.pool.checkIn(connection); + stateTransition(this, STATE_CONNECTED); + this.emit(Topology.OPEN, this); + this.emit(Topology.CONNECT, this); + + return this; + } + + stateTransition(this, STATE_CONNECTED); + this.emit(Topology.OPEN, this); + this.emit(Topology.CONNECT, this); + + return this; + } catch (error) { + this.close(); + throw error; + } + } + + closeCheckedOutConnections() { + for (const server of this.s.servers.values()) { + server.closeCheckedOutConnections(); + } + } + + /** Close this topology */ + close(): void { + if (this.s.state === STATE_CLOSED || this.s.state === STATE_CLOSING) { + return; + } + + for (const server of this.s.servers.values()) { + closeServer(server, this); + } + + this.s.servers.clear(); + + stateTransition(this, STATE_CLOSING); + + drainWaitQueue(this.waitQueue, new MongoTopologyClosedError()); + + if (this.s.srvPoller) { + this.s.srvPoller.stop(); + this.s.srvPoller.removeListener(SrvPoller.SRV_RECORD_DISCOVERY, this.s.detectSrvRecords); + } + + this.removeListener(Topology.TOPOLOGY_DESCRIPTION_CHANGED, this.s.detectShardedTopology); + + stateTransition(this, STATE_CLOSED); + + // emit an event for close + this.emitAndLog(Topology.TOPOLOGY_CLOSED, new TopologyClosedEvent(this.s.id)); + } + + /** + * Selects a server according to the selection predicate provided + * + * @param selector - An optional selector to select servers by, defaults to a random selection within a latency window + * @param options - Optional settings related to server selection + * @param callback - The callback used to indicate success or failure + * @returns An instance of a `Server` meeting the criteria of the predicate provided + */ + async selectServer( + selector: string | ReadPreference | ServerSelector, + options: SelectServerOptions & Abortable + ): Promise { + let serverSelector; + if (typeof selector !== 'function') { + if (typeof selector === 'string') { + serverSelector = readPreferenceServerSelector(ReadPreference.fromString(selector)); + } else { + let readPreference; + if (selector instanceof ReadPreference) { + readPreference = selector; + } else { + ReadPreference.translate(options); + readPreference = options.readPreference || ReadPreference.primary; + } + + serverSelector = readPreferenceServerSelector(readPreference as ReadPreference); + } + } else { + serverSelector = selector; + } + + options = { serverSelectionTimeoutMS: this.s.serverSelectionTimeoutMS, ...options }; + if ( + this.client.mongoLogger?.willLog(MongoLoggableComponent.SERVER_SELECTION, SeverityLevel.DEBUG) + ) { + this.client.mongoLogger?.debug( + MongoLoggableComponent.SERVER_SELECTION, + new ServerSelectionStartedEvent(selector, this.description, options.operationName) + ); + } + let timeout; + if (options.timeoutContext) timeout = options.timeoutContext.serverSelectionTimeout; + else { + timeout = Timeout.expires(options.serverSelectionTimeoutMS ?? 0); + } + + const isSharded = this.description.type === TopologyType.Sharded; + const session = options.session; + const transaction = session && session.transaction; + + if (isSharded && transaction && transaction.server) { + if ( + this.client.mongoLogger?.willLog( + MongoLoggableComponent.SERVER_SELECTION, + SeverityLevel.DEBUG + ) + ) { + this.client.mongoLogger?.debug( + MongoLoggableComponent.SERVER_SELECTION, + new ServerSelectionSucceededEvent( + selector, + this.description, + transaction.server.pool.address, + options.operationName + ) + ); + } + + if (!options.timeoutContext || options.timeoutContext.clearServerSelectionTimeout) { + timeout?.clear(); + } + + return transaction.server; + } + + const { promise: serverPromise, resolve, reject } = promiseWithResolvers(); + + const waitQueueMember: ServerSelectionRequest = { + serverSelector, + topologyDescription: this.description, + mongoLogger: this.client.mongoLogger, + transaction, + resolve, + reject, + cancelled: false, + startTime: processTimeMS(), + operationName: options.operationName, + waitingLogged: false, + deprioritizedServers: options.deprioritizedServers + }; + + const abortListener = addAbortListener(options.signal, function () { + waitQueueMember.cancelled = true; + reject(this.reason); + }); + + this.waitQueue.push(waitQueueMember); + processWaitQueue(this); + + try { + timeout?.throwIfExpired(); + const server = await (timeout ? Promise.race([serverPromise, timeout]) : serverPromise); + if (options.timeoutContext?.csotEnabled() && server.description.minRoundTripTime !== 0) { + options.timeoutContext.minRoundTripTime = server.description.minRoundTripTime; + } + return server; + } catch (error) { + if (TimeoutError.is(error)) { + // Timeout + waitQueueMember.cancelled = true; + const timeoutError = new MongoServerSelectionError( + `Server selection timed out after ${timeout?.duration} ms`, + this.description + ); + if ( + this.client.mongoLogger?.willLog( + MongoLoggableComponent.SERVER_SELECTION, + SeverityLevel.DEBUG + ) + ) { + this.client.mongoLogger?.debug( + MongoLoggableComponent.SERVER_SELECTION, + new ServerSelectionFailedEvent( + selector, + this.description, + timeoutError, + options.operationName + ) + ); + } + + if (options.timeoutContext?.csotEnabled()) { + throw new MongoOperationTimeoutError('Timed out during server selection', { + cause: timeoutError + }); + } + throw timeoutError; + } + // Other server selection error + throw error; + } finally { + abortListener?.[kDispose](); + if (!options.timeoutContext || options.timeoutContext.clearServerSelectionTimeout) { + timeout?.clear(); + } + } + } + /** + * Update the internal TopologyDescription with a ServerDescription + * + * @param serverDescription - The server to update in the internal list of server descriptions + */ + serverUpdateHandler(serverDescription: ServerDescription): void { + if (!this.s.description.hasServer(serverDescription.address)) { + return; + } + + // ignore this server update if its from an outdated topologyVersion + if (isStaleServerDescription(this.s.description, serverDescription)) { + return; + } + + // these will be used for monitoring events later + const previousTopologyDescription = this.s.description; + const previousServerDescription = this.s.description.servers.get(serverDescription.address); + if (!previousServerDescription) { + return; + } + + // Driver Sessions Spec: "Whenever a driver receives a cluster time from + // a server it MUST compare it to the current highest seen cluster time + // for the deployment. If the new cluster time is higher than the + // highest seen cluster time it MUST become the new highest seen cluster + // time. Two cluster times are compared using only the BsonTimestamp + // value of the clusterTime embedded field." + const clusterTime = serverDescription.$clusterTime; + if (clusterTime) { + _advanceClusterTime(this, clusterTime); + } + + // If we already know all the information contained in this updated description, then + // we don't need to emit SDAM events, but still need to update the description, in order + // to keep client-tracked attributes like last update time and round trip time up to date + const equalDescriptions = + previousServerDescription && previousServerDescription.equals(serverDescription); + + // first update the TopologyDescription + this.s.description = this.s.description.update(serverDescription); + if (this.s.description.compatibilityError) { + this.emit(Topology.ERROR, new MongoCompatibilityError(this.s.description.compatibilityError)); + return; + } + + // emit monitoring events for this change + if (!equalDescriptions) { + const newDescription = this.s.description.servers.get(serverDescription.address); + if (newDescription) { + this.emit( + Topology.SERVER_DESCRIPTION_CHANGED, + new ServerDescriptionChangedEvent( + this.s.id, + serverDescription.address, + previousServerDescription, + newDescription + ) + ); + } + } + + // update server list from updated descriptions + updateServers(this, serverDescription); + + // attempt to resolve any outstanding server selection attempts + if (this.waitQueue.length > 0) { + processWaitQueue(this); + } + + if (!equalDescriptions) { + this.emitAndLog( + Topology.TOPOLOGY_DESCRIPTION_CHANGED, + new TopologyDescriptionChangedEvent( + this.s.id, + previousTopologyDescription, + this.s.description + ) + ); + } + } + + auth(credentials?: MongoCredentials, callback?: Callback): void { + if (typeof credentials === 'function') ((callback = credentials), (credentials = undefined)); + if (typeof callback === 'function') callback(undefined, true); + } + + isConnected(): boolean { + return this.s.state === STATE_CONNECTED; + } + + isDestroyed(): boolean { + return this.s.state === STATE_CLOSED; + } + + // NOTE: There are many places in code where we explicitly check the last hello + // to do feature support detection. This should be done any other way, but for + // now we will just return the first hello seen, which should suffice. + lastHello(): Document { + const serverDescriptions = Array.from(this.description.servers.values()); + if (serverDescriptions.length === 0) return {}; + const sd = serverDescriptions.filter( + (sd: ServerDescription) => sd.type !== ServerType.Unknown + )[0]; + + const result = sd || { maxWireVersion: this.description.commonWireVersion }; + return result; + } + + get commonWireVersion(): number { + return this.description.commonWireVersion; + } + + get logicalSessionTimeoutMinutes(): number | null { + return this.description.logicalSessionTimeoutMinutes; + } + + get clusterTime(): ClusterTime | undefined { + return this.s.clusterTime; + } + + set clusterTime(clusterTime: ClusterTime | undefined) { + this.s.clusterTime = clusterTime; + } +} + +/** Destroys a server, and removes all event listeners from the instance */ +function closeServer(server: Server, topology: Topology) { + for (const event of LOCAL_SERVER_EVENTS) { + server.removeAllListeners(event); + } + + server.close(); + topology.emitAndLog( + Topology.SERVER_CLOSED, + new ServerClosedEvent(topology.s.id, server.description.address) + ); + + for (const event of SERVER_RELAY_EVENTS) { + server.removeAllListeners(event); + } +} + +/** Predicts the TopologyType from options */ +function topologyTypeFromOptions(options?: TopologyOptions) { + if (options?.directConnection) { + return TopologyType.Single; + } + + if (options?.replicaSet) { + return TopologyType.ReplicaSetNoPrimary; + } + + if (options?.loadBalanced) { + return TopologyType.LoadBalanced; + } + + return TopologyType.Unknown; +} + +/** + * Creates new server instances and attempts to connect them + * + * @param topology - The topology that this server belongs to + * @param serverDescription - The description for the server to initialize and connect to + */ +function createAndConnectServer(topology: Topology, serverDescription: ServerDescription) { + topology.emitAndLog( + Topology.SERVER_OPENING, + new ServerOpeningEvent(topology.s.id, serverDescription.address) + ); + + const server = new Server(topology, serverDescription, topology.s.options); + for (const event of SERVER_RELAY_EVENTS) { + server.on(event, (e: any) => topology.emit(event, e)); + } + + server.on(Server.DESCRIPTION_RECEIVED, description => topology.serverUpdateHandler(description)); + + server.connect(); + return server; +} + +/** + * @param topology - Topology to update. + * @param incomingServerDescription - New server description. + */ +function updateServers(topology: Topology, incomingServerDescription?: ServerDescription) { + // update the internal server's description + if (incomingServerDescription && topology.s.servers.has(incomingServerDescription.address)) { + const server = topology.s.servers.get(incomingServerDescription.address); + if (server) { + server.s.description = incomingServerDescription; + if ( + incomingServerDescription.error instanceof MongoError && + incomingServerDescription.error.hasErrorLabel(MongoErrorLabel.ResetPool) + ) { + const interruptInUseConnections = incomingServerDescription.error.hasErrorLabel( + MongoErrorLabel.InterruptInUseConnections + ); + + server.pool.clear({ interruptInUseConnections }); + } else if (incomingServerDescription.error == null) { + const newTopologyType = topology.s.description.type; + const shouldMarkPoolReady = + incomingServerDescription.isDataBearing || + (incomingServerDescription.type !== ServerType.Unknown && + newTopologyType === TopologyType.Single); + if (shouldMarkPoolReady) { + server.pool.ready(); + } + } + } + } + + // add new servers for all descriptions we currently don't know about locally + for (const serverDescription of topology.description.servers.values()) { + if (!topology.s.servers.has(serverDescription.address)) { + const server = createAndConnectServer(topology, serverDescription); + topology.s.servers.set(serverDescription.address, server); + } + } + + // for all servers no longer known, remove their descriptions and destroy their instances + for (const entry of topology.s.servers) { + const serverAddress = entry[0]; + if (topology.description.hasServer(serverAddress)) { + continue; + } + + if (!topology.s.servers.has(serverAddress)) { + continue; + } + + const server = topology.s.servers.get(serverAddress); + topology.s.servers.delete(serverAddress); + + // prepare server for garbage collection + if (server) { + closeServer(server, topology); + } + } +} + +function drainWaitQueue(queue: List, drainError: MongoDriverError) { + while (queue.length) { + const waitQueueMember = queue.shift(); + if (!waitQueueMember) { + continue; + } + + if (!waitQueueMember.cancelled) { + if ( + waitQueueMember.mongoLogger?.willLog( + MongoLoggableComponent.SERVER_SELECTION, + SeverityLevel.DEBUG + ) + ) { + waitQueueMember.mongoLogger?.debug( + MongoLoggableComponent.SERVER_SELECTION, + new ServerSelectionFailedEvent( + waitQueueMember.serverSelector, + waitQueueMember.topologyDescription, + drainError, + waitQueueMember.operationName + ) + ); + } + waitQueueMember.reject(drainError); + } + } +} + +function processWaitQueue(topology: Topology) { + if (topology.s.state === STATE_CLOSED) { + drainWaitQueue(topology.waitQueue, new MongoTopologyClosedError()); + return; + } + + const isSharded = topology.description.type === TopologyType.Sharded; + const serverDescriptions = Array.from(topology.description.servers.values()); + const membersToProcess = topology.waitQueue.length; + for (let i = 0; i < membersToProcess; ++i) { + const waitQueueMember = topology.waitQueue.shift(); + if (!waitQueueMember) { + continue; + } + + if (waitQueueMember.cancelled) { + continue; + } + + let selectedDescriptions; + try { + const serverSelector = waitQueueMember.serverSelector; + const deprioritizedServers = waitQueueMember.deprioritizedServers; + selectedDescriptions = serverSelector + ? serverSelector(topology.description, serverDescriptions, deprioritizedServers) + : serverDescriptions; + } catch (selectorError) { + if ( + topology.client.mongoLogger?.willLog( + MongoLoggableComponent.SERVER_SELECTION, + SeverityLevel.DEBUG + ) + ) { + topology.client.mongoLogger?.debug( + MongoLoggableComponent.SERVER_SELECTION, + new ServerSelectionFailedEvent( + waitQueueMember.serverSelector, + topology.description, + selectorError, + waitQueueMember.operationName + ) + ); + } + waitQueueMember.reject(selectorError); + continue; + } + + let selectedServer: Server | undefined; + if (selectedDescriptions.length === 0) { + if (!waitQueueMember.waitingLogged) { + if ( + topology.client.mongoLogger?.willLog( + MongoLoggableComponent.SERVER_SELECTION, + SeverityLevel.INFORMATIONAL + ) + ) { + topology.client.mongoLogger?.info( + MongoLoggableComponent.SERVER_SELECTION, + new WaitingForSuitableServerEvent( + waitQueueMember.serverSelector, + topology.description, + topology.s.serverSelectionTimeoutMS !== 0 + ? topology.s.serverSelectionTimeoutMS - + (processTimeMS() - waitQueueMember.startTime) + : -1, + waitQueueMember.operationName + ) + ); + } + waitQueueMember.waitingLogged = true; + } + topology.waitQueue.push(waitQueueMember); + continue; + } else if (selectedDescriptions.length === 1) { + selectedServer = topology.s.servers.get(selectedDescriptions[0].address); + } else { + const descriptions = shuffle(selectedDescriptions, 2); + const server1 = topology.s.servers.get(descriptions[0].address); + const server2 = topology.s.servers.get(descriptions[1].address); + + selectedServer = + server1 && server2 && server1.s.operationCount < server2.s.operationCount + ? server1 + : server2; + } + + if (!selectedServer) { + const serverSelectionError = new MongoServerSelectionError( + 'server selection returned a server description but the server was not found in the topology', + topology.description + ); + if ( + topology.client.mongoLogger?.willLog( + MongoLoggableComponent.SERVER_SELECTION, + SeverityLevel.DEBUG + ) + ) { + topology.client.mongoLogger?.debug( + MongoLoggableComponent.SERVER_SELECTION, + new ServerSelectionFailedEvent( + waitQueueMember.serverSelector, + topology.description, + serverSelectionError, + waitQueueMember.operationName + ) + ); + } + waitQueueMember.reject(serverSelectionError); + return; + } + const transaction = waitQueueMember.transaction; + if (isSharded && transaction && transaction.isActive && selectedServer) { + transaction.pinServer(selectedServer); + } + + if ( + topology.client.mongoLogger?.willLog( + MongoLoggableComponent.SERVER_SELECTION, + SeverityLevel.DEBUG + ) + ) { + topology.client.mongoLogger?.debug( + MongoLoggableComponent.SERVER_SELECTION, + new ServerSelectionSucceededEvent( + waitQueueMember.serverSelector, + waitQueueMember.topologyDescription, + selectedServer.pool.address, + waitQueueMember.operationName + ) + ); + } + waitQueueMember.resolve(selectedServer); + } + + if (topology.waitQueue.length > 0) { + // ensure all server monitors attempt monitoring soon + for (const [, server] of topology.s.servers) { + queueMicrotask(function scheduleServerCheck() { + return server.requestCheck(); + }); + } + } +} + +function isStaleServerDescription( + topologyDescription: TopologyDescription, + incomingServerDescription: ServerDescription +) { + const currentServerDescription = topologyDescription.servers.get( + incomingServerDescription.address + ); + const currentTopologyVersion = currentServerDescription?.topologyVersion; + return ( + compareTopologyVersion(currentTopologyVersion, incomingServerDescription.topologyVersion) > 0 + ); +} diff --git a/gateway/node_modules/mongodb/src/sdam/topology_description.ts b/gateway/node_modules/mongodb/src/sdam/topology_description.ts new file mode 100644 index 0000000000000000000000000000000000000000..a1c301dbc2db3fab38015dcd2917fed3529ae91a --- /dev/null +++ b/gateway/node_modules/mongodb/src/sdam/topology_description.ts @@ -0,0 +1,548 @@ +import { EJSON, type ObjectId } from '../bson'; +import * as WIRE_CONSTANTS from '../cmap/wire_protocol/constants'; +import { type MongoError, MongoRuntimeError, MongoStalePrimaryError } from '../error'; +import { compareObjectId, shuffle } from '../utils'; +import { ServerType, TopologyType } from './common'; +import { ServerDescription } from './server_description'; +import type { SrvPollingEvent } from './srv_polling'; + +// constants related to compatibility checks +const MIN_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_SERVER_VERSION; +const MAX_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_SERVER_VERSION; +const MIN_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_WIRE_VERSION; +const MAX_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_WIRE_VERSION; + +const MONGOS_OR_UNKNOWN = new Set([ServerType.Mongos, ServerType.Unknown]); +const MONGOS_OR_STANDALONE = new Set([ServerType.Mongos, ServerType.Standalone]); +const NON_PRIMARY_RS_MEMBERS = new Set([ + ServerType.RSSecondary, + ServerType.RSArbiter, + ServerType.RSOther +]); + +/** @public */ +export interface TopologyDescriptionOptions { + heartbeatFrequencyMS?: number; + localThresholdMS?: number; +} + +/** + * Representation of a deployment of servers + * @public + */ +export class TopologyDescription { + type: TopologyType; + setName: string | null; + maxSetVersion: number | null; + maxElectionId: ObjectId | null; + servers: Map; + stale: boolean; + compatible: boolean; + compatibilityError?: string; + logicalSessionTimeoutMinutes: number | null; + heartbeatFrequencyMS: number; + localThresholdMS: number; + commonWireVersion: number; + /** + * Create a TopologyDescription + */ + constructor( + topologyType: TopologyType, + serverDescriptions: Map | null = null, + setName: string | null = null, + maxSetVersion: number | null = null, + maxElectionId: ObjectId | null = null, + commonWireVersion: number | null = null, + options: TopologyDescriptionOptions | null = null + ) { + options = options ?? {}; + + this.type = topologyType ?? TopologyType.Unknown; + this.servers = serverDescriptions ?? new Map(); + this.stale = false; + this.compatible = true; + this.heartbeatFrequencyMS = options.heartbeatFrequencyMS ?? 0; + this.localThresholdMS = options.localThresholdMS ?? 15; + this.setName = setName ?? null; + this.maxElectionId = maxElectionId ?? null; + this.maxSetVersion = maxSetVersion ?? null; + this.commonWireVersion = commonWireVersion ?? 0; + + // determine server compatibility + for (const serverDescription of this.servers.values()) { + // Load balancer mode is always compatible. + if ( + serverDescription.type === ServerType.Unknown || + serverDescription.type === ServerType.LoadBalancer + ) { + continue; + } + + if (serverDescription.minWireVersion > MAX_SUPPORTED_WIRE_VERSION) { + this.compatible = false; + this.compatibilityError = `Server at ${serverDescription.address} requires wire version ${serverDescription.minWireVersion}, but this version of the driver only supports up to ${MAX_SUPPORTED_WIRE_VERSION} (MongoDB ${MAX_SUPPORTED_SERVER_VERSION})`; + } + + if (serverDescription.maxWireVersion < MIN_SUPPORTED_WIRE_VERSION) { + this.compatible = false; + this.compatibilityError = `Server at ${serverDescription.address} reports wire version ${serverDescription.maxWireVersion}, but this version of the driver requires at least ${MIN_SUPPORTED_WIRE_VERSION} (MongoDB ${MIN_SUPPORTED_SERVER_VERSION}).`; + break; + } + } + + // Whenever a client updates the TopologyDescription from a hello response, it MUST set + // TopologyDescription.logicalSessionTimeoutMinutes to the smallest logicalSessionTimeoutMinutes + // value among ServerDescriptions of all data-bearing server types. If any have a null + // logicalSessionTimeoutMinutes, then TopologyDescription.logicalSessionTimeoutMinutes MUST be + // set to null. + this.logicalSessionTimeoutMinutes = null; + for (const [, server] of this.servers) { + if (server.isReadable) { + if (server.logicalSessionTimeoutMinutes == null) { + // If any of the servers have a null logicalSessionsTimeout, then the whole topology does + this.logicalSessionTimeoutMinutes = null; + break; + } + + if (this.logicalSessionTimeoutMinutes == null) { + // First server with a non null logicalSessionsTimeout + this.logicalSessionTimeoutMinutes = server.logicalSessionTimeoutMinutes; + continue; + } + + // Always select the smaller of the: + // current server logicalSessionsTimeout and the topologies logicalSessionsTimeout + this.logicalSessionTimeoutMinutes = Math.min( + this.logicalSessionTimeoutMinutes, + server.logicalSessionTimeoutMinutes + ); + } + } + } + + /** + * Returns a new TopologyDescription based on the SrvPollingEvent + * @internal + */ + updateFromSrvPollingEvent(ev: SrvPollingEvent, srvMaxHosts = 0): TopologyDescription { + /** The SRV addresses defines the set of addresses we should be using */ + const incomingHostnames = ev.hostnames(); + const currentHostnames = new Set(this.servers.keys()); + + const hostnamesToAdd = new Set(incomingHostnames); + const hostnamesToRemove = new Set(); + for (const hostname of currentHostnames) { + // filter hostnamesToAdd (made from incomingHostnames) down to what is *not* present in currentHostnames + hostnamesToAdd.delete(hostname); + if (!incomingHostnames.has(hostname)) { + // If the SRV Records no longer include this hostname + // we have to stop using it + hostnamesToRemove.add(hostname); + } + } + + if (hostnamesToAdd.size === 0 && hostnamesToRemove.size === 0) { + // No new hosts to add and none to remove + return this; + } + + const serverDescriptions = new Map(this.servers); + for (const removedHost of hostnamesToRemove) { + serverDescriptions.delete(removedHost); + } + + if (hostnamesToAdd.size > 0) { + if (srvMaxHosts === 0) { + // Add all! + for (const hostToAdd of hostnamesToAdd) { + serverDescriptions.set(hostToAdd, new ServerDescription(hostToAdd)); + } + } else if (serverDescriptions.size < srvMaxHosts) { + // Add only the amount needed to get us back to srvMaxHosts + const selectedHosts = shuffle(hostnamesToAdd, srvMaxHosts - serverDescriptions.size); + for (const selectedHostToAdd of selectedHosts) { + serverDescriptions.set(selectedHostToAdd, new ServerDescription(selectedHostToAdd)); + } + } + } + + return new TopologyDescription( + this.type, + serverDescriptions, + this.setName, + this.maxSetVersion, + this.maxElectionId, + this.commonWireVersion, + { heartbeatFrequencyMS: this.heartbeatFrequencyMS, localThresholdMS: this.localThresholdMS } + ); + } + + /** + * Returns a copy of this description updated with a given ServerDescription + * @internal + */ + update(serverDescription: ServerDescription): TopologyDescription { + const address = serverDescription.address; + + // potentially mutated values + let { type: topologyType, setName, maxSetVersion, maxElectionId, commonWireVersion } = this; + + const serverType = serverDescription.type; + const serverDescriptions = new Map(this.servers); + + // update common wire version + if (serverDescription.maxWireVersion !== 0) { + if (commonWireVersion === 0) { + commonWireVersion = serverDescription.maxWireVersion; + } else { + commonWireVersion = Math.min(commonWireVersion, serverDescription.maxWireVersion); + } + } + + if ( + typeof serverDescription.setName === 'string' && + typeof setName === 'string' && + serverDescription.setName !== setName + ) { + if (topologyType === TopologyType.Single) { + // "Single" Topology with setName mismatch is direct connection usage, mark unknown do not remove + serverDescription = new ServerDescription(address); + } else { + serverDescriptions.delete(address); + } + } + + // update the actual server description + serverDescriptions.set(address, serverDescription); + + if (topologyType === TopologyType.Single) { + // once we are defined as single, that never changes + return new TopologyDescription( + TopologyType.Single, + serverDescriptions, + setName, + maxSetVersion, + maxElectionId, + commonWireVersion, + { heartbeatFrequencyMS: this.heartbeatFrequencyMS, localThresholdMS: this.localThresholdMS } + ); + } + + if (topologyType === TopologyType.Unknown) { + if (serverType === ServerType.Standalone && this.servers.size !== 1) { + serverDescriptions.delete(address); + } else { + topologyType = topologyTypeForServerType(serverType); + } + } + + if (topologyType === TopologyType.Sharded) { + if (!MONGOS_OR_UNKNOWN.has(serverType)) { + serverDescriptions.delete(address); + } + } + + if (topologyType === TopologyType.ReplicaSetNoPrimary) { + if (MONGOS_OR_STANDALONE.has(serverType)) { + serverDescriptions.delete(address); + } + + if (serverType === ServerType.RSPrimary) { + const result = updateRsFromPrimary( + serverDescriptions, + serverDescription, + setName, + maxSetVersion, + maxElectionId + ); + + topologyType = result[0]; + setName = result[1]; + maxSetVersion = result[2]; + maxElectionId = result[3]; + } else if (NON_PRIMARY_RS_MEMBERS.has(serverType)) { + const result = updateRsNoPrimaryFromMember(serverDescriptions, serverDescription, setName); + topologyType = result[0]; + setName = result[1]; + } + } + + if (topologyType === TopologyType.ReplicaSetWithPrimary) { + if (MONGOS_OR_STANDALONE.has(serverType)) { + serverDescriptions.delete(address); + topologyType = checkHasPrimary(serverDescriptions); + } else if (serverType === ServerType.RSPrimary) { + const result = updateRsFromPrimary( + serverDescriptions, + serverDescription, + setName, + maxSetVersion, + maxElectionId + ); + + topologyType = result[0]; + setName = result[1]; + maxSetVersion = result[2]; + maxElectionId = result[3]; + } else if (NON_PRIMARY_RS_MEMBERS.has(serverType)) { + topologyType = updateRsWithPrimaryFromMember( + serverDescriptions, + serverDescription, + setName + ); + } else { + topologyType = checkHasPrimary(serverDescriptions); + } + } + + return new TopologyDescription( + topologyType, + serverDescriptions, + setName, + maxSetVersion, + maxElectionId, + commonWireVersion, + { heartbeatFrequencyMS: this.heartbeatFrequencyMS, localThresholdMS: this.localThresholdMS } + ); + } + + get error(): MongoError | null { + const descriptionsWithError = Array.from(this.servers.values()).filter( + (sd: ServerDescription) => sd.error + ); + + if (descriptionsWithError.length > 0) { + return descriptionsWithError[0].error; + } + + return null; + } + + /** + * Determines if the topology description has any known servers + */ + get hasKnownServers(): boolean { + return Array.from(this.servers.values()).some( + (sd: ServerDescription) => sd.type !== ServerType.Unknown + ); + } + + /** + * Determines if this topology description has a data-bearing server available. + */ + get hasDataBearingServers(): boolean { + return Array.from(this.servers.values()).some((sd: ServerDescription) => sd.isDataBearing); + } + + /** + * Determines if the topology has a definition for the provided address + * @internal + */ + hasServer(address: string): boolean { + return this.servers.has(address); + } + + /** + * Returns a JSON-serializable representation of the TopologyDescription. This is primarily + * intended for use with JSON.stringify(). + * + * This method will not throw. + */ + toJSON() { + return EJSON.serialize(this); + } +} + +function topologyTypeForServerType(serverType: ServerType): TopologyType { + switch (serverType) { + case ServerType.Standalone: + return TopologyType.Single; + case ServerType.Mongos: + return TopologyType.Sharded; + case ServerType.RSPrimary: + return TopologyType.ReplicaSetWithPrimary; + case ServerType.RSOther: + case ServerType.RSSecondary: + return TopologyType.ReplicaSetNoPrimary; + default: + return TopologyType.Unknown; + } +} + +function updateRsFromPrimary( + serverDescriptions: Map, + serverDescription: ServerDescription, + setName: string | null = null, + maxSetVersion: number | null = null, + maxElectionId: ObjectId | null = null +): [TopologyType, string | null, number | null, ObjectId | null] { + const setVersionElectionIdMismatch = ( + serverDescription: ServerDescription, + maxSetVersion: number | null, + maxElectionId: ObjectId | null + ) => { + return ( + `primary marked stale due to electionId/setVersion mismatch:` + + ` server setVersion: ${serverDescription.setVersion},` + + ` server electionId: ${serverDescription.electionId},` + + ` topology setVersion: ${maxSetVersion},` + + ` topology electionId: ${maxElectionId}` + ); + }; + setName = setName || serverDescription.setName; + if (setName !== serverDescription.setName) { + serverDescriptions.delete(serverDescription.address); + return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId]; + } + + if (serverDescription.maxWireVersion >= 17) { + const electionIdComparison = compareObjectId(maxElectionId, serverDescription.electionId); + const maxElectionIdIsEqual = electionIdComparison === 0; + const maxElectionIdIsLess = electionIdComparison === -1; + const maxSetVersionIsLessOrEqual = + (maxSetVersion ?? -1) <= (serverDescription.setVersion ?? -1); + + if (maxElectionIdIsLess || (maxElectionIdIsEqual && maxSetVersionIsLessOrEqual)) { + // The reported electionId was greater + // or the electionId was equal and reported setVersion was greater + // Always update both values, they are a tuple + maxElectionId = serverDescription.electionId; + maxSetVersion = serverDescription.setVersion; + } else { + // Stale primary + // replace serverDescription with a default ServerDescription of type "Unknown" + serverDescriptions.set( + serverDescription.address, + new ServerDescription(serverDescription.address, undefined, { + error: new MongoStalePrimaryError( + setVersionElectionIdMismatch(serverDescription, maxSetVersion, maxElectionId) + ) + }) + ); + + return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId]; + } + } else { + const electionId = serverDescription.electionId ? serverDescription.electionId : null; + if (serverDescription.setVersion && electionId) { + if (maxSetVersion && maxElectionId) { + if ( + maxSetVersion > serverDescription.setVersion || + compareObjectId(maxElectionId, electionId) > 0 + ) { + // this primary is stale, we must remove it + serverDescriptions.set( + serverDescription.address, + new ServerDescription(serverDescription.address, undefined, { + error: new MongoStalePrimaryError( + setVersionElectionIdMismatch(serverDescription, maxSetVersion, maxElectionId) + ) + }) + ); + + return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId]; + } + } + + maxElectionId = serverDescription.electionId; + } + + if ( + serverDescription.setVersion != null && + (maxSetVersion == null || serverDescription.setVersion > maxSetVersion) + ) { + maxSetVersion = serverDescription.setVersion; + } + } + + // We've heard from the primary. Is it the same primary as before? + for (const [address, server] of serverDescriptions) { + if (server.type === ServerType.RSPrimary && server.address !== serverDescription.address) { + // Reset old primary's type to Unknown. + serverDescriptions.set( + address, + new ServerDescription(server.address, undefined, { + error: new MongoStalePrimaryError( + 'primary marked stale due to discovery of newer primary' + ) + }) + ); + + // There can only be one primary + break; + } + } + + // Discover new hosts from this primary's response. + serverDescription.allHosts.forEach((address: string) => { + if (!serverDescriptions.has(address)) { + serverDescriptions.set(address, new ServerDescription(address)); + } + }); + + // Remove hosts not in the response. + const currentAddresses = Array.from(serverDescriptions.keys()); + const responseAddresses = serverDescription.allHosts; + currentAddresses + .filter((addr: string) => responseAddresses.indexOf(addr) === -1) + .forEach((address: string) => { + serverDescriptions.delete(address); + }); + + return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId]; +} + +function updateRsWithPrimaryFromMember( + serverDescriptions: Map, + serverDescription: ServerDescription, + setName: string | null = null +): TopologyType { + if (setName == null) { + // TODO(NODE-3483): should be an appropriate runtime error + throw new MongoRuntimeError('Argument "setName" is required if connected to a replica set'); + } + + if ( + setName !== serverDescription.setName || + (serverDescription.me && serverDescription.address !== serverDescription.me) + ) { + serverDescriptions.delete(serverDescription.address); + } + + return checkHasPrimary(serverDescriptions); +} + +function updateRsNoPrimaryFromMember( + serverDescriptions: Map, + serverDescription: ServerDescription, + setName: string | null = null +): [TopologyType, string | null] { + const topologyType = TopologyType.ReplicaSetNoPrimary; + setName = setName ?? serverDescription.setName; + if (setName !== serverDescription.setName) { + serverDescriptions.delete(serverDescription.address); + return [topologyType, setName]; + } + + serverDescription.allHosts.forEach((address: string) => { + if (!serverDescriptions.has(address)) { + serverDescriptions.set(address, new ServerDescription(address)); + } + }); + + if (serverDescription.me && serverDescription.address !== serverDescription.me) { + serverDescriptions.delete(serverDescription.address); + } + + return [topologyType, setName]; +} + +function checkHasPrimary(serverDescriptions: Map): TopologyType { + for (const serverDescription of serverDescriptions.values()) { + if (serverDescription.type === ServerType.RSPrimary) { + return TopologyType.ReplicaSetWithPrimary; + } + } + + return TopologyType.ReplicaSetNoPrimary; +} diff --git a/gateway/node_modules/mongodb/src/sessions.ts b/gateway/node_modules/mongodb/src/sessions.ts new file mode 100644 index 0000000000000000000000000000000000000000..5e129f0775fffdc56b8c89641de884550357a9d9 --- /dev/null +++ b/gateway/node_modules/mongodb/src/sessions.ts @@ -0,0 +1,1287 @@ +import { setTimeout } from 'timers/promises'; + +import { Binary, ByteUtils, type Document, Long, type Timestamp } from './bson'; +import type { CommandOptions, Connection } from './cmap/connection'; +import { ConnectionPoolMetrics } from './cmap/metrics'; +import { type MongoDBResponse } from './cmap/wire_protocol/responses'; +import { PINNED, UNPINNED } from './constants'; +import type { AbstractCursor } from './cursor/abstract_cursor'; +import { + type AnyError, + isRetryableWriteError, + MongoAPIError, + MongoCompatibilityError, + MONGODB_ERROR_CODES, + type MongoDriverError, + MongoError, + MongoErrorLabel, + MongoExpiredSessionError, + MongoInvalidArgumentError, + MongoOperationTimeoutError, + MongoRuntimeError, + MongoServerError, + MongoTransactionError, + MongoWriteConcernError +} from './error'; +import type { MongoClient, MongoOptions } from './mongo_client'; +import { TypedEventEmitter } from './mongo_types'; +import { executeOperation } from './operations/execute_operation'; +import { RunCommandOperation } from './operations/run_command'; +import { ReadConcernLevel } from './read_concern'; +import { ReadPreference } from './read_preference'; +import { _advanceClusterTime, type ClusterTime, TopologyType } from './sdam/common'; +import { TimeoutContext } from './timeout'; +import { + isTransactionCommand, + Transaction, + type TransactionOptions, + TxnState +} from './transactions'; +import { + calculateDurationInMs, + commandSupportsAfterClusterTime, + isPromiseLike, + List, + MongoDBNamespace, + noop, + processTimeMS, + squashError, + uuidV4 +} from './utils'; +import { WriteConcern, type WriteConcernOptions, type WriteConcernSettings } from './write_concern'; + +/** @public */ +export interface ClientSessionOptions { + /** Whether causal consistency should be enabled on this session */ + causalConsistency?: boolean; + /** Whether all read operations should be read from the same snapshot for this session (NOTE: not compatible with `causalConsistency=true`) */ + snapshot?: boolean; + /** The default TransactionOptions to use for transactions started on this session. */ + defaultTransactionOptions?: TransactionOptions; + /** + * @public + * @experimental + * An overriding timeoutMS value to use for a client-side timeout. + * If not provided the session uses the timeoutMS specified on the MongoClient. + */ + defaultTimeoutMS?: number; + + /** @internal */ + owner?: symbol | AbstractCursor; + /** @internal */ + explicit?: boolean; + /** @internal */ + initialClusterTime?: ClusterTime; +} + +/** @public */ +export type WithTransactionCallback = (session: ClientSession) => Promise; + +/** @public */ +export type ClientSessionEvents = { + ended(session: ClientSession): void; +}; + +/** @public */ +export interface EndSessionOptions { + /** + * An optional error which caused the call to end this session + * @internal + */ + error?: AnyError; + force?: boolean; + forceClear?: boolean; + + /** Specifies the time an operation will run until it throws a timeout error */ + timeoutMS?: number; +} + +/** + * A class representing a client session on the server + * + * NOTE: not meant to be instantiated directly. + * @public + */ +export class ClientSession + extends TypedEventEmitter + implements AsyncDisposable +{ + /** @internal */ + client: MongoClient; + /** @internal */ + sessionPool: ServerSessionPool; + hasEnded: boolean; + clientOptions: MongoOptions; + supports: { causalConsistency: boolean }; + clusterTime?: ClusterTime; + operationTime?: Timestamp; + explicit: boolean; + /** @internal */ + owner?: symbol | AbstractCursor; + defaultTransactionOptions: TransactionOptions; + /** @internal */ + transaction: Transaction; + /** + * @internal + * Keeps track of whether or not the current transaction has attempted to be committed. Is + * initially undefined. Gets set to false when startTransaction is called. When commitTransaction is sent to server, if the commitTransaction succeeds, it is then set to undefined, otherwise, set to true + */ + private commitAttempted?: boolean; + public readonly snapshotEnabled: boolean; + + /** @internal */ + private _serverSession: ServerSession | null; + /** @internal */ + public snapshotTime?: Timestamp; + /** @internal */ + public pinnedConnection?: Connection; + /** @internal */ + public txnNumberIncrement: number; + /** + * @experimental + * Specifies the time an operation in a given `ClientSession` will run until it throws a timeout error + */ + timeoutMS?: number; + + /** @internal */ + public timeoutContext: TimeoutContext | null = null; + + /** + * Create a client session. + * @internal + * @param client - The current client + * @param sessionPool - The server session pool (Internal Class) + * @param options - Optional settings + * @param clientOptions - Optional settings provided when creating a MongoClient + */ + constructor( + client: MongoClient, + sessionPool: ServerSessionPool, + options: ClientSessionOptions, + clientOptions: MongoOptions + ) { + super(); + this.on('error', noop); + + if (client == null) { + // TODO(NODE-3483) + throw new MongoRuntimeError('ClientSession requires a MongoClient'); + } + + if (sessionPool == null || !(sessionPool instanceof ServerSessionPool)) { + // TODO(NODE-3483) + throw new MongoRuntimeError('ClientSession requires a ServerSessionPool'); + } + + options = options ?? {}; + + this.snapshotEnabled = options.snapshot === true; + if (options.causalConsistency === true && this.snapshotEnabled) { + throw new MongoInvalidArgumentError( + 'Properties "causalConsistency" and "snapshot" are mutually exclusive' + ); + } + + this.client = client; + this.sessionPool = sessionPool; + this.hasEnded = false; + this.clientOptions = clientOptions; + this.timeoutMS = options.defaultTimeoutMS ?? client.s.options?.timeoutMS; + + this.explicit = !!options.explicit; + this._serverSession = this.explicit ? this.sessionPool.acquire() : null; + this.txnNumberIncrement = 0; + + const defaultCausalConsistencyValue = this.explicit && options.snapshot !== true; + this.supports = { + // if we can enable causal consistency, do so by default + causalConsistency: options.causalConsistency ?? defaultCausalConsistencyValue + }; + + this.clusterTime = options.initialClusterTime; + + this.operationTime = undefined; + this.owner = options.owner; + this.defaultTransactionOptions = { ...options.defaultTransactionOptions }; + this.transaction = new Transaction(); + } + + /** The server id associated with this session */ + get id(): ServerSessionId | undefined { + return this.serverSession?.id; + } + + get serverSession(): ServerSession { + let serverSession = this._serverSession; + if (serverSession == null) { + if (this.explicit) { + throw new MongoRuntimeError('Unexpected null serverSession for an explicit session'); + } + if (this.hasEnded) { + throw new MongoRuntimeError('Unexpected null serverSession for an ended implicit session'); + } + serverSession = this.sessionPool.acquire(); + this._serverSession = serverSession; + } + return serverSession; + } + + get loadBalanced(): boolean { + return this.client.topology?.description.type === TopologyType.LoadBalanced; + } + + /** @internal */ + pin(conn: Connection): void { + if (this.pinnedConnection) { + throw TypeError('Cannot pin multiple connections to the same session'); + } + + this.pinnedConnection = conn; + conn.emit( + PINNED, + this.inTransaction() ? ConnectionPoolMetrics.TXN : ConnectionPoolMetrics.CURSOR + ); + } + + /** @internal */ + unpin(options?: { force?: boolean; forceClear?: boolean; error?: AnyError }): void { + if (this.loadBalanced) { + return maybeClearPinnedConnection(this, options); + } + + this.transaction.unpinServer(); + } + + get isPinned(): boolean { + return this.loadBalanced ? !!this.pinnedConnection : this.transaction.isPinned; + } + + /** + * Frees any client-side resources held by the current session. If a session is in a transaction, + * the transaction is aborted. + * + * Does not end the session on the server. + * + * @param options - Optional settings. Currently reserved for future use + */ + async endSession(options?: EndSessionOptions): Promise { + try { + if (this.inTransaction()) { + await this.abortTransaction({ ...options, throwTimeout: true }); + } + } catch (error) { + // spec indicates that we should ignore all errors for `endSessions` + if (error.name === 'MongoOperationTimeoutError') throw error; + squashError(error); + } finally { + if (!this.hasEnded) { + const serverSession = this.serverSession; + if (serverSession != null) { + // release the server session back to the pool + this.sessionPool.release(serverSession); + // Store a clone of the server session for reference (debugging) + this._serverSession = new ServerSession(serverSession); + } + // mark the session as ended, and emit a signal + this.hasEnded = true; + this.emit('ended', this); + } + maybeClearPinnedConnection(this, { force: true, ...options }); + } + } + /** + * An alias for {@link ClientSession.endSession|ClientSession.endSession()}. + */ + async [Symbol.asyncDispose]() { + await this.endSession({ force: true }); + } + + /** + * Advances the operationTime for a ClientSession. + * + * @param operationTime - the `BSON.Timestamp` of the operation type it is desired to advance to + */ + advanceOperationTime(operationTime: Timestamp): void { + if (this.operationTime == null) { + this.operationTime = operationTime; + return; + } + + if (operationTime.greaterThan(this.operationTime)) { + this.operationTime = operationTime; + } + } + + /** + * Advances the clusterTime for a ClientSession to the provided clusterTime of another ClientSession + * + * @param clusterTime - the $clusterTime returned by the server from another session in the form of a document containing the `BSON.Timestamp` clusterTime and signature + */ + advanceClusterTime(clusterTime: ClusterTime): void { + if (!clusterTime || typeof clusterTime !== 'object') { + throw new MongoInvalidArgumentError('input cluster time must be an object'); + } + if (!clusterTime.clusterTime || clusterTime.clusterTime._bsontype !== 'Timestamp') { + throw new MongoInvalidArgumentError( + 'input cluster time "clusterTime" property must be a valid BSON Timestamp' + ); + } + if ( + !clusterTime.signature || + clusterTime.signature.hash?._bsontype !== 'Binary' || + (typeof clusterTime.signature.keyId !== 'bigint' && + typeof clusterTime.signature.keyId !== 'number' && + clusterTime.signature.keyId?._bsontype !== 'Long') // apparently we decode the key to number? + ) { + throw new MongoInvalidArgumentError( + 'input cluster time must have a valid "signature" property with BSON Binary hash and BSON Long keyId' + ); + } + + _advanceClusterTime(this, clusterTime); + } + + /** + * Used to determine if this session equals another + * + * @param session - The session to compare to + */ + equals(session: ClientSession): boolean { + if (!(session instanceof ClientSession)) { + return false; + } + + if (this.id == null || session.id == null) { + return false; + } + + return ByteUtils.equals(this.id.id.buffer, session.id.id.buffer); + } + + /** + * Increment the transaction number on the internal ServerSession + * + * @privateRemarks + * This helper increments a value stored on the client session that will be + * added to the serverSession's txnNumber upon applying it to a command. + * This is because the serverSession is lazily acquired after a connection is obtained + */ + incrementTransactionNumber(): void { + this.txnNumberIncrement += 1; + } + + /** @returns whether this session is currently in a transaction or not */ + inTransaction(): boolean { + return this.transaction.isActive; + } + + /** + * Starts a new transaction with the given options. + * + * @remarks + * **IMPORTANT**: Running operations in parallel is not supported during a transaction. The use of `Promise.all`, + * `Promise.allSettled`, `Promise.race`, etc to parallelize operations inside a transaction is + * undefined behaviour. + * + * @param options - Options for the transaction + */ + startTransaction(options?: TransactionOptions): void { + if (this.snapshotEnabled) { + throw new MongoCompatibilityError('Transactions are not supported in snapshot sessions'); + } + + if (this.inTransaction()) { + throw new MongoTransactionError('Transaction already in progress'); + } + + if (this.isPinned && this.transaction.isCommitted) { + this.unpin(); + } + + this.commitAttempted = false; + // increment txnNumber + this.incrementTransactionNumber(); + // create transaction state + this.transaction = new Transaction({ + readConcern: + options?.readConcern ?? + this.defaultTransactionOptions.readConcern ?? + this.clientOptions?.readConcern, + writeConcern: + options?.writeConcern ?? + this.defaultTransactionOptions.writeConcern ?? + this.clientOptions?.writeConcern, + readPreference: + options?.readPreference ?? + this.defaultTransactionOptions.readPreference ?? + this.clientOptions?.readPreference, + maxCommitTimeMS: options?.maxCommitTimeMS ?? this.defaultTransactionOptions.maxCommitTimeMS + }); + + this.transaction.transition(TxnState.STARTING_TRANSACTION); + } + + /** + * Commits the currently active transaction in this session. + * + * @param options - Optional options, can be used to override `defaultTimeoutMS`. + */ + async commitTransaction(options?: { timeoutMS?: number }): Promise { + if (this.transaction.state === TxnState.NO_TRANSACTION) { + throw new MongoTransactionError('No transaction started'); + } + + if ( + this.transaction.state === TxnState.STARTING_TRANSACTION || + this.transaction.state === TxnState.TRANSACTION_COMMITTED_EMPTY + ) { + // the transaction was never started, we can safely exit here + this.transaction.transition(TxnState.TRANSACTION_COMMITTED_EMPTY); + return; + } + + if (this.transaction.state === TxnState.TRANSACTION_ABORTED) { + throw new MongoTransactionError( + 'Cannot call commitTransaction after calling abortTransaction' + ); + } + + const command: { + commitTransaction: 1; + writeConcern?: WriteConcernSettings; + recoveryToken?: Document; + maxTimeMS?: number; + } = { commitTransaction: 1 }; + + const timeoutMS = + typeof options?.timeoutMS === 'number' + ? options.timeoutMS + : typeof this.timeoutMS === 'number' + ? this.timeoutMS + : null; + + const wc = this.transaction.options.writeConcern ?? this.clientOptions?.writeConcern; + if (wc != null) { + if (timeoutMS == null && this.timeoutContext == null) { + WriteConcern.apply(command, { wtimeoutMS: 10000, w: 'majority', ...wc }); + } else { + const wcKeys = Object.keys(wc); + if (wcKeys.length > 2 || (!wcKeys.includes('wtimeoutMS') && !wcKeys.includes('wTimeoutMS'))) + // if the write concern was specified with wTimeoutMS, then we set both wtimeoutMS + // and wTimeoutMS, guaranteeing at least two keys, so if we have more than two keys, + // then we can automatically assume that we should add the write concern to the command. + // If it has 2 or fewer keys, we need to check that those keys aren't the wtimeoutMS + // or wTimeoutMS options before we add the write concern to the command + WriteConcern.apply(command, { ...wc, wtimeoutMS: undefined }); + } + } + + if (this.transaction.state === TxnState.TRANSACTION_COMMITTED || this.commitAttempted) { + if (timeoutMS == null && this.timeoutContext == null) { + WriteConcern.apply(command, { wtimeoutMS: 10000, ...wc, w: 'majority' }); + } else { + WriteConcern.apply(command, { w: 'majority', ...wc, wtimeoutMS: undefined }); + } + } + + if (typeof this.transaction.options.maxTimeMS === 'number') { + command.maxTimeMS = this.transaction.options.maxTimeMS; + } + + if (this.transaction.recoveryToken) { + command.recoveryToken = this.transaction.recoveryToken; + } + + const operation = new RunCommandOperation(new MongoDBNamespace('admin'), command, { + session: this, + readPreference: ReadPreference.primary, + bypassPinningCheck: true + }); + operation.maxAttempts = this.clientOptions.maxAdaptiveRetries + 1; + + const timeoutContext = + this.timeoutContext ?? + (typeof timeoutMS === 'number' + ? TimeoutContext.create({ + serverSelectionTimeoutMS: this.clientOptions.serverSelectionTimeoutMS, + socketTimeoutMS: this.clientOptions.socketTimeoutMS, + timeoutMS + }) + : null); + + try { + await executeOperation(this.client, operation, timeoutContext); + this.commitAttempted = undefined; + return; + } catch (firstCommitError) { + this.commitAttempted = true; + + const remainingAttempts = this.clientOptions.maxAdaptiveRetries + 1 - operation.attemptsMade; + if (remainingAttempts <= 0) { + throw firstCommitError; + } + + if (firstCommitError instanceof MongoError && isRetryableWriteError(firstCommitError)) { + // SPEC-1185: apply majority write concern when retrying commitTransaction + WriteConcern.apply(command, { wtimeoutMS: 10000, ...wc, w: 'majority' }); + // per txns spec, must unpin session in this case + this.unpin({ force: true }); + + try { + const op = new RunCommandOperation(new MongoDBNamespace('admin'), command, { + session: this, + readPreference: ReadPreference.primary, + bypassPinningCheck: true + }); + op.maxAttempts = remainingAttempts; + await executeOperation(this.client, op, timeoutContext); + return; + } catch (retryCommitError) { + // If the retry failed, we process that error instead of the original + if (shouldAddUnknownTransactionCommitResultLabel(retryCommitError)) { + retryCommitError.addErrorLabel(MongoErrorLabel.UnknownTransactionCommitResult); + } + + if (shouldUnpinAfterCommitError(retryCommitError)) { + this.unpin({ error: retryCommitError }); + } + + throw retryCommitError; + } + } + + if (shouldAddUnknownTransactionCommitResultLabel(firstCommitError)) { + firstCommitError.addErrorLabel(MongoErrorLabel.UnknownTransactionCommitResult); + } + + if (shouldUnpinAfterCommitError(firstCommitError)) { + this.unpin({ error: firstCommitError }); + } + + throw firstCommitError; + } finally { + this.transaction.transition(TxnState.TRANSACTION_COMMITTED); + } + } + + /** + * Aborts the currently active transaction in this session. + * + * @param options - Optional options, can be used to override `defaultTimeoutMS`. + */ + async abortTransaction(options?: { timeoutMS?: number }): Promise; + /** @internal */ + async abortTransaction(options?: { timeoutMS?: number; throwTimeout?: true }): Promise; + async abortTransaction(options?: { timeoutMS?: number; throwTimeout?: true }): Promise { + if (this.transaction.state === TxnState.NO_TRANSACTION) { + throw new MongoTransactionError('No transaction started'); + } + + if (this.transaction.state === TxnState.STARTING_TRANSACTION) { + // the transaction was never started, we can safely exit here + this.transaction.transition(TxnState.TRANSACTION_ABORTED); + return; + } + + if (this.transaction.state === TxnState.TRANSACTION_ABORTED) { + throw new MongoTransactionError('Cannot call abortTransaction twice'); + } + + if ( + this.transaction.state === TxnState.TRANSACTION_COMMITTED || + this.transaction.state === TxnState.TRANSACTION_COMMITTED_EMPTY + ) { + throw new MongoTransactionError( + 'Cannot call abortTransaction after calling commitTransaction' + ); + } + + const command: { + abortTransaction: 1; + writeConcern?: WriteConcernOptions; + recoveryToken?: Document; + } = { abortTransaction: 1 }; + + const timeoutMS = + typeof options?.timeoutMS === 'number' + ? options.timeoutMS + : this.timeoutContext?.csotEnabled() + ? this.timeoutContext.timeoutMS // refresh timeoutMS for abort operation + : typeof this.timeoutMS === 'number' + ? this.timeoutMS + : null; + + const timeoutContext = + timeoutMS != null + ? TimeoutContext.create({ + timeoutMS, + serverSelectionTimeoutMS: this.clientOptions.serverSelectionTimeoutMS, + socketTimeoutMS: this.clientOptions.socketTimeoutMS + }) + : null; + + const wc = this.transaction.options.writeConcern ?? this.clientOptions?.writeConcern; + if (wc != null && timeoutMS == null) { + WriteConcern.apply(command, { wtimeoutMS: 10000, w: 'majority', ...wc }); + } + + if (this.transaction.recoveryToken) { + command.recoveryToken = this.transaction.recoveryToken; + } + + const operation = new RunCommandOperation(new MongoDBNamespace('admin'), command, { + session: this, + readPreference: ReadPreference.primary, + bypassPinningCheck: true + }); + + try { + await executeOperation(this.client, operation, timeoutContext); + this.unpin(); + return; + } catch (firstAbortError) { + this.unpin(); + + if (firstAbortError.name === 'MongoRuntimeError') throw firstAbortError; + if (options?.throwTimeout && firstAbortError.name === 'MongoOperationTimeoutError') { + throw firstAbortError; + } + + if (firstAbortError instanceof MongoError && isRetryableWriteError(firstAbortError)) { + try { + await executeOperation(this.client, operation, timeoutContext); + return; + } catch (secondAbortError) { + if (secondAbortError.name === 'MongoRuntimeError') throw secondAbortError; + if (options?.throwTimeout && secondAbortError.name === 'MongoOperationTimeoutError') { + throw secondAbortError; + } + // we do not retry the retry + } + } + + // The spec indicates that if the operation times out or fails with a non-retryable error, we should ignore all errors on `abortTransaction` + } finally { + this.transaction.transition(TxnState.TRANSACTION_ABORTED); + if (this.loadBalanced) { + maybeClearPinnedConnection(this, { force: false }); + } + } + } + + /** + * This is here to ensure that ClientSession is never serialized to BSON. + */ + toBSON(): never { + throw new MongoRuntimeError('ClientSession cannot be serialized to BSON.'); + } + + /** + * Starts a transaction and runs a provided function, ensuring the commitTransaction is always attempted when all operations run in the function have completed. + * + * **IMPORTANT:** This method requires the function passed in to return a Promise. That promise must be made by `await`-ing all operations in such a way that rejections are propagated to the returned promise. + * + * **IMPORTANT:** Running operations in parallel is not supported during a transaction. The use of `Promise.all`, + * `Promise.allSettled`, `Promise.race`, etc to parallelize operations inside a transaction is + * undefined behaviour. + * + * **IMPORTANT:** When running an operation inside a `withTransaction` callback, if it is not + * provided the explicit session in its options, it will not be part of the transaction and it will not respect timeoutMS. + * + * + * @remarks + * - If all operations successfully complete and the `commitTransaction` operation is successful, then the provided function will return the result of the provided function. + * - If the transaction is unable to complete or an error is thrown from within the provided function, then the provided function will throw an error. + * - If the transaction is manually aborted within the provided function it will not throw. + * - If the driver needs to attempt to retry the operations, the provided function may be called multiple times. + * + * Checkout a descriptive example here: + * @see https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-implement-transactions + * + * If a command inside withTransaction fails: + * - It may cause the transaction on the server to be aborted. + * - This situation is normally handled transparently by the driver. + * - However, if the application catches such an error and does not rethrow it, the driver will not be able to determine whether the transaction was aborted or not. + * - The driver will then retry the transaction indefinitely. + * + * To avoid this situation, the application must not silently handle errors within the provided function. + * If the application needs to handle errors within, it must await all operations such that if an operation is rejected it becomes the rejection of the callback function passed into withTransaction. + * + * @param fn - callback to run within a transaction + * @param options - optional settings for the transaction + * @returns A raw command response or undefined + */ + async withTransaction( + fn: WithTransactionCallback, + options?: TransactionOptions & { + /** + * Configures a timeoutMS expiry for the entire withTransactionCallback. + * + * @remarks + * - The remaining timeout will not be applied to callback operations that do not use the ClientSession. + * - Overriding timeoutMS for operations executed using the explicit session inside the provided callback will result in a client-side error. + */ + timeoutMS?: number; + } + ): Promise { + const MAX_TIMEOUT = 120_000; + + const timeoutMS = options?.timeoutMS ?? this.timeoutMS ?? null; + this.timeoutContext = + timeoutMS != null + ? TimeoutContext.create({ + timeoutMS, + serverSelectionTimeoutMS: this.clientOptions.serverSelectionTimeoutMS, + socketTimeoutMS: this.clientOptions.socketTimeoutMS + }) + : null; + + // 1. Define the following: + // 1.1 Record the current monotonic time, which will be used to enforce the 120-second / CSOT timeout before later retry attempts. + // 1.2 Set `transactionAttempt` to `0`. + // 1.3 Set `TIMEOUT_MS` to be `timeoutMS` if given, otherwise MAX_TIMEOUT (120-seconds). + + // Timeout Error propagation + // When the previously encountered error needs to be propagated because there is no more time for another attempt, + // and it is not already a timeout error, then: + // - A timeout error MUST be propagated instead. It MUST expose the previously encountered error as specified in + // the "Errors" section of the CSOT specification. + // - If exposing the previously encountered error from a timeout error is impossible in a driver, then the driver + // is exempt from the requirement and MUST propagate the previously encountered error as is. The timeout error + // MUST copy all error labels from the previously encountered error. + + // The spec describes timeout checks as "elapsed time < TIMEOUT_MS" (where elapsed = now - start). + // We precompute `deadline = now + remainingTimeMS` so each check becomes simply `now < deadline`. + const csotEnabled = !!this.timeoutContext?.csotEnabled(); + const remainingTimeMS = this.timeoutContext?.csotEnabled() + ? this.timeoutContext.remainingTimeMS + : MAX_TIMEOUT; + const deadline = processTimeMS() + remainingTimeMS; + + let committed = false; + let result: T; + + let lastError: Error | null = null; + + try { + retryTransaction: for ( + let transactionAttempt = 0, isRetry = false; + !committed; + ++transactionAttempt, isRetry = transactionAttempt > 0 + ) { + // 2. If `transactionAttempt` > 0: + if (isRetry) { + // 2.1 Calculate backoffMS to be jitter * min(BACKOFF_INITIAL * 1.5 ** (transactionAttempt - 1), BACKOFF_MAX). + // If elapsed time + backoffMS > TIMEOUT_MS, then propagate the previously encountered error to the caller of + // withTransaction as per timeout error propagation and return immediately. Otherwise, sleep for backoffMS. + // 2.1.1 jitter is a random float between [0, 1), optionally including 1, depending on what is most natural + // for the given driver language. + // 2.1.2 transactionAttempt is the variable defined in step 1. + // 2.1.3 BACKOFF_INITIAL is 5ms + // 2.1.4 BACKOFF_MAX is 500ms + const BACKOFF_INITIAL_MS = 5; + const BACKOFF_MAX_MS = 500; + const BACKOFF_GROWTH = 1.5; + const jitter = Math.random(); + const backoffMS = + jitter * + Math.min( + BACKOFF_INITIAL_MS * BACKOFF_GROWTH ** (transactionAttempt - 1), + BACKOFF_MAX_MS + ); + + if (processTimeMS() + backoffMS >= deadline) { + throw makeTimeoutError( + lastError ?? + new MongoRuntimeError( + `Transaction retry did not record an error: should never occur. Please file a bug.` + ), + csotEnabled + ); + } + + await setTimeout(backoffMS); + } + + // 3. Invoke startTransaction on the session and increment transactionAttempt. If TransactionOptions were + // specified in the call to withTransaction, those MUST be used for startTransaction. Note that + // ClientSession.defaultTransactionOptions will be used in the absence of any explicit TransactionOptions. + // 4. If startTransaction reported an error, propagate that error to the caller of withTransaction as is and + // return immediately. + this.startTransaction(options); + + try { + // 5. Invoke the callback. Drivers MUST ensure that the ClientSession can be accessed within the callback + // (e.g. pass ClientSession as the first parameter, rely on lexical scoping). Drivers MAY pass additional + // parameters as needed (e.g. user data solicited by withTransaction). + const promise = fn(this); + if (!isPromiseLike(promise)) { + throw new MongoInvalidArgumentError( + 'Function provided to `withTransaction` must return a Promise' + ); + } + + // 6. Control returns to withTransaction. Determine the current state of the ClientSession and whether the + // callback reported an error (e.g. thrown exception, error output parameter). + result = await promise; + + // 8. If the ClientSession is in the "no transaction", "transaction aborted", or "transaction committed" + // state, assume the callback intentionally aborted or committed the transaction and return immediately. + if ( + this.transaction.state === TxnState.NO_TRANSACTION || + this.transaction.state === TxnState.TRANSACTION_COMMITTED || + this.transaction.state === TxnState.TRANSACTION_ABORTED + ) { + return result; + } + } catch (fnError) { + // 7. If the callback reported an error + if (!(fnError instanceof MongoError) || fnError instanceof MongoInvalidArgumentError) { + // This first preemptive abort regardless of TxnState isn't spec, + // and it's unclear whether it's serving a practical purpose, but this logic is OLD + await this.abortTransaction(); + throw fnError; + } + + lastError = fnError; + + // 7.1 If the ClientSession is in the "starting transaction" or "transaction in progress" + // state, invoke abortTransaction on the session. + if ( + this.transaction.state === TxnState.STARTING_TRANSACTION || + this.transaction.state === TxnState.TRANSACTION_IN_PROGRESS + ) { + await this.abortTransaction(); + } + + // 7.2 If the callback's error includes a "TransientTransactionError" label, jump back to step two. + if (fnError.hasErrorLabel(MongoErrorLabel.TransientTransactionError)) { + if (processTimeMS() >= deadline) { + throw makeTimeoutError(lastError, csotEnabled); + } + continue retryTransaction; + } + + // 7.3 If the callback's error includes a "UnknownTransactionCommitResult" label, the callback must + // have manually committed a transaction, propagate the callback's error to the caller of withTransaction + // as is and return immediately. + // 7.4 Otherwise, propagate the callback's error to the caller of withTransaction as is and return immediately. + throw fnError; + } + + retryCommit: while (!committed) { + try { + // 9. Invoke commitTransaction on the session. + await this.commitTransaction(); + committed = true; + } catch (commitError) { + // 10. If commitTransaction reported an error: + lastError = commitError; + + // 10.1 If the commitTransaction error includes a UnknownTransactionCommitResult label and the error is + // not MaxTimeMSExpired + if ( + commitError.hasErrorLabel(MongoErrorLabel.UnknownTransactionCommitResult) && + !isMaxTimeMSExpiredError(commitError) + ) { + // 10.1.1 If the elapsed time of withTransaction exceeded TIMEOUT_MS, propagate the commitTransaction + // error to the caller of withTransaction as per timeout error propagation and return immediately. + if (processTimeMS() >= deadline) { + throw makeTimeoutError(commitError, csotEnabled); + } + // 10.1.2 Otherwise, jump back to step nine. We will trust commitTransaction to apply a majority write + // concern on retry attempts (see: Majority write concern is used when retrying commitTransaction). + continue retryCommit; + } + + // 10.2 If the commitTransaction error includes a TransientTransactionError label, jump back to step two. + if (commitError.hasErrorLabel(MongoErrorLabel.TransientTransactionError)) { + continue retryTransaction; + } + + // 10.3 Otherwise, propagate the commitTransaction error to the caller of withTransaction as is and return + // immediately. + throw commitError; + } + } + } + + // 11. The transaction was committed successfully. Return immediately. + // @ts-expect-error Result is always defined if we reach here, the for-loop above convinces TS it is not. + return result; + } finally { + this.timeoutContext = null; + } + } +} + +function makeTimeoutError(cause: Error, csotEnabled: boolean): Error { + // Async APIs know how to cancel themselves and might return CSOT error + if (cause instanceof MongoOperationTimeoutError) { + return cause; + } + if (csotEnabled) { + const timeoutError = new MongoOperationTimeoutError('Timed out during withTransaction', { + cause + }); + if (cause instanceof MongoError) { + for (const label of cause.errorLabels) { + timeoutError.addErrorLabel(label); + } + } + return timeoutError; + } + return cause; +} + +const NON_DETERMINISTIC_WRITE_CONCERN_ERRORS = new Set([ + 'CannotSatisfyWriteConcern', + 'UnknownReplWriteConcern', + 'UnsatisfiableWriteConcern' +]); + +function shouldUnpinAfterCommitError(commitError: Error) { + if (commitError instanceof MongoError) { + if ( + isRetryableWriteError(commitError) || + commitError instanceof MongoWriteConcernError || + isMaxTimeMSExpiredError(commitError) + ) { + if (isUnknownTransactionCommitResult(commitError)) { + // per txns spec, must unpin session in this case + return true; + } + } else if (commitError.hasErrorLabel(MongoErrorLabel.TransientTransactionError)) { + return true; + } + } + return false; +} + +function shouldAddUnknownTransactionCommitResultLabel(commitError: MongoError) { + let ok = isRetryableWriteError(commitError); + ok ||= commitError instanceof MongoWriteConcernError; + ok ||= isMaxTimeMSExpiredError(commitError); + ok &&= isUnknownTransactionCommitResult(commitError); + return ok; +} + +function isUnknownTransactionCommitResult(err: MongoError): err is MongoError { + const isNonDeterministicWriteConcernError = + err instanceof MongoServerError && + err.codeName && + NON_DETERMINISTIC_WRITE_CONCERN_ERRORS.has(err.codeName); + + return ( + isMaxTimeMSExpiredError(err) || + (!isNonDeterministicWriteConcernError && + err.code !== MONGODB_ERROR_CODES.UnsatisfiableWriteConcern && + err.code !== MONGODB_ERROR_CODES.UnknownReplWriteConcern) + ); +} + +export function maybeClearPinnedConnection( + session: ClientSession, + options?: EndSessionOptions +): void { + // unpin a connection if it has been pinned + const conn = session.pinnedConnection; + const error = options?.error; + + if ( + session.inTransaction() && + error && + error instanceof MongoError && + error.hasErrorLabel(MongoErrorLabel.TransientTransactionError) + ) { + return; + } + + const topology = session.client.topology; + // NOTE: the spec talks about what to do on a network error only, but the tests seem to + // to validate that we don't unpin on _all_ errors? + if (conn && topology != null) { + const servers = Array.from(topology.s.servers.values()); + const loadBalancer = servers[0]; + + if (options?.error == null || options?.force) { + loadBalancer.pool.checkIn(conn); + session.pinnedConnection = undefined; + conn.emit( + UNPINNED, + session.transaction.state !== TxnState.NO_TRANSACTION + ? ConnectionPoolMetrics.TXN + : ConnectionPoolMetrics.CURSOR + ); + + if (options?.forceClear) { + loadBalancer.pool.clear({ serviceId: conn.serviceId }); + } + } + } +} + +function isMaxTimeMSExpiredError(err: MongoError): boolean { + if (err == null || !(err instanceof MongoServerError)) { + return false; + } + + return ( + err.code === MONGODB_ERROR_CODES.MaxTimeMSExpired || + err.writeConcernError?.code === MONGODB_ERROR_CODES.MaxTimeMSExpired + ); +} + +/** @public */ +export type ServerSessionId = { id: Binary }; + +/** + * Reflects the existence of a session on the server. Can be reused by the session pool. + * WARNING: not meant to be instantiated directly. For internal use only. + * @public + */ +export class ServerSession { + id: ServerSessionId; + lastUse: number; + txnNumber: number; + isDirty: boolean; + + /** @internal */ + constructor(cloned?: ServerSession | null) { + if (cloned != null) { + const idBytes = ByteUtils.allocateUnsafe(16); + idBytes.set(cloned.id.id.buffer); + this.id = { id: new Binary(idBytes, cloned.id.id.sub_type) }; + this.lastUse = cloned.lastUse; + this.txnNumber = cloned.txnNumber; + this.isDirty = cloned.isDirty; + return; + } + this.id = { id: new Binary(uuidV4(), Binary.SUBTYPE_UUID) }; + this.lastUse = processTimeMS(); + this.txnNumber = 0; + this.isDirty = false; + } + + /** + * Determines if the server session has timed out. + * + * @param sessionTimeoutMinutes - The server's "logicalSessionTimeoutMinutes" + */ + hasTimedOut(sessionTimeoutMinutes: number): boolean { + // Take the difference of the lastUse timestamp and now, which will result in a value in + // milliseconds, and then convert milliseconds to minutes to compare to `sessionTimeoutMinutes` + const idleTimeMinutes = Math.round( + ((calculateDurationInMs(this.lastUse) % 86400000) % 3600000) / 60000 + ); + + return idleTimeMinutes > sessionTimeoutMinutes - 1; + } +} + +/** + * Maintains a pool of Server Sessions. + * For internal use only + * @internal + */ +export class ServerSessionPool { + client: MongoClient; + sessions: List; + + constructor(client: MongoClient) { + if (client == null) { + throw new MongoRuntimeError('ServerSessionPool requires a MongoClient'); + } + + this.client = client; + this.sessions = new List(); + } + + /** + * Acquire a Server Session from the pool. + * Iterates through each session in the pool, removing any stale sessions + * along the way. The first non-stale session found is removed from the + * pool and returned. If no non-stale session is found, a new ServerSession is created. + */ + acquire(): ServerSession { + const sessionTimeoutMinutes = this.client.topology?.logicalSessionTimeoutMinutes ?? 10; + + let session: ServerSession | null = null; + + // Try to obtain from session pool + while (this.sessions.length > 0) { + const potentialSession = this.sessions.shift(); + if ( + potentialSession != null && + (!!this.client.topology?.loadBalanced || + !potentialSession.hasTimedOut(sessionTimeoutMinutes)) + ) { + session = potentialSession; + break; + } + } + + // If nothing valid came from the pool make a new one + if (session == null) { + session = new ServerSession(); + } + + return session; + } + + /** + * Release a session to the session pool + * Adds the session back to the session pool if the session has not timed out yet. + * This method also removes any stale sessions from the pool. + * + * @param session - The session to release to the pool + */ + release(session: ServerSession): void { + const sessionTimeoutMinutes = this.client.topology?.logicalSessionTimeoutMinutes ?? 10; + + if (this.client.topology?.loadBalanced && !sessionTimeoutMinutes) { + this.sessions.unshift(session); + } + + if (!sessionTimeoutMinutes) { + return; + } + + this.sessions.prune(session => session.hasTimedOut(sessionTimeoutMinutes)); + + if (!session.hasTimedOut(sessionTimeoutMinutes)) { + if (session.isDirty) { + return; + } + + // otherwise, readd this session to the session pool + this.sessions.unshift(session); + } + } +} + +/** + * Optionally decorate a command with sessions specific keys + * + * @param session - the session tracking transaction state + * @param command - the command to decorate + * @param options - Optional settings passed to calling operation + * + * @internal + */ +export function applySession( + session: ClientSession, + command: Document, + options: CommandOptions +): MongoDriverError | undefined { + if (session.hasEnded) { + return new MongoExpiredSessionError(); + } + + // May acquire serverSession here + const serverSession = session.serverSession; + if (serverSession == null) { + return new MongoRuntimeError('Unable to acquire server session'); + } + + if (options.writeConcern?.w === 0) { + if (session && session.explicit) { + // Error if user provided an explicit session to an unacknowledged write (SPEC-1019) + return new MongoAPIError('Cannot have explicit session with unacknowledged writes'); + } + return; + } + + // mark the last use of this session, and apply the `lsid` + serverSession.lastUse = processTimeMS(); + command.lsid = serverSession.id; + + const inTxnOrTxnCommand = session.inTransaction() || isTransactionCommand(command); + const isRetryableWrite = !!options.willRetryWrite; + + if (isRetryableWrite || inTxnOrTxnCommand) { + serverSession.txnNumber += session.txnNumberIncrement; + session.txnNumberIncrement = 0; + // TODO(NODE-2674): Preserve int64 sent from MongoDB + command.txnNumber = Long.fromNumber(serverSession.txnNumber); + } + + if (!inTxnOrTxnCommand) { + if (session.transaction.state !== TxnState.NO_TRANSACTION) { + session.transaction.transition(TxnState.NO_TRANSACTION); + } + + if ( + session.supports.causalConsistency && + session.operationTime && + commandSupportsAfterClusterTime(command) + ) { + command.readConcern = command.readConcern || {}; + Object.assign(command.readConcern, { afterClusterTime: session.operationTime }); + } else if (session.snapshotEnabled) { + command.readConcern = command.readConcern || { level: ReadConcernLevel.snapshot }; + if (session.snapshotTime != null) { + Object.assign(command.readConcern, { atClusterTime: session.snapshotTime }); + } + } + + return; + } + + // now attempt to apply transaction-specific sessions data + + // `autocommit` must always be false to differentiate from retryable writes + command.autocommit = false; + + if (session.transaction.state === TxnState.STARTING_TRANSACTION) { + command.startTransaction = true; + + const readConcern = + session.transaction.options.readConcern || session?.clientOptions?.readConcern; + if (readConcern) { + command.readConcern = readConcern; + } + + if (session.supports.causalConsistency && session.operationTime) { + command.readConcern = command.readConcern || {}; + Object.assign(command.readConcern, { afterClusterTime: session.operationTime }); + } + } + return; +} + +export function updateSessionFromResponse(session: ClientSession, document: MongoDBResponse): void { + if (document.$clusterTime) { + _advanceClusterTime(session, document.$clusterTime); + } + + if (document.operationTime && session && session.supports.causalConsistency) { + session.advanceOperationTime(document.operationTime); + } + + if (document.recoveryToken && session && session.inTransaction()) { + session.transaction._recoveryToken = document.recoveryToken; + } + + if (session?.snapshotEnabled && session.snapshotTime == null) { + // find and aggregate commands return atClusterTime on the cursor + // distinct includes it in the response body + const atClusterTime = document.atClusterTime; + if (atClusterTime) { + session.snapshotTime = atClusterTime; + } + } + + if (session.transaction.state === TxnState.STARTING_TRANSACTION) { + if (document.ok === 1) { + session.transaction.transition(TxnState.TRANSACTION_IN_PROGRESS); + } else { + const error = new MongoServerError(document.toObject()); + const isRetryableError = error.hasErrorLabel(MongoErrorLabel.RetryableError); + + if (!isRetryableError) { + session.transaction.transition(TxnState.TRANSACTION_IN_PROGRESS); + } + } + } +} diff --git a/gateway/node_modules/mongodb/src/sort.ts b/gateway/node_modules/mongodb/src/sort.ts new file mode 100644 index 0000000000000000000000000000000000000000..0b239ddd49425357091a204d5d921a43b8ddfa82 --- /dev/null +++ b/gateway/node_modules/mongodb/src/sort.ts @@ -0,0 +1,141 @@ +import { MongoInvalidArgumentError } from './error'; + +/** @public */ +export type SortDirection = + | 1 + | -1 + | 'asc' + | 'desc' + | 'ascending' + | 'descending' + | { readonly $meta: string }; + +/** @public */ +export type Sort = + | string + | Exclude + | ReadonlyArray + | { readonly [key: string]: SortDirection } + | ReadonlyMap + | ReadonlyArray + | readonly [string, SortDirection]; + +/** Below stricter types were created for sort that correspond with type that the cmd takes */ + +/** @public */ +export type SortDirectionForCmd = 1 | -1 | { $meta: string }; + +/** @public */ +export type SortForCmd = Map; + +/** @internal */ +type SortPairForCmd = [string, SortDirectionForCmd]; + +/** @internal */ +function prepareDirection(direction: any = 1): SortDirectionForCmd { + const value = `${direction}`.toLowerCase(); + if (isMeta(direction)) return direction; + switch (value) { + case 'ascending': + case 'asc': + case '1': + return 1; + case 'descending': + case 'desc': + case '-1': + return -1; + default: + throw new MongoInvalidArgumentError(`Invalid sort direction: ${JSON.stringify(direction)}`); + } +} + +/** @internal */ +function isMeta(t: SortDirection): t is { $meta: string } { + return typeof t === 'object' && t != null && '$meta' in t && typeof t.$meta === 'string'; +} + +/** @internal */ +function isPair(t: Sort): t is readonly [string, SortDirection] { + if (Array.isArray(t) && t.length === 2) { + try { + prepareDirection(t[1]); + return true; + } catch { + return false; + } + } + return false; +} + +function isDeep(t: Sort): t is ReadonlyArray { + return Array.isArray(t) && Array.isArray(t[0]); +} + +function isMap(t: Sort): t is ReadonlyMap { + return t instanceof Map && t.size > 0; +} + +function isReadonlyArray(value: any): value is readonly T[] { + return Array.isArray(value); +} + +/** @internal */ +function pairToMap(v: readonly [string, SortDirection]): SortForCmd { + return new Map([[`${v[0]}`, prepareDirection([v[1]])]]); +} + +/** @internal */ +function deepToMap(t: ReadonlyArray): SortForCmd { + const sortEntries: SortPairForCmd[] = t.map(([k, v]) => [`${k}`, prepareDirection(v)]); + return new Map(sortEntries); +} + +/** @internal */ +function stringsToMap(t: ReadonlyArray): SortForCmd { + const sortEntries: SortPairForCmd[] = t.map(key => [`${key}`, 1]); + return new Map(sortEntries); +} + +/** @internal */ +function objectToMap(t: { readonly [key: string]: SortDirection }): SortForCmd { + const sortEntries: SortPairForCmd[] = Object.entries(t).map(([k, v]) => [ + `${k}`, + prepareDirection(v) + ]); + return new Map(sortEntries); +} + +/** @internal */ +function mapToMap(t: ReadonlyMap): SortForCmd { + const sortEntries: SortPairForCmd[] = Array.from(t).map(([k, v]) => [ + `${k}`, + prepareDirection(v) + ]); + return new Map(sortEntries); +} + +/** converts a Sort type into a type that is valid for the server (SortForCmd) */ +export function formatSort( + sort: Sort | undefined, + direction?: SortDirection +): SortForCmd | undefined { + if (sort == null) return undefined; + + if (typeof sort === 'string') return new Map([[sort, prepareDirection(direction)]]); // 'fieldName' + + if (typeof sort !== 'object') { + throw new MongoInvalidArgumentError( + `Invalid sort format: ${JSON.stringify(sort)} Sort must be a valid object` + ); + } + + if (!isReadonlyArray(sort)) { + if (isMap(sort)) return mapToMap(sort); // Map + if (Object.keys(sort).length) return objectToMap(sort); // { [fieldName: string]: SortDirection } + return undefined; + } + if (!sort.length) return undefined; + if (isDeep(sort)) return deepToMap(sort); // [ [fieldName, sortDir], [fieldName, sortDir] ... ] + if (isPair(sort)) return pairToMap(sort); // [ fieldName, sortDir ] + return stringsToMap(sort); // [ fieldName, fieldName ] +} diff --git a/gateway/node_modules/mongodb/src/timeout.ts b/gateway/node_modules/mongodb/src/timeout.ts new file mode 100644 index 0000000000000000000000000000000000000000..26e04943f6a492c1c2563523b081715ee3434ac5 --- /dev/null +++ b/gateway/node_modules/mongodb/src/timeout.ts @@ -0,0 +1,405 @@ +import { clearTimeout, setTimeout } from 'timers'; + +import { type Document } from './bson'; +import { MongoInvalidArgumentError, MongoOperationTimeoutError, MongoRuntimeError } from './error'; +import { type ClientSession } from './sessions'; +import { csotMin, noop, squashError } from './utils'; + +/** @internal */ +export class TimeoutError extends Error { + duration: number; + override get name(): 'TimeoutError' { + return 'TimeoutError'; + } + + constructor(message: string, options: { cause?: Error; duration: number }) { + super(message, options); + this.duration = options.duration; + } + + static is(error: unknown): error is TimeoutError { + return ( + error != null && typeof error === 'object' && 'name' in error && error.name === 'TimeoutError' + ); + } +} + +type Executor = ConstructorParameters>[0]; +type Reject = Parameters>[0]>[1]; +/** + * @internal + * This class is an abstraction over timeouts + * The Timeout class can only be in the pending or rejected states. It is guaranteed not to resolve + * if interacted with exclusively through its public API + * */ +export class Timeout extends Promise { + private id?: NodeJS.Timeout; + + public readonly start: number; + public ended: number | null = null; + public duration: number; + private timedOut = false; + public cleared = false; + + get remainingTime(): number { + if (this.timedOut) return 0; + if (this.duration === 0) return Infinity; + return this.start + this.duration - Math.trunc(performance.now()); + } + + get timeElapsed(): number { + return Math.trunc(performance.now()) - this.start; + } + + /** Create a new timeout that expires in `duration` ms */ + private constructor( + executor: Executor = () => null, + options?: { duration: number; unref?: true; rejection?: Error } + ) { + const duration = options?.duration ?? 0; + const unref = !!options?.unref; + const rejection = options?.rejection; + + if (duration < 0) { + throw new MongoInvalidArgumentError('Cannot create a Timeout with a negative duration'); + } + + let reject!: Reject; + super((_, promiseReject) => { + reject = promiseReject; + + executor(noop, promiseReject); + }); + + this.duration = duration; + this.start = Math.trunc(performance.now()); + + if (rejection == null && this.duration > 0) { + this.id = setTimeout(() => { + this.ended = Math.trunc(performance.now()); + this.timedOut = true; + reject(new TimeoutError(`Expired after ${duration}ms`, { duration })); + }, this.duration); + if (typeof this.id.unref === 'function' && unref) { + // Ensure we do not keep the Node.js event loop running + this.id.unref(); + } + } else if (rejection != null) { + this.ended = Math.trunc(performance.now()); + this.timedOut = true; + reject(rejection); + } + } + + /** + * Clears the underlying timeout. This method is idempotent + */ + clear(): void { + clearTimeout(this.id); + this.id = undefined; + this.timedOut = false; + this.cleared = true; + } + + throwIfExpired(): void { + if (this.timedOut) { + // This method is invoked when someone wants to throw immediately instead of await the result of this promise + // Since they won't be handling the rejection from the promise (because we're about to throw here) + // attach handling to prevent this from bubbling up to Node.js + this.then(undefined, squashError); + throw new TimeoutError('Timed out', { duration: this.duration }); + } + } + + public static expires(duration: number, unref?: true): Timeout { + return new Timeout(undefined, { duration, unref }); + } + + static override reject(rejection?: Error): Timeout { + return new Timeout(undefined, { duration: 0, unref: true, rejection }); + } +} + +/** @internal */ +export type TimeoutContextOptions = (LegacyTimeoutContextOptions | CSOTTimeoutContextOptions) & { + session?: ClientSession; +}; + +/** @internal */ +export type LegacyTimeoutContextOptions = { + serverSelectionTimeoutMS: number; + waitQueueTimeoutMS: number; + socketTimeoutMS?: number; +}; + +/** @internal */ +export type CSOTTimeoutContextOptions = { + timeoutMS: number; + serverSelectionTimeoutMS: number; + socketTimeoutMS?: number; +}; + +function isLegacyTimeoutContextOptions(v: unknown): v is LegacyTimeoutContextOptions { + return ( + v != null && + typeof v === 'object' && + 'serverSelectionTimeoutMS' in v && + typeof v.serverSelectionTimeoutMS === 'number' && + 'waitQueueTimeoutMS' in v && + typeof v.waitQueueTimeoutMS === 'number' + ); +} + +function isCSOTTimeoutContextOptions(v: unknown): v is CSOTTimeoutContextOptions { + return ( + v != null && + typeof v === 'object' && + 'serverSelectionTimeoutMS' in v && + typeof v.serverSelectionTimeoutMS === 'number' && + 'timeoutMS' in v && + typeof v.timeoutMS === 'number' + ); +} + +/** @internal */ +export abstract class TimeoutContext { + static create(options: TimeoutContextOptions): TimeoutContext { + if (options.session?.timeoutContext != null) return options.session?.timeoutContext; + if (isCSOTTimeoutContextOptions(options)) return new CSOTTimeoutContext(options); + else if (isLegacyTimeoutContextOptions(options)) return new LegacyTimeoutContext(options); + else throw new MongoRuntimeError('Unrecognized options'); + } + + abstract get maxTimeMS(): number | null; + + abstract get serverSelectionTimeout(): Timeout | null; + + abstract get connectionCheckoutTimeout(): Timeout | null; + + abstract get clearServerSelectionTimeout(): boolean; + + abstract get timeoutForSocketWrite(): Timeout | null; + + abstract get timeoutForSocketRead(): Timeout | null; + + abstract csotEnabled(): this is CSOTTimeoutContext; + + abstract refresh(): void; + + abstract clear(): void; + + /** Returns a new instance of the TimeoutContext, with all timeouts refreshed and restarted. */ + abstract refreshed(): TimeoutContext; + + abstract addMaxTimeMSToCommand(command: Document, options: { omitMaxTimeMS?: boolean }): void; + + abstract getSocketTimeoutMS(): number | undefined; +} + +/** @internal */ +export class CSOTTimeoutContext extends TimeoutContext { + timeoutMS: number; + serverSelectionTimeoutMS: number; + socketTimeoutMS?: number; + + clearServerSelectionTimeout: boolean; + + private _serverSelectionTimeout?: Timeout | null; + private _connectionCheckoutTimeout?: Timeout | null; + public minRoundTripTime = 0; + public start: number; + + constructor(options: CSOTTimeoutContextOptions) { + super(); + this.start = Math.trunc(performance.now()); + + this.timeoutMS = options.timeoutMS; + + this.serverSelectionTimeoutMS = options.serverSelectionTimeoutMS; + + this.socketTimeoutMS = options.socketTimeoutMS; + + this.clearServerSelectionTimeout = false; + } + + get maxTimeMS(): number { + return this.remainingTimeMS - this.minRoundTripTime; + } + + get remainingTimeMS() { + const timePassed = Math.trunc(performance.now()) - this.start; + return this.timeoutMS <= 0 ? Infinity : this.timeoutMS - timePassed; + } + + csotEnabled(): this is CSOTTimeoutContext { + return true; + } + + get serverSelectionTimeout(): Timeout | null { + // check for undefined + if (typeof this._serverSelectionTimeout !== 'object' || this._serverSelectionTimeout?.cleared) { + const { remainingTimeMS, serverSelectionTimeoutMS } = this; + if (remainingTimeMS <= 0) + return Timeout.reject( + new MongoOperationTimeoutError(`Timed out in server selection after ${this.timeoutMS}ms`) + ); + const usingServerSelectionTimeoutMS = + serverSelectionTimeoutMS !== 0 && + csotMin(remainingTimeMS, serverSelectionTimeoutMS) === serverSelectionTimeoutMS; + if (usingServerSelectionTimeoutMS) { + this._serverSelectionTimeout = Timeout.expires(serverSelectionTimeoutMS); + } else { + if (remainingTimeMS > 0 && Number.isFinite(remainingTimeMS)) { + this._serverSelectionTimeout = Timeout.expires(remainingTimeMS); + } else { + this._serverSelectionTimeout = null; + } + } + } + + return this._serverSelectionTimeout; + } + + get connectionCheckoutTimeout(): Timeout | null { + if ( + typeof this._connectionCheckoutTimeout !== 'object' || + this._connectionCheckoutTimeout?.cleared + ) { + if (typeof this._serverSelectionTimeout === 'object') { + // null or Timeout + this._connectionCheckoutTimeout = this._serverSelectionTimeout; + } else { + throw new MongoRuntimeError( + 'Unreachable. If you are seeing this error, please file a ticket on the NODE driver project on Jira' + ); + } + } + return this._connectionCheckoutTimeout; + } + + get timeoutForSocketWrite(): Timeout | null { + const { remainingTimeMS } = this; + if (!Number.isFinite(remainingTimeMS)) return null; + if (remainingTimeMS > 0) return Timeout.expires(remainingTimeMS); + return Timeout.reject(new MongoOperationTimeoutError('Timed out before socket write')); + } + + get timeoutForSocketRead(): Timeout | null { + const { remainingTimeMS } = this; + if (!Number.isFinite(remainingTimeMS)) return null; + if (remainingTimeMS > 0) return Timeout.expires(remainingTimeMS); + return Timeout.reject(new MongoOperationTimeoutError('Timed out before socket read')); + } + + refresh(): void { + this.start = Math.trunc(performance.now()); + this.minRoundTripTime = 0; + this._serverSelectionTimeout?.clear(); + this._connectionCheckoutTimeout?.clear(); + } + + clear(): void { + this._serverSelectionTimeout?.clear(); + this._connectionCheckoutTimeout?.clear(); + } + + /** + * @internal + * Throws a MongoOperationTimeoutError if the context has expired. + * If the context has not expired, returns the `remainingTimeMS` + **/ + getRemainingTimeMSOrThrow(message?: string): number { + const { remainingTimeMS } = this; + if (remainingTimeMS <= 0) + throw new MongoOperationTimeoutError(message ?? `Expired after ${this.timeoutMS}ms`); + return remainingTimeMS; + } + + /** + * @internal + * This method is intended to be used in situations where concurrent operation are on the same deadline, but cannot share a single `TimeoutContext` instance. + * Returns a new instance of `CSOTTimeoutContext` constructed with identical options, but setting the `start` property to `this.start`. + */ + clone(): CSOTTimeoutContext { + const timeoutContext = new CSOTTimeoutContext({ + timeoutMS: this.timeoutMS, + serverSelectionTimeoutMS: this.serverSelectionTimeoutMS + }); + timeoutContext.start = this.start; + return timeoutContext; + } + + override refreshed(): CSOTTimeoutContext { + return new CSOTTimeoutContext(this); + } + + override addMaxTimeMSToCommand(command: Document, options: { omitMaxTimeMS?: boolean }): void { + if (options.omitMaxTimeMS) return; + const maxTimeMS = this.remainingTimeMS - this.minRoundTripTime; + if (maxTimeMS > 0 && Number.isFinite(maxTimeMS)) command.maxTimeMS = maxTimeMS; + } + + override getSocketTimeoutMS(): number | undefined { + return 0; + } +} + +/** @internal */ +export class LegacyTimeoutContext extends TimeoutContext { + options: LegacyTimeoutContextOptions; + clearServerSelectionTimeout: boolean; + + constructor(options: LegacyTimeoutContextOptions) { + super(); + this.options = options; + this.clearServerSelectionTimeout = true; + } + + csotEnabled(): this is CSOTTimeoutContext { + return false; + } + + get serverSelectionTimeout(): Timeout | null { + if (this.options.serverSelectionTimeoutMS != null && this.options.serverSelectionTimeoutMS > 0) + return Timeout.expires(this.options.serverSelectionTimeoutMS); + return null; + } + + get connectionCheckoutTimeout(): Timeout | null { + if (this.options.waitQueueTimeoutMS != null && this.options.waitQueueTimeoutMS > 0) + return Timeout.expires(this.options.waitQueueTimeoutMS); + return null; + } + + get timeoutForSocketWrite(): Timeout | null { + return null; + } + + get timeoutForSocketRead(): Timeout | null { + return null; + } + + refresh(): void { + return; + } + + clear(): void { + return; + } + + get maxTimeMS() { + return null; + } + + override refreshed(): LegacyTimeoutContext { + return new LegacyTimeoutContext(this.options); + } + + override addMaxTimeMSToCommand(_command: Document, _options: { omitMaxTimeMS?: boolean }): void { + // No max timeMS is added to commands in legacy timeout mode. + } + + override getSocketTimeoutMS(): number | undefined { + return this.options.socketTimeoutMS; + } +} diff --git a/gateway/node_modules/mongodb/src/transactions.ts b/gateway/node_modules/mongodb/src/transactions.ts new file mode 100644 index 0000000000000000000000000000000000000000..a9cfd9cf27355186069b7827177e3a86b3f45916 --- /dev/null +++ b/gateway/node_modules/mongodb/src/transactions.ts @@ -0,0 +1,181 @@ +import type { Document } from './bson'; +import { MongoRuntimeError, MongoTransactionError } from './error'; +import type { CommandOperationOptions } from './operations/command'; +import { ReadConcern, type ReadConcernLike } from './read_concern'; +import { ReadPreference, type ReadPreferenceLike } from './read_preference'; +import type { Server } from './sdam/server'; +import { WriteConcern } from './write_concern'; + +/** @internal */ +export const TxnState = Object.freeze({ + NO_TRANSACTION: 'NO_TRANSACTION', + STARTING_TRANSACTION: 'STARTING_TRANSACTION', + TRANSACTION_IN_PROGRESS: 'TRANSACTION_IN_PROGRESS', + TRANSACTION_COMMITTED: 'TRANSACTION_COMMITTED', + TRANSACTION_COMMITTED_EMPTY: 'TRANSACTION_COMMITTED_EMPTY', + TRANSACTION_ABORTED: 'TRANSACTION_ABORTED' +} as const); + +/** @internal */ +export type TxnState = (typeof TxnState)[keyof typeof TxnState]; + +const stateMachine: { [state in TxnState]: TxnState[] } = { + [TxnState.NO_TRANSACTION]: [TxnState.NO_TRANSACTION, TxnState.STARTING_TRANSACTION], + [TxnState.STARTING_TRANSACTION]: [ + TxnState.TRANSACTION_IN_PROGRESS, + TxnState.TRANSACTION_COMMITTED, + TxnState.TRANSACTION_COMMITTED_EMPTY, + TxnState.TRANSACTION_ABORTED + ], + [TxnState.TRANSACTION_IN_PROGRESS]: [ + TxnState.TRANSACTION_IN_PROGRESS, + TxnState.TRANSACTION_COMMITTED, + TxnState.TRANSACTION_ABORTED + ], + [TxnState.TRANSACTION_COMMITTED]: [ + TxnState.TRANSACTION_COMMITTED, + TxnState.TRANSACTION_COMMITTED_EMPTY, + TxnState.STARTING_TRANSACTION, + TxnState.NO_TRANSACTION + ], + [TxnState.TRANSACTION_ABORTED]: [TxnState.STARTING_TRANSACTION, TxnState.NO_TRANSACTION], + [TxnState.TRANSACTION_COMMITTED_EMPTY]: [ + TxnState.TRANSACTION_COMMITTED_EMPTY, + TxnState.NO_TRANSACTION + ] +}; + +const ACTIVE_STATES: Set = new Set([ + TxnState.STARTING_TRANSACTION, + TxnState.TRANSACTION_IN_PROGRESS +]); + +const COMMITTED_STATES: Set = new Set([ + TxnState.TRANSACTION_COMMITTED, + TxnState.TRANSACTION_COMMITTED_EMPTY, + TxnState.TRANSACTION_ABORTED +]); + +/** + * Configuration options for a transaction. + * @public + */ +export interface TransactionOptions extends Omit { + // TODO(NODE-3344): These options use the proper class forms of these settings, it should accept the basic enum values too + /** A default read concern for commands in this transaction */ + readConcern?: ReadConcernLike; + /** A default writeConcern for commands in this transaction */ + writeConcern?: WriteConcern; + /** A default read preference for commands in this transaction */ + readPreference?: ReadPreferenceLike; + /** Specifies the maximum amount of time to allow a commit action on a transaction to run in milliseconds */ + maxCommitTimeMS?: number; +} + +/** + * @internal + */ +export class Transaction { + state: TxnState; + options: TransactionOptions; + _pinnedServer?: Server; + _recoveryToken?: Document; + + /** Create a transaction */ + constructor(options?: TransactionOptions) { + options = options ?? {}; + this.state = TxnState.NO_TRANSACTION; + this.options = {}; + + const writeConcern = WriteConcern.fromOptions(options); + if (writeConcern) { + if (writeConcern.w === 0) { + throw new MongoTransactionError('Transactions do not support unacknowledged write concern'); + } + + this.options.writeConcern = writeConcern; + } + + if (options.readConcern) { + this.options.readConcern = ReadConcern.fromOptions(options); + } + + if (options.readPreference) { + this.options.readPreference = ReadPreference.fromOptions(options); + } + + if (options.maxCommitTimeMS) { + this.options.maxTimeMS = options.maxCommitTimeMS; + } + + // TODO: This isn't technically necessary + this._pinnedServer = undefined; + this._recoveryToken = undefined; + } + + get server(): Server | undefined { + return this._pinnedServer; + } + + get recoveryToken(): Document | undefined { + return this._recoveryToken; + } + + get isPinned(): boolean { + return !!this.server; + } + + /** + * @returns Whether the transaction has started + */ + get isStarting(): boolean { + return this.state === TxnState.STARTING_TRANSACTION; + } + + /** + * @returns Whether this session is presently in a transaction + */ + get isActive(): boolean { + return ACTIVE_STATES.has(this.state); + } + + get isCommitted(): boolean { + return COMMITTED_STATES.has(this.state); + } + /** + * Transition the transaction in the state machine + * @param nextState - The new state to transition to + */ + transition(nextState: TxnState): void { + const nextStates = stateMachine[this.state]; + if (nextStates && nextStates.includes(nextState)) { + this.state = nextState; + if ( + this.state === TxnState.NO_TRANSACTION || + this.state === TxnState.STARTING_TRANSACTION || + this.state === TxnState.TRANSACTION_ABORTED + ) { + this.unpinServer(); + } + return; + } + + throw new MongoRuntimeError( + `Attempted illegal state transition from [${this.state}] to [${nextState}]` + ); + } + + pinServer(server: Server): void { + if (this.isActive) { + this._pinnedServer = server; + } + } + + unpinServer(): void { + this._pinnedServer = undefined; + } +} + +export function isTransactionCommand(command: Document): boolean { + return !!(command.commitTransaction || command.abortTransaction); +} diff --git a/gateway/node_modules/mongodb/src/utils.ts b/gateway/node_modules/mongodb/src/utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..50c0ddf0a201972d6e0523beb1a95c236978bf28 --- /dev/null +++ b/gateway/node_modules/mongodb/src/utils.ts @@ -0,0 +1,1456 @@ +import type { SrvRecord } from 'dns'; +import { type EventEmitter } from 'events'; +import { promises as fs } from 'fs'; +import * as http from 'http'; +import * as process from 'process'; +import { clearTimeout, setTimeout } from 'timers'; + +import { + ByteUtils, + deserialize, + type Document, + NumberUtils, + ObjectId, + resolveBSONOptions +} from './bson'; +import type { Connection } from './cmap/connection'; +import { MAX_SUPPORTED_WIRE_VERSION } from './cmap/wire_protocol/constants'; +import type { Collection } from './collection'; +import { kDecoratedKeys, LEGACY_HELLO_COMMAND } from './constants'; +import type { AbstractCursor } from './cursor/abstract_cursor'; +import type { FindCursor } from './cursor/find_cursor'; +import type { Db } from './db'; +import { + type AnyError, + MongoAPIError, + MongoInvalidArgumentError, + MongoNetworkTimeoutError, + MongoNotConnectedError, + MongoParseError, + MongoRuntimeError +} from './error'; +import type { MongoClient } from './mongo_client'; +import { type Abortable } from './mongo_types'; +import type { CommandOperationOptions, OperationParent } from './operations/command'; +import type { Hint, OperationOptions } from './operations/operation'; +import { ReadConcern } from './read_concern'; +import { ReadPreference } from './read_preference'; +import { ServerType } from './sdam/common'; +import type { Server } from './sdam/server'; +import type { Topology } from './sdam/topology'; +import type { ClientSession } from './sessions'; +import { type TimeoutContextOptions } from './timeout'; +import { WriteConcern } from './write_concern'; + +/** + * MongoDB Driver style callback + * @public + */ +export type Callback = (error?: AnyError, result?: T) => void; + +export type AnyOptions = Document; + +/** + * Returns true if value is a Uint8Array or a Buffer + * @param value - any value that may be a Uint8Array + */ +export function isUint8Array(value: unknown): value is Uint8Array { + return ( + value != null && + typeof value === 'object' && + Symbol.toStringTag in value && + value[Symbol.toStringTag] === 'Uint8Array' + ); +} + +/** + * Determines if a connection's address matches a user provided list + * of domain wildcards. + */ +export function hostMatchesWildcards(host: string, wildcards: string[]): boolean { + for (const wildcard of wildcards) { + // Exact match always wins + if (host === wildcard) { + return true; + } + + // Wildcard match with leading *. + if (wildcard.startsWith('*.')) { + const suffix = wildcard.substring(2); + // Exact match or strict subdomain match + if (host === suffix || host.endsWith(`.${suffix}`)) { + return true; + } + } + // Wildcard match with leading */ + if (wildcard.startsWith('*/')) { + const suffix = wildcard.substring(2); + // Exact match or strict subpath match + if (host === suffix || host.endsWith(`/${suffix}`)) { + return true; + } + } + } + return false; +} + +/** + * Ensure Hint field is in a shape we expect: + * - object of index names mapping to 1 or -1 + * - just an index name + * @internal + */ +export function normalizeHintField(hint?: Hint): Hint | undefined { + let finalHint = undefined; + + if (typeof hint === 'string') { + finalHint = hint; + } else if (Array.isArray(hint)) { + finalHint = {}; + + hint.forEach(param => { + finalHint[param] = 1; + }); + } else if (hint != null && typeof hint === 'object') { + finalHint = {} as Document; + for (const name in hint) { + finalHint[name] = hint[name]; + } + } + + return finalHint; +} + +const TO_STRING = (object: unknown) => Object.prototype.toString.call(object); +/** + * Checks if arg is an Object: + * - **NOTE**: the check is based on the `[Symbol.toStringTag]() === 'Object'` + * @internal + */ + +export function isObject(arg: unknown): arg is object { + return '[object Object]' === TO_STRING(arg); +} + +/** @internal */ +export function mergeOptions(target: T, source: S): T & S { + return { ...target, ...source }; +} + +/** @internal */ +export function filterOptions(options: AnyOptions, names: ReadonlyArray): AnyOptions { + const filterOptions: AnyOptions = {}; + + for (const name in options) { + if (names.includes(name)) { + filterOptions[name] = options[name]; + } + } + + // Filtered options + return filterOptions; +} + +/** + * Applies a write concern to a command based on well defined inheritance rules, optionally + * detecting support for the write concern in the first place. + * @internal + * + * @param target - the target command we will be applying the write concern to + * @param sources - sources where we can inherit default write concerns from + * @param options - optional settings passed into a command for write concern overrides + */ + +/** + * Checks if a given value is a Promise + * + * @typeParam T - The resolution type of the possible promise + * @param value - An object that could be a promise + * @returns true if the provided value is a Promise + */ +export function isPromiseLike(value?: unknown): value is PromiseLike { + return ( + value != null && + typeof value === 'object' && + 'then' in value && + typeof value.then === 'function' + ); +} + +/** + * Applies collation to a given command. + * @internal + * + * @param command - the command on which to apply collation + * @param target - target of command + * @param options - options containing collation settings + */ +export function decorateWithCollation(command: Document, options: AnyOptions): void { + if (options.collation && typeof options.collation === 'object') { + command.collation = options.collation; + } +} + +/** + * Applies a read concern to a given command. + * @internal + * + * @param command - the command on which to apply the read concern + * @param coll - the parent collection of the operation calling this method + */ +export function decorateWithReadConcern( + command: Document, + coll: { s: { readConcern?: ReadConcern } }, + options?: OperationOptions +): void { + if (options && options.session && options.session.inTransaction()) { + return; + } + const readConcern = Object.assign({}, command.readConcern || {}); + if (coll.s.readConcern) { + Object.assign(readConcern, coll.s.readConcern); + } + + if (Object.keys(readConcern).length > 0) { + Object.assign(command, { readConcern: readConcern }); + } +} + +/** + * @internal + */ +export type TopologyProvider = + | MongoClient + | ClientSession + | FindCursor + | AbstractCursor + | Collection + | Db; + +/** + * A helper function to get the topology from a given provider. Throws + * if the topology cannot be found. + * @throws MongoNotConnectedError + * @internal + */ +export function getTopology(provider: TopologyProvider): Topology { + // MongoClient or ClientSession or AbstractCursor + if ('topology' in provider && provider.topology) { + return provider.topology; + } else if ('client' in provider && provider.client.topology) { + return provider.client.topology; + } + + throw new MongoNotConnectedError('MongoClient must be connected to perform this operation'); +} + +/** @internal */ +export function ns(ns: string): MongoDBNamespace { + return MongoDBNamespace.fromString(ns); +} + +/** @public */ +export class MongoDBNamespace { + db: string; + collection?: string; + /** + * Create a namespace object + * + * @param db - database name + * @param collection - collection name + */ + constructor(db: string, collection?: string) { + this.db = db; + this.collection = collection === '' ? undefined : collection; + } + + toString(): string { + return this.collection ? `${this.db}.${this.collection}` : this.db; + } + + withCollection(collection: string): MongoDBCollectionNamespace { + return new MongoDBCollectionNamespace(this.db, collection); + } + + static fromString(namespace?: string): MongoDBNamespace { + if (typeof namespace !== 'string' || namespace === '') { + // TODO(NODE-3483): Replace with MongoNamespaceError + throw new MongoRuntimeError(`Cannot parse namespace from "${namespace}"`); + } + + const [db, ...collectionParts] = namespace.split('.'); + const collection = collectionParts.join('.'); + return new MongoDBNamespace(db, collection === '' ? undefined : collection); + } +} + +/** + * @public + * + * A class representing a collection's namespace. This class enforces (through Typescript) that + * the `collection` portion of the namespace is defined and should only be + * used in scenarios where this can be guaranteed. + */ +export class MongoDBCollectionNamespace extends MongoDBNamespace { + override collection: string; + + constructor(db: string, collection: string) { + super(db, collection); + this.collection = collection; + } + + static override fromString(namespace?: string): MongoDBCollectionNamespace { + return super.fromString(namespace) as MongoDBCollectionNamespace; + } +} + +/** @internal */ +export function* makeCounter(seed = 0): Generator { + let count = seed; + while (true) { + const newCount = count; + count += 1; + yield newCount; + } +} + +/** + * Synchronously Generate a UUIDv4 + * @internal + */ +export function uuidV4(): Uint8Array { + const result = crypto.getRandomValues(new Uint8Array(16)); + result[6] = (result[6] & 0x0f) | 0x40; + result[8] = (result[8] & 0x3f) | 0x80; + return result; +} + +/** + * A helper function for determining `maxWireVersion` between legacy and new topology instances + * @internal + */ +export function maxWireVersion(handshakeAware?: Connection | Topology | Server): number { + if (handshakeAware) { + if (handshakeAware.hello) { + return handshakeAware.hello.maxWireVersion; + } + + if (handshakeAware.serverApi?.version) { + // We return the max supported wire version for serverAPI. + return MAX_SUPPORTED_WIRE_VERSION; + } + // This is the fallback case for load balanced mode. If we are building commands the + // object being checked will be a connection, and we will have a hello response on + // it. For other cases, such as retryable writes, the object will be a server or + // topology, and there will be no hello response on those objects, so we return + // the max wire version so we support retryability. Once we have a min supported + // wire version of 9, then the needsRetryableWriteLabel() check can remove the + // usage of passing the wire version into it. + if (handshakeAware.loadBalanced) { + return MAX_SUPPORTED_WIRE_VERSION; + } + + if ('lastHello' in handshakeAware && typeof handshakeAware.lastHello === 'function') { + const lastHello = handshakeAware.lastHello(); + if (lastHello) { + return lastHello.maxWireVersion; + } + } + + if ( + handshakeAware.description && + 'maxWireVersion' in handshakeAware.description && + handshakeAware.description.maxWireVersion != null + ) { + return handshakeAware.description.maxWireVersion; + } + } + + return 0; +} + +/** @internal */ +export function arrayStrictEqual(arr: unknown[], arr2: unknown[]): boolean { + if (!Array.isArray(arr) || !Array.isArray(arr2)) { + return false; + } + + return arr.length === arr2.length && arr.every((elt, idx) => elt === arr2[idx]); +} + +/** @internal */ +export function errorStrictEqual(lhs?: AnyError | null, rhs?: AnyError | null): boolean { + if (lhs === rhs) { + return true; + } + + if (!lhs || !rhs) { + return lhs === rhs; + } + + if ((lhs == null && rhs != null) || (lhs != null && rhs == null)) { + return false; + } + + if (lhs.constructor.name !== rhs.constructor.name) { + return false; + } + + if (lhs.message !== rhs.message) { + return false; + } + + return true; +} + +interface StateTable { + [key: string]: string[]; +} +interface ObjectWithState { + s: { state: string }; + emit(event: 'stateChanged', state: string, newState: string): void; +} +interface StateTransitionFunction { + (target: ObjectWithState, newState: string): void; +} + +/** @public */ +export type EventEmitterWithState = { + /** @internal */ + stateChanged(previous: string, current: string): void; +}; + +/** @internal */ +export function makeStateMachine(stateTable: StateTable): StateTransitionFunction { + return function stateTransition(target, newState) { + const legalStates = stateTable[target.s.state]; + if (legalStates && legalStates.indexOf(newState) < 0) { + throw new MongoRuntimeError( + `illegal state transition from [${target.s.state}] => [${newState}], allowed: [${legalStates}]` + ); + } + + target.emit('stateChanged', target.s.state, newState); + target.s.state = newState; + }; +} + +/** + * This function returns the number of milliseconds since an arbitrary point in time. + * This function should only be used to measure time intervals. + * @internal + * */ +export function processTimeMS(): number { + return Math.floor(performance.now()); +} + +/** @internal */ +export function calculateDurationInMs(started: number | undefined): number { + if (typeof started !== 'number') { + return -1; + } + + const elapsed = processTimeMS() - started; + return elapsed < 0 ? 0 : elapsed; +} + +/** @internal */ +export function hasAtomicOperators( + doc: Document | Document[], + options?: CommandOperationOptions +): boolean { + if (Array.isArray(doc)) { + for (const document of doc) { + if (hasAtomicOperators(document)) { + return true; + } + } + return false; + } + + const keys = Object.keys(doc); + // In this case we need to throw if all the atomic operators are undefined. + if (options?.ignoreUndefined) { + let allUndefined = true; + for (const key of keys) { + // eslint-disable-next-line no-restricted-syntax + if (doc[key] !== undefined) { + allUndefined = false; + break; + } + } + if (allUndefined) { + throw new MongoInvalidArgumentError( + 'Update operations require that all atomic operators have defined values, but none were provided.' + ); + } + } + + return keys.length > 0 && keys[0][0] === '$'; +} + +export function resolveTimeoutOptions>( + client: MongoClient, + options: T +): T & + Pick< + MongoClient['s']['options'], + 'timeoutMS' | 'serverSelectionTimeoutMS' | 'waitQueueTimeoutMS' | 'socketTimeoutMS' + > { + const { socketTimeoutMS, serverSelectionTimeoutMS, waitQueueTimeoutMS, timeoutMS } = + client.s.options; + return { socketTimeoutMS, serverSelectionTimeoutMS, waitQueueTimeoutMS, timeoutMS, ...options }; +} +/** + * Merge inherited properties from parent into options, prioritizing values from options, + * then values from parent. + * + * @param parent - An optional owning class of the operation being run. ex. Db/Collection/MongoClient. + * @param options - The options passed to the operation method. + * + * @internal + */ +export function resolveOptions( + parent: OperationParent | undefined, + options?: T +): T { + const result: T = Object.assign({}, options, resolveBSONOptions(options, parent)); + + const timeoutMS = options?.timeoutMS ?? parent?.timeoutMS; + // Users cannot pass a readConcern/writeConcern to operations in a transaction + const session = options?.session; + + if (!session?.inTransaction()) { + const readConcern = ReadConcern.fromOptions(options) ?? parent?.readConcern; + if (readConcern) { + result.readConcern = readConcern; + } + + let writeConcern = WriteConcern.fromOptions(options) ?? parent?.writeConcern; + if (writeConcern) { + if (timeoutMS != null) { + writeConcern = WriteConcern.fromOptions({ + writeConcern: { + ...writeConcern, + wtimeout: undefined, + wtimeoutMS: undefined + } + }); + } + result.writeConcern = writeConcern; + } + } + + result.timeoutMS = timeoutMS; + + const readPreference = ReadPreference.fromOptions(options) ?? parent?.readPreference; + if (readPreference) { + result.readPreference = readPreference; + } + + const isConvenientTransaction = session?.explicit && session?.timeoutContext != null; + if (isConvenientTransaction && options?.timeoutMS != null) { + throw new MongoInvalidArgumentError( + 'An operation cannot be given a timeoutMS setting when inside a withTransaction call that has a timeoutMS setting' + ); + } + + return result; +} + +export function isSuperset(set: Set | any[], subset: Set | any[]): boolean { + set = Array.isArray(set) ? new Set(set) : set; + subset = Array.isArray(subset) ? new Set(subset) : subset; + for (const elem of subset) { + if (!set.has(elem)) { + return false; + } + } + return true; +} + +/** + * Checks if the document is a Hello request + * @internal + */ +export function isHello(doc: Document): boolean { + return doc[LEGACY_HELLO_COMMAND] || doc.hello ? true : false; +} + +/** Returns the items that are uniquely in setA */ +export function setDifference(setA: Iterable, setB: Iterable): Set { + const difference = new Set(setA); + for (const elem of setB) { + difference.delete(elem); + } + return difference; +} + +const HAS_OWN = (object: unknown, prop: string) => + Object.prototype.hasOwnProperty.call(object, prop); + +export function isRecord( + value: unknown, + requiredKeys: T +): value is Record; +export function isRecord(value: unknown): value is Record; +export function isRecord( + value: unknown, + requiredKeys: string[] | undefined = undefined +): value is Record { + if (!isObject(value)) { + return false; + } + + const ctor = (value as any).constructor; + if (ctor && ctor.prototype) { + if (!isObject(ctor.prototype)) { + return false; + } + + // Check to see if some method exists from the Object exists + if (!HAS_OWN(ctor.prototype, 'isPrototypeOf')) { + return false; + } + } + + if (requiredKeys) { + const keys = Object.keys(value); + return isSuperset(keys, requiredKeys); + } + + return true; +} + +type ListNode = { + value: T; + next: ListNode | HeadNode; + prev: ListNode | HeadNode; +}; + +type HeadNode = { + value: null; + next: ListNode; + prev: ListNode; +}; + +/** + * When a list is empty the head is a reference with pointers to itself + * So this type represents that self referential state + */ +type EmptyNode = { + value: null; + next: EmptyNode; + prev: EmptyNode; +}; + +/** + * A sequential list of items in a circularly linked list + * @remarks + * The head node is special, it is always defined and has a value of null. + * It is never "included" in the list, in that, it is not returned by pop/shift or yielded by the iterator. + * The circular linkage and always defined head node are to reduce checks for null next/prev references to zero. + * New nodes are declared as object literals with keys always in the same order: next, prev, value. + * @internal + */ +export class List { + private readonly head: HeadNode | EmptyNode; + private count: number; + + get length() { + return this.count; + } + + get [Symbol.toStringTag]() { + return 'List' as const; + } + + constructor() { + this.count = 0; + + // this is carefully crafted: + // declaring a complete and consistently key ordered + // object is beneficial to the runtime optimizations + this.head = { + next: null, + prev: null, + value: null + } as unknown as EmptyNode; + this.head.next = this.head; + this.head.prev = this.head; + } + + toArray() { + return Array.from(this); + } + + toString() { + return `head <=> ${this.toArray().join(' <=> ')} <=> head`; + } + + *[Symbol.iterator](): Generator { + for (const node of this.nodes()) { + yield node.value; + } + } + + private *nodes(): Generator, void, void> { + let ptr: HeadNode | ListNode | EmptyNode = this.head.next; + while (ptr !== this.head) { + // Save next before yielding so that we make removing within iteration safe + const { next } = ptr as ListNode; + yield ptr as ListNode; + ptr = next; + } + } + + /** Insert at end of list */ + push(value: T) { + this.count += 1; + const newNode: ListNode = { + next: this.head as HeadNode, + prev: this.head.prev as ListNode, + value + }; + this.head.prev.next = newNode; + this.head.prev = newNode; + } + + /** Inserts every item inside an iterable instead of the iterable itself */ + pushMany(iterable: Iterable) { + for (const value of iterable) { + this.push(value); + } + } + + /** Insert at front of list */ + unshift(value: T) { + this.count += 1; + const newNode: ListNode = { + next: this.head.next as ListNode, + prev: this.head as HeadNode, + value + }; + this.head.next.prev = newNode; + this.head.next = newNode; + } + + private remove(node: ListNode | EmptyNode): T | null { + if (node === this.head || this.length === 0) { + return null; + } + + this.count -= 1; + + const prevNode = node.prev; + const nextNode = node.next; + prevNode.next = nextNode; + nextNode.prev = prevNode; + + return node.value; + } + + /** Removes the first node at the front of the list */ + shift(): T | null { + return this.remove(this.head.next); + } + + /** Removes the last node at the end of the list */ + pop(): T | null { + return this.remove(this.head.prev); + } + + /** Iterates through the list and removes nodes where filter returns true */ + prune(filter: (value: T) => boolean) { + for (const node of this.nodes()) { + if (filter(node.value)) { + this.remove(node); + } + } + } + + clear() { + this.count = 0; + this.head.next = this.head as EmptyNode; + this.head.prev = this.head as EmptyNode; + } + + /** Returns the first item in the list, does not remove */ + first(): T | null { + // If the list is empty, value will be the head's null + return this.head.next.value; + } + + /** Returns the last item in the list, does not remove */ + last(): T | null { + // If the list is empty, value will be the head's null + return this.head.prev.value; + } +} + +/** + * A pool of Buffers which allow you to read them as if they were one + * @internal + */ +export class BufferPool { + private buffers: List; + private totalByteLength: number; + + constructor() { + this.buffers = new List(); + this.totalByteLength = 0; + } + + get length(): number { + return this.totalByteLength; + } + + /** Adds a buffer to the internal buffer pool list */ + append(buffer: Uint8Array): void { + this.buffers.push(buffer); + this.totalByteLength += buffer.length; + } + + /** + * If BufferPool contains 4 bytes or more construct an int32 from the leading bytes, + * otherwise return null. Size can be negative, caller should error check. + */ + getInt32(): number | null { + if (this.totalByteLength < 4) { + return null; + } + const firstBuffer = this.buffers.first(); + if (firstBuffer != null && firstBuffer.byteLength >= 4) { + return NumberUtils.getInt32LE(firstBuffer, 0); + } + + // Unlikely case: an int32 is split across buffers. + // Use read and put the returned buffer back on top + const top4Bytes = this.read(4); + const value = NumberUtils.getInt32LE(top4Bytes, 0); + + // Put it back. + this.totalByteLength += 4; + this.buffers.unshift(top4Bytes); + + return value; + } + + /** Reads the requested number of bytes, optionally consuming them */ + read(size: number): Uint8Array { + if (typeof size !== 'number' || size < 0) { + throw new MongoInvalidArgumentError('Argument "size" must be a non-negative number'); + } + + // oversized request returns empty buffer + if (size > this.totalByteLength) { + return ByteUtils.allocate(0); + } + + // We know we have enough, we just don't know how it is spread across chunks + // TODO(NODE-4732): alloc API should change based on raw option + const result = ByteUtils.allocateUnsafe(size); + + for (let bytesRead = 0; bytesRead < size; ) { + const buffer = this.buffers.shift(); + if (buffer == null) { + break; + } + const bytesRemaining = size - bytesRead; + const bytesReadable = Math.min(bytesRemaining, buffer.byteLength); + const bytes = buffer.subarray(0, bytesReadable); + + result.set(bytes, bytesRead); + + bytesRead += bytesReadable; + this.totalByteLength -= bytesReadable; + if (bytesReadable < buffer.byteLength) { + this.buffers.unshift(buffer.subarray(bytesReadable)); + } + } + + return result; + } +} + +/** @public */ +export class HostAddress { + host: string | undefined = undefined; + port: number | undefined = undefined; + socketPath: string | undefined = undefined; + isIPv6 = false; + + constructor(hostString: string) { + const escapedHost = hostString.split(' ').join('%20'); // escape spaces, for socket path hosts + + if (escapedHost.endsWith('.sock')) { + // heuristically determine if we're working with a domain socket + this.socketPath = decodeURIComponent(escapedHost); + return; + } + + const urlString = `iLoveJS://${escapedHost}`; + let url; + try { + url = new URL(urlString); + } catch (urlError) { + const runtimeError = new MongoRuntimeError(`Unable to parse ${escapedHost} with URL`); + runtimeError.cause = urlError; + throw runtimeError; + } + + const hostname = url.hostname; + const port = url.port; + + let normalized = decodeURIComponent(hostname).toLowerCase(); + if (normalized.startsWith('[') && normalized.endsWith(']')) { + this.isIPv6 = true; + normalized = normalized.substring(1, hostname.length - 1); + } + + this.host = normalized.toLowerCase(); + + if (typeof port === 'number') { + this.port = port; + } else if (typeof port === 'string' && port !== '') { + this.port = Number.parseInt(port, 10); + } else { + this.port = 27017; + } + + if (this.port === 0) { + throw new MongoParseError('Invalid port (zero) with hostname'); + } + Object.freeze(this); + } + + [Symbol.for('nodejs.util.inspect.custom')](): string { + return this.inspect(); + } + + inspect(): string { + return `new HostAddress('${this.toString()}')`; + } + + toString(): string { + if (typeof this.host === 'string') { + if (this.isIPv6) { + return `[${this.host}]:${this.port}`; + } + return `${this.host}:${this.port}`; + } + return `${this.socketPath}`; + } + + static fromString(this: void, s: string): HostAddress { + return new HostAddress(s); + } + + static fromHostPort(host: string, port: number): HostAddress { + if (host.includes(':')) { + host = `[${host}]`; // IPv6 address + } + return HostAddress.fromString(`${host}:${port}`); + } + + static fromSrvRecord({ name, port }: SrvRecord): HostAddress { + return HostAddress.fromHostPort(name, port); + } + + toHostPort(): { host: string; port: number } { + if (this.socketPath) { + return { host: this.socketPath, port: 0 }; + } + + const host = this.host ?? ''; + const port = this.port ?? 0; + return { host, port }; + } +} + +export const DEFAULT_PK_FACTORY = { + // We prefer not to rely on ObjectId having a createPk method + createPk(): ObjectId { + return new ObjectId(); + } +}; + +/** + * When the driver used emitWarning the code will be equal to this. + * @public + * + * @example + * ```ts + * process.on('warning', (warning) => { + * if (warning.code === MONGODB_WARNING_CODE) console.error('Ah an important warning! :)') + * }) + * ``` + */ +export const MONGODB_WARNING_CODE = 'MONGODB DRIVER'; + +/** @internal */ +export function emitWarning(message: string): void { + return process.emitWarning(message, { code: MONGODB_WARNING_CODE } as any); +} + +const emittedWarnings = new Set(); +/** + * Will emit a warning once for the duration of the application. + * Uses the message to identify if it has already been emitted + * so using string interpolation can cause multiple emits + * @internal + */ +export function emitWarningOnce(message: string): void { + if (!emittedWarnings.has(message)) { + emittedWarnings.add(message); + return emitWarning(message); + } +} + +/** + * Takes a JS object and joins the values into a string separated by ', ' + */ +export function enumToString(en: Record): string { + return Object.values(en).join(', '); +} + +/** + * Determine if a server supports retryable writes. + * + * @internal + */ +export function supportsRetryableWrites(server?: Server): boolean { + if (!server) { + return false; + } + + if (server.loadBalanced) { + // Loadbalanced topologies will always support retry writes + return true; + } + + if (server.description.logicalSessionTimeoutMinutes != null) { + // that supports sessions + if (server.description.type !== ServerType.Standalone) { + // and that is not a standalone + return true; + } + } + + return false; +} + +/** + * Fisher–Yates Shuffle + * + * Reference: https://bost.ocks.org/mike/shuffle/ + * @param sequence - items to be shuffled + * @param limit - Defaults to `0`. If nonzero shuffle will slice the randomized array e.g, `.slice(0, limit)` otherwise will return the entire randomized array. + */ +export function shuffle(sequence: Iterable, limit = 0): Array { + const items = Array.from(sequence); // shallow copy in order to never shuffle the input + + if (limit > items.length) { + throw new MongoRuntimeError('Limit must be less than the number of items'); + } + + let remainingItemsToShuffle = items.length; + const lowerBound = limit % items.length === 0 ? 1 : items.length - limit; + while (remainingItemsToShuffle > lowerBound) { + // Pick a remaining element + const randomIndex = Math.floor(Math.random() * remainingItemsToShuffle); + remainingItemsToShuffle -= 1; + + // And swap it with the current element + const swapHold = items[remainingItemsToShuffle]; + items[remainingItemsToShuffle] = items[randomIndex]; + items[randomIndex] = swapHold; + } + + return limit % items.length === 0 ? items : items.slice(lowerBound); +} + +/** + * TODO(NODE-4936): read concern eligibility for commands should be codified in command construction + * @internal + * @see https://github.com/mongodb/specifications/blob/master/source/read-write-concern/read-write-concern.md#read-concern + */ +export function commandSupportsReadConcern(command: Document): boolean { + if (command.aggregate || command.count || command.distinct || command.find || command.geoNear) { + return true; + } + return false; +} + +/** + * @internal + */ +export function commandSupportsAfterClusterTime(command: Document): boolean { + // READ operations + if (command.aggregate || command.count || command.distinct || command.find || command.geoNear) { + return true; + } + + // WRITE operations + if ( + command.bulkWrite || + command.create || + command.createIndexes || + command.delete || + command.drop || + command.dropDatabase || + command.dropIndexes || + command.findAndModify || + command.insert || + command.update + ) { + return true; + } + + return false; +} + +/** + * Compare objectIds. `null` is always less + * - `+1 = oid1 is greater than oid2` + * - `-1 = oid1 is less than oid2` + * - `+0 = oid1 is equal oid2` + */ +export function compareObjectId(oid1?: ObjectId | null, oid2?: ObjectId | null): 0 | 1 | -1 { + if (oid1 == null && oid2 == null) { + return 0; + } + + if (oid1 == null) { + return -1; + } + + if (oid2 == null) { + return 1; + } + + return ByteUtils.compare(oid1.id, oid2.id); +} + +export function parseInteger(value: unknown): number | null { + if (typeof value === 'number') return Math.trunc(value); + const parsedValue = Number.parseInt(String(value), 10); + + return Number.isNaN(parsedValue) ? null : parsedValue; +} + +export function parseUnsignedInteger(value: unknown): number | null { + const parsedInt = parseInteger(value); + + return parsedInt != null && parsedInt >= 0 ? parsedInt : null; +} + +/** + * This function throws a MongoAPIError in the event that either of the following is true: + * * If the provided address domain does not match the provided parent domain + * * If the parent domain contains less than three `.` separated parts and the provided address does not contain at least one more domain level than its parent + * + * If a DNS server were to become compromised SRV records would still need to + * advertise addresses that are under the same domain as the srvHost. + * + * @param address - The address to check against a domain + * @param srvHost - The domain to check the provided address against + * @returns void + */ +export function checkParentDomainMatch(address: string, srvHost: string): void { + // Remove trailing dot if exists on either the resolved address or the srv hostname + const normalizedAddress = address.endsWith('.') ? address.slice(0, address.length - 1) : address; + const normalizedSrvHost = srvHost.endsWith('.') ? srvHost.slice(0, srvHost.length - 1) : srvHost; + + const allCharacterBeforeFirstDot = /^.*?\./; + const srvIsLessThanThreeParts = normalizedSrvHost.split('.').length < 3; + // Remove all characters before first dot + // Add leading dot back to string so + // an srvHostDomain = '.trusted.site' + // will not satisfy an addressDomain that endsWith '.fake-trusted.site' + const addressDomain = `.${normalizedAddress.replace(allCharacterBeforeFirstDot, '')}`; + let srvHostDomain = srvIsLessThanThreeParts + ? normalizedSrvHost + : `.${normalizedSrvHost.replace(allCharacterBeforeFirstDot, '')}`; + + if (!srvHostDomain.startsWith('.')) { + srvHostDomain = '.' + srvHostDomain; + } + if ( + srvIsLessThanThreeParts && + normalizedAddress.split('.').length <= normalizedSrvHost.split('.').length + ) { + throw new MongoAPIError( + 'Server record does not have at least one more domain level than parent URI' + ); + } + if (!addressDomain.endsWith(srvHostDomain)) { + throw new MongoAPIError('Server record does not share hostname with parent URI'); + } +} + +/** + * Perform a get request that returns status and body. + * @internal + */ +export function get( + url: URL | string, + options: http.RequestOptions = {} +): Promise<{ body: string; status: number | undefined }> { + return new Promise((resolve, reject) => { + /* eslint-disable prefer-const */ + let timeoutId: NodeJS.Timeout; + const request = http + .get(url, options, response => { + response.setEncoding('utf8'); + let body = ''; + response.on('data', chunk => (body += chunk)); + response.on('end', () => { + clearTimeout(timeoutId); + resolve({ status: response.statusCode, body }); + }); + }) + .on('error', error => { + clearTimeout(timeoutId); + reject(error); + }) + .end(); + timeoutId = setTimeout(() => { + request.destroy(new MongoNetworkTimeoutError(`request timed out after 10 seconds`)); + }, 10000); + }); +} + +/** @internal */ +export const DOCUMENT_DB_CHECK = /(\.docdb\.amazonaws\.com$)|(\.docdb-elastic\.amazonaws\.com$)/; +/** @internal */ +export const COSMOS_DB_CHECK = /\.cosmos\.azure\.com$/; + +/** @internal */ +export const DOCUMENT_DB_MSG = + 'You appear to be connected to a DocumentDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/documentdb'; +/** @internal */ +export const COSMOS_DB_MSG = + 'You appear to be connected to a CosmosDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb'; + +/** @internal */ +export function isHostMatch(match: RegExp, host?: string): boolean { + return host && match.test(host.toLowerCase()) ? true : false; +} + +export function promiseWithResolvers(): { + promise: Promise; + resolve: (value: T) => void; + reject: (error: Error) => void; +} { + let resolve!: (value: T) => void; + let reject!: (error: Error) => void; + const promise = new Promise(function withResolversExecutor(promiseResolve, promiseReject) { + resolve = promiseResolve; + reject = promiseReject; + }); + return { promise, resolve, reject } as const; +} + +/** + * A noop function intended for use in preventing unhandled rejections. + * + * @example + * ```js + * const promise = myAsyncTask(); + * // eslint-disable-next-line github/no-then + * promise.then(undefined, squashError); + * ``` + */ +export function squashError(_error: unknown) { + return; +} + +export const randomBytes = (size: number): Promise => { + return Promise.resolve(crypto.getRandomValues(new Uint8Array(size))); +}; + +/** + * Replicates the events.once helper. + * + * Removes unused signal logic and It **only** supports 0 or 1 argument events. + * + * @param ee - An event emitter that may emit `ev` + * @param name - An event name to wait for + */ +export async function once(ee: EventEmitter, name: string, options?: Abortable): Promise { + options?.signal?.throwIfAborted(); + + const { promise, resolve, reject } = promiseWithResolvers(); + const onEvent = (data: T) => resolve(data); + const onError = (error: Error) => reject(error); + const abortListener = addAbortListener(options?.signal, function () { + reject(this.reason); + }); + + ee.once(name, onEvent).once('error', onError); + + try { + return await promise; + } finally { + ee.off(name, onEvent); + ee.off('error', onError); + abortListener?.[kDispose](); + } +} + +export function maybeAddIdToDocuments( + collection: Collection, + document: Document, + options: { forceServerObjectId?: boolean } +): Document { + const forceServerObjectId = + options.forceServerObjectId ?? collection.db.options?.forceServerObjectId ?? false; + + // no need to modify the docs if server sets the ObjectId + if (forceServerObjectId) { + return document; + } + + if (document._id == null) { + document._id = collection.s.pkFactory.createPk(); + } + + return document; +} + +export async function fileIsAccessible(fileName: string, mode?: number) { + try { + await fs.access(fileName, mode); + return true; + } catch { + return false; + } +} + +export function csotMin(duration1: number, duration2: number): number { + if (duration1 === 0) return duration2; + if (duration2 === 0) return duration1; + return Math.min(duration1, duration2); +} + +export function noop() { + return; +} + +/** + * Recurse through the (identically-shaped) `decrypted` and `original` + * objects and attach a `decryptedKeys` property on each sub-object that + * contained encrypted fields. Because we only call this on BSON responses, + * we do not need to worry about circular references. + * + * @internal + */ +export function decorateDecryptionResult( + decrypted: Document & { [kDecoratedKeys]?: Array }, + original: Document, + isTopLevelDecorateCall = true +): void { + if (isTopLevelDecorateCall) { + // The original value could have been either a JS object or a BSON buffer + if (ByteUtils.isUint8Array(original)) { + original = deserialize(original); + } + if (ByteUtils.isUint8Array(decrypted)) { + throw new MongoRuntimeError('Expected result of decryption to be deserialized BSON object'); + } + } + + if (!decrypted || typeof decrypted !== 'object') return; + for (const k of Object.keys(decrypted)) { + const originalValue = original[k]; + + // An object was decrypted by libmongocrypt if and only if it was + // a BSON Binary object with subtype 6. + if (originalValue && originalValue._bsontype === 'Binary' && originalValue.sub_type === 6) { + if (!decrypted[kDecoratedKeys]) { + Object.defineProperty(decrypted, kDecoratedKeys, { + value: [], + configurable: true, + enumerable: false, + writable: false + }); + } + // this is defined in the preceding if-statement + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + decrypted[kDecoratedKeys]!.push(k); + // Do not recurse into this decrypted value. It could be a sub-document/array, + // in which case there is no original value associated with its subfields. + continue; + } + + decorateDecryptionResult(decrypted[k], originalValue, false); + } +} + +/** @internal */ +export const kDispose: unique symbol = (Symbol.dispose as any) ?? Symbol('dispose'); + +/** @internal */ +export interface Disposable { + [kDispose](): void; +} + +/** + * A utility that helps with writing listener code idiomatically + * + * @example + * ```js + * using listener = addAbortListener(signal, function () { + * console.log('aborted', this.reason); + * }); + * ``` + * + * @param signal - if exists adds an abort listener + * @param listener - the listener to be added to signal + * @returns A disposable that will remove the abort listener + */ +export function addAbortListener( + signal: AbortSignal | undefined | null, + listener: (this: AbortSignal, event: Event) => void +): Disposable | undefined { + if (signal == null) return; + signal.addEventListener('abort', listener, { once: true }); + return { [kDispose]: () => signal.removeEventListener('abort', listener) }; +} + +/** + * Takes a promise and races it with a promise wrapping the abort event of the optionally provided signal. + * The given promise is _always_ ordered before the signal's abort promise. + * When given an already rejected promise and an already aborted signal, the promise's rejection takes precedence. + * + * Any asynchronous processing in `promise` will continue even after the abort signal has fired, + * but control will be returned to the caller + * + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race + * + * @param promise - A promise to discard if the signal aborts + * @param options - An options object carrying an optional signal + */ +export async function abortable( + promise: Promise, + { signal }: { signal?: AbortSignal } +): Promise { + if (signal == null) { + return await promise; + } + + const { promise: aborted, reject } = promiseWithResolvers(); + + const abortListener = signal.aborted + ? reject(signal.reason) + : addAbortListener(signal, function () { + reject(this.reason); + }); + + try { + return await Promise.race([promise, aborted]); + } finally { + abortListener?.[kDispose](); + } +} diff --git a/gateway/node_modules/mongodb/src/write_concern.ts b/gateway/node_modules/mongodb/src/write_concern.ts new file mode 100644 index 0000000000000000000000000000000000000000..622460fa79aeddaebd87d405912224257fa9c3cd --- /dev/null +++ b/gateway/node_modules/mongodb/src/write_concern.ts @@ -0,0 +1,183 @@ +import { type Document } from './bson'; +import { MongoDBResponse } from './cmap/wire_protocol/responses'; +import { MongoWriteConcernError } from './error'; + +/** @public */ +export type W = number | 'majority'; + +/** @public */ +export interface WriteConcernOptions { + /** Write Concern as an object */ + writeConcern?: WriteConcern | WriteConcernSettings; +} + +/** @public */ +export interface WriteConcernSettings { + /** The write concern */ + w?: W; + /** + * The write concern timeout. + */ + wtimeoutMS?: number; + /** The journal write concern */ + journal?: boolean; + + // legacy options + /** + * The journal write concern. + * @deprecated Will be removed in the next major version. Please use the journal option. + */ + j?: boolean; + /** + * The write concern timeout. + */ + wtimeout?: number; + /** + * The file sync write concern. + * @deprecated Will be removed in the next major version. Please use the journal option. + */ + fsync?: boolean | 1; +} + +export const WRITE_CONCERN_KEYS = ['w', 'wtimeout', 'j', 'journal', 'fsync']; + +/** The write concern options that decorate the server command. */ +interface CommandWriteConcernOptions { + /** The write concern */ + w?: W; + /** The journal write concern. */ + j?: boolean; + /** The write concern timeout. */ + wtimeout?: number; +} + +/** + * A MongoDB WriteConcern, which describes the level of acknowledgement + * requested from MongoDB for write operations. + * @public + * + * @see https://www.mongodb.com/docs/manual/reference/write-concern/ + */ +export class WriteConcern { + /** + * Request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags. + * If w is 0 and is set on a write operation, the server will not send a response. + */ + readonly w?: W; + /** Request acknowledgment that the write operation has been written to the on-disk journal */ + readonly journal?: boolean; + /** + * Specify a time limit to prevent write operations from blocking indefinitely. + */ + readonly wtimeoutMS?: number; + /** + * Specify a time limit to prevent write operations from blocking indefinitely. + * @deprecated Will be removed in the next major version. Please use wtimeoutMS. + */ + wtimeout?: number; + /** + * Request acknowledgment that the write operation has been written to the on-disk journal. + * @deprecated Will be removed in the next major version. Please use journal. + */ + j?: boolean; + /** + * Equivalent to the j option. + * @deprecated Will be removed in the next major version. Please use journal. + */ + fsync?: boolean | 1; + + /** + * Constructs a WriteConcern from the write concern properties. + * @param w - request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags. + * @param wtimeoutMS - specify a time limit to prevent write operations from blocking indefinitely + * @param journal - request acknowledgment that the write operation has been written to the on-disk journal + * @param fsync - equivalent to the j option. Is deprecated and will be removed in the next major version. + */ + constructor(w?: W, wtimeoutMS?: number, journal?: boolean, fsync?: boolean | 1) { + if (w != null) { + if (!Number.isNaN(Number(w))) { + this.w = Number(w); + } else { + this.w = w; + } + } + if (wtimeoutMS != null) { + this.wtimeoutMS = this.wtimeout = wtimeoutMS; + } + if (journal != null) { + this.journal = this.j = journal; + } + if (fsync != null) { + this.journal = this.j = fsync ? true : false; + } + } + + /** + * Apply a write concern to a command document. Will modify and return the command. + */ + static apply(command: Document, writeConcern: WriteConcern): Document { + const wc: CommandWriteConcernOptions = {}; + // The write concern document sent to the server has w/wtimeout/j fields. + if (writeConcern.w != null) wc.w = writeConcern.w; + if (writeConcern.wtimeoutMS != null) wc.wtimeout = writeConcern.wtimeoutMS; + if (writeConcern.journal != null) wc.j = writeConcern.j; + command.writeConcern = wc; + return command; + } + + /** Construct a WriteConcern given an options object. */ + static fromOptions( + options?: WriteConcernOptions | WriteConcern | W, + inherit?: WriteConcernOptions | WriteConcern + ): WriteConcern | undefined { + if (options == null) return undefined; + inherit = inherit ?? {}; + let opts: WriteConcernSettings | WriteConcern | undefined; + if (typeof options === 'string' || typeof options === 'number') { + opts = { w: options }; + } else if (options instanceof WriteConcern) { + opts = options; + } else { + opts = options.writeConcern; + } + const parentOpts: WriteConcern | WriteConcernSettings | undefined = + inherit instanceof WriteConcern ? inherit : inherit.writeConcern; + + const mergedOpts = { ...parentOpts, ...opts } as WriteConcernSettings; + const { + w = undefined, + wtimeout = undefined, + j = undefined, + fsync = undefined, + journal = undefined, + wtimeoutMS = undefined + } = mergedOpts; + if ( + w != null || + wtimeout != null || + wtimeoutMS != null || + j != null || + journal != null || + fsync != null + ) { + return new WriteConcern(w, wtimeout ?? wtimeoutMS, j ?? journal, fsync); + } + return undefined; + } +} + +/** Called with either a plain object or MongoDBResponse */ +export function throwIfWriteConcernError(response: unknown): void { + if (typeof response === 'object' && response != null) { + const writeConcernError: object | null = + MongoDBResponse.is(response) && response.has('writeConcernError') + ? response.toObject() + : !MongoDBResponse.is(response) && 'writeConcernError' in response + ? response + : null; + + if (writeConcernError != null) { + throw new MongoWriteConcernError(writeConcernError as any); + } + } +} diff --git a/gateway/node_modules/mongodb/tsconfig.json b/gateway/node_modules/mongodb/tsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..6604ef28717c3a0611f4448a3deb46f6c0e5c280 --- /dev/null +++ b/gateway/node_modules/mongodb/tsconfig.json @@ -0,0 +1,46 @@ +{ + "compilerOptions": { + "allowJs": true, + "checkJs": false, + "strict": true, + "alwaysStrict": true, + "target": "ES2023", + "module": "commonJS", + "moduleResolution": "node", + "skipLibCheck": true, + "lib": [ + "es2023" + ], + // We don't make use of tslib helpers, all syntax used is supported by target engine + "importHelpers": false, + "noEmitHelpers": true, + // Never emit error filled code + "noEmitOnError": true, + "outDir": "lib", + // We want the sourcemaps in a separate file + "inlineSourceMap": false, + "sourceMap": true, + // API-Extractor uses declaration maps to report problems in source, no need to distribute + "declaration": true, + "declarationMap": true, + // we include sources in the release + "inlineSources": false, + // Prevents web types from being suggested by vscode. + "types": [ + "node" + ], + "forceConsistentCasingInFileNames": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + // TODO(NODE-3659): Enable useUnknownInCatchVariables and add type assertions or remove unnecessary catch blocks + "useUnknownInCatchVariables": false, + "useDefineForClassFields": false + }, + "ts-node": { + "transpileOnly": true, + "compiler": "typescript-cached-transpile" + }, + "include": [ + "src/**/*" + ] +} diff --git a/gateway/node_modules/mongoose/LICENSE.md b/gateway/node_modules/mongoose/LICENSE.md new file mode 100644 index 0000000000000000000000000000000000000000..54b4a4c6c16832975154bbf01ff00cf0c17922d0 --- /dev/null +++ b/gateway/node_modules/mongoose/LICENSE.md @@ -0,0 +1,22 @@ +# MIT License + +Copyright (c) 2010-2013 LearnBoost +Copyright (c) 2013-2021 Automattic + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/gateway/node_modules/mongoose/README.md b/gateway/node_modules/mongoose/README.md new file mode 100644 index 0000000000000000000000000000000000000000..c62296fe9785ce89a2587f01e65cf44af7904ec4 --- /dev/null +++ b/gateway/node_modules/mongoose/README.md @@ -0,0 +1,397 @@ +# Mongoose + +Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed to work in an asynchronous environment. Mongoose supports [Node.js](https://nodejs.org/en/) and [Deno](https://deno.land/) (alpha). + +[![Build Status](https://github.com/Automattic/mongoose/workflows/Test/badge.svg)](https://github.com/Automattic/mongoose) +[![NPM version](https://badge.fury.io/js/mongoose.svg)](http://badge.fury.io/js/mongoose) +[![Deno version](https://deno.land/badge/mongoose/version)](https://deno.land/x/mongoose) +[![Deno popularity](https://deno.land/badge/mongoose/popularity)](https://deno.land/x/mongoose) + +[![npm](https://nodei.co/npm/mongoose.png)](https://www.npmjs.com/package/mongoose) + +## Documentation + +The official documentation website is [mongoosejs.com](https://mongoosejs.com/). + +Mongoose 9.0.0 was released on November 21, 2025. You can find more details on [backwards breaking changes in 9.0.0 on our docs site](https://mongoosejs.com/docs/migrating_to_9.html). + +## Support + +* [Stack Overflow](http://stackoverflow.com/questions/tagged/mongoose) +* [Bug Reports](https://github.com/Automattic/mongoose/issues/) +* [Mongoose Slack Channel](http://slack.mongoosejs.io/) +* [Help Forum](http://groups.google.com/group/mongoose-orm) +* [MongoDB Support](https://www.mongodb.com/docs/manual/support/) + +## Plugins + +Check out the [plugins search site](https://plugins.mongoosejs.io/) to see hundreds of related modules from the community. Next, learn how to write your own plugin from the [docs](https://mongoosejs.com/docs/plugins.html) or [this blog post](http://thecodebarbarian.com/2015/03/06/guide-to-mongoose-plugins). + +## Contributors + +Pull requests are always welcome! Please base pull requests against the `master` +branch and follow the [contributing guide](https://github.com/Automattic/mongoose/blob/master/CONTRIBUTING.md). + +If your pull request makes documentation changes, please do **not** +modify any `.html` files. The `.html` files are compiled code, so please make +your changes in `docs/*.pug`, `lib/*.js`, or `test/docs/*.js`. + +View all 400+ [contributors](https://github.com/Automattic/mongoose/graphs/contributors). + +## Installation + +First install [Node.js](https://nodejs.org/) and [MongoDB](https://www.mongodb.org/downloads), then install the `mongoose` package using your preferred package manager: + +### Using npm + +```sh +npm install mongoose +``` + +### Using pnpm + +```sh +pnpm add mongoose +``` + +### Using Yarn + +```sh +yarn add mongoose +``` + +### Using Bun + +```sh +bun add mongoose +``` + +Mongoose 6.8.0 also includes alpha support for [Deno](https://deno.land/). + +## Importing + +```javascript +// Using Node.js `require()` +const mongoose = require('mongoose'); + +// Using ES6 imports +import mongoose from 'mongoose'; +``` + +Or, using [Deno's `createRequire()` for CommonJS support](https://deno.land/std@0.113.0/node/README.md?source=#commonjs-modules-loading) as follows. + +```javascript +import { createRequire } from 'https://deno.land/std@0.177.0/node/module.ts'; +const require = createRequire(import.meta.url); + +const mongoose = require('mongoose'); + +mongoose.connect('mongodb://127.0.0.1:27017/test') + .then(() => console.log('Connected!')); +``` + +You can then run the above script using the following. + +```sh +deno run --allow-net --allow-read --allow-sys --allow-env mongoose-test.js +``` + +## Mongoose Studio + +[Mongoose Studio](https://mongoosestudio.app?utm_source=mongoose&utm_medium=referral&utm_campaign=readme) is a free, fully open-source, browser-based MongoDB GUI built by the Mongoose team for apps that already use Mongoose. Install `@mongoosejs/studio` from npm and run it alongside your app as Express middleware, or deploy it on Vercel or Netlify, to browse and edit documents, query with your existing models and schemas with autocomplete, build dashboards, visualize and edit GeoJSON, and use AI-assisted MongoDB workflows without moving data into a hosted third-party workspace or sharing raw MongoDB connection strings. + +## Mongoose for Enterprise + +Available as part of the Tidelift Subscription + +The maintainers of mongoose and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-mongoose?utm_source=npm-mongoose&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) + +## Overview + +### Connecting to MongoDB + +First, we need to define a connection. If your app uses only one database, you should use `mongoose.connect`. If you need to create additional connections, use `mongoose.createConnection`. + +Both `connect` and `createConnection` take a `mongodb://` URI, or the parameters `host, database, port, options`. + +```js +await mongoose.connect('mongodb://127.0.0.1/my_database'); +``` + +Once connected, the `open` event is fired on the `Connection` instance. If you're using `mongoose.connect`, the `Connection` is `mongoose.connection`. Otherwise, `mongoose.createConnection` return value is a `Connection`. + +**Note:** *If the local connection fails then try using 127.0.0.1 instead of localhost. Sometimes issues may arise when the local hostname has been changed.* + +**Important!** Mongoose buffers all the commands until it's connected to the database. This means that you don't have to wait until it connects to MongoDB in order to define models, run queries, etc. + +### Defining a Model + +Models are defined through the `Schema` interface. + +```js +const Schema = mongoose.Schema; +const ObjectId = Schema.ObjectId; + +const BlogPost = new Schema({ + author: ObjectId, + title: String, + body: String, + date: Date +}); +``` + +Aside from defining the structure of your documents and the types of data you're storing, a Schema handles the definition of: + +* [Validators](https://mongoosejs.com/docs/validation.html) (async and sync) +* [Defaults](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-default) +* [Getters](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-get) +* [Setters](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set) +* [Indexes](https://mongoosejs.com/docs/guide.html#indexes) +* [Middleware](https://mongoosejs.com/docs/middleware.html) +* [Methods](https://mongoosejs.com/docs/guide.html#methods) definition +* [Statics](https://mongoosejs.com/docs/guide.html#statics) definition +* [Plugins](https://mongoosejs.com/docs/plugins.html) +* [pseudo-JOINs](https://mongoosejs.com/docs/populate.html) + +The following example shows some of these features: + +```js +const Comment = new Schema({ + name: { type: String, default: 'hahaha' }, + age: { type: Number, min: 18, index: true }, + bio: { type: String, match: /[a-z]/ }, + date: { type: Date, default: Date.now }, + buff: Buffer +}); + +// a setter +Comment.path('name').set(function(v) { + return capitalize(v); +}); + +// middleware +Comment.pre('save', function(next) { + notify(this.get('email')); + next(); +}); +``` + +### Accessing a Model + +Once we define a model through `mongoose.model('ModelName', mySchema)`, we can access it through the same function + +```js +const MyModel = mongoose.model('ModelName'); +``` + +Or just do it all at once + +```js +const MyModel = mongoose.model('ModelName', mySchema); +``` + +The first argument is the *singular* name of the collection your model is for. **Mongoose automatically looks for the *plural* version of your model name.** For example, if you use + +```js +const MyModel = mongoose.model('Ticket', mySchema); +``` + +Then `MyModel` will use the **tickets** collection, not the **ticket** collection. For more details read the [model docs](https://mongoosejs.com/docs/api/mongoose.html#mongoose_Mongoose-model). + +Once we have our model, we can then instantiate it, and save it: + +```js +const instance = new MyModel(); +instance.my.key = 'hello'; +await instance.save(); +``` + +Or we can find documents from the same collection + +```js +await MyModel.find({}); +``` + +You can also `findOne`, `findById`, `update`, etc. + +```js +const instance = await MyModel.findOne({ /* ... */ }); +console.log(instance.my.key); // 'hello' +``` + +For more details check out [the docs](https://mongoosejs.com/docs/queries.html). + +**Important!** If you opened a separate connection using `mongoose.createConnection()` but attempt to access the model through `mongoose.model('ModelName')` it will not work as expected since it is not hooked up to an active db connection. In this case access your model through the connection you created: + +```js +const conn = mongoose.createConnection('your connection string'); +const MyModel = conn.model('ModelName', schema); +const m = new MyModel(); +await m.save(); // works +``` + +vs + +```js +const conn = mongoose.createConnection('your connection string'); +const MyModel = mongoose.model('ModelName', schema); +const m = new MyModel(); +await m.save(); // does not work b/c the default connection object was never connected +``` + +### Embedded Documents + +In the first example snippet, we defined a key in the Schema that looks like: + +```txt +comments: [Comment] +``` + +Where `Comment` is a `Schema` we created. This means that creating embedded documents is as simple as: + +```js +// retrieve my model +const BlogPost = mongoose.model('BlogPost'); + +// create a blog post +const post = new BlogPost(); + +// create a comment +post.comments.push({ title: 'My comment' }); + +await post.save(); +``` + +The same goes for removing them: + +```js +const post = await BlogPost.findById(myId); +post.comments[0].deleteOne(); +await post.save(); +``` + +Embedded documents enjoy all the same features as your models. Defaults, validators, middleware. + +### Middleware + +See the [docs](https://mongoosejs.com/docs/middleware.html) page. + +#### Intercepting and mutating method arguments + +You can intercept method arguments via middleware. + +For example, this would allow you to broadcast changes about your Documents every time someone `set`s a path in your Document to a new value: + +```js +schema.pre('set', function(next, path, val, typel) { + // `this` is the current Document + this.emit('set', path, val); + + // Pass control to the next pre + next(); +}); +``` + +Moreover, you can mutate the incoming `method` arguments so that subsequent middleware see different values for those arguments. To do so, just pass the new values to `next`: + +```js +schema.pre(method, function firstPre(next, methodArg1, methodArg2) { + // Mutate methodArg1 + next('altered-' + methodArg1.toString(), methodArg2); +}); + +// pre declaration is chainable +schema.pre(method, function secondPre(next, methodArg1, methodArg2) { + console.log(methodArg1); + // => 'altered-originalValOfMethodArg1' + + console.log(methodArg2); + // => 'originalValOfMethodArg2' + + // Passing no arguments to `next` automatically passes along the current argument values + // i.e., the following `next()` is equivalent to `next(methodArg1, methodArg2)` + // and also equivalent to, with the example method arg + // values, `next('altered-originalValOfMethodArg1', 'originalValOfMethodArg2')` + next(); +}); +``` + +#### Schema gotcha + +`type`, when used in a schema has special meaning within Mongoose. If your schema requires using `type` as a nested property you must use object notation: + +```js +new Schema({ + broken: { type: Boolean }, + asset: { + name: String, + type: String // uh oh, it broke. asset will be interpreted as String + } +}); + +new Schema({ + works: { type: Boolean }, + asset: { + name: String, + type: { type: String } // works. asset is an object with a type property + } +}); +``` + +### Driver Access + +Mongoose is built on top of the [official MongoDB Node.js driver](https://github.com/mongodb/node-mongodb-native). Each mongoose model keeps a reference to a [native MongoDB driver collection](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html). The collection object can be accessed using `YourModel.collection`. However, using the collection object directly bypasses all mongoose features, including hooks, validation, etc. The one +notable exception is that `YourModel.collection` still buffers +commands. As such, `YourModel.collection.find()` will **not** +return a cursor. + +## API Docs + +[Mongoose API documentation](https://mongoosejs.com/docs/api/mongoose.html), generated using [dox](https://github.com/tj/dox) +and [acquit](https://github.com/vkarpov15/acquit). + +## Related Projects + +### MongoDB Runners + +* [run-rs](https://www.npmjs.com/package/run-rs) +* [mongodb-memory-server](https://www.npmjs.com/package/mongodb-memory-server) +* [mongodb-topology-manager](https://www.npmjs.com/package/mongodb-topology-manager) + +### Unofficial CLIs + +* [mongoosejs-cli](https://www.npmjs.com/package/mongoosejs-cli) + +### Data Seeding + +* [dookie](https://www.npmjs.com/package/dookie) +* [seedgoose](https://www.npmjs.com/package/seedgoose) +* [mongoose-data-seed](https://www.npmjs.com/package/mongoose-data-seed) + +### Express Session Stores + +* [connect-mongodb-session](https://www.npmjs.com/package/connect-mongodb-session) +* [connect-mongo](https://www.npmjs.com/package/connect-mongo) + +## License + +Copyright (c) 2010 LearnBoost <dev@learnboost.com> + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/gateway/node_modules/mongoose/SECURITY.md b/gateway/node_modules/mongoose/SECURITY.md new file mode 100644 index 0000000000000000000000000000000000000000..41b89d8347d578ee930b156592e3c3168bd6980c --- /dev/null +++ b/gateway/node_modules/mongoose/SECURITY.md @@ -0,0 +1 @@ +Please follow the instructions on [Tidelift's security page](https://tidelift.com/docs/security) to report a security issue. diff --git a/gateway/node_modules/mongoose/eslint.config.mjs b/gateway/node_modules/mongoose/eslint.config.mjs new file mode 100644 index 0000000000000000000000000000000000000000..2572bf68228b431364bf5e7a60ae5a64cee962ef --- /dev/null +++ b/gateway/node_modules/mongoose/eslint.config.mjs @@ -0,0 +1,201 @@ +import { defineConfig, globalIgnores } from 'eslint/config'; +import mochaNoOnly from 'eslint-plugin-mocha-no-only'; +import globals from 'globals'; +import tseslint from 'typescript-eslint'; +import js from '@eslint/js'; + +export default defineConfig([ + globalIgnores([ + '**/tools', + '**/dist', + 'test/files/*', + '**/benchmarks', + '**/*.min.js', + '**/docs/js/native.js', + '!**/.*', + '**/node_modules', + '**/.git', + '**/data', + '**/docs/3.*/**', + '**/docs/5.*/**', + '**/docs/vendor/*' + ]), + js.configs.recommended, + // general options + { + languageOptions: { + globals: globals.node, + ecmaVersion: 2022, // nodejs 18.0.0, + sourceType: 'commonjs' + }, + rules: { + 'comma-style': 'error', + + indent: ['error', 2, { + SwitchCase: 1, + VariableDeclarator: 2 + }], + + 'keyword-spacing': 'error', + 'no-whitespace-before-property': 'error', + 'no-buffer-constructor': 'warn', + 'no-console': 'off', + 'no-constant-condition': 'off', + 'no-multi-spaces': 'error', + 'func-call-spacing': 'error', + 'no-trailing-spaces': 'error', + 'no-undef': 'error', + 'no-unneeded-ternary': 'error', + 'no-const-assign': 'error', + 'no-useless-rename': 'error', + 'no-dupe-keys': 'error', + 'space-in-parens': ['error', 'never'], + + 'spaced-comment': ['error', 'always', { + block: { + markers: ['!'], + balanced: true + } + }], + + 'key-spacing': ['error', { + beforeColon: false, + afterColon: true + }], + + 'comma-spacing': ['error', { + before: false, + after: true + }], + + 'array-bracket-spacing': 1, + + 'arrow-spacing': ['error', { + before: true, + after: true + }], + + 'object-curly-spacing': ['error', 'always'], + 'comma-dangle': ['error', 'never'], + 'no-unreachable': 'error', + quotes: ['error', 'single'], + 'quote-props': ['error', 'as-needed'], + semi: 'error', + 'no-extra-semi': 'error', + 'semi-spacing': 'error', + 'no-spaced-func': 'error', + 'no-throw-literal': 'error', + 'space-before-blocks': 'error', + 'space-before-function-paren': ['error', 'never'], + 'space-infix-ops': 'error', + 'space-unary-ops': 'error', + 'no-var': 'warn', + 'prefer-const': 'warn', + strict: ['error', 'global'], + + 'no-restricted-globals': ['error', { + name: 'context', + message: 'Don\'t use Mocha\'s global context' + }], + + 'no-prototype-builtins': 'off', + 'no-empty': 'off', + 'eol-last': 'warn', + + 'no-multiple-empty-lines': ['warn', { + max: 2 + }] + } + }, + // general typescript options + { + files: ['**/*.{ts,tsx}', '**/*.md/*.ts', '**/*.md/*.typescript'], + extends: [ + tseslint.configs.recommended + ], + languageOptions: { + parserOptions: { + projectService: { + allowDefaultProject: [], + defaultProject: 'tsconfig.json' + } + } + }, + rules: { + '@typescript-eslint/triple-slash-reference': 'off', + '@typescript-eslint/no-non-null-assertion': 'off', + '@typescript-eslint/no-empty-function': 'off', + + 'spaced-comment': ['error', 'always', { + block: { + markers: ['!'], + balanced: true + }, + + markers: ['/'] + }], + + '@typescript-eslint/no-explicit-any': 'off', + '@typescript-eslint/ban-types': 'off', + '@typescript-eslint/no-unused-vars': 'off', + '@typescript-eslint/explicit-module-boundary-types': 'off', + '@typescript-eslint/prefer-optional-chain': 'error', + '@typescript-eslint/no-dupe-class-members': 'error', + '@typescript-eslint/no-redeclare': 'error', + '@typescript-eslint/space-infix-ops': 'off', + '@typescript-eslint/no-require-imports': 'off', + '@typescript-eslint/no-empty-object-type': 'off', + '@typescript-eslint/no-wrapper-object-types': 'off', + '@typescript-eslint/no-unused-expressions': 'off', + '@typescript-eslint/no-unsafe-function-type': 'off' + } + }, + // type test specific options + { + files: ['test/types/**/*.ts'], + rules: { + '@typescript-eslint/no-empty-interface': 'off' + } + }, + // test specific options (including type tests) + { + files: ['test/**/*.js', 'test/**/*.ts'], + ignores: ['deno*.mjs'], + plugins: { + 'mocha-no-only': mochaNoOnly + }, + languageOptions: { + globals: globals.mocha + }, + rules: { + 'no-self-assign': 'off', + 'mocha-no-only/mocha-no-only': ['error'] + } + }, + // deno specific options + { + files: ['**/deno*.mjs'], + languageOptions: { + globals: { + // "globals" currently has no definition for deno + Deno: 'readonly' + } + } + }, + // general options for module files + { + files: ['**/*.mjs'], + languageOptions: { + sourceType: 'module' + } + }, + // doc script specific options + { + files: ['**/docs/js/**/*.js'], + languageOptions: { + globals: { + ...Object.fromEntries(Object.entries(globals.node).map(([key]) => [key, 'off'])), + ...globals.browser } + } + } +]); diff --git a/gateway/node_modules/mongoose/index.js b/gateway/node_modules/mongoose/index.js new file mode 100644 index 0000000000000000000000000000000000000000..6ebbd5fd5d3eeba2b7bfdcb859901e20595a38d3 --- /dev/null +++ b/gateway/node_modules/mongoose/index.js @@ -0,0 +1,64 @@ +/** + * Export lib/mongoose + * + */ + +'use strict'; + +const mongoose = require('./lib/'); + +module.exports = mongoose; +module.exports.default = mongoose; +module.exports.mongoose = mongoose; + +// Re-export for ESM support +module.exports.cast = mongoose.cast; +module.exports.STATES = mongoose.STATES; +module.exports.setDriver = mongoose.setDriver; +module.exports.set = mongoose.set; +module.exports.get = mongoose.get; +module.exports.createConnection = mongoose.createConnection; +module.exports.connect = mongoose.connect; +module.exports.disconnect = mongoose.disconnect; +module.exports.startSession = mongoose.startSession; +module.exports.pluralize = mongoose.pluralize; +module.exports.model = mongoose.model; +module.exports.deleteModel = mongoose.deleteModel; +module.exports.modelNames = mongoose.modelNames; +module.exports.plugin = mongoose.plugin; +module.exports.connections = mongoose.connections; +module.exports.version = mongoose.version; +module.exports.Aggregate = mongoose.Aggregate; +module.exports.Mongoose = mongoose.Mongoose; +module.exports.Schema = mongoose.Schema; +module.exports.SchemaType = mongoose.SchemaType; +module.exports.SchemaTypes = mongoose.SchemaTypes; +module.exports.VirtualType = mongoose.VirtualType; +module.exports.Types = mongoose.Types; +module.exports.Query = mongoose.Query; +module.exports.Model = mongoose.Model; +module.exports.Document = mongoose.Document; +module.exports.ObjectId = mongoose.ObjectId; +module.exports.isValidObjectId = mongoose.isValidObjectId; +module.exports.isObjectIdOrHexString = mongoose.isObjectIdOrHexString; +module.exports.syncIndexes = mongoose.syncIndexes; +module.exports.Decimal128 = mongoose.Decimal128; +module.exports.Mixed = mongoose.Mixed; +module.exports.Date = mongoose.Date; +module.exports.Number = mongoose.Number; +module.exports.Error = mongoose.Error; +module.exports.MongooseError = mongoose.MongooseError; +module.exports.now = mongoose.now; +module.exports.CastError = mongoose.CastError; +module.exports.SchemaTypeOptions = mongoose.SchemaTypeOptions; +module.exports.mongo = mongoose.mongo; +module.exports.mquery = mongoose.mquery; +module.exports.sanitizeFilter = mongoose.sanitizeFilter; +module.exports.trusted = mongoose.trusted; +module.exports.skipMiddlewareFunction = mongoose.skipMiddlewareFunction; +module.exports.overwriteMiddlewareResult = mongoose.overwriteMiddlewareResult; + +// The following properties are not exported using ESM because `setDriver()` can mutate these +// module.exports.connection = mongoose.connection; +// module.exports.Collection = mongoose.Collection; +// module.exports.Connection = mongoose.Connection; diff --git a/gateway/node_modules/mongoose/package.json b/gateway/node_modules/mongoose/package.json new file mode 100644 index 0000000000000000000000000000000000000000..02e7c637903d586204aefbe17a431224c8743e81 --- /dev/null +++ b/gateway/node_modules/mongoose/package.json @@ -0,0 +1,141 @@ +{ + "name": "mongoose", + "description": "Mongoose MongoDB ODM", + "version": "9.9.3", + "author": "Guillermo Rauch ", + "keywords": [ + "mongodb", + "document", + "model", + "schema", + "database", + "odm", + "data", + "datastore", + "query", + "nosql", + "orm", + "db" + ], + "type": "commonjs", + "license": "MIT", + "dependencies": { + "@standard-schema/spec": "^1.1.0", + "kareem": "3.3.0", + "mongodb": "~7.5", + "mpath": "0.9.0", + "mquery": "6.0.0", + "ms": "2.1.3", + "sift": "17.1.3" + }, + "devDependencies": { + "@ark/attest": "0.56.3", + "@eslint/js": "^9.39.3", + "@mongodb-js/mongodb-downloader": "^1.0.0", + "@types/node": "^20.19.0", + "acquit": "1.4.0", + "acquit-ignore": "0.2.2", + "acquit-require": "0.1.1", + "ajv": "8.20.0", + "c8": "12.0.0", + "cheerio": "1.2.0", + "dox": "1.0.0", + "eslint": "10.8.0", + "eslint-plugin-mocha-no-only": "1.2.0", + "express": "5.2.1", + "fs-extra": "~11.4.0", + "glob": "^13.0.6", + "globals": "^17.4.0", + "highlight.js": "11.11.1", + "linkinator": "8.x", + "lodash.isequal": "4.5.0", + "lodash.isequalwith": "4.4.0", + "markdownlint-cli2": "0.23.2", + "marked": "18.0.7", + "mkdirp": "^3.0.1", + "mocha": "12.0.0-rc.5", + "moment": "2.30.1", + "mongodb-client-encryption": "^7.2.0", + "mongodb-memory-server": "11.2.0", + "mongodb-runner": "^6.0.0", + "ncp": "^2.0.0", + "pug": "3.0.4", + "sinon": "22.1.0", + "tstyche": "^7.0.0", + "typescript": "5.9.3", + "typescript-eslint": "^8.31.1", + "uuid": "14.0.1", + "xss": "1.0.15" + }, + "directories": { + "lib": "./lib/mongoose" + }, + "scripts": { + "docs:clean": "npm run docs:clean:stable", + "docs:clean:stable": "rm -rf index.html && rm -rf ./docs/*.html && rm -rf ./docs/api && rm -rf ./docs/tutorials/*.html && rm -rf ./docs/typescript/*.html && rm -rf ./docs/*.html && rm -rf ./docs/source/_docs && rm -rf ./tmp", + "docs:clean:5x": "rm -rf index.html && rm -rf ./docs/5.x && rm -rf ./docs/source/_docs && rm -rf ./tmp", + "docs:clean:6x": "rm -rf index.html && rm -rf ./docs/6.x && rm -rf ./docs/source/_docs && rm -rf ./tmp", + "docs:copy:tmp": "mkdirp ./tmp/docs/css && mkdirp ./tmp/docs/js && mkdirp ./tmp/docs/images && mkdirp ./tmp/docs/tutorials && mkdirp ./tmp/docs/typescript && mkdirp ./tmp/docs/api && ncp ./docs/css ./tmp/docs/css --filter=.css$ && ncp ./docs/js ./tmp/docs/js --filter=.js$ && ncp ./docs/images ./tmp/docs/images && ncp ./docs/tutorials ./tmp/docs/tutorials && ncp ./docs/typescript ./tmp/docs/typescript && ncp ./docs/api ./tmp/docs/api && cp index.html ./tmp && cp docs/*.html ./tmp/docs/", + "docs:copy:tmp:5x": "rm -rf ./docs/5.x && ncp ./tmp ./docs/5.x", + "docs:copy:tmp:6x": "rm -rf ./docs/6.x && ncp ./tmp ./docs/6.x", + "docs:generate": "node ./scripts/website.js", + "docs:generate:sponsorData": "node ./scripts/loadSponsorData.js", + "docs:test": "npm run docs:generate", + "docs:view": "node ./scripts/static.js", + "docs:prepare:publish:stable": "git checkout gh-pages && git merge master && env GENERATE_SEARCH=true npm run docs:generate", + "docs:prepare:publish:5x": "git checkout 5.x && git merge 5.x && npm run docs:clean:stable && npm run docs:generate && npm run docs:copy:tmp && git checkout gh-pages && npm run docs:copy:tmp:5x", + "docs:prepare:publish:6x": "git checkout 6.x && git merge 6.x && npm run docs:clean:stable && env DOCS_DEPLOY=true npm run docs:generate && mv ./docs/6.x ./tmp && git checkout gh-pages && npm run docs:copy:tmp:6x", + "docs:prepare:publish:7x": "env DOCS_DEPLOY=true npm run docs:generate && git checkout gh-pages && rm -rf ./docs/7.x && mv ./tmp ./docs/7.x", + "docs:prepare:publish:8x": "env DOCS_DEPLOY=true npm run docs:generate && git checkout gh-pages && rm -rf ./docs/8.x && mv ./tmp ./docs/8.x", + "docs:check-links": "linkinator http://127.0.0.1:8089 --silent --recurse --skip cdn.carbonads.com --skip m.servedby-buysellads.com --skip \"127\\.0\\.0\\.1:8089/docs/\\d+.x\"", + "docs:update-mongodb-links": "node ./scripts/update-mongodb-links.js", + "lint": "eslint .", + "lint-js": "eslint . --ext .js --ext .cjs", + "lint-ts": "eslint . --ext .ts", + "lint-md": "markdownlint-cli2 \"docs/**/*.md\" \"docs/*.md\" \"*.md\"", + "release": "git pull && git push origin master --tags && npm publish", + "release-5x": "git pull origin 5.x && git push origin 5.x && git push origin 5.x --tags && npm publish --tag 5x", + "release-6x": "git pull origin 6.x && git push origin 6.x && git push origin 6.x --tags && npm publish --tag 6x", + "mongo": "node ./tools/repl.js", + "publish-7x": "npm publish --tag 7x", + "create-separate-require-instance": "rm -rf ./node_modules/mongoose-separate-require-instance && node ./scripts/create-tarball && tar -xzf mongoose.tgz -C ./node_modules && mv ./node_modules/package ./node_modules/mongoose-separate-require-instance", + "test": "mocha --exit --ignore \"test/encryption/**/*.test.js\" \"./test/**/*.test.js\"", + "test:ci": "npm run test -- --reporter min --timeout 10000", + "test-deno": "deno run --allow-env --allow-read --allow-net --allow-run --allow-sys --allow-write ./test/deno.mjs", + "test-deno:ci": "npm run test-deno -- --reporter min", + "test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit --ignore \"test/encryption/**/*.test.js\" \"./test/**/*.test.js\"", + "test-rs:ci": "npm run test-rs -- --reporter min", + "test:types": "tstyche", + "setup-test-encryption": "node scripts/setup-encryption-tests.js", + "test-encryption": "mocha --exit ./test/encryption/*.test.js", + "test-encryption:ci": "npm run test-encryption -- --reporter min", + "tdd": "mocha --watch --inspect --ignore \"test/encryption/**/*.test.js\" \"./test/**/*.test.js\" --watch-files lib/**/*.js test/**/*.js", + "test-coverage": "c8 --reporter=html --reporter=text npm test", + "test-coverage:ci": "c8 --reporter=html --reporter=text npm run test:ci", + "ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check", + "ts-benchmark:local": "node ./scripts/create-tarball && cd ./benchmarks/typescript/simple && rm -rf ./node_modules && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check", + "attest-benchmark": "node ./benchmarks/typescript/infer.bench.mts" + }, + "main": "./index.js", + "types": "./types/index.d.ts", + "engines": { + "node": ">=20.19.0" + }, + "bugs": { + "url": "https://github.com/Automattic/mongoose/issues/new" + }, + "repository": { + "type": "git", + "url": "git://github.com/Automattic/mongoose.git" + }, + "homepage": "https://mongoosejs.com", + "config": { + "mongodbMemoryServer": { + "disablePostinstall": true + } + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/mongoose" + } +} diff --git a/gateway/node_modules/mongoose/tstyche.json b/gateway/node_modules/mongoose/tstyche.json new file mode 100644 index 0000000000000000000000000000000000000000..5a02c9c502fd239ee70df8982aa20f68e6a40a8c --- /dev/null +++ b/gateway/node_modules/mongoose/tstyche.json @@ -0,0 +1,7 @@ +{ + "$schema": "https://tstyche.org/schemas/config.json", + "testFileMatch": [ + "test/types/*.test.*", + "test/types/**/*.test.*" + ] +} diff --git a/gateway/node_modules/mongoose/types/aggregate.d.ts b/gateway/node_modules/mongoose/types/aggregate.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..bd3aae80b0b5af5efd15cac2396b66c99a5a1780 --- /dev/null +++ b/gateway/node_modules/mongoose/types/aggregate.d.ts @@ -0,0 +1,185 @@ +declare module 'mongoose' { + import mongodb = require('mongodb'); + + /** Extract generic type from Aggregate class */ + type AggregateExtract

= P extends Aggregate ? T : never; + + interface AggregateOptions extends Omit, SessionOption { + /** set to `false` to skip all user-defined middleware, or `{ pre: false }` / `{ post: false }` to skip only pre or post hooks */ + middleware?: boolean | SkipMiddlewareOptions; + [key: string]: any; + } + + class Aggregate implements SessionOperation { + /** + * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js) + * You do not need to call this function explicitly, the JavaScript runtime + * will call it for you. + */ + [Symbol.asyncIterator](): AsyncIterableIterator>; + + // Returns a string representation of this aggregation. + [Symbol.toStringTag]: string; + + options: AggregateOptions; + + /** + * Sets an option on this aggregation. This function will be deprecated in a + * future release. + * + * @deprecated + */ + addCursorFlag(flag: CursorFlag, value: boolean): this; + + /** + * Appends a new $addFields operator to this aggregate pipeline. + * Requires MongoDB v3.4+ to work + */ + addFields(arg: PipelineStage.AddFields['$addFields']): this; + + /** Sets the allowDiskUse option for the aggregation query */ + allowDiskUse(value: boolean): this; + + /** Appends new operators to this aggregate pipeline */ + append(...args: PipelineStage[]): this; + + /** + * Executes the query returning a `Promise` which will be + * resolved with either the doc(s) or rejected with the error. + * Like [`.then()`](#query_Query-then), but only takes a rejection handler. + */ + catch: Promise['catch']; + + /** Set the collation. */ + collation(options: mongodb.CollationOptions): this; + + /** Appends a new $count operator to this aggregate pipeline. */ + count(fieldName: PipelineStage.Count['$count']): this; + + /** Appends a new $densify operator to this aggregate pipeline */ + densify(arg: PipelineStage.Densify['$densify']): this; + + /** + * Sets the cursor option for the aggregation query + */ + cursor(options?: Record): Cursor; + + + /** Executes the aggregate pipeline on the currently bound Model. */ + exec(): Promise; + + /** Execute the aggregation with explain */ + explain(verbosity: mongodb.ExplainVerbosityLike): Promise; + explain(): Promise; + + /** Combines multiple aggregation pipelines. */ + facet(options: PipelineStage.Facet['$facet']): this; + + /** Appends a new $fill operator to this aggregate pipeline */ + fill(arg: PipelineStage.Fill['$fill']): this; + + /** + * Executes the aggregation returning a `Promise` which will be + * resolved with `.finally()` chained. + */ + finally: Promise['finally']; + + /** Appends new custom $graphLookup operator(s) to this aggregate pipeline, performing a recursive search on a collection. */ + graphLookup(options: PipelineStage.GraphLookup['$graphLookup']): this; + + /** Appends new custom $group operator to this aggregate pipeline. */ + group(arg: PipelineStage.Group['$group']): this; + + /** Sets the hint option for the aggregation query */ + hint(value: Record | string): this; + + /** + * Appends a new $limit operator to this aggregate pipeline. + * @param num maximum number of records to pass to the next stage + */ + limit(num: PipelineStage.Limit['$limit']): this; + + /** Appends new custom $lookup operator to this aggregate pipeline. */ + lookup(options: PipelineStage.Lookup['$lookup']): this; + + /** + * Appends a new custom $match operator to this aggregate pipeline. + * @param arg $match operator contents + */ + match(arg: PipelineStage.Match['$match']): this; + + /** + * Binds this aggregate to a model. + * @param model the model to which the aggregate is to be bound + */ + model(model: Model): this; + + /** + * Returns the current model bound to this aggregate object + */ + model(): Model; + + /** Appends a new $geoNear operator to this aggregate pipeline. */ + near(arg: PipelineStage.GeoNear['$geoNear']): this; + + /** Returns the current pipeline */ + pipeline(): PipelineStage[]; + + /** Returns the current pipeline typed for use in `$unionWith` */ + pipelineForUnionWith(): PipelineStage.UnionWithPipelineStage[]; + + /** Appends a new $project operator to this aggregate pipeline. */ + project(arg: PipelineStage.Project['$project'] | string): this; + + /** Sets the readPreference option for the aggregation query. */ + read(pref: mongodb.ReadPreferenceLike): this; + + /** Sets the readConcern level for the aggregation query. */ + readConcern(level: string): this; + + /** Appends a new $redact operator to this aggregate pipeline. */ + redact(expression: PipelineStage.Redact['$redact'], thenExpr: '$$DESCEND' | '$$PRUNE' | '$$KEEP' | AnyObject, elseExpr: '$$DESCEND' | '$$PRUNE' | '$$KEEP' | AnyObject): this; + + /** Appends a new $replaceRoot operator to this aggregate pipeline. */ + replaceRoot(newRoot: PipelineStage.ReplaceRoot['$replaceRoot']['newRoot'] | string): this; + + /** + * Helper for [Atlas Text Search](https://www.mongodb.com/docs/atlas/atlas-search/tutorial/)'s + * `$search` stage. + */ + search(options: PipelineStage.Search['$search']): this; + + /** Lets you set arbitrary options, for middlewares or plugins. */ + option(value: AggregateOptions): this; + + /** Appends new custom $sample operator to this aggregate pipeline. */ + sample(arg: PipelineStage.Sample['$sample']['size']): this; + + /** Sets the session for this aggregation. Useful for [transactions](/docs/transactions.html). */ + session(session: mongodb.ClientSession | null): this; + + /** + * Appends a new $skip operator to this aggregate pipeline. + * @param num number of records to skip before next stage + */ + skip(num: PipelineStage.Skip['$skip']): this; + + /** Appends a new $sort operator to this aggregate pipeline. */ + sort(arg: string | Record | PipelineStage.Sort['$sort']): this; + + /** Provides promise for aggregate. */ + then: Promise['then']; + + /** + * Appends a new $sortByCount operator to this aggregate pipeline. Accepts either a string field name + * or a pipeline object. + */ + sortByCount(arg: string | PipelineStage.SortByCount['$sortByCount']): this; + + /** Appends new $unionWith operator to this aggregate pipeline. */ + unionWith(options: PipelineStage.UnionWith['$unionWith']): this; + + /** Appends new custom $unwind operator(s) to this aggregate pipeline. */ + unwind(...args: PipelineStage.Unwind['$unwind'][]): this; + } +} diff --git a/gateway/node_modules/mongoose/types/augmentations.d.ts b/gateway/node_modules/mongoose/types/augmentations.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..4d10d06639b58e322836bc859787c3907d5c531c --- /dev/null +++ b/gateway/node_modules/mongoose/types/augmentations.d.ts @@ -0,0 +1,9 @@ +// this import is required so that types get merged instead of completely overwritten +import 'mongodb'; + +declare module 'mongodb' { + interface ObjectId { + /** Mongoose automatically adds a conveniency "_id" getter on the base ObjectId class */ + _id: this; + } +} diff --git a/gateway/node_modules/mongoose/types/callback.d.ts b/gateway/node_modules/mongoose/types/callback.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..370379ab4bfc22348d7a5b732878d82a2675ed96 --- /dev/null +++ b/gateway/node_modules/mongoose/types/callback.d.ts @@ -0,0 +1,8 @@ +declare module 'mongoose' { + type CallbackError = NativeError | null; + + type Callback = (error: CallbackError, result: T) => void; + + type CallbackWithoutResult = (error: CallbackError) => void; + type CallbackWithoutResultAndOptionalError = (error?: CallbackError) => void; +} diff --git a/gateway/node_modules/mongoose/types/collection.d.ts b/gateway/node_modules/mongoose/types/collection.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..7b61079392b004c4a2a13b08d39377650040f87c --- /dev/null +++ b/gateway/node_modules/mongoose/types/collection.d.ts @@ -0,0 +1,49 @@ +declare module 'mongoose' { + import mongodb = require('mongodb'); + + export class BaseCollection extends mongodb.Collection { + /** + * Collection constructor + * @param name name of the collection + * @param conn A MongooseConnection instance + * @param opts optional collection options + */ + constructor(name: string, conn: Connection, opts?: any); + + /* + * Abstract methods. Some of these are already defined on the + * mongodb.Collection interface so they've been commented out. + */ + ensureIndex(...args: any[]): any; + findAndModify(...args: any[]): any; + getIndexes(...args: any[]): any; + + /** Formatter for debug print args */ + $format(arg: any, color?: boolean, shell?: boolean): string; + /** Debug print helper */ + $print(name: string, i: string | number, args: any[], color?: boolean, shell?: boolean): void; + + /** The collection name */ + get collectionName(): string; + /** The Connection instance */ + conn: Connection; + /** The collection name */ + name: string; + } + + /* + * section drivers/node-mongodb-native/collection.js + */ + class Collection extends BaseCollection { + /** + * Collection constructor + * @param name name of the collection + * @param conn A MongooseConnection instance + * @param opts optional collection options + */ + constructor(name: string, conn: Connection, opts?: any); + + /** Retrieves information about this collections indexes. */ + getIndexes(): ReturnType['indexInformation']>; + } +} diff --git a/gateway/node_modules/mongoose/types/connection.d.ts b/gateway/node_modules/mongoose/types/connection.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..e18e337aad16b197c5cd36f241a20c8c7cc94719 --- /dev/null +++ b/gateway/node_modules/mongoose/types/connection.d.ts @@ -0,0 +1,297 @@ +declare module 'mongoose' { + import mongodb = require('mongodb'); + import events = require('events'); + + /** The Mongoose module's default connection. Equivalent to `mongoose.connections[0]`, see [`connections`](#mongoose_Mongoose-connections). */ + const connection: Connection; + + /** An array containing all connections associated with this Mongoose instance. */ + const connections: Connection[]; + + /** Opens Mongoose's default connection to MongoDB, see [connections docs](https://mongoosejs.com/docs/connections.html) */ + function connect(uri: string, options?: ConnectOptions): Promise; + + /** Creates a Connection instance. */ + function createConnection(uri: string, options?: ConnectOptions): Connection; + function createConnection(): Connection; + + function disconnect(): Promise; + + /** + * Connection ready state + * + * - 0 = disconnected + * - 1 = connected + * - 2 = connecting + * - 3 = disconnecting + * - 99 = uninitialized + */ + enum ConnectionStates { + disconnected = 0, + connected = 1, + connecting = 2, + disconnecting = 3, + uninitialized = 99, + } + + /** Expose connection states for user-land */ + const STATES: typeof ConnectionStates; + + interface ConnectOptions extends mongodb.MongoClientOptions { + /** Set to false to [disable buffering](http://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection. */ + bufferCommands?: boolean; + /** The name of the database you want to use. If not provided, Mongoose uses the database name from connection string. */ + dbName?: string; + /** username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility. */ + user?: string; + /** password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility. */ + pass?: string; + /** Set to false to disable automatic index creation for all models associated with this connection. */ + autoIndex?: boolean; + /** Set to `false` to disable Mongoose automatically calling `createCollection()` on every model created on this connection. */ + autoCreate?: boolean; + /** + * Sanitizes query filters against [query selector injection attacks]( + * https://thecodebarbarian.com/2014/09/04/defending-against-query-selector-injection-attacks.html + * ) by wrapping any nested objects that have a property whose name starts with $ in a $eq. + */ + sanitizeFilter?: boolean; + } + + export type AnyConnectionBulkWriteModel = Omit, 'namespace'> + | Omit, 'namespace'> + | Omit, 'namespace'> + | Omit, 'namespace'> + | Omit, 'namespace'> + | Omit, 'namespace'>; + + export type ConnectionBulkWriteModel = Record> = { + [ModelName in keyof SchemaMap]: AnyConnectionBulkWriteModel & { + model: ModelName; + }; + }[keyof SchemaMap]; + + class Connection extends events.EventEmitter implements SessionStarter { + /** Runs a [db-level aggregate()](https://www.mongodb.com/docs/manual/reference/method/db.aggregate/) on this connection's underlying `db` */ + aggregate(pipeline?: PipelineStage[] | null, options?: AggregateOptions): Aggregate>; + + /** Returns a promise that resolves when this connection successfully connects to MongoDB */ + asPromise(): Promise; + + /** The Mongoose instance this connection is associated with */ + base: Mongoose; + + bulkWrite>( + ops: Array>, + options: mongodb.ClientBulkWriteOptions & { ordered: false } + ): Promise } }>; + bulkWrite>( + ops: Array>, + options?: mongodb.ClientBulkWriteOptions + ): Promise; + + /** Closes the connection */ + close(force?: boolean): Promise; + + /** Closes and destroys the connection. Connection once destroyed cannot be reopened */ + destroy(force?: boolean): Promise; + + /** Retrieves a collection, creating it if not cached. */ + collection(name: string, options?: mongodb.CreateCollectionOptions): Collection; + + /** A hash of the collections associated with this connection */ + readonly collections: { [index: string]: Collection }; + + /** A hash of the global options that are associated with this connection */ + readonly config: any; + + /** The mongodb.Db instance, set when the connection is opened */ + readonly db: mongodb.Db | undefined; + + /** + * Helper for `createCollection()`. Will explicitly create the given collection + * with specified options. Used to create [capped collections](https://www.mongodb.com/docs/manual/core/capped-collections/) + * and [views](https://www.mongodb.com/docs/manual/core/views/) from mongoose. + */ + createCollection(name: string, options?: mongodb.CreateCollectionOptions): Promise>; + + /** + * https://mongoosejs.com/docs/api/connection.html#Connection.prototype.createCollections() + */ + createCollections(continueOnError?: boolean): Promise>>; + + /** + * Removes the model named `name` from this connection, if it exists. You can + * use this function to clean up any models you created in your tests to + * prevent OverwriteModelErrors. + */ + deleteModel(name: string | RegExp): this; + + /** + * Helper for `dropCollection()`. Will delete the given collection, including + * all documents and indexes. + */ + dropCollection(collection: string): Promise; + + /** + * Helper for `dropDatabase()`. Deletes the given database, including all + * collections, documents, and indexes. + */ + dropDatabase(): Promise; + + /** Gets the value of the option `key`. */ + get(key: string): any; + + /** + * Returns the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/7.0/classes/MongoClient.html) instance + * that this connection uses to talk to MongoDB. + */ + getClient(): mongodb.MongoClient; + + /** + * The host name portion of the URI. If multiple hosts, such as a replica set, + * this will contain the first host name in the URI + */ + readonly host: string; + + /** + * A number identifier for this connection. Used for debugging when + * you have [multiple connections](/docs/connections.html#multiple_connections). + */ + readonly id: number; + + /** + * Helper for MongoDB Node driver's `listCollections()`. + * Returns an array of collection names. + */ + listCollections(): Promise[]>; + + /** + * Helper for MongoDB Node driver's `listDatabases()`. + * Returns an array of database names. + */ + listDatabases(): Promise; + + /** + * A [POJO](https://masteringjs.io/tutorials/fundamentals/pojo) containing + * a map from model names to models. Contains all models that have been + * added to this connection using [`Connection#model()`](/docs/api/connection.html#connection_Connection-model). + */ + readonly models: Readonly<{ [index: string]: Model }>; + + /** Defines or retrieves a model. */ + model( + name: string, + schema?: TSchema, + collection?: string, + options?: CompileModelOptions + ): Model< + InferSchemaType, + ObtainSchemaGeneric, + ObtainSchemaGeneric, + ObtainSchemaGeneric, + // If first schema generic param is set, that means we have an explicit raw doc type, + // so user should also specify a hydrated doc type if the auto inferred one isn't correct. + IsItRecordAndNotAny> extends true + ? ObtainSchemaGeneric + : HydratedDocument< + InferSchemaType, + ObtainSchemaGeneric & ObtainSchemaGeneric, + ObtainSchemaGeneric, + ObtainSchemaGeneric, + InferSchemaType, + ObtainSchemaGeneric + >, + TSchema, + ObtainSchemaGeneric + > & ObtainSchemaGeneric; + model( + name: string, + schema?: Schema, + collection?: string, + options?: CompileModelOptions + ): U; + model(name: string, schema?: Schema, collection?: string, options?: CompileModelOptions): Model; + + /** Returns an array of model names created on this connection. */ + modelNames(): Array; + + /** The name of the database this connection points to. */ + readonly name: string; + + /** Opens the connection with a URI using `MongoClient.connect()`. */ + openUri(uri: string, options?: ConnectOptions): Promise; + + /** The password specified in the URI */ + readonly pass: string; + + /** + * The port portion of the URI. If multiple hosts, such as a replica set, + * this will contain the port from the first host name in the URI. + */ + readonly port: number; + + /** Declares a plugin executed on all schemas you pass to `conn.model()` */ + plugin(fn: (schema: S, opts?: any) => void, opts?: O): Connection; + + /** The plugins that will be applied to all models created on this connection. */ + plugins: Array; + + /** + * Connection ready state + * + * - 0 = disconnected + * - 1 = connected + * - 2 = connecting + * - 3 = disconnecting + * - 99 = uninitialized + */ + readonly readyState: ConnectionStates; + + /** Sets the value of the option `key`. */ + set(key: string, value: any): any; + + /** + * Set the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/7.0/classes/MongoClient.html) instance + * that this connection uses to talk to MongoDB. This is useful if you already have a MongoClient instance, and want to + * reuse it. + */ + setClient(client: mongodb.MongoClient): this; + + /** + * _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://www.mongodb.com/docs/manual/release-notes/3.6/#client-sessions) + * for benefits like causal consistency, [retryable writes](https://www.mongodb.com/docs/manual/core/retryable-writes/), + * and [transactions](http://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html). + */ + startSession(options?: ClientSessionOptions): Promise; + + /** + * Makes the indexes in MongoDB match the indexes defined in every model's + * schema. This function will drop any indexes that are not defined in + * the model's schema except the `_id` index, and build any indexes that + * are in your schema but not in MongoDB. + */ + syncIndexes(options?: SyncIndexesOptions): Promise; + + /** + * _Requires MongoDB >= 3.6.0._ Executes the wrapped async function + * in a transaction. Mongoose will commit the transaction if the + * async function executes successfully and attempt to retry if + * there was a retryable error. + */ + transaction(fn: (session: mongodb.ClientSession) => Promise, options?: mongodb.TransactionOptions): Promise; + + /** Switches to a different database using the same connection pool. */ + useDb(name: string, options?: { useCache?: boolean }): Connection; + + /** The username specified in the URI */ + readonly user: string; + + /** Watches the entire underlying database for changes. Similar to [`Model.watch()`](/docs/api/model.html#model_Model-watch). */ + watch(pipeline?: Array, options?: mongodb.ChangeStreamOptions): mongodb.ChangeStream; + + withSession(executor: (session: ClientSession) => Promise): Promise; + } + + export class BaseConnection extends Connection {} +} diff --git a/gateway/node_modules/mongoose/types/cursor.d.ts b/gateway/node_modules/mongoose/types/cursor.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..ced66e564cc61221303588ba7e676d248f58f7fd --- /dev/null +++ b/gateway/node_modules/mongoose/types/cursor.d.ts @@ -0,0 +1,67 @@ +declare module 'mongoose' { + + import stream = require('stream'); + + type CursorFlag = 'tailable' | 'oplogReplay' | 'noCursorTimeout' | 'awaitData' | 'partial'; + + interface EachAsyncOptions { + parallel?: number; + batchSize?: number; + continueOnError?: boolean; + signal?: AbortSignal; + } + + class Cursor extends stream.Readable { + [Symbol.asyncIterator](): Cursor, Options, IteratorResult>; + + [Symbol.asyncDispose](): Promise; + + /** + * Adds a [cursor flag](https://mongodb.github.io/node-mongodb-native/7.0/classes/FindCursor.html#addCursorFlag). + * Useful for setting the `noCursorTimeout` and `tailable` flags. + */ + addCursorFlag(flag: CursorFlag, value: boolean): this; + + /** + * Marks this cursor as closed. Will stop streaming and subsequent calls to + * `next()` will error. + */ + close(): Promise; + + /** + * Destroy this cursor, closing the underlying cursor. Will stop streaming + * and subsequent calls to `next()` will error. + */ + destroy(): this; + + /** + * Rewind this cursor to its uninitialized state. Any options that are present on the cursor will + * remain in effect. Iterating this cursor will cause new queries to be sent to the server, even + * if the resultant data has already been retrieved by this cursor. + */ + rewind(): this; + + /** + * Execute `fn` for every document(s) in the cursor. If batchSize is provided + * `fn` will be executed for each batch of documents. If `fn` returns a promise, + * will wait for the promise to resolve before iterating on to the next one. + * Returns a promise that resolves when done. + */ + eachAsync(fn: (doc: DocType[], i: number) => any, options: EachAsyncOptions & { batchSize: number }): Promise; + eachAsync(fn: (doc: DocType, i: number) => any, options?: EachAsyncOptions): Promise; + + /** + * Registers a transform function which subsequently maps documents retrieved + * via the streams interface or `.next()` + */ + map(fn: (res: DocType) => ResultType): Cursor; + + /** + * Get the next document from this cursor. Will return `null` when there are + * no documents left. + */ + next(): Promise; + + options: Options; + } +} diff --git a/gateway/node_modules/mongoose/types/document.d.ts b/gateway/node_modules/mongoose/types/document.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..523a3dd8419e37d65c3fdc0602f79b3246ecfd7d --- /dev/null +++ b/gateway/node_modules/mongoose/types/document.d.ts @@ -0,0 +1,345 @@ +declare module 'mongoose' { + import mongodb = require('mongodb'); + + /** A list of paths to skip. If set, Mongoose will validate every modified path that is not in this list. */ + type pathsToSkip = string[] | string; + + interface ValidateOptions { + /** List of paths to skip. If set, Mongoose will validate every modified path that is not in this list. */ + pathsToSkip?: pathsToSkip; + /** set to `false` to skip all user-defined middleware, or `{ pre: false }` / `{ post: false }` to skip only pre or post hooks */ + middleware?: boolean | SkipMiddlewareOptions; + } + + interface ValidateSyncOptions extends Omit { + /** `validateSync()` does not run middleware. Use `validate()` if you need middleware. */ + middleware?: never; + [k: string]: any; + } + + interface DocumentSetOptions { + merge?: boolean; + + [key: string]: any; + } + + class ModifiedPathsSnapshot {} + + /** + * Generic types for Document: + * * T - the type of _id + * * TQueryHelpers - Object with any helpers that should be mixed into the Query type + * * DocType - the type of the actual Document created + */ + class Document, TSchemaOptions = {}> { + constructor(doc?: any); + + /** This documents _id. */ + _id: T; + + /** Assert that a given path or paths is populated. Throws an error if not populated. */ + $assertPopulated(path: string | string[], values?: Partial): PopulateDocumentResult, DocType>; + + /** Clear the document's modified paths. */ + $clearModifiedPaths(): this; + + /** Returns a deep clone of this document */ + $clone(): this; + + /** + * Creates a snapshot of this document's internal change tracking state. You can later + * reset this document's change tracking state using `$restoreModifiedPathsSnapshot()`. + */ + $createModifiedPathsSnapshot(): ModifiedPathsSnapshot; + + /* Get all subdocs (by bfs) */ + $getAllSubdocs(): Document[]; + + /** Don't run validation on this path or persist changes to this path. */ + $ignore(path: string): void; + + /** Checks if a path is set to its default. If no path set, checks if any path is set to its default. */ + $isDefault(path?: string): boolean; + + /** Getter/setter, determines whether the document was removed or not. */ + $isDeleted(val?: boolean): boolean; + + /** Returns an array of all populated documents associated with the query */ + $getPopulatedDocs(): Document[]; + + /** + * Increments the numeric value at `path` by the given `val`. + * When you call `save()` on this document, Mongoose will send a + * `$inc` as opposed to a `$set`. + */ + $inc(path: string | string[], val?: number): this; + + /** + * Returns true if the given path is nullish or only contains empty objects. + * Useful for determining whether this subdoc will get stripped out by the + * [minimize option](/docs/guide.html#minimize). + */ + $isEmpty(path: string): boolean; + + /** Checks if a path is invalid */ + $isValid(path: string): boolean; + + /** + * Empty object that you can use for storing properties on the document. This + * is handy for passing data to middleware without conflicting with Mongoose + * internals. + */ + $locals: Record; + + /** Marks a path as valid, removing existing validation errors. */ + $markValid(path: string): void; + + /** Returns the model with the given name on this document's associated connection. */ + $model>(name: string): ModelType; + $model>(): ModelType; + + /** + * A string containing the current operation that Mongoose is executing + * on this document. Can be `null`, `'save'`, `'validate'`, or `'remove'`. + */ + $op: 'save' | 'validate' | 'remove' | null; + + /** + * Restore this document's change tracking state to the given snapshot. + * Note that `$restoreModifiedPathsSnapshot()` does **not** modify the document's + * properties, just resets the change tracking state. + */ + $restoreModifiedPathsSnapshot(snapshot: ModifiedPathsSnapshot): this; + + /** + * Getter/setter around the session associated with this document. Used to + * automatically set `session` if you `save()` a doc that you got from a + * query with an associated session. + */ + $session(session?: ClientSession | null): ClientSession | null; + + /** Alias for `set()`, used internally to avoid conflicts */ + $set(path: string | Record, val: any, type: any, options?: DocumentSetOptions): this; + $set(path: string | Record, val: any, options?: DocumentSetOptions): this; + $set(value: string | Record): this; + + /** Set this property to add additional query filters when Mongoose saves this document and `isNew` is false. */ + $where: Record; + + /** If this is a discriminator model, `baseModelName` is the name of the base model. */ + baseModelName?: string; + + /** Collection the model uses. */ + collection: Collection; + + /** Connection the model uses. */ + db: Connection; + + /** Removes this document from the db. */ + deleteOne(options?: QueryOptions): QueryWithHelpers< + mongodb.DeleteResult, + this, + TQueryHelpers, + DocType, + 'deleteOne' + >; + + /** + * Takes a populated field and returns it to its unpopulated state. If called with + * no arguments, then all populated fields are returned to their unpopulated state. + */ + depopulate(path?: string | string[]): MergeType; + + /** + * Returns the list of paths that have been directly modified. A direct + * modified path is a path that you explicitly set, whether via `doc.foo = 'bar'`, + * `Object.assign(doc, { foo: 'bar' })`, or `doc.set('foo', 'bar')`. + */ + directModifiedPaths(): Array; + + /** + * Returns true if this document is equal to another document. + * + * Documents are considered equal when they have matching `_id`s, unless neither + * document has an `_id`, in which case this function falls back to using + * `deepEqual()`. + */ + equals(doc: Document): boolean; + + /** Returns the current validation errors. */ + errors?: Error.ValidationError; + + /** Returns the value of a path. */ + get(path: T, type?: any, options?: any): DocType[T]; + get(path: string, type?: any, options?: any): any; + + /** + * Returns the changes that happened to the document + * in the format that will be sent to MongoDB. + */ + $getChanges(): UpdateQuery; + + /** + * Returns the changes that happened to the document + * in the format that will be sent to MongoDB. + * @deprecated Use `$getChanges()` instead. + */ + getChanges(): UpdateQuery; + + /** Signal that we desire an increment of this documents version. */ + increment(): this; + + /** + * Initializes the document without setters or marking anything modified. + * Called internally after a document is returned from mongodb. Normally, + * you do **not** need to call this function on your own. + */ + init(obj: AnyObject, opts?: AnyObject): this; + + /** Marks a path as invalid, causing validation to fail. */ + invalidate(path: T, errorMsg: string | NativeError, value?: any, kind?: string): NativeError | null; + invalidate(path: string, errorMsg: string | NativeError, value?: any, kind?: string): NativeError | null; + + /** Returns true if `path` was directly set and modified, else false. */ + isDirectModified(path: T | Array): boolean; + isDirectModified(path: string | Array): boolean; + + /** Checks if `path` was explicitly selected. If no projection, always returns true. */ + isDirectSelected(path: T): boolean; + isDirectSelected(path: string): boolean; + + /** Checks if `path` is in the `init` state, that is, it was set by `Document#init()` and not modified since. */ + isInit(path: T): boolean; + isInit(path: string): boolean; + + /** + * Returns true if any of the given paths are modified, else false. If no arguments, returns `true` if any path + * in this document is modified. + */ + isModified(path?: T | Array, options?: { ignoreAtomics?: boolean } | null): boolean; + isModified(path?: string | Array, options?: { ignoreAtomics?: boolean } | null): boolean; + + /** Boolean flag specifying if the document is new. */ + isNew: boolean; + + /** Checks if `path` was selected in the source query which initialized this document. */ + isSelected(path: T): boolean; + isSelected(path: string): boolean; + + /** Marks the path as having pending changes to write to the db. */ + markModified(path: T, scope?: any): void; + markModified(path: string, scope?: any): void; + + /** Returns the model with the given name on this document's associated connection. */ + model>(name: string): ModelType; + model>(): ModelType; + + /** Returns the list of paths that have been modified. */ + modifiedPaths(options?: { includeChildren?: boolean }): Array; + + /** + * Overwrite all values in this document with the values of `obj`, except + * for immutable properties. Behaves similarly to `set()`, except for it + * unsets all properties that aren't in `obj`. + */ + overwrite(obj: AnyObject): this; + + /** + * If this document is a subdocument or populated document, returns the + * document's parent. Returns undefined otherwise. + */ + $parent(): Document | undefined; + + /** Populates document references. */ + populate(path: string | PopulateOptions | (string | PopulateOptions)[]): Promise, DocType>>; + populate(path: string, select?: string | AnyObject, model?: Model, match?: AnyObject, options?: PopulateOptions): Promise, DocType>>; + + /** Gets _id(s) used during population of the given `path`. If the path was not populated, returns `undefined`. */ + populated(path: string): any; + + /** Sends a replaceOne command with this document `_id` as the query selector. */ + replaceOne(replacement?: AnyObject, options?: QueryOptions | null): Query; + + /** Saves this document by inserting a new document into the database if [document.isNew](/docs/api/document.html#document_Document-isNew) is `true`, or sends an [updateOne](/docs/api/document.html#document_Document-updateOne) operation with just the modified paths if `isNew` is `false`. */ + save(options?: SaveOptions): Promise; + + /** The document's schema. */ + schema: Schema; + + /** Sets the value of a path, or many paths. */ + set(path: T, val: DocType[T], type: any, options?: DocumentSetOptions): this; + set(path: string | Record, val: any, type: any, options?: DocumentSetOptions): this; + set(path: string | Record, val: any, options?: DocumentSetOptions): this; + set(value: string | Record): this; + + toBSON(): Require_id; + + /** The return value of this method is used in calls to JSON.stringify(doc). */ + toJSON( + this: PopulatedDocumentMarker, + options: { depopulate: true } + ): Default__v, TSchemaOptions>; + toJSON( + this: PopulatedDocumentMarker, + options: O + ): ToObjectReturnType; + toJSON( + this: PopulatedDocumentMarker, + options: O + ): ToObjectReturnType; + toJSON( + this: PopulatedDocumentMarker + ): DefaultToObjectReturnType; + toJSON(options: O): ToObjectReturnType; + toJSON(options?: ToObjectOptions): DefaultToObjectReturnType; + toJSON(options?: ToObjectOptions): Default__v, ResolveSchemaOptions>; + + /** Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)). */ + toObject( + this: PopulatedDocumentMarker, + options: { depopulate: true } + ): Default__v, TSchemaOptions>; + toObject( + this: PopulatedDocumentMarker, + options: O + ): ToObjectReturnType; + toObject( + this: PopulatedDocumentMarker, + options: O + ): ToObjectReturnType; + toObject( + this: PopulatedDocumentMarker + ): DefaultToObjectReturnType; + toObject(options: O): ToObjectReturnType; + toObject(options?: ToObjectOptions): DefaultToObjectReturnType; + toObject(options?: ToObjectOptions): Default__v, ResolveSchemaOptions>; + + /** Clears the modified state on the specified path. */ + unmarkModified(path: T): void; + unmarkModified(path: string): void; + + /** Sends an updateOne command with this document `_id` as the query selector. */ + updateOne(update?: UpdateQuery | UpdateWithAggregationPipeline, options?: QueryOptions | null): Query; + + /** Executes registered validation rules for this document. */ + validate(pathsToValidate?: T | T[], options?: Omit & AnyObject): Promise; + validate(pathsToValidate?: pathsToValidate, options?: Omit & AnyObject): Promise; + validate(options: ValidateOptions): Promise; + + /** + * Executes registered validation rules (skipping asynchronous validators) for this document. + * @deprecated Use `validate()` instead. `validateSync()` does not run middleware and will be removed in Mongoose 10. + */ + validateSync(options: ValidateSyncOptions): Error.ValidationError | null; + /** + * Executes registered validation rules (skipping asynchronous validators) for this document. + * @deprecated Use `validate()` instead. `validateSync()` does not run middleware and will be removed in Mongoose 10. + */ + validateSync(pathsToValidate?: T | T[], options?: Omit): Error.ValidationError | null; + /** + * Executes registered validation rules (skipping asynchronous validators) for this document. + * @deprecated Use `validate()` instead. `validateSync()` does not run middleware and will be removed in Mongoose 10. + */ + validateSync(pathsToValidate?: pathsToValidate, options?: Omit): Error.ValidationError | null; + } +} diff --git a/gateway/node_modules/mongoose/types/error.d.ts b/gateway/node_modules/mongoose/types/error.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..cf34cb69fabf9fea8cdb3ce30d0f67128db335a2 --- /dev/null +++ b/gateway/node_modules/mongoose/types/error.d.ts @@ -0,0 +1,143 @@ +declare class NativeError extends global.Error { } + +declare module 'mongoose' { + import mongodb = require('mongodb'); + + type CastError = Error.CastError; + type SyncIndexesError = Error.SyncIndexesError; + + export class MongooseError extends global.Error { + constructor(msg: string); + + /** The type of error. "MongooseError" for generic errors. */ + name: string; + + static messages: any; + + static Messages: any; + } + + class Error extends MongooseError { } + + namespace Error { + + export class CastError extends MongooseError { + name: 'CastError'; + stringValue: string; + kind: string; + value: any; + path: string; + reason?: NativeError | null; + + constructor(type: string, value: any, path: string, reason?: NativeError, schemaType?: SchemaType); + } + export class SyncIndexesError extends MongooseError { + name: 'SyncIndexesError'; + errors?: Record; + + constructor(type: string, value: any, path: string, reason?: NativeError, schemaType?: SchemaType); + } + + export class DivergentArrayError extends MongooseError { + name: 'DivergentArrayError'; + } + + export class MissingSchemaError extends MongooseError { + name: 'MissingSchemaError'; + } + + export class DocumentNotFoundError extends MongooseError { + name: 'DocumentNotFoundError'; + result: any; + numAffected: number; + filter: any; + query: any; + } + + export class ObjectExpectedError extends MongooseError { + name: 'ObjectExpectedError'; + path: string; + } + + export class ObjectParameterError extends MongooseError { + name: 'ObjectParameterError'; + } + + export class OverwriteModelError extends MongooseError { + name: 'OverwriteModelError'; + } + + export class ParallelSaveError extends MongooseError { + name: 'ParallelSaveError'; + } + + export class ParallelValidateError extends MongooseError { + name: 'ParallelValidateError'; + } + + export class MongooseServerSelectionError extends MongooseError { + name: 'MongooseServerSelectionError'; + } + + export class StrictModeError extends MongooseError { + name: 'StrictModeError'; + isImmutableError: boolean; + path: string; + } + + export class ValidationError extends MongooseError { + name: 'ValidationError'; + + errors: { [path: string]: ValidatorError | CastError }; + addError: (path: string, error: ValidatorError | CastError) => void; + + /** Index of the document in insertMany() that failed validation (only set for unordered insertMany) */ + index?: number; + + constructor(instance?: MongooseError); + } + + export class ValidatorError extends MongooseError { + name: 'ValidatorError'; + properties: { + message: string, + type?: string, + path?: string, + value?: any, + reason?: any + }; + kind: string; + path: string; + value: any; + reason?: MongooseError | null; + + constructor(properties: { + message?: string, + type?: string, + path?: string, + value?: any, + reason?: any + }); + } + + export class VersionError extends MongooseError { + name: 'VersionError'; + version: number; + modifiedPaths: Array; + + constructor(doc: Document, currentVersion: number, modifiedPaths: Array); + } + + export class StrictPopulateError extends MongooseError { + name: 'StrictPopulateError'; + path: string; + } + + export class MongooseBulkSaveIncompleteError extends MongooseError { + name: 'MongooseBulkSaveIncompleteError'; + modelName: string; + bulkWriteResult: mongodb.BulkWriteResult; + numDocumentsNotUpdated: number; + } + } +} diff --git a/gateway/node_modules/mongoose/types/expressions.d.ts b/gateway/node_modules/mongoose/types/expressions.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..4a56c8330adc514e6cfba6d9d7d66607c9f408d8 --- /dev/null +++ b/gateway/node_modules/mongoose/types/expressions.d.ts @@ -0,0 +1,3069 @@ +declare module 'mongoose' { + + /** + * [Expressions reference](https://www.mongodb.com/docs/manual/meta/aggregation-quick-reference/#expressions) + */ + type AggregationVariables = + SpecialPathVariables | + '$$NOW' | + '$$CLUSTER_TIME' | + '$$DESCEND' | + '$$PRUNE' | + '$$KEEP'; + + type SpecialPathVariables = + '$$ROOT' | + '$$CURRENT' | + '$$REMOVE'; + + export namespace Expression { + export interface Abs { + /** + * Returns the absolute value of a number. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/abs/#mongodb-expression-exp.-abs + */ + $abs: Path | ArithmeticExpressionOperator; + } + + export interface Add { + /** + * Adds numbers to return the sum, or adds numbers and a date to return a new date. If adding numbers and a date, treats the numbers as milliseconds. Accepts any number of argument expressions, but at most, one expression can resolve to a date. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/add/#mongodb-expression-exp.-add + */ + $add: Expression[]; + } + + export interface Ceil { + /** + * Returns the smallest integer greater than or equal to the specified number. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/ceil/#mongodb-expression-exp.-ceil + */ + $ceil: NumberExpression; + } + + export interface Divide { + /** + * Returns the result of dividing the first number by the second. Accepts two argument expressions. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/divide/#mongodb-expression-exp.-divide + */ + $divide: NumberExpression[]; + } + + export interface Exp { + /** + * Raises e to the specified exponent. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/exp/#mongodb-expression-exp.-exp + */ + $exp: NumberExpression; + } + + export interface Floor { + /** + * Returns the largest integer less than or equal to the specified number. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/floor/#mongodb-expression-exp.-floor + */ + $floor: NumberExpression; + } + + export interface Ln { + /** + * Calculates the natural log of a number. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/ln/#mongodb-expression-exp.-ln + */ + $ln: NumberExpression; + } + + export interface Log { + /** + * Calculates the log of a number in the specified base. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/log/#mongodb-expression-exp.-log + */ + $log: [NumberExpression, NumberExpression]; + } + + export interface Log10 { + /** + * Calculates the log base 10 of a number. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/log10/#mongodb-expression-exp.-log10 + */ + $log10: NumberExpression; + } + + export interface Mod { + /** + * Returns the remainder of the first number divided by the second. Accepts two argument expressions. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/mod/#mongodb-expression-exp.-mod + */ + $mod: [NumberExpression, NumberExpression]; + } + export interface Multiply { + /** + * Multiplies numbers to return the product. Accepts any number of argument expressions. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/multiply/#mongodb-expression-exp.-multiply + */ + $multiply: NumberExpression[]; + } + + export interface Pow { + /** + * Raises a number to the specified exponent. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/pow/#mongodb-expression-exp.-pow + */ + $pow: [NumberExpression, NumberExpression]; + } + + export interface Round { + /** + * Rounds a number to to a whole integer or to a specified decimal place. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/round/#mongodb-expression-exp.-round + */ + $round: [NumberExpression, NumberExpression?]; + } + + export interface Sqrt { + /** + * Calculates the square root. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/sqrt/#mongodb-expression-exp.-sqrt + */ + $sqrt: NumberExpression; + } + + export interface Subtract { + /** + * Returns the result of subtracting the second value from the first. If the two values are numbers, return the difference. If the two values are dates, return the difference in milliseconds. If the two values are a date and a number in milliseconds, return the resulting date. Accepts two argument expressions. If the two values are a date and a number, specify the date argument first as it is not meaningful to subtract a date from a number. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/subtract/#mongodb-expression-exp.-subtract + */ + $subtract: (NumberExpression | DateExpression)[]; + } + + export interface Trunc { + /** + * Truncates a number to a whole integer or to a specified decimal place. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/trunc/#mongodb-expression-exp.-trunc + */ + $trunc: [NumberExpression, NumberExpression?]; + } + + export interface Sin { + /** + * Returns the sine of a value that is measured in radians. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/sin/#mongodb-expression-exp.-sin + */ + $sin: NumberExpression; + } + + export interface Cos { + /** + * Returns the cosine of a value that is measured in radians. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/cos/#mongodb-expression-exp.-cos + */ + $cos: NumberExpression; + } + + export interface Tan { + /** + * Returns the tangent of a value that is measured in radians. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/tan/#mongodb-expression-exp.-tan + */ + $tan: NumberExpression; + } + + export interface Asin { + /** + * Returns the inverse sin (arc sine) of a value in radians. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/asin/#mongodb-expression-exp.-asin + */ + $asin: NumberExpression; + } + + export interface Acos { + /** + * Returns the inverse cosine (arc cosine) of a value in radians. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/acos/#mongodb-expression-exp.-acos + */ + $acos: NumberExpression; + } + + export interface Atan { + /** + * Returns the inverse tangent (arc tangent) of a value in radians. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/atan/#mongodb-expression-exp.-atan + */ + $atan: NumberExpression; + } + + export interface Atan2 { + /** + * Returns the inverse tangent (arc tangent) of y / x in radians, where y and x are the first and second values passed to the expression respectively. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/atan2/#mongodb-expression-exp.-atan2 + */ + $atan2: NumberExpression; + } + + export interface Asinh { + /** + * Returns the inverse hyperbolic sine (hyperbolic arc sine) of a value in radians. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/asinh/#mongodb-expression-exp.-asinh + */ + $asinh: NumberExpression; + } + + export interface Acosh { + /** + * Returns the inverse hyperbolic cosine (hyperbolic arc cosine) of a value in radians. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/acosh/#mongodb-expression-exp.-acosh + */ + $acosh: NumberExpression; + } + + export interface Atanh { + + /** + * Returns the inverse hyperbolic tangent (hyperbolic arc tangent) of a value in radians. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/atanh/#mongodb-expression-exp.-atanh + */ + $atanh: NumberExpression; + } + + export interface Sinh { + /** + * Returns the hyperbolic sine of a value that is measured in radians. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/sinh/#mongodb-expression-exp.-sinh + */ + $sinh: NumberExpression; + } + + export interface Cosh { + /** + * Returns the hyperbolic cosine of a value that is measured in radians. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/cosh/#mongodb-expression-exp.-cosh + */ + $cosh: NumberExpression; + } + + export interface Tanh { + /** + * Returns the hyperbolic tangent of a value that is measured in radians. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/tanh/#mongodb-expression-exp.-tanh + */ + $tanh: NumberExpression; + } + + export interface DegreesToRadians { + /** + * Converts a value from degrees to radians. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/degreesToRadians/#mongodb-expression-exp.-degreesToRadians + */ + $degreesToRadians: NumberExpression; + } + + export interface RadiansToDegrees { + /** + * Converts a value from radians to degrees. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/radiansToDegrees/#mongodb-expression-exp.-radiansToDegrees + */ + $radiansToDegrees: NumberExpression; + } + + export interface Meta { + /** + * Access available per-document metadata related to the aggregation operation. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/#mongodb-expression-exp.-meta + */ + $meta: 'textScore' | 'indexKey'; + } + + export interface DateAdd { + /** + * Adds a number of time units to a date object. + * + * @version 5.0.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateAdd/#mongodb-expression-exp.-dateAdd + */ + $dateAdd: { + /** + * The beginning date, in UTC, for the addition operation. The startDate can be any expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + startDate: DateExpression; + /** + * The unit used to measure the amount of time added to the startDate. The unit is an expression that resolves to one of the following strings: + * - year + * - quarter + * - week + * - month + * - day + * - hour + * - minute + * - second + * - millisecond + */ + unit: StringExpression; + /** + * The number of units added to the startDate. The amount is an expression that resolves to an integer or long. The amount can also resolve to an integral decimal or a double if that value can be converted to a long without loss of precision. + */ + amount: NumberExpression; + /** + * The timezone to carry out the operation. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface DateDiff { + /** + * Returns the difference between two dates. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateDiff/#mongodb-expression-exp.-dateDiff + */ + $dateDiff: { + /** + * The start of the time period. The startDate can be any expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + startDate: DateExpression; + /** + * The end of the time period. The endDate can be any expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + endDate: DateExpression; + /** + * The time measurement unit between the startDate and endDate. It is an expression that resolves to a string: + * - year + * - quarter + * - week + * - month + * - day + * - hour + * - minute + * - second + * - millisecond + */ + unit: StringExpression; + /** + * The timezone to carry out the operation. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + /** + * Used when the unit is equal to week. Defaults to Sunday. The startOfWeek parameter is an expression that resolves to a case insensitive string: + * - monday (or mon) + * - tuesday (or tue) + * - wednesday (or wed) + * - thursday (or thu) + * - friday (or fri) + * - saturday (or sat) + * - sunday (or sun) + */ + startOfWeek?: StringExpression; + } + } + + // TODO: Can be done better + export interface DateFromParts { + /** + * Constructs a BSON Date object given the date's constituent parts. + * + * @version 3.6 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateFromParts/#mongodb-expression-exp.-dateFromParts + */ + $dateFromParts: { + /** + * ISO Week Date Year. Can be any expression that evaluates to a number. + * + * Value range: 1-9999 + * + * If the number specified is outside this range, $dateFromParts errors. Starting in MongoDB 4.4, the lower bound for this value is 1. In previous versions of MongoDB, the lower bound was 0. + */ + isoWeekYear?: NumberExpression; + /** + * Week of year. Can be any expression that evaluates to a number. + * + * Defaults to 1. + * + * Value range: 1-53 + * + * Starting in MongoDB 4.0, if the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples. + */ + isoWeek?: NumberExpression; + /** + * Day of week (Monday 1 - Sunday 7). Can be any expression that evaluates to a number. + * + * Defaults to 1. + * + * Value range: 1-7 + * + * Starting in MongoDB 4.0, if the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples. + */ + isoDayOfWeek?: NumberExpression; + /** + * Calendar year. Can be any expression that evaluates to a number. + * + * Value range: 1-9999 + * + * If the number specified is outside this range, $dateFromParts errors. Starting in MongoDB 4.4, the lower bound for this value is 1. In previous versions of MongoDB, the lower bound was 0. + */ + year?: NumberExpression; + /** + * Month. Can be any expression that evaluates to a number. + * + * Defaults to 1. + * + * Value range: 1-12 + * + * Starting in MongoDB 4.0, if the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples. + */ + month?: NumberExpression; + /** + * Day of month. Can be any expression that evaluates to a number. + * + * Defaults to 1. + * + * Value range: 1-31 + * + * Starting in MongoDB 4.0, if the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples. + */ + day?: NumberExpression; + /** + * Hour. Can be any expression that evaluates to a number. + * + * Defaults to 0. + * + * Value range: 0-23 + * + * Starting in MongoDB 4.0, if the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples. + */ + hour?: NumberExpression; + /** + * Minute. Can be any expression that evaluates to a number. + * + * Defaults to 0. + * + * Value range: 0-59 Starting in MongoDB 4.0, if the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples. + */ + minute?: NumberExpression; + /** + * Second. Can be any expression that evaluates to a number. + * + * Defaults to 0. + * + * Value range: 0-59 + * + * Starting in MongoDB 4.0, if the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples. + */ + second?: NumberExpression; + /** + * Millisecond. Can be any expression that evaluates to a number. + * + * Defaults to 0. + * + * Value range: 0-999 + * + * Starting in MongoDB 4.0, if the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples. + */ + millisecond?: NumberExpression; + /** + * The timezone to carry out the operation. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface DateFromString { + /** + * Converts a date/time string to a date object. + * + * @version 3.6 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateFromString/#mongodb-expression-exp.-dateFromString + */ + $dateFromString: { + dateString: StringExpression; + /** + * The date format specification of the dateString. The format can be any expression that evaluates to a string literal, containing 0 or more format specifiers. For a list of specifiers available, see Format Specifiers. + * + * If unspecified, $dateFromString uses "%Y-%m-%dT%H:%M:%S.%LZ" as the default format. + * @version 4.0 + */ + format?: FormatString; + /** + * The time zone to use to format the date. + * + * Note: If the dateString argument is formatted like '2017-02-08T12:10:40.787Z', in which the 'Z' at the end indicates Zulu time (UTC time zone), you cannot specify the timezone argument. + */ + timezone?: tzExpression; + /** + * Optional. If $dateFromString encounters an error while parsing the given dateString, it outputs the result value of the provided onError expression. This result value can be of any type. + * + * If you do not specify onError, $dateFromString throws an error if it cannot parse dateString. + */ + onError?: Expression; + /** + * Optional. If the dateString provided to $dateFromString is null or missing, it outputs the result value of the provided onNull expression. This result value can be of any type. + * + * If you do not specify onNull and dateString is null or missing, then $dateFromString outputs null. + */ + onNull?: Expression; + }; + } + + export interface DateSubtract { + /** + * Subtracts a number of time units from a date object. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateSubtract/#mongodb-expression-exp.-dateSubtract + */ + $dateSubtract: { + /** + * The beginning date, in UTC, for the subtraction operation. The startDate can be any expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + startDate: DateExpression; + /** + * The unit of time, specified as an expression that must resolve to one of these strings: + * - year + * - quarter + * - week + * - month + * - day + * - hour + * - minute + * - second + * - millisecond + * + * Together, binSize and unit specify the time period used in the $dateTrunc calculation. + */ + unit: StringExpression; + /** + * The number of units subtracted from the startDate. The amount is an expression that resolves to an integer or long. The amount can also resolve to an integral decimal and or a double if that value can be converted to a long without loss of precision. + */ + amount: NumberExpression; + /** + * The timezone to carry out the operation. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface DateToParts { + /** + * Returns a document containing the constituent parts of a date. + * + * @version 3.6 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateToParts/#mongodb-expression-exp.-dateToParts + */ + $dateToParts: { + /** + * The input date for which to return parts. can be any expression that resolves to a Date, a Timestamp, or an ObjectID. For more information on expressions, see Expressions. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + * + * @version 3.6 + */ + timezone?: tzExpression; + /** + * If set to true, modifies the output document to use ISO week date fields. Defaults to false. + */ + iso8601?: boolean; + }; + } + + export interface DateToString { + /** + * Returns the date as a formatted string. + * + * @version 3.6 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateToString/#mongodb-expression-exp.-dateToString + */ + $dateToString: { + /** + * The date to convert to string. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The date format specification. can be any string literal, containing 0 or more format specifiers. For a list of specifiers available, see Format Specifiers. + * + * If unspecified, $dateToString uses "%Y-%m-%dT%H:%M:%S.%LZ" as the default format. + * + * Changed in version 4.0: The format field is optional if featureCompatibilityVersion (fCV) is set to "4.0" or greater. For more information on fCV, see setFeatureCompatibilityVersion. + */ + format?: FormatString; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + * + * @version 3.6 + */ + timezone?: tzExpression; + /** + * The value to return if the date is null or missing. The arguments can be any valid expression. + * + * If unspecified, $dateToString returns null if the date is null or missing. + * + * Changed in version 4.0: Requires featureCompatibilityVersion (fCV) set to "4.0" or greater. For more information on fCV, see setFeatureCompatibilityVersion. + */ + onNull?: Expression; + }; + } + + export interface DateTrunc { + /** + * Truncates a date. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateTrunc/#mongodb-expression-exp.-dateTrunc + */ + $dateTrunc: { + /** + * The date to truncate, specified in UTC. The date can be any expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The unit of time, specified as an expression that must resolve to one of these strings: + * - year + * - quarter + * - week + * - month + * - day + * - hour + * - minute + * - second + * - millisecond + * + * Together, binSize and unit specify the time period used in the $dateTrunc calculation. + */ + unit: StringExpression; + /** + * The numeric time value, specified as an expression that must resolve to a positive non-zero number. Defaults to 1. + * + * Together, binSize and unit specify the time period used in the $dateTrunc calculation. + */ + binSize?: NumberExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + /** + * Used when the unit is equal to week. Defaults to Sunday. The startOfWeek parameter is an expression that resolves to a case insensitive string: + * - monday (or mon) + * - tuesday (or tue) + * - wednesday (or wed) + * - thursday (or thu) + * - friday (or fri) + * - saturday (or sat) + * - sunday (or sun) + */ + startOfWeek?: StringExpression; + } + } + + export interface DayOfMonth { + /** + * Returns the day of the month for a date as a number between 1 and 31. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfMonth/#mongodb-expression-exp.-dayOfMonth + */ + $dayOfMonth: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface DayOfWeek { + /** + * Returns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday). + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/#mongodb-expression-exp.-dayOfWeek + */ + $dayOfWeek: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface DayOfYear { + /** + * Returns the day of the year for a date as a number between 1 and 366 (leap year). + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfYear/#mongodb-expression-exp.-dayOfYear + */ + $dayOfYear: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface Hour { + /** + * Returns the hour for a date as a number between 0 and 23. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/hour/#mongodb-expression-exp.-hour + */ + $hour: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface IsoDayOfWeek { + /** + * Returns the weekday number in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday). + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/isoDayOfWeek/#mongodb-expression-exp.-isoDayOfWeek + */ + $isoDayOfWeek: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface IsoWeek { + /** + * Returns the week number in ISO 8601 format, ranging from 1 to 53. Week numbers start at 1 with the week (Monday through Sunday) that contains the year's first Thursday. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/isoWeek/#mongodb-expression-exp.-isoWeek + */ + $isoWeek: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface IsoWeekYear { + /** + * Returns the year number in ISO 8601 format. The year starts with the Monday of week 1 (ISO 8601) and ends with the Sunday of the last week (ISO 8601). + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/isoWeekYear/#mongodb-expression-exp.-isoWeekYear + */ + $isoWeekYear: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface Millisecond { + /** + * Returns the milliseconds of a date as a number between 0 and 999. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/millisecond/#mongodb-expression-exp.-millisecond + */ + $millisecond: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface Minute { + /** + * Returns the minute for a date as a number between 0 and 59. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/minute/#mongodb-expression-exp.-minute + */ + $minute: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface Month { + /** + * Returns the month for a date as a number between 1 (January) and 12 (December). + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/month/#mongodb-expression-exp.-month + */ + $month: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface Second { + /** + * Returns the seconds for a date as a number between 0 and 60 (leap seconds). + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/second/#mongodb-expression-exp.-second + */ + $second: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface ToDate { + /** + * Converts value to a Date. + * + * @version 4.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/toDate/#mongodb-expression-exp.-toDate + */ + $toDate: Expression; + } + + export interface Week { + /** + * Returns the week number for a date as a number between 0 (the partial week that precedes the first Sunday of the year) and 53 (leap year). + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/week/#mongodb-expression-exp.-week + */ + $week: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface Year { + /** + * Returns the year for a date as a number (e.g. 2014). + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/year/#mongodb-expression-exp.-year + */ + $year: DateExpression | { + /** + * The date to which the operator is applied. must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID. + */ + date: DateExpression; + /** + * The timezone of the operation result. must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC. + */ + timezone?: tzExpression; + }; + } + + export interface And { + /** + * Returns true only when all its expressions evaluate to true. Accepts any number of argument expressions. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/and/#mongodb-expression-exp.-and + */ + $and: (Expression | Record)[]; + } + + export interface Not { + /** + * Returns the boolean value that is the opposite of its argument expression. Accepts a single argument expression. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/not/#mongodb-expression-exp.-not + */ + $not: [Expression]; + } + + export interface Or { + /** + * Returns true when any of its expressions evaluates to true. Accepts any number of argument expressions. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/or/#mongodb-expression-exp.-or + */ + $or: (Expression | Record)[]; + } + + export interface Cmp { + /** + * Returns 0 if the two values are equivalent, 1 if the first value is greater than the second, and -1 if the first value is less than the second. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/cmp/#mongodb-expression-exp.-cmp + */ + $cmp: [Record | Expression, Record | Expression]; + } + + export interface Eq { + /** + * Returns true if the values are equivalent. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/eq/#mongodb-expression-exp.-eq + */ + $eq: AnyExpression | [AnyExpression, AnyExpression]; + } + + export interface Gt { + /** + * Returns true if the first value is greater than the second. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/gt/#mongodb-expression-exp.-gt + */ + $gt: NumberExpression | [NumberExpression, NumberExpression]; + } + + export interface Gte { + /** + * Returns true if the first value is greater than or equal to the second. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/gte/#mongodb-expression-exp.-gte + */ + $gte: NumberExpression | [NumberExpression, NumberExpression]; + } + + export interface Lt { + /** + * Returns true if the first value is less than the second. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/lt/#mongodb-expression-exp.-lt + */ + $lt: NumberExpression | [NumberExpression, NumberExpression]; + } + + export interface Lte { + /** + * Returns true if the first value is less than or equal to the second. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/lte/#mongodb-expression-exp.-lte + */ + $lte: NumberExpression | [NumberExpression, NumberExpression]; + } + + export interface Ne { + /** + * Returns true if the values are not equivalent. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/ne/#mongodb-expression-exp.-ne + */ + $ne: Expression | [Expression, Expression | NullExpression] | null; + } + + export interface Cond { + /** + * A ternary operator that evaluates one expression, and depending on the result, returns the value of one of the other two expressions. Accepts either three expressions in an ordered list or three named parameters. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/cond/#mongodb-expression-exp.-cond + */ + $cond: { if: Expression, then: AnyExpression, else: AnyExpression } | [BooleanExpression, AnyExpression, AnyExpression]; + } + + export interface IfNull { + /** + * Returns either the non-null result of the first expression or the result of the second expression if the first expression results in a null result. Null result encompasses instances of undefined values or missing fields. Accepts two expressions as arguments. The result of the second expression can be null. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/ifNull/#mongodb-expression-exp.-ifNull + */ + $ifNull: Expression[]; + } + + export interface Switch { + /** + * Evaluates a series of case expressions. When it finds an expression which evaluates to true, $switch executes a specified expression and breaks out of the control flow. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/switch/#mongodb-expression-exp.-switch + */ + $switch: { + /** + * An array of control branch documents. Each branch is a document with the following fields: + * - $case + * - $then + */ + branches: { case: Expression, then: Expression }[]; + /** + * The path to take if no branch case expression evaluates to true. + * + * Although optional, if default is unspecified and no branch case evaluates to true, $switch returns an error. + */ + default: Expression; + }; + } + + export interface ArrayElemAt { + /** + * Returns the element at the specified array index. + * + * @version 3.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayElemAt/#mongodb-expression-exp.-arrayElemAt + */ + $arrayElemAt: [ArrayExpression, NumberExpression]; + } + + export interface ArrayToObject { + /** + * Converts an array of key value pairs to a document. + * + * @version 3.4.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayToObject/#mongodb-expression-exp.-arrayToObject + */ + $arrayToObject: ArrayExpression; + } + + export interface ConcatArrays { + /** + * Concatenates arrays to return the concatenated array. + * + * @version 3.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/concatArrays/#mongodb-expression-exp.-concatArrays + */ + $concatArrays: Expression[]; + } + + export interface Filter { + /** + * Selects a subset of the array to return an array with only the elements that match the filter condition. + * + * @version 3.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#mongodb-expression-exp.-filter + */ + $filter: { + /** + * An expression that resolves to an array. + */ + input: ArrayExpression; + /** + * A name for the variable that represents each individual element of the input array. If no name is specified, the variable name defaults to this. + */ + as?: string; + /** + * An expression that resolves to a boolean value used to determine if an element should be included in the output array. The expression references each element of the input array individually with the variable name specified in as. + */ + cond: BooleanExpression; + /** + * A number expression that restricts the number of matching array elements that $filter returns. You cannot specify a limit less than 1. The matching array elements are returned in the order they appear in the input array. + * + * If the specified limit is greater than the number of matching array elements, $filter returns all matching array elements. + * If the limit is null, $filter returns all matching array elements. + * + * @version 5.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#using-the-limit-field + */ + limit?: NumberExpression; + } + } + + export interface First { + /** + * Returns the first array element. Distinct from $first accumulator. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/first/#mongodb-expression-exp.-first + */ + $first: Expression; + } + + export interface FirstN { + /** + * $firstN can be used as an aggregation accumulator or array operator. + * As an aggregation accumulator, it returns an aggregation of the first n elements within a group. + * As an array operator, it returns the specified number of elements from the beginning of an array. + * + * @version 5.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN/#mongodb-expression-exp.-first + */ + $firstN: { + input: Expression + n: Expression, + }; + } + + export interface In { + /** + * Returns a boolean indicating whether a specified value is in an array. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/in/#mongodb-expression-exp.-in + */ + $in: [Expression, ArrayExpression]; + } + + export interface IndexOfArray { + /** + * Searches an array for an occurrence of a specified value and returns the array index of the first occurrence. If the substring is not found, returns -1. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexOfArray/#mongodb-expression-exp.-indexOfArray + */ + $indexOfArray: [ArrayExpression, Expression] | [ArrayExpression, Expression, NumberExpression] | [ArrayExpression, Expression, NumberExpression, NumberExpression]; + } + + export interface IsArray { + /** + * Determines if the operand is an array. Returns a boolean. + * + * @version 3.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/isArray/#mongodb-expression-exp.-isArray + */ + $isArray: [Expression]; + } + + export interface Last { + /** + * Returns the last array element. Distinct from $last accumulator. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/last/#mongodb-expression-exp.-last + */ + $last: Expression; + } + + export interface LastN { + /** + * $lastN can be used as an aggregation accumulator or array operator. + * As an aggregation accumulator, it an aggregation of the last n elements within a group. + * As an array operator, it returns the specified number of elements from the end of an array. + * + * @version 5.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/lastN/#mongodb-group-grp.-lastN + */ + $lastN: { + input: Expression + n: Expression, + }; + } + + export interface LinearFill { + /** + * Fills null and missing fields in a window using linear interpolation based on surrounding field values. + * + * @version 5.3 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/linearFill + */ + $linearFill: Expression + } + + export interface Locf { + /** + * Last observation carried forward. Sets values for null and missing fields in a window to the last non-null value for the field. + * + * @version 5.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/locf + */ + $locf: Expression + } + + export interface Map { + /** + * Applies a subexpression to each element of an array and returns the array of resulting values in order. Accepts named parameters. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/map/#mongodb-expression-exp.-map + */ + $map: { + /** + * An expression that resolves to an array. + */ + input: ArrayExpression; + /** + * A name for the variable that represents each individual element of the input array. If no name is specified, the variable name defaults to this. + */ + as?: string; + /** + * An expression that is applied to each element of the input array. The expression references each element individually with the variable name specified in as. + */ + in: Expression; + }; + } + + export interface ObjectToArray { + /** + * Converts a document to an array of documents representing key-value pairs. + * + * @version 3.4.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/objectToArray/#mongodb-expression-exp.-objectToArray + */ + $objectToArray: ObjectExpression; + } + + export interface Range { + /** + * Outputs an array containing a sequence of integers according to user-defined inputs. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/range/#mongodb-expression-exp.-range + */ + $range: [NumberExpression, NumberExpression] | [NumberExpression, NumberExpression, NumberExpression]; + } + + export interface Reduce { + /** + * Applies an expression to each element in an array and combines them into a single value. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/reduce/#mongodb-expression-exp.-reduce + */ + $reduce: { + /** + * Can be any valid expression that resolves to an array. For more information on expressions, see Expressions. + * + * If the argument resolves to a value of null or refers to a missing field, $reduce returns null. + * + * If the argument does not resolve to an array or null nor refers to a missing field, $reduce returns an error. + */ + input: ArrayExpression; + /** + * The initial cumulative value set before in is applied to the first element of the input array. + */ + initialValue: Expression; + /** + * A valid expression that $reduce applies to each element in the input array in left-to-right order. Wrap the input value with $reverseArray to yield the equivalent of applying the combining expression from right-to-left. + * + * During evaluation of the in expression, two variables will be available: + * - `value` is the variable that represents the cumulative value of the expression. + * - `this` is the variable that refers to the element being processed. + */ + in: Expression; + }; + } + + export interface ReverseArray { + /** + * Returns an array with the elements in reverse order. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/reverseArray/#mongodb-expression-exp.-reverseArray + */ + $reverseArray: ArrayExpression; + } + + export interface Size { + /** + * Returns the number of elements in the array. Accepts a single expression as argument. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/size/#mongodb-expression-exp.-size + */ + $size: ArrayExpression; + } + + export interface Slice { + /** + * Returns a subset of an array. + * + * @version 3.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice/#mongodb-expression-exp.-slice + */ + $slice: [ArrayExpression, NumberExpression] | [ArrayExpression, NumberExpression, NumberExpression]; + } + + export interface Zip { + /** + * Merge two arrays together. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/zip/#mongodb-expression-exp.-zip + */ + $zip: { + /** + * An array of expressions that resolve to arrays. The elements of these input arrays combine to form the arrays of the output array. + * + * If any of the inputs arrays resolves to a value of null or refers to a missing field, $zip returns null. + * + * If any of the inputs arrays does not resolve to an array or null nor refers to a missing field, $zip returns an error. + */ + inputs: ArrayExpression[]; + /** + * A boolean which specifies whether the length of the longest array determines the number of arrays in the output array. + * + * The default value is false: the shortest array length determines the number of arrays in the output array. + */ + useLongestLength?: boolean; + /** + * An array of default element values to use if the input arrays have different lengths. You must specify useLongestLength: true along with this field, or else $zip will return an error. + * + * If useLongestLength: true but defaults is empty or not specified, $zip uses null as the default value. + * + * If specifying a non-empty defaults, you must specify a default for each input array or else $zip will return an error. + */ + defaults?: ArrayExpression; + }; + } + + export interface Concat { + /** + * Concatenates any number of strings. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/concat/#mongodb-expression-exp.-concat + */ + $concat: StringExpression[]; + } + + export interface IndexOfBytes { + /** + * Searches a string for an occurrence of a substring and returns the UTF-8 byte index of the first occurrence. If the substring is not found, returns -1. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexOfBytes/#mongodb-expression-exp.-indexOfBytes + */ + $indexOfBytes: [StringExpression, StringExpression] | [StringExpression, StringExpression, NumberExpression] | [StringExpression, StringExpression, NumberExpression, NumberExpression]; + } + + export interface IndexOfCP { + /** + * Searches a string for an occurrence of a substring and returns the UTF-8 code point index of the first occurrence. If the substring is not found, returns -1 + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexOfCP/#mongodb-expression-exp.-indexOfCP + */ + $indexOfCP: [StringExpression, StringExpression] | [StringExpression, StringExpression, NumberExpression] | [StringExpression, StringExpression, NumberExpression, NumberExpression]; + } + + export interface Ltrim { + /** + * Removes whitespace or the specified characters from the beginning of a string. + * + * @version 4.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/ltrim/#mongodb-expression-exp.-ltrim + */ + $ltrim: { + /** + * The string to trim. The argument can be any valid expression that resolves to a string. For more information on expressions, see Expressions. + */ + input: StringExpression; + /** + * The character(s) to trim from the beginning of the input. + * + * The argument can be any valid expression that resolves to a string. The $ltrim operator breaks down the string into individual UTF code point to trim from input. + * + * If unspecified, $ltrim removes whitespace characters, including the null character. For the list of whitespace characters, see Whitespace Characters. + */ + chars?: StringExpression; + }; + } + + export interface RegexFind { + /** + * Applies a regular expression (regex) to a string and returns information on the first matched substring. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexFind/#mongodb-expression-exp.-regexFind + */ + $regexFind: { + /** + * The string on which you wish to apply the regex pattern. Can be a string or any valid expression that resolves to a string. + */ + input: Expression; // TODO: Resolving to string, which ones? + /** + * The regex pattern to apply. Can be any valid expression that resolves to either a string or regex pattern //. When using the regex //, you can also specify the regex options i and m (but not the s or x options): + * - "pattern" + * - /pattern/ + * - /pattern/options + * + * Alternatively, you can also specify the regex options with the options field. To specify the s or x options, you must use the options field. + * + * You cannot specify options in both the regex and the options field. + */ + regex: RegExp | string; + /** + * The following are available for use with regular expression. + * + * Note: You cannot specify options in both the regex and the options field. + * + * Option Description + * + * `i` Case insensitivity to match both upper and lower cases. You can specify the option in the options field or as part of the regex field. + * + * `m` For patterns that include anchors (i.e. ^ for the start, $ for the end), match at the beginning or end of each line for strings with multiline values. Without this option, these anchors match at beginning or end of the string. + * If the pattern contains no anchors or if the string value has no newline characters (e.g. \n), the m option has no effect. + * + * `x` "Extended" capability to ignore all white space characters in the pattern unless escaped or included in a character class. + * Additionally, it ignores characters in-between and including an un-escaped hash/pound (#) character and the next new line, so that you may include comments in complicated patterns. This only applies to data characters; white space characters may never appear within special character sequences in a pattern. + * The x option does not affect the handling of the VT character (i.e. code 11). + * You can specify the option only in the options field. + * + * `s` Allows the dot character (i.e. .) to match all characters including newline characters. + * You can specify the option only in the options field. + */ + options?: RegexOptions; + }; + } + + export interface RegexFindAll { + /** + * Applies a regular expression (regex) to a string and returns information on the all matched substrings. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexFindAll/#mongodb-expression-exp.-regexFindAll + */ + $regexFindAll: { + /** + * The string on which you wish to apply the regex pattern. Can be a string or any valid expression that resolves to a string. + */ + input: Expression; // TODO: Resolving to string, which ones? + /** + * The regex pattern to apply. Can be any valid expression that resolves to either a string or regex pattern //. When using the regex //, you can also specify the regex options i and m (but not the s or x options): + * - "pattern" + * - /pattern/ + * - /pattern/options + * + * Alternatively, you can also specify the regex options with the options field. To specify the s or x options, you must use the options field. + * + * You cannot specify options in both the regex and the options field. + */ + regex: RegExp | string; + /** + * The following are available for use with regular expression. + * + * Note: You cannot specify options in both the regex and the options field. + * + * Option Description + * + * `i` Case insensitivity to match both upper and lower cases. You can specify the option in the options field or as part of the regex field. + * + * `m` For patterns that include anchors (i.e. ^ for the start, $ for the end), match at the beginning or end of each line for strings with multiline values. Without this option, these anchors match at beginning or end of the string. + * If the pattern contains no anchors or if the string value has no newline characters (e.g. \n), the m option has no effect. + * + * `x` "Extended" capability to ignore all white space characters in the pattern unless escaped or included in a character class. + * Additionally, it ignores characters in-between and including an un-escaped hash/pound (#) character and the next new line, so that you may include comments in complicated patterns. This only applies to data characters; white space characters may never appear within special character sequences in a pattern. + * The x option does not affect the handling of the VT character (i.e. code 11). + * You can specify the option only in the options field. + * + * `s` Allows the dot character (i.e. .) to match all characters including newline characters. + * You can specify the option only in the options field. + */ + options?: RegexOptions; + }; + } + + export interface RegexMatch { + /** + * Applies a regular expression (regex) to a string and returns a boolean that indicates if a match is found or not. + * + * @version 4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexMatch/#mongodb-expression-exp.-regexMatch + */ + $regexMatch: { + /** + * The string on which you wish to apply the regex pattern. Can be a string or any valid expression that resolves to a string. + */ + input: Expression; // TODO: Resolving to string, which ones? + /** + * The regex pattern to apply. Can be any valid expression that resolves to either a string or regex pattern //. When using the regex //, you can also specify the regex options i and m (but not the s or x options): + * - "pattern" + * - /pattern/ + * - /pattern/options + * + * Alternatively, you can also specify the regex options with the options field. To specify the s or x options, you must use the options field. + * + * You cannot specify options in both the regex and the options field. + */ + regex: RegExp | string; + /** + * The following are available for use with regular expression. + * + * Note: You cannot specify options in both the regex and the options field. + * + * Option Description + * + * `i` Case insensitivity to match both upper and lower cases. You can specify the option in the options field or as part of the regex field. + * + * `m` For patterns that include anchors (i.e. ^ for the start, $ for the end), match at the beginning or end of each line for strings with multiline values. Without this option, these anchors match at beginning or end of the string. + * If the pattern contains no anchors or if the string value has no newline characters (e.g. \n), the m option has no effect. + * + * `x` "Extended" capability to ignore all white space characters in the pattern unless escaped or included in a character class. + * Additionally, it ignores characters in-between and including an un-escaped hash/pound (#) character and the next new line, so that you may include comments in complicated patterns. This only applies to data characters; white space characters may never appear within special character sequences in a pattern. + * The x option does not affect the handling of the VT character (i.e. code 11). + * You can specify the option only in the options field. + * + * `s` Allows the dot character (i.e. .) to match all characters including newline characters. + * You can specify the option only in the options field. + */ + options?: RegexOptions; + }; + } + + export interface ReplaceOne { + /** + * Replaces the first instance of a matched string in a given input. + * + * @version 4.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/replaceOne/#mongodb-expression-exp.-replaceOne + */ + $replaceOne: { + /** + * The string on which you wish to apply the find. Can be any valid expression that resolves to a string or a null. If input refers to a field that is missing, $replaceOne returns null. + */ + input: StringExpression; + /** + * The string to search for within the given input. Can be any valid expression that resolves to a string or a null. If find refers to a field that is missing, $replaceOne returns null. + */ + find: StringExpression; + /** + * The string to use to replace the first matched instance of find in input. Can be any valid expression that resolves to a string or a null. + */ + replacement: StringExpression; + }; + } + + export interface ReplaceAll { + /** + * Replaces all instances of a matched string in a given input. + * + * @version 4.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/replaceAll/#mongodb-expression-exp.-replaceAll + */ + $replaceAll: { + /** + * The string on which you wish to apply the find. Can be any valid expression that resolves to a string or a null. If input refers to a field that is missing, $replaceAll returns null. + */ + input: StringExpression; + /** + * The string to search for within the given input. Can be any valid expression that resolves to a string or a null. If find refers to a field that is missing, $replaceAll returns null. + */ + find: StringExpression; + /** + * The string to use to replace all matched instances of find in input. Can be any valid expression that resolves to a string or a null. + */ + replacement: StringExpression; + }; + } + + export interface Rtrim { + /** + * Removes whitespace or the specified characters from the end of a string. + * + * @version 4.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/rtrim/#mongodb-expression-exp.-rtrim + */ + $rtrim: { + /** + * The string to trim. The argument can be any valid expression that resolves to a string. For more information on expressions, see Expressions. + */ + input: StringExpression; + /** + * The character(s) to trim from the beginning of the input. + * + * The argument can be any valid expression that resolves to a string. The $rtrim operator breaks down the string into individual UTF code point to trim from input. + * + * If unspecified, $rtrim removes whitespace characters, including the null character. For the list of whitespace characters, see Whitespace Characters. + */ + chars?: StringExpression; + }; + } + + export interface Split { + /** + * Splits a string into substrings based on a delimiter. Returns an array of substrings. If the delimiter is not found within the string, returns an array containing the original string. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/split/#mongodb-expression-exp.-split + */ + $split: [StringExpression, StringExpression]; + } + + export interface StrLenBytes { + /** + * Returns the number of UTF-8 encoded bytes in a string. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/strLenBytes/#mongodb-expression-exp.-strLenBytes + */ + $strLenBytes: StringExpression; + } + + export interface StrLenCP { + /** + * Returns the number of UTF-8 code points in a string. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/strLenCP/#mongodb-expression-exp.-strLenCP + */ + $strLenCP: StringExpression; + } + + export interface Strcasecmp { + /** + * Performs case-insensitive string comparison and returns: 0 if two strings are equivalent, 1 if the first string is greater than the second, and -1 if the first string is less than the second. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/strcasecmp/#mongodb-expression-exp.-strcasecmp + */ + $strcasecmp: [StringExpression, StringExpression]; + } + + export interface Substr { + /** + * Deprecated. Use $substrBytes or $substrCP. + * + * @deprecated 3.4 + * @alias {Expression.SubstrBytes} + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/substr/#mongodb-expression-exp.-substr + */ + $substr: [StringExpression, number, number]; + } + + export interface SubstrBytes { + /** + * Returns the substring of a string. Starts with the character at the specified UTF-8 byte index (zero-based) in the string and continues for the specified number of bytes. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/substrBytes/#mongodb-expression-exp.-substrBytes + */ + $substrBytes: [StringExpression, number, number]; + } + + export interface SubstrCP { + /** + * Returns the substring of a string. Starts with the character at the specified UTF-8 code point (CP) index (zero-based) in the string and continues for the number of code points specified. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/substrCP/#mongodb-expression-exp.-substrCP + */ + $substrCP: [StringExpression, number, number]; + } + + export interface ToLower { + /** + * Converts a string to lowercase. Accepts a single argument expression. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/toLower/#mongodb-expression-exp.-toLower + */ + $toLower: StringExpression; + } + + export interface ToString { + /** + * Converts value to a string. + * + * @version 4.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/toString/#mongodb-expression-exp.-toString + */ + $toString: Expression; + } + + export interface Trim { + /** + * Removes whitespace or the specified characters from the beginning and end of a string. + * + * @version 4.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/trim/#mongodb-expression-exp.-trim + */ + $trim: { + /** + * The string to trim. The argument can be any valid expression that resolves to a string. For more information on expressions, see Expressions. + */ + input: StringExpression; + /** + * The character(s) to trim from the beginning of the input. + * + * The argument can be any valid expression that resolves to a string. The $trim operator breaks down the string into individual UTF code point to trim from input. + * + * If unspecified, $trim removes whitespace characters, including the null character. For the list of whitespace characters, see Whitespace Characters. + */ + chars?: StringExpression; + }; + } + + export interface ToUpper { + /** + * Converts a string to uppercase. Accepts a single argument expression. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/toUpper/#mongodb-expression-exp.-toUpper + */ + $toUpper: StringExpression; + } + + export interface Literal { + + /** + * Returns a value without parsing. Use for values that the aggregation pipeline may interpret as an + * expression. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/literal/#mongodb-expression-exp.-literal + */ + $literal: any; + } + + export interface GetField { + + /** + * Returns the value of a specified field from a document. If you don't specify an object, $getField returns + * the value of the field from $$CURRENT. + * + * @version 4.4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/getField/#mongodb-expression-exp.-getField + */ + $getField: { + /** + * Field in the input object for which you want to return a value. field can be any valid expression that + * resolves to a string constant. + */ + field: StringExpression; + /** + * A valid expression that contains the field for which you want to return a value. input must resolve to an + * object, missing, null, or undefined. If omitted, defaults to the document currently being processed in the + * pipeline ($$CURRENT). + */ + input?: ObjectExpression | SpecialPathVariables | NullExpression; + } + } + + export interface Rand { + + /** + * Returns a random float between 0 and 1 each time it is called. + * + * @version 4.4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/rand/#mongodb-expression-exp.-rand + */ + $rand: Record; + } + + export interface SampleRate { + + /** + * Matches a random selection of input documents. The number of documents selected approximates the sample + * rate expressed as a percentage of the total number of documents. + * + * @version 4.4.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/sampleRate/#mongodb-expression-exp.-sampleRate + */ + $sampleRate: number; + } + + export interface MergeObjects { + + /** + * Combines multiple documents into a single document. + * + * @version 3.6 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/mergeObjects/#mongodb-expression-exp.-mergeObjects + */ + $mergeObjects: ObjectExpression | ObjectExpression[] | ArrayExpression | Record; + } + + export interface SetField { + + /** + * Adds, updates, or removes a specified field in a document. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/setField/#mongodb-expression-exp.-setField + */ + $setField: { + /** + * Field in the input object that you want to add, update, or remove. field can be any valid expression that + * resolves to a string constant. + */ + field: StringExpression; + /** + * Document that contains the field that you want to add or update. input must resolve to an object, missing, + * null, or undefined + */ + input?: ObjectExpression | NullExpression; + /** + * The value that you want to assign to field. value can be any valid expression. + */ + value?: Expression | SpecialPathVariables; + } + } + + export interface UnsetField { + + /** + * Removes a specified field in a document. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/unsetField/#mongodb-expression-exp.-unsetField + */ + $unsetField: { + /** + * Field in the input object that you want to add, update, or remove. field can be any valid expression that + * resolves to a string constant. + */ + field: StringExpression; + /** + * Document that contains the field that you want to add or update. input must resolve to an object, missing, + * null, or undefined. + */ + input?: ObjectExpression | SpecialPathVariables | NullExpression; + } + } + + export interface Let { + + /** + * Binds variables for use in the specified expression, and returns the result of the expression. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/let/#mongodb-expression-exp.-let + */ + $let: { + /** + * Assignment block for the variables accessible in the in expression. To assign a variable, specify a + * string for the variable name and assign a valid expression for the value. + */ + vars: { [key: string]: Expression; }; + /** + * The expression to evaluate. + */ + in: Expression; + } + } + + export interface AllElementsTrue { + /** + * Evaluates an array as a set and returns true if no element in the array is false. Otherwise, returns false. An + * empty array returns true. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/allElementsTrue/#mongodb-expression-exp.-allElementsTrue + */ + $allElementsTrue: ArrayExpression; + } + + export interface AnyElementsTrue { + /** + * Evaluates an array as a set and returns true if any of the elements are true and false otherwise. An empty + * array returns false. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/anyElementsTrue/#mongodb-expression-exp.-anyElementsTrue + */ + $anyElementTrue: ArrayExpression; + } + + export interface SetDifference { + /** + * Takes two sets and returns an array containing the elements that only exist in the first set; i.e. performs a + * relative complement of the second set relative to the first. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/setDifference/#mongodb-expression-exp.-setDifference + */ + $setDifference: [ArrayExpression, ArrayExpression]; + } + + export interface SetEquals { + /** + * Compares two or more arrays and returns true if they have the same distinct elements and false otherwise. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/setEquals/#mongodb-expression-exp.-setEquals + */ + $setEquals: ArrayExpression[]; + } + + export interface SetIntersection { + /** + * Takes two or more arrays and returns an array that contains the elements that appear in every input array. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/setIntersection/#mongodb-expression-exp.-setIntersection + */ + $setIntersection: ArrayExpression[]; + } + + export interface SetIsSubset { + /** + * Takes two arrays and returns true when the first array is a subset of the second, including when the first + * array equals the second array, and false otherwise. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/setIsSubset/#mongodb-expression-exp.-setIsSubset + */ + $setIsSubset: [ArrayExpression, ArrayExpression]; + } + + export interface SetUnion { + /** + * Takes two or more arrays and returns an array containing the elements that appear in any input array. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/setUnion/#mongodb-expression-exp.-setUnion + */ + $setUnion: ArrayExpression[]; + } + + export interface Accumulator { + /** + * Defines a custom accumulator operator. Accumulators are operators that maintain their state (e.g. totals, + * maximums, minimums, and related data) as documents progress through the pipeline. Use the $accumulator operator + * to execute your own JavaScript functions to implement behavior not supported by the MongoDB Query Language. See + * also $function. + * + * @version 4.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator/#mongodb-expression-exp.-accumulator + */ + $accumulator: { + /** + * Function used to initialize the state. The init function receives its arguments from the initArgs array + * expression. You can specify the function definition as either BSON type Code or String. + */ + init: CodeExpression; + /** + * Arguments passed to the init function. + */ + initArgs?: ArrayExpression; + /** + * Function used to accumulate documents. The accumulate function receives its arguments from the current state + * and accumulateArgs array expression. The result of the accumulate function becomes the new state. You can + * specify the function definition as either BSON type Code or String. + */ + accumulate: CodeExpression; + /** + * Arguments passed to the accumulate function. You can use accumulateArgs to specify what field value(s) to + * pass to the accumulate function. + */ + accumulateArgs: ArrayExpression; + /** + * Function used to merge two internal states. merge must be either a String or Code BSON type. merge returns + * the combined result of the two merged states. For information on when the merge function is called, see Merge + * Two States with $merge. + */ + merge: CodeExpression; + /** + * Function used to update the result of the accumulation. + */ + finalize?: CodeExpression; + /** + * The language used in the $accumulator code. + */ + lang: 'js'; + } + } + + export interface AddToSet { + /** + * Returns an array of all unique values that results from applying an expression to each document in a group. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/addToSet/#mongodb-expression-exp.-addToSet + */ + $addToSet: Expression | Record; + } + + export interface Avg { + /** + * Returns the average value of the numeric values. $avg ignores non-numeric values. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/avg/#mongodb-expression-exp.-avg + */ + $avg: Expression; + } + + export interface Bottom { + /** + * Returns the bottom element within a group according to the specified sort order. + * + * @version 5.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottom/#mongodb-group-grp.-bottom + */ + $bottom: { + sortBy: AnyObject, + output: Expression + }; + } + + export interface BottomN { + /** + * Returns an aggregation of the bottom n elements within a group, according to the specified sort order. + * If the group contains fewer than n elements, $bottomN returns all elements in the group. + * + * @version 5.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottomN/#mongodb-group-grp.-bottomN + */ + $bottomN: { + n: Expression, + sortBy: AnyObject, + output: Expression + }; + } + + export interface Count { + /** + * Returns the number of documents in a group. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/count/#mongodb-expression-exp.-count + */ + $count: Record | Path; + } + + export interface CovariancePop { + /** + * Returns the population covariance of two numeric expressions that are evaluated using documents in the + * $setWindowFields stage window. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/covariancePop/#mongodb-expression-exp.-covariancePop + */ + $covariancePop: [NumberExpression, NumberExpression]; + } + + export interface CovarianceSamp { + /** + * Returns the sample covariance of two numeric expressions that are evaluated using documents in the + * $setWindowFields stage window. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/covarianceSamp/#mongodb-expression-exp.-covarianceSamp + */ + $covarianceSamp: [NumberExpression, NumberExpression]; + } + + export interface DenseRank { + /** + * Returns the document position (known as the rank) relative to other documents in the $setWindowFields stage + * partition. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/denseRank/#mongodb-expression-exp.-denseRank + */ + $denseRank: Record; + } + + export interface Derivative { + /** + * Returns the average rate of change within the specified window, which is calculated using the: + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/derivative/#mongodb-expression-exp.-derivative + */ + $derivative: { + /** + * Specifies the expression to evaluate. The expression must evaluate to a number. + */ + input: NumberExpression; + /** + * A string that specifies the time unit. + */ + unit?: DateUnit; + } + } + + export interface DocumentNumber { + /** + * Returns the position of a document (known as the document number) in the $setWindowFields stage partition. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/documentNumber/#mongodb-expression-exp.-documentNumber + */ + $documentNumber: Record; + } + + export interface ExpMovingAvg { + /** + * Returns the exponential moving average of numeric expressions applied to documents in a partition defined in + * the $setWindowFields stage. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/expMovingAvg/#mongodb-expression-exp.-expMovingAvg + */ + $expMovingAvg: { + /** + * Specifies the expression to evaluate. Non-numeric expressions are ignored. + */ + input: Expression; + + /** + * An integer that specifies the number of historical documents that have a significant mathematical weight in + * the exponential moving average calculation, with the most recent documents contributing the most weight. + * + * You must specify either N or alpha. You cannot specify both. + */ + N: NumberExpression; + + /** + * A double that specifies the exponential decay value to use in the exponential moving average calculation. A + * higher alpha value assigns a lower mathematical significance to previous results from the calculation. + * + * You must specify either N or alpha. You cannot specify both. + */ + alpha?: never; + } | + { + /** + * Specifies the expression to evaluate. Non-numeric expressions are ignored. + */ + input: Expression; + + /** + * An integer that specifies the number of historical documents that have a significant mathematical weight in + * the exponential moving average calculation, with the most recent documents contributing the most weight. + * + * You must specify either N or alpha. You cannot specify both. + */ + N?: never; + + /** + * A double that specifies the exponential decay value to use in the exponential moving average calculation. A + * higher alpha value assigns a lower mathematical significance to previous results from the calculation. + * + * You must specify either N or alpha. You cannot specify both. + */ + alpha: NumberExpression; + } + } + + export interface Integral { + /** + * Returns the approximation of the area under a curve, which is calculated using the trapezoidal rule where each + * set of adjacent documents form a trapezoid using the: + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/integral/#mongodb-expression-exp.-integral + */ + $integral: { + /** + * Specifies the expression to evaluate. You must provide an expression that returns a number. + */ + input: NumberExpression; + + /** + * A string that specifies the time unit. + */ + unit?: DateUnit; + } + } + + export interface Max { + /** + * Returns the maximum value. $max compares both value and type, using the specified BSON comparison order for + * values of different types. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/max/#mongodb-expression-exp.-max + */ + $max: Expression | Expression[]; + } + + export interface MaxN { + /** + * Returns an aggregation of the maxmimum value n elements within a group. + * If the group contains fewer than n elements, $maxN returns all elements in the group. + * + * @version 5.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/maxN/#mongodb-group-grp.-maxN + */ + $maxN: { + input: Expression + n: Expression, + }; + } + + export interface Min { + /** + * Returns the minimum value. $min compares both value and type, using the specified BSON comparison order for + * values of different types. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/min/#mongodb-expression-exp.-min + */ + $min: Expression | Expression[]; + } + + export interface MinN { + /** + * Returns an aggregation of the minimum value n elements within a group. + * If the group contains fewer than n elements, $minN returns all elements in the group. + * + * @version 5.2 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/minN/#mongodb-group-grp.-minN + */ + $minN: { + input: Expression + n: Expression, + }; + } + + export interface Push { + /** + * Returns an array of all values that result from applying an expression to documents. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/push/#mongodb-expression-exp.-push + */ + $push: Expression | Record; + } + + export interface Rank { + /** + * Returns the document position (known as the rank) relative to other documents in the $setWindowFields stage + * partition. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/rank/#mongodb-expression-exp.-rank + */ + $rank: Record; + } + + export interface Shift { + /** + * Returns the value from an expression applied to a document in a specified position relative to the current + * document in the $setWindowFields stage partition. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/shift/#mongodb-expression-exp.-shift + */ + $shift: { + /** + * Specifies an expression to evaluate and return in the output. + */ + output: Expression; + /** + * Specifies an integer with a numeric document position relative to the current document in the output. + */ + by: number; + /** + * Specifies an optional default expression to evaluate if the document position is outside of the implicit + * $setWindowFields stage window. The implicit window contains all the documents in the partition. + */ + default?: Expression; + } + } + + export interface Median { + /** + * Returns an approximation of the median, the 50th percentile, as a scalar value. + * + * @see https://www.mongodb.com/docs/v7.0/reference/operator/aggregation/median/ + */ + $median: { + input: number | Expression, + method: 'approximate' + } + } + + export interface Percentile { + /** + * Returns an array of scalar values that correspond to specified percentile values. + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/percentile/ + */ + $percentile: { + input: number | Expression, + p: (number | Expression)[], + method: 'approximate' + } + } + + export interface StdDevPop { + /** + * Calculates the population standard deviation of the input values. Use if the values encompass the entire + * population of data you want to represent and do not wish to generalize about a larger population. $stdDevPop + * ignores non-numeric values. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/stdDevPop/#mongodb-expression-exp.-stdDevPop + */ + $stdDevPop: Expression; + } + + export interface StdDevSamp { + /** + * Calculates the sample standard deviation of the input values. Use if the values encompass a sample of a + * population of data from which to generalize about the population. $stdDevSamp ignores non-numeric values. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/stdDevSamp/#mongodb-expression-exp.-stdDevSamp + */ + $stdDevSamp: Expression; + } + + export interface Sum { + /** + * Calculates and returns the collective sum of numeric values. $sum ignores non-numeric values. + * + * @version 5.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/sum/#mongodb-expression-exp.-sum + */ + $sum: number | Expression | Expression[]; + } + + export interface Convert { + /** + * Checks if the specified expression resolves to one of the following numeric + * - Integer + * - Decimal + * - Double + * - Long + * + * @version 4.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/convert/#mongodb-expression-exp.-convert + */ + $convert: { + input: Expression; + to: K; + onError?: Expression; + onNull?: Expression; + }; + } + + export interface IsNumber { + /** + * Checks if the specified expression resolves to one of the following numeric + * - Integer + * - Decimal + * - Double + * - Long + * + * @version 4.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/isNumber/#mongodb-expression-exp.-isNumber + */ + $isNumber: Expression; + } + + export interface ToBool { + /** + * Converts a value to a boolean. + * + * @version 4.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/toBool/#mongodb-expression-exp.-toBool + */ + $toBool: Expression; + } + + export interface ToDecimal { + /** + * Converts a value to a decimal. If the value cannot be converted to a decimal, $toDecimal errors. If the value + * is null or missing, $toDecimal returns null. + * + * @version 4.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/toDecimal/#mongodb-expression-exp.-toDecimal + */ + $toDecimal: Expression; + } + + export interface ToDouble { + /** + * Converts a value to a double. If the value cannot be converted to an double, $toDouble errors. If the value is + * null or missing, $toDouble returns null. + * + * @version 4.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/toDouble/#mongodb-expression-exp.-toDouble + */ + $toDouble: Expression; + } + + export interface ToInt { + /** + * Converts a value to a long. If the value cannot be converted to a long, $toLong errors. If the value is null or + * missing, $toLong returns null. + * + * @version 4.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/toInt/#mongodb-expression-exp.-toInt + */ + $toInt: Expression; + } + + export interface ToLong { + /** + * Converts a value to a long. If the value cannot be converted to a long, $toLong errors. If the value is null or + * missing, $toLong returns null. + * + * @version 4.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/toLong/#mongodb-expression-exp.-toLong + */ + $toLong: Expression; + } + + export interface ToObjectId { + /** + * Converts a value to an ObjectId(). If the value cannot be converted to an ObjectId, $toObjectId errors. If the + * value is null or missing, $toObjectId returns null. + * + * @version 4.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/toObjectId/#mongodb-expression-exp.-toObjectId + */ + $toObjectId: Expression; + } + + export interface Top { + $top: { + sortBy: AnyObject, + output: Expression + }; + } + + export interface TopN { + $topN: { + n: Expression, + sortBy: AnyObject, + output: Expression + }; + } + + export interface ToString { + /** + * Converts a value to a string. If the value cannot be converted to a string, $toString errors. If the value is + * null or missing, $toString returns null. + * + * @version 4.0 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/toString/#mongodb-expression-exp.-toString + */ + $toString: Expression; + } + + export interface Type { + /** + * Returns a string that specifies the BSON type of the argument. + * + * @version 3.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/type/#mongodb-expression-exp.-type + */ + $type: Expression; + } + + export interface BinarySize { + /** + * Returns the size of a given string or binary data value's content in bytes. + * + * @version 4.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/binarySize/#mongodb-expression-exp.-binarySize + */ + $binarySize: NullExpression | StringExpression | BinaryExpression; + } + + export interface BsonSize { + /** + * Returns the size in bytes of a given document (i.e. bsontype Object) when encoded as BSON. You can use + * $bsonSize as an alternative to the Object.bsonSize() method. + * + * @version 4.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/bsonSize/#mongodb-expression-exp.-bsonSize + */ + $bsonSize: NullExpression | ObjectExpression; + } + + export interface Function { + /** + * Defines a custom aggregation function or expression in JavaScript. + * + * @version 4.4 + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/function/#mongodb-expression-exp.-function + */ + $function: { + /** + * The function definition. You can specify the function definition as either BSON type Code or String. + */ + body: CodeExpression; + /** + * Arguments passed to the function body. If the body function does not take an argument, you can specify an + * empty array [ ] + */ + args: ArrayExpression; + /** + * The language used in the body. You must specify lang: "js". + */ + lang: 'js' + }; + } + } + + type Path = string; + + + export type Expression = + Path | + ArithmeticExpressionOperator | + ArrayExpressionOperator | + BooleanExpressionOperator | + ComparisonExpressionOperator | + ConditionalExpressionOperator | + CustomAggregationExpressionOperator | + DataSizeOperator | + DateExpressionOperator | + LiteralExpressionOperator | + MiscellaneousExpressionOperator | + ObjectExpressionOperator | + SetExpressionOperator | + StringExpressionOperator | + TextExpressionOperator | + TrigonometryExpressionOperator | + TypeExpressionOperator | + AccumulatorOperator | + VariableExpressionOperator | + WindowOperator | + Expression.Top | + Expression.TopN | + any; + + export type NullExpression = null; + + export type CodeExpression = + string | + Function; + + export type BinaryExpression = + Path; + + export type FunctionExpression = + Expression.Function; + + export type AnyExpression = + ArrayExpression | + BooleanExpression | + NumberExpression | + ObjectExpression | + StringExpression | + DateExpression | + BinaryExpression | + FunctionExpression | + ObjectIdExpression | + ConditionalExpressionOperator | + any; + + export type ObjectIdExpression = + TypeExpressionOperatorReturningObjectId; + + export type ArrayExpression = + T[] | + Path | + ArrayExpressionOperatorReturningAny | + ArrayExpressionOperatorReturningArray | + StringExpressionOperatorReturningArray | + ObjectExpressionOperatorReturningArray | + SetExpressionOperatorReturningArray | + LiteralExpressionOperatorReturningAny | + WindowOperatorReturningArray | + CustomAggregationExpressionOperatorReturningAny | + WindowOperatorReturningAny; + + export type BooleanExpression = + boolean | + Path | + BooleanExpressionOperator | + ArrayExpressionOperatorReturningAny | + ComparisonExpressionOperatorReturningBoolean | + StringExpressionOperatorReturningBoolean | + SetExpressionOperatorReturningBoolean | + LiteralExpressionOperatorReturningAny | + CustomAggregationExpressionOperatorReturningAny | + TypeExpressionOperatorReturningBoolean; + + export type NumberExpression = + number | + Path | + ArrayExpressionOperatorReturningAny | + ArrayExpressionOperatorReturningNumber | + ArithmeticExpressionOperator | + ComparisonExpressionOperatorReturningNumber | + TrigonometryExpressionOperator | + MiscellaneousExpressionOperatorReturningNumber | + StringExpressionOperatorReturningNumber | + LiteralExpressionOperatorReturningAny | + ObjectExpressionOperator | + SetExpressionOperator | + WindowOperatorReturningNumber | + WindowOperatorReturningAny | + DataSizeOperatorReturningNumber | + CustomAggregationExpressionOperatorReturningAny | + TypeExpressionOperatorReturningNumber | + DateExpression | + DateExpressionOperatorReturningNumber; + + export type ObjectExpression = + Path | + ArrayExpressionOperatorReturningAny | + DateExpressionOperatorReturningObject | + StringExpressionOperatorReturningObject | + ObjectExpressionOperatorReturningObject | + CustomAggregationExpressionOperatorReturningAny | + LiteralExpressionOperatorReturningAny; + + export type StringExpression = + Path | + ArrayExpressionOperatorReturningAny | + DateExpressionOperatorReturningString | + StringExpressionOperatorReturningString | + LiteralExpressionReturningAny | + CustomAggregationExpressionOperatorReturningAny | + TypeExpressionOperatorReturningString | + T; + + export type DateExpression = + Path | + NativeDate | + DateExpressionOperatorReturningDate | + TypeExpressionOperatorReturningDate | + LiteralExpressionReturningAny; + + export type ArithmeticExpressionOperator = + Expression.Abs | + Expression.Add | + Expression.Ceil | + Expression.Divide | + Expression.Exp | + Expression.Floor | + Expression.Ln | + Expression.Log | + Expression.Log10 | + Expression.Mod | + Expression.Multiply | + Expression.Pow | + Expression.Round | + Expression.Sqrt | + Expression.Subtract | + Expression.Trunc; + + export type ArrayExpressionOperator = + ArrayExpressionOperatorReturningAny | + ArrayExpressionOperatorReturningBoolean | + ArrayExpressionOperatorReturningNumber | + ArrayExpressionOperatorReturningObject; + + export type LiteralExpressionOperator = + Expression.Literal; + + export type LiteralExpressionReturningAny = + LiteralExpressionOperatorReturningAny; + + export type LiteralExpressionOperatorReturningAny = + Expression.Literal; + + export type MiscellaneousExpressionOperator = + Expression.Rand | + Expression.SampleRate; + + export type MiscellaneousExpressionOperatorReturningNumber = + Expression.Rand; + + export type ArrayExpressionOperatorReturningAny = + Expression.ArrayElemAt | + Expression.First | + Expression.Last | + Expression.Reduce; + + export type ArrayExpressionOperatorReturningArray = + Expression.ConcatArrays | + Expression.Filter | + Expression.FirstN | + Expression.LastN | + Expression.Map | + Expression.ObjectToArray | + Expression.Range | + Expression.ReverseArray | + Expression.Slice | + Expression.Zip; + + export type ArrayExpressionOperatorReturningNumber = + Expression.IndexOfArray | + Expression.Size; + + export type ArrayExpressionOperatorReturningObject = + Expression.ArrayToObject; + + export type ArrayExpressionOperatorReturningBoolean = + Expression.In | + Expression.IsArray; + + export type BooleanExpressionOperator = + Expression.And | + Expression.Or | + Expression.Not; + + export type ComparisonExpressionOperator = + ComparisonExpressionOperatorReturningBoolean | + ComparisonExpressionOperatorReturningNumber; + + export type ComparisonExpressionOperatorReturningBoolean = + Expression.Eq | + Expression.Gt | + Expression.Gte | + Expression.Lt | + Expression.Lte | + Expression.Ne; + + export type ComparisonExpressionOperatorReturningNumber = + Expression.Cmp; + + export type ConditionalExpressionOperator = + Expression.Cond | + Expression.IfNull | + Expression.Switch; + + export type StringExpressionOperator = + StringExpressionOperatorReturningArray | + StringExpressionOperatorReturningBoolean | + StringExpressionOperatorReturningNumber | + StringExpressionOperatorReturningObject | + StringExpressionOperatorReturningString; + + export type StringExpressionOperatorReturningArray = + Expression.RegexFindAll | + Expression.Split; + + export type StringExpressionOperatorReturningBoolean = + Expression.RegexMatch; + + export type StringExpressionOperatorReturningNumber = + Expression.IndexOfBytes | + Expression.IndexOfCP | + Expression.Strcasecmp | + Expression.StrLenBytes | + Expression.StrLenCP; + + export type StringExpressionOperatorReturningObject = + Expression.RegexFind; + + export type StringExpressionOperatorReturningString = + Expression.Concat | + Expression.Ltrim | + Expression.Ltrim | + Expression.ReplaceOne | + Expression.ReplaceAll | + Expression.Substr | + Expression.SubstrBytes | + Expression.SubstrCP | + Expression.ToLower | + Expression.ToString | + Expression.ToUpper | + Expression.Trim; + + export type ObjectExpressionOperator = + Expression.MergeObjects | + Expression.ObjectToArray | + Expression.SetField | + Expression.UnsetField; + + export type ObjectExpressionOperatorReturningArray = + Expression.ObjectToArray; + + export type ObjectExpressionOperatorReturningObject = + Expression.MergeObjects | + Expression.SetField | + Expression.UnsetField; + + export type VariableExpressionOperator = + Expression.Let; + + export type VariableExpressionOperatorReturningAny = + Expression.Let; + + export type SetExpressionOperator = + Expression.AllElementsTrue | + Expression.AnyElementsTrue | + Expression.SetDifference | + Expression.SetEquals | + Expression.SetIntersection | + Expression.SetIsSubset | + Expression.SetUnion; + + export type SetExpressionOperatorReturningBoolean = + Expression.AllElementsTrue | + Expression.AnyElementsTrue | + Expression.SetEquals | + Expression.SetIsSubset; + + export type SetExpressionOperatorReturningArray = + Expression.SetDifference | + Expression.SetIntersection | + Expression.SetUnion; + + /** + * Trigonometry expressions perform trigonometric operations on numbers. + * Values that represent angles are always input or output in radians. + * Use $degreesToRadians and $radiansToDegrees to convert between degree + * and radian measurements. + */ + export type TrigonometryExpressionOperator = + Expression.Sin | + Expression.Cos | + Expression.Tan | + Expression.Asin | + Expression.Acos | + Expression.Atan | + Expression.Atan2 | + Expression.Asinh | + Expression.Acosh | + Expression.Atanh | + Expression.Sinh | + Expression.Cosh | + Expression.Tanh | + Expression.DegreesToRadians | + Expression.RadiansToDegrees; + + export type TextExpressionOperator = + Expression.Meta; + + export type WindowOperator = + Expression.AddToSet | + Expression.Avg | + Expression.Count | + Expression.CovariancePop | + Expression.CovarianceSamp | + Expression.DenseRank | + Expression.Derivative | + Expression.DocumentNumber | + Expression.ExpMovingAvg | + Expression.First | + Expression.FirstN | + Expression.Integral | + Expression.Last | + Expression.LastN | + Expression.LinearFill | + Expression.Locf | + Expression.Max | + Expression.MaxN | + Expression.Median | + Expression.Min | + Expression.MinN | + Expression.Percentile | + Expression.Push | + Expression.Rank | + Expression.Shift | + Expression.StdDevPop | + Expression.StdDevSamp | + Expression.Sum; + + export type WindowOperatorReturningAny = + Expression.First | + Expression.Last | + Expression.Shift; + + export type WindowOperatorReturningArray = + Expression.AddToSet | + Expression.FirstN | + Expression.LastN | + Expression.MaxN | + Expression.MinN | + Expression.Push; + + export type WindowOperatorReturningNumber = + Expression.Avg | + Expression.Count | + Expression.CovariancePop | + Expression.CovarianceSamp | + Expression.DenseRank | + Expression.DocumentNumber | + Expression.ExpMovingAvg | + Expression.Integral | + Expression.Max | + Expression.Median | + Expression.Min | + Expression.Percentile | + Expression.StdDevPop | + Expression.StdDevSamp | + Expression.Sum; + + export type TypeExpressionOperator = + Expression.Convert | + Expression.IsNumber | + Expression.ToBool | + Expression.ToDate | + Expression.ToDecimal | + Expression.ToDouble | + Expression.ToInt | + Expression.ToLong | + Expression.ToObjectId | + Expression.ToString | + Expression.Type; + + export type TypeExpressionOperatorReturningNumber = + Expression.Convert<'double' | 1 | 'int' | 16 | 'long' | 18 | 'decimal' | 19> | + Expression.ToDecimal | + Expression.ToDouble | + Expression.ToInt | + Expression.ToLong; + + export type TypeExpressionOperatorReturningBoolean = + Expression.Convert<'bool' | 8> | + Expression.IsNumber | + Expression.ToBool; + + + export type TypeExpressionOperatorReturningString = + Expression.Convert<'string' | 2> | + Expression.ToString | + Expression.Type; + + export type TypeExpressionOperatorReturningObjectId = + Expression.Convert<'objectId' | 7> | + Expression.ToObjectId; + + export type TypeExpressionOperatorReturningDate = + Expression.Convert<'date' | 9> | + Expression.ToDate; + + export type DataSizeOperator = + Expression.BinarySize | + Expression.BsonSize; + + export type DataSizeOperatorReturningNumber = + Expression.BinarySize | + Expression.BsonSize; + + export type CustomAggregationExpressionOperator = + Expression.Accumulator | + Expression.Function; + + export type CustomAggregationExpressionOperatorReturningAny = + Expression.Function; + + export type AccumulatorOperator = + Expression.Accumulator | + Expression.AddToSet | + Expression.Avg | + Expression.Bottom | + Expression.BottomN | + Expression.Count | + Expression.First | + Expression.FirstN | + Expression.Last | + Expression.LastN | + Expression.Max | + Expression.MaxN | + Expression.Median | + Expression.MergeObjects | + Expression.Min | + Expression.MinN | + Expression.Percentile | + Expression.Push | + Expression.StdDevPop | + Expression.StdDevSamp | + Expression.Sum | + Expression.Top | + Expression.TopN; + + export type tzExpression = UTCOffset | StringExpressionOperatorReturningBoolean | string; + + type hh = '-00' | '-01' | '-02' | '-03' | '-04' | '-05' | '-06' | '-07' | '-08' | '-09' | '-10' | '-11' | '-12' | + '+00' | '+01' | '+02' | '+03' | '+04' | '+05' | '+06' | '+07' | '+08' | '+09' | '+10' | '+11' | '+12' | '+13' | '+14'; + type mm = '00' | '30' | '45'; + + type UTCOffset = `${hh}` | `${hh}${mm}` | `${hh}:${mm}`; + + type RegexOptions = + 'i' | 'm' | 's' | 'x' | + 'is' | 'im' | 'ix' | 'si' | 'sm' | 'sx' | 'mi' | 'ms' | 'mx' | 'xi' | 'xs' | 'xm' | + 'ism' | 'isx' | 'ims' | 'imx' | 'ixs' | 'ixm' | 'sim' | 'six' | 'smi' | 'smx' | 'sxi' | 'sxm' | 'mis' | 'mix' | 'msi' | 'msx' | 'mxi' | 'mxs' | 'xis' | 'xim' | 'xsi' | 'xsm' | 'xmi' | 'xms' | + 'ismx' | 'isxm' | 'imsx' | 'imxs' | 'ixsm' | 'ixms' | 'simx' | 'sixm' | 'smix' | 'smxi' | 'sxim' | 'sxmi' | 'misx' | 'mixs' | 'msix' | 'msxi' | 'mxis' | 'mxsi' | 'xism' | 'xims' | 'xsim' | 'xsmi' | 'xmis' | 'xmsi'; + + type StartOfWeek = + 'monday' | 'mon' | + 'tuesday' | 'tue' | + 'wednesday' | 'wed' | + 'thursday' | 'thu' | + 'friday' | 'fri' | + 'saturday' | 'sat' | + 'sunday' | 'sun'; + + type DateUnit = 'year' | 'quarter' | 'week' | 'month' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond'; + + type FormatString = string; + + export type DateExpressionOperator = + DateExpressionOperatorReturningDate | + DateExpressionOperatorReturningNumber | + DateExpressionOperatorReturningString | + DateExpressionOperatorReturningObject; + + export type DateExpressionOperatorReturningObject = + Expression.DateToParts; + + export type DateExpressionOperatorReturningNumber = + Expression.DateDiff | + Expression.DayOfMonth | + Expression.DayOfWeek | + Expression.DayOfYear | + Expression.IsoDayOfWeek | + Expression.IsoWeek | + Expression.IsoWeekYear | + Expression.Millisecond | + Expression.Second | + Expression.Minute | + Expression.Hour | + Expression.Month | + Expression.Year; + + export type DateExpressionOperatorReturningDate = + Expression.DateAdd | + Expression.DateFromParts | + Expression.DateFromString | + Expression.DateSubtract | + Expression.DateTrunc | + Expression.ToDate; + + export type DateExpressionOperatorReturningString = + Expression.DateToString; + +} diff --git a/gateway/node_modules/mongoose/types/helpers.d.ts b/gateway/node_modules/mongoose/types/helpers.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..91e2ea27577321fd4c9d0ce0ca549c26bca7e8d0 --- /dev/null +++ b/gateway/node_modules/mongoose/types/helpers.d.ts @@ -0,0 +1,32 @@ +declare module 'mongoose' { + import mongodb = require('mongodb'); + + /** + * Mongoose uses this function to get the current time when setting + * [timestamps](/docs/guide.html#timestamps). You may stub out this function + * using a tool like [Sinon](https://www.npmjs.com/package/sinon) for testing. + */ + function now(): NativeDate; + + /** + * Tells `sanitizeFilter()` to skip the given object when filtering out potential query selector injection attacks. + * Use this method when you have a known query selector that you want to use. + */ + function trusted(obj: T): T; + + /** + * Returns true if the given value is a Mongoose ObjectId (using `instanceof`) or if the + * given value is a 24 character hex string, which is the most commonly used string representation + * of an ObjectId. + */ + function isObjectIdOrHexString(v: mongodb.ObjectId): true; + function isObjectIdOrHexString(v: mongodb.ObjectId | string): boolean; + function isObjectIdOrHexString(v: any): false; + + /** + * Returns true if Mongoose can cast the given value to an ObjectId, or + * false otherwise. + */ + function isValidObjectId(v: mongodb.ObjectId | Types.ObjectId): true; + function isValidObjectId(v: any): boolean; +} diff --git a/gateway/node_modules/mongoose/types/index.d.ts b/gateway/node_modules/mongoose/types/index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..ee55438a13f44f64b08602ef8d1d5d00c78a2142 --- /dev/null +++ b/gateway/node_modules/mongoose/types/index.d.ts @@ -0,0 +1,1191 @@ +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// + +declare class NativeDate extends globalThis.Date { } + +declare module 'mongoose' { + import Kareem = require('kareem'); + import events = require('events'); + import mongodb = require('mongodb'); + import mongoose = require('mongoose'); + + export type Mongoose = typeof mongoose; + + /** + * Mongoose constructor. The exports object of the `mongoose` module is an instance of this + * class. Most apps will only use this one instance. + */ + export const Mongoose: new (options?: MongooseOptions | null) => Mongoose; + + export let Promise: any; + + /** + * Can be extended to explicitly type specific models. + */ + export interface Models { + [modelName: string]: Model + } + + /** An array containing all models associated with this Mongoose instance. */ + export const models: Models; + + /** + * Removes the model named `name` from the default connection, if it exists. + * You can use this function to clean up any models you created in your tests to + * prevent OverwriteModelErrors. + */ + export function deleteModel(name: string | RegExp): Mongoose; + + /** + * Sanitizes query filters against query selector injection attacks by wrapping + * any nested objects that have a property whose name starts with `$` in a `$eq`. + */ + export function sanitizeFilter(filter: QueryFilter): QueryFilter; + + /** Gets mongoose options */ + export function get(key: K): MongooseOptions[K]; + + export function omitUndefined>(val: T): T; + + export type HydratedDocFromModel> = ReturnType; + + /* ! ignore */ + export type CompileModelOptions = { + overwriteModels?: boolean, + connection?: Connection + }; + + export function model( + name: string, + schema?: TSchema, + collection?: string, + options?: CompileModelOptions + ): Model< + InferSchemaType, + ObtainSchemaGeneric, + ObtainSchemaGeneric, + ObtainSchemaGeneric, + // If first schema generic param is set, that means we have an explicit raw doc type, + // so user should also specify a hydrated doc type if the auto inferred one isn't correct. + IsItRecordAndNotAny> extends true + ? ObtainSchemaGeneric + : HydratedDocument< + InferSchemaType, + ObtainSchemaGeneric & ObtainSchemaGeneric, + ObtainSchemaGeneric, + ObtainSchemaGeneric, + InferSchemaType, + ObtainSchemaGeneric + >, + TSchema, + ObtainSchemaGeneric + > & ObtainSchemaGeneric; + + export function model(name: string, schema?: Schema | Schema, collection?: string, options?: CompileModelOptions): Model; + + export function model( + name: string, + schema?: Schema, + collection?: string, + options?: CompileModelOptions + ): U; + + /** Returns an array of model names created on this instance of Mongoose. */ + export function modelNames(): Array; + + /** + * Overwrites the current driver used by this Mongoose instance. A driver is a + * Mongoose-specific interface that defines functions like `find()`. + */ + export function setDriver(driver: any): Mongoose; + + /** The node-mongodb-native driver Mongoose uses. */ + export { mongodb as mongo }; + + /** Declares a global plugin executed on all Schemas. */ + export function plugin(fn: (schema: Schema, opts?: any) => void, opts?: any): Mongoose; + + /** Getter/setter around function for pluralizing collection names. */ + export function pluralize(fn?: ((str: string) => string) | null): ((str: string) => string) | null; + + /** Sets mongoose options */ + export function set(key: K, value: MongooseOptions[K]): Mongoose; + export function set(options: { [K in keyof MongooseOptions]: MongooseOptions[K] }): Mongoose; + + /** The Mongoose version */ + export const version: string; + + export type AnyKeys = { [P in keyof T]?: T[P] | any }; + export interface AnyObject { + [k: string]: any + } + + export type Require_id = T extends { _id?: infer U } + ? IfAny> + : T & { _id: Types.ObjectId }; + + export type Default_id = TSchemaOptions extends { _id: false } ? T : Require_id; + + export type Default__v = TSchemaOptions extends { versionKey: false } + ? T + : TSchemaOptions extends { versionKey: infer VK } + ? ( + // If VK is a *literal* string, add that property + T & { + [K in VK as K extends string + ? (string extends K ? never : K) // drop if wide string + : never + ]: number + } + ) + : T extends { __v?: infer U } + ? T + : T & { __v: number }; + + /** Helper type for getting the hydrated document type from the raw document type. The hydrated document type is what `new MyModel()` returns. */ + export type HydratedDocument< + HydratedDocPathsType, + TOverrides = {}, + TQueryHelpers = {}, + TVirtuals = {}, + RawDocType = HydratedDocPathsType, + TSchemaOptions = DefaultSchemaOptions + > = IfAny< + HydratedDocPathsType, + any, + TOverrides extends Record ? + Document & + Default__v, TSchemaOptions> & + IfEquals< + TVirtuals, + {}, + AddDefaultId, + TVirtuals + > : + IfAny< + TOverrides, + Document & Default__v, TSchemaOptions>, + Document & MergeType< + Default__v, TSchemaOptions>, + HydratedDocumentOverrides< + IfEquals< + TOverrides, + {}, + TOverrides, + TOverrides & AddDefaultId + > + > + > + > + >; + + export type HydratedSingleSubdocument< + DocType, + TOverrides = {} + > = IfAny< + DocType, + any, + TOverrides extends Record ? + Types.Subdocument, DocType> & Require_id : + IfAny< + TOverrides, + Types.Subdocument, DocType> & Require_id, + Types.Subdocument, DocType> & MergeType< + Require_id, + TOverrides + > + > + >; + export type HydratedArraySubdocument = IfAny< + DocType, + any, + TOverrides extends Record ? + Types.ArraySubdocument, DocType> & Require_id : + IfAny< + TOverrides, + Types.ArraySubdocument, DocType> & Require_id, + Types.ArraySubdocument, DocType> & MergeType< + Require_id, + TOverrides + > + > + >; + + export type HydratedDocumentFromSchema = HydratedDocument< + InferSchemaType, + ObtainSchemaGeneric & ObtainSchemaGeneric, + ObtainSchemaGeneric, + ObtainSchemaGeneric, + InferSchemaType, + ObtainSchemaGeneric + >; + + export interface TagSet { + [k: string]: string; + } + + export interface ToObjectOptions> { + /** if `options.virtuals = true`, you can set `options.aliases = false` to skip applying aliases. This option is a no-op if `options.virtuals = false`. */ + aliases?: boolean; + /** if true, replace any conventionally populated paths with the original id in the output. Has no affect on virtual populated paths. */ + depopulate?: boolean; + /** if true, convert Maps to POJOs. Useful if you want to `JSON.stringify()` the result of `toObject()`. */ + flattenMaps?: boolean; + /** if true, convert any ObjectIds in the result to 24 character hex strings. */ + flattenObjectIds?: boolean; + /** if true, convert any UUIDs in the result to 36 character hex strings. */ + flattenUUIDs?: boolean; + /** apply all getters (path and virtual getters) */ + getters?: boolean; + /** remove empty objects (defaults to true) */ + minimize?: boolean; + /** If true, the resulting object will only have fields that are defined in the document's schema. By default, `toJSON()` & `toObject()` returns all fields in the underlying document from MongoDB, including ones that are not listed in the schema. */ + schemaFieldsOnly?: boolean; + /** if set, mongoose will call this function to allow you to transform the returned object */ + transform?: boolean | (( + doc: THydratedDocumentType, + ret: Default__v>, + options: ToObjectOptions + ) => any); + /** If true, omits fields that are excluded in this document's projection. Unless you specified a projection, this will omit any field that has `select: false` in the schema. */ + useProjection?: boolean; + /** if false, exclude the version key (`__v` by default) from the output */ + versionKey?: boolean; + /** apply virtual getters (can override getters option) */ + virtuals?: boolean | string[]; + } + + export type DiscriminatorModel = T extends Model + ? + M extends Model + ? Model & T, MQueryHelpers | TQueryHelpers, MInstanceMethods | TInstanceMethods, MVirtuals | TVirtuals> + : M + : M; + + export type DiscriminatorSchema = + DisSchema extends Schema + ? Schema, DiscriminatorModel, DisSchemaInstanceMethods | TInstanceMethods, DisSchemaQueryhelpers | TQueryHelpers, DisSchemaVirtuals | TVirtuals, DisSchemaStatics & TStaticMethods> + : Schema; + + type QueryResultType = T extends Query ? ResultType : never; + + type PluginFunction< + DocType, + M, + TInstanceMethods, + TQueryHelpers, + TVirtuals, + TStaticMethods> = (schema: Schema, opts?: any) => void; + + export class Schema< + RawDocType = any, + TModelType = Model, + TInstanceMethods = {}, + TQueryHelpers = {}, + TVirtuals = {}, + TStaticMethods = {}, + TSchemaOptions = DefaultSchemaOptions, + DocType extends ApplySchemaOptions< + ObtainDocumentType>, + ResolveSchemaOptions + > = ApplySchemaOptions< + ObtainDocumentType>, + ResolveSchemaOptions + >, + THydratedDocumentType = HydratedDocument< + DocType, + AddDefaultId & TInstanceMethods, + TQueryHelpers, + AddDefaultId, + IsItRecordAndNotAny extends true ? RawDocType : DocType, + ResolveSchemaOptions + >, + TSchemaDefinition = IfAny, RawDocType, THydratedDocumentType>>, + LeanResultType = IsItRecordAndNotAny extends true ? RawDocType : Default__v>>> + > + extends events.EventEmitter { + /** + * Create a new schema + */ + constructor( + definition?: SchemaDefinition, RawDocType, THydratedDocumentType> | DocType, + options?: SchemaOptions< + FlatRecord, + TInstanceMethods, + TQueryHelpers, + TStaticMethods, + TVirtuals, + THydratedDocumentType, + IfEquals< + TModelType, + Model, + Model, + TModelType + > + > | ResolveSchemaOptions + ); + + /* Creates a new schema with the given definition and options. Equivalent to `new Schema(definition, options)`, but with better automatic type inference. */ + static create< + TSchemaDefinition extends SchemaDefinition, + TSchemaOptions extends DefaultSchemaOptions, + RawDocType extends ApplySchemaOptions< + InferRawDocType>, + ResolveSchemaOptions + >, + THydratedDocumentType extends AnyObject = HydratedDocument< + InferHydratedDocType>, + TSchemaOptions extends { methods: infer M } ? M : {}, + TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {}, + ResolveVirtuals, + RawDocType, + ResolveSchemaOptions + > + >(def: TSchemaDefinition): Schema< + RawDocType, + Model, + TSchemaOptions extends { methods: infer M } ? M : {}, + TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {}, + ResolveVirtuals, + TSchemaOptions extends { statics: any } ? TSchemaOptions['statics'] : {}, + TSchemaOptions, + ApplySchemaOptions< + ObtainDocumentType>, + ResolveSchemaOptions + >, + THydratedDocumentType, + TSchemaDefinition, + ApplySchemaOptions< + InferRawDocType, { bufferToBinary: true }>, + ResolveSchemaOptions + > + >; + + static create< + TSchemaDefinition extends SchemaDefinition, + TSchemaOptions extends SchemaOptions>, + RawDocType extends ApplySchemaOptions< + InferRawDocType>, + ResolveSchemaOptions + >, + TMethods = TSchemaOptions extends { methods: infer M } ? { [K in keyof M]: OmitThisParameter } : {}, + TStatics = TSchemaOptions extends { statics: infer S } ? { [K in keyof S]: OmitThisParameter } : {} + >(def: TSchemaDefinition, options: TSchemaOptions & { + statics?: SchemaOptionsStaticsPropertyType< + TStatics, + Model + > + }): Schema< + RawDocType, + Model, + TMethods, + TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {}, + ResolveVirtuals, + TStatics, + TSchemaOptions, + ApplySchemaOptions< + ObtainDocumentType>, + ResolveSchemaOptions + >, + HydratedDocument< + InferHydratedDocType>, + TMethods, + TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {}, + ResolveVirtuals, + RawDocType, + ResolveSchemaOptions + >, + TSchemaDefinition, + ApplySchemaOptions< + InferRawDocType, { bufferToBinary: true }>, + ResolveSchemaOptions + > + >; + + /** Adds key path / schema type pairs to this schema. */ + add(obj: SchemaDefinition, RawDocType> | Schema, prefix?: string): this; + + /** + * Add an alias for `path`. This means getting or setting the `alias` + * is equivalent to getting or setting the `path`. + */ + alias(path: string, alias: string | string[]): this; + + /** + * Array of child schemas (from document arrays and single nested subdocs) + * and their corresponding compiled models. Each element of the array is + * an object with 2 properties: `schema` and `model`. + */ + childSchemas: { schema: Schema, model: any }[]; + + /** Removes all indexes on this schema */ + clearIndexes(): this; + + /** Returns a copy of this schema */ + clone(): this; + + discriminator(name: string | number, schema: DisSchema, options?: DiscriminatorOptions): this; + + /** Returns a new schema that has the picked `paths` from this schema. */ + pick(paths: string[], options?: SchemaOptions): T; + + /** Object containing discriminators defined on this schema */ + discriminators?: { [name: string]: Schema }; + + /** Iterates the schemas paths similar to Array#forEach. */ + eachPath(fn: (path: string, type: SchemaType) => void): this; + + /** Defines an index (most likely compound) for this schema. */ + index(fields: IndexDefinition, options?: Omit & { unique?: boolean | [true, string] }): this; + + /** + * Define a search index for this schema. + * + * @remarks Search indexes are only supported when used against a 7.0+ Mongo Atlas cluster. + */ + searchIndex(description: SearchIndexDescription): this; + + /** + * Returns a list of indexes that this schema declares, via `schema.index()` + * or by `index: true` in a path's options. + */ + indexes(): Array<[IndexDefinition, IndexOptions]>; + + /** Gets a schema option. */ + get(key: K): SchemaOptions[K]; + + /** + * Loads an ES6 class into a schema. Maps [setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set) + [getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get), [static methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static), + * and [instance methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Class_body_and_method_definitions) + * to schema [virtuals](http://mongoosejs.com/docs/guide.html#virtuals), + * [statics](http://mongoosejs.com/docs/guide.html#statics), and + * [methods](http://mongoosejs.com/docs/guide.html#methods). + */ + loadClass(model: Function, onlyVirtuals?: boolean): this; + + /** Adds an instance method to documents constructed from Models compiled from this schema. */ + method(name: string, fn: (this: Context, ...args: any[]) => any, opts?: any): this; + method(obj: Partial): this; + + /** Object of currently defined methods on this schema. */ + methods: AddThisParameter & AnyObject; + + /** The original object passed to the schema constructor */ + obj: SchemaDefinition, RawDocType>; + + /** Returns a new schema that has the `paths` from the original schema, minus the omitted ones. */ + omit(paths: string[], options?: SchemaOptions): T; + + options: SchemaOptions; + + /** Gets/sets schema paths. */ + path>(path: string): ResultType; + path(path: pathGeneric): SchemaType; + path(path: string, constructor: any): this; + + /** Lists all paths and their type in the schema. */ + paths: { + [key: string]: SchemaType; + }; + + /** Returns the pathType of `path` for this schema. */ + pathType(path: string): string; + + /** Registers a plugin for this schema. */ + plugin, POptions extends Parameters[1] = Parameters[1]>(fn: PFunc, opts?: POptions): this; + plugin, any, any, any, any>, POptions extends Parameters[1] = Parameters[1]>(fn: PFunc, opts?: POptions): this; + + /** Defines a post hook for the model. */ + + // PostMiddlewareFunction + // with errorHandler set to true + post>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption): this; + post(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption): this; + post>(method: 'aggregate' | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption>): this; + post(method: 'insertMany' | RegExp, options: SchemaPostOptions & { errorHandler: true }, fn: ErrorHandlingMiddlewareWithOption): this; + + // this = never since it never happens + post(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: false }, fn: PostMiddlewareFunction): this; + post(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPostOptions & { document: boolean, query: false }, fn: PostMiddlewareFunction>): this; + post(method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: true }, fn: PostMiddlewareFunction): this; + // this = Document + post(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], fn: PostMiddlewareFunction): this; + post(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPostOptions & SchemaPostOptions, fn: PostMiddlewareFunction): this; + post(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: true, query: false }, fn: PostMiddlewareFunction): this; + post(method: 'init', fn: PostMiddlewareFunction): this; + + // this = Query + post>(method: MongooseRawResultQueryMiddleware|MongooseRawResultQueryMiddleware[], fn: PostMiddlewareFunction | ModifyResult>>): this; + post>(method: MongooseDefaultQueryMiddleware|MongooseDefaultQueryMiddleware[], fn: PostMiddlewareFunction>): this; + post>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPostOptions, fn: PostMiddlewareFunction>): this; + post>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: true }, fn: PostMiddlewareFunction>): this; + // this = Union of Document and Query, could be called with any of them + post>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: true, query: true }, fn: PostMiddlewareFunction>): this; + post>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction>): this; + + // ErrorHandlingMiddlewareFunction + // this = never since it never happens + post(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPostOptions & { document: boolean, query: false }, fn: ErrorHandlingMiddlewareFunction): this; + post(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPostOptions & { document: false, query: boolean }, fn: ErrorHandlingMiddlewareFunction): this; + post(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: false }, fn: ErrorHandlingMiddlewareFunction): this; + // this = Document + post(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], fn: ErrorHandlingMiddlewareFunction): this; + post(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction): this; + post(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: true, query: false }, fn: ErrorHandlingMiddlewareFunction): this; + // this = Query + post>(method: MongooseDefaultQueryMiddleware|MongooseDefaultQueryMiddleware[], fn: ErrorHandlingMiddlewareFunction): this; + post>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction): this; + post>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: false, query: true }, fn: ErrorHandlingMiddlewareFunction): this; + // this = Union of Document and Query, could be called with any of them + post>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPostOptions & { document: true, query: true }, fn: ErrorHandlingMiddlewareFunction): this; + post>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction): this; + + // method aggregate and insertMany with PostMiddlewareFunction + post>(method: 'aggregate' | RegExp, fn: PostMiddlewareFunction>>): this; + post>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction>>): this; + post(method: 'insertMany' | RegExp, fn: PostMiddlewareFunction): this; + post(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction): this; + + // method aggregate and insertMany with ErrorHandlingMiddlewareFunction + post>(method: 'aggregate' | RegExp, fn: ErrorHandlingMiddlewareFunction>): this; + post>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction>): this; + post(method: 'bulkWrite' | 'createCollection' | 'insertMany' | RegExp, fn: ErrorHandlingMiddlewareFunction): this; + post(method: 'bulkWrite' | 'createCollection' | 'insertMany' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction): this; + + /** Defines a pre hook for the model. */ + // this = never since it never happens + pre(method: 'save', options: SchemaPreOptions & { document: false, query: boolean }, fn: PreSaveMiddlewareFunction): this; + pre(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: false, query: false }, fn: PreMiddlewareFunction): this; + pre(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPreOptions & { document: boolean, query: false }, fn: PreMiddlewareFunction): this; + pre(method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: false, query: boolean }, fn: PreMiddlewareFunction): this; + // this = Union of Document and Query, could be called with any of them + pre>( + method: 'updateOne' | RegExp, + options: SchemaPreOptions & { document: true, query: true }, + fn: PreUpdateOneMiddlewareFunction + ): this; + pre>( + method: 'deleteOne' | RegExp, + options: SchemaPreOptions & { document: true, query: true }, + fn: PreDeleteOneMiddlewareFunction + ): this; + // this = Document + pre(method: 'save', fn: PreSaveMiddlewareFunction): this; + pre(method: 'init', fn: (this: T, doc: U) => void): this; + pre(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], fn: PreMiddlewareFunction): this; + pre(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPreOptions, fn: PreMiddlewareFunction): this; + pre( + method: 'updateOne' | RegExp, + options: SchemaPreOptions & { document: true }, + fn: PreUpdateOneMiddlewareFunction + ): this; + pre( + method: 'deleteOne' | RegExp, + options: SchemaPreOptions & { document: true }, + fn: PreDeleteOneMiddlewareFunction + ): this; + pre(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: true, query: false }, fn: PreMiddlewareFunction): this; + // this = Query + pre>(method: MongooseDefaultQueryMiddleware|MongooseDefaultQueryMiddleware[], fn: PreMiddlewareFunction): this; + pre>(method: MongooseDistinctQueryMiddleware|MongooseDistinctQueryMiddleware[], options: SchemaPreOptions, fn: PreMiddlewareFunction): this; + pre>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: false, query: true }, fn: PreMiddlewareFunction): this; + // this = Union of Document and Query, could be called with any of them + pre>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, options: SchemaPreOptions & { document: true, query: true }, fn: PreMiddlewareFunction): this; + pre>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, fn: PreMiddlewareFunction): this; + // method aggregate + pre>(method: 'aggregate' | RegExp, fn: PreMiddlewareFunction): this; + /* method insertMany */ + pre( + method: 'insertMany' | RegExp, + fn: ( + this: TModelType, + docs: any | Array, + options?: InsertManyOptions & { lean?: boolean } + ) => void | Promise + ): this; + /* method bulkWrite */ + pre( + method: 'bulkWrite' | RegExp, + fn: ( + this: TModelType, + ops: Array>, + options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions + ) => void | Promise + ): this; + /* method createCollection */ + pre( + method: 'createCollection' | RegExp, + fn: ( + this: TModelType, + options?: mongodb.CreateCollectionOptions & Pick + ) => void | Promise + ): this; + + /** Object of currently defined query helpers on this schema. */ + query: TQueryHelpers; + + /** Adds a method call to the queue. */ + queue(name: string, args: any[]): this; + + /** Removes the given `path` (or [`paths`]). */ + remove(paths: string | Array): this; + + /** Removes index by name or index spec */ + removeIndex(index: string | AnyObject): this; + + /** Returns an Array of path strings that are required by this schema. */ + requiredPaths(invalidate?: boolean): string[]; + + /** Sets a schema option. */ + set(key: K, value: SchemaOptions[K], _tags?: any): this; + + /** Adds static "class" methods to Models compiled from this schema. */ + static(name: K, fn: TStaticMethods[K]): this; + static(obj: Partial & { [name: string]: (this: TModelType, ...args: any[]) => any }): this; + static(name: string, fn: (this: TModelType, ...args: any[]) => any): this; + + /** Object of currently defined statics on this schema. */ + statics: { [F in keyof TStaticMethods]: TStaticMethods[F] } & + { [name: string]: (this: TModelType, ...args: any[]) => unknown }; + + toJSONSchema(options?: { useBsonType?: boolean }): Record; + + /** Creates a virtual type with the given name. */ + virtual( + name: keyof TVirtuals | string, + options?: VirtualTypeOptions + ): VirtualType; + + /** Object of currently defined virtuals on this schema */ + virtuals: TVirtuals; + + /** Returns the virtual type with the given `name`. */ + virtualpath(name: string): VirtualType | null; + + static ObjectId: typeof Schema.Types.ObjectId; + } + + export type NumberSchemaDefinition = typeof Number | 'number' | 'Number' | typeof Schema.Types.Number | Schema.Types.Number; + export type StringSchemaDefinition = typeof String | 'string' | 'String' | typeof Schema.Types.String | Schema.Types.String; + export type BooleanSchemaDefinition = typeof Boolean | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean | Schema.Types.Boolean; + export type DateSchemaDefinition = DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date | Schema.Types.Date; + export type ObjectIdSchemaDefinition = 'ObjectId' | 'ObjectID' | typeof Schema.Types.ObjectId | Schema.Types.ObjectId | Types.ObjectId | typeof Types.ObjectId; + export type BufferSchemaDefinition = typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer; + export type Decimal128SchemaDefinition = 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 | Schema.Types.Decimal128 | Types.Decimal128; + export type BigintSchemaDefinition = 'bigint' | 'BigInt' | typeof Schema.Types.BigInt | Schema.Types.BigInt | typeof BigInt | BigInt; + export type UuidSchemaDefinition = 'uuid' | 'UUID' | typeof Schema.Types.UUID | Schema.Types.UUID; + export type MapSchemaDefinition = MapConstructor | 'Map' | typeof Schema.Types.Map; + export type UnionSchemaDefinition = 'Union' | 'union' | typeof Schema.Types.Union | Schema.Types.Union; + export type DoubleSchemaDefinition = 'double' | 'Double' | typeof Schema.Types.Double | Schema.Types.Double; + + export type SchemaDefinitionWithBuiltInClass = T extends number + ? NumberSchemaDefinition + : T extends string + ? StringSchemaDefinition + : T extends boolean + ? BooleanSchemaDefinition + : T extends NativeDate + ? DateSchemaDefinition + : (Function | string); + + export type SchemaDefinitionProperty> = + // ThisType intersection here avoids corrupting ThisType for SchemaTypeOptions (see test gh11828) + | SchemaDefinitionWithBuiltInClass & ThisType + | SchemaTypeOptions + | typeof SchemaType + | Schema + | Schema[] + | SchemaTypeOptions, EnforcedDocType, THydratedDocumentType>[] + | Function[] + | SchemaDefinition + | SchemaDefinition, EnforcedDocType, THydratedDocumentType>[] + | typeof Schema.Types.Mixed + | MixedSchemaTypeOptions; + + export type SchemaDefinition> = T extends undefined + ? { [path: string]: SchemaDefinitionProperty; } + : { [path in keyof T]?: SchemaDefinitionProperty; }; + + export type AnyArray = T[] | ReadonlyArray; + export type ExtractMongooseArray = T extends Types.Array ? AnyArray> : T; + + export interface MixedSchemaTypeOptions extends SchemaTypeOptions { + type: typeof Schema.Types.Mixed; + } + + export type RefType = + | number + | string + | Buffer + | undefined + | Types.ObjectId + | Types.Buffer + | typeof Schema.Types.Number + | typeof Schema.Types.String + | typeof Schema.Types.Buffer + | typeof Schema.Types.ObjectId + | typeof Schema.Types.UUID; + + + export type InferId = mongodb.InferIdType; + + export interface VirtualTypeOptions { + /** If `ref` is not nullish, this becomes a populated virtual. */ + ref?: string | Function; + + /** The local field to populate on if this is a populated virtual. */ + localField?: string | ((this: HydratedDocType, doc: HydratedDocType) => string); + + /** The foreign field to populate on if this is a populated virtual. */ + foreignField?: string | ((this: HydratedDocType, doc: HydratedDocType) => string); + + /** + * By default, a populated virtual is an array. If you set `justOne`, + * the populated virtual will be a single doc or `null`. + */ + justOne?: boolean; + + /** If you set this to `true`, Mongoose will call any custom getters you defined on this virtual. */ + getters?: boolean; + + /** + * If you set this to `true`, `populate()` will set this virtual to the number of populated + * documents, as opposed to the documents themselves, using `Query#countDocuments()`. + */ + count?: boolean; + + /** Add an extra match condition to `populate()`. */ + match?: QueryFilter | ((doc: Record, virtual?: this) => Record | null); + + /** Add a default `limit` to the `populate()` query. */ + limit?: number; + + /** Add a default `skip` to the `populate()` query. */ + skip?: number; + + /** + * For legacy reasons, `limit` with `populate()` may give incorrect results because it only + * executes a single query for every document being populated. If you set `perDocumentLimit`, + * Mongoose will ensure correct `limit` per document by executing a separate query for each + * document to `populate()`. For example, `.find().populate({ path: 'test', perDocumentLimit: 2 })` + * will execute 2 additional queries if `.find()` returns 2 documents. + */ + perDocumentLimit?: number; + + /** Additional options like `limit` and `lean`. */ + options?: QueryOptions & { match?: AnyObject }; + + /** If true and the given `name` is a direct child of an array, apply the virtual to the array rather than the elements. */ + applyToArray?: boolean; + + /** Additional options for plugins */ + [extra: string]: any; + } + + export class VirtualType { + /** Applies getters to `value`. */ + applyGetters(value: any, doc: Document): any; + + /** Applies setters to `value`. */ + applySetters(value: any, doc: Document): any; + + /** Adds a custom getter to this virtual. */ + get(fn: (this: T, value: any, virtualType: VirtualType, doc: T) => any): this; + + /** Adds a custom setter to this virtual. */ + set(fn: (this: T, value: any, virtualType: VirtualType, doc: T) => void): this; + } + + export type ReturnsNewDoc = { new: true } | { returnOriginal: false } | { returnDocument: 'after' }; + + export type ArrayProjectionOperators = { $slice: number | [number, number]; $elemMatch?: never } | { $elemMatch: Record; $slice?: never }; + /** + * This Type Assigns `Element | undefined` recursively to the `T` type. + * if it is an array it will do this to the element of the array, if it is an object it will do this for the properties of the object. + * `Element` is the truthy or falsy values that are going to be used as the value of the projection.(1 | true or 0 | false) + * For the elements of the array we will use: `Element | `undefined` | `ArrayProjectionOperators` + * @example + * type CalculatedType = Projector<{ a: string, b: number, c: { d: string }, d: string[] }, true> + * type CalculatedType = { + a?: true | undefined; + b?: true | undefined; + c?: true | { + d?: true | undefined; + } | undefined; + d?: true | ArrayProjectionOperators | undefined; + } + */ + export type Projector = T extends Array + ? Projector | ArrayProjectionOperators + : T extends TreatAsPrimitives + ? Element + : T extends Record + ? { + [K in keyof T]?: T[K] extends Record ? Projector | Element : Element; + } + : Element; + type _IDType = { _id?: boolean | number }; + export type InclusionProjection = IsItRecordAndNotAny extends true + ? Omit, boolean | number>, '_id'> & _IDType + : AnyObject; + export type ExclusionProjection = IsItRecordAndNotAny extends true + ? Omit, false | 0>, '_id'> & _IDType + : AnyObject; + + export type ProjectionType = (InclusionProjection & AnyObject) + | (ExclusionProjection & AnyObject) + | string; + export type SortValues = SortOrder; + + export type SortOrder = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending'; + + type _UpdateQuery = { + /** @see https://www.mongodb.com/docs/manual/reference/operator/update-field/ */ + $currentDate?: AnyKeys & AdditionalProperties; + $inc?: AnyKeys & AdditionalProperties; + $min?: AnyKeys & AdditionalProperties; + $max?: AnyKeys & AdditionalProperties; + $mul?: AnyKeys & AdditionalProperties; + $rename?: Record; + $set?: AnyKeys & AdditionalProperties; + $setOnInsert?: AnyKeys & AdditionalProperties; + $unset?: AnyKeys & AdditionalProperties; + + /** @see https://www.mongodb.com/docs/manual/reference/operator/update-array/ */ + $addToSet?: AnyKeys & AdditionalProperties; + $pop?: AnyKeys & AdditionalProperties; + $pull?: AnyKeys & AdditionalProperties; + $push?: AnyKeys & AdditionalProperties; + $pullAll?: AnyKeys & AdditionalProperties; + + /** @see https://www.mongodb.com/docs/manual/reference/operator/update-bitwise/ */ + $bit?: AnyKeys; + }; + + export type UpdateWithAggregationPipeline = UpdateAggregationStage[]; + export type UpdateAggregationStage = { $addFields: any } | + { $set: any } | + { $project: any } | + { $unset: any } | + { $replaceRoot: any } | + { $replaceWith: any }; + + /** + * Update query command to perform on the document + * @example + * ```js + * { age: 30 } + * ``` + */ + export type UpdateQuery = AnyKeys & _UpdateQuery & AnyObject; + + /** + * A more strict form of UpdateQuery that enforces updating only + * known top-level properties. + * @example + * ```ts + * function updateUser(_id: mongoose.Types.ObjectId, update: UpdateQueryKnownOnly) { + * return User.updateOne({ _id }, update); + * } + * ``` + */ + export type UpdateQueryKnownOnly = _UpdateQuery; + + export type FlattenMaps = { + [K in keyof T]: FlattenProperty; + }; + + export type BufferToBinaryProperty = unknown extends Buffer + ? T + : T extends Buffer + ? mongodb.Binary + : T extends Types.DocumentArray + ? Types.DocumentArray> + : T extends Types.Subdocument + ? HydratedSingleSubdocument> + : BufferToBinary; + + /** + * Converts any Buffer properties into mongodb.Binary instances, which is what `lean()` returns + */ + export type BufferToBinary = unknown extends Buffer + ? T + : T extends Buffer + ? mongodb.Binary + : T extends Document + ? T + : T extends TreatAsPrimitives + ? T + : T extends Record + ? { + [K in keyof T]: BufferToBinaryProperty + } + : T; + + /** + * Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization + */ + export type BufferToJSON = unknown extends Buffer + ? T + : T extends Buffer + ? { type: 'buffer', data: number[] } + : T extends Document + ? T + : T extends TreatAsPrimitives + ? T + : T extends Record ? { + [K in keyof T]: T[K] extends Buffer + ? { type: 'buffer', data: number[] } + : T[K] extends Types.DocumentArray + ? Types.DocumentArray> + : T[K] extends Types.Subdocument + ? HydratedSingleSubdocument + : BufferToBinary; + } : T; + + /** + * Converts any UUID properties into strings for JSON serialization + */ + export type UUIDToString = T extends Types.UUID + ? string + : T extends mongodb.UUID + ? string + : T extends Document + ? T + : T extends TreatAsPrimitives + ? T + : T extends Record ? { + [K in keyof T]: T[K] extends Types.UUID + ? string + : T[K] extends mongodb.UUID + ? string + : T[K] extends Types.DocumentArray + ? Types.DocumentArray> + : T[K] extends Types.Subdocument + ? HydratedSingleSubdocument> + : UUIDToString; + } : T; + + /** + * Alias for UUIDToString for backwards compatibility. + * @deprecated Use UUIDToString instead. + */ + export type UUIDToJSON = UUIDToString; + + /** + * Converts any ObjectId properties into strings for JSON serialization + */ + export type ObjectIdToString = T extends mongodb.ObjectId + ? string + : T extends Document + ? T + : T extends TreatAsPrimitives + ? T + : T extends Record ? { + [K in keyof T]: T[K] extends mongodb.ObjectId + ? string + : T[K] extends Types.DocumentArray + ? Types.DocumentArray> + : T[K] extends Types.Subdocument + ? HydratedSingleSubdocument> + : ObjectIdToString; + } : T; + + /** + * Converts any Date properties into strings for JSON serialization + */ + export type DateToString = T extends NativeDate + ? string + : T extends Document + ? T + : T extends TreatAsPrimitives + ? T + : T extends Record ? { + [K in keyof T]: T[K] extends NativeDate + ? string + : T[K] extends (NativeDate | null | undefined) + ? string | null | undefined + : T[K] extends Types.DocumentArray + ? Types.DocumentArray> + : T[K] extends Types.Subdocument + ? HydratedSingleSubdocument> + : DateToString; + } : T; + + /** + * Converts any Mongoose subdocuments (single nested or doc arrays) into POJO equivalents + */ + export type SubdocsToPOJOs = T extends Document + ? T + : T extends TreatAsPrimitives + ? T + : T extends Record ? { + [K in keyof T]: T[K] extends Types.DocumentArray + ? ItemType[] + : T[K] extends Types.Subdocument + ? SubdocType + : SubdocsToPOJOs; + } : T; + + export type JSONSerialized = SubdocsToPOJOs< + FlattenMaps< + BufferToJSON< + ObjectIdToString< + UUIDToJSON< + DateToString + > + > + > + > + >; + + /** + * Helper types for computing toObject/toJSON return types based on options. + * These compose transforms conditionally to avoid combinatorial explosion of overloads. + */ + type ApplyVirtuals = + O extends { virtuals: true } ? T & TVirtuals : T; + + type GetVersionKeyName = + TSchemaOptions extends { versionKey: infer VK } + ? VK extends string ? VK : '__v' + : '__v'; + + type ApplyVersionKey = + O extends { versionKey: false } ? Omit> : T; + + /** + * Single-pass type that applies all flatten transforms (flattenMaps, flattenObjectIds, flattenUUIDs) + * in one recursive traversal. This avoids TypeScript's "union type too complex" error that occurs + * when nesting separate recursive transform types. + * + * By handling all transforms in one pass, we correctly handle ObjectIds/UUIDs nested inside Maps + * since we recurse into Map values during the same traversal that converts ObjectIds/UUIDs. + */ + type ApplyFlattenTransforms = + // Handle ObjectId first (before TreatAsPrimitives since ObjectId is in TreatAsPrimitives) + T extends mongodb.ObjectId + ? O extends { flattenObjectIds: true } ? string : T + // Handle UUID (both Types.UUID and mongodb.UUID) + : T extends Types.UUID + ? O extends { flattenUUIDs: true } ? string : T + : T extends mongodb.UUID + ? O extends { flattenUUIDs: true } ? string : T + // Handle Map - flatten to Record and recurse into values + : T extends Map + ? O extends { flattenMaps: true } + ? Record> + : T + // Don't recurse into Documents + : T extends Document + ? T + // Don't modify primitives + : T extends TreatAsPrimitives + ? T + // Handle DocumentArray - recurse into items + : T extends Types.DocumentArray + ? Types.DocumentArray> + // Handle plain arrays - recurse into items + : T extends Array + ? ApplyFlattenTransforms[] + // Handle Subdocument - recurse into subdoc type + : T extends Types.Subdocument + ? HydratedSingleSubdocument> + // Handle regular objects - recurse into properties + : T extends Record + ? { [K in keyof T]: ApplyFlattenTransforms } + : T; + + /** + * Extracts the `toObject` or `toJSON` options declared in the schema options. + * The runtime applies these as defaults when the corresponding method is + * called without arguments. + */ + export type SchemaDeclaredToObjectOptions = + Key extends keyof TSchemaOptions + ? NonNullable extends infer O extends ToObjectOptions + ? O + : {} + : {}; + + /** + * Computes the return type of toObject/toJSON when called without arguments, + * applying the options declared in the schema like the runtime does. When the + * schema declares no options for the method, the plain document shape is + * returned without running it through the option transforms. + */ + export type DefaultToObjectReturnType = + SchemaDeclaredToObjectOptions extends infer O extends ToObjectOptions + ? keyof O extends never + ? Default__v, TSchemaOptions> + : ToObjectReturnType + : Default__v, TSchemaOptions>; + + /** + * Computes the return type of toObject/toJSON based on the provided options. + * Uses a single-pass transform for flatten operations to correctly handle all combinations. + */ + export type ToObjectReturnType = + ApplyVersionKey< + Default__v< + ApplyFlattenTransforms< + ApplyVirtuals, TVirtuals, O>, + O + >, + TSchemaOptions + >, + O, + TSchemaOptions + >; + + /** + * Separate type is needed for properties of union type (for example, Types.DocumentArray | undefined) to apply conditional check to each member of it + * https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types + */ + type FlattenProperty = T extends Map + ? Record : T extends TreatAsPrimitives + ? T : T extends Document ? T : T extends Types.DocumentArray + ? Types.DocumentArray> : FlattenMaps; + + export type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined; + export type TreatAsPrimitives = actualPrimitives | NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId | Buffer | Function | mongodb.Binary | mongodb.ClientSession; + + export type SchemaDefinitionType = T extends Document ? Omit> : T; + + /* for ts-mongoose */ + export class mquery { } + + export function overwriteMiddlewareResult(val: any): Kareem.OverwriteMiddlewareResult; + + export function skipMiddlewareFunction(val: any): Kareem.SkipWrappedFunction; + + export function overwriteMiddlewareArguments(val: any): Kareem.OverwriteArguments; + + export default mongoose; +} diff --git a/gateway/node_modules/mongoose/types/indexes.d.ts b/gateway/node_modules/mongoose/types/indexes.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..10cbdd5054b856f3e6eda90a30696cb1821e7c9a --- /dev/null +++ b/gateway/node_modules/mongoose/types/indexes.d.ts @@ -0,0 +1,97 @@ +declare module 'mongoose' { + import mongodb = require('mongodb'); + + /** + * Makes the indexes in MongoDB match the indexes defined in every model's + * schema. This function will drop any indexes that are not defined in + * the model's schema except the `_id` index, and build any indexes that + * are in your schema but not in MongoDB. + */ + function syncIndexes(options?: SyncIndexesOptions): Promise; + + interface IndexManager { + /* Deletes all indexes that aren't defined in this model's schema. Used by `syncIndexes()`. Returns list of dropped index names. */ + cleanIndexes(options?: { toDrop?: string[], hideIndexes?: boolean }): Promise; + + /** + * Similar to `ensureIndexes()`, except for it uses the [`createIndex`](https://mongodb.github.io/node-mongodb-native/7.0/classes/Collection.html#createIndex) + * function. + */ + createIndexes(options?: mongodb.CreateIndexesOptions): Promise; + + /** + * Does a dry-run of Model.syncIndexes(), meaning that + * the result of this function would be the result of + * Model.syncIndexes(). + */ + diffIndexes(options?: Record): Promise + + /** + * Sends `createIndex` commands to mongo for each index declared in the schema. + * The `createIndex` commands are sent in series. + */ + ensureIndexes(options?: mongodb.CreateIndexesOptions): Promise; + + /** + * Lists the indexes currently defined in MongoDB. This may or may not be + * the same as the indexes defined in your schema depending on whether you + * use the [`autoIndex` option](/docs/guide.html#autoIndex) and if you + * build indexes manually. + */ + listIndexes(): Promise>; + + /** + * Makes the indexes in MongoDB match the indexes defined in this model's + * schema. This function will drop any indexes that are not defined in + * the model's schema except the `_id` index, and build any indexes that + * are in your schema but not in MongoDB. + */ + syncIndexes(options?: SyncIndexesOptions): Promise>; + } + + interface IndexesDiff { + /** Indexes that would be created in mongodb. */ + toCreate: Array + /** Indexes that would be dropped in mongodb. */ + toDrop: Array + } + + type IndexDirection = 1 | -1 | '2d' | '2dsphere' | 'geoHaystack' | 'hashed' | 'text' | 'ascending' | 'asc' | 'descending' | 'desc'; + type IndexDefinition = Record; + + interface SyncIndexesOptions extends mongodb.CreateIndexesOptions { + continueOnError?: boolean; + hideIndexes?: boolean; + } + type ConnectionSyncIndexesResult = Record; + type OneCollectionSyncIndexesResult = Array & mongodb.MongoServerError; + + type IndexOptions = Omit & { + /** + * `expires` utilizes the `ms` module from [vercel](https://github.com/vercel/ms) allowing us to use a friendlier syntax: + * + * @example + * ```js + * const schema = new Schema({ prop1: Date }); + * + * // expire in 24 hours + * schema.index({ prop1: 1 }, { expires: 60*60*24 }) + * + * // expire in 24 hours + * schema.index({ prop1: 1 }, { expires: '24h' }) + * + * // expire in 1.5 hours + * schema.index({ prop1: 1 }, { expires: '1.5h' }) + * + * // expire in 7 days + * schema.index({ prop1: 1 }, { expires: '7d' }) + * ``` + */ + expires?: number | string; + weights?: Record; + + unique?: boolean | [true, string] + }; + + type SearchIndexDescription = mongodb.SearchIndexDescription; +} diff --git a/gateway/node_modules/mongoose/types/inferhydrateddoctype.d.ts b/gateway/node_modules/mongoose/types/inferhydrateddoctype.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..b1789ceca969871f0b6afcacd96fd9a7b18af52e --- /dev/null +++ b/gateway/node_modules/mongoose/types/inferhydrateddoctype.d.ts @@ -0,0 +1,151 @@ +import { + IsPathRequired, + IsSchemaTypeFromBuiltinClass, + RequiredPaths, + OptionalPaths, + PathEnumOrString +} from './inferschematype'; +import { UUID } from 'mongodb'; + +declare module 'mongoose' { + export type InferHydratedDocTypeFromSchema> = ObtainSchemaGeneric; + + /** + * Given a schema definition, returns the hydrated document type from the schema definition. + */ + export type InferHydratedDocType< + DocDefinition, + TSchemaOptions extends Record = DefaultSchemaOptions + > = Require_id & + OptionalPaths) + ]: IsPathRequired extends true + ? ObtainHydratedDocumentPathType + : ObtainHydratedDocumentPathType | null; + }, TSchemaOptions>>; + + /** + * @summary Obtains schema Path type. + * @description Obtains Path type by separating path type from other options and calling {@link ResolveHydratedPathType} + * @param {PathValueType} PathValueType Document definition path type. + * @param {TypeKey} TypeKey A generic refers to document definition. + */ + type ObtainHydratedDocumentPathType< + PathValueType, + TypeKey extends string = DefaultTypeKey + > = ResolveHydratedPathType< + TypeKey extends keyof PathValueType + ? TypeKey extends keyof PathValueType[TypeKey] + ? PathValueType + : PathValueType[TypeKey] + : PathValueType, + TypeKey extends keyof PathValueType + ? TypeKey extends keyof PathValueType[TypeKey] + ? {} + : Omit + : {}, + TypeKey, + HydratedDocTypeHint, + HydratedDiscriminatorEnumType + >; + + /** + * @summary Allows users to optionally choose their own type for a schema field for stronger typing. + */ + type HydratedDocTypeHint = T extends { __hydratedDocTypeHint: infer U } ? U + : never; + + type DiscriminatorHydratedKey = + ObtainSchemaGeneric extends infer TSchemaOptions ? + 'discriminatorKey' extends keyof TSchemaOptions ? + TSchemaOptions['discriminatorKey'] extends string ? TSchemaOptions['discriminatorKey'] : '__t' + : '__t' + : '__t'; + + type DiscriminatorHydratedBaseType = + InferHydratedDocTypeFromSchema extends Record, any> ? + InferHydratedDocTypeFromSchema + : MergeType, { [K in DiscriminatorHydratedKey]?: null }>; + + type ResolveDiscriminatorHydratedPathType = + IsAny extends true ? never + : TDiscriminators extends Record ? + DiscriminatorHydratedBaseType | + { + [K in keyof TDiscriminators]: TDiscriminators[K] extends Schema ? + MergeType, InferHydratedDocTypeFromSchema> & + { [KDiscriminatorKey in DiscriminatorHydratedKey]: K } + : never + }[keyof TDiscriminators] + : never; + + type HydratedDiscriminatorEnumType = string extends keyof T ? never + : number extends keyof T ? never + : T extends { type: infer BaseType; discriminators: infer TDiscriminators } ? + IsAny extends true ? never + : BaseType extends Schema ? + ResolveDiscriminatorHydratedPathType + : never + : never; + + /** + * Same as inferSchemaType, except: + * + * 1. Replace `Types.DocumentArray` and `Types.Array` with vanilla `Array` + * 2. Replace `ObtainDocumentPathType` with `ObtainHydratedDocumentPathType` + * 3. Replace `ResolvePathType` with `ResolveHydratedPathType` + * + * @summary Resolve path type by returning the corresponding type. + * @param {PathValueType} PathValueType Document definition path type. + * @param {Options} Options Document definition path options except path type. + * @param {TypeKey} TypeKey A generic of literal string type. Refers to the property used for path type definition. + * @returns Type + */ + type ResolveHydratedPathType = {}, TypeKey extends string = DefaultSchemaOptions['typeKey'], TypeHint = never, TDiscriminatorEnumType = never> = + IsNotNever extends true ? TypeHint + : IsNotNever extends true ? TDiscriminatorEnumType + : PathValueType extends Schema ? + THydratedDocumentType : + PathValueType extends AnyArray ? + IfEquals ? + IsItRecordAndNotAny extends true ? + Types.DocumentArray & EmbeddedHydratedDocType> : + Types.DocumentArray, Types.Subdocument['_id'], unknown, InferHydratedDocType> & InferHydratedDocType> : + Item extends Record ? + Item[TypeKey] extends Function | String ? + Types.Array> : + Types.DocumentArray< + InferRawDocType, + Types.Subdocument['_id'], unknown, InferHydratedDocType> & InferHydratedDocType + >: + IsSchemaTypeFromBuiltinClass extends true ? + Types.Array }, TypeKey>> : + IsItRecordAndNotAny extends true ? + Item extends Record ? + Types.Array> : + Types.DocumentArray< + InferRawDocType, + Types.Subdocument['_id'], unknown, InferHydratedDocType> & InferHydratedDocType + > : + Types.Array> + > + : PathValueType extends StringSchemaDefinition ? PathEnumOrString + : IfEquals extends true ? PathEnumOrString + : PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray ? Options['enum'][number] : number + : PathValueType extends DateSchemaDefinition ? NativeDate + : PathValueType extends BufferSchemaDefinition ? Buffer + : PathValueType extends BooleanSchemaDefinition ? boolean + : PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId + : PathValueType extends Decimal128SchemaDefinition ? Types.Decimal128 + : PathValueType extends BigintSchemaDefinition ? bigint + : PathValueType extends UuidSchemaDefinition ? UUID + : PathValueType extends DoubleSchemaDefinition ? Types.Double + : PathValueType extends typeof Schema.Types.Mixed ? any + : PathValueType extends MapSchemaDefinition ? Map> + : IfEquals extends true ? any + : PathValueType extends typeof SchemaType ? PathValueType['prototype'] + : PathValueType extends ArrayConstructor ? Types.Array + : PathValueType extends Record ? InferHydratedDocType + : unknown; +} diff --git a/gateway/node_modules/mongoose/types/inferrawdoctype.d.ts b/gateway/node_modules/mongoose/types/inferrawdoctype.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..a783951c14668acf7c946c60d43797a5c230003a --- /dev/null +++ b/gateway/node_modules/mongoose/types/inferrawdoctype.d.ts @@ -0,0 +1,201 @@ +import { + IsSchemaTypeFromBuiltinClass, + PathEnumOrString, + OptionalPaths, + RequiredPaths, + IsPathRequired, + PathAllowsNull +} from './inferschematype'; +import { Binary, UUID } from 'mongodb'; + +declare module 'mongoose' { + export type InferRawDocTypeFromSchema> = IsItRecordAndNotAny> extends true + ? ObtainSchemaGeneric + : unknown extends ObtainSchemaGeneric + ? Require_id>>> + : InferRawDocType>; + + export type InferRawDocTypeWithout_id< + SchemaDefinition, + TSchemaOptions extends Record = DefaultSchemaOptions, + TTransformOptions = { bufferToBinary: false } + > = ApplySchemaOptions<{ + [ + K in keyof (RequiredPaths & + OptionalPaths) + ]: IsPathRequired extends true + ? ObtainRawDocumentPathType + : PathAllowsNull extends true + ? ObtainRawDocumentPathType | null + : ObtainRawDocumentPathType; + }, TSchemaOptions>; + + export type InferRawDocType< + SchemaDefinition, + TSchemaOptions extends Record = DefaultSchemaOptions, + TTransformOptions = { bufferToBinary: false } + > = TSchemaOptions extends { _id: false } + ? InferRawDocTypeWithout_id + : Require_id>; + + /** + * @summary Allows users to optionally choose their own type for a schema field for stronger typing. + * Make sure to check for `any` because `T extends { __rawDocTypeHint: infer U }` will infer `unknown` if T is `any`. + */ + type RawDocTypeHint = IsAny extends true ? never + : T extends { __rawDocTypeHint: infer U } ? U: never; + + type DiscriminatorRawKey = + ObtainSchemaGeneric extends infer TSchemaOptions ? + 'discriminatorKey' extends keyof TSchemaOptions ? + TSchemaOptions['discriminatorKey'] extends string ? TSchemaOptions['discriminatorKey'] : '__t' + : '__t' + : '__t'; + + type DiscriminatorRawBaseType = + InferRawDocTypeFromSchema extends Record, any> ? + InferRawDocTypeFromSchema + : MergeType, { [K in DiscriminatorRawKey]?: null }>; + + type ResolveDiscriminatorRawPathType = + IsAny extends true ? never + : TDiscriminators extends Record ? + DiscriminatorRawBaseType | + { + [K in keyof TDiscriminators]: TDiscriminators[K] extends Schema ? + MergeType, InferRawDocTypeFromSchema> & + { [KDiscriminatorKey in DiscriminatorRawKey]: K } + : never + }[keyof TDiscriminators] + : never; + + type RawDiscriminatorEnumType = string extends keyof T ? never + : number extends keyof T ? never + : T extends { type: infer BaseType; discriminators: infer TDiscriminators } ? + IsAny extends true ? never + : BaseType extends Schema ? + ResolveDiscriminatorRawPathType + : never + : never; + + /** + * @summary Obtains schema Path type. + * @description Obtains Path type by separating path type from other options and calling {@link ResolveRawPathType} + * @param {PathValueType} PathValueType Document definition path type. + * @param {TypeKey} TypeKey A generic refers to document definition. + */ + type ObtainRawDocumentPathType< + PathValueType, + TypeKey extends string = DefaultTypeKey, + TTransformOptions = { bufferToBinary: false } + > = ResolveRawPathType< + TypeKey extends keyof PathValueType ? + TypeKey extends keyof PathValueType[TypeKey] ? + PathValueType + : PathValueType[TypeKey] + : PathValueType, + TypeKey extends keyof PathValueType ? + TypeKey extends keyof PathValueType[TypeKey] ? + {} + : Omit + : {}, + TypeKey, + TTransformOptions, + RawDocTypeHint, + RawDiscriminatorEnumType, + TypeKey extends keyof PathValueType ? false : true + >; + + type neverOrAny = ' ~neverOrAny~'; + + /** + * Same as inferSchemaType, except: + * + * 1. Replace `Types.DocumentArray` and `Types.Array` with vanilla `Array` + * 2. Replace `ObtainDocumentPathType` with `ObtainRawDocumentPathType` + * 3. Replace `ResolvePathType` with `ResolveRawPathType` + * + * @summary Resolve path type by returning the corresponding type. + * @param {PathValueType} PathValueType Document definition path type. + * @param {Options} Options Document definition path options except path type. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". + * @returns Number, "Number" or "number" will be resolved to number type. + */ + type ResolveRawPathType< + PathValueType, + Options extends SchemaTypeOptions = {}, + TypeKey extends string = DefaultSchemaOptions['typeKey'], + TTransformOptions = { bufferToBinary: false }, + TypeHint = never, + TDiscriminatorEnumType = never, + IsNestedPath extends boolean = false + > = + IsNotNever extends true ? TypeHint + : IsNotNever extends true ? TDiscriminatorEnumType + : [PathValueType] extends [neverOrAny] ? PathValueType + : PathValueType extends Schema ? + IsItRecordAndNotAny extends true ? + RawDocType : + unknown extends TSchemaDefinition ? + TSchemaOptions extends { _id: false } ? + FlattenMaps> : + Require_id>> : + InferRawDocType, TTransformOptions> + : PathValueType extends ReadonlyArray ? + IfEquals extends true ? any[] + : Item extends Schema ? + // If Item is a schema, infer its type. + Array extends true ? + RawDocType : + unknown extends TSchemaDefinition ? + TSchemaOptions extends { _id: false } ? + FlattenMaps> : + Require_id>> : + InferRawDocType, TTransformOptions>> + : TypeKey extends keyof Item ? + Item[TypeKey] extends Function | String ? + // If Item has a type key that's a string or a callable, it must be a scalar, + // so we can directly obtain its path type. + ObtainRawDocumentPathType[] + : // If the type key isn't callable, then this is an array of objects, in which case + // we need to call InferRawDocType to correctly infer its type. + Array> + : IsSchemaTypeFromBuiltinClass extends true ? ResolveRawPathType }, TypeKey, TTransformOptions>[] + : IsItRecordAndNotAny extends true ? + Item extends Record ? + ObtainRawDocumentPathType[] + : Array> + : ObtainRawDocumentPathType[] + : PathValueType extends StringSchemaDefinition ? PathEnumOrString + : IfEquals extends true ? PathEnumOrString + : PathValueType extends NumberSchemaDefinition ? + Options['enum'] extends ReadonlyArray ? + Options['enum'][number] + : number + : PathValueType extends DateSchemaDefinition ? NativeDate + : PathValueType extends BufferSchemaDefinition ? (TTransformOptions extends { bufferToBinary: true } ? Binary : Buffer) + : PathValueType extends BooleanSchemaDefinition ? boolean + : PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId + : PathValueType extends Decimal128SchemaDefinition ? Types.Decimal128 + : PathValueType extends BigintSchemaDefinition ? bigint + : PathValueType extends UuidSchemaDefinition ? Types.UUID + : PathValueType extends MapSchemaDefinition ? Record> + : PathValueType extends DoubleSchemaDefinition ? Types.Double + : PathValueType extends UnionSchemaDefinition ? + ResolveRawPathType ? Item : never> + : PathValueType extends ArrayConstructor ? any[] + : PathValueType extends typeof Schema.Types.Mixed ? any + : IfEquals extends true ? any + : IfEquals extends true ? any + : PathValueType extends typeof SchemaType ? PathValueType['prototype'] + : PathValueType extends Record ? + IsNestedPath extends true ? + // Nested path (no type key) - no _id + InferRawDocTypeWithout_id + : Options extends { _id: false } ? + // Subdocument with _id: false + InferRawDocTypeWithout_id + : // Subdocument with _id (default) + InferRawDocType + : unknown; +} diff --git a/gateway/node_modules/mongoose/types/inferschematype.d.ts b/gateway/node_modules/mongoose/types/inferschematype.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..259865ed649694a5a08bdc720e9246d7e807c6c0 --- /dev/null +++ b/gateway/node_modules/mongoose/types/inferschematype.d.ts @@ -0,0 +1,387 @@ +import { + AnyArray, + BooleanSchemaDefinition, + BigintSchemaDefinition, + BufferSchemaDefinition, + DateSchemaDefinition, + Decimal128SchemaDefinition, + DefaultSchemaOptions, + DefaultTypeKey, + DoubleSchemaDefinition, + IfAny, + IfEquals, + InferSchemaType, + IsItRecordAndNotAny, + MapSchemaDefinition, + MergeType, + NumberSchemaDefinition, + ObjectIdSchemaDefinition, + ObtainDocumentType, + Schema, + SchemaType, + SchemaTypeOptions, + StringSchemaDefinition, + Types, + UnionSchemaDefinition, + UuidSchemaDefinition +} from 'mongoose'; + +declare module 'mongoose' { + /** + * @summary Obtains document schema type. + * @description Obtains document schema type from document Definition OR returns enforced schema type if it's provided. + * @param {DocDefinition} DocDefinition A generic equals to the type of document definition "provided in as first parameter in Schema constructor". + * @param {EnforcedDocType} EnforcedDocType A generic type enforced by user "provided before schema constructor". + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". + */ + type ObtainDocumentType< + DocDefinition, + EnforcedDocType = any, + TSchemaOptions extends Record = DefaultSchemaOptions + > = + IsItRecordAndNotAny extends true ? EnforcedDocType + : { + [K in keyof (RequiredPaths & + OptionalPaths)]: IsPathRequired< + DocDefinition[K], + TSchemaOptions['typeKey'] + > extends true ? + ObtainDocumentPathType + : PathAllowsNull extends true ? + ObtainDocumentPathType | null + : ObtainDocumentPathType; + }; + + /** + * @summary Obtains document schema type from Schema instance. + * @param {Schema} TSchema `typeof` a schema instance. + * @example + * const userSchema = new Schema({userName:String}); + * type UserType = InferSchemaType; + * // result + * type UserType = {userName?: string} + */ + export type InferSchemaType = IfAny>; + + export type DefaultIdVirtual = { id: string }; + export type AddDefaultId = (DocType extends { id: any } ? TVirtuals : TSchemaOptions extends { id: false } ? TVirtuals : TVirtuals & { id: string }); + + /** + * @summary Obtains schema Generic type by using generic alias. + * @param {TSchema} TSchema A generic of schema type instance. + * @param {alias} alias Targeted generic alias. + */ + type ObtainSchemaGeneric< + TSchema, + alias extends + | 'EnforcedDocType' + | 'M' + | 'TInstanceMethods' + | 'TQueryHelpers' + | 'TVirtuals' + | 'TStaticMethods' + | 'TSchemaOptions' + | 'DocType' + | 'THydratedDocumentType' + | 'TSchemaDefinition' + | 'TLeanResultType' + > = + TSchema extends ( + Schema< + infer EnforcedDocType, + infer M, + infer TInstanceMethods, + infer TQueryHelpers, + infer TVirtuals, + infer TStaticMethods, + infer TSchemaOptions, + infer DocType, + infer THydratedDocumentType, + infer TSchemaDefinition, + infer TLeanResultType + > + ) ? + { + EnforcedDocType: EnforcedDocType; + M: M; + TInstanceMethods: IfEquals; + TQueryHelpers: IfEquals; + TVirtuals: AddDefaultId, TSchemaOptions>; + TStaticMethods: IfEquals; + TSchemaOptions: TSchemaOptions; + DocType: DocType; + THydratedDocumentType: THydratedDocumentType; + TSchemaDefinition: TSchemaDefinition; + TLeanResultType: TLeanResultType; + }[alias] + : unknown; + + type TypesAreEqual = [A] extends [B] ? [B] extends [A] ? true : false : false; + + type ResolveSchemaOptions = + keyof T extends never ? DefaultSchemaOptions : + TypesAreEqual extends true ? DefaultSchemaOptions : + MergeType; + + /*! + * ResolveVirtuals resolves the virtuals option from the schema options. + * Adds the default `id` virtual if it is not disabled in schema options or + * overwritten in the RawDocType. + */ + type ResolveVirtuals = + ResolveSchemaOptions extends { virtuals: infer V } + ? AddDefaultId> + : {}; + + type ApplySchemaOptions = ResolveTimestamps; + + type DefaultTimestampProps = { + createdAt: NativeDate; + updatedAt: NativeDate; + }; + + type ResolveTimestamps = + O extends { timestamps?: false } ? T + : O extends { timestamps: infer TimestampOptions } ? + TimestampOptions extends true ? T & DefaultTimestampProps + : TimestampOptions extends SchemaTimestampsConfig ? + Show< + T & { + [K in keyof TimestampOptions & keyof DefaultTimestampProps as TimestampOptions[K] extends true ? K + : TimestampOptions[K] & string]: NativeDate; + } + > + : T + : T; +} + +type RequiredPropertyDefinition = + | { + required: true | string | [true, string | undefined] | { isRequired: true }; + } + | ArrayConstructor + | any[]; + +/** + * @summary Checks if a document path is required or optional. + * @param {P} P Document path. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". + */ +type IsPathRequired = + P extends RequiredPropertyDefinition ? true + : P extends { required: boolean } ? + P extends { required: false } ? + false + : true + : P extends { default: undefined | null | ((...args: any[]) => undefined) | ((...args: any[]) => null) } ? false + : P extends { default: any } ? true + : P extends Record ? true + : false; + +// Internal type used to efficiently check for never or any types +// can be efficiently checked like: +// `[T] extends [neverOrAny] ? T : ...` +// to avoid edge cases +type neverOrAny = ' ~neverOrAny~'; + +/** + * @summary A Utility to obtain schema's required path keys. + * @param {T} T A generic refers to document definition. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". + * @returns required paths keys of document definition. + */ +type RequiredPathKeys = Exclude>; + +/** + * @summary A Utility to obtain schema's required paths. + * @param {T} T A generic refers to document definition. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". + * @returns a record contains required paths with the corresponding type. + */ +type RequiredPaths = Pick< + { -readonly [K in keyof T]: T[K] }, + RequiredPathKeys +>; + +/** + * @summary A Utility to obtain schema's optional path keys. + * @param {T} T A generic refers to document definition. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". + * @returns optional paths keys of document definition. + */ +type OptionalPathKeys = { + [K in keyof T]: IsPathRequired extends true ? never : K; +}[keyof T]; + +/** + * @summary A Utility to obtain schema's optional paths. + * @param {T} T A generic refers to document definition. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". + * @returns a record contains optional paths with the corresponding type. + */ +type OptionalPaths = Pick< + { -readonly [K in keyof T]?: T[K] }, + OptionalPathKeys +>; + +/** + * @summary Checks if a document path allows `null` values. + * @param {P} P Document path. + */ +export type PathAllowsNull

= P extends { allowNull: false } ? false : true; + +/** + * @summary Allows users to optionally choose their own type for a schema field for stronger typing. + */ +type TypeHint = T extends { __typehint: infer U } ? U : never; + +/** + * @summary Obtains schema Path type. + * @description Obtains Path type by separating path type from other options and calling {@link ResolvePathType} + * @param {PathValueType} PathValueType Document definition path type. + * @param {TypeKey} TypeKey A generic refers to document definition. + */ +type ObtainDocumentPathType = ResolvePathType< + TypeKey extends keyof PathValueType ? + TypeKey extends keyof PathValueType[TypeKey] ? + PathValueType + : PathValueType[TypeKey] + : PathValueType, + TypeKey extends keyof PathValueType ? + TypeKey extends keyof PathValueType[TypeKey] ? + {} + : Omit + : {}, + TypeKey, + TypeHint, + DiscriminatorEnumType +>; + +/** + * @param {T} T A generic refers to string path enums. + * @returns Path enum values type as literal strings or string. + */ +type PathEnumOrString['enum']> = + T extends ReadonlyArray ? E + : T extends { values: any } ? PathEnumOrString + : T extends Record ? V + : string; + +type UnionToType = T[number] extends infer U + ? ResolvePathType + : never; + +type ResolveDiscriminatorPathType = + IfAny ? + TDiscriminators[keyof TDiscriminators] extends Schema ? + MergeType, InferSchemaType> + : never + : never>; + +type DiscriminatorEnumType = string extends keyof T ? never + : number extends keyof T ? never + : T extends { type: infer BaseType; discriminators: infer TDiscriminators } ? + IfAny + : never> + : never; + +type IsSchemaTypeFromBuiltinClass = + T extends typeof String ? true + : unknown extends Buffer ? false + : T extends typeof Number ? true + : T extends typeof Boolean ? true + : T extends typeof Buffer ? true + : T extends typeof Schema.Types.ObjectId ? true + : T extends typeof Schema.Types.UUID ? true + : T extends typeof Schema.Types.Decimal128 ? true + : T extends typeof Schema.Types.Int32 ? true + : T extends typeof Schema.Types.String ? true + : T extends typeof Schema.Types.Number ? true + : T extends typeof Schema.Types.Date ? true + : T extends typeof Schema.Types.Double ? true + : T extends typeof Schema.Types.Boolean ? true + : T extends Types.ObjectId ? true + : T extends Types.Decimal128 ? true + : T extends NativeDate ? true + : T extends typeof Schema.Types.Mixed ? true + : T extends Types.UUID ? true + : T extends Buffer ? true + : false; + +/** + * @summary Resolve path type by returning the corresponding type. + * @param {PathValueType} PathValueType Document definition path type. + * @param {Options} Options Document definition path options except path type. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". + * @returns Number, "Number" or "number" will be resolved to number type. + */ +type ResolvePathType< + PathValueType, + Options extends SchemaTypeOptions = {}, + TypeKey extends string = DefaultSchemaOptions['typeKey'], + TypeHint = never, + TDiscriminatorEnumType = never +> = [TypeHint] extends [never] + ? [TDiscriminatorEnumType] extends [never] + ? PathValueType extends Schema ? + InferSchemaType + : PathValueType extends AnyArray ? + [Item] extends [never] + ? any[] + : Item extends Schema ? + // If Item is a schema, infer its type. + Types.DocumentArray> + : Item extends Record ? + Item[TypeKey] extends Schema ? + Types.DocumentArray< + [DiscriminatorEnumType] extends [never] ? InferSchemaType : DiscriminatorEnumType + > + : Item[TypeKey] extends Function | String ? + // If Item has a type key that's a string or a callable, it must be a scalar, + // so we can directly obtain its path type. + ObtainDocumentPathType[] + : // If the type key isn't callable, then this is an array of objects, in which case + // we need to call ObtainDocumentType to correctly infer its type. + Types.DocumentArray> + : IsSchemaTypeFromBuiltinClass extends true ? ResolvePathType }, TypeKey>[] + : IsItRecordAndNotAny extends true ? + Item extends Record ? + ObtainDocumentPathType[] + : Types.DocumentArray> + : ObtainDocumentPathType[] + : PathValueType extends StringSchemaDefinition ? PathEnumOrString + : IfEquals extends true ? PathEnumOrString + : PathValueType extends NumberSchemaDefinition ? + Options['enum'] extends ReadonlyArray ? + Options['enum'][number] + : number + : PathValueType extends DateSchemaDefinition ? NativeDate + : PathValueType extends UuidSchemaDefinition ? Types.UUID + : PathValueType extends BufferSchemaDefinition ? Buffer + : PathValueType extends BooleanSchemaDefinition ? boolean + : PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId + : PathValueType extends Decimal128SchemaDefinition ? Types.Decimal128 + : PathValueType extends BigintSchemaDefinition ? bigint + : PathValueType extends DoubleSchemaDefinition ? Types.Double + : PathValueType extends MapSchemaDefinition ? Map> + : PathValueType extends UnionSchemaDefinition ? + Options['of'] extends AnyArray ? ResolvePathType + : never + : PathValueType extends ArrayConstructor ? any[] + : PathValueType extends typeof Schema.Types.Mixed ? any + : PathValueType extends ObjectConstructor ? any + : IfEquals extends true ? any + : PathValueType extends typeof SchemaType ? PathValueType['prototype'] + : PathValueType extends Record ? + ObtainDocumentType< + PathValueType, + any, + { + typeKey: TypeKey; + } + > + : unknown + : TDiscriminatorEnumType + : TypeHint; diff --git a/gateway/node_modules/mongoose/types/middlewares.d.ts b/gateway/node_modules/mongoose/types/middlewares.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..1e707c2e11091aa7f2b90535da166810b00eb6d8 --- /dev/null +++ b/gateway/node_modules/mongoose/types/middlewares.d.ts @@ -0,0 +1,66 @@ +declare module 'mongoose' { + import Kareem = require('kareem'); + + type MongooseQueryAndDocumentMiddleware = 'updateOne' | 'deleteOne'; + + type MongooseDistinctDocumentMiddleware = 'save' | 'validate'; + type MongooseDocumentMiddleware = MongooseDistinctDocumentMiddleware | MongooseQueryAndDocumentMiddleware; + + type MongooseRawResultQueryMiddleware = 'findOneAndUpdate' | 'findOneAndReplace' | 'findOneAndDelete'; + type MongooseDistinctQueryMiddleware = 'estimatedDocumentCount' | 'countDocuments' | 'deleteMany' | 'distinct' | 'find' | 'findOne' | 'findOneAndDelete' | 'findOneAndReplace' | 'findOneAndUpdate' | 'replaceOne' | 'updateMany'; + + type MongooseDefaultQueryMiddleware = MongooseDistinctQueryMiddleware | 'updateOne' | 'deleteOne'; + type MongooseQueryMiddleware = MongooseDistinctQueryMiddleware | MongooseQueryAndDocumentMiddleware; + + type MongooseQueryOrDocumentMiddleware = MongooseDistinctQueryMiddleware|MongooseDistinctDocumentMiddleware|MongooseQueryAndDocumentMiddleware; + + type MiddlewareOptions = { + /** + * Enable this Hook for the Document Methods + * @default true + */ + document?: boolean, + /** + * Enable this Hook for the Query Methods + * @default true + */ + query?: boolean, + /** + * Explicitly set this function to be a Error handler instead of based on how many arguments are used + * @default false + */ + errorHandler?: boolean + }; + type SchemaPreOptions = MiddlewareOptions; + type SchemaPostOptions = MiddlewareOptions; + + interface SkipMiddlewareOptions { + /** If `false`, skip pre middleware. */ + pre?: boolean; + /** If `false`, skip post middleware. */ + post?: boolean; + } + + type PreMiddlewareFunction = ( + this: ThisType, + opts?: Record + ) => void | Promise | Kareem.SkipWrappedFunction; + type PreDeleteOneMiddlewareFunction = ( + this: ThisType, + doc: ThisType, + opts?: Record + ) => void | Promise | Kareem.SkipWrappedFunction; + type PreUpdateOneMiddlewareFunction = ( + this: ThisType, + doc: ThisType, + update?: Record, + opts?: Record + ) => void | Promise | Kareem.SkipWrappedFunction; + type PreSaveMiddlewareFunction = ( + this: ThisType, + opts: SaveOptions + ) => void | Promise | Kareem.SkipWrappedFunction; + type PostMiddlewareFunction = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise | Kareem.OverwriteMiddlewareResult; + type ErrorHandlingMiddlewareFunction = (this: ThisType, err: NativeError, res: ResType, next: CallbackWithoutResultAndOptionalError) => void; + type ErrorHandlingMiddlewareWithOption = (this: ThisType, err: NativeError, res: ResType | null, next: CallbackWithoutResultAndOptionalError) => void | Promise | Kareem.OverwriteMiddlewareResult; +} diff --git a/gateway/node_modules/mongoose/types/models.d.ts b/gateway/node_modules/mongoose/types/models.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..5316d319c59314678758e2f2ea5366e5f7647e53 --- /dev/null +++ b/gateway/node_modules/mongoose/types/models.d.ts @@ -0,0 +1,1420 @@ +declare module 'mongoose' { + import mongodb = require('mongodb'); + import { StandardSchemaV1 as StandardSchemaV1Spec, StandardTypedV1 as StandardTypedV1Spec } from '@standard-schema/spec'; + + export interface DiscriminatorOptions { + value?: string | number | ObjectId; + clone?: boolean; + overwriteModels?: boolean; + mergeHooks?: boolean; + mergePlugins?: boolean; + } + + export interface AcceptsDiscriminator { + /** Adds a discriminator type. */ + discriminator( + name: string | number, + schema: Schema, + value?: string | number | ObjectId | DiscriminatorOptions + ): Model; + discriminator( + name: string | number, + schema: Schema, + value?: string | number | ObjectId | DiscriminatorOptions + ): Model; + } + + export type MongooseBulkWriteResult = mongodb.BulkWriteResult & { + mongoose?: { + validationErrors: Error[], + results: Array + } + }; + + export interface MongooseBulkWriteOptions extends mongodb.BulkWriteOptions { + session?: ClientSession; + skipValidation?: boolean; + throwOnValidationError?: boolean; + strict?: boolean | 'throw'; + /** When false, do not add timestamps to documents. Can be overridden at the operation level. */ + timestamps?: boolean; + /** set to `false` to skip all user-defined middleware, or `{ pre: false }` / `{ post: false }` to skip only pre or post hooks */ + middleware?: boolean | SkipMiddlewareOptions; + } + + interface MongooseBulkSaveOptions extends mongodb.BulkWriteOptions { + timestamps?: boolean; + session?: ClientSession; + validateBeforeSave?: boolean; + /** set to `false` to skip all user-defined middleware, or `{ pre: false }` / `{ post: false }` to skip only pre or post hooks */ + middleware?: boolean | SkipMiddlewareOptions; + } + + /** + * @deprecated use AnyBulkWriteOperation instead + */ + interface MongooseBulkWritePerWriteOptions { + timestamps?: boolean; + strict?: boolean | 'throw'; + session?: ClientSession; + skipValidation?: boolean; + } + + interface HydrateOptions { + setters?: boolean; + hydratedPopulatedDocs?: boolean; + virtuals?: boolean; + strict?: boolean | 'throw'; + } + + interface InsertManyOptions extends + PopulateOption, + SessionOption { + limit?: number; + // @deprecated, use includeResultMetadata instead + rawResult?: boolean; + includeResultMetadata?: boolean; + ordered?: boolean; + lean?: boolean; + throwOnValidationError?: boolean; + /** set to `false` to skip all user-defined middleware, or `{ pre: false }` / `{ post: false }` to skip only pre or post hooks */ + middleware?: boolean | SkipMiddlewareOptions; + timestamps?: boolean | QueryTimestampsConfig; + } + + interface InsertManyResult extends mongodb.InsertManyResult { + mongoose?: { validationErrors?: Array }; + } + + type UpdateWriteOpResult = mongodb.UpdateResult; + type UpdateResult = mongodb.UpdateResult; + type DeleteResult = mongodb.DeleteResult; + + interface ModifyResult { + value: Default__v> | null; + /** see https://www.mongodb.com/docs/manual/reference/command/findAndModify/#lasterrorobject */ + lastErrorObject?: { + updatedExisting?: boolean; + upserted?: mongodb.ObjectId; + }; + ok: 0 | 1; + } + + type WriteConcern = mongodb.WriteConcern; + + /** A list of paths to validate. If set, Mongoose will validate only the modified paths that are in the given list. */ + type PathsToValidate = string[] | string; + /** + * @deprecated + */ + type pathsToValidate = PathsToValidate; + + export import StandardTypedV1 = StandardTypedV1Spec; + export import StandardSchemaV1 = StandardSchemaV1Spec; + + interface SaveOptions extends + SessionOption { + checkKeys?: boolean; + j?: boolean; + /** An array of paths that tell mongoose to only validate and save the paths in `pathsToSave`. */ + pathsToSave?: string[]; + safe?: boolean | WriteConcern; + timestamps?: boolean | QueryTimestampsConfig; + validateBeforeSave?: boolean; + validateModifiedOnly?: boolean; + w?: number | string; + wtimeout?: number; + /** set to `false` to skip all user-defined middleware, or `{ pre: false }` / `{ post: false }` to skip only pre or post hooks */ + middleware?: boolean | SkipMiddlewareOptions; + } + + interface CreateOptions extends SaveOptions { + ordered?: boolean; + aggregateErrors?: boolean; + } + + interface RemoveOptions extends SessionOption, Omit {} + + interface MongooseBulkWritePerOperationOptions { + /** Skip validation for this operation. */ + skipValidation?: boolean; + /** When false, do not add timestamps. When true, overrides the `timestamps` option set in the `bulkWrite` options. */ + timestamps?: boolean; + } + + interface MongooseBulkUpdatePerOperationOptions extends MongooseBulkWritePerOperationOptions { + /** When true, allows updating fields that are marked as `immutable` in the schema. */ + overwriteImmutable?: boolean; + /** When false, do not set default values on insert. */ + setDefaultsOnInsert?: boolean; + } + + export type InsertOneModel = + mongodb.InsertOneModel & MongooseBulkWritePerOperationOptions; + + export type ReplaceOneModel = + Omit, 'filter'> & + { filter: QueryFilter } & + MongooseBulkUpdatePerOperationOptions; + + export type UpdateOneModel = + Omit, 'filter'> & + { filter: QueryFilter } & + MongooseBulkUpdatePerOperationOptions; + + export type UpdateManyModel = + Omit, 'filter'> & + { filter: QueryFilter } & + MongooseBulkUpdatePerOperationOptions; + + export type DeleteOneModel = + Omit, 'filter'> & + { filter: QueryFilter }; + + export type DeleteManyModel = + Omit, 'filter'> & + { filter: QueryFilter }; + + export type AnyBulkWriteOperation = + | { insertOne: InsertOneModel } + | { replaceOne: ReplaceOneModel } + | { updateOne: UpdateOneModel } + | { updateMany: UpdateManyModel } + | { deleteOne: DeleteOneModel } + | { deleteMany: DeleteManyModel }; + + const Model: Model; + + /* + * Apply common casting logic to the given type, allowing: + * - strings for ObjectIds + * - strings and numbers for Dates + * - strings for Buffers + * - strings for UUIDs + * - POJOs for subdocuments + * - vanilla arrays of POJOs for document arrays + * - POJOs and array of arrays for maps + */ + type CreateObjectWithExtraKeys = T | (T & Record); + type ApplyBasicCreateCasting = { + [K in keyof T]: NonNullable extends Map + ? (Record | Array<[KeyType, ValueType]> | T[K] | QueryTypeCasting>) + : NonNullable extends Types.DocumentArray + ? RawSubdocType[] | T[K] | QueryTypeCasting> + : NonNullable extends Document + ? CreateObjectWithExtraKeys> | T[K] | QueryTypeCasting> + : NonNullable extends TreatAsPrimitives + ? QueryTypeCasting + : NonNullable extends object + ? CreateObjectWithExtraKeys> | T[K] | QueryTypeCasting> + : QueryTypeCasting; + }; + + type HasLeanOption = 'lean' extends keyof ObtainSchemaGeneric ? + ObtainSchemaGeneric['lean'] extends Record ? + true : + ObtainSchemaGeneric['lean'] : + false; + + /** + * Models are fancy constructors compiled from `Schema` definitions. + * An instance of a model is called a document. + * Models are responsible for creating and reading documents from the underlying MongoDB database + */ + export interface Model< + TRawDocType, + TQueryHelpers = {}, + TInstanceMethods = {}, + TVirtuals = {}, + THydratedDocumentType = HydratedDocument, + TSchema = any, + TLeanResultType = TRawDocType> extends + NodeJS.EventEmitter, + IndexManager, + SessionStarter { + new >(doc?: DocType, fields?: any | null, options?: AnyObject): THydratedDocumentType; + + aggregate(pipeline?: PipelineStage[], options?: AggregateOptions): Aggregate>; + aggregate(pipeline: PipelineStage[]): Aggregate>; + + /** Base Mongoose instance the model uses. */ + base: Mongoose; + + /** Standard Schema adapter for validating input with this model's schema. */ + readonly '~standard': StandardSchemaV1.Props< + Default__v< + Default_id>, + ObtainSchemaGeneric + > + >; + + /** + * If this is a discriminator model, `baseModelName` is the name of + * the base model. + */ + baseModelName: string | undefined; + + /* Cast the given POJO to the model's schema */ + castObject(obj: AnyObject, options?: { ignoreCastErrors?: boolean }): TRawDocType; + + /* Apply defaults to the given document or POJO. */ + applyDefaults(obj: AnyObject): AnyObject; + applyDefaults(obj: TRawDocType): TRawDocType; + + /* Apply virtuals to the given POJO. */ + applyVirtuals(obj: AnyObject, virtalsToApply?: string[]): AnyObject; + + /** + * Apply this model's timestamps to a given POJO, including subdocument timestamps + */ + applyTimestamps(obj: AnyObject, options?: { isUpdate?: boolean, currentTime?: () => Date }): AnyObject; + + /** + * Sends multiple `insertOne`, `updateOne`, `updateMany`, `replaceOne`, + * `deleteOne`, and/or `deleteMany` operations to the MongoDB server in one + * command. This is faster than sending multiple independent operations (e.g. + * if you use `create()`) because with `bulkWrite()` there is only one network + * round trip to the MongoDB server. + */ + bulkWrite( + writes: Array>, + options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions & { ordered: false } + ): Promise; + bulkWrite( + writes: Array>, + options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions + ): Promise; + + /** + * Sends multiple `save()` calls in a single `bulkWrite()`. This is faster than + * sending multiple `save()` calls because with `bulkSave()` there is only one + * network round trip to the MongoDB server. + */ + bulkSave(documents: Array, options?: MongooseBulkSaveOptions): Promise; + + /** Collection the model uses. */ + collection: Collection; + + /** Creates a `countDocuments` query: counts the number of documents that match `filter`. */ + countDocuments( + filter?: QueryFilter, + options?: (mongodb.CountOptions & MongooseBaseQueryOptions & mongodb.Abortable) | null + ): QueryWithHelpers< + number, + THydratedDocumentType, + TQueryHelpers, + TRawDocType, + 'countDocuments', + TInstanceMethods & TVirtuals + >; + countDocuments( + filter?: Query, + options?: (mongodb.CountOptions & MongooseBaseQueryOptions & mongodb.Abortable) | null + ): QueryWithHelpers< + number, + THydratedDocumentType, + TQueryHelpers, + TRawDocType, + 'countDocuments', + TInstanceMethods & TVirtuals + >; + + /** Creates a new document or documents */ + create(): Promise; + create(doc: Partial): Promise; + create(docs: Array>): Promise; + create(docs: Array>>>, options: CreateOptions & { aggregateErrors: true }): Promise<(THydratedDocumentType | Error)[]>; + create(docs: Array>>>, options?: CreateOptions): Promise; + create(doc: DeepPartial>>): Promise; + create(...docs: Array>>>): Promise; + + /** + * Create the collection for this model. By default, if no indexes are specified, + * mongoose will not create the collection for the model until any documents are + * created. Use this method to create the collection explicitly. + */ + createCollection(options?: mongodb.CreateCollectionOptions & Pick & { middleware?: boolean | SkipMiddlewareOptions }): Promise>; + + /** + * Create an [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/). + * This function only works when connected to MongoDB Atlas. + */ + createSearchIndex(description: SearchIndexDescription): Promise; + + /** + * Creates all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) defined in this model's schema. + * This function only works when connected to MongoDB Atlas. + */ + createSearchIndexes(): Promise; + + /** Connection the model uses. */ + db: Connection; + + /** + * Deletes all of the documents that match `conditions` from the collection. + * Behaves like `remove()`, but deletes all documents that match `conditions` + * regardless of the `single` option. + */ + deleteMany( + filter?: QueryFilter, + options?: (mongodb.DeleteOptions & MongooseBaseQueryOptions) | null + ): QueryWithHelpers< + mongodb.DeleteResult, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'deleteMany', + TInstanceMethods & TVirtuals + >; + deleteMany( + filter?: Query, + options?: (mongodb.DeleteOptions & MongooseBaseQueryOptions) | null + ): QueryWithHelpers< + mongodb.DeleteResult, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'deleteMany', + TInstanceMethods & TVirtuals + >; + + /** + * Deletes the first document that matches `conditions` from the collection. + * Behaves like `remove()`, but deletes at most one document regardless of the + * `single` option. + */ + deleteOne( + filter?: QueryFilter, + options?: (mongodb.DeleteOptions & MongooseBaseQueryOptions) | null + ): QueryWithHelpers< + mongodb.DeleteResult, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'deleteOne', + TInstanceMethods & TVirtuals + >; + deleteOne( + filter?: Query, + options?: (mongodb.DeleteOptions & MongooseBaseQueryOptions) | null + ): QueryWithHelpers< + mongodb.DeleteResult, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'deleteOne', + TInstanceMethods & TVirtuals + >; + + /** Adds a discriminator type. */ + discriminator>( + name: string | number, + schema: TDiscriminatorSchema, + value?: string | number | ObjectId | DiscriminatorOptions + ): Model< + TRawDocType & InferSchemaType, + TQueryHelpers & ObtainSchemaGeneric, + TInstanceMethods & ObtainSchemaGeneric, + TVirtuals & ObtainSchemaGeneric + > & ObtainSchemaGeneric & ObtainSchemaGeneric; + discriminator( + name: string | number, + schema: Schema, + value?: string | number | ObjectId | DiscriminatorOptions + ): Model; + discriminator( + name: string | number, + schema: Schema, + value?: string | number | ObjectId | DiscriminatorOptions + ): U; + + /** + * Delete an existing [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) by name. + * This function only works when connected to MongoDB Atlas. + */ + dropSearchIndex(name: string): Promise; + + /** + * Event emitter that reports any errors that occurred. Useful for global error + * handling. + */ + events: NodeJS.EventEmitter; + + /** + * Finds a single document by its _id field. `findById(id)` is almost* + * equivalent to `findOne({ _id: id })`. If you want to query by a document's + * `_id`, use `findById()` instead of `findOne()`. + */ + findById( + id: any, + projection: ProjectionType | null | undefined, + options: QueryOptions & { lean: true } + ): QueryWithHelpers< + TLeanResultType | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOne', + TInstanceMethods & TVirtuals + >; + findById( + id: any, + projection: ProjectionType | null | undefined, + options: QueryOptions & { lean: false } + ): QueryWithHelpers< + ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOne', + TInstanceMethods & TVirtuals + >; + findById( + id?: any, + projection?: ProjectionType | null | undefined, + options?: QueryOptions | null + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType | null : ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOne', + TInstanceMethods & TVirtuals + >; + + /** Finds one document. */ + findOne( + filter: QueryFilter, + projection: ProjectionType | null | undefined, + options: QueryOptions & { lean: true } & mongodb.Abortable + ): QueryWithHelpers< + TLeanResultType | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOne', + TInstanceMethods & TVirtuals + >; + findOne( + filter: Query, + projection: ProjectionType | null | undefined, + options: QueryOptions & { lean: true } & mongodb.Abortable + ): QueryWithHelpers< + TLeanResultType | null, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOne', + TInstanceMethods & TVirtuals + >; + findOne( + filter: QueryFilter, + projection: ProjectionType | null | undefined, + options: QueryOptions & { lean: false } & mongodb.Abortable + ): QueryWithHelpers< + ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOne', + TInstanceMethods & TVirtuals + >; + findOne( + filter?: QueryFilter, + projection?: ProjectionType | null | undefined, + options?: QueryOptions & mongodb.Abortable | null | undefined + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType | null : ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOne', + TInstanceMethods & TVirtuals + >; + findOne( + filter?: Query, + projection?: ProjectionType | null | undefined, + options?: QueryOptions & mongodb.Abortable | null | undefined + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType | null : THydratedDocumentType | null, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOne', + TInstanceMethods & TVirtuals + >; + + /** + * Shortcut for creating a new Document from existing raw data, pre-saved in the DB. + * The document returned has no paths marked as modified initially. + * With `strict: false`, fields not in the schema are kept on the document; pass + * `ExtraFields` to describe their types, e.g. + * `Model.hydrate<{ totalOrders: number }>(obj, null, { strict: false })`. + */ + hydrate( + obj: any, + projection: ProjectionType | null | undefined, + options: HydrateOptions & { strict: false } + ): THydratedDocumentType & ExtraFields; + hydrate(obj: any, projection?: ProjectionType | null | undefined, options?: HydrateOptions): THydratedDocumentType; + + /** + * This function is responsible for building [indexes](https://www.mongodb.com/docs/manual/indexes/), + * unless [`autoIndex`](http://mongoosejs.com/docs/guide.html#autoIndex) is turned off. + * Mongoose calls this function automatically when a model is created using + * [`mongoose.model()`](/docs/api/mongoose.html#mongoose_Mongoose-model) or + * [`connection.model()`](/docs/api/connection.html#connection_Connection-model), so you + * don't need to call it. + */ + init(): Promise; + + /** Inserts one or more new documents as a single `insertMany` call to the MongoDB server. */ + insertMany( + docs: Array + ): Promise>; + insertMany( + doc: Array, + options: InsertManyOptions & { ordered: false; rawResult: true; } + ): Promise> & { + mongoose: { + validationErrors: (CastError | Error.ValidatorError)[]; + results: Array< + Error | + Object | + THydratedDocumentType + > + } + }>; + insertMany( + docs: Array, + options: InsertManyOptions & { lean: true, rawResult: true; } + ): Promise>>; + insertMany( + doc: DocContents | TRawDocType, + options: InsertManyOptions & { ordered: false; rawResult: true; } + ): Promise> & { + mongoose: { + validationErrors: (CastError | Error.ValidatorError)[]; + results: Array< + Error | + Object | + MergeType + > + } + }>; + insertMany( + docs: Array, + options: InsertManyOptions & { lean: true; } + ): Promise>>; + insertMany( + docs: Array, + options: InsertManyOptions & { rawResult: true; } + ): Promise>>; + insertMany( + docs: Array, + options: InsertManyOptions & { lean: true; } + ): Promise>>; + insertMany( + docs: Array, + options: InsertManyOptions & { rawResult: true; } + ): Promise>>; + insertMany( + doc: DocContents, + options: InsertManyOptions & { lean: true; } + ): Promise>>; + insertMany( + doc: DocContents, + options: InsertManyOptions & { rawResult: true; } + ): Promise>>; + insertMany( + doc: Array, + options: InsertManyOptions + ): Promise>; + insertMany( + docs: Array + ): Promise>>>; + insertMany( + doc: DocContents, + options: InsertManyOptions + ): Promise>>>; + insertMany( + docs: Array, + options: InsertManyOptions + ): Promise>>>; + insertMany( + doc: DocContents + ): Promise< + Array>> + >; + + /** + * Shortcut for saving one document to the database. + * `MyModel.insertOne(obj, options)` is almost equivalent to `new MyModel(obj).save(options)`. + * The difference is that `insertOne()` checks if `obj` is already a document, and checks for discriminators. + */ + insertOne(doc: Partial>, options?: SaveOptions): Promise; + + /** + * List all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) on this model's collection. + * This function only works when connected to MongoDB Atlas. + */ + listSearchIndexes(options?: mongodb.ListSearchIndexesOptions): Promise>; + + /** The name of the model */ + modelName: string; + + /** Populates document references. */ + populate( + docs: Array, + options: PopulateOptions | Array | string + ): Promise>; + populate( + doc: any, options: PopulateOptions | Array | string + ): Promise; + populate( + docs: Array, + options: PopulateOptions | Array | string + ): Promise, TRawDocType>>>; + populate( + doc: any, options: PopulateOptions | Array | string + ): Promise, TRawDocType>>; + + /** + * Update an existing [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/). + * This function only works when connected to MongoDB Atlas. + */ + updateSearchIndex(name: string, definition: AnyObject): Promise; + + /** + * Changes the Connection instance this model uses to make requests to MongoDB. + * This function is most useful for changing the Connection that a Model defined using `mongoose.model()` uses + * after initialization. + */ + useConnection(connection: Connection): this; + + /** Casts and validates the given object against this model's schema, returning the casted-and-validated copy of `obj`, passing the given `context` to custom validators. */ + validate(): Promise; + validate(obj: any): Promise; + validate(obj: any, pathsOrOptions: PathsToValidate): Promise; + validate(obj: any, pathsOrOptions: { pathsToSkip?: pathsToSkip }): Promise; + + /** Watches the underlying collection for changes using [MongoDB change streams](https://www.mongodb.com/docs/manual/changeStreams/). */ + watch(pipeline?: Array>, options?: mongodb.ChangeStreamOptions & { hydrate?: boolean }): mongodb.ChangeStream; + + /** Adds a `$where` clause to this query */ + $where(argument: string | Function): QueryWithHelpers, THydratedDocumentType, TQueryHelpers, TRawDocType, 'find', TInstanceMethods & TVirtuals>; + + /** Registered discriminators for this model. */ + discriminators: { [name: string]: Model } | undefined; + + /** Translate any aliases fields/conditions so the final query or document object is pure */ + translateAliases(raw: any): any; + + /** Creates a `distinct` query: returns the distinct values of the given `field` that match `filter`. */ + distinct( + field: DocKey, + filter?: QueryFilter, + options?: QueryOptions + ): QueryWithHelpers< + Array< + DocKey extends keyof WithLevel1NestedPaths + ? WithoutUndefined[DocKey]>> + : unknown + >, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'distinct', + TInstanceMethods & TVirtuals + >; + distinct( + field: DocKey, + filter?: Query, + options?: QueryOptions + ): QueryWithHelpers< + Array< + DocKey extends keyof WithLevel1NestedPaths + ? WithoutUndefined[DocKey]>> + : unknown + >, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'distinct', + TInstanceMethods & TVirtuals + >; + + /** Creates a `estimatedDocumentCount` query: counts the number of documents in the collection. */ + estimatedDocumentCount(options?: QueryOptions): QueryWithHelpers< + number, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'estimatedDocumentCount', + TInstanceMethods & TVirtuals + >; + + /** + * Returns a document with its `_id` if at least one document exists in the database that matches + * the given `filter`, and `null` otherwise. + */ + exists( + filter: QueryFilter + ): QueryWithHelpers< + { _id: InferId } | null, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOne', + TInstanceMethods & TVirtuals + >; + exists( + filter: Query + ): QueryWithHelpers< + { _id: InferId } | null, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOne', + TInstanceMethods & TVirtuals + >; + + /** Creates a `find` query: gets a list of documents that match `filter`. */ + find( + filter: QueryFilter, + projection: ProjectionType | null | undefined, + options: QueryOptions & { lean: true } & mongodb.Abortable + ): QueryWithHelpers< + GetLeanResultType, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'find', + TInstanceMethods & TVirtuals + >; + find( + filter: Query, + projection: ProjectionType | null | undefined, + options: QueryOptions & { lean: true } & mongodb.Abortable + ): QueryWithHelpers< + GetLeanResultType, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'find', + TInstanceMethods & TVirtuals + >; + find( + filter: QueryFilter, + projection: ProjectionType | null | undefined, + options: QueryOptions & { lean: false } & mongodb.Abortable + ): QueryWithHelpers< + ResultDoc[], + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'find', + TInstanceMethods & TVirtuals + >; + find( + filter?: QueryFilter, + projection?: ProjectionType | null | undefined, + options?: QueryOptions & mongodb.Abortable + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType[] : ResultDoc[], + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'find', + TInstanceMethods & TVirtuals + >; + find( + filter?: Query, + projection?: ProjectionType | null | undefined, + options?: QueryOptions & mongodb.Abortable + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType[] : THydratedDocumentType[], + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'find', + TInstanceMethods & TVirtuals + >; + + /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */ + findByIdAndDelete( + id: mongodb.ObjectId | any, + options: QueryOptions & { includeResultMetadata: true, lean: true } + ): QueryWithHelpers< + ModifyResult, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + findByIdAndDelete( + id: mongodb.ObjectId | any, + options: QueryOptions & { lean: true } + ): QueryWithHelpers< + TLeanResultType | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + findByIdAndDelete( + id: mongodb.ObjectId | any, + options: QueryOptions & { lean: false } + ): QueryWithHelpers< + ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + findByIdAndDelete( + id: mongodb.ObjectId | any, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers< + HasLeanOption extends true ? ModifyResult : ModifyResult, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + findByIdAndDelete( + id?: mongodb.ObjectId | any, + options?: QueryOptions | null + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType | null : ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + + + /** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */ + findByIdAndUpdate( + filter: QueryFilter, + update: UpdateQuery, + options: QueryOptions & { includeResultMetadata: true, lean: true } + ): QueryWithHelpers< + ModifyResult, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findByIdAndUpdate( + filter: Query, + update: UpdateQuery, + options: QueryOptions & { includeResultMetadata: true, lean: true } + ): QueryWithHelpers< + ModifyResult, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findByIdAndUpdate( + id: mongodb.ObjectId | any, + update: UpdateQuery, + options: QueryOptions & { lean: true } + ): QueryWithHelpers< + TLeanResultType | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findByIdAndUpdate( + id: mongodb.ObjectId | any, + update: UpdateQuery, + options: QueryOptions & { lean: false } + ): QueryWithHelpers< + ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findByIdAndUpdate( + id: mongodb.ObjectId | any, + update: UpdateQuery, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers< + HasLeanOption extends true ? ModifyResult : ModifyResult, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findByIdAndUpdate( + id: mongodb.ObjectId | any, + update: UpdateQuery, + options: QueryOptions & { upsert: true } & ReturnsNewDoc + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType : ResultDoc, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findByIdAndUpdate( + id?: mongodb.ObjectId | any, + update?: UpdateQuery, + options?: QueryOptions | null + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType | null : ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + + /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */ + findOneAndDelete( + filter: QueryFilter, + options: QueryOptions & { lean: true } + ): QueryWithHelpers< + TLeanResultType | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + findOneAndDelete( + filter: Query, + options: QueryOptions & { lean: true } + ): QueryWithHelpers< + TLeanResultType | null, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + findOneAndDelete( + filter: QueryFilter, + options: QueryOptions & { lean: false } + ): QueryWithHelpers< + ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + findOneAndDelete( + filter: Query, + options: QueryOptions & { lean: false } + ): QueryWithHelpers< + THydratedDocumentType | null, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + findOneAndDelete( + filter: QueryFilter, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers< + HasLeanOption extends true ? ModifyResult : ModifyResult, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + findOneAndDelete( + filter: Query, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers< + HasLeanOption extends true ? ModifyResult : ModifyResult, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + findOneAndDelete( + filter?: QueryFilter | null, + options?: QueryOptions | null + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType | null : ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + findOneAndDelete( + filter?: Query | null, + options?: QueryOptions | null + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType | null : THydratedDocumentType | null, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndDelete', + TInstanceMethods & TVirtuals + >; + + /** Creates a `findOneAndReplace` query: atomically finds the given document and replaces it with `replacement`. */ + findOneAndReplace( + filter: QueryFilter, + replacement: TRawDocType | AnyObject, + options: QueryOptions & { lean: true } + ): QueryWithHelpers< + TLeanResultType | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndReplace', + TInstanceMethods & TVirtuals + >; + findOneAndReplace( + filter: Query, + replacement: TRawDocType | AnyObject, + options: QueryOptions & { lean: true } + ): QueryWithHelpers< + TLeanResultType | null, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndReplace', + TInstanceMethods & TVirtuals + >; + findOneAndReplace( + filter: QueryFilter, + replacement: TRawDocType | AnyObject, + options: QueryOptions & { lean: false } + ): QueryWithHelpers< + ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndReplace', + TInstanceMethods & TVirtuals + >; + findOneAndReplace( + filter: Query, + replacement: TRawDocType | AnyObject, + options: QueryOptions & { lean: false } + ): QueryWithHelpers< + THydratedDocumentType | null, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndReplace', + TInstanceMethods & TVirtuals + >; + findOneAndReplace( + filter: QueryFilter, + replacement: TRawDocType | AnyObject, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers< + HasLeanOption extends true ? ModifyResult : ModifyResult, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndReplace', + TInstanceMethods & TVirtuals + >; + findOneAndReplace( + filter: Query, + replacement: TRawDocType | AnyObject, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers< + HasLeanOption extends true ? ModifyResult : ModifyResult, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndReplace', + TInstanceMethods & TVirtuals + >; + findOneAndReplace( + filter: QueryFilter, + replacement: TRawDocType | AnyObject, + options: QueryOptions & { upsert: true } & ReturnsNewDoc + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType : ResultDoc, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndReplace', + TInstanceMethods & TVirtuals + >; + findOneAndReplace( + filter: Query, + replacement: TRawDocType | AnyObject, + options: QueryOptions & { upsert: true } & ReturnsNewDoc + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType : THydratedDocumentType, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndReplace', + TInstanceMethods & TVirtuals + >; + findOneAndReplace( + filter?: QueryFilter, + replacement?: TRawDocType | AnyObject, + options?: QueryOptions | null + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType | null : ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndReplace', + TInstanceMethods & TVirtuals + >; + findOneAndReplace( + filter?: Query, + replacement?: TRawDocType | AnyObject, + options?: QueryOptions | null + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType | null : THydratedDocumentType | null, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndReplace', + TInstanceMethods & TVirtuals + >; + + /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */ + findOneAndUpdate( + filter: QueryFilter, + update: UpdateQuery, + options: QueryOptions & { includeResultMetadata: true, lean: true } + ): QueryWithHelpers< + ModifyResult, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findOneAndUpdate( + filter: Query, + update: UpdateQuery, + options: QueryOptions & { includeResultMetadata: true, lean: true } + ): QueryWithHelpers< + ModifyResult, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findOneAndUpdate( + filter: QueryFilter, + update: UpdateQuery, + options: QueryOptions & { lean: true } + ): QueryWithHelpers< + GetLeanResultType | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findOneAndUpdate( + filter: Query, + update: UpdateQuery, + options: QueryOptions & { lean: true } + ): QueryWithHelpers< + GetLeanResultType | null, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findOneAndUpdate( + filter: QueryFilter, + update: UpdateQuery, + options: QueryOptions & { lean: false } + ): QueryWithHelpers< + ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findOneAndUpdate( + filter: Query, + update: UpdateQuery, + options: QueryOptions & { lean: false } + ): QueryWithHelpers< + THydratedDocumentType | null, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findOneAndUpdate( + filter: QueryFilter, + update: UpdateQuery, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers< + HasLeanOption extends true ? ModifyResult : ModifyResult, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findOneAndUpdate( + filter: Query, + update: UpdateQuery, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers< + HasLeanOption extends true ? ModifyResult : ModifyResult, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findOneAndUpdate( + filter: QueryFilter, + update: UpdateQuery, + options: QueryOptions & { upsert: true } & ReturnsNewDoc + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType : ResultDoc, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findOneAndUpdate( + filter: Query, + update: UpdateQuery, + options: QueryOptions & { upsert: true } & ReturnsNewDoc + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType : THydratedDocumentType, + THydratedDocumentType, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findOneAndUpdate( + filter?: QueryFilter, + update?: UpdateQuery, + options?: QueryOptions | null + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType | null : ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + findOneAndUpdate( + filter?: Query, + update?: UpdateQuery, + options?: QueryOptions | null + ): QueryWithHelpers< + HasLeanOption extends true ? TLeanResultType | null : ResultDoc | null, + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'findOneAndUpdate', + TInstanceMethods & TVirtuals + >; + + /** Creates a `replaceOne` query: finds the first document that matches `filter` and replaces it with `replacement`. */ + replaceOne( + filter?: QueryFilter, + replacement?: TRawDocType | AnyObject, + options?: (mongodb.ReplaceOptions & QueryOptions) | null + ): QueryWithHelpers; + replaceOne( + filter?: Query, + replacement?: TRawDocType | AnyObject, + options?: (mongodb.ReplaceOptions & QueryOptions) | null + ): QueryWithHelpers; + + /** Apply changes made to this model's schema after this model was compiled. */ + recompileSchema(): void; + + /** Schema the model uses. */ + schema: IfAny< + TSchema, + Schema, TInstanceMethods, TQueryHelpers, TVirtuals>, + TSchema + >; + + /** Creates a `updateMany` query: updates all documents that match `filter` with `update`. */ + updateMany( + filter: QueryFilter, + update: UpdateQuery | UpdateWithAggregationPipeline, + options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions) | null + ): QueryWithHelpers; + updateMany( + filter: Query, + update: UpdateQuery | UpdateWithAggregationPipeline, + options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions) | null + ): QueryWithHelpers; + + /** Creates a `updateOne` query: updates the first document that matches `filter` with `update`. */ + updateOne( + filter: QueryFilter, + update: UpdateQuery | UpdateWithAggregationPipeline, + options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions) | null + ): QueryWithHelpers; + updateOne( + filter: Query, + update: UpdateQuery | UpdateWithAggregationPipeline, + options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions) | null + ): QueryWithHelpers; + + /** Creates a Query, applies the passed conditions, and returns the Query. */ + where( + path: string, + val?: any + ): QueryWithHelpers extends true ? TRawDocType[] : ResultDoc[], ResultDoc, TQueryHelpers, TRawDocType, 'find', TInstanceMethods>; + where(obj: object): QueryWithHelpers< + HasLeanOption extends true ? TRawDocType[] : ResultDoc[], + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'find', + TInstanceMethods & TVirtuals + >; + where(): QueryWithHelpers< + HasLeanOption extends true ? TRawDocType[] : ResultDoc[], + ResultDoc, + TQueryHelpers, + TLeanResultType, + 'find', + TInstanceMethods & TVirtuals + >; + + /** + * If auto encryption is enabled, returns a ClientEncryption instance that is configured with the same settings that + * Mongoose's underlying MongoClient is using. If the client has not yet been configured, returns null. + */ + clientEncryption(): mongodb.ClientEncryption | null; + } +} diff --git a/gateway/node_modules/mongoose/types/mongooseoptions.d.ts b/gateway/node_modules/mongoose/types/mongooseoptions.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..b7c0bff83025d58b7d22a1e77aa6d6c6e2f5cce7 --- /dev/null +++ b/gateway/node_modules/mongoose/types/mongooseoptions.d.ts @@ -0,0 +1,240 @@ +declare module 'mongoose' { + import stream = require('stream'); + + interface MongooseOptions { + /** + * Set to `true` to set `allowDiskUse` to true to all aggregation operations by default. + * + * @default false + */ + allowDiskUse?: boolean; + /** + * Set to `false` to skip applying global plugins to child schemas. + * + * @default true + */ + applyPluginsToChildSchemas?: boolean; + + /** + * Set to `true` to apply global plugins to discriminator schemas. + * This typically isn't necessary because plugins are applied to the base schema and + * discriminators copy all middleware, methods, statics, and properties from the base schema. + * + * @default false + */ + applyPluginsToDiscriminators?: boolean; + + /** + * autoCreate is `true` by default unless readPreference is secondary or secondaryPreferred, + * which means Mongoose will attempt to create every model's underlying collection before + * creating indexes. If readPreference is secondary or secondaryPreferred, Mongoose will + * default to false for both autoCreate and autoIndex because both createCollection() and + * createIndex() will fail when connected to a secondary. + */ + autoCreate?: boolean; + + /** + * Set to `false` to disable automatic index creation for all models associated with this Mongoose instance. + * + * @default true + */ + autoIndex?: boolean; + + /** + * enable/disable mongoose's buffering mechanism for all connections and models. + * + * @default true + */ + bufferCommands?: boolean; + + /** + * If bufferCommands is on, this option sets the maximum amount of time Mongoose + * buffering will wait before throwing an error. + * If not specified, Mongoose will use 10000 (10 seconds). + * + * @default 10000 + */ + bufferTimeoutMS?: number; + + /** + * Set to `true` to `clone()` all schemas before compiling into a model. + * + * @default false + */ + cloneSchemas?: boolean; + + /** + * Set to `false` to disable the creation of the initial default connection. + * + * @default true + */ + createInitialConnection?: boolean; + + /** + * If `true`, prints the operations mongoose sends to MongoDB to the console. + * If a writable stream is passed, it will log to that stream, without colorization. + * If a callback function is passed, it will receive the collection name, the method + * name, then all arguments passed to the method. For example, if you wanted to + * replicate the default logging, you could output from the callback + * `Mongoose: ${collectionName}.${methodName}(${methodArgs.join(', ')})`. + * + * @default false + */ + debug?: + | boolean + | { color?: boolean; shell?: boolean; timestamp?: boolean; } + | stream.Writable + | ((collectionName: string, methodName: string, ...methodArgs: any[]) => void); + + /** + * If `true`, adds a `id` virtual to all schemas unless overwritten on a per-schema basis. + * @defaultValue true + */ + id?: boolean; + + /** + * If `false`, it will change the `createdAt` field to be [`immutable: false`](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-immutable) + * which means you can update the `createdAt`. + * + * @default true + */ + 'timestamps.createdAt.immutable'?: boolean + + /** If set, attaches [maxTimeMS](https://www.mongodb.com/docs/manual/reference/operator/meta/maxTimeMS/) to every query */ + maxTimeMS?: number; + + /** + * Mongoose adds a getter to MongoDB ObjectId's called `_id` that + * returns `this` for convenience with populate. Set this to false to remove the getter. + * + * @default true + */ + objectIdGetter?: boolean; + + /** + * Set to `true` to default to overwriting models with the same name when calling + * `mongoose.model()`, as opposed to throwing an `OverwriteModelError`. + * + * @default false + */ + overwriteModels?: boolean; + + /** + * Set the default value for the `returnDocument` option to `findOneAndUpdate()`, + * `findByIdAndUpdate()`, and `findOneAndReplace()`. + * - `'before'`: Return the document before the update was applied. + * - `'after'`: Return the document after the update was applied. + * + * @default 'before' + */ + returnDocument?: 'before' | 'after'; + + /** + * @deprecated Use `returnDocument` instead. + */ + returnOriginal?: boolean; + + /** + * Set to true to enable [update validators]( + * https://mongoosejs.com/docs/validation.html#update-validators + * ) for all validators by default. + * + * @default false + */ + runValidators?: boolean; + + /** + * Sanitizes query filters against [query selector injection attacks]( + * https://thecodebarbarian.com/2014/09/04/defending-against-query-selector-injection-attacks.html + * ) by wrapping any nested objects that have a property whose name starts with $ in a $eq. + */ + sanitizeFilter?: boolean; + + sanitizeProjection?: boolean; + + /** + * Set to false to opt out of Mongoose adding all fields that you `populate()` + * to your `select()`. The schema-level option `selectPopulatedPaths` overwrites this one. + * + * @default true + */ + selectPopulatedPaths?: boolean; + + /** + * Mongoose also sets defaults on update() and findOneAndUpdate() when the upsert option is + * set by adding your schema's defaults to a MongoDB $setOnInsert operator. You can disable + * this behavior by setting the setDefaultsOnInsert option to false. + * + * @default true + */ + setDefaultsOnInsert?: boolean; + + /** + * Sets the default strict mode for schemas. + * May be `false`, `true`, or `'throw'`. + * + * @default true + */ + strict?: boolean | 'throw'; + + /** + * Set to `false` to allow populating paths that aren't in the schema. + * + * @default true + */ + strictPopulate?: boolean; + + /** + * Sets the default [strictQuery](https://mongoosejs.com/docs/guide.html#strictQuery) mode for schemas. + * May be `false`, `true`, or `'throw'`. + * + * @default false + */ + strictQuery?: boolean | 'throw'; + + /** + * Sets the default [strictRead](https://mongoosejs.com/docs/guide.html#strictRead) mode for schemas. + * May be `false`, `true`, or `'throw'`. + * + * @default false + */ + strictRead?: boolean | 'throw'; + + /** + * Overwrites default objects to `toJSON()`, for determining how Mongoose + * documents get serialized by `JSON.stringify()` + * + * @default { transform: true, flattenDecimals: true } + */ + toJSON?: ToObjectOptions; + + /** + * Overwrites default objects to `toObject()` + * + * @default { transform: true, flattenDecimals: true } + */ + toObject?: ToObjectOptions; + + /** + * Set to true to make Mongoose use Node.js' built-in AsyncLocalStorage (Node >= 16.0.0) + * to set `session` option on all operations within a `connection.transaction(fn)` call + * by default. Defaults to false. + */ + transactionAsyncLocalStorage?: boolean; + + /** + * If `true`, convert any aliases in filter, projection, update, and distinct + * to their database property names. Defaults to false. + */ + translateAliases?: boolean; + + /** + * If `true`, allows passing update pipelines (arrays) to update operations by default + * without explicitly setting `updatePipeline: true` in each query. This is the global + * default for the `updatePipeline` query option. + * + * @default false + */ + updatePipeline?: boolean; + } +} diff --git a/gateway/node_modules/mongoose/types/pipelinestage.d.ts b/gateway/node_modules/mongoose/types/pipelinestage.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..a998ee095f0067ff6efee307890f2376f10d955b --- /dev/null +++ b/gateway/node_modules/mongoose/types/pipelinestage.d.ts @@ -0,0 +1,334 @@ +declare module 'mongoose' { + /** + * [Stages reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/#aggregation-pipeline-stages) + */ + export type PipelineStage = + | PipelineStage.AddFields + | PipelineStage.Bucket + | PipelineStage.BucketAuto + | PipelineStage.CollStats + | PipelineStage.Count + | PipelineStage.Densify + | PipelineStage.Documents + | PipelineStage.Facet + | PipelineStage.Fill + | PipelineStage.GeoNear + | PipelineStage.GraphLookup + | PipelineStage.Group + | PipelineStage.IndexStats + | PipelineStage.Limit + | PipelineStage.ListSessions + | PipelineStage.Lookup + | PipelineStage.Match + | PipelineStage.Merge + | PipelineStage.Out + | PipelineStage.PlanCacheStats + | PipelineStage.Project + | PipelineStage.Redact + | PipelineStage.ReplaceRoot + | PipelineStage.ReplaceWith + | PipelineStage.Sample + | PipelineStage.Search + | PipelineStage.SearchMeta + | PipelineStage.Set + | PipelineStage.SetWindowFields + | PipelineStage.Skip + | PipelineStage.Sort + | PipelineStage.SortByCount + | PipelineStage.UnionWith + | PipelineStage.Unset + | PipelineStage.Unwind + | PipelineStage.VectorSearch; + + export namespace PipelineStage { + export interface AddFields { + /** [`$addFields` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/addFields/) */ + $addFields: Record + } + + export interface Bucket { + /** [`$bucket` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/bucket/) */ + $bucket: { + groupBy: Expression; + boundaries: any[]; + default?: any + output?: Record + } + } + + export interface BucketAuto { + /** [`$bucketAuto` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/bucketAuto/) */ + $bucketAuto: { + groupBy: Expression | Record; + buckets: number; + output?: Record; + granularity?: 'R5' | 'R10' | 'R20' | 'R40' | 'R80' | '1-2-5' | 'E6' | 'E12' | 'E24' | 'E48' | 'E96' | 'E192' | 'POWERSOF2'; + } + } + + export interface CollStats { + /** [`$collStats` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/collStats/) */ + $collStats: { + latencyStats?: { histograms?: boolean }; + storageStats?: { scale?: number }; + count?: Record; + queryExecStats?: Record; + } + } + + export interface Count { + /** [`$count` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/count/) */ + $count: string; + } + + export interface Densify{ + /** [`$densify` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/densify/) */ + $densify: { + field: string, + partitionByFields?: string[], + range: { + step: number, + unit?: 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year', + bounds: number[] | globalThis.Date[] | 'full' | 'partition' + } + } + } + + export interface Documents { + /** [`$documents` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/documents/) */ + $documents: Record[] + } + + export interface Fill { + /** [`$fill` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/fill/) */ + $fill: { + partitionBy?: Expression, + partitionByFields?: string[], + sortBy?: Record, + output: Record + } + } + + export interface Facet { + /** [`$facet` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/facet/) */ + $facet: Record; + } + + export type FacetPipelineStage = Exclude; + export type UnionWithPipelineStage = Exclude; + + export interface GeoNear { + /** [`$geoNear` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/geoNear/) */ + $geoNear: { + near: { type: 'Point'; coordinates: [number, number] } | [number, number]; + distanceField: string; + distanceMultiplier?: number; + includeLocs?: string; + key?: string; + maxDistance?: number; + minDistance?: number; + query?: AnyObject; + spherical?: boolean; + /** + * Deprecated. Use only with MondoDB below 4.2 (removed in 4.2) + * @deprecated + */ + num?: number; + } + } + + export interface GraphLookup { + /** [`$graphLookup` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/graphLookup/) */ + $graphLookup: { + from: string; + startWith: any + connectFromField: string; + connectToField: string; + as: string; + maxDepth?: number; + depthField?: string; + restrictSearchWithMatch?: AnyObject; + } + } + + export interface Group { + /** [`$group` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group) */ + $group: { _id: any } | { [key: string]: AccumulatorOperator } + } + + export interface IndexStats { + /** [`$indexStats` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexStats/) */ + $indexStats: Record; + } + + export interface Limit { + /** [`$limit` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/limit/) */ + $limit: number + } + + export interface ListSessions { + /** [`$listSessions` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/listSessions/) */ + $listSessions: { users?: { user: string; db: string }[] } | { allUsers?: true } + } + + export interface Lookup { + /** [`$lookup` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/) */ + $lookup: { + from: string + as: string + localField?: string + foreignField?: string + let?: Record + pipeline?: Exclude[] + } + } + + export interface Match { + /** [`$match` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match/) */ + $match: QueryFilter; + } + + export interface Merge { + /** [`$merge` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge/) */ + $merge: { + into: string | { db: string; coll: string } + on?: string | string[] + let?: Record + whenMatched?: 'replace' | 'keepExisting' | 'merge' | 'fail' | Extract[] + whenNotMatched?: 'insert' | 'discard' | 'fail' + } + } + + export interface Out { + /** [`$out` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/out/) */ + $out: string | { db: string; coll: string } + } + + export interface PlanCacheStats { + /** [`$planCacheStats` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/planCacheStats/) */ + $planCacheStats: Record + } + + export interface Project { + /** [`$project` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/project/) */ + $project: { [field: string]: AnyExpression | Expression | Project['$project'] } + } + + export interface Redact { + /** [`$redact` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/redact/) */ + $redact: Expression; + } + + export interface ReplaceRoot { + /** [`$replaceRoot` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/replaceRoot/) */ + $replaceRoot: { newRoot: AnyExpression } + } + + export interface ReplaceWith { + /** [`$replaceWith` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/replaceWith/) */ + $replaceWith: ObjectExpressionOperator | { [field: string]: Expression } | `$${string}`; + } + + export interface Sample { + /** [`$sample` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sample/) */ + $sample: { size: number } + } + + export interface Search { + /** [`$search` reference](https://www.mongodb.com/docs/atlas/atlas-search/query-syntax/) */ + $search: { + index?: string; + highlight?: { + /** [`highlightPath` reference](https://docs.atlas.mongodb.com/atlas-search/path-construction/#multiple-field-search) */ + path: string | string[] | { value: string, multi: string }; + maxCharsToExamine?: number; + maxNumPassages?: number; + }; + [operator: string]: any; + } + } + + export interface SearchMeta { + /** [`$searchMeta` reference](https://www.mongodb.com/docs/atlas/atlas-search/query-syntax/#mongodb-pipeline-pipe.-searchMeta) */ + $searchMeta: { + index?: string; + highlight?: { + /** [`highlightPath` reference](https://docs.atlas.mongodb.com/atlas-search/path-construction/#multiple-field-search) */ + path: string | string[] | { value: string, multi: string }; + maxCharsToExamine?: number; + maxNumPassages?: number; + }; + [operator: string]: any; + } + } + + export interface Set { + /** [`$set` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/set/) */ + $set: Record + } + + export interface SetWindowFields { + /** [`$setWindowFields` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setWindowFields/) */ + $setWindowFields: { + partitionBy?: any + sortBy?: Record + output: Record< + string, + WindowOperator & { + window?: { + documents?: [string | number, string | number] + range?: [string | number, string | number] + unit?: 'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond' + } + } + > + } + } + + export interface Skip { + /** [`$skip` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/skip/) */ + $skip: number + } + + export interface Sort { + /** [`$sort` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort/) */ + $sort: Record + } + + export interface SortByCount { + /** [`$sortByCount` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sortByCount/) */ + $sortByCount: Expression; + } + + export interface UnionWith { + /** [`$unionWith` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unionWith/) */ + $unionWith: + | string + | { coll: string; pipeline?: UnionWithPipelineStage[] } + | { coll?: string; pipeline: UnionWithPipelineStage[] } + } + + export interface Unset { + /** [`$unset` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unset/) */ + $unset: string | string[] + } + + export interface Unwind { + /** [`$unwind` reference](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unwind/) */ + $unwind: string | { path: string; includeArrayIndex?: string; preserveNullAndEmptyArrays?: boolean } + } + export interface VectorSearch { + /** [`$vectorSearch` reference](https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/) */ + $vectorSearch: { + exact?: boolean; + index: string, + path: string, + queryVector: number[], + numCandidates?: number, + limit: number, + filter?: Expression, + } + } + + } +} diff --git a/gateway/node_modules/mongoose/types/populate.d.ts b/gateway/node_modules/mongoose/types/populate.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..dd3bbda288a7cc101f88209e956684226d006e8f --- /dev/null +++ b/gateway/node_modules/mongoose/types/populate.d.ts @@ -0,0 +1,105 @@ +declare module 'mongoose' { + + /** + * Reference another Model + */ + type PopulatedDoc< + PopulatedType, + RawId extends RefType = (PopulatedType extends { _id?: RefType; } ? NonNullable : Types.ObjectId) | undefined + > = PopulatedType | RawId; + + const mongoosePopulatedDocumentMarker: unique symbol; + + type ExtractDocumentObjectType = T extends infer ObjectType & Document ? FlatRecord : T; + + type PopulatePathToRawDocType = + T extends Types.DocumentArray + ? PopulatePathToRawDocType[] + : T extends Array + ? PopulatePathToRawDocType[] + : T extends Document + ? SubdocsToPOJOs> + : T extends Record + ? { [K in keyof T]: PopulatePathToRawDocType } + : T; + + type PopulatedPathsDocumentType = UnpackedIntersection>; + + type PopulatedDocumentMarker< + PopulatedRawDocType, + DepopulatedRawDocType, + > = { + [mongoosePopulatedDocumentMarker]?: { + populated: PopulatedRawDocType, + depopulated: DepopulatedRawDocType + } + }; + + type ResolvePopulatedRawDocType< + ThisType, + FallbackRawDocType, + O = never + > = ThisType extends PopulatedDocumentMarker + ? O extends { depopulate: true } + ? DepopulatedRawDocType + : PopulatedRawDocType + : FallbackRawDocType; + + type PopulateDocumentResult< + Doc, + Paths, + PopulatedRawDocType, + DepopulatedRawDocType = PopulatedRawDocType + > = MergeType & PopulatedDocumentMarker; + + interface PopulateOptions { + /** space delimited path(s) to populate */ + path: string; + /** fields to select */ + select?: any; + /** query conditions to match */ + match?: any; + /** optional model to use for population */ + model?: string | Model; + /** by default, Mongoose removes null and undefined values from populated arrays. Use this option to make `populate()` retain `null` and `undefined` array entries. */ + retainNullValues?: boolean; + /** if true, Mongoose will call any getters defined on the `localField`. By default, Mongoose gets the raw value of `localField`. */ + getters?: boolean; + /** if true, Mongoose will clone populated docs before assigning them, so docs that are populated onto multiple parents don't share 1 copy. */ + clone?: boolean; + /** By default, Mongoose throws a cast error if `localField` and `foreignField` schemas don't line up. If you enable this option, Mongoose will instead filter out any `localField` properties that cannot be casted to `foreignField`'s schema type. */ + skipInvalidIds?: boolean; + /** optional query options like sort, limit, etc */ + options?: QueryOptions; + /** correct limit on populated array */ + perDocumentLimit?: number; + /** optional boolean, set to `false` to allow populating paths that aren't in the schema */ + strictPopulate?: boolean; + /** deep populate */ + populate?: string | PopulateOptions | (string | PopulateOptions)[]; + /** + * If true Mongoose will always set `path` to a document, or `null` if no document was found. + * If false Mongoose will always set `path` to an array, which will be empty if no documents are found. + * Inferred from schema by default. + */ + justOne?: boolean; + /** transform function to call on every populated doc */ + transform?: (doc: any, id: any) => any; + /** Overwrite the schema-level local field to populate on if this is a populated virtual. */ + localField?: string; + /** Overwrite the schema-level foreign field to populate on if this is a populated virtual. */ + foreignField?: string; + /** Set to `false` to prevent Mongoose from repopulating paths that are already populated */ + forceRepopulate?: boolean; + /** + * Set to `true` to execute any populate queries one at a time, as opposed to in parallel. + * We recommend setting this option to `true` if using transactions, especially if also populating multiple paths or paths with multiple models. + * MongoDB server does **not** support multiple operations in parallel on a single transaction. + */ + ordered?: boolean; + } + + interface PopulateOption { + populate?: string | string[] | PopulateOptions | PopulateOptions[]; + } +} diff --git a/gateway/node_modules/mongoose/types/query.d.ts b/gateway/node_modules/mongoose/types/query.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..b736e3ef92cb402119f35ad46035c6b55be8acf2 --- /dev/null +++ b/gateway/node_modules/mongoose/types/query.d.ts @@ -0,0 +1,982 @@ +declare module 'mongoose' { + import mongodb = require('mongodb'); + + type StringQueryTypeCasting = string extends T ? string | RegExp : T | RegExp; + type ObjectIdQueryTypeCasting = Types.ObjectId | string; + type DateQueryTypeCasting = string | number | NativeDate; + type UUIDQueryTypeCasting = Types.UUID | string; + type BufferQueryCasting = Buffer | mongodb.Binary | number[] | string | { $binary: string | mongodb.Binary }; + type QueryTypeCasting = T extends string + ? StringQueryTypeCasting + : T extends Types.ObjectId + ? ObjectIdQueryTypeCasting + : T extends Types.UUID + ? UUIDQueryTypeCasting + : T extends Buffer + ? BufferQueryCasting + : T extends NativeDate + ? DateQueryTypeCasting + : T; + + export type ApplyBasicQueryCasting = QueryTypeCasting | QueryTypeCasting[] | (T extends (infer U)[] ? QueryTypeCasting : T) | null; + + type RemoveIndexSignature = { + [K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K]; + }; + + type StrictFilterOperators = RemoveIndexSignature>; + + type StrictCondition = mongodb.AlternativeType | StrictFilterOperators>; + + type _StrictFilter = { + [P in keyof TSchema]?: StrictCondition>; + } & StrictRootFilterOperators; + + type StrictRootFilterOperators = Omit, '$and' | '$or' | '$nor'> & { + $and?: _StrictFilter[]; + $or?: _StrictFilter[]; + $nor?: _StrictFilter[]; + }; + + type _QueryFilter = ( + { [P in keyof T]?: StrictCondition>; } & + StrictRootFilterOperators<{ [P in keyof mongodb.WithId]?: ApplyBasicQueryCasting[P]>; }> + ); + type _QueryFilterLooseId = ( + { [P in keyof T]?: StrictCondition>; } & + StrictRootFilterOperators< + { [P in keyof T]?: ApplyBasicQueryCasting; } + > + ); + type QueryFilter = IsItRecordAndNotAny extends true ? _QueryFilter> : _QueryFilterLooseId>; + + type MongooseBaseQueryOptionKeys = + | 'cloneUpdate' + | 'context' + | 'middleware' + | 'multipleCastError' + | 'overwriteDiscriminatorKey' + | 'overwriteImmutable' + | 'populate' + | 'runValidators' + | 'sanitizeProjection' + | 'sanitizeFilter' + | 'schemaLevelProjections' + | 'setDefaultsOnInsert' + | 'strict' + | 'strictQuery' + | 'translateAliases' + | 'updatePipeline'; + + type MongooseBaseQueryOptions = Pick, MongooseBaseQueryOptionKeys | 'timestamps' | 'lean'> & { + [other: string]: any; + }; + + type MongooseUpdateQueryOptions = Pick, MongooseBaseQueryOptionKeys | 'timestamps'>; + + type ProjectionFields = { [Key in keyof DocType]?: any } & Record; + + type QueryWithHelpers< + ResultType, + DocType, + THelpers = {}, + RawDocType = DocType, + QueryOp = 'find', + TDocOverrides = Record + > = Query & THelpers; + + interface QueryTimestampsConfig { + createdAt?: boolean; + updatedAt?: boolean; + } + + // Options that can be passed to Query.prototype.lean() + interface LeanOptions { + // Set to false to strip out the version key + versionKey?: boolean; + // Transform the result document in place. `doc` is the raw document being transformed. + // Typed as `Record` because TypeScript gets confused when handling Document.prototype.deleteOne() + // and other document methods that try to infer the raw doc type from the Document class. + transform?: (doc: Record) => void; + [key: string]: any; + } + + interface QueryOptions extends + PopulateOption, + SessionOption { + arrayFilters?: AnyObject[]; + batchSize?: number; + collation?: mongodb.CollationOptions; + comment?: any; + context?: string; + /** + * If `false`, Mongoose will not clone the update before executing the query. + */ + cloneUpdate?: boolean; + explain?: mongodb.ExplainVerbosityLike; + fields?: any | string; + hint?: mongodb.Hint; + /** + * If truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. + */ + lean?: boolean | LeanOptions; + limit?: number; + maxTimeMS?: number; + multi?: boolean; + multipleCastError?: boolean; + /** + * By default, `findOneAndUpdate()` returns the document as it was **before** + * `update` was applied. If you set `new: true`, `findOneAndUpdate()` will + * instead give you the object after `update` was applied. + * Use `returnDocument: 'after'` instead of `new: true`, or `returnDocument: 'before'` instead of `new: false`. + * + * @deprecated + */ + new?: boolean; + + /** + * If `false`, skip applying default schema values to the returned document(s). + * @default true + */ + defaults?: boolean; + overwriteDiscriminatorKey?: boolean; + /** + * Mongoose removes updated immutable properties from `update` by default (excluding $setOnInsert). + * Set `overwriteImmutable` to `true` to allow updating immutable properties using other update operators. + */ + overwriteImmutable?: boolean; + projection?: AnyObject | string; + /** + * if true, returns the full ModifyResult rather than just the document + */ + includeResultMetadata?: boolean; + readPreference?: string | mongodb.ReadPreferenceMode; + /** + * An alias for the `new` option. `returnOriginal: false` is equivalent to `new: true`. + * Use `returnDocument: 'after'` instead of `returnOriginal: false`, or `returnDocument: 'before'` instead of `returnOriginal: true`. + * + * @deprecated + */ + returnOriginal?: boolean; + /** + * Has two possible values, `'before'` and `'after'`. By default, it will return the document before the update was applied. + * @default 'before' + */ + returnDocument?: 'before' | 'after'; + /** + * Set to true to enable `update validators` + * (https://mongoosejs.com/docs/validation.html#update-validators). Defaults to false. + */ + runValidators?: boolean; + /* Set to `true` to automatically sanitize potentially unsafe user-generated query projections */ + sanitizeProjection?: boolean; + /** + * Set to `true` to automatically sanitize potentially unsafe query filters by stripping out query selectors that + * aren't explicitly allowed using `mongoose.trusted()`. + */ + sanitizeFilter?: boolean; + /** + * Enable or disable schema level projections for this query. Enabled by default. + * Set to `false` to include fields with `select: false` in the query result by default. + */ + schemaLevelProjections?: boolean; + setDefaultsOnInsert?: boolean; + skip?: number; + sort?: any; + /** overwrites the schema's strict mode option */ + strict?: boolean | string; + /** + * equal to `strict` by default, may be `false`, `true`, or `'throw'`. Sets the default + * [strictQuery](https://mongoosejs.com/docs/guide.html#strictQuery) mode for schemas. + */ + strictQuery?: boolean | 'throw'; + tailable?: number; + /** + * If set to `false` and schema-level timestamps are enabled, + * skip timestamps for this update. Note that this allows you to overwrite + * timestamps. Does nothing if schema-level timestamps are not set. + */ + timestamps?: boolean | QueryTimestampsConfig; + /** + * If `true`, convert any aliases in filter, projection, update, and distinct + * to their database property names. Defaults to false. + */ + translateAliases?: boolean; + upsert?: boolean; + useBigInt64?: boolean; + /** + * Set to true to allow passing in an update pipeline instead of an update document. + * Mongoose disallows update pipelines by default because Mongoose does not cast update pipelines. + */ + updatePipeline?: boolean; + writeConcern?: mongodb.WriteConcern; + + /** set to `false` to skip all user-defined middleware, or `{ pre: false }` / `{ post: false }` to skip only pre or post hooks */ + middleware?: boolean | SkipMiddlewareOptions; + + [other: string]: any; + } + + type QueryOpThatReturnsDocument = 'find' | 'findOne' | 'findOneAndUpdate' | 'findOneAndReplace' | 'findOneAndDelete'; + + type GetLeanResultType = QueryOp extends QueryOpThatReturnsDocument + ? (ResultType extends any[] ? Default__v>[] : Default__v>) + : ResultType; + + type MergePopulatePaths> = QueryOp extends QueryOpThatReturnsDocument + ? ResultType extends null + ? ResultType + : ResultType extends (infer U)[] + ? U extends Document + ? PopulateDocumentResult, RawDocType>[] + : (MergeType)[] + : ResultType extends Document + ? PopulateDocumentResult, RawDocType> + : MergeType + : MergeType; + + class Query> implements SessionOperation { + _mongooseOptions: QueryOptions; + + /** + * Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/7.0/classes/FindCursor.html). + * A QueryCursor exposes a Streams3 interface, as well as a `.next()` function. + * This is equivalent to calling `.cursor()` with no arguments. + */ + [Symbol.asyncIterator](): AsyncIterableIterator>; + + /** Executes the query */ + exec(): Promise; + + $where(argument: string | Function): QueryWithHelpers< + DocType[], + DocType, + THelpers, + RawDocType, + QueryOp, + TDocOverrides + >; + + /** Specifies an `$all` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + all(path: string, val: Array): this; + all(val: Array): this; + + /** Sets the allowDiskUse option for the query (ignored for < 4.4.0) */ + allowDiskUse(value: boolean): this; + + /** Specifies arguments for an `$and` condition. */ + and(array: QueryFilter[]): this; + + /** Specifies the batchSize option. */ + batchSize(val: number): this; + + /** Specifies a `$box` condition */ + box(lower: number[], upper: number[]): this; + box(val: any): this; + + /** + * Casts this query to the schema of `model`. + * + * @param {Model} [model] the model to cast to. If not set, defaults to `this.model` + * @param {Object} [obj] If not set, defaults to this query's conditions + * @return {Object} the casted `obj` + */ + cast(model?: Model | null, obj?: any): any; + + /** + * Executes the query returning a `Promise` which will be + * resolved with either the doc(s) or rejected with the error. + * Like `.then()`, but only takes a rejection handler. + */ + catch: Promise['catch']; + + /** + * Executes the query returning a `Promise` which will be + * resolved with `.finally()` chained. + */ + finally: Promise['finally']; + + // Returns a string representation of this query. + [Symbol.toStringTag]: string; + + /** Specifies a `$center` or `$centerSphere` condition. */ + circle(path: string, area: any): this; + circle(area: any): this; + + /** Make a copy of this query so you can re-execute it. */ + clone(): this; + + /** Adds a collation to this op (MongoDB 3.4 and up) */ + collation(value: mongodb.CollationOptions): this; + + /** Specifies the `comment` option. */ + comment(val: string): this; + + /** Specifies this query as a `countDocuments` query. */ + countDocuments( + criteria?: QueryFilter, + options?: QueryOptions + ): QueryWithHelpers; + countDocuments( + criteria?: Query, + options?: QueryOptions + ): QueryWithHelpers; + + /** + * Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/7.0/classes/FindCursor.html). + * A QueryCursor exposes a Streams3 interface, as well as a `.next()` function. + */ + cursor(options?: QueryOptions): Cursor, QueryOptions>; + + /** + * Declare and/or execute this query as a `deleteMany()` operation. Works like + * remove, except it deletes _every_ document that matches `filter` in the + * collection, regardless of the value of `single`. + */ + deleteMany( + filter?: QueryFilter, + options?: QueryOptions + ): QueryWithHelpers; + deleteMany( + filter?: Query, + options?: QueryOptions + ): QueryWithHelpers; + deleteMany(filter: QueryFilter): QueryWithHelpers< + any, + DocType, + THelpers, + RawDocType, + 'deleteMany', + TDocOverrides + >; + deleteMany(filter: Query): QueryWithHelpers< + any, + DocType, + THelpers, + RawDocType, + 'deleteMany', + TDocOverrides + >; + deleteMany(): QueryWithHelpers; + + /** + * Declare and/or execute this query as a `deleteOne()` operation. Works like + * remove, except it deletes at most one document regardless of the `single` + * option. + */ + deleteOne( + filter?: QueryFilter, + options?: QueryOptions + ): QueryWithHelpers; + deleteOne( + filter?: Query, + options?: QueryOptions + ): QueryWithHelpers; + deleteOne(filter: QueryFilter): QueryWithHelpers< + any, + DocType, + THelpers, + RawDocType, + 'deleteOne', + TDocOverrides + >; + deleteOne(filter: Query): QueryWithHelpers< + any, + DocType, + THelpers, + RawDocType, + 'deleteOne', + TDocOverrides + >; + deleteOne(): QueryWithHelpers; + + /** Creates a `distinct` query: returns the distinct values of the given `field` that match `filter`. */ + distinct( + field: DocKey, + filter?: QueryFilter, + options?: QueryOptions + ): QueryWithHelpers< + Array< + DocKey extends keyof WithLevel1NestedPaths + ? WithoutUndefined[DocKey]>> + : ResultType + >, + DocType, + THelpers, + RawDocType, + 'distinct', + TDocOverrides + >; + distinct( + field: DocKey, + filter?: Query, + options?: QueryOptions + ): QueryWithHelpers< + Array< + DocKey extends keyof WithLevel1NestedPaths + ? WithoutUndefined[DocKey]>> + : ResultType + >, + DocType, + THelpers, + RawDocType, + 'distinct', + TDocOverrides + >; + + /** Specifies a `$elemMatch` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + elemMatch(path: string, val: any): this; + elemMatch(val: Function | any): this; + + /** + * Gets/sets the error flag on this query. If this flag is not null or + * undefined, the `exec()` promise will reject without executing. + */ + error(): NativeError | null; + error(val: NativeError | null): this; + + /** Specifies the complementary comparison value for paths specified with `where()` */ + equals(val: any): this; + + /** Creates a `estimatedDocumentCount` query: counts the number of documents in the collection. */ + estimatedDocumentCount(options?: QueryOptions): QueryWithHelpers< + number, + DocType, + THelpers, + RawDocType, + 'estimatedDocumentCount', + TDocOverrides + >; + + /** Specifies a `$exists` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + exists(path: string, val: boolean): this; + exists(val: boolean): this; + + /** + * Sets the [`explain` option](https://www.mongodb.com/docs/manual/reference/method/cursor.explain/), + * which makes this query return detailed execution stats instead of the actual + * query result. This method is useful for determining what index your queries + * use. + */ + explain(verbose?: mongodb.ExplainVerbosityLike): this; + + /** Creates a `find` query: gets a list of documents that match `filter`. */ + find( + filter?: QueryFilter, + projection?: ProjectionType | null, + options?: QueryOptions | null + ): QueryWithHelpers, DocType, THelpers, RawDocType, 'find', TDocOverrides>; + find( + filter?: Query, + projection?: ProjectionType | null, + options?: QueryOptions | null + ): QueryWithHelpers, DocType, THelpers, RawDocType, 'find', TDocOverrides>; + + /** Declares the query a findOne operation. When executed, returns the first found document. */ + findOne( + filter?: QueryFilter, + projection?: ProjectionType | null, + options?: QueryOptions | null + ): QueryWithHelpers; + findOne( + filter?: Query, + projection?: ProjectionType | null, + options?: QueryOptions | null + ): QueryWithHelpers; + + /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */ + findOneAndDelete( + filter?: QueryFilter, + options?: QueryOptions | null + ): QueryWithHelpers; + findOneAndDelete( + filter?: Query, + options?: QueryOptions | null + ): QueryWithHelpers; + + /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */ + findOneAndUpdate( + filter: QueryFilter, + update: UpdateQuery, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>; + findOneAndUpdate( + filter: Query, + update: UpdateQuery, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers, DocType, THelpers, RawDocType, 'findOneAndUpdate', TDocOverrides>; + findOneAndUpdate( + filter: QueryFilter, + update: UpdateQuery, + options: QueryOptions & { upsert: true } & ReturnsNewDoc + ): QueryWithHelpers; + findOneAndUpdate( + filter: Query, + update: UpdateQuery, + options: QueryOptions & { upsert: true } & ReturnsNewDoc + ): QueryWithHelpers; + findOneAndUpdate( + filter?: QueryFilter, + update?: UpdateQuery, + options?: QueryOptions | null + ): QueryWithHelpers; + findOneAndUpdate( + filter?: Query, + update?: UpdateQuery, + options?: QueryOptions | null + ): QueryWithHelpers; + + /** Declares the query a findById operation. When executed, returns the document with the given `_id`. */ + findById( + id: mongodb.ObjectId | any, + projection?: ProjectionType | null, + options?: QueryOptions | null + ): QueryWithHelpers; + + /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */ + findByIdAndDelete( + id: mongodb.ObjectId | any, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers, DocType, THelpers, RawDocType, 'findOneAndDelete', TDocOverrides>; + findByIdAndDelete( + id?: mongodb.ObjectId | any, + options?: QueryOptions | null + ): QueryWithHelpers; + + /** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */ + findByIdAndUpdate( + id: mongodb.ObjectId | any, + update: UpdateQuery, + options: QueryOptions & { includeResultMetadata: true } + ): QueryWithHelpers; + findByIdAndUpdate( + id: mongodb.ObjectId | any, + update: UpdateQuery, + options: QueryOptions & { upsert: true } & ReturnsNewDoc + ): QueryWithHelpers; + findByIdAndUpdate( + id?: mongodb.ObjectId | any, + update?: UpdateQuery, + options?: QueryOptions | null + ): QueryWithHelpers; + + /** Specifies a `$geometry` condition */ + geometry(object: { type: string, coordinates: any[] }): this; + + /** + * For update operations, returns the value of a path in the update's `$set`. + * Useful for writing getters/setters that can work with both update operations + * and `save()`. + */ + get(path: string): any; + + /** Returns the current query filter (also known as conditions) as a POJO. */ + getFilter(): QueryFilter; + + /** Gets query options. */ + getOptions(): QueryOptions; + + /** Gets a list of paths to be populated by this query */ + getPopulatedPaths(): Array; + + /** Returns the current query filter. Equivalent to `getFilter()`. */ + getQuery(): QueryFilter; + + /** Returns the current update operations as a JSON object. */ + getUpdate(): UpdateQuery | UpdateWithAggregationPipeline | null; + + /** Specifies a `$gt` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + gt(path: string, val: any): this; + gt(val: number): this; + + /** Specifies a `$gte` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + gte(path: string, val: any): this; + gte(val: number): this; + + /** Sets query hints. */ + hint(val: any): this; + + /** Specifies an `$in` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + in(path: string, val: any[]): this; + in(val: Array): this; + + /** Declares an intersects query for `geometry()`. */ + intersects(arg?: any): this; + + /** Requests acknowledgement that this operation has been persisted to MongoDB's on-disk journal. */ + j(val: boolean | null): this; + + /** Sets the lean option. */ + lean(): QueryWithHelpers< + ResultType extends null + ? GetLeanResultType | null + : GetLeanResultType, + DocType, + THelpers, + RawDocType, + QueryOp, + TDocOverrides + >; + lean( + val: true | LeanOptions + ): QueryWithHelpers< + ResultType extends null + ? GetLeanResultType | null + : GetLeanResultType, + DocType, + THelpers, + RawDocType, + QueryOp, + TDocOverrides + >; + lean( + val: false + ): QueryWithHelpers< + ResultType extends AnyArray + ? DocType[] + : ResultType extends null + ? DocType | null + : DocType, + DocType, + THelpers, + RawDocType, + QueryOp, + TDocOverrides + >; + lean(): QueryWithHelpers< + ResultType extends null + ? LeanResultType | null + : LeanResultType, + DocType, + THelpers, + RawDocType, + QueryOp, + TDocOverrides + >; + lean( + val: boolean | LeanOptions + ): QueryWithHelpers< + ResultType extends null + ? LeanResultType | null + : LeanResultType, + DocType, + THelpers, + RawDocType, + QueryOp, + TDocOverrides + >; + + /** Specifies the maximum number of documents the query will return. */ + limit(val: number): this; + + /** Specifies a `$lt` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + lt(path: string, val: any): this; + lt(val: number): this; + + /** Specifies a `$lte` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + lte(path: string, val: any): this; + lte(val: number): this; + + /** + * Runs a function `fn` and treats the return value of `fn` as the new value + * for the query to resolve to. + */ + transform(fn: (doc: ResultType) => MappedType): QueryWithHelpers; + + /** Specifies an `$maxDistance` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + maxDistance(path: string, val: number): this; + maxDistance(val: number): this; + + /** + * Sets the [maxTimeMS](https://www.mongodb.com/docs/manual/reference/method/cursor.maxTimeMS/) + * option. This will tell the MongoDB server to abort if the query or write op + * has been running for more than `ms` milliseconds. + */ + maxTimeMS(ms: number): this; + + /** Merges another Query or conditions object into this one. */ + merge(source: QueryFilter): this; + merge(source: Query): this; + + /** Specifies a `$mod` condition, filters documents for documents whose `path` property is a number that is equal to `remainder` modulo `divisor`. */ + mod(path: string, val: number): this; + mod(val: Array): this; + + /** The model this query was created from */ + model: Model; // Can't use DocType, causes "Type instantiation is excessively deep" + + /** + * Getter/setter around the current mongoose-specific options for this query + * Below are the current Mongoose-specific options. + */ + mongooseOptions(val?: QueryOptions): QueryOptions; + + /** Specifies a `$ne` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + ne(path: string, val: any): this; + ne(val: any): this; + + /** Specifies a `$near` or `$nearSphere` condition */ + near(path: string, val: any): this; + near(val: any): this; + + /** Specifies an `$nin` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + nin(path: string, val: any[]): this; + nin(val: Array): this; + + /** Specifies arguments for an `$nor` condition. */ + nor(array: Array>): this; + + /** Specifies arguments for an `$or` condition. */ + or(array: Array>): this; + + /** + * Make this query throw an error if no documents match the given `filter`. + * This is handy for integrating with async/await, because `orFail()` saves you + * an extra `if` statement to check if no document was found. + */ + orFail(err?: NativeError | (() => NativeError)): QueryWithHelpers, DocType, THelpers, RawDocType, QueryOp, TDocOverrides>; + + /** Specifies a `$polygon` condition */ + polygon(path: string, ...coordinatePairs: number[][]): this; + polygon(...coordinatePairs: number[][]): this; + + /** Specifies paths which should be populated with other documents. */ + populate( + path: string | string[], + select?: string | any, + model?: string | Model, + match?: any + ): QueryWithHelpers< + ResultType, + DocType, + THelpers, + RawDocType, + QueryOp, + TDocOverrides + >; + populate( + options: PopulateOptions | (PopulateOptions | string)[] + ): QueryWithHelpers< + ResultType, + DocType, + THelpers, + RawDocType, + QueryOp, + TDocOverrides + >; + populate( + path: string | string[], + select?: string | any, + model?: string | Model, + match?: any + ): QueryWithHelpers< + MergePopulatePaths, + DocType, + THelpers, + UnpackedIntersection, + QueryOp, + TDocOverrides + >; + populate( + options: PopulateOptions | (PopulateOptions | string)[] + ): QueryWithHelpers< + MergePopulatePaths, + DocType, + THelpers, + UnpackedIntersection, + QueryOp, + TDocOverrides + >; + + /** Add pre middleware to this query instance. Doesn't affect other queries. */ + pre(fn: Function): this; + + /** Add post middleware to this query instance. Doesn't affect other queries. */ + post(fn: Function): this; + + /** Get/set the current projection (AKA fields). Pass `null` to remove the current projection. */ + projection(fields?: ProjectionFields | string): ProjectionFields; + projection(fields: null): null; + projection(): ProjectionFields | null; + + /** Determines the MongoDB nodes from which to read. */ + read(mode: string | mongodb.ReadPreferenceMode, tags?: any[]): this; + + /** Sets the readConcern option for the query. */ + readConcern(level: string): this; + + /** Specifies a `$regex` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + regex(path: string, val: RegExp): this; + regex(val: string | RegExp): this; + + /** + * Declare and/or execute this query as a replaceOne() operation. Same as + * `update()`, except MongoDB will replace the existing document and will + * not accept any [atomic](https://www.mongodb.com/docs/manual/tutorial/model-data-for-atomic-operations/#pattern) operators (`$set`, etc.) + */ + replaceOne( + filter?: QueryFilter, + replacement?: DocType | AnyObject, + options?: QueryOptions | null + ): QueryWithHelpers; + replaceOne( + filter?: Query, + replacement?: DocType | AnyObject, + options?: QueryOptions | null + ): QueryWithHelpers; + + /** + * Sets this query's `sanitizeProjection` option. With `sanitizeProjection()`, you can pass potentially untrusted user data to `.select()`. + */ + sanitizeProjection(value: boolean): this; + + /** + * Enable or disable schema level projections for this query. Enabled by default. + * Set to `false` to include fields with `select: false` in the query result by default. + */ + schemaLevelProjections(value: boolean): this; + + /** Specifies which document fields to include or exclude (also known as the query "projection") */ + select( + arg: string | readonly string[] | Record + ): QueryWithHelpers< + IfEquals< + RawDocTypeOverride, + {}, + ResultType, + ResultType extends any[] + ? ResultType extends HydratedDocument[] + ? HydratedDocument[] + : RawDocTypeOverride[] + : (ResultType extends HydratedDocument + ? HydratedDocument + : RawDocTypeOverride) | (null extends ResultType ? null : never) + >, + DocType, + THelpers, + IfEquals< + RawDocTypeOverride, + {}, + RawDocType, + RawDocTypeOverride + >, + QueryOp, + TDocOverrides + >; + + /** Determines if field selection has been made. */ + selected(): boolean; + + /** Determines if exclusive field selection has been made. */ + selectedExclusively(): boolean; + + /** Determines if inclusive field selection has been made. */ + selectedInclusively(): boolean; + + /** + * Sets the [MongoDB session](https://www.mongodb.com/docs/manual/reference/server-sessions/) + * associated with this query. Sessions are how you mark a query as part of a + * [transaction](/docs/transactions.html). + */ + session(session: mongodb.ClientSession | null): this; + + /** + * Adds a `$set` to this query's update without changing the operation. + * This is useful for query middleware so you can add an update regardless + * of whether you use `updateOne()`, `updateMany()`, `findOneAndUpdate()`, etc. + */ + set(path: string | Record, value?: any): this; + + /** Sets query options. Some options only make sense for certain operations. */ + setOptions(options: QueryOptions, overwrite?: boolean): this; + + /** Sets the query conditions to the provided JSON object. */ + setQuery(val: QueryFilter | null): void; + setQuery(val: Query | null): void; + + setUpdate(update: UpdateQuery | UpdateWithAggregationPipeline, cloneUpdate?: boolean): void; + + /** Specifies an `$size` query condition. When called with one argument, the most recent path passed to `where()` is used. */ + size(path: string, val: number): this; + size(val: number): this; + + /** Specifies the number of documents to skip. */ + skip(val: number): this; + + /** Specifies a `$slice` projection for an array. */ + slice(path: string, val: number | Array): this; + slice(val: number | Array): this; + + /** Sets the sort order. If an object is passed, values allowed are `asc`, `desc`, `ascending`, `descending`, `1`, and `-1`. */ + sort( + arg?: string | Record | [string, SortOrder][] | undefined | null, + options?: { override?: boolean } + ): this; + + /** Sets the tailable option (for use with capped collections). */ + tailable(bool?: boolean, opts?: { + numberOfRetries?: number; + tailableRetryInterval?: number; + }): this; + + /** + * Executes the query returning a `Promise` which will be + * resolved with either the doc(s) or rejected with the error. + */ + then: Promise['then']; + + /** Converts this query to a customized, reusable query constructor with all arguments and options retained. */ + toConstructor(): RetType; + + /** + * Declare and/or execute this query as an updateMany() operation. Same as + * `update()`, except MongoDB will update _all_ documents that match `filter` + */ + updateMany( + filter: QueryFilter, + update: UpdateQuery | UpdateWithAggregationPipeline, + options?: QueryOptions | null + ): QueryWithHelpers; + updateMany( + filter: Query, + update: UpdateQuery | UpdateWithAggregationPipeline, + options?: QueryOptions | null + ): QueryWithHelpers; + + /** + * Declare and/or execute this query as an updateOne() operation. Same as + * `update()`, except it does not support the `multi` or `overwrite` options. + */ + updateOne( + filter: QueryFilter, + update: UpdateQuery | UpdateWithAggregationPipeline, + options?: QueryOptions | null + ): QueryWithHelpers; + updateOne( + filter: Query, + update: UpdateQuery | UpdateWithAggregationPipeline, + options?: QueryOptions | null + ): QueryWithHelpers; + + /** + * Sets the specified number of `mongod` servers, or tag set of `mongod` servers, + * that must acknowledge this write before this write is considered successful. + */ + w(val: string | number | null): this; + + /** Specifies a path for use with chaining. */ + where(path: string, val?: any): this; + where(obj: object): this; + where(): this; + + /** Defines a `$within` or `$geoWithin` argument for geo-spatial queries. */ + within(val?: any): this; + + /** + * If [`w > 1`](/docs/api/query.html#query_Query-w), the maximum amount of time to + * wait for this write to propagate through the replica set before this + * operation fails. The default is `0`, which means no timeout. + */ + wtimeout(ms: number): this; + } +} diff --git a/gateway/node_modules/mongoose/types/schemaoptions.d.ts b/gateway/node_modules/mongoose/types/schemaoptions.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..9d1db85b5ca34a562a1a74d9c5db6b9f0b756903 --- /dev/null +++ b/gateway/node_modules/mongoose/types/schemaoptions.d.ts @@ -0,0 +1,290 @@ +declare module 'mongoose' { + import mongodb = require('mongodb'); + + interface SchemaTimestampsConfig { + createdAt?: boolean | string; + updatedAt?: boolean | string; + currentTime?: () => (NativeDate | number); + } + + type SchemaOptionsStaticsPropertyType = IfEquals< + TStaticMethods, + {}, + Record unknown> & ThisType, + { [K in keyof TStaticMethods]: OmitThisParameter } & ThisType + >; + + type TypeKeyBaseType = string; + + type DefaultTypeKey = 'type'; + interface SchemaOptions< + DocType = unknown, + TInstanceMethods = {}, + QueryHelpers = {}, + TStaticMethods = {}, + TVirtuals = {}, + THydratedDocumentType = HydratedDocument, + TModelType = Model + > { + /** + * By default, Mongoose's init() function creates all the indexes defined in your model's schema by + * calling Model.createIndexes() after you successfully connect to MongoDB. If you want to disable + * automatic index builds, you can set autoIndex to false. + */ + autoIndex?: boolean; + /** + * Similar to autoIndex, except for automatically creates any Atlas search indexes defined in your + * schema. Unlike autoIndex, this option defaults to false. + */ + autoSearchIndex?: boolean; + /** + * If set to `true`, Mongoose will call Model.createCollection() to create the underlying collection + * in MongoDB if autoCreate is set to true. Calling createCollection() sets the collection's default + * collation based on the collation option and establishes the collection as a capped collection if + * you set the capped schema option. + */ + autoCreate?: boolean; + /** + * By default, mongoose buffers commands when the connection goes down until the driver manages to reconnect. + * To disable buffering, set bufferCommands to false. + */ + bufferCommands?: boolean; + /** + * If bufferCommands is on, this option sets the maximum amount of time Mongoose buffering will wait before + * throwing an error. If not specified, Mongoose will use 10000 (10 seconds). + */ + bufferTimeoutMS?: number; + /** + * Mongoose supports MongoDBs capped collections. To specify the underlying MongoDB collection be capped, set + * the capped option to the maximum size of the collection in bytes. + */ + capped?: boolean | number | { size?: number; max?: number; autoIndexId?: boolean; }; + /** Sets a default collation for every query and aggregation. */ + collation?: mongodb.CollationOptions; + + /** Arbitrary options passed to `createCollection()` */ + collectionOptions?: mongodb.CreateCollectionOptions; + + /** Default lean options for queries */ + lean?: boolean | LeanOptions; + + /** The timeseries option to use when creating the model's collection. */ + timeseries?: mongodb.TimeSeriesCollectionOptions; + + /** The number of seconds after which a document in a timeseries collection expires. */ + expireAfterSeconds?: number; + + /** The time after which a document in a timeseries collection expires. */ + expires?: number | string; + + /** + * Mongoose by default produces a collection name by passing the model name to the utils.toCollectionName + * method. This method pluralizes the name. Set this option if you need a different name for your collection. + */ + collection?: string; + /** + * When you define a [discriminator](/docs/discriminators.html), Mongoose adds a path to your + * schema that stores which discriminator a document is an instance of. By default, Mongoose + * adds an `__t` path, but you can set `discriminatorKey` to overwrite this default. + * + * @default '__t' + */ + discriminatorKey?: string; + + /** + * Option for nested Schemas. + * + * If true, skip building indexes on this schema's path. + * + * @default false + */ + excludeIndexes?: boolean; + /** + * Mongoose assigns each of your schemas an id virtual getter by default which returns the document's _id field + * cast to a string, or in the case of ObjectIds, its hexString. + */ + id?: boolean; + /** + * Mongoose assigns each of your schemas an _id field by default if one is not passed into the Schema + * constructor. The type assigned is an ObjectId to coincide with MongoDB's default behavior. If you + * don't want an _id added to your schema at all, you may disable it using this option. + */ + _id?: boolean; + /** + * Mongoose will, by default, "minimize" schemas by removing empty objects. This behavior can be + * overridden by setting minimize option to false. It will then store empty objects. + */ + minimize?: boolean; + /** + * Optimistic concurrency is a strategy to ensure the document you're updating didn't change between when you + * loaded it using find() or findOne(), and when you update it using save(). Set to `true` to enable + * optimistic concurrency for all fields. Set to a string array to enable optimistic concurrency only for + * the specified fields; note that this **replaces** the default array versioning behavior. Set to + * `{ exclude: string[] }` to enable optimistic concurrency for all fields except the specified ones; + * this also **replaces** the default array versioning. + */ + optimisticConcurrency?: boolean | string[] | { exclude: string[] }; + /** + * If `plugin()` called with tags, Mongoose will only apply plugins to schemas that have + * a matching tag in `pluginTags` + */ + pluginTags?: string[]; + /** + * Allows setting query#read options at the schema level, providing us a way to apply default ReadPreferences + * to all queries derived from a model. + */ + read?: string; + /** + * Set a default readConcern for all queries at the schema level + */ + readConcern?: { level: 'local' | 'available' | 'majority' | 'snapshot' | 'linearizable' } + /** Allows setting write concern at the schema level. */ + writeConcern?: WriteConcern; + /** defaults to true. */ + safe?: boolean | { w?: number | string; wtimeout?: number; j?: boolean }; + /** + * The shardKey option is used when we have a sharded MongoDB architecture. Each sharded collection is + * given a shard key which must be present in all insert/update operations. We just need to set this + * schema option to the same shard key and we'll be all set. + */ + shardKey?: Record; + /** + * The strict option, (enabled by default), ensures that values passed to our model constructor that were not + * specified in our schema do not get saved to the db. + */ + strict?: boolean | 'throw'; + /** + * equal to `strict` by default, may be `false`, `true`, or `'throw'`. Sets the default + * [strictQuery](https://mongoosejs.com/docs/guide.html#strictQuery) mode for schemas. + */ + strictQuery?: boolean | 'throw'; + /** + * May be `false`, `true`, or `'throw'`. Sets the default + * [strictRead](https://mongoosejs.com/docs/guide.html#strictRead) mode for schemas. + */ + strictRead?: boolean | 'throw'; + /** Exactly the same as the toObject option but only applies when the document's toJSON method is called. */ + toJSON?: ToObjectOptions; + /** + * Documents have a toObject method which converts the mongoose document into a plain JavaScript object. + * This method accepts a few options. Instead of applying these options on a per-document basis, we may + * declare the options at the schema level and have them applied to all of the schema's documents by + * default. + */ + toObject?: ToObjectOptions; + /** + * By default, if you have an object with key 'type' in your schema, mongoose will interpret it as a + * type declaration. However, for applications like geoJSON, the 'type' property is important. If you want to + * control which key mongoose uses to find type declarations, set the 'typeKey' schema option. + */ + typeKey?: string; + + /** + * By default, documents are automatically validated before they are saved to the database. This is to + * prevent saving an invalid document. If you want to handle validation manually, and be able to save + * objects which don't pass validation, you can set validateBeforeSave to false. + */ + validateBeforeSave?: boolean; + /** + * By default, validation will run on modified and required paths before saving to the database. + * You can choose to have Mongoose only validate modified paths by setting validateModifiedOnly to true. + */ + validateModifiedOnly?: boolean; + /** + * The versionKey is a property set on each document when first created by Mongoose. This keys value + * contains the internal revision of the document. The versionKey option is a string that represents + * the path to use for versioning. The default is '__v'. + * + * @default '__v' + */ + versionKey?: string | false; + /** + * By default, Mongoose will automatically select() any populated paths for you, unless you explicitly exclude them. + * + * @default true + */ + selectPopulatedPaths?: boolean; + /** + * skipVersioning allows excluding paths from versioning (i.e., the internal revision will not be + * incremented even if these paths are updated). DO NOT do this unless you know what you're doing. + * For subdocuments, include this on the parent document using the fully qualified path. + */ + skipVersioning?: { [key: string]: boolean; }; + /** + * Validation errors in a single nested schema are reported + * both on the child and on the parent schema. + * Set storeSubdocValidationError to false on the child schema + * to make Mongoose only report the parent error. + */ + storeSubdocValidationError?: boolean; + /** + * The timestamps option tells mongoose to assign createdAt and updatedAt fields to your schema. The type + * assigned is Date. By default, the names of the fields are createdAt and updatedAt. Customize the + * field names by setting timestamps.createdAt and timestamps.updatedAt. + */ + timestamps?: boolean | SchemaTimestampsConfig; + + /** + * Using `save`, `isNew`, and other Mongoose reserved names as schema path names now triggers a warning, not an error. + * You can suppress the warning by setting { suppressReservedKeysWarning: true } schema options. Keep in mind that this + * can break plugins that rely on these reserved names. + */ + suppressReservedKeysWarning?: boolean, + + /** + * Model Statics methods. + */ + statics?: SchemaOptionsStaticsPropertyType + + /** + * Document instance methods. + */ + methods?: IfEquals< + TInstanceMethods, + {}, + Record unknown>, + AddThisParameter & AnyObject + > + + /** + * Query helper functions. + */ + query?: IfEquals< + QueryHelpers, + {}, + Record>(this: T, ...args: any) => T>, + QueryHelpers + > + + /** + * Set whether to cast non-array values to arrays. + * @default true + */ + castNonArrays?: boolean; + + /** + * Virtual paths. + */ + virtuals?: SchemaOptionsVirtualsPropertyType, + + /** + * Set to `true` to default to overwriting models with the same name when calling `mongoose.model()`, as opposed to throwing an `OverwriteModelError`. + * @default false + */ + overwriteModels?: boolean; + + /** + * Required when the schema is encrypted. + */ + encryptionType?: 'csfle' | 'queryableEncryption'; + } + + interface DefaultSchemaOptions { + typeKey: 'type'; + id: true; + _id: true; + discriminatorKey: '__t'; + timestamps: false; + versionKey: '__v' + } +} diff --git a/gateway/node_modules/mongoose/types/schematypes.d.ts b/gateway/node_modules/mongoose/types/schematypes.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..36e4d7ad4d3669d5b0b8ad7e996cbceed237eedb --- /dev/null +++ b/gateway/node_modules/mongoose/types/schematypes.d.ts @@ -0,0 +1,667 @@ +import { BSON } from 'mongodb'; + +declare module 'mongoose' { + /** The Mongoose Date [SchemaType](/docs/schematypes.html). */ + type Date = Schema.Types.Date; + + /** + * The Mongoose Decimal128 [SchemaType](/docs/schematypes.html). Used for + * declaring paths in your schema that should be + * [128-bit decimal floating points](http://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-34-decimal.html). + * Do not use this to create a new Decimal128 instance, use `mongoose.Types.Decimal128` + * instead. + */ + type Decimal128 = Schema.Types.Decimal128; + + /** + * The Mongoose Int32 [SchemaType](/docs/schematypes.html). Used for + * declaring paths in your schema that should be + * 32-bit integers + * Do not use this to create a new Int32 instance, use `mongoose.Types.Int32` + * instead. + */ + type Int32 = Schema.Types.Int32; + + /** + * The Mongoose Mixed [SchemaType](/docs/schematypes.html). Used for + * declaring paths in your schema that Mongoose's change tracking, casting, + * and validation should ignore. + */ + type Mixed = Schema.Types.Mixed; + + /** + * The Mongoose Number [SchemaType](/docs/schematypes.html). Used for + * declaring paths in your schema that Mongoose should cast to numbers. + */ + type Number = Schema.Types.Number; + + /** + * The Mongoose Double [SchemaType](/docs/schematypes.html). Used for + * declaring paths in your schema that Mongoose should cast to doubles (IEEE 754-2008)/ + */ + type Double = Schema.Types.Double; + + /** + * The Mongoose ObjectId [SchemaType](/docs/schematypes.html). Used for + * declaring paths in your schema that should be + * [MongoDB ObjectIds](https://www.mongodb.com/docs/manual/reference/method/ObjectId/). + * Do not use this to create a new ObjectId instance, use `mongoose.Types.ObjectId` + * instead. + */ + type ObjectId = Schema.Types.ObjectId; + + /** The various Mongoose SchemaTypes. */ + const SchemaTypes: typeof Schema.Types; + + type DefaultType = T extends Schema.Types.Mixed ? any : Partial>; + + class SchemaTypeOptions> { + type?: T extends string ? StringSchemaDefinition + : T extends number ? NumberSchemaDefinition + : T extends boolean ? BooleanSchemaDefinition + : T extends NativeDate ? DateSchemaDefinition + : T extends Map ? SchemaDefinition + : T extends Buffer ? SchemaDefinition + : T extends Types.ObjectId ? ObjectIdSchemaDefinition + : T extends Types.ObjectId[] ? + AnyArray | AnyArray> + : T extends object[] ? + | AnyArray> + | AnyArray, EnforcedDocType, THydratedDocumentType>> + | AnyArray, EnforcedDocType, THydratedDocumentType>> + : T extends string[] ? + AnyArray | AnyArray> + : T extends number[] ? + AnyArray | AnyArray> + : T extends boolean[] ? + AnyArray | AnyArray> + : T extends Function[] ? + AnyArray | AnyArray, EnforcedDocType, THydratedDocumentType>> + : T | typeof SchemaType | Schema | SchemaDefinition | Function | AnyArray; + + /** Defines a virtual with the given name that gets/sets this path. */ + alias?: string | string[]; + + /** Function or object describing how to validate this schematype. See [validation docs](https://mongoosejs.com/docs/validation.html). */ + validate?: SchemaValidator | AnyArray>; + + /** Allows overriding casting logic for this individual path. If a string, the given string overwrites Mongoose's default cast error message. */ + cast?: + | string + | boolean + | ((value: any) => T) + | [(value: any) => T, string] + | [((value: any) => T) | null, (value: any, path: string, model: Model, kind: string) => string]; + + /** + * If true, attach a required validator to this path, which ensures this path + * cannot be set to a nullish value. If a function, Mongoose calls the + * function and only checks for nullish values if the function returns a truthy value. + */ + required?: + | boolean + | ((this: THydratedDocumentType) => boolean) + | [boolean, string] + | [(this: THydratedDocumentType) => boolean, string]; + + /** + * Controls whether this path may be set to `null`. By default, Mongoose allows + * `null` for non-required paths. Set `allowNull: false` to allow `undefined` + * but disallow `null`. + */ + allowNull?: boolean; + + /** + * The default value for this path. If a function, Mongoose executes the function + * and uses the return value as the default. + */ + default?: DefaultType | ((this: THydratedDocumentType, doc: THydratedDocumentType) => DefaultType | null | undefined) | null | undefined; + + /** + * The model that `populate()` should use if populating this path. + */ + ref?: string | Model | ((this: any, doc: any) => string | Model); + + /** + * The path in the document that `populate()` should use to find the model + * to use. + */ + + refPath?: string | ((this: any, doc: any) => string); + + /** + * Whether to include or exclude this path by default when loading documents + * using `find()`, `findOne()`, etc. + */ + select?: boolean | number; + + /** + * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will + * build an index on this path when the model is compiled. + */ + index?: boolean | IndexDirection | IndexOptions; + + /** + * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose + * will build a unique index on this path when the + * model is compiled. [The `unique` option is **not** a validator](/docs/validation.html#the-unique-option-is-not-a-validator). + */ + unique?: boolean | number | [true, string]; + + /** + * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will + * disallow changes to this path once the document is saved to the database for the first time. Read more + * about [immutability in Mongoose here](http://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html). + */ + immutable?: boolean | ((this: any, doc: any) => boolean); + + /** + * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will + * build a sparse index on this path. + */ + sparse?: boolean | number; + + /** + * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose + * will build a text index on this path. + */ + text?: boolean | number | any; + + /** + * Define a transform function for this individual schema type. + * Only called when calling `toJSON()` or `toObject()`. + */ + transform?: (this: any, val: T) => any; + + /** defines a custom getter for this property using [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty). */ + get?: (value: any, doc?: this) => T | undefined; + + /** defines a custom setter for this property using [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty). */ + set?: (value: any, priorVal?: T, doc?: this) => any; + + /** array of allowed values for this path. Allowed for strings, numbers, and arrays of strings */ + enum?: + | Array + | ReadonlyArray + | { values: Array | ReadonlyArray; message?: string } + | { [path: string]: string | number | null }; + + /** The default [subtype](http://bsonspec.org/spec.html) associated with this buffer when it is stored in MongoDB. Only allowed for buffer paths */ + subtype?: number; + + /** The minimum value allowed for this path. Only allowed for numbers and dates. */ + min?: + | number + | NativeDate + | [number, string] + | [NativeDate, string] + | readonly [number, string] + | readonly [NativeDate, string]; + + /** The maximum value allowed for this path. Only allowed for numbers and dates. */ + max?: + | number + | NativeDate + | [number, string] + | [NativeDate, string] + | readonly [number, string] + | readonly [NativeDate, string]; + + /** Set to false to disable minimizing empty single nested subdocuments by default */ + minimize?: boolean; + + /** Defines a TTL index on this path. Only allowed for dates. */ + expires?: string | number; + + /** If `true`, Mongoose will skip gathering indexes on subpaths. Only allowed for subdocuments and subdocument arrays. */ + excludeIndexes?: boolean; + + /** If set, overrides the child schema's `_id` option. Only allowed for subdocuments and subdocument arrays. */ + _id?: boolean; + + /** Embedded discriminators for this path. Only allowed for subdocuments and subdocument arrays. */ + discriminators?: Record>; + + /** If set, specifies the type of this map's values. Mongoose will cast this map's values to the given type. */ + of?: Function | SchemaDefinitionProperty | (Function | SchemaDefinitionProperty)[]; + + /** If true, uses Mongoose's default `_id` settings. Only allowed for ObjectIds */ + auto?: boolean; + + /** Attaches a validator that succeeds if the data string matches the given regular expression, and fails otherwise. */ + match?: RegExp | [RegExp, string] | readonly [RegExp, string]; + + /** If truthy, Mongoose will add a [custom setter](https://mongoosejs.com/docs/api/schematype.html#SchemaType.prototype.set()) that lowercases this string using JavaScript's built-in `String#toLowerCase()`. */ + lowercase?: boolean; + + /** If truthy, Mongoose will add a [custom setter](https://mongoosejs.com/docs/api/schematype.html#SchemaType.prototype.set()) that removes leading and trailing whitespace using JavaScript's built-in `String#trim()`. */ + trim?: boolean; + + /** If truthy, Mongoose will add a [custom setter](https://mongoosejs.com/docs/api/schematype.html#SchemaType.prototype.set()) that uppercases this string using JavaScript's built-in `String#toUpperCase()`. */ + uppercase?: boolean; + + /** If set, Mongoose will add a custom validator that ensures the given string's `length` is at least the given number. */ + minlength?: number | [number, string] | readonly [number, string]; + minLength?: number | [number, string] | readonly [number, string]; + + /** If set, Mongoose will add a custom validator that ensures the given string's `length` is at most the given number. */ + maxlength?: number | [number, string] | readonly [number, string]; + maxLength?: number | [number, string] | readonly [number, string]; + + [other: string]: any; + + /** + * If set, configures the field for automatic encryption. + */ + encrypt?: EncryptSchemaTypeOptions; + } + + interface Validator { + message?: string | ((props: ValidatorProps) => string); + type?: string; + validator?: ValidatorFunction; + reason?: Error; + } + + type ValidatorFunction = (this: DocType, value: any, validatorProperties?: Validator) => any; + + interface QueryEncryptionEncryptOptions { + /** The id of the dataKey to use for encryption. Must be a BSON binary subtype 4 (UUID). */ + keyId: BSON.Binary; + + /** + * Specifies the type of queries that the field can be queried on the encrypted field. + */ + queries?: 'equality' | 'range'; + } + + interface ClientSideEncryptionEncryptOptions { + /** The id of the dataKey to use for encryption. Must be a BSON binary subtype 4 (UUID). */ + keyId: [BSON.Binary]; + + /** + * The algorithm to use for encryption. + */ + algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' | 'AEAD_AES_256_CBC_HMAC_SHA_512-Random'; + } + + export type EncryptSchemaTypeOptions = QueryEncryptionEncryptOptions | ClientSideEncryptionEncryptOptions; + + class SchemaType { + /** SchemaType constructor */ + constructor(path: string, options?: AnyObject, instance?: string); + + /** Get/set the function used to cast arbitrary values to this type. */ + static cast(caster?: Function | boolean): Function; + + static checkRequired(checkRequired?: (v: any) => boolean): (v: any) => boolean; + + /** Sets a default option for this schema type. */ + static set(option: string, value: any): void; + + /** Attaches a getter for all instances of this schema type. */ + static get(getter: (value: any) => any): void; + + /** Array containing default setters for all instances of this SchemaType */ + static setters: (( + val?: unknown, + priorVal?: unknown, + doc?: Document, + options?: Record | null + ) => unknown)[]; + + /** Contains the handlers for different query operators for this schema type. */ + $conditionalHandlers: { [op: string]: (val: any, context: any) => any }; + + /** The class that Mongoose uses internally to instantiate this SchemaType's `options` property. */ + OptionsConstructor: SchemaTypeOptions; + + /** Cast `val` to this schema type. Each class that inherits from schema type should implement this function. */ + cast(val: any, doc?: Document, init?: boolean, prev?: any, options?: any): any; + cast(val: any, doc?: Document, init?: boolean, prev?: any, options?: any): ResultType; + + /** Sets a default value for this SchemaType. */ + default(val: any): any; + + /** Adds a getter to this schematype. */ + get(fn: Function): this; + + /** Gets this SchemaType's embedded SchemaType, if any */ + getEmbeddedSchemaType(): SchemaType | undefined; + + /** + * Defines this path as immutable. Mongoose prevents you from changing + * immutable paths unless the parent document has [`isNew: true`](/docs/api/document.html#document_Document-isNew). + */ + immutable(bool: boolean): this; + + /** Declares the index options for this schematype. */ + index(options: any): this; + + /** String representation of what type this is, like 'ObjectID' or 'Number' */ + instance: string; + + /** True if this SchemaType has a required validator. False otherwise. */ + isRequired?: boolean; + + /** The options this SchemaType was instantiated with */ + options: AnyObject; + + /** The path to this SchemaType in a Schema. */ + path: string; + + /** + * Set the model that this path refers to. This is the option that [populate](https://mongoosejs.com/docs/populate.html) + * looks at to determine the foreign collection it should query. + */ + ref(ref: string | boolean | Model): this; + + /** + * Adds a required validator to this SchemaType. The validator gets added + * to the front of this SchemaType's validators array using unshift(). + */ + required(required: boolean, message?: string): this; + + /** Adds or removes a validator that disallows `null` without making this path required. */ + allowNull(allowNull: boolean): this; + + /** If the SchemaType is a subdocument or document array, this is the schema of that subdocument */ + schema?: Schema; + + /** Sets default select() behavior for this path. */ + select(val: boolean): this; + + /** Adds a setter to this schematype. */ + set(fn: Function): this; + + /** Declares a sparse index. */ + sparse(bool: boolean): this; + + /** Declares a full text index. */ + text(bool: boolean): this; + + toJSONSchema(options?: { useBsonType?: boolean }): Record; + + /** Defines a custom function for transforming this path when converting a document to JSON. */ + transform(fn: (value: any) => any): this; + + /** Declares an unique index. */ + unique(bool: boolean): this; + + /** The validators that Mongoose should run to validate properties at this SchemaType's path. */ + validators: Validator[]; + + /** Adds validator(s) for this document path. */ + validate(obj: RegExp | ValidatorFunction | Validator, errorMsg?: string, type?: string): this; + + /** Adds multiple validators for this document path. */ + validateAll(validators: Array | Validator>): this; + + /** Default options for this SchemaType */ + static defaultOptions?: Record; + } + + namespace Schema { + namespace Types { + class Array extends SchemaType implements AcceptsDiscriminator { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'Array'; + readonly schemaName: 'Array'; + + static options: { castNonArrays: boolean }; + + discriminator(name: string | number, schema: Schema, value?: string): U; + discriminator(name: string | number, schema: Schema, value?: string): Model; + + /** The schematype embedded in this array */ + embeddedSchemaType: SchemaType; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + + /** + * Adds an enum validator if this is an array of strings or numbers. Equivalent to + * `SchemaString.prototype.enum()` or `SchemaNumber.prototype.enum()` + */ + enum(vals: string[] | number[]): this; + } + + class BigInt extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'BigInt'; + readonly schemaName: 'BigInt'; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class Boolean extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'Boolean'; + readonly schemaName: 'Boolean'; + + /** Configure which values get casted to `true`. */ + static convertToTrue: Set; + + /** Configure which values get casted to `false`. */ + static convertToFalse: Set; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class Buffer extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'Buffer'; + readonly schemaName: 'Buffer'; + + /** + * Sets the default [subtype](https://studio3t.com/whats-new/best-practices-uuid-mongodb/) + * for this buffer. You can find a [list of allowed subtypes here](http://api.mongodb.com/python/current/api/bson/binary.html). + */ + subtype(subtype: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 128): this; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class Date extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'Date'; + readonly schemaName: 'Date'; + + /** Declares a TTL index (rounded to the nearest second) for _Date_ types only. */ + expires(when: number | string): this; + + /** Sets a maximum date validator. */ + max(value: NativeDate, message?: string): this; + + /** Sets a minimum date validator. */ + min(value: NativeDate, message?: string): this; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class Decimal128 extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'Decimal128'; + readonly schemaName: 'Decimal128'; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class Int32 extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'Int32'; + readonly schemaName: 'Int32'; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class DocumentArray extends SchemaType implements AcceptsDiscriminator { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'DocumentArray'; + readonly schemaName: 'DocumentArray'; + + static options: { castNonArrays: boolean }; + + schemaOptions?: SchemaOptions; + + discriminator(name: string | number, schema: Schema, value?: string): Model; + discriminator(name: string | number, schema: Schema, value?: string): U; + + /** The schema used for documents in this array */ + schema: Schema; + + /** The schematype embedded in this array */ + embeddedSchemaType: Subdocument; + + /** The constructor used for subdocuments in this array */ + Constructor: typeof Types.Subdocument; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class DocumentArrayElement extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'DocumentArrayElement'; + readonly schemaName: 'DocumentArrayElement'; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class Map extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'Map'; + readonly schemaName: 'Map'; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class Mixed extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'Mixed'; + readonly schemaName: 'Mixed'; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class Number extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'Number'; + readonly schemaName: 'Number'; + + /** Sets a enum validator */ + enum(vals: number[]): this; + + /** Sets a maximum number validator. */ + max(value: number, message?: string): this; + + /** Sets a minimum number validator. */ + min(value: number, message?: string): this; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class Double extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'Double'; + readonly schemaName: 'Double'; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class ObjectId extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'ObjectId'; + readonly schemaName: 'ObjectId'; + + /** Adds an auto-generated ObjectId default if turnOn is true. */ + auto(turnOn: boolean): this; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class Subdocument extends SchemaType implements AcceptsDiscriminator { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: string; + + /** The document's schema */ + schema: Schema; + + /** The constructor used to create subdocuments based on this schematype */ + Constructor: typeof Types.Subdocument; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + + discriminator(name: string | number, schema: Schema, value?: string): U; + discriminator(name: string | number, schema: Schema, value?: string): Model; + + cast( + val: any, + doc?: Document, + init?: boolean, + prev?: any, + options?: any + ): HydratedSingleSubdocument; + } + + class String extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'String'; + readonly schemaName: 'String'; + + /** Adds an enum validator */ + enum(vals: string[] | any): this; + + /** Adds a lowercase [setter](http://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set). */ + lowercase(shouldApply?: boolean): this; + + /** Sets a regexp validator. */ + match(value: RegExp, message: string): this; + + /** Sets a maximum length validator. */ + maxlength(value: number, message: string): this; + + /** Sets a minimum length validator. */ + minlength(value: number, message: string): this; + + /** Adds a trim [setter](http://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set). */ + trim(shouldTrim?: boolean): this; + + /** Adds an uppercase [setter](http://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set). */ + uppercase(shouldApply?: boolean): this; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class Union extends SchemaType { + static schemaName: 'Union'; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + + class UUID extends SchemaType { + /** This schema type's name, to defend against minifiers that mangle function names. */ + static schemaName: 'UUID'; + readonly schemaName: 'UUID'; + + /** Default options for this SchemaType */ + static defaultOptions: Record; + } + } + } +} diff --git a/gateway/node_modules/mongoose/types/session.d.ts b/gateway/node_modules/mongoose/types/session.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..e7f41f7376d3bf605d22207be3146f4870db761b --- /dev/null +++ b/gateway/node_modules/mongoose/types/session.d.ts @@ -0,0 +1,32 @@ +declare module 'mongoose' { + import mongodb = require('mongodb'); + + type ClientSessionOptions = mongodb.ClientSessionOptions; + type ClientSession = mongodb.ClientSession; + + /** + * _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://www.mongodb.com/docs/manual/release-notes/3.6/#client-sessions) + * for benefits like causal consistency, [retryable writes](https://www.mongodb.com/docs/manual/core/retryable-writes/), + * and [transactions](http://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html). + */ + function startSession(options?: ClientSessionOptions): Promise; + + interface SessionOperation { + /** Sets the session. Useful for [transactions](/docs/transactions.html). */ + session(session: mongodb.ClientSession | null): this; + } + + interface SessionStarter { + + /** + * Starts a [MongoDB session](https://www.mongodb.com/docs/manual/release-notes/3.6/#client-sessions) + * for benefits like causal consistency, [retryable writes](https://www.mongodb.com/docs/manual/core/retryable-writes/), + * and [transactions](http://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html). + */ + startSession(options?: ClientSessionOptions): Promise; + } + + interface SessionOption { + session?: ClientSession | null; + } +} diff --git a/gateway/node_modules/mongoose/types/tracing.d.ts b/gateway/node_modules/mongoose/types/tracing.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..9a12cb20722daa741d9cdc043c65e1367033d6b3 --- /dev/null +++ b/gateway/node_modules/mongoose/types/tracing.d.ts @@ -0,0 +1,10 @@ +declare module 'mongoose' { + interface TracingContext { + operation: string; + collection: string; + database: string; + serverAddress: string; + serverPort: number; + args: Record; + } +} diff --git a/gateway/node_modules/mongoose/types/types.d.ts b/gateway/node_modules/mongoose/types/types.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a15ab99c5354a906c938cce3c3d455f308afff3 --- /dev/null +++ b/gateway/node_modules/mongoose/types/types.d.ts @@ -0,0 +1,109 @@ + +declare module 'mongoose' { + import mongodb = require('mongodb'); + + class NativeBuffer extends Buffer {} + + namespace Types { + class Array extends global.Array { + /** Pops the array atomically at most one time per document `save()`. */ + $pop(): T; + + /** Atomically shifts the array at most one time per document `save()`. */ + $shift(): T; + + /** Adds values to the array if not already present. */ + addToSet(...args: any[]): any[]; + + isMongooseArray: true; + + /** Pushes items to the array non-atomically. */ + nonAtomicPush(...args: any[]): number; + + /** Wraps [`Array#push`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push) with proper change tracking. */ + push(...args: any[]): number; + + /** + * Pulls items from the array atomically. Equality is determined by casting + * the provided value to an embedded document and comparing using + * [the `Document.equals()` function.](./api/document.html#document_Document-equals) + */ + pull(...args: any[]): this; + + /** + * Alias of [pull](#mongoosearray_MongooseArray-pull) + */ + remove(...args: any[]): this; + + /** Sets the casted `val` at index `i` and marks the array modified. */ + set(index: number, val: T): this; + + /** Atomically shifts the array at most one time per document `save()`. */ + shift(): T; + + /** Returns a native js Array. */ + toObject(options?: ToObjectOptions): any; + toObject(options?: ToObjectOptions): T; + + /** Wraps [`Array#unshift`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/unshift) with proper change tracking. */ + unshift(...args: any[]): number; + } + + class Buffer extends NativeBuffer { + /** Sets the subtype option and marks the buffer modified. */ + subtype(subtype: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 128 | ToObjectOptions): void; + + /** Converts this buffer to its Binary type representation. */ + toObject(subtype?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 128): mongodb.Binary; + } + + class Decimal128 extends mongodb.Decimal128 { } + + class DocumentArray = Types.Subdocument, unknown, T> & T> extends Types.Array { + /** DocumentArray constructor */ + constructor(values: AnyObject[]); + + isMongooseDocumentArray: true; + + /** Creates a subdocument casted to this schema. */ + create(obj: any): THydratedDocumentType; + + /** Searches array items for the first document with a matching _id. */ + id(id: ObjectId | string | number | Buffer): THydratedDocumentType | null; + + push(...args: (AnyKeys & AnyObject)[]): number; + + splice(start: number, deleteCount?: number, ...args: (AnyKeys & AnyObject)[]): THydratedDocumentType[]; + } + + class Map extends global.Map { + /** Converts a Mongoose map into a vanilla JavaScript map. */ + toObject(options?: ToObjectOptions & { flattenMaps?: boolean }): any; + } + + class ObjectId extends mongodb.ObjectId { + } + + class Subdocument extends Document { + $isSingleNested: true; + + /** Returns the top level document of this sub-document. */ + ownerDocument(): Document; + + /** Returns this sub-documents parent document. */ + parent(): Document; + + /** Returns this sub-documents parent document. */ + $parent(): Document; + } + + class ArraySubdocument extends Subdocument { + /** Returns this sub-documents parent array. */ + parentArray(): Types.DocumentArray; + } + + class UUID extends mongodb.UUID {} + + class Double extends mongodb.Double {} + } +} diff --git a/gateway/node_modules/mongoose/types/utility.d.ts b/gateway/node_modules/mongoose/types/utility.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..b93736e7ecd1b1dbaef94be7d44263e29bc0ade8 --- /dev/null +++ b/gateway/node_modules/mongoose/types/utility.d.ts @@ -0,0 +1,184 @@ +declare module 'mongoose' { + type IfAny = 0 extends 1 & IFTYPE + ? THENTYPE + : ELSETYPE; + type IsUnknown = unknown extends T ? true : false; + + type IsNotNever = [T] extends [never] ? false : true; + type IsAny = 0 extends 1 & T ? true : false; + + type WithLevel1NestedPaths = IsItRecordAndNotAny extends true ? { + [P in K | NestedPaths, K>]: P extends K + // Handle top-level paths + // First, drill into documents so we don't end up surfacing `$assertPopulated`, etc. + ? Extract, Document> extends never + // If not a document, then return the type. Otherwise, get the DocType. + ? NonNullable + : Extract, Document> extends Document + ? DocType | Exclude, Document> + : never + // Handle nested paths + : P extends `${infer Key}.${infer Rest}` + ? Key extends keyof T + ? NonNullable extends (infer U)[] + ? NonNullable extends Types.DocumentArray + ? Rest extends keyof NonNullable + ? NonNullable[Rest] + : never + : Rest extends keyof NonNullable + ? NonNullable[Rest] + : never + : Rest extends keyof NonNullable + ? NonNullable[Rest] + : never + : never + : never; + } : T; + + type HasStringIndex = + string extends Extract ? true : false; + + type SafeObjectKeys = + HasStringIndex extends true ? never : Extract; + + type NestedPaths = + K extends string + ? T[K] extends TreatAsPrimitives + ? never + : Extract, Document> extends never + ? NonNullable extends Array + ? NonNullable extends Types.DocumentArray + ? SafeObjectKeys> extends never + ? never + : `${K}.${SafeObjectKeys>}` + : NonNullable extends Record + ? SafeObjectKeys> extends never + ? never + : `${K}.${SafeObjectKeys>}` + : never + : NonNullable extends object + ? SafeObjectKeys> extends never + ? never + : `${K}.${SafeObjectKeys>}` + : never + : Extract, Document> extends Document + ? DocType extends object + ? SafeObjectKeys> extends never + ? never + : `${K}.${SafeObjectKeys>}` + : never + : never + : never; + + type WithoutUndefined = T extends undefined ? never : T; + + /** + * @summary Removes keys from a type + * @description It helps to exclude keys from a type + * @param {T} T A generic type to be checked. + * @param {K} K Keys from T that are to be excluded from the generic type + * @returns T with the keys in K excluded + */ + type ExcludeKeys = { + [P in keyof T as P extends K ? never : P]: T[P]; + }; + + type Unpacked = T extends (infer U)[] + ? U + : T extends ReadonlyArray + ? U + : T; + + type DeepPartial = + T extends TreatAsPrimitives ? T : + T extends Array ? DeepPartial[] : + T extends Record ? { [K in keyof T]?: DeepPartial } : + T; + + type UnpackedIntersection = T extends null + ? null + : T extends (infer A)[] + ? (A extends any ? (Omit & U) : never)[] + : keyof U extends never + ? T + : T extends any ? (Omit & U) : never; + + type MergeType = A extends unknown ? Omit & B : never; + + /** + * @summary Converts Unions to one record "object". + * @description It makes intellisense dialog box easier to read as a single object instead of showing that in multiple object unions. + * @param {T} T The type to be converted. + */ + type FlatRecord = { [K in keyof T]: T[K] }; + + /** Force an operation like `{ a: 0 } & { b: 1 }` to be computed so that it displays `{ a: 0; b: 1 }`. */ + export type Show = { [k in keyof t]: t[k] } & unknown; + + /** + * @summary Checks if a type is "Record" or "any". + * @description It Helps to check if user has provided schema type "EnforcedDocType" + * @param {T} T A generic type to be checked. + * @returns true if {@link T} is Record OR false if {@link T} is of any type. + */ + type IsItRecordAndNotAny = IfEquals< + T, + any, + false, + T extends Record ? true : false + >; + + /** + * @summary Checks if two types are identical. + * @param {T} T The first type to be compared with {@link U}. + * @param {U} U The second type to be compared with {@link T}. + * @param {Y} Y A type to be returned if {@link T} & {@link U} are identical. + * @param {N} N A type to be returned if {@link T} & {@link U} are not identical. + */ + type IfEquals = (() => G extends T + ? 1 + : 0) extends () => G extends U ? 1 : 0 + ? Y + : N; + + /** + * @summary Extracts 'this' parameter from a function, if it exists. Otherwise, returns fallback. + * @param {T} T Function type to extract 'this' parameter from. + * @param {F} F Fallback type to return if 'this' parameter does not exist. + */ + type ThisParameter = T extends { (this: infer This, ...args: never): void } ? This : F; + + /** + * @summary Decorates all functions in an object with 'this' parameter. + * @param {T} T Object with functions as values to add 'D' parameter to as 'this'. {@link D} + * @param {D} D The type to be added as 'this' parameter to all functions in {@link T}. + */ + type AddThisParameter = { + [K in keyof T]: T[K] extends (...args: infer A) => infer R + ? IsUnknown> extends true + ? (this: D, ...args: A) => R + : T[K] + : T[K]; + }; + + type OmitThisParameterIfFunction = T extends (...args: any[]) => any ? OmitThisParameter : T; + + // Strip explicit `this` parameter from methods in schema type options. The `this` parameter + // is just for type-checking the function body. Keeping the explicit 'this' typing can cause + // compiler errors on the method calls, so remove it for the hydrated document definition. + type HydratedDocumentOverrides = { + [K in keyof T]: OmitThisParameterIfFunction; + }; + + /** + * @summary Adds timestamp fields to a type + * @description Adds createdAt and updatedAt fields of type Date, or custom timestamp fields if specified + * @param {T} T The type to add timestamp fields to + * @param {P} P Optional SchemaTimestampsConfig or boolean to customize timestamp field names + * @returns T with timestamp fields added + */ + export type WithTimestamps< + T, + P extends SchemaTimestampsConfig | boolean = true + > = ResolveTimestamps; +} diff --git a/gateway/node_modules/mongoose/types/validation.d.ts b/gateway/node_modules/mongoose/types/validation.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..1300afa732941bcb5d0aff8c5ddc1b68b6d535bf --- /dev/null +++ b/gateway/node_modules/mongoose/types/validation.d.ts @@ -0,0 +1,39 @@ +declare module 'mongoose' { + type SchemaValidator = RegExp | + [RegExp, string] | + Function | + [Function, string] | + ValidateOpts | + ValidateOpts[]; + + interface ValidatorProps { + path: string; + fullPath: string; + value: any; + reason?: Error; + } + + interface ValidatorMessageFn { + (props: ValidatorProps): string; + } + + type ValidateFn = ( + this: THydratedDocumentType | Query, + value: any, + props?: ValidatorProps & Record + ) => boolean; + + type AsyncValidateFn = ( + this: THydratedDocumentType | Query, + value: any, + props?: ValidatorProps & Record + ) => Promise; + + interface ValidateOpts { + msg?: string; + message?: string | ValidatorMessageFn; + type?: string; + validator: ValidateFn | AsyncValidateFn; + propsParameter?: boolean; + } +} diff --git a/gateway/node_modules/mongoose/types/virtuals.d.ts b/gateway/node_modules/mongoose/types/virtuals.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..de0083091bdd9e434b9bbff68dc96291cc7c9e21 --- /dev/null +++ b/gateway/node_modules/mongoose/types/virtuals.d.ts @@ -0,0 +1,14 @@ +declare module 'mongoose' { + type VirtualPathFunctions = { + get?: TVirtualPathFN; + set?: TVirtualPathFN; + options?: VirtualTypeOptions; + }; + + type TVirtualPathFN = + (value: PathType, virtual: VirtualType, doc: THydratedDocumentType) => TReturn; + + type SchemaOptionsVirtualsPropertyType, THydratedDocumentType = any> = { + [K in keyof VirtualPaths]: VirtualPathFunctions + } & ThisType; +} diff --git a/gateway/node_modules/mpath/.travis.yml b/gateway/node_modules/mpath/.travis.yml new file mode 100644 index 0000000000000000000000000000000000000000..9bdf212d6566f94c9ca3b1eb3185da309fce0588 --- /dev/null +++ b/gateway/node_modules/mpath/.travis.yml @@ -0,0 +1,9 @@ +language: node_js +node_js: + - "4" + - "5" + - "6" + - "7" + - "8" + - "9" + - "10" diff --git a/gateway/node_modules/mpath/History.md b/gateway/node_modules/mpath/History.md new file mode 100644 index 0000000000000000000000000000000000000000..5235d689df81c1cd3119c72d2901ef134d69a54b --- /dev/null +++ b/gateway/node_modules/mpath/History.md @@ -0,0 +1,88 @@ +0.9.0 / 2022-04-17 +================== + * feat: export `stringToParts()` + +0.8.4 / 2021-09-01 +================== + * fix: throw error if `parts` contains an element that isn't a string or number #13 + +0.8.3 / 2020-12-30 +================== + * fix: use var instead of let/const for Node.js 4.x support + +0.8.2 / 2020-12-30 +================== + * fix(stringToParts): fall back to legacy treatment for square brackets if square brackets contents aren't a number Automattic/mongoose#9640 + * chore: add eslint + +0.8.1 / 2020-12-10 +================== + * fix(stringToParts): handle empty string and trailing dot the same way that `split()` does for backwards compat + +0.8.0 / 2020-11-14 +================== + * feat: support square bracket indexing for `get()`, `set()`, `has()`, and `unset()` + +0.7.0 / 2020-03-24 +================== + * BREAKING CHANGE: remove `component.json` #9 [AlexeyGrigorievBoost](https://github.com/AlexeyGrigorievBoost) + +0.6.0 / 2019-05-01 +================== + * feat: support setting dotted paths within nested arrays + +0.5.2 / 2019-04-25 +================== + * fix: avoid using subclassed array constructor when doing `map()` + +0.5.1 / 2018-08-30 +================== + * fix: prevent writing to constructor and prototype as well as __proto__ + +0.5.0 / 2018-08-30 +================== + * BREAKING CHANGE: disallow setting/unsetting __proto__ properties + * feat: re-add support for Node < 4 for this release + +0.4.1 / 2018-04-08 +================== + * fix: allow opting out of weird `$` set behavior re: Automattic/mongoose#6273 + +0.4.0 / 2018-03-27 +================== + * feat: add support for ES6 maps + * BREAKING CHANGE: drop support for Node < 4 + +0.3.0 / 2017-06-05 +================== + * feat: add has() and unset() functions + +0.2.1 / 2013-03-22 +================== + + * test; added for #5 + * fix typo that breaks set #5 [Contra](https://github.com/Contra) + +0.2.0 / 2013-03-15 +================== + + * added; adapter support for set + * added; adapter support for get + * add basic benchmarks + * add support for using module as a component #2 [Contra](https://github.com/Contra) + +0.1.1 / 2012-12-21 +================== + + * added; map support + +0.1.0 / 2012-12-13 +================== + + * added; set('array.property', val, object) support + * added; get('array.property', object) support + +0.0.1 / 2012-11-03 +================== + + * initial release diff --git a/gateway/node_modules/mpath/LICENSE b/gateway/node_modules/mpath/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..38c529daa677faf963fc24fcd00cdb5b61f07102 --- /dev/null +++ b/gateway/node_modules/mpath/LICENSE @@ -0,0 +1,22 @@ +(The MIT License) + +Copyright (c) 2012 [Aaron Heckmann](aaron.heckmann+github@gmail.com) + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/gateway/node_modules/mpath/README.md b/gateway/node_modules/mpath/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9831dd066c482a7350e9acdb16ee266394951dad --- /dev/null +++ b/gateway/node_modules/mpath/README.md @@ -0,0 +1,278 @@ +#mpath + +{G,S}et javascript object values using MongoDB-like path notation. + +###Getting + +```js +var mpath = require('mpath'); + +var obj = { + comments: [ + { title: 'funny' }, + { title: 'exciting!' } + ] +} + +mpath.get('comments.1.title', obj) // 'exciting!' +``` + +`mpath.get` supports array property notation as well. + +```js +var obj = { + comments: [ + { title: 'funny' }, + { title: 'exciting!' } + ] +} + +mpath.get('comments.title', obj) // ['funny', 'exciting!'] +``` + +Array property and indexing syntax, when used together, are very powerful. + +```js +var obj = { + array: [ + { o: { array: [{x: {b: [4,6,8]}}, { y: 10} ] }} + , { o: { array: [{x: {b: [1,2,3]}}, { x: {z: 10 }}, { x: 'Turkey Day' }] }} + , { o: { array: [{x: {b: null }}, { x: { b: [null, 1]}}] }} + , { o: { array: [{x: null }] }} + , { o: { array: [{y: 3 }] }} + , { o: { array: [3, 0, null] }} + , { o: { name: 'ha' }} + ]; +} + +var found = mpath.get('array.o.array.x.b.1', obj); + +console.log(found); // prints.. + + [ [6, undefined] + , [2, undefined, undefined] + , [null, 1] + , [null] + , [undefined] + , [undefined, undefined, undefined] + , undefined + ] + +``` + +#####Field selection rules: + +The following rules are iteratively applied to each `segment` in the passed `path`. For example: + +```js +var path = 'one.two.14'; // path +'one' // segment 0 +'two' // segment 1 +14 // segment 2 +``` + +- 1) when value of the segment parent is not an array, return the value of `parent.segment` +- 2) when value of the segment parent is an array + - a) if the segment is an integer, replace the parent array with the value at `parent[segment]` + - b) if not an integer, keep the array but replace each array `item` with the value returned from calling `get(remainingSegments, item)` or undefined if falsey. + +#####Maps + +`mpath.get` also accepts an optional `map` argument which receives each individual found value. The value returned from the `map` function will be used in the original found values place. + +```js +var obj = { + comments: [ + { title: 'funny' }, + { title: 'exciting!' } + ] +} + +mpath.get('comments.title', obj, function (val) { + return 'funny' == val + ? 'amusing' + : val; +}); +// ['amusing', 'exciting!'] +``` + +###Setting + +```js +var obj = { + comments: [ + { title: 'funny' }, + { title: 'exciting!' } + ] +} + +mpath.set('comments.1.title', 'hilarious', obj) +console.log(obj.comments[1].title) // 'hilarious' +``` + +`mpath.set` supports the same array property notation as `mpath.get`. + +```js +var obj = { + comments: [ + { title: 'funny' }, + { title: 'exciting!' } + ] +} + +mpath.set('comments.title', ['hilarious', 'fruity'], obj); + +console.log(obj); // prints.. + + { comments: [ + { title: 'hilarious' }, + { title: 'fruity' } + ]} +``` + +Array property and indexing syntax can be used together also when setting. + +```js +var obj = { + array: [ + { o: { array: [{x: {b: [4,6,8]}}, { y: 10} ] }} + , { o: { array: [{x: {b: [1,2,3]}}, { x: {z: 10 }}, { x: 'Turkey Day' }] }} + , { o: { array: [{x: {b: null }}, { x: { b: [null, 1]}}] }} + , { o: { array: [{x: null }] }} + , { o: { array: [{y: 3 }] }} + , { o: { array: [3, 0, null] }} + , { o: { name: 'ha' }} + ] +} + +mpath.set('array.1.o', 'this was changed', obj); + +console.log(require('util').inspect(obj, false, 1000)); // prints.. + +{ + array: [ + { o: { array: [{x: {b: [4,6,8]}}, { y: 10} ] }} + , { o: 'this was changed' } + , { o: { array: [{x: {b: null }}, { x: { b: [null, 1]}}] }} + , { o: { array: [{x: null }] }} + , { o: { array: [{y: 3 }] }} + , { o: { array: [3, 0, null] }} + , { o: { name: 'ha' }} + ]; +} + +mpath.set('array.o.array.x', 'this was changed too', obj); + +console.log(require('util').inspect(obj, false, 1000)); // prints.. + +{ + array: [ + { o: { array: [{x: 'this was changed too'}, { y: 10, x: 'this was changed too'} ] }} + , { o: 'this was changed' } + , { o: { array: [{x: 'this was changed too'}, { x: 'this was changed too'}] }} + , { o: { array: [{x: 'this was changed too'}] }} + , { o: { array: [{x: 'this was changed too', y: 3 }] }} + , { o: { array: [3, 0, null] }} + , { o: { name: 'ha' }} + ]; +} +``` + +####Setting arrays + +By default, setting a property within an array to another array results in each element of the new array being set to the item in the destination array at the matching index. An example is helpful. + +```js +var obj = { + comments: [ + { title: 'funny' }, + { title: 'exciting!' } + ] +} + +mpath.set('comments.title', ['hilarious', 'fruity'], obj); + +console.log(obj); // prints.. + + { comments: [ + { title: 'hilarious' }, + { title: 'fruity' } + ]} +``` + +If we do not desire this destructuring-like assignment behavior we may instead specify the `$` operator in the path being set to force the array to be copied directly. + +```js +var obj = { + comments: [ + { title: 'funny' }, + { title: 'exciting!' } + ] +} + +mpath.set('comments.$.title', ['hilarious', 'fruity'], obj); + +console.log(obj); // prints.. + + { comments: [ + { title: ['hilarious', 'fruity'] }, + { title: ['hilarious', 'fruity'] } + ]} +``` + +####Field assignment rules + +The rules utilized mirror those used on `mpath.get`, meaning we can take values returned from `mpath.get`, update them, and reassign them using `mpath.set`. Note that setting nested arrays of arrays can get unweildy quickly. Check out the [tests](https://github.com/aheckmann/mpath/blob/master/test/index.js) for more extreme examples. + +#####Maps + +`mpath.set` also accepts an optional `map` argument which receives each individual value being set. The value returned from the `map` function will be used in the original values place. + +```js +var obj = { + comments: [ + { title: 'funny' }, + { title: 'exciting!' } + ] +} + +mpath.set('comments.title', ['hilarious', 'fruity'], obj, function (val) { + return val.length; +}); + +console.log(obj); // prints.. + + { comments: [ + { title: 9 }, + { title: 6 } + ]} +``` + +### Custom object types + +Sometimes you may want to enact the same functionality on custom object types that store all their real data internally, say for an ODM type object. No fear, `mpath` has you covered. Simply pass the name of the property being used to store the internal data and it will be traversed instead: + +```js +var mpath = require('mpath'); + +var obj = { + comments: [ + { title: 'exciting!', _doc: { title: 'great!' }} + ] +} + +mpath.get('comments.0.title', obj, '_doc') // 'great!' +mpath.set('comments.0.title', 'nov 3rd', obj, '_doc') +mpath.get('comments.0.title', obj, '_doc') // 'nov 3rd' +mpath.get('comments.0.title', obj) // 'exciting' +``` + +When used with a `map`, the `map` argument comes last. + +```js +mpath.get(path, obj, '_doc', map); +mpath.set(path, val, obj, '_doc', map); +``` + +[LICENSE](https://github.com/aheckmann/mpath/blob/master/LICENSE) + diff --git a/gateway/node_modules/mpath/SECURITY.md b/gateway/node_modules/mpath/SECURITY.md new file mode 100644 index 0000000000000000000000000000000000000000..1c916a34f92666ee47e3ee5358414492cfdda510 --- /dev/null +++ b/gateway/node_modules/mpath/SECURITY.md @@ -0,0 +1,5 @@ +# Reporting a Vulnerability + +Please report suspected security vulnerabilities to val [at] karpov [dot] io. +You will receive a response from us within 72 hours. +If the issue is confirmed, we will release a patch as soon as possible depending on complexity. diff --git a/gateway/node_modules/mpath/index.js b/gateway/node_modules/mpath/index.js new file mode 100644 index 0000000000000000000000000000000000000000..47c17cdc8075807f7a05f4fa7539bd2602a55e55 --- /dev/null +++ b/gateway/node_modules/mpath/index.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = exports = require('./lib'); diff --git a/gateway/node_modules/mpath/package.json b/gateway/node_modules/mpath/package.json new file mode 100644 index 0000000000000000000000000000000000000000..6d1242d4eb320271ca9b3d2ba4beaf7ac4b772a2 --- /dev/null +++ b/gateway/node_modules/mpath/package.json @@ -0,0 +1,144 @@ +{ + "name": "mpath", + "version": "0.9.0", + "description": "{G,S}et object values using MongoDB-like path notation", + "main": "index.js", + "scripts": { + "lint": "eslint .", + "test": "mocha test/*" + }, + "engines": { + "node": ">=4.0.0" + }, + "repository": "git://github.com/aheckmann/mpath.git", + "keywords": [ + "mongodb", + "path", + "get", + "set" + ], + "author": "Aaron Heckmann ", + "license": "MIT", + "devDependencies": { + "mocha": "5.x", + "benchmark": "~1.0.0", + "eslint": "7.16.0" + }, + "eslintConfig": { + "extends": [ + "eslint:recommended" + ], + "parserOptions": { + "ecmaVersion": 2015 + }, + "env": { + "node": true, + "es6": true + }, + "rules": { + "comma-style": "error", + "indent": [ + "error", + 2, + { + "SwitchCase": 1, + "VariableDeclarator": 2 + } + ], + "keyword-spacing": "error", + "no-whitespace-before-property": "error", + "no-buffer-constructor": "warn", + "no-console": "off", + "no-multi-spaces": "error", + "no-constant-condition": "off", + "func-call-spacing": "error", + "no-trailing-spaces": "error", + "no-undef": "error", + "no-unneeded-ternary": "error", + "no-const-assign": "error", + "no-useless-rename": "error", + "no-dupe-keys": "error", + "space-in-parens": [ + "error", + "never" + ], + "spaced-comment": [ + "error", + "always", + { + "block": { + "markers": [ + "!" + ], + "balanced": true + } + } + ], + "key-spacing": [ + "error", + { + "beforeColon": false, + "afterColon": true + } + ], + "comma-spacing": [ + "error", + { + "before": false, + "after": true + } + ], + "array-bracket-spacing": 1, + "arrow-spacing": [ + "error", + { + "before": true, + "after": true + } + ], + "object-curly-spacing": [ + "error", + "always" + ], + "comma-dangle": [ + "error", + "never" + ], + "no-unreachable": "error", + "quotes": [ + "error", + "single" + ], + "quote-props": [ + "error", + "as-needed" + ], + "semi": "error", + "no-extra-semi": "error", + "semi-spacing": "error", + "no-spaced-func": "error", + "no-throw-literal": "error", + "space-before-blocks": "error", + "space-before-function-paren": [ + "error", + "never" + ], + "space-infix-ops": "error", + "space-unary-ops": "error", + "no-var": "warn", + "prefer-const": "warn", + "strict": [ + "error", + "global" + ], + "no-restricted-globals": [ + "error", + { + "name": "context", + "message": "Don't use Mocha's global context" + } + ], + "no-prototype-builtins": "off" + } + } +} diff --git a/gateway/node_modules/mpath/test/.eslintrc.yml b/gateway/node_modules/mpath/test/.eslintrc.yml new file mode 100644 index 0000000000000000000000000000000000000000..c0c68038fdae5ce449fb2efa6114caec9d6d0a0d --- /dev/null +++ b/gateway/node_modules/mpath/test/.eslintrc.yml @@ -0,0 +1,4 @@ +env: + mocha: true +rules: + no-unused-vars: off \ No newline at end of file diff --git a/gateway/node_modules/mpath/test/index.js b/gateway/node_modules/mpath/test/index.js new file mode 100644 index 0000000000000000000000000000000000000000..ce07b198995094d8b59a33a4faf4a07e9281174b --- /dev/null +++ b/gateway/node_modules/mpath/test/index.js @@ -0,0 +1,1879 @@ +'use strict'; + +/** + * Test dependencies. + */ + +const mpath = require('../'); +const assert = require('assert'); + +/** + * logging helper + */ + +function log(o) { + console.log(); + console.log(require('util').inspect(o, false, 1000)); +} + +/** + * special path for override tests + */ + +const special = '_doc'; + +/** + * Tests + */ + +describe('mpath', function() { + + /** + * test doc creator + */ + + function doc() { + const o = { first: { second: { third: [3, { name: 'aaron' }, 9] } } }; + o.comments = [ + { name: 'one' }, + { name: 'two', _doc: { name: '2' } }, + { name: 'three', + comments: [{}, { comments: [{ val: 'twoo' }] }], + _doc: { name: '3', comments: [{}, { _doc: { comments: [{ val: 2 }] } }] } } + ]; + o.name = 'jiro'; + o.array = [ + { o: { array: [{ x: { b: [4, 6, 8] } }, { y: 10 }] } }, + { o: { array: [{ x: { b: [1, 2, 3] } }, { x: { z: 10 } }, { x: { b: 'hi' } }] } }, + { o: { array: [{ x: { b: null } }, { x: { b: [null, 1] } }] } }, + { o: { array: [{ x: null }] } }, + { o: { array: [{ y: 3 }] } }, + { o: { array: [3, 0, null] } }, + { o: { name: 'ha' } } + ]; + o.arr = [ + { arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: true } + ]; + return o; + } + + describe('get', function() { + const o = doc(); + + it('`path` must be a string or array', function(done) { + assert.throws(function() { + mpath.get({}, o); + }, /Must be either string or array/); + assert.throws(function() { + mpath.get(4, o); + }, /Must be either string or array/); + assert.throws(function() { + mpath.get(function() {}, o); + }, /Must be either string or array/); + assert.throws(function() { + mpath.get(/asdf/, o); + }, /Must be either string or array/); + assert.throws(function() { + mpath.get(Math, o); + }, /Must be either string or array/); + assert.throws(function() { + mpath.get(Buffer, o); + }, /Must be either string or array/); + assert.doesNotThrow(function() { + mpath.get('string', o); + }); + assert.doesNotThrow(function() { + mpath.get([], o); + }); + done(); + }); + + describe('without `special`', function() { + it('works', function(done) { + assert.equal('jiro', mpath.get('name', o)); + + assert.deepEqual( + { second: { third: [3, { name: 'aaron' }, 9] } } + , mpath.get('first', o) + ); + + assert.deepEqual( + { third: [3, { name: 'aaron' }, 9] } + , mpath.get('first.second', o) + ); + + assert.deepEqual( + [3, { name: 'aaron' }, 9] + , mpath.get('first.second.third', o) + ); + + assert.deepEqual( + 3 + , mpath.get('first.second.third.0', o) + ); + + assert.deepEqual( + 9 + , mpath.get('first.second.third.2', o) + ); + + assert.deepEqual( + { name: 'aaron' } + , mpath.get('first.second.third.1', o) + ); + + assert.deepEqual( + 'aaron' + , mpath.get('first.second.third.1.name', o) + ); + + assert.deepEqual([ + { name: 'one' }, + { name: 'two', _doc: { name: '2' } }, + { name: 'three', + comments: [{}, { comments: [{ val: 'twoo' }] }], + _doc: { name: '3', comments: [{}, { _doc: { comments: [{ val: 2 }] } }] } }], + mpath.get('comments', o)); + + assert.deepEqual({ name: 'one' }, mpath.get('comments.0', o)); + assert.deepEqual('one', mpath.get('comments.0.name', o)); + assert.deepEqual('two', mpath.get('comments.1.name', o)); + assert.deepEqual('three', mpath.get('comments.2.name', o)); + + assert.deepEqual([{}, { comments: [{ val: 'twoo' }] }] + , mpath.get('comments.2.comments', o)); + + assert.deepEqual({ comments: [{ val: 'twoo' }] } + , mpath.get('comments.2.comments.1', o)); + + assert.deepEqual('twoo', mpath.get('comments.2.comments.1.comments.0.val', o)); + + done(); + }); + + it('handles array.property dot-notation', function(done) { + assert.deepEqual( + ['one', 'two', 'three'] + , mpath.get('comments.name', o) + ); + done(); + }); + + it('handles array.array notation', function(done) { + assert.deepEqual( + [undefined, undefined, [{}, { comments: [{ val: 'twoo' }] }]] + , mpath.get('comments.comments', o) + ); + done(); + }); + + it('handles prop.prop.prop.arrayProperty notation', function(done) { + assert.deepEqual( + [undefined, 'aaron', undefined] + , mpath.get('first.second.third.name', o) + ); + assert.deepEqual( + [1, 'aaron', 1] + , mpath.get('first.second.third.name', o, function(v) { + return undefined === v ? 1 : v; + }) + ); + done(); + }); + + it('handles array.prop.array', function(done) { + assert.deepEqual( + [[{ x: { b: [4, 6, 8] } }, { y: 10 }], + [{ x: { b: [1, 2, 3] } }, { x: { z: 10 } }, { x: { b: 'hi' } }], + [{ x: { b: null } }, { x: { b: [null, 1] } }], + [{ x: null }], + [{ y: 3 }], + [3, 0, null], + undefined + ] + , mpath.get('array.o.array', o) + ); + done(); + }); + + it('handles array.prop.array.index', function(done) { + assert.deepEqual( + [{ x: { b: [4, 6, 8] } }, + { x: { b: [1, 2, 3] } }, + { x: { b: null } }, + { x: null }, + { y: 3 }, + 3, + undefined + ] + , mpath.get('array.o.array.0', o) + ); + done(); + }); + + it('handles array.prop.array.index.prop', function(done) { + assert.deepEqual( + [{ b: [4, 6, 8] }, + { b: [1, 2, 3] }, + { b: null }, + null, + undefined, + undefined, + undefined + ] + , mpath.get('array.o.array.0.x', o) + ); + done(); + }); + + it('handles array.prop.array.prop', function(done) { + assert.deepEqual( + [[undefined, 10], + [undefined, undefined, undefined], + [undefined, undefined], + [undefined], + [3], + [undefined, undefined, undefined], + undefined + ] + , mpath.get('array.o.array.y', o) + ); + assert.deepEqual( + [[{ b: [4, 6, 8] }, undefined], + [{ b: [1, 2, 3] }, { z: 10 }, { b: 'hi' }], + [{ b: null }, { b: [null, 1] }], + [null], + [undefined], + [undefined, undefined, undefined], + undefined + ] + , mpath.get('array.o.array.x', o) + ); + done(); + }); + + it('handles array.prop.array.prop.prop', function(done) { + assert.deepEqual( + [[[4, 6, 8], undefined], + [[1, 2, 3], undefined, 'hi'], + [null, [null, 1]], + [null], + [undefined], + [undefined, undefined, undefined], + undefined + ] + , mpath.get('array.o.array.x.b', o) + ); + done(); + }); + + it('handles array.prop.array.prop.prop.index', function(done) { + assert.deepEqual( + [[6, undefined], + [2, undefined, 'i'], // undocumented feature (string indexing) + [null, 1], + [null], + [undefined], + [undefined, undefined, undefined], + undefined + ] + , mpath.get('array.o.array.x.b.1', o) + ); + assert.deepEqual( + [[6, 0], + [2, 0, 'i'], // undocumented feature (string indexing) + [null, 1], + [null], + [0], + [0, 0, 0], + 0 + ] + , mpath.get('array.o.array.x.b.1', o, function(v) { + return undefined === v ? 0 : v; + }) + ); + done(); + }); + + it('handles array.index.prop.prop', function(done) { + assert.deepEqual( + [{ x: { b: [1, 2, 3] } }, { x: { z: 10 } }, { x: { b: 'hi' } }] + , mpath.get('array.1.o.array', o) + ); + assert.deepEqual( + ['hi', 'hi', 'hi'] + , mpath.get('array.1.o.array', o, function(v) { + if (Array.isArray(v)) { + return v.map(function(val) { + return 'hi'; + }); + } + return v; + }) + ); + done(); + }); + + it('handles array.array.index', function(done) { + assert.deepEqual( + [{ a: { c: 48 } }, undefined] + , mpath.get('arr.arr.1', o) + ); + assert.deepEqual( + ['woot', undefined] + , mpath.get('arr.arr.1', o, function(v) { + if (v && v.a && v.a.c) return 'woot'; + return v; + }) + ); + done(); + }); + + it('handles array.array.index.prop', function(done) { + assert.deepEqual( + [{ c: 48 }, 'woot'] + , mpath.get('arr.arr.1.a', o, function(v) { + if (undefined === v) return 'woot'; + return v; + }) + ); + assert.deepEqual( + [{ c: 48 }, undefined] + , mpath.get('arr.arr.1.a', o) + ); + mpath.set('arr.arr.1.a', [{ c: 49 }, undefined], o); + assert.deepEqual( + [{ c: 49 }, undefined] + , mpath.get('arr.arr.1.a', o) + ); + mpath.set('arr.arr.1.a', [{ c: 48 }, undefined], o); + done(); + }); + + it('handles array.array.index.prop.prop', function(done) { + assert.deepEqual( + [48, undefined] + , mpath.get('arr.arr.1.a.c', o) + ); + assert.deepEqual( + [48, 'woot'] + , mpath.get('arr.arr.1.a.c', o, function(v) { + if (undefined === v) return 'woot'; + return v; + }) + ); + done(); + }); + + }); + + describe('with `special`', function() { + describe('that is a string', function() { + it('works', function(done) { + assert.equal('jiro', mpath.get('name', o, special)); + + assert.deepEqual( + { second: { third: [3, { name: 'aaron' }, 9] } } + , mpath.get('first', o, special) + ); + + assert.deepEqual( + { third: [3, { name: 'aaron' }, 9] } + , mpath.get('first.second', o, special) + ); + + assert.deepEqual( + [3, { name: 'aaron' }, 9] + , mpath.get('first.second.third', o, special) + ); + + assert.deepEqual( + 3 + , mpath.get('first.second.third.0', o, special) + ); + + assert.deepEqual( + 4 + , mpath.get('first.second.third.0', o, special, function(v) { + return 3 === v ? 4 : v; + }) + ); + + assert.deepEqual( + 9 + , mpath.get('first.second.third.2', o, special) + ); + + assert.deepEqual( + { name: 'aaron' } + , mpath.get('first.second.third.1', o, special) + ); + + assert.deepEqual( + 'aaron' + , mpath.get('first.second.third.1.name', o, special) + ); + + assert.deepEqual([ + { name: 'one' }, + { name: 'two', _doc: { name: '2' } }, + { name: 'three', + comments: [{}, { comments: [{ val: 'twoo' }] }], + _doc: { name: '3', comments: [{}, { _doc: { comments: [{ val: 2 }] } }] } }], + mpath.get('comments', o, special)); + + assert.deepEqual({ name: 'one' }, mpath.get('comments.0', o, special)); + assert.deepEqual('one', mpath.get('comments.0.name', o, special)); + assert.deepEqual('2', mpath.get('comments.1.name', o, special)); + assert.deepEqual('3', mpath.get('comments.2.name', o, special)); + assert.deepEqual('nice', mpath.get('comments.2.name', o, special, function(v) { + return '3' === v ? 'nice' : v; + })); + + assert.deepEqual([{}, { _doc: { comments: [{ val: 2 }] } }] + , mpath.get('comments.2.comments', o, special)); + + assert.deepEqual({ _doc: { comments: [{ val: 2 }] } } + , mpath.get('comments.2.comments.1', o, special)); + + assert.deepEqual(2, mpath.get('comments.2.comments.1.comments.0.val', o, special)); + done(); + }); + + it('handles array.property dot-notation', function(done) { + assert.deepEqual( + ['one', '2', '3'] + , mpath.get('comments.name', o, special) + ); + assert.deepEqual( + ['one', 2, '3'] + , mpath.get('comments.name', o, special, function(v) { + return '2' === v ? 2 : v; + }) + ); + done(); + }); + + it('handles array.array notation', function(done) { + assert.deepEqual( + [undefined, undefined, [{}, { _doc: { comments: [{ val: 2 }] } }]] + , mpath.get('comments.comments', o, special) + ); + done(); + }); + + it('handles array.array.index.array', function(done) { + assert.deepEqual( + [undefined, undefined, [{ val: 2 }]] + , mpath.get('comments.comments.1.comments', o, special) + ); + done(); + }); + + it('handles array.array.index.array.prop', function(done) { + assert.deepEqual( + [undefined, undefined, [2]] + , mpath.get('comments.comments.1.comments.val', o, special) + ); + assert.deepEqual( + ['nil', 'nil', [2]] + , mpath.get('comments.comments.1.comments.val', o, special, function(v) { + return undefined === v ? 'nil' : v; + }) + ); + done(); + }); + }); + + describe('that is a function', function() { + const special = function(obj, key) { + return obj[key]; + }; + + it('works', function(done) { + assert.equal('jiro', mpath.get('name', o, special)); + + assert.deepEqual( + { second: { third: [3, { name: 'aaron' }, 9] } } + , mpath.get('first', o, special) + ); + + assert.deepEqual( + { third: [3, { name: 'aaron' }, 9] } + , mpath.get('first.second', o, special) + ); + + assert.deepEqual( + [3, { name: 'aaron' }, 9] + , mpath.get('first.second.third', o, special) + ); + + assert.deepEqual( + 3 + , mpath.get('first.second.third.0', o, special) + ); + + assert.deepEqual( + 4 + , mpath.get('first.second.third.0', o, special, function(v) { + return 3 === v ? 4 : v; + }) + ); + + assert.deepEqual( + 9 + , mpath.get('first.second.third.2', o, special) + ); + + assert.deepEqual( + { name: 'aaron' } + , mpath.get('first.second.third.1', o, special) + ); + + assert.deepEqual( + 'aaron' + , mpath.get('first.second.third.1.name', o, special) + ); + + assert.deepEqual([ + { name: 'one' }, + { name: 'two', _doc: { name: '2' } }, + { name: 'three', + comments: [{}, { comments: [{ val: 'twoo' }] }], + _doc: { name: '3', comments: [{}, { _doc: { comments: [{ val: 2 }] } }] } }], + mpath.get('comments', o, special)); + + assert.deepEqual({ name: 'one' }, mpath.get('comments.0', o, special)); + assert.deepEqual('one', mpath.get('comments.0.name', o, special)); + assert.deepEqual('two', mpath.get('comments.1.name', o, special)); + assert.deepEqual('three', mpath.get('comments.2.name', o, special)); + assert.deepEqual('nice', mpath.get('comments.2.name', o, special, function(v) { + return 'three' === v ? 'nice' : v; + })); + + assert.deepEqual([{}, { comments: [{ val: 'twoo' }] }] + , mpath.get('comments.2.comments', o, special)); + + assert.deepEqual({ comments: [{ val: 'twoo' }] } + , mpath.get('comments.2.comments.1', o, special)); + + assert.deepEqual('twoo', mpath.get('comments.2.comments.1.comments.0.val', o, special)); + + let overide = false; + assert.deepEqual('twoo', mpath.get('comments.8.comments.1.comments.0.val', o, function(obj, path) { + if (Array.isArray(obj) && 8 == path) { + overide = true; + return obj[2]; + } + return obj[path]; + })); + assert.ok(overide); + + done(); + }); + + it('in combination with map', function(done) { + const special = function(obj, key) { + if (Array.isArray(obj)) return obj[key]; + return obj.mpath; + }; + const map = function(val) { + return 'convert' == val + ? 'mpath' + : val; + }; + const o = { mpath: [{ mpath: 'converse' }, { mpath: 'convert' }] }; + + assert.equal('mpath', mpath.get('something.1.kewl', o, special, map)); + done(); + }); + }); + }); + }); + + describe('set', function() { + it('prevents writing to __proto__', function() { + const obj = {}; + mpath.set('__proto__.x', 'foobar', obj); + assert.ok(!({}.x)); + + mpath.set('constructor.prototype.x', 'foobar', obj); + assert.ok(!({}.x)); + }); + + describe('without `special`', function() { + const o = doc(); + + it('works', function(done) { + mpath.set('name', 'a new val', o, function(v) { + return 'a new val' === v ? 'changed' : v; + }); + assert.deepEqual('changed', o.name); + + mpath.set('name', 'changed', o); + assert.deepEqual('changed', o.name); + + mpath.set('first.second.third', [1, { name: 'x' }, 9], o); + assert.deepEqual([1, { name: 'x' }, 9], o.first.second.third); + + mpath.set('first.second.third.1.name', 'y', o); + assert.deepEqual([1, { name: 'y' }, 9], o.first.second.third); + + mpath.set('comments.1.name', 'ttwwoo', o); + assert.deepEqual({ name: 'ttwwoo', _doc: { name: '2' } }, o.comments[1]); + + mpath.set('comments.2.comments.1.comments.0.expand', 'added', o); + assert.deepEqual( + { val: 'twoo', expand: 'added' } + , o.comments[2].comments[1].comments[0]); + + mpath.set('comments.2.comments.1.comments.2', 'added', o); + assert.equal(3, o.comments[2].comments[1].comments.length); + assert.deepEqual( + { val: 'twoo', expand: 'added' } + , o.comments[2].comments[1].comments[0]); + assert.deepEqual( + undefined + , o.comments[2].comments[1].comments[1]); + assert.deepEqual( + 'added' + , o.comments[2].comments[1].comments[2]); + + done(); + }); + + describe('array.path', function() { + describe('with single non-array value', function() { + it('works', function(done) { + mpath.set('arr.yep', false, o, function(v) { + return false === v ? true : v; + }); + assert.deepEqual([ + { yep: true, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: true } + ], o.arr); + + mpath.set('arr.yep', false, o); + + assert.deepEqual([ + { yep: false, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: false } + ], o.arr); + + done(); + }); + }); + describe('with array of values', function() { + it('that are equal in length', function(done) { + mpath.set('arr.yep', ['one', 2], o, function(v) { + return 'one' === v ? 1 : v; + }); + assert.deepEqual([ + { yep: 1, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: 2 } + ], o.arr); + mpath.set('arr.yep', ['one', 2], o); + + assert.deepEqual([ + { yep: 'one', arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: 2 } + ], o.arr); + + done(); + }); + + it('that is less than length', function(done) { + mpath.set('arr.yep', [47], o, function(v) { + return 47 === v ? 4 : v; + }); + assert.deepEqual([ + { yep: 4, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: 2 } + ], o.arr); + + mpath.set('arr.yep', [47], o); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: 2 } + ], o.arr); + + done(); + }); + + it('that is greater than length', function(done) { + mpath.set('arr.yep', [5, 6, 7], o, function(v) { + return 5 === v ? 'five' : v; + }); + assert.deepEqual([ + { yep: 'five', arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: 6 } + ], o.arr); + + mpath.set('arr.yep', [5, 6, 7], o); + assert.deepEqual([ + { yep: 5, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: 6 } + ], o.arr); + + done(); + }); + }); + }); + + describe('array.$.path', function() { + describe('with single non-array value', function() { + it('copies the value to each item in array', function(done) { + mpath.set('arr.$.yep', { xtra: 'double good' }, o, function(v) { + return v && v.xtra ? 'hi' : v; + }); + assert.deepEqual([ + { yep: 'hi', arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: 'hi' } + ], o.arr); + + mpath.set('arr.$.yep', { xtra: 'double good' }, o); + assert.deepEqual([ + { yep: { xtra: 'double good' }, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: { xtra: 'double good' } } + ], o.arr); + + done(); + }); + }); + describe('with array of values', function() { + it('copies the value to each item in array', function(done) { + mpath.set('arr.$.yep', [15], o, function(v) { + return v.length === 1 ? [] : v; + }); + assert.deepEqual([ + { yep: [], arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: [] } + ], o.arr); + + mpath.set('arr.$.yep', [15], o); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: [15] } + ], o.arr); + + done(); + }); + }); + }); + + describe('array.index.path', function() { + it('works', function(done) { + mpath.set('arr.1.yep', 0, o, function(v) { + return 0 === v ? 'zero' : v; + }); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: 'zero' } + ], o.arr); + + mpath.set('arr.1.yep', 0, o); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: 0 } + ], o.arr); + + done(); + }); + }); + + describe('array.index.array.path', function() { + it('with single value', function(done) { + mpath.set('arr.0.arr.e', 35, o, function(v) { + return 35 === v ? 3 : v; + }); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: 47 }, e: 3 }, { a: { c: 48 }, e: 3 }, { d: 'yep', e: 3 }] }, + { yep: 0 } + ], o.arr); + + mpath.set('arr.0.arr.e', 35, o); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: 47 }, e: 35 }, { a: { c: 48 }, e: 35 }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + done(); + }); + it('with array', function(done) { + mpath.set('arr.0.arr.e', ['a', 'b'], o, function(v) { + return 'a' === v ? 'x' : v; + }); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: 47 }, e: 'x' }, { a: { c: 48 }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + mpath.set('arr.0.arr.e', ['a', 'b'], o); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: 47 }, e: 'a' }, { a: { c: 48 }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + done(); + }); + }); + + describe('array.index.array.path.path', function() { + it('with single value', function(done) { + mpath.set('arr.0.arr.a.b', 36, o, function(v) { + return 36 === v ? 3 : v; + }); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: 3 }, e: 'a' }, { a: { c: 48, b: 3 }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + mpath.set('arr.0.arr.a.b', 36, o); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: 36 }, e: 'a' }, { a: { c: 48, b: 36 }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + done(); + }); + it('with array', function(done) { + mpath.set('arr.0.arr.a.b', [1, 2, 3, 4], o, function(v) { + return 2 === v ? 'two' : v; + }); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: 1 }, e: 'a' }, { a: { c: 48, b: 'two' }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + mpath.set('arr.0.arr.a.b', [1, 2, 3, 4], o); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: 1 }, e: 'a' }, { a: { c: 48, b: 2 }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + done(); + }); + }); + + describe('array.index.array.$.path.path', function() { + it('with single value', function(done) { + mpath.set('arr.0.arr.$.a.b', '$', o, function(v) { + return '$' === v ? 'dolla billz' : v; + }); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: 'dolla billz' }, e: 'a' }, { a: { c: 48, b: 'dolla billz' }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + mpath.set('arr.0.arr.$.a.b', '$', o); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: '$' }, e: 'a' }, { a: { c: 48, b: '$' }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + done(); + }); + it('with array', function(done) { + mpath.set('arr.0.arr.$.a.b', [1], o, function(v) { + return Array.isArray(v) ? {} : v; + }); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: {} }, e: 'a' }, { a: { c: 48, b: {} }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + mpath.set('arr.0.arr.$.a.b', [1], o); + assert.deepEqual([ + { yep: [15], arr: [{ a: { b: [1] }, e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + done(); + }); + }); + + describe('array.array.index.path', function() { + it('with single value', function(done) { + mpath.set('arr.arr.0.a', 'single', o, function(v) { + return 'single' === v ? 'double' : v; + }); + assert.deepEqual([ + { yep: [15], arr: [{ a: 'double', e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + mpath.set('arr.arr.0.a', 'single', o); + assert.deepEqual([ + { yep: [15], arr: [{ a: 'single', e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + done(); + }); + it('with array', function(done) { + mpath.set('arr.arr.0.a', [4, 8, 15, 16, 23, 42], o, function(v) { + return 4 === v ? 3 : v; + }); + assert.deepEqual([ + { yep: [15], arr: [{ a: 3, e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: false } + ], o.arr); + + mpath.set('arr.arr.0.a', [4, 8, 15, 16, 23, 42], o); + assert.deepEqual([ + { yep: [15], arr: [{ a: 4, e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: false } + ], o.arr); + + done(); + }); + }); + + describe('array.array.$.index.path', function() { + it('with single value', function(done) { + mpath.set('arr.arr.$.0.a', 'singles', o, function(v) { + return 0; + }); + assert.deepEqual([ + { yep: [15], arr: [{ a: 0, e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + mpath.set('arr.arr.$.0.a', 'singles', o); + assert.deepEqual([ + { yep: [15], arr: [{ a: 'singles', e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + mpath.set('$.arr.arr.0.a', 'single', o); + assert.deepEqual([ + { yep: [15], arr: [{ a: 'single', e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + done(); + }); + it('with array', function(done) { + mpath.set('arr.arr.$.0.a', [4, 8, 15, 16, 23, 42], o, function(v) { + return 'nope'; + }); + assert.deepEqual([ + { yep: [15], arr: [{ a: 'nope', e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + mpath.set('arr.arr.$.0.a', [4, 8, 15, 16, 23, 42], o); + assert.deepEqual([ + { yep: [15], arr: [{ a: [4, 8, 15, 16, 23, 42], e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + mpath.set('arr.$.arr.0.a', [4, 8, 15, 16, 23, 42, 108], o); + assert.deepEqual([ + { yep: [15], arr: [{ a: [4, 8, 15, 16, 23, 42, 108], e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + done(); + }); + }); + + describe('array.array.path.index', function() { + it('with single value', function(done) { + mpath.set('arr.arr.a.7', 47, o, function(v) { + return 1; + }); + assert.deepEqual([ + { yep: [15], arr: [{ a: [4, 8, 15, 16, 23, 42, 108, 1], e: 'a' }, { a: { c: 48, b: [1], 7: 1 }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + mpath.set('arr.arr.a.7', 47, o); + assert.deepEqual([ + { yep: [15], arr: [{ a: [4, 8, 15, 16, 23, 42, 108, 47], e: 'a' }, { a: { c: 48, b: [1], 7: 47 }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0 } + ], o.arr); + + done(); + }); + it('with array', function(done) { + o.arr[1].arr = [{ a: [] }, { a: [] }, { a: null }]; + mpath.set('arr.arr.a.7', [[null, 46], [undefined, 'woot']], o); + + const a1 = []; + const a2 = []; + a1[7] = undefined; + a2[7] = 'woot'; + + assert.deepEqual([ + { yep: [15], arr: [{ a: [4, 8, 15, 16, 23, 42, 108, null], e: 'a' }, { a: { c: 48, b: [1], 7: 46 }, e: 'b' }, { d: 'yep', e: 35 }] }, + { yep: 0, arr: [{ a: a1 }, { a: a2 }, { a: null }] } + ], o.arr); + + done(); + }); + }); + + describe('handles array.array.path', function() { + it('with single', function(done) { + o.arr[1].arr = [{}, {}]; + assert.deepEqual([{}, {}], o.arr[1].arr); + o.arr.push({ arr: 'something else' }); + o.arr.push({ arr: ['something else'] }); + o.arr.push({ arr: [[]] }); + o.arr.push({ arr: [5] }); + + const weird = []; + weird.e = 'xmas'; + + // test + mpath.set('arr.arr.e', 47, o, function(v) { + return 'xmas'; + }); + assert.deepEqual([ + { yep: [15], arr: [ + { a: [4, 8, 15, 16, 23, 42, 108, null], e: 'xmas' }, + { a: { c: 48, b: [1], 7: 46 }, e: 'xmas' }, + { d: 'yep', e: 'xmas' } + ] + }, + { yep: 0, arr: [{ e: 'xmas' }, { e: 'xmas' }] }, + { arr: 'something else' }, + { arr: ['something else'] }, + { arr: [weird] }, + { arr: [5] } + ] + , o.arr); + + weird.e = 47; + + mpath.set('arr.arr.e', 47, o); + assert.deepEqual([ + { yep: [15], arr: [ + { a: [4, 8, 15, 16, 23, 42, 108, null], e: 47 }, + { a: { c: 48, b: [1], 7: 46 }, e: 47 }, + { d: 'yep', e: 47 } + ] + }, + { yep: 0, arr: [{ e: 47 }, { e: 47 }] }, + { arr: 'something else' }, + { arr: ['something else'] }, + { arr: [weird] }, + { arr: [5] } + ] + , o.arr); + + done(); + }); + it('with arrays', function(done) { + mpath.set('arr.arr.e', [[1, 2, 3], [4, 5], null, [], [6], [7, 8, 9]], o, function(v) { + return 10; + }); + + const weird = []; + weird.e = 10; + + assert.deepEqual([ + { yep: [15], arr: [ + { a: [4, 8, 15, 16, 23, 42, 108, null], e: 10 }, + { a: { c: 48, b: [1], 7: 46 }, e: 10 }, + { d: 'yep', e: 10 } + ] + }, + { yep: 0, arr: [{ e: 10 }, { e: 10 }] }, + { arr: 'something else' }, + { arr: ['something else'] }, + { arr: [weird] }, + { arr: [5] } + ] + , o.arr); + + mpath.set('arr.arr.e', [[1, 2, 3], [4, 5], null, [], [6], [7, 8, 9]], o); + + weird.e = 6; + + assert.deepEqual([ + { yep: [15], arr: [ + { a: [4, 8, 15, 16, 23, 42, 108, null], e: 1 }, + { a: { c: 48, b: [1], 7: 46 }, e: 2 }, + { d: 'yep', e: 3 } + ] + }, + { yep: 0, arr: [{ e: 4 }, { e: 5 }] }, + { arr: 'something else' }, + { arr: ['something else'] }, + { arr: [weird] }, + { arr: [5] } + ] + , o.arr); + + done(); + }); + }); + }); + + describe('with `special`', function() { + const o = doc(); + + it('works', function(done) { + mpath.set('name', 'chan', o, special, function(v) { + return 'hi'; + }); + assert.deepEqual('hi', o.name); + + mpath.set('name', 'changer', o, special); + assert.deepEqual('changer', o.name); + + mpath.set('first.second.third', [1, { name: 'y' }, 9], o, special); + assert.deepEqual([1, { name: 'y' }, 9], o.first.second.third); + + mpath.set('first.second.third.1.name', 'z', o, special); + assert.deepEqual([1, { name: 'z' }, 9], o.first.second.third); + + mpath.set('comments.1.name', 'ttwwoo', o, special); + assert.deepEqual({ name: 'two', _doc: { name: 'ttwwoo' } }, o.comments[1]); + + mpath.set('comments.2.comments.1.comments.0.expander', 'adder', o, special, function(v) { + return 'super'; + }); + assert.deepEqual( + { val: 2, expander: 'super' } + , o.comments[2]._doc.comments[1]._doc.comments[0]); + + mpath.set('comments.2.comments.1.comments.0.expander', 'adder', o, special); + assert.deepEqual( + { val: 2, expander: 'adder' } + , o.comments[2]._doc.comments[1]._doc.comments[0]); + + mpath.set('comments.2.comments.1.comments.2', 'set', o, special); + assert.equal(3, o.comments[2]._doc.comments[1]._doc.comments.length); + assert.deepEqual( + { val: 2, expander: 'adder' } + , o.comments[2]._doc.comments[1]._doc.comments[0]); + assert.deepEqual( + undefined + , o.comments[2]._doc.comments[1]._doc.comments[1]); + assert.deepEqual( + 'set' + , o.comments[2]._doc.comments[1]._doc.comments[2]); + done(); + }); + + describe('array.path', function() { + describe('with single non-array value', function() { + it('works', function(done) { + o.arr[1]._doc = { special: true }; + + mpath.set('arr.yep', false, o, special, function(v) { + return 'yes'; + }); + assert.deepEqual([ + { yep: 'yes', arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: true, _doc: { special: true, yep: 'yes' } } + ], o.arr); + + mpath.set('arr.yep', false, o, special); + assert.deepEqual([ + { yep: false, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: true, _doc: { special: true, yep: false } } + ], o.arr); + + done(); + }); + }); + describe('with array of values', function() { + it('that are equal in length', function(done) { + mpath.set('arr.yep', ['one', 2], o, special, function(v) { + return 2 === v ? 20 : v; + }); + assert.deepEqual([ + { yep: 'one', arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: true, _doc: { special: true, yep: 20 } } + ], o.arr); + + mpath.set('arr.yep', ['one', 2], o, special); + assert.deepEqual([ + { yep: 'one', arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: true, _doc: { special: true, yep: 2 } } + ], o.arr); + + done(); + }); + + it('that is less than length', function(done) { + mpath.set('arr.yep', [47], o, special, function(v) { + return 80; + }); + assert.deepEqual([ + { yep: 80, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: true, _doc: { special: true, yep: 2 } } + ], o.arr); + + mpath.set('arr.yep', [47], o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }, + { yep: true, _doc: { special: true, yep: 2 } } + ], o.arr); + + // add _doc to first element + o.arr[0]._doc = { yep: 46, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] }; + + mpath.set('arr.yep', [20], o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], _doc: { yep: 20, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] } }, + { yep: true, _doc: { special: true, yep: 2 } } + ], o.arr); + + done(); + }); + + it('that is greater than length', function(done) { + mpath.set('arr.yep', [5, 6, 7], o, special, function() { + return 'x'; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], _doc: { yep: 'x', arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] } }, + { yep: true, _doc: { special: true, yep: 'x' } } + ], o.arr); + + mpath.set('arr.yep', [5, 6, 7], o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], _doc: { yep: 5, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] } }, + { yep: true, _doc: { special: true, yep: 6 } } + ], o.arr); + + done(); + }); + }); + }); + + describe('array.$.path', function() { + describe('with single non-array value', function() { + it('copies the value to each item in array', function(done) { + mpath.set('arr.$.yep', { xtra: 'double good' }, o, special, function(v) { + return 9; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: 9, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] } }, + { yep: true, _doc: { special: true, yep: 9 } } + ], o.arr); + + mpath.set('arr.$.yep', { xtra: 'double good' }, o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: { xtra: 'double good' }, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] } }, + { yep: true, _doc: { special: true, yep: { xtra: 'double good' } } } + ], o.arr); + + done(); + }); + }); + describe('with array of values', function() { + it('copies the value to each item in array', function(done) { + mpath.set('arr.$.yep', [15], o, special, function(v) { + return 'array'; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: 'array', arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] } }, + { yep: true, _doc: { special: true, yep: 'array' } } + ], o.arr); + + mpath.set('arr.$.yep', [15], o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] } }, + { yep: true, _doc: { special: true, yep: [15] } } + ], o.arr); + + done(); + }); + }); + }); + + describe('array.index.path', function() { + it('works', function(done) { + mpath.set('arr.1.yep', 0, o, special, function(v) { + return 1; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] } }, + { yep: true, _doc: { special: true, yep: 1 } } + ], o.arr); + + mpath.set('arr.1.yep', 0, o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + done(); + }); + }); + + describe('array.index.array.path', function() { + it('with single value', function(done) { + mpath.set('arr.0.arr.e', 35, o, special, function(v) { + return 30; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: 47 }, e: 30 }, { a: { c: 48 }, e: 30 }, { d: 'yep', e: 30 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.0.arr.e', 35, o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: 47 }, e: 35 }, { a: { c: 48 }, e: 35 }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + done(); + }); + it('with array', function(done) { + mpath.set('arr.0.arr.e', ['a', 'b'], o, special, function(v) { + return 'a' === v ? 'A' : v; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: 47 }, e: 'A' }, { a: { c: 48 }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.0.arr.e', ['a', 'b'], o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: 47 }, e: 'a' }, { a: { c: 48 }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + done(); + }); + }); + + describe('array.index.array.path.path', function() { + it('with single value', function(done) { + mpath.set('arr.0.arr.a.b', 36, o, special, function(v) { + return 20; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: 20 }, e: 'a' }, { a: { c: 48, b: 20 }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.0.arr.a.b', 36, o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: 36 }, e: 'a' }, { a: { c: 48, b: 36 }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + done(); + }); + it('with array', function(done) { + mpath.set('arr.0.arr.a.b', [1, 2, 3, 4], o, special, function(v) { + return v * 2; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: 2 }, e: 'a' }, { a: { c: 48, b: 4 }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.0.arr.a.b', [1, 2, 3, 4], o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: 1 }, e: 'a' }, { a: { c: 48, b: 2 }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + done(); + }); + }); + + describe('array.index.array.$.path.path', function() { + it('with single value', function(done) { + mpath.set('arr.0.arr.$.a.b', '$', o, special, function(v) { + return 'dollaz'; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: 'dollaz' }, e: 'a' }, { a: { c: 48, b: 'dollaz' }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.0.arr.$.a.b', '$', o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: '$' }, e: 'a' }, { a: { c: 48, b: '$' }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + done(); + }); + it('with array', function(done) { + mpath.set('arr.0.arr.$.a.b', [1], o, special, function(v) { + return {}; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: {} }, e: 'a' }, { a: { c: 48, b: {} }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.0.arr.$.a.b', [1], o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: { b: [1] }, e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + done(); + }); + }); + + describe('array.array.index.path', function() { + it('with single value', function(done) { + mpath.set('arr.arr.0.a', 'single', o, special, function(v) { + return 88; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: 88, e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.arr.0.a', 'single', o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: 'single', e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + done(); + }); + it('with array', function(done) { + mpath.set('arr.arr.0.a', [4, 8, 15, 16, 23, 42], o, special, function(v) { + return v * 2; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: 8, e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.arr.0.a', [4, 8, 15, 16, 23, 42], o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: 4, e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + done(); + }); + }); + + describe('array.array.$.index.path', function() { + it('with single value', function(done) { + mpath.set('arr.arr.$.0.a', 'singles', o, special, function(v) { + return v.toUpperCase(); + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: 'SINGLES', e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.arr.$.0.a', 'singles', o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: 'singles', e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('$.arr.arr.0.a', 'single', o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: 'single', e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + done(); + }); + it('with array', function(done) { + mpath.set('arr.arr.$.0.a', [4, 8, 15, 16, 23, 42], o, special, function(v) { + return Array; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: Array, e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.arr.$.0.a', [4, 8, 15, 16, 23, 42], o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: [4, 8, 15, 16, 23, 42], e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.$.arr.0.a', [4, 8, 15, 16, 23, 42, 108], o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: [4, 8, 15, 16, 23, 42, 108], e: 'a' }, { a: { c: 48, b: [1] }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + done(); + }); + }); + + describe('array.array.path.index', function() { + it('with single value', function(done) { + mpath.set('arr.arr.a.7', 47, o, special, function(v) { + return Object; + }); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: [4, 8, 15, 16, 23, 42, 108, Object], e: 'a' }, { a: { c: 48, b: [1], 7: Object }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.arr.a.7', 47, o, special); + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: [4, 8, 15, 16, 23, 42, 108, 47], e: 'a' }, { a: { c: 48, b: [1], 7: 47 }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { special: true, yep: 0 } } + ], o.arr); + + done(); + }); + it('with array', function(done) { + o.arr[1]._doc.arr = [{ a: [] }, { a: [] }, { a: null }]; + mpath.set('arr.arr.a.7', [[null, 46], [undefined, 'woot']], o, special, function(v) { + return undefined === v ? 'nope' : v; + }); + + const a1 = []; + const a2 = []; + a1[7] = 'nope'; + a2[7] = 'woot'; + + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: [4, 8, 15, 16, 23, 42, 108, null], e: 'a' }, { a: { c: 48, b: [1], 7: 46 }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { arr: [{ a: a1 }, { a: a2 }, { a: null }], special: true, yep: 0 } } + ], o.arr); + + mpath.set('arr.arr.a.7', [[null, 46], [undefined, 'woot']], o, special); + + a1[7] = undefined; + + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { yep: [15], arr: [{ a: [4, 8, 15, 16, 23, 42, 108, null], e: 'a' }, { a: { c: 48, b: [1], 7: 46 }, e: 'b' }, { d: 'yep', e: 35 }] } }, + { yep: true, _doc: { arr: [{ a: a1 }, { a: a2 }, { a: null }], special: true, yep: 0 } } + ], o.arr); + + done(); + }); + }); + + describe('handles array.array.path', function() { + it('with single', function(done) { + o.arr[1]._doc.arr = [{}, {}]; + assert.deepEqual([{}, {}], o.arr[1]._doc.arr); + o.arr.push({ _doc: { arr: 'something else' } }); + o.arr.push({ _doc: { arr: ['something else'] } }); + o.arr.push({ _doc: { arr: [[]] } }); + o.arr.push({ _doc: { arr: [5] } }); + + // test + mpath.set('arr.arr.e', 47, o, special); + + const weird = []; + weird.e = 47; + + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { + yep: [15], + arr: [ + { a: [4, 8, 15, 16, 23, 42, 108, null], e: 47 }, + { a: { c: 48, b: [1], 7: 46 }, e: 47 }, + { d: 'yep', e: 47 } + ] + } + }, + { yep: true, + _doc: { + arr: [ + { e: 47 }, + { e: 47 } + ], + special: true, + yep: 0 + } + }, + { _doc: { arr: 'something else' } }, + { _doc: { arr: ['something else'] } }, + { _doc: { arr: [weird] } }, + { _doc: { arr: [5] } } + ] + , o.arr); + + done(); + }); + it('with arrays', function(done) { + mpath.set('arr.arr.e', [[1, 2, 3], [4, 5], null, [], [6], [7, 8, 9]], o, special); + + const weird = []; + weird.e = 6; + + assert.deepEqual([ + { yep: 47, arr: [{ a: { b: 47 } }, { a: { c: 48 } }, { d: 'yep' }], + _doc: { + yep: [15], + arr: [ + { a: [4, 8, 15, 16, 23, 42, 108, null], e: 1 }, + { a: { c: 48, b: [1], 7: 46 }, e: 2 }, + { d: 'yep', e: 3 } + ] + } + }, + { yep: true, + _doc: { + arr: [ + { e: 4 }, + { e: 5 } + ], + special: true, + yep: 0 + } + }, + { _doc: { arr: 'something else' } }, + { _doc: { arr: ['something else'] } }, + { _doc: { arr: [weird] } }, + { _doc: { arr: [5] } } + ] + , o.arr); + + done(); + }); + }); + + describe('that is a function', function() { + describe('without map', function() { + it('works on array value', function(done) { + const o = { hello: { world: [{ how: 'are' }, { you: '?' }] } }; + const special = function(obj, key, val) { + if (val) { + obj[key] = val; + } else { + return 'thing' == key + ? obj.world + : obj[key]; + } + }; + mpath.set('hello.thing.how', 'arrrr', o, special); + assert.deepEqual(o, { hello: { world: [{ how: 'arrrr' }, { you: '?', how: 'arrrr' }] } }); + done(); + }); + it('works on non-array value', function(done) { + const o = { hello: { world: { how: 'are you' } } }; + const special = function(obj, key, val) { + if (val) { + obj[key] = val; + } else { + return 'thing' == key + ? obj.world + : obj[key]; + } + }; + mpath.set('hello.thing.how', 'RU', o, special); + assert.deepEqual(o, { hello: { world: { how: 'RU' } } }); + done(); + }); + }); + it('works with map', function(done) { + const o = { hello: { world: [{ how: 'are' }, { you: '?' }] } }; + const special = function(obj, key, val) { + if (val) { + obj[key] = val; + } else { + return 'thing' == key + ? obj.world + : obj[key]; + } + }; + const map = function(val) { + return 'convert' == val + ? 'ºº' + : val; + }; + mpath.set('hello.thing.how', 'convert', o, special, map); + assert.deepEqual(o, { hello: { world: [{ how: 'ºº' }, { you: '?', how: 'ºº' }] } }); + done(); + }); + }); + + }); + + describe('get/set integration', function() { + const o = doc(); + + it('works', function(done) { + const vals = mpath.get('array.o.array.x.b', o); + + vals[0][0][2] = 10; + vals[1][0][1] = 0; + vals[1][1] = 'Rambaldi'; + vals[1][2] = [12, 14]; + vals[2] = [{ changed: true }, [null, ['changed', 'to', 'array']]]; + + mpath.set('array.o.array.x.b', vals, o); + + const t = [ + { o: { array: [{ x: { b: [4, 6, 10] } }, { y: 10 }] } }, + { o: { array: [{ x: { b: [1, 0, 3] } }, { x: { b: 'Rambaldi', z: 10 } }, { x: { b: [12, 14] } }] } }, + { o: { array: [{ x: { b: { changed: true } } }, { x: { b: [null, ['changed', 'to', 'array']] } }] } }, + { o: { array: [{ x: null }] } }, + { o: { array: [{ y: 3 }] } }, + { o: { array: [3, 0, null] } }, + { o: { name: 'ha' } } + ]; + assert.deepEqual(t, o.array); + done(); + }); + + it('array.prop', function(done) { + mpath.set('comments.name', ['this', 'was', 'changed'], o); + + assert.deepEqual([ + { name: 'this' }, + { name: 'was', _doc: { name: '2' } }, + { name: 'changed', + comments: [{}, { comments: [{ val: 'twoo' }] }], + _doc: { name: '3', comments: [{}, { _doc: { comments: [{ val: 2 }] } }] } } + ], o.comments); + + mpath.set('comments.name', ['also', 'changed', 'this'], o, special); + + assert.deepEqual([ + { name: 'also' }, + { name: 'was', _doc: { name: 'changed' } }, + { name: 'changed', + comments: [{}, { comments: [{ val: 'twoo' }] }], + _doc: { name: 'this', comments: [{}, { _doc: { comments: [{ val: 2 }] } }] } } + ], o.comments); + + done(); + }); + + it('nested array', function(done) { + const obj = { arr: [[{ test: 41 }]] }; + mpath.set('arr.test', [[42]], obj); + assert.deepEqual(obj.arr, [[{ test: 42 }]]); + done(); + }); + }); + + describe('multiple $ use', function() { + const o = doc(); + it('is ok', function(done) { + assert.doesNotThrow(function() { + mpath.set('arr.$.arr.$.a', 35, o); + }); + done(); + }); + }); + + it('has', function(done) { + assert.ok(mpath.has('a', { a: 1 })); + assert.ok(mpath.has('a', { a: undefined })); + assert.ok(!mpath.has('a', {})); + assert.ok(!mpath.has('a', null)); + + assert.ok(mpath.has('a.b', { a: { b: 1 } })); + assert.ok(mpath.has('a.b', { a: { b: undefined } })); + assert.ok(!mpath.has('a.b', { a: 1 })); + assert.ok(!mpath.has('a.b', { a: null })); + + done(); + }); + + it('underneath a map', function(done) { + if (!global.Map) { + done(); + return; + } + assert.equal(mpath.get('a.b', { a: new Map([['b', 1]]) }), 1); + + const m = new Map([['b', 1]]); + const obj = { a: m }; + mpath.set('a.c', 2, obj); + assert.equal(m.get('c'), 2); + + done(); + }); + + it('unset', function(done) { + let o = { a: 1 }; + mpath.unset('a', o); + assert.deepEqual(o, {}); + + o = { a: { b: 1 } }; + mpath.unset('a.b', o); + assert.deepEqual(o, { a: {} }); + + o = { a: null }; + mpath.unset('a.b', o); + assert.deepEqual(o, { a: null }); + + done(); + }); + + it('unset with __proto__', function(done) { + // Should refuse to set __proto__ + function Clazz() {} + Clazz.prototype.foobar = true; + + mpath.unset('__proto__.foobar', new Clazz()); + assert.ok(Clazz.prototype.foobar); + + mpath.unset('constructor.prototype.foobar', new Clazz()); + assert.ok(Clazz.prototype.foobar); + + done(); + }); + + it('get() underneath subclassed array', function(done) { + class MyArray extends Array {} + + const obj = { + arr: new MyArray() + }; + obj.arr.push({ test: 2 }); + + const arr = mpath.get('arr.test', obj); + assert.equal(arr.constructor.name, 'Array'); + assert.ok(!(arr instanceof MyArray)); + + done(); + }); + + it('ignores setting a nested path that doesnt exist', function(done) { + const o = doc(); + assert.doesNotThrow(function() { + mpath.set('thing.that.is.new', 10, o); + }); + done(); + }); + }); +}); diff --git a/gateway/node_modules/mpath/test/stringToParts.js b/gateway/node_modules/mpath/test/stringToParts.js new file mode 100644 index 0000000000000000000000000000000000000000..09940dfa9892dd4b5a8d6e6418ff808e7e0bff8c --- /dev/null +++ b/gateway/node_modules/mpath/test/stringToParts.js @@ -0,0 +1,30 @@ +'use strict'; + +const assert = require('assert'); +const stringToParts = require('../lib/stringToParts'); + +describe('stringToParts', function() { + it('handles brackets for numbers', function() { + assert.deepEqual(stringToParts('list[0].name'), ['list', '0', 'name']); + assert.deepEqual(stringToParts('list[0][1].name'), ['list', '0', '1', 'name']); + }); + + it('handles dot notation', function() { + assert.deepEqual(stringToParts('a.b.c'), ['a', 'b', 'c']); + assert.deepEqual(stringToParts('a..b.d'), ['a', '', 'b', 'd']); + }); + + it('ignores invalid numbers in square brackets', function() { + assert.deepEqual(stringToParts('foo[1mystring]'), ['foo[1mystring]']); + assert.deepEqual(stringToParts('foo[1mystring].bar[1]'), ['foo[1mystring]', 'bar', '1']); + assert.deepEqual(stringToParts('foo[1mystring][2]'), ['foo[1mystring]', '2']); + }); + + it('handles empty string', function() { + assert.deepEqual(stringToParts(''), ['']); + }); + + it('handles trailing dot', function() { + assert.deepEqual(stringToParts('a.b.'), ['a', 'b', '']); + }); +}); \ No newline at end of file diff --git a/gateway/node_modules/mquery/.github/ISSUE_TEMPLATE.md b/gateway/node_modules/mquery/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000000000000000000000000000000000000..20412e6543d53b60e49d984ca2bc1b5c1dac8960 --- /dev/null +++ b/gateway/node_modules/mquery/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,11 @@ + + +**Do you want to request a *feature* or report a *bug*?** + +**What is the current behavior?** + +**If the current behavior is a bug, please provide the steps to reproduce.** + +**What is the expected behavior?** + +**What are the versions of Node.js and mquery are you are using? Note that "latest" is not a version.** diff --git a/gateway/node_modules/mquery/.github/PULL_REQUEST_TEMPLATE.md b/gateway/node_modules/mquery/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000000000000000000000000000000000000..eb1a69d9766582d7884ee82353575e9902427bca --- /dev/null +++ b/gateway/node_modules/mquery/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,9 @@ + + +**Summary** + + + +**Examples** + + diff --git a/gateway/node_modules/mquery/History.md b/gateway/node_modules/mquery/History.md new file mode 100644 index 0000000000000000000000000000000000000000..4aac5e3d223da73ae140224e82e13c9e8b96bf45 --- /dev/null +++ b/gateway/node_modules/mquery/History.md @@ -0,0 +1,393 @@ +6.0.0 / 2025-11-18 +================== + * BREAKING CHANGE: Make calling updateOne(), updateMany(), and findOneAndX() with one arg set the query filter rather than the update to line up with modern expectations of optional arguments + * BREAKING CHANGE: remove debug dependency, use node debuglog instead + * BREAKING CHANGE: remove count() and findOneAndRemove(): use countDocuments() and findOneAndDelete() instead + * BREAKING CHANGE: require Node >= 20.19.0 + * feat: add findOneAndDelete, findOneAndReplace, countDocuments, estimatedDocumentCount + * feat: use mongodb driver 6 in tests + +5.0.0 / 2023-02-23 +================== + * BREAKING CHANGE: drop callback support #137 [hasezoey](https://github.com/hasezoey) + * BREAKING CHANGE: remove custom promise library support #137 [hasezoey](https://github.com/hasezoey) + * BREAKING CHANGE: remove long deprecated `update`, `remove` functions #136 [hasezoey](https://github.com/hasezoey) + * BREAKING CHANGE: remove collection ducktyping: first param to `mquery()` is now always the query filter #138 + * feat: support MongoDB Node driver 5 #137 [hasezoey](https://github.com/hasezoey) + +4.0.3 / 2022-05-17 +================== + * fix: allow using `comment` with `findOneAndUpdate()`, `count()`, `distinct()` and `hint` with `findOneAndUpdate()` Automattic/mongoose#11793 + +4.0.2 / 2022-01-23 +================== + * perf: replace regexp-clone with native functionality #131 [Uzlopak](https://github.com/Uzlopak) + +4.0.1 / 2022-01-20 +================== + * perf: remove sliced, add various microoptimizations #130 [Uzlopak](https://github.com/Uzlopak) + * refactor: convert NodeCollection to a class #128 [jimmywarting](https://github.com/jimmywarting) + +4.0.0 / 2021-08-24 + +4.0.0-rc0 / 2021-08-19 +====================== + * BREAKING CHANGE: drop support for Node < 12 #123 + * BREAKING CHANGE: upgrade to mongodb driver 4.x: drop support for `findAndModify()`, use native `findOneAndUpdate/Delete` #124 + * BREAKING CHANGE: rename findStream -> findCursor #124 + * BREAKING CHANGE: use native ES6 promises by default, remove bluebird dependency #123 + +3.2.5 / 2021-03-29 +================== + * fix(utils): make `mergeClone()` skip special properties like `__proto__` #121 [zpbrent](https://github.com/zpbrent) + +3.2.4 / 2021-02-12 +================== + * fix(utils): make clone() only copy own properties Automattic/mongoose#9876 + +3.2.3 / 2020-12-10 +================== + * fix(utils): avoid copying special properties like `__proto__` when merging and cloning. Fix CVE-2020-35149 + +3.2.2 / 2019-09-22 +================== + * fix: dont re-call setOptions() when pulling base class options Automattic/mongoose#8159 + +3.2.1 / 2018-08-24 +================== + * chore: upgrade deps + +3.2.0 / 2018-08-24 +================== + * feat: add $useProjection to opt in to using `projection` instead of `fields` re: MongoDB deprecation warnings Automattic/mongoose#6880 + +3.1.2 / 2018-08-01 +================== + * chore: move eslint to devDependencies #110 [jakesjews](https://github.com/jakesjews) + +3.1.1 / 2018-07-30 +================== + * chore: add eslint #107 [Fonger](https://github.com/Fonger) + * docs: clean up readConcern docs #106 [Fonger](https://github.com/Fonger) + +3.1.0 / 2018-07-29 +================== + * feat: add `readConcern()` helper #105 [Fonger](https://github.com/Fonger) + * feat: add `maxTimeMS()` as alias of `maxTime()` #105 [Fonger](https://github.com/Fonger) + * feat: add `collation()` helper #105 [Fonger](https://github.com/Fonger) + +3.0.1 / 2018-07-02 +================== + * fix: parse sort array options correctly #103 #102 [Fonger](https://github.com/Fonger) + +3.0.0 / 2018-01-20 +================== + * chore: upgrade deps and add nsp + +3.0.0-rc0 / 2017-12-06 +====================== + * BREAKING CHANGE: remove support for node < 4 + * BREAKING CHANGE: remove support for retainKeyOrder, will always be true by default re: Automattic/mongoose#2749 + +2.3.3 / 2017-11-19 +================== + * fixed; catch sync errors in cursor.toArray() re: Automattic/mongoose#5812 + +2.3.2 / 2017-09-27 +================== + * fixed; bumped debug -> 2.6.9 re: #89 + +2.3.1 / 2017-05-22 +================== + * fixed; bumped debug -> 2.6.7 re: #86 + +2.3.0 / 2017-03-05 +================== + * added; replaceOne function + * added; deleteOne and deleteMany functions + +2.2.3 / 2017-01-31 +================== + * fixed; throw correct error when passing incorrectly formatted array to sort() + +2.2.2 / 2017-01-31 +================== + * fixed; allow passing maps to sort() + +2.2.1 / 2017-01-29 +================== + * fixed; allow passing string to hint() + +2.2.0 / 2017-01-08 +================== + * added; updateOne and updateMany functions + +2.1.0 / 2016-12-22 +================== + * added; ability to pass an array to select() #81 [dciccale](https://github.com/dciccale) + +2.0.0 / 2016-09-25 +================== + * added; support for mongodb driver 2.0 streams + +1.12.0 / 2016-09-25 +=================== + * added; `retainKeyOrder` option re: Automattic/mongoose#4542 + +1.11.0 / 2016-06-04 +=================== + * added; `.minDistance()` helper and minDistance for `.near()` Automattic/mongoose#4179 + +1.10.1 / 2016-04-26 +=================== + * fixed; ensure conditions is an object before assigning #75 + +1.10.0 / 2016-03-16 +================== + + * updated; bluebird to latest 2.10.2 version #74 [matskiv](https://github.com/matskiv) + +1.9.0 / 2016-03-15 +================== + * added; `.eq` as a shortcut for `.equals` #72 [Fonger](https://github.com/Fonger) + * added; ability to use array syntax for sort re: https://jira.mongodb.org/browse/NODE-578 #67 + +1.8.0 / 2016-03-01 +================== + * fixed; dont throw an error if count used with sort or select Automattic/mongoose#3914 + +1.7.0 / 2016-02-23 +================== + * fixed; don't treat objects with a length property as argument objects #70 + * added; `.findCursor()` method #69 [nswbmw](https://github.com/nswbmw) + * added; `_compiledUpdate` property #68 [nswbmw](https://github.com/nswbmw) + +1.6.2 / 2015-07-12 +================== + + * fixed; support exec cb being called synchronously #66 + +1.6.1 / 2015-06-16 +================== + + * fixed; do not treat $meta projection as inclusive [vkarpov15](https://github.com/vkarpov15) + +1.6.0 / 2015-05-27 +================== + + * update dependencies #65 [bachp](https://github.com/bachp) + +1.5.0 / 2015-03-31 +================== + + * fixed; debug output + * fixed; allow hint usage with count #61 [trueinsider](https://github.com/trueinsider) + +1.4.0 / 2015-03-29 +================== + + * added; object support to slice() #60 [vkarpov15](https://github.com/vkarpov15) + * debug; improved output #57 [flyvictor](https://github.com/flyvictor) + +1.3.0 / 2014-11-06 +================== + + * added; setTraceFunction() #53 from [jlai](https://github.com/jlai) + +1.2.1 / 2014-09-26 +================== + + * fixed; distinct assignment in toConstructor() #51 from [esco](https://github.com/esco) + +1.2.0 / 2014-09-18 +================== + + * added; stream() support for find() + +1.1.0 / 2014-09-15 +================== + + * add #then for co / koa support + * start checking code coverage + +1.0.0 / 2014-07-07 +================== + + * Remove broken require() calls until they're actually implemented #48 [vkarpov15](https://github.com/vkarpov15) + +0.9.0 / 2014-05-22 +================== + + * added; thunk() support + * release 0.8.0 + +0.8.0 / 2014-05-15 +================== + + * added; support for maxTimeMS #44 [yoitsro](https://github.com/yoitsro) + * updated; devDependency (driver to 1.4.4) + +0.7.0 / 2014-05-02 +================== + + * fixed; pass $maxDistance in $near object as described in docs #43 [vkarpov15](https://github.com/vkarpov15) + * fixed; cloning buffers #42 [gjohnson](https://github.com/gjohnson) + * tests; a little bit more `mongodb` agnostic #34 [refack](https://github.com/refack) + +0.6.0 / 2014-04-01 +================== + + * fixed; Allow $meta args in sort() so text search sorting works #37 [vkarpov15](https://github.com/vkarpov15) + +0.5.3 / 2014-02-22 +================== + + * fixed; cloning mongodb.Binary + +0.5.2 / 2014-01-30 +================== + + * fixed; cloning ObjectId constructors + * fixed; cloning of ReadPreferences #30 [ashtuchkin](https://github.com/ashtuchkin) + * tests; use specific mongodb version #29 [AvianFlu](https://github.com/AvianFlu) + * tests; remove dependency on ObjectId #28 [refack](https://github.com/refack) + * tests; add failing ReadPref test + +0.5.1 / 2014-01-17 +================== + + * added; deprecation notice to tags parameter #27 [ashtuchkin](https://github.com/ashtuchkin) + * readme; add links + +0.5.0 / 2014-01-16 +================== + + * removed; mongodb driver dependency #26 [ashtuchkin](https://github.com/ashtuchkin) + * removed; first class support of read preference tags #26 (still supported though) [ashtuchkin](https://github.com/ashtuchkin) + * added; better ObjectId clone support + * fixed; cloning objects that have no constructor #21 + * docs; cleaned up [ashtuchkin](https://github.com/ashtuchkin) + +0.4.2 / 2014-01-08 +================== + + * updated; debug module 0.7.4 [refack](https://github.com/refack) + +0.4.1 / 2014-01-07 +================== + + * fixed; inclusive/exclusive logic + +0.4.0 / 2014-01-06 +================== + + * added; selected() + * added; selectedInclusively() + * added; selectedExclusively() + +0.3.3 / 2013-11-14 +================== + + * Fix Mongo DB Dependency #20 [rschmukler](https://github.com/rschmukler) + +0.3.2 / 2013-09-06 +================== + + * added; geometry support for near() + +0.3.1 / 2013-08-22 +================== + + * fixed; update retains key order #19 + +0.3.0 / 2013-08-22 +================== + + * less hardcoded isNode env detection #18 [vshulyak](https://github.com/vshulyak) + * added; validation of findAndModify varients + * clone update doc before execution + * stricter env checks + +0.2.7 / 2013-08-2 +================== + + * Now support GeoJSON point values for Query#near + +0.2.6 / 2013-07-30 +================== + + * internally, 'asc' and 'desc' for sorts are now converted into 1 and -1, respectively + +0.2.5 / 2013-07-30 +================== + + * updated docs + * changed internal representation of `sort` to use objects instead of arrays + +0.2.4 / 2013-07-25 +================== + + * updated; sliced to 0.0.5 + +0.2.3 / 2013-07-09 +================== + + * now using a callback in collection.find instead of directly calling toArray() on the cursor [ebensing](https://github.com/ebensing) + +0.2.2 / 2013-07-09 +================== + + * now exposing mongodb export to allow for better testing [ebensing](https://github.com/ebensing) + +0.2.1 / 2013-07-08 +================== + + * select no longer accepts arrays as parameters [ebensing](https://github.com/ebensing) + +0.2.0 / 2013-07-05 +================== + + * use $geoWithin by default + +0.1.2 / 2013-07-02 +================== + + * added use$geoWithin flag [ebensing](https://github.com/ebensing) + * fix read preferences typo [ebensing](https://github.com/ebensing) + * fix reference to old param name in exists() [ebensing](https://github.com/ebensing) + +0.1.1 / 2013-06-24 +================== + + * fixed; $intersects -> $geoIntersects #14 [ebensing](https://github.com/ebensing) + * fixed; Retain key order when copying objects #15 [ebensing](https://github.com/ebensing) + * bump mongodb dev dep + +0.1.0 / 2013-05-06 +================== + + * findAndModify; return the query + * move mquery.proto.canMerge to mquery.canMerge + * overwrite option now works with non-empty objects + * use strict mode + * validate count options + * validate distinct options + * add aggregate to base collection methods + * clone merge arguments + * clone merged update arguments + * move subclass to mquery.prototype.toConstructor + * fixed; maxScan casing + * use regexp-clone + * added; geometry/intersects support + * support $and + * near: do not use "radius" + * callbacks always fire on next turn of loop + * defined collection interface + * remove time from tests + * clarify goals + * updated docs; + +0.0.1 / 2012-12-15 +================== + + * initial release diff --git a/gateway/node_modules/mquery/LICENSE b/gateway/node_modules/mquery/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..38c529daa677faf963fc24fcd00cdb5b61f07102 --- /dev/null +++ b/gateway/node_modules/mquery/LICENSE @@ -0,0 +1,22 @@ +(The MIT License) + +Copyright (c) 2012 [Aaron Heckmann](aaron.heckmann+github@gmail.com) + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/gateway/node_modules/mquery/README.md b/gateway/node_modules/mquery/README.md new file mode 100644 index 0000000000000000000000000000000000000000..86a06ebc0f92ec70e405c97c1027ede1b3ede312 --- /dev/null +++ b/gateway/node_modules/mquery/README.md @@ -0,0 +1,1387 @@ +# mquery + +`mquery` is a fluent mongodb query builder designed to run in multiple environments. + +[![Build Status](https://travis-ci.org/aheckmann/mquery.svg?branch=master)](https://travis-ci.org/aheckmann/mquery) +[![NPM version](https://badge.fury.io/js/mquery.svg)](http://badge.fury.io/js/mquery) + +[![npm](https://nodei.co/npm/mquery.png)](https://www.npmjs.com/package/mquery) + +## Features + +- fluent query builder api +- custom base query support +- MongoDB 2.4 geoJSON support +- method + option combinations validation +- node.js driver compatibility +- environment detection +- [debug](https://github.com/visionmedia/debug) support +- separated collection implementations for maximum flexibility + +## Use + +```js +const mongo = require('mongodb'); + +const client = new mongo.MongoClient(uri); +await client.connect(); +// get a collection +const collection = client.collection('artists'); + +// pass it to the constructor +await mquery(collection).find({...}); + +// or pass it to the collection method +const docs = await mquery().find({...}).collection(collection); + +// or better yet, create a custom query constructor that has it always set +const Artist = mquery(collection).toConstructor(); +const docs = await Artist().find(...).where(...); +``` + +`mquery` requires a collection object to work with. In the example above we just pass the collection object created using the official [MongoDB driver](https://github.com/mongodb/node-mongodb-native). + +## Fluent API + +- [mquery](#mquery) + - [Features](#features) + - [Use](#use) + - [Fluent API](#fluent-api) + - [Helpers](#helpers) + - [find()](#find) + - [findOne()](#findone) + - [countDocuments()](#countdocuments) + - [estimatedDocumentCount()](#estimateddocumentcount) + - [findOneAndUpdate()](#findoneandupdate) + - [findOneAndUpdate() options](#findoneandupdate-options) + - [findOneAndReplace()](#findoneandreplace) + - [findOneAndReplace() options](#findoneandreplace-options) + - [findOneAndRemove()](#findoneandremove) + - [findOneAndRemove() options](#findoneandremove-options) + - [distinct()](#distinct) + - [updateMany()](#updatemany) + - [updateOne()](#updateone) + - [replaceOne()](#replaceone) + - [deleteOne()](#deleteone) + - [deleteMany()](#deletemany) + - [exec()](#exec) + - [cursor()](#cursor) + - [stream()](#stream) + - [all()](#all) + - [and()](#and) + - [box()](#box) + - [circle()](#circle) + - [elemMatch()](#elemmatch) + - [equals()](#equals) + - [eq()](#eq) + - [exists()](#exists) + - [geometry()](#geometry) + - [gt()](#gt) + - [gte()](#gte) + - [in()](#in) + - [intersects()](#intersects) + - [lt()](#lt) + - [lte()](#lte) + - [maxDistance()](#maxdistance) + - [mod()](#mod) + - [ne()](#ne) + - [nin()](#nin) + - [nor()](#nor) + - [near()](#near) + - [Example](#example) + - [or()](#or) + - [polygon()](#polygon) + - [regex()](#regex) + - [select()](#select) + - [String syntax](#string-syntax) + - [selected()](#selected) + - [selectedInclusively()](#selectedinclusively) + - [selectedExclusively()](#selectedexclusively) + - [size()](#size) + - [slice()](#slice) + - [within()](#within) + - [where()](#where) + - [$where()](#where-1) + - [batchSize()](#batchsize) + - [collation()](#collation) + - [comment()](#comment) + - [hint()](#hint) + - [j()](#j) + - [limit()](#limit) + - [maxTime()](#maxtime) + - [skip()](#skip) + - [sort()](#sort) + - [read()](#read) + - [Preferences:](#preferences) + - [Preference Tags:](#preference-tags) + - [readConcern()](#readconcern) + - [Read Concern Level:](#read-concern-level) + - [writeConcern()](#writeconcern) + - [Write Concern:](#write-concern) + - [slaveOk()](#slaveok) + - [tailable()](#tailable) + - [wtimeout()](#wtimeout) + - [Helpers](#helpers-1) + - [collection()](#collection) + - [then()](#then) + - [merge(object)](#mergeobject) + - [setOptions(options)](#setoptionsoptions) + - [setOptions() options](#setoptions-options) + - [setTraceFunction(func)](#settracefunctionfunc) + - [mquery.setGlobalTraceFunction(func)](#mquerysetglobaltracefunctionfunc) + - [mquery.canMerge(conditions)](#mquerycanmergeconditions) + - [mquery.use$geoWithin](#mqueryusegeowithin) + - [Custom Base Queries](#custom-base-queries) + - [Validation](#validation) + - [Debug support](#debug-support) + - [General compatibility](#general-compatibility) + - [ObjectIds](#objectids) + - [Read Preferences](#read-preferences) + - [Future goals](#future-goals) + - [Installation](#installation) + - [License](#license) + +## Helpers + +- [collection](#collection) +- [then](#then) +- [merge](#mergeobject) +- [setOptions](#setoptionsoptions) +- [setTraceFunction](#settracefunctionfunc) +- [mquery.setGlobalTraceFunction](#mquerysetglobaltracefunctionfunc) +- [mquery.canMerge](#mquerycanmergeconditions) +- [mquery.use$geoWithin](#mqueryusegeowithin) + +### find() + +Declares this query a _find_ query. Optionally pass a match clause. + +```js +mquery().find() +mquery().find(match) +await mquery().find() +const docs = await mquery().find(match); +assert(Array.isArray(docs)); +``` + +### findOne() + +Declares this query a _findOne_ query. Optionally pass a match clause. + +```js +mquery().findOne() +mquery().findOne(match) +await mquery().findOne() +const doc = await mquery().findOne(match); +if (doc) { + // the document may not be found + console.log(doc); +} +``` + +### countDocuments() + +Declares this query a _countDocuments_ query. Optionally pass a match clause. + +```js +mquery().countDocuments() +mquery().countDocuments(match) +await mquery().countDocuments() +const number = await mquery().countDocuments(match); +console.log('we found %d matching documents', number); +``` + +### estimatedDocumentCount() + +Declares this query an _estimatedDocumentCount_ query. Gets an estimated count of documents in a collection using collection metadata. + +```js +mquery().estimatedDocumentCount() +const number = await mquery().estimatedDocumentCount(); +console.log('estimated documents: %d', number); +``` + +### findOneAndUpdate() + +Declares this query a _findAndModify_ with update query. Optionally pass a match clause, update document, options. + +When executed, the first matching document (if found) is modified according to the update document and passed back. + +#### findOneAndUpdate() options + +Options are passed to the `setOptions()` method. + +- `new`: boolean - true to return the modified document rather than the original. defaults to false +- `upsert`: boolean - creates the object if it doesn't exist. defaults to false +- `sort`: if multiple docs are found by the match condition, sets the sort order to choose which doc to update + +```js +query.findOneAndUpdate() +query.findOneAndUpdate(updateDocument) +query.findOneAndUpdate(match, updateDocument) +query.findOneAndUpdate(match, updateDocument, options) + +// the following all execute the command +await query.findOneAndUpdate() +await query.findOneAndUpdate(updateDocument) +await query.findOneAndUpdate(match, updateDocument) +const doc = await query.findOneAndUpdate(match, updateDocument, options); +if (doc) { + // the document may not be found + console.log(doc); +} +``` + +### findOneAndReplace() + +Declares this query a _findOneAndReplace_ query. Finds a matching document, replaces it with the provided replacement, and returns the found document (if any). + +#### findOneAndReplace() options + +Options are passed to the `setOptions()` method. + +- `new`: boolean - true to return the modified document rather than the original. defaults to false +- `upsert`: boolean - creates the object if it doesn't exist. defaults to false +- `sort`: if multiple docs are found by the match condition, sets the sort order to choose which doc to replace + +```js +query.findOneAndReplace() +query.findOneAndReplace(replacement) +query.findOneAndReplace(match, replacement) +query.findOneAndReplace(match, replacement, options) + +// the following all execute the command +await query.findOneAndReplace() +await query.findOneAndReplace(replacement) +await query.findOneAndReplace(match, replacement) +const doc = await query.findOneAndReplace(match, replacement, options); +if (doc) { + // the document may not be found + console.log(doc); +} +``` + +### findOneAndRemove() + +Declares this query a _findAndModify_ with remove query. Alias of findOneAndDelete. +Optionally pass a match clause, options. + +When executed, the first matching document (if found) is modified according to the update document, removed from the collection and passed as a result. + +#### findOneAndRemove() options + +Options are passed to the `setOptions()` method. + +- `sort`: if multiple docs are found by the condition, sets the sort order to choose which doc to modify and remove + +```js +A.where().findOneAndDelete() +A.where().findOneAndRemove() +A.where().findOneAndRemove(match) +A.where().findOneAndRemove(match, options) + +// the following all execute the command +await A.where().findOneAndRemove() +await A.where().findOneAndRemove(match) +const doc = await A.where().findOneAndRemove(match, options); +if (doc) { + // the document may not be found + console.log(doc); +} +``` + +### distinct() + +Declares this query a _distinct_ query. Optionally pass the distinct field, a match clause. + +```js +mquery().distinct() +mquery().distinct(match) +mquery().distinct(match, field) +mquery().distinct(field) + +// the following all execute the command +await mquery().distinct() +await mquery().distinct(field) +await mquery().distinct(match) +const result = await mquery().distinct(match, field); +console.log(result); +``` + +### updateMany() + +Declares this query an _updateMany_ query. Updates all documents that match `criteria`. + +When executed, the first argument is the query, and the second argument is the update document. + +_All paths passed that are not $atomic operations will become $set ops._ + +```js +mquery().updateMany({ name: /^match/ }, { field: 'value' }) +await mquery().updateMany({ name: /^match/ }, { field: 'value' }) +await mquery().where({ name: /^match/ }).updateMany({ field: 'value' }) +``` + +### updateOne() + +Declares this query an _updateOne_ query. Updates only the first document that matches `criteria`. + +When executed, the first argument is the query, and the second argument is the update document. + +_All paths passed that are not $atomic operations will become $set ops._ + +```js +mquery().updateOne({ name: 'match' }, { field: 'value' }) +await mquery().updateOne({ name: 'match' }, { field: 'value' }) +await mquery().where({ name: 'match' }).updateOne({ field: 'value' }) +``` + +### replaceOne() + +Declares this query a _replaceOne_ query. Replaces the first document that matches `criteria` with the provided replacement document. + +Similar to `updateOne()`, except `replaceOne()` is not allowed to use atomic modifiers (`$set`, `$push`, etc.). Calling `replaceOne()` will always replace the existing doc. + +```js +mquery().replaceOne({ _id: 1 }, { name: 'new name', age: 25 }) +await mquery().replaceOne({ _id: 1 }, { name: 'new name', age: 25 }) +``` + +### deleteOne() + +Declares this query a _deleteOne_ query. Deletes the first document that matches `criteria`. + +```js +mquery().deleteOne({ name: 'match' }) +await mquery().deleteOne({ name: 'match' }) +await mquery().where({ name: 'match' }).deleteOne() +``` + +### deleteMany() + +Declares this query a _deleteMany_ query. Deletes all documents that match `criteria`. + +```js +mquery().deleteMany({ name: /^match/ }) +await mquery().deleteMany({ name: /^match/ }) +await mquery().where({ name: /^match/ }).deleteMany() +``` + +### exec() + +Executes the query. + +```js +const docs = await mquery().findOne().where('route').intersects(polygon).exec() +``` + +### cursor() + +Returns a cursor for the given `find` query. + +```js +const cursor = mquery().find({ name: /^match/ }).cursor(); +cursor.on('data', function(doc) { + console.log(doc); +}); +cursor.on('end', function() { + console.log('done'); +}); +``` + +Note: this only works with `find()` operations. + +### stream() + +Executes the query and returns a stream. + +```js +var stream = mquery().find().stream(options); +stream.on('data', cb); +stream.on('close', fn); +``` + +Note: this only works with `find()` operations. + +Note: returns the stream object directly from the node-mongodb-native driver. (currently streams1 type stream). Any options will be passed along to the [driver method](http://mongodb.github.io/node-mongodb-native/api-generated/cursor.html#stream). + +--- + +### all() + +Specifies an `$all` query condition + +```js +mquery().where('permission').all(['read', 'write']) +``` + +[MongoDB documentation](http://docs.mongodb.org/manual/reference/operator/all/) + +### and() + +Specifies arguments for an `$and` condition + +```js +mquery().and([{ color: 'green' }, { status: 'ok' }]) +``` + +[MongoDB documentation](http://docs.mongodb.org/manual/reference/operator/and/) + +### box() + +Specifies a `$box` condition + +```js +var lowerLeft = [40.73083, -73.99756] +var upperRight= [40.741404, -73.988135] + +mquery().where('location').within().box(lowerLeft, upperRight) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/box/) + +### circle() + +Specifies a `$center` or `$centerSphere` condition. + +```js +var area = { center: [50, 50], radius: 10, unique: true } +query.where('loc').within().circle(area) +query.circle('loc', area); + +// for spherical calculations +var area = { center: [50, 50], radius: 10, unique: true, spherical: true } +query.where('loc').within().circle(area) +query.circle('loc', area); +``` + +- [MongoDB Documentation - center](http://docs.mongodb.org/manual/reference/operator/center/) +- [MongoDB Documentation - centerSphere](http://docs.mongodb.org/manual/reference/operator/centerSphere/) + +### elemMatch() + +Specifies an `$elemMatch` condition + +```js +query.where('comment').elemMatch({ author: 'autobot', votes: {$gte: 5}}) + +query.elemMatch('comment', function (elem) { + elem.where('author').equals('autobot'); + elem.where('votes').gte(5); +}) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/elemMatch/) + +### equals() + +Specifies the complementary comparison value for the path specified with `where()`. + +```js +mquery().where('age').equals(49); + +// is the same as + +mquery().where({ 'age': 49 }); +``` + +### eq() + +Alias of `equals()`. Specifies the complementary comparison value for the path specified with `where()`. + +```js +mquery().where('age').eq(49); + +// is the same as + +mquery().where('age').equals(49); + +// is the same as + +mquery().where({ 'age': 49 }); +``` + +### exists() + +Specifies an `$exists` condition + +```js +// { name: { $exists: true }} +mquery().where('name').exists() +mquery().where('name').exists(true) +mquery().exists('name') + +// { name: { $exists: false }} +mquery().where('name').exists(false); +mquery().exists('name', false); +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/exists/) + +### geometry() + +Specifies a `$geometry` condition + +```js +var polyA = [[[ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ]]] +query.where('loc').within().geometry({ type: 'Polygon', coordinates: polyA }) + +// or +var polyB = [[ 0, 0 ], [ 1, 1 ]] +query.where('loc').within().geometry({ type: 'LineString', coordinates: polyB }) + +// or +var polyC = [ 0, 0 ] +query.where('loc').within().geometry({ type: 'Point', coordinates: polyC }) + +// or +query.where('loc').intersects().geometry({ type: 'Point', coordinates: polyC }) + +// or +query.where('loc').near().geometry({ type: 'Point', coordinates: [3,5] }) +``` + +`geometry()` **must** come after `intersects()`, `within()`, or `near()`. + +The `object` argument must contain `type` and `coordinates` properties. + +- type `String` +- coordinates `Array` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/geometry/) + +### gt() + +Specifies a `$gt` query condition. + +```js +mquery().where('clicks').gt(999) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/gt/) + +### gte() + +Specifies a `$gte` query condition. + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/gte/) + +```js +mquery().where('clicks').gte(1000) +``` + +### in() + +Specifies an `$in` query condition. + +```js +mquery().where('author_id').in([3, 48901, 761]) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/in/) + +### intersects() + +Declares an `$geoIntersects` query for `geometry()`. + +```js +query.where('path').intersects().geometry({ + type: 'LineString' + , coordinates: [[180.0, 11.0], [180, 9.0]] +}) + +// geometry arguments are supported +query.where('path').intersects({ + type: 'LineString' + , coordinates: [[180.0, 11.0], [180, 9.0]] +}) +``` + +**Must** be used after `where()`. + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/geoIntersects/) + +### lt() + +Specifies a `$lt` query condition. + +```js +mquery().where('clicks').lt(50) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/lt/) + +### lte() + +Specifies a `$lte` query condition. + +```js +mquery().where('clicks').lte(49) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/lte/) + +### maxDistance() + +Specifies a `$maxDistance` query condition. + +```js +mquery().where('location').near({ center: [139, 74.3] }).maxDistance(5) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/maxDistance/) + +### mod() + +Specifies a `$mod` condition + +```js +mquery().where('count').mod(2, 0) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/mod/) + +### ne() + +Specifies a `$ne` query condition. + +```js +mquery().where('status').ne('ok') +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/ne/) + +### nin() + +Specifies an `$nin` query condition. + +```js +mquery().where('author_id').nin([3, 48901, 761]) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/nin/) + +### nor() + +Specifies arguments for an `$nor` condition. + +```js +mquery().nor([{ color: 'green' }, { status: 'ok' }]) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/nor/) + +### near() + +Specifies arguments for a `$near` or `$nearSphere` condition. + +These operators return documents sorted by distance. + +#### Example + +```js +query.where('loc').near({ center: [10, 10] }); +query.where('loc').near({ center: [10, 10], maxDistance: 5 }); +query.near('loc', { center: [10, 10], maxDistance: 5 }); + +// GeoJSON +query.where('loc').near({ center: { type: 'Point', coordinates: [10, 10] }}); +query.where('loc').near({ center: { type: 'Point', coordinates: [10, 10] }, maxDistance: 5, spherical: true }); +query.where('loc').near().geometry({ type: 'Point', coordinates: [10, 10] }); + +// For a $nearSphere condition, pass the `spherical` option. +query.near({ center: [10, 10], maxDistance: 5, spherical: true }); +``` + +[MongoDB Documentation](http://www.mongodb.org/display/DOCS/Geospatial+Indexing) + +### or() + +Specifies arguments for an `$or` condition. + +```js +mquery().or([{ color: 'red' }, { status: 'emergency' }]) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/or/) + +### polygon() + +Specifies a `$polygon` condition + +```js +mquery().where('loc').within().polygon([10,20], [13, 25], [7,15]) +mquery().polygon('loc', [10,20], [13, 25], [7,15]) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/polygon/) + +### regex() + +Specifies a `$regex` query condition. + +```js +mquery().where('name').regex(/^sixstepsrecords/) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/regex/) + +### select() + +Specifies which document fields to include or exclude + +```js +// 1 means include, 0 means exclude +mquery().select({ name: 1, address: 1, _id: 0 }) + +// or + +mquery().select('name address -_id') +``` + +#### String syntax + +When passing a string, prefixing a path with `-` will flag that path as excluded. When a path does not have the `-` prefix, it is included. + +```js +// include a and b, exclude c +query.select('a b -c'); + +// or you may use object notation, useful when +// you have keys already prefixed with a "-" +query.select({a: 1, b: 1, c: 0}); +``` + +_Cannot be used with `distinct()`._ + +### selected() + +Determines if the query has selected any fields. + +```js +var query = mquery(); +query.selected() // false +query.select('-name'); +query.selected() // true +``` + +### selectedInclusively() + +Determines if the query has selected any fields inclusively. + +```js +var query = mquery().select('name'); +query.selectedInclusively() // true + +var query = mquery(); +query.selected() // false +query.select('-name'); +query.selectedInclusively() // false +query.selectedExclusively() // true +``` + +### selectedExclusively() + +Determines if the query has selected any fields exclusively. + +```js +var query = mquery().select('-name'); +query.selectedExclusively() // true + +var query = mquery(); +query.selected() // false +query.select('name'); +query.selectedExclusively() // false +query.selectedInclusively() // true +``` + +### size() + +Specifies a `$size` query condition. + +```js +mquery().where('someArray').size(6) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/size/) + +### slice() + +Specifies a `$slice` projection for a `path` + +```js +mquery().where('comments').slice(5) +mquery().where('comments').slice(-5) +mquery().where('comments').slice([-10, 5]) +``` + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/projection/slice/) + +### within() + +Sets a `$geoWithin` or `$within` argument for geo-spatial queries. + +```js +mquery().within().box() +mquery().within().circle() +mquery().within().geometry() + +mquery().where('loc').within({ center: [50,50], radius: 10, unique: true, spherical: true }); +mquery().where('loc').within({ box: [[40.73, -73.9], [40.7, -73.988]] }); +mquery().where('loc').within({ polygon: [[],[],[],[]] }); + +mquery().where('loc').within([], [], []) // polygon +mquery().where('loc').within([], []) // box +mquery().where('loc').within({ type: 'LineString', coordinates: [...] }); // geometry +``` + +As of mquery 2.0, `$geoWithin` is used by default. This impacts you if running MongoDB < 2.4. To alter this behavior, see [mquery.use$geoWithin](#mqueryusegeowithin). + +**Must** be used after `where()`. + +[MongoDB Documentation](http://docs.mongodb.org/manual/reference/operator/geoWithin/) + +### where() + +Specifies a `path` for use with chaining + +```js +// instead of writing: +mquery().find({age: {$gte: 21, $lte: 65}}); + +// we can instead write: +mquery().where('age').gte(21).lte(65); + +// passing query conditions is permitted too +mquery().find().where({ name: 'vonderful' }) + +// chaining +await mquery() + .where('age').gte(21).lte(65) + .where({ 'name': /^vonderful/i }) + .where('friends').slice(10) + .exec() +``` + +### $where() + +Specifies a `$where` condition. + +Use `$where` when you need to select documents using a JavaScript expression. + +```js +await query.$where('this.comments.length > 10 || this.name.length > 5').exec() + +query.$where(function () { + return this.comments.length > 10 || this.name.length > 5; +}) +``` + +Only use `$where` when you have a condition that cannot be met using other MongoDB operators like `$lt`. Be sure to read about all of [its caveats](http://docs.mongodb.org/manual/reference/operator/where/) before using. + +--- + +### batchSize() + +Specifies the batchSize option. + +```js +query.batchSize(100) +``` + +_Cannot be used with `distinct()`._ + +[MongoDB documentation](http://docs.mongodb.org/manual/reference/method/cursor.batchSize/) + +### collation() + +Specifies the collation option. + +```js +query.collation({ locale: "en_US", strength: 1 }) +``` + +[MongoDB documentation](https://docs.mongodb.com/manual/reference/method/cursor.collation/#cursor.collation) + +### comment() + +Specifies the comment option. + +```js +query.comment('login query'); +``` + +_Cannot be used with `distinct()`._ + +[MongoDB documentation](http://docs.mongodb.org/manual/reference/operator/) + +### hint() + +Sets query hints. + +```js +mquery().hint({ indexA: 1, indexB: -1 }) +``` + +_Cannot be used with `distinct()`._ + +[MongoDB documentation](http://docs.mongodb.org/manual/reference/operator/hint/) + +### j() + +Requests acknowledgement that this operation has been persisted to MongoDB's on-disk journal. + +This option is only valid for operations that write to the database: + +- `deleteOne()` +- `deleteMany()` +- `findOneAndDelete()` +- `findOneAndUpdate()` +- `updateOne()` +- `updateMany()` + +Defaults to the `j` value if it is specified in [writeConcern](#writeconcern) + +```js +mquery().j(true); +``` + +### limit() + +Specifies the limit option. + +```js +query.limit(20) +``` + +_Cannot be used with `distinct()`._ + +[MongoDB documentation](http://docs.mongodb.org/manual/reference/method/cursor.limit/) + +### maxTime() + +Specifies the maxTimeMS option. + +```js +query.maxTime(100) +query.maxTimeMS(100) +``` + +[MongoDB documentation](http://docs.mongodb.org/manual/reference/method/cursor.maxTimeMS/) + +### skip() + +Specifies the skip option. + +```js +query.skip(100).limit(20) +``` + +_Cannot be used with `distinct()`._ + +[MongoDB documentation](http://docs.mongodb.org/manual/reference/method/cursor.skip/) + +### sort() + +Sets the query sort order. + +If an object is passed, key values allowed are `asc`, `desc`, `ascending`, `descending`, `1`, and `-1`. + +If a string is passed, it must be a space delimited list of path names. The sort order of each path is ascending unless the path name is prefixed with `-` which will be treated as descending. + +```js +// these are equivalent +query.sort({ field: 'asc', test: -1 }); +query.sort('field -test'); +``` + +_Cannot be used with `distinct()`._ + +[MongoDB documentation](http://docs.mongodb.org/manual/reference/method/cursor.sort/) + +### read() + +Sets the readPreference option for the query. + +```js +mquery().read('primary') +mquery().read('p') // same as primary + +mquery().read('primaryPreferred') +mquery().read('pp') // same as primaryPreferred + +mquery().read('secondary') +mquery().read('s') // same as secondary + +mquery().read('secondaryPreferred') +mquery().read('sp') // same as secondaryPreferred + +mquery().read('nearest') +mquery().read('n') // same as nearest + +mquery().setReadPreference('primary') // alias of .read() +``` + +#### Preferences: + +- `primary` - (default) Read from primary only. Operations will produce an error if primary is unavailable. Cannot be combined with tags. +- `secondary` - Read from secondary if available, otherwise error. +- `primaryPreferred` - Read from primary if available, otherwise a secondary. +- `secondaryPreferred` - Read from a secondary if available, otherwise read from the primary. +- `nearest` - All operations read from among the nearest candidates, but unlike other modes, this option will include both the primary and all secondaries in the random selection. + +Aliases + +- `p` primary +- `pp` primaryPreferred +- `s` secondary +- `sp` secondaryPreferred +- `n` nearest + +#### Preference Tags: + +To keep the separation of concerns between `mquery` and your driver +clean, `mquery#read()` no longer handles specifying a second `tags` argument as of version 0.5. +If you need to specify tags, pass any non-string argument as the first argument. +`mquery` will pass this argument untouched to your collections methods later. +For example: + +```js +// example of specifying tags using the Node.js driver +var ReadPref = require('mongodb').ReadPreference; +var preference = new ReadPref('secondary', [{ dc:'sf', s: 1 },{ dc:'ma', s: 2 }]); +mquery(...).read(preference).exec(); +``` + +Read more about how to use read preferences [here](http://docs.mongodb.org/manual/applications/replication/#read-preference) and [here](http://mongodb.github.com/node-mongodb-native/driver-articles/anintroductionto1_1and2_2.html#read-preferences). + +### readConcern() + +Sets the readConcern option for the query. + +```js +// local +mquery().readConcern('local') +mquery().readConcern('l') +mquery().r('l') + +// available +mquery().readConcern('available') +mquery().readConcern('a') +mquery().r('a') + +// majority +mquery().readConcern('majority') +mquery().readConcern('m') +mquery().r('m') + +// linearizable +mquery().readConcern('linearizable') +mquery().readConcern('lz') +mquery().r('lz') + +// snapshot +mquery().readConcern('snapshot') +mquery().readConcern('s') +mquery().r('s') +``` + +#### Read Concern Level: + +- `local` - The query returns from the instance with no guarantee guarantee that the data has been written to a majority of the replica set members (i.e. may be rolled back). (MongoDB 3.2+) +- `available` - The query returns from the instance with no guarantee guarantee that the data has been written to a majority of the replica set members (i.e. may be rolled back). (MongoDB 3.6+) +- `majority` - The query returns the data that has been acknowledged by a majority of the replica set members. The documents returned by the read operation are durable, even in the event of failure. (MongoDB 3.2+) +- `linearizable` - The query returns data that reflects all successful majority-acknowledged writes that completed prior to the start of the read operation. The query may wait for concurrently executing writes to propagate to a majority of replica set members before returning results. (MongoDB 3.4+) +- `snapshot` - Only available for operations within multi-document transactions. Upon transaction commit with write concern "majority", the transaction operations are guaranteed to have read from a snapshot of majority-committed data. (MongoDB 4.0+) + +Aliases + +- `l` local +- `a` available +- `m` majority +- `lz` linearizable +- `s` snapshot + +Read more about how to use read concern [here](https://docs.mongodb.com/manual/reference/read-concern/). + +### writeConcern() + +Sets the writeConcern option for the query. + +This option is only valid for operations that write to the database: + +- `deleteOne()` +- `deleteMany()` +- `findOneAndDelete()` +- `findOneAndUpdate()` +- `updateOne()` +- `updateMany()` + +```js +mquery().writeConcern(0) +mquery().writeConcern(1) +mquery().writeConcern({ w: 1, j: true, wtimeout: 2000 }) +mquery().writeConcern('majority') +mquery().writeConcern('m') // same as majority +mquery().writeConcern('tagSetName') // if the tag set is 'm', use .writeConcern({ w: 'm' }) instead +mquery().w(1) // w is alias of writeConcern +``` + +#### Write Concern: + +writeConcern({ w: ``, j: ``, wtimeout: `` }`) + +- the w option to request acknowledgement that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags +- the j option to request acknowledgement that the write operation has been written to the journal +- the wtimeout option to specify a time limit to prevent write operations from blocking indefinitely + +Can be break down to use the following syntax: + +mquery().w(``).j(``).wtimeout(``) + +Read more about how to use write concern [here](https://docs.mongodb.com/manual/reference/write-concern/) + +### slaveOk() + +Sets the slaveOk option. `true` allows reading from secondaries. + +**deprecated** use [read()](#read) preferences instead if on mongodb >= 2.2 + +```js +query.slaveOk() // true +query.slaveOk(true) +query.slaveOk(false) +``` + +[MongoDB documentation](http://docs.mongodb.org/manual/reference/method/rs.slaveOk/) + +### tailable() + +Sets tailable option. + +```js +mquery().tailable() <== true +mquery().tailable(true) +mquery().tailable(false) +``` + +_Cannot be used with `distinct()`._ + +[MongoDB Documentation](http://docs.mongodb.org/manual/tutorial/create-tailable-cursor/) + +### wtimeout() + +Specifies a time limit, in milliseconds, for the write concern. If `w > 1`, it is maximum amount of time to +wait for this write to propagate through the replica set before this operation fails. The default is `0`, which means no timeout. + +This option is only valid for operations that write to the database: + +- `deleteOne()` +- `deleteMany()` +- `findOneAndDelete()` +- `findOneAndUpdate()` +- `updateOne()` +- `updateMany()` + +Defaults to `wtimeout` value if it is specified in [writeConcern](#writeconcern) + +```js +mquery().wtimeout(2000) +mquery().wTimeout(2000) +``` + +## Helpers + +### collection() + +Sets the querys collection. + +```js +mquery().collection(aCollection) +``` + +### then() + +Executes the query and returns a promise which will be resolved with the query results or rejected if the query responds with an error. + +```js +mquery().find(..).then(success, error); +``` + +This is very useful when combined with [co](https://github.com/visionmedia/co) or [koa](https://github.com/koajs/koa), which automatically resolve promise-like objects for you. + +```js +co(function*(){ + var doc = yield mquery().findOne({ _id: 499 }); + console.log(doc); // { _id: 499, name: 'amazing', .. } +})(); +``` + +_NOTE_: +The returned promise is a [bluebird](https://github.com/petkaantonov/bluebird/) promise but this is customizable. If you want to +use your favorite promise library, simply set `mquery.Promise = YourPromiseConstructor`. +Your `Promise` must be [promises A+](http://promisesaplus.com/) compliant. + +### merge(object) + +Merges other mquery or match condition objects into this one. When an mquery instance is passed, its match conditions, field selection and options are merged. + +```js +const drum = mquery({ type: 'drum' }).collection(instruments); +const redDrum = mquery({ color: 'red' }).merge(drum); +const n = await redDrum.count(); +console.log('there are %d red drums', n); +``` + +Internally uses `mquery.canMerge` to determine validity. + +### setOptions(options) + +Sets query options. + +```js +mquery().setOptions({ collection: coll, limit: 20 }) +``` + +#### setOptions() options + +- [tailable](#tailable) * +- [sort](#sort) * +- [limit](#limit) * +- [skip](#skip) * +- [maxTime](#maxtime) * +- [batchSize](#batchsize) * +- [comment](#comment) * +- [hint](#hint) * +- [collection](#collection): the collection to query against + +_* denotes a query helper method is also available_ + +### setTraceFunction(func) + +Set a function to trace this query. Useful for profiling or logging. + +```js +function traceFunction (method, queryInfo, query) { + console.log('starting ' + method + ' query'); + + return function (err, result, millis) { + console.log('finished ' + method + ' query in ' + millis + 'ms'); + }; +} + +mquery().setTraceFunction(traceFunction).findOne({name: 'Joe'}, cb); +``` + +The trace function is passed (method, queryInfo, query) + +- method is the name of the method being called (e.g. findOne) +- queryInfo contains information about the query: + - conditions: query conditions/criteria + - options: options such as sort, fields, etc + - doc: document being updated +- query is the query object + +The trace function should return a callback function which accepts: +- err: error, if any +- result: result, if any +- millis: time spent waiting for query result + +NOTE: stream requests are not traced. + +### mquery.setGlobalTraceFunction(func) + +Similar to `setTraceFunction()` but automatically applied to all queries. + +```js +mquery.setTraceFunction(traceFunction); +``` + +### mquery.canMerge(conditions) + +Determines if `conditions` can be merged using `mquery().merge()`. + +```js +var query = mquery({ type: 'drum' }); +var okToMerge = mquery.canMerge(anObject) +if (okToMerge) { + query.merge(anObject); +} +``` + +## mquery.use$geoWithin + +MongoDB 2.4 introduced the `$geoWithin` operator which replaces and is 100% backward compatible with `$within`. As of mquery 0.2, we default to using `$geoWithin` for all `within()` calls. + +If you are running MongoDB < 2.4 this will be problematic. To force `mquery` to be backward compatible and always use `$within`, set the `mquery.use$geoWithin` flag to `false`. + +```js +mquery.use$geoWithin = false; +``` + +## Custom Base Queries + +Often times we want custom base queries that encapsulate predefined criteria. With `mquery` this is easy. First create the query you want to reuse and call its `toConstructor()` method which returns a new subclass of `mquery` that retains all options and criteria of the original. + +```js +var greatMovies = mquery(movieCollection).where('rating').gte(4.5).toConstructor(); + +// use it! +const n = await greatMovies().count(); +console.log('There are %d great movies', n); + +const docs = await greatMovies().where({ name: /^Life/ }).select('name').find(); +console.log(docs); +``` + +## Validation + +Method and options combinations are checked for validity at runtime to prevent creation of invalid query constructs. For example, a `distinct` query does not support specifying options like `hint` or field selection. In this case an error will be thrown so you can catch these mistakes in development. + +## Debug support + +Debug mode is provided through the use of the [debug](https://github.com/visionmedia/debug) module. To enable: + +```sh +DEBUG=mquery node yourprogram.js +``` + +Read the debug module documentation for more details. + +## General compatibility + +### ObjectIds + +`mquery` clones query arguments before passing them to a `collection` method for execution. +This prevents accidental side-affects to the objects you pass. +To clone `ObjectIds` we need to make some assumptions. + +First, to check if an object is an `ObjectId`, we check its constructors name. If it matches either +`ObjectId` or `ObjectID` we clone it. + +To clone `ObjectIds`, we call its optional `clone` method. If a `clone` method does not exist, we fall +back to calling `new obj.constructor(obj.id)`. We assume, for compatibility with the +Node.js driver, that the `ObjectId` instance has a public `id` property and that +when creating an `ObjectId` instance we can pass that `id` as an argument. + +#### Read Preferences + +`mquery` supports specifying [Read Preferences](https://www.mongodb.com/docs/manual/core/read-preference/) to control from which MongoDB node your query will read. +The Read Preferences spec also support specifying tags. To pass tags, some +drivers (Node.js driver) require passing a special constructor that handles both the read preference and its tags. +If you need to specify tags, pass an instance of your drivers ReadPreference constructor or roll your own. `mquery` will store whatever you provide and pass later to your collection during execution. + +## Future goals + +- mongo shell compatibility +- browser compatibility + +## Installation + +```sh +npm install mquery +``` + +## License + +[MIT](https://github.com/aheckmann/mquery/blob/master/LICENSE) diff --git a/gateway/node_modules/mquery/SECURITY.md b/gateway/node_modules/mquery/SECURITY.md new file mode 100644 index 0000000000000000000000000000000000000000..41b89d8347d578ee930b156592e3c3168bd6980c --- /dev/null +++ b/gateway/node_modules/mquery/SECURITY.md @@ -0,0 +1 @@ +Please follow the instructions on [Tidelift's security page](https://tidelift.com/docs/security) to report a security issue. diff --git a/gateway/node_modules/mquery/package.json b/gateway/node_modules/mquery/package.json new file mode 100644 index 0000000000000000000000000000000000000000..f4582175949ae368966f9289a8fd944fbde511ba --- /dev/null +++ b/gateway/node_modules/mquery/package.json @@ -0,0 +1,35 @@ +{ + "name": "mquery", + "version": "6.0.0", + "description": "Expressive query building for MongoDB", + "main": "lib/mquery.js", + "scripts": { + "test": "mocha --exit test/index.js test/*.test.js", + "fix-lint": "eslint . --fix", + "lint": "eslint ." + }, + "repository": { + "type": "git", + "url": "git://github.com/aheckmann/mquery.git" + }, + "engines": { + "node": ">=20.19.0" + }, + "devDependencies": { + "eslint": "8.x", + "eslint-plugin-mocha-no-only": "1.1.1", + "mocha": "11.x", + "mongodb": "6.x" + }, + "bugs": { + "url": "https://github.com/aheckmann/mquery/issues/new" + }, + "author": "Aaron Heckmann ", + "license": "MIT", + "keywords": [ + "mongodb", + "query", + "builder" + ], + "homepage": "https://github.com/aheckmann/mquery/" +} diff --git a/gateway/node_modules/punycode/LICENSE-MIT.txt b/gateway/node_modules/punycode/LICENSE-MIT.txt new file mode 100644 index 0000000000000000000000000000000000000000..a41e0a7ef970ecdd83d82cd99bda97b22077bc62 --- /dev/null +++ b/gateway/node_modules/punycode/LICENSE-MIT.txt @@ -0,0 +1,20 @@ +Copyright Mathias Bynens + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/gateway/node_modules/punycode/README.md b/gateway/node_modules/punycode/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f611016b01a27ad709e837880d4ddc2f19331ff2 --- /dev/null +++ b/gateway/node_modules/punycode/README.md @@ -0,0 +1,148 @@ +# Punycode.js [![punycode on npm](https://img.shields.io/npm/v/punycode)](https://www.npmjs.com/package/punycode) [![](https://data.jsdelivr.com/v1/package/npm/punycode/badge)](https://www.jsdelivr.com/package/npm/punycode) + +Punycode.js is a robust Punycode converter that fully complies to [RFC 3492](https://tools.ietf.org/html/rfc3492) and [RFC 5891](https://tools.ietf.org/html/rfc5891). + +This JavaScript library is the result of comparing, optimizing and documenting different open-source implementations of the Punycode algorithm: + +* [The C example code from RFC 3492](https://tools.ietf.org/html/rfc3492#appendix-C) +* [`punycode.c` by _Markus W. Scherer_ (IBM)](http://opensource.apple.com/source/ICU/ICU-400.42/icuSources/common/punycode.c) +* [`punycode.c` by _Ben Noordhuis_](https://github.com/bnoordhuis/punycode/blob/master/punycode.c) +* [JavaScript implementation by _some_](http://stackoverflow.com/questions/183485/can-anyone-recommend-a-good-free-javascript-for-punycode-to-unicode-conversion/301287#301287) +* [`punycode.js` by _Ben Noordhuis_](https://github.com/joyent/node/blob/426298c8c1c0d5b5224ac3658c41e7c2a3fe9377/lib/punycode.js) (note: [not fully compliant](https://github.com/joyent/node/issues/2072)) + +This project was [bundled](https://github.com/joyent/node/blob/master/lib/punycode.js) with Node.js from [v0.6.2+](https://github.com/joyent/node/compare/975f1930b1...61e796decc) until [v7](https://github.com/nodejs/node/pull/7941) (soft-deprecated). + +This project provides a CommonJS module that uses ES2015+ features and JavaScript module, which work in modern Node.js versions and browsers. For the old Punycode.js version that offers the same functionality in a UMD build with support for older pre-ES2015 runtimes, including Rhino, Ringo, and Narwhal, see [v1.4.1](https://github.com/mathiasbynens/punycode.js/releases/tag/v1.4.1). + +## Installation + +Via [npm](https://www.npmjs.com/): + +```bash +npm install punycode --save +``` + +In [Node.js](https://nodejs.org/): + +> ⚠️ Note that userland modules don't hide core modules. +> For example, `require('punycode')` still imports the deprecated core module even if you executed `npm install punycode`. +> Use `require('punycode/')` to import userland modules rather than core modules. + +```js +const punycode = require('punycode/'); +``` + +## API + +### `punycode.decode(string)` + +Converts a Punycode string of ASCII symbols to a string of Unicode symbols. + +```js +// decode domain name parts +punycode.decode('maana-pta'); // 'mañana' +punycode.decode('--dqo34k'); // '☃-⌘' +``` + +### `punycode.encode(string)` + +Converts a string of Unicode symbols to a Punycode string of ASCII symbols. + +```js +// encode domain name parts +punycode.encode('mañana'); // 'maana-pta' +punycode.encode('☃-⌘'); // '--dqo34k' +``` + +### `punycode.toUnicode(input)` + +Converts a Punycode string representing a domain name or an email address to Unicode. Only the Punycoded parts of the input will be converted, i.e. it doesn’t matter if you call it on a string that has already been converted to Unicode. + +```js +// decode domain names +punycode.toUnicode('xn--maana-pta.com'); +// → 'mañana.com' +punycode.toUnicode('xn----dqo34k.com'); +// → '☃-⌘.com' + +// decode email addresses +punycode.toUnicode('джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'); +// → 'джумла@джpумлатест.bрфa' +``` + +### `punycode.toASCII(input)` + +Converts a lowercased Unicode string representing a domain name or an email address to Punycode. Only the non-ASCII parts of the input will be converted, i.e. it doesn’t matter if you call it with a domain that’s already in ASCII. + +```js +// encode domain names +punycode.toASCII('mañana.com'); +// → 'xn--maana-pta.com' +punycode.toASCII('☃-⌘.com'); +// → 'xn----dqo34k.com' + +// encode email addresses +punycode.toASCII('джумла@джpумлатест.bрфa'); +// → 'джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq' +``` + +### `punycode.ucs2` + +#### `punycode.ucs2.decode(string)` + +Creates an array containing the numeric code point values of each Unicode symbol in the string. While [JavaScript uses UCS-2 internally](https://mathiasbynens.be/notes/javascript-encoding), this function will convert a pair of surrogate halves (each of which UCS-2 exposes as separate characters) into a single code point, matching UTF-16. + +```js +punycode.ucs2.decode('abc'); +// → [0x61, 0x62, 0x63] +// surrogate pair for U+1D306 TETRAGRAM FOR CENTRE: +punycode.ucs2.decode('\uD834\uDF06'); +// → [0x1D306] +``` + +#### `punycode.ucs2.encode(codePoints)` + +Creates a string based on an array of numeric code point values. + +```js +punycode.ucs2.encode([0x61, 0x62, 0x63]); +// → 'abc' +punycode.ucs2.encode([0x1D306]); +// → '\uD834\uDF06' +``` + +### `punycode.version` + +A string representing the current Punycode.js version number. + +## For maintainers + +### How to publish a new release + +1. On the `main` branch, bump the version number in `package.json`: + + ```sh + npm version patch -m 'Release v%s' + ``` + + Instead of `patch`, use `minor` or `major` [as needed](https://semver.org/). + + Note that this produces a Git commit + tag. + +1. Push the release commit and tag: + + ```sh + git push && git push --tags + ``` + + Our CI then automatically publishes the new release to npm, under both the [`punycode`](https://www.npmjs.com/package/punycode) and [`punycode.js`](https://www.npmjs.com/package/punycode.js) names. + +## Author + +| [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") | +|---| +| [Mathias Bynens](https://mathiasbynens.be/) | + +## License + +Punycode.js is available under the [MIT](https://mths.be/mit) license. diff --git a/gateway/node_modules/punycode/package.json b/gateway/node_modules/punycode/package.json new file mode 100644 index 0000000000000000000000000000000000000000..b8b76fc76c2162104f066c6f2330a7cacdee4a72 --- /dev/null +++ b/gateway/node_modules/punycode/package.json @@ -0,0 +1,58 @@ +{ + "name": "punycode", + "version": "2.3.1", + "description": "A robust Punycode converter that fully complies to RFC 3492 and RFC 5891, and works on nearly all JavaScript platforms.", + "homepage": "https://mths.be/punycode", + "main": "punycode.js", + "jsnext:main": "punycode.es6.js", + "module": "punycode.es6.js", + "engines": { + "node": ">=6" + }, + "keywords": [ + "punycode", + "unicode", + "idn", + "idna", + "dns", + "url", + "domain" + ], + "license": "MIT", + "author": { + "name": "Mathias Bynens", + "url": "https://mathiasbynens.be/" + }, + "contributors": [ + { + "name": "Mathias Bynens", + "url": "https://mathiasbynens.be/" + } + ], + "repository": { + "type": "git", + "url": "https://github.com/mathiasbynens/punycode.js.git" + }, + "bugs": "https://github.com/mathiasbynens/punycode.js/issues", + "files": [ + "LICENSE-MIT.txt", + "punycode.js", + "punycode.es6.js" + ], + "scripts": { + "test": "mocha tests", + "build": "node scripts/prepublish.js" + }, + "devDependencies": { + "codecov": "^3.8.3", + "nyc": "^15.1.0", + "mocha": "^10.2.0" + }, + "jspm": { + "map": { + "./punycode.js": { + "node": "@node/punycode" + } + } + } +} diff --git a/gateway/node_modules/punycode/punycode.es6.js b/gateway/node_modules/punycode/punycode.es6.js new file mode 100644 index 0000000000000000000000000000000000000000..dadece25b35f595d717ec52adfc94d23d3b1edda --- /dev/null +++ b/gateway/node_modules/punycode/punycode.es6.js @@ -0,0 +1,444 @@ +'use strict'; + +/** Highest positive signed 32-bit float value */ +const maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1 + +/** Bootstring parameters */ +const base = 36; +const tMin = 1; +const tMax = 26; +const skew = 38; +const damp = 700; +const initialBias = 72; +const initialN = 128; // 0x80 +const delimiter = '-'; // '\x2D' + +/** Regular expressions */ +const regexPunycode = /^xn--/; +const regexNonASCII = /[^\0-\x7F]/; // Note: U+007F DEL is excluded too. +const regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g; // RFC 3490 separators + +/** Error messages */ +const errors = { + 'overflow': 'Overflow: input needs wider integers to process', + 'not-basic': 'Illegal input >= 0x80 (not a basic code point)', + 'invalid-input': 'Invalid input' +}; + +/** Convenience shortcuts */ +const baseMinusTMin = base - tMin; +const floor = Math.floor; +const stringFromCharCode = String.fromCharCode; + +/*--------------------------------------------------------------------------*/ + +/** + * A generic error utility function. + * @private + * @param {String} type The error type. + * @returns {Error} Throws a `RangeError` with the applicable error message. + */ +function error(type) { + throw new RangeError(errors[type]); +} + +/** + * A generic `Array#map` utility function. + * @private + * @param {Array} array The array to iterate over. + * @param {Function} callback The function that gets called for every array + * item. + * @returns {Array} A new array of values returned by the callback function. + */ +function map(array, callback) { + const result = []; + let length = array.length; + while (length--) { + result[length] = callback(array[length]); + } + return result; +} + +/** + * A simple `Array#map`-like wrapper to work with domain name strings or email + * addresses. + * @private + * @param {String} domain The domain name or email address. + * @param {Function} callback The function that gets called for every + * character. + * @returns {String} A new string of characters returned by the callback + * function. + */ +function mapDomain(domain, callback) { + const parts = domain.split('@'); + let result = ''; + if (parts.length > 1) { + // In email addresses, only the domain name should be punycoded. Leave + // the local part (i.e. everything up to `@`) intact. + result = parts[0] + '@'; + domain = parts[1]; + } + // Avoid `split(regex)` for IE8 compatibility. See #17. + domain = domain.replace(regexSeparators, '\x2E'); + const labels = domain.split('.'); + const encoded = map(labels, callback).join('.'); + return result + encoded; +} + +/** + * Creates an array containing the numeric code points of each Unicode + * character in the string. While JavaScript uses UCS-2 internally, + * this function will convert a pair of surrogate halves (each of which + * UCS-2 exposes as separate characters) into a single code point, + * matching UTF-16. + * @see `punycode.ucs2.encode` + * @see + * @memberOf punycode.ucs2 + * @name decode + * @param {String} string The Unicode input string (UCS-2). + * @returns {Array} The new array of code points. + */ +function ucs2decode(string) { + const output = []; + let counter = 0; + const length = string.length; + while (counter < length) { + const value = string.charCodeAt(counter++); + if (value >= 0xD800 && value <= 0xDBFF && counter < length) { + // It's a high surrogate, and there is a next character. + const extra = string.charCodeAt(counter++); + if ((extra & 0xFC00) == 0xDC00) { // Low surrogate. + output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); + } else { + // It's an unmatched surrogate; only append this code unit, in case the + // next code unit is the high surrogate of a surrogate pair. + output.push(value); + counter--; + } + } else { + output.push(value); + } + } + return output; +} + +/** + * Creates a string based on an array of numeric code points. + * @see `punycode.ucs2.decode` + * @memberOf punycode.ucs2 + * @name encode + * @param {Array} codePoints The array of numeric code points. + * @returns {String} The new Unicode string (UCS-2). + */ +const ucs2encode = codePoints => String.fromCodePoint(...codePoints); + +/** + * Converts a basic code point into a digit/integer. + * @see `digitToBasic()` + * @private + * @param {Number} codePoint The basic numeric code point value. + * @returns {Number} The numeric value of a basic code point (for use in + * representing integers) in the range `0` to `base - 1`, or `base` if + * the code point does not represent a value. + */ +const basicToDigit = function(codePoint) { + if (codePoint >= 0x30 && codePoint < 0x3A) { + return 26 + (codePoint - 0x30); + } + if (codePoint >= 0x41 && codePoint < 0x5B) { + return codePoint - 0x41; + } + if (codePoint >= 0x61 && codePoint < 0x7B) { + return codePoint - 0x61; + } + return base; +}; + +/** + * Converts a digit/integer into a basic code point. + * @see `basicToDigit()` + * @private + * @param {Number} digit The numeric value of a basic code point. + * @returns {Number} The basic code point whose value (when used for + * representing integers) is `digit`, which needs to be in the range + * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is + * used; else, the lowercase form is used. The behavior is undefined + * if `flag` is non-zero and `digit` has no uppercase form. + */ +const digitToBasic = function(digit, flag) { + // 0..25 map to ASCII a..z or A..Z + // 26..35 map to ASCII 0..9 + return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5); +}; + +/** + * Bias adaptation function as per section 3.4 of RFC 3492. + * https://tools.ietf.org/html/rfc3492#section-3.4 + * @private + */ +const adapt = function(delta, numPoints, firstTime) { + let k = 0; + delta = firstTime ? floor(delta / damp) : delta >> 1; + delta += floor(delta / numPoints); + for (/* no initialization */; delta > baseMinusTMin * tMax >> 1; k += base) { + delta = floor(delta / baseMinusTMin); + } + return floor(k + (baseMinusTMin + 1) * delta / (delta + skew)); +}; + +/** + * Converts a Punycode string of ASCII-only symbols to a string of Unicode + * symbols. + * @memberOf punycode + * @param {String} input The Punycode string of ASCII-only symbols. + * @returns {String} The resulting string of Unicode symbols. + */ +const decode = function(input) { + // Don't use UCS-2. + const output = []; + const inputLength = input.length; + let i = 0; + let n = initialN; + let bias = initialBias; + + // Handle the basic code points: let `basic` be the number of input code + // points before the last delimiter, or `0` if there is none, then copy + // the first basic code points to the output. + + let basic = input.lastIndexOf(delimiter); + if (basic < 0) { + basic = 0; + } + + for (let j = 0; j < basic; ++j) { + // if it's not a basic code point + if (input.charCodeAt(j) >= 0x80) { + error('not-basic'); + } + output.push(input.charCodeAt(j)); + } + + // Main decoding loop: start just after the last delimiter if any basic code + // points were copied; start at the beginning otherwise. + + for (let index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) { + + // `index` is the index of the next character to be consumed. + // Decode a generalized variable-length integer into `delta`, + // which gets added to `i`. The overflow checking is easier + // if we increase `i` as we go, then subtract off its starting + // value at the end to obtain `delta`. + const oldi = i; + for (let w = 1, k = base; /* no condition */; k += base) { + + if (index >= inputLength) { + error('invalid-input'); + } + + const digit = basicToDigit(input.charCodeAt(index++)); + + if (digit >= base) { + error('invalid-input'); + } + if (digit > floor((maxInt - i) / w)) { + error('overflow'); + } + + i += digit * w; + const t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); + + if (digit < t) { + break; + } + + const baseMinusT = base - t; + if (w > floor(maxInt / baseMinusT)) { + error('overflow'); + } + + w *= baseMinusT; + + } + + const out = output.length + 1; + bias = adapt(i - oldi, out, oldi == 0); + + // `i` was supposed to wrap around from `out` to `0`, + // incrementing `n` each time, so we'll fix that now: + if (floor(i / out) > maxInt - n) { + error('overflow'); + } + + n += floor(i / out); + i %= out; + + // Insert `n` at position `i` of the output. + output.splice(i++, 0, n); + + } + + return String.fromCodePoint(...output); +}; + +/** + * Converts a string of Unicode symbols (e.g. a domain name label) to a + * Punycode string of ASCII-only symbols. + * @memberOf punycode + * @param {String} input The string of Unicode symbols. + * @returns {String} The resulting Punycode string of ASCII-only symbols. + */ +const encode = function(input) { + const output = []; + + // Convert the input in UCS-2 to an array of Unicode code points. + input = ucs2decode(input); + + // Cache the length. + const inputLength = input.length; + + // Initialize the state. + let n = initialN; + let delta = 0; + let bias = initialBias; + + // Handle the basic code points. + for (const currentValue of input) { + if (currentValue < 0x80) { + output.push(stringFromCharCode(currentValue)); + } + } + + const basicLength = output.length; + let handledCPCount = basicLength; + + // `handledCPCount` is the number of code points that have been handled; + // `basicLength` is the number of basic code points. + + // Finish the basic string with a delimiter unless it's empty. + if (basicLength) { + output.push(delimiter); + } + + // Main encoding loop: + while (handledCPCount < inputLength) { + + // All non-basic code points < n have been handled already. Find the next + // larger one: + let m = maxInt; + for (const currentValue of input) { + if (currentValue >= n && currentValue < m) { + m = currentValue; + } + } + + // Increase `delta` enough to advance the decoder's state to , + // but guard against overflow. + const handledCPCountPlusOne = handledCPCount + 1; + if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) { + error('overflow'); + } + + delta += (m - n) * handledCPCountPlusOne; + n = m; + + for (const currentValue of input) { + if (currentValue < n && ++delta > maxInt) { + error('overflow'); + } + if (currentValue === n) { + // Represent delta as a generalized variable-length integer. + let q = delta; + for (let k = base; /* no condition */; k += base) { + const t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); + if (q < t) { + break; + } + const qMinusT = q - t; + const baseMinusT = base - t; + output.push( + stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0)) + ); + q = floor(qMinusT / baseMinusT); + } + + output.push(stringFromCharCode(digitToBasic(q, 0))); + bias = adapt(delta, handledCPCountPlusOne, handledCPCount === basicLength); + delta = 0; + ++handledCPCount; + } + } + + ++delta; + ++n; + + } + return output.join(''); +}; + +/** + * Converts a Punycode string representing a domain name or an email address + * to Unicode. Only the Punycoded parts of the input will be converted, i.e. + * it doesn't matter if you call it on a string that has already been + * converted to Unicode. + * @memberOf punycode + * @param {String} input The Punycoded domain name or email address to + * convert to Unicode. + * @returns {String} The Unicode representation of the given Punycode + * string. + */ +const toUnicode = function(input) { + return mapDomain(input, function(string) { + return regexPunycode.test(string) + ? decode(string.slice(4).toLowerCase()) + : string; + }); +}; + +/** + * Converts a Unicode string representing a domain name or an email address to + * Punycode. Only the non-ASCII parts of the domain name will be converted, + * i.e. it doesn't matter if you call it with a domain that's already in + * ASCII. + * @memberOf punycode + * @param {String} input The domain name or email address to convert, as a + * Unicode string. + * @returns {String} The Punycode representation of the given domain name or + * email address. + */ +const toASCII = function(input) { + return mapDomain(input, function(string) { + return regexNonASCII.test(string) + ? 'xn--' + encode(string) + : string; + }); +}; + +/*--------------------------------------------------------------------------*/ + +/** Define the public API */ +const punycode = { + /** + * A string representing the current Punycode.js version number. + * @memberOf punycode + * @type String + */ + 'version': '2.3.1', + /** + * An object of methods to convert from JavaScript's internal character + * representation (UCS-2) to Unicode code points, and back. + * @see + * @memberOf punycode + * @type Object + */ + 'ucs2': { + 'decode': ucs2decode, + 'encode': ucs2encode + }, + 'decode': decode, + 'encode': encode, + 'toASCII': toASCII, + 'toUnicode': toUnicode +}; + +export { ucs2decode, ucs2encode, decode, encode, toASCII, toUnicode }; +export default punycode; diff --git a/gateway/node_modules/punycode/punycode.js b/gateway/node_modules/punycode/punycode.js new file mode 100644 index 0000000000000000000000000000000000000000..a1ef251924c81e2cf620132f7765058c690eeb6e --- /dev/null +++ b/gateway/node_modules/punycode/punycode.js @@ -0,0 +1,443 @@ +'use strict'; + +/** Highest positive signed 32-bit float value */ +const maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1 + +/** Bootstring parameters */ +const base = 36; +const tMin = 1; +const tMax = 26; +const skew = 38; +const damp = 700; +const initialBias = 72; +const initialN = 128; // 0x80 +const delimiter = '-'; // '\x2D' + +/** Regular expressions */ +const regexPunycode = /^xn--/; +const regexNonASCII = /[^\0-\x7F]/; // Note: U+007F DEL is excluded too. +const regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g; // RFC 3490 separators + +/** Error messages */ +const errors = { + 'overflow': 'Overflow: input needs wider integers to process', + 'not-basic': 'Illegal input >= 0x80 (not a basic code point)', + 'invalid-input': 'Invalid input' +}; + +/** Convenience shortcuts */ +const baseMinusTMin = base - tMin; +const floor = Math.floor; +const stringFromCharCode = String.fromCharCode; + +/*--------------------------------------------------------------------------*/ + +/** + * A generic error utility function. + * @private + * @param {String} type The error type. + * @returns {Error} Throws a `RangeError` with the applicable error message. + */ +function error(type) { + throw new RangeError(errors[type]); +} + +/** + * A generic `Array#map` utility function. + * @private + * @param {Array} array The array to iterate over. + * @param {Function} callback The function that gets called for every array + * item. + * @returns {Array} A new array of values returned by the callback function. + */ +function map(array, callback) { + const result = []; + let length = array.length; + while (length--) { + result[length] = callback(array[length]); + } + return result; +} + +/** + * A simple `Array#map`-like wrapper to work with domain name strings or email + * addresses. + * @private + * @param {String} domain The domain name or email address. + * @param {Function} callback The function that gets called for every + * character. + * @returns {String} A new string of characters returned by the callback + * function. + */ +function mapDomain(domain, callback) { + const parts = domain.split('@'); + let result = ''; + if (parts.length > 1) { + // In email addresses, only the domain name should be punycoded. Leave + // the local part (i.e. everything up to `@`) intact. + result = parts[0] + '@'; + domain = parts[1]; + } + // Avoid `split(regex)` for IE8 compatibility. See #17. + domain = domain.replace(regexSeparators, '\x2E'); + const labels = domain.split('.'); + const encoded = map(labels, callback).join('.'); + return result + encoded; +} + +/** + * Creates an array containing the numeric code points of each Unicode + * character in the string. While JavaScript uses UCS-2 internally, + * this function will convert a pair of surrogate halves (each of which + * UCS-2 exposes as separate characters) into a single code point, + * matching UTF-16. + * @see `punycode.ucs2.encode` + * @see + * @memberOf punycode.ucs2 + * @name decode + * @param {String} string The Unicode input string (UCS-2). + * @returns {Array} The new array of code points. + */ +function ucs2decode(string) { + const output = []; + let counter = 0; + const length = string.length; + while (counter < length) { + const value = string.charCodeAt(counter++); + if (value >= 0xD800 && value <= 0xDBFF && counter < length) { + // It's a high surrogate, and there is a next character. + const extra = string.charCodeAt(counter++); + if ((extra & 0xFC00) == 0xDC00) { // Low surrogate. + output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); + } else { + // It's an unmatched surrogate; only append this code unit, in case the + // next code unit is the high surrogate of a surrogate pair. + output.push(value); + counter--; + } + } else { + output.push(value); + } + } + return output; +} + +/** + * Creates a string based on an array of numeric code points. + * @see `punycode.ucs2.decode` + * @memberOf punycode.ucs2 + * @name encode + * @param {Array} codePoints The array of numeric code points. + * @returns {String} The new Unicode string (UCS-2). + */ +const ucs2encode = codePoints => String.fromCodePoint(...codePoints); + +/** + * Converts a basic code point into a digit/integer. + * @see `digitToBasic()` + * @private + * @param {Number} codePoint The basic numeric code point value. + * @returns {Number} The numeric value of a basic code point (for use in + * representing integers) in the range `0` to `base - 1`, or `base` if + * the code point does not represent a value. + */ +const basicToDigit = function(codePoint) { + if (codePoint >= 0x30 && codePoint < 0x3A) { + return 26 + (codePoint - 0x30); + } + if (codePoint >= 0x41 && codePoint < 0x5B) { + return codePoint - 0x41; + } + if (codePoint >= 0x61 && codePoint < 0x7B) { + return codePoint - 0x61; + } + return base; +}; + +/** + * Converts a digit/integer into a basic code point. + * @see `basicToDigit()` + * @private + * @param {Number} digit The numeric value of a basic code point. + * @returns {Number} The basic code point whose value (when used for + * representing integers) is `digit`, which needs to be in the range + * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is + * used; else, the lowercase form is used. The behavior is undefined + * if `flag` is non-zero and `digit` has no uppercase form. + */ +const digitToBasic = function(digit, flag) { + // 0..25 map to ASCII a..z or A..Z + // 26..35 map to ASCII 0..9 + return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5); +}; + +/** + * Bias adaptation function as per section 3.4 of RFC 3492. + * https://tools.ietf.org/html/rfc3492#section-3.4 + * @private + */ +const adapt = function(delta, numPoints, firstTime) { + let k = 0; + delta = firstTime ? floor(delta / damp) : delta >> 1; + delta += floor(delta / numPoints); + for (/* no initialization */; delta > baseMinusTMin * tMax >> 1; k += base) { + delta = floor(delta / baseMinusTMin); + } + return floor(k + (baseMinusTMin + 1) * delta / (delta + skew)); +}; + +/** + * Converts a Punycode string of ASCII-only symbols to a string of Unicode + * symbols. + * @memberOf punycode + * @param {String} input The Punycode string of ASCII-only symbols. + * @returns {String} The resulting string of Unicode symbols. + */ +const decode = function(input) { + // Don't use UCS-2. + const output = []; + const inputLength = input.length; + let i = 0; + let n = initialN; + let bias = initialBias; + + // Handle the basic code points: let `basic` be the number of input code + // points before the last delimiter, or `0` if there is none, then copy + // the first basic code points to the output. + + let basic = input.lastIndexOf(delimiter); + if (basic < 0) { + basic = 0; + } + + for (let j = 0; j < basic; ++j) { + // if it's not a basic code point + if (input.charCodeAt(j) >= 0x80) { + error('not-basic'); + } + output.push(input.charCodeAt(j)); + } + + // Main decoding loop: start just after the last delimiter if any basic code + // points were copied; start at the beginning otherwise. + + for (let index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) { + + // `index` is the index of the next character to be consumed. + // Decode a generalized variable-length integer into `delta`, + // which gets added to `i`. The overflow checking is easier + // if we increase `i` as we go, then subtract off its starting + // value at the end to obtain `delta`. + const oldi = i; + for (let w = 1, k = base; /* no condition */; k += base) { + + if (index >= inputLength) { + error('invalid-input'); + } + + const digit = basicToDigit(input.charCodeAt(index++)); + + if (digit >= base) { + error('invalid-input'); + } + if (digit > floor((maxInt - i) / w)) { + error('overflow'); + } + + i += digit * w; + const t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); + + if (digit < t) { + break; + } + + const baseMinusT = base - t; + if (w > floor(maxInt / baseMinusT)) { + error('overflow'); + } + + w *= baseMinusT; + + } + + const out = output.length + 1; + bias = adapt(i - oldi, out, oldi == 0); + + // `i` was supposed to wrap around from `out` to `0`, + // incrementing `n` each time, so we'll fix that now: + if (floor(i / out) > maxInt - n) { + error('overflow'); + } + + n += floor(i / out); + i %= out; + + // Insert `n` at position `i` of the output. + output.splice(i++, 0, n); + + } + + return String.fromCodePoint(...output); +}; + +/** + * Converts a string of Unicode symbols (e.g. a domain name label) to a + * Punycode string of ASCII-only symbols. + * @memberOf punycode + * @param {String} input The string of Unicode symbols. + * @returns {String} The resulting Punycode string of ASCII-only symbols. + */ +const encode = function(input) { + const output = []; + + // Convert the input in UCS-2 to an array of Unicode code points. + input = ucs2decode(input); + + // Cache the length. + const inputLength = input.length; + + // Initialize the state. + let n = initialN; + let delta = 0; + let bias = initialBias; + + // Handle the basic code points. + for (const currentValue of input) { + if (currentValue < 0x80) { + output.push(stringFromCharCode(currentValue)); + } + } + + const basicLength = output.length; + let handledCPCount = basicLength; + + // `handledCPCount` is the number of code points that have been handled; + // `basicLength` is the number of basic code points. + + // Finish the basic string with a delimiter unless it's empty. + if (basicLength) { + output.push(delimiter); + } + + // Main encoding loop: + while (handledCPCount < inputLength) { + + // All non-basic code points < n have been handled already. Find the next + // larger one: + let m = maxInt; + for (const currentValue of input) { + if (currentValue >= n && currentValue < m) { + m = currentValue; + } + } + + // Increase `delta` enough to advance the decoder's state to , + // but guard against overflow. + const handledCPCountPlusOne = handledCPCount + 1; + if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) { + error('overflow'); + } + + delta += (m - n) * handledCPCountPlusOne; + n = m; + + for (const currentValue of input) { + if (currentValue < n && ++delta > maxInt) { + error('overflow'); + } + if (currentValue === n) { + // Represent delta as a generalized variable-length integer. + let q = delta; + for (let k = base; /* no condition */; k += base) { + const t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); + if (q < t) { + break; + } + const qMinusT = q - t; + const baseMinusT = base - t; + output.push( + stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0)) + ); + q = floor(qMinusT / baseMinusT); + } + + output.push(stringFromCharCode(digitToBasic(q, 0))); + bias = adapt(delta, handledCPCountPlusOne, handledCPCount === basicLength); + delta = 0; + ++handledCPCount; + } + } + + ++delta; + ++n; + + } + return output.join(''); +}; + +/** + * Converts a Punycode string representing a domain name or an email address + * to Unicode. Only the Punycoded parts of the input will be converted, i.e. + * it doesn't matter if you call it on a string that has already been + * converted to Unicode. + * @memberOf punycode + * @param {String} input The Punycoded domain name or email address to + * convert to Unicode. + * @returns {String} The Unicode representation of the given Punycode + * string. + */ +const toUnicode = function(input) { + return mapDomain(input, function(string) { + return regexPunycode.test(string) + ? decode(string.slice(4).toLowerCase()) + : string; + }); +}; + +/** + * Converts a Unicode string representing a domain name or an email address to + * Punycode. Only the non-ASCII parts of the domain name will be converted, + * i.e. it doesn't matter if you call it with a domain that's already in + * ASCII. + * @memberOf punycode + * @param {String} input The domain name or email address to convert, as a + * Unicode string. + * @returns {String} The Punycode representation of the given domain name or + * email address. + */ +const toASCII = function(input) { + return mapDomain(input, function(string) { + return regexNonASCII.test(string) + ? 'xn--' + encode(string) + : string; + }); +}; + +/*--------------------------------------------------------------------------*/ + +/** Define the public API */ +const punycode = { + /** + * A string representing the current Punycode.js version number. + * @memberOf punycode + * @type String + */ + 'version': '2.3.1', + /** + * An object of methods to convert from JavaScript's internal character + * representation (UCS-2) to Unicode code points, and back. + * @see + * @memberOf punycode + * @type Object + */ + 'ucs2': { + 'decode': ucs2decode, + 'encode': ucs2encode + }, + 'decode': decode, + 'encode': encode, + 'toASCII': toASCII, + 'toUnicode': toUnicode +}; + +module.exports = punycode; diff --git a/gateway/node_modules/sift/MIT-LICENSE.txt b/gateway/node_modules/sift/MIT-LICENSE.txt new file mode 100644 index 0000000000000000000000000000000000000000..c080d2eb03165de6d8d1e630a5ed3e221be3324d --- /dev/null +++ b/gateway/node_modules/sift/MIT-LICENSE.txt @@ -0,0 +1,20 @@ +Copyright (c) 2015 Craig Condon + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/gateway/node_modules/sift/README.md b/gateway/node_modules/sift/README.md new file mode 100644 index 0000000000000000000000000000000000000000..0501d1a198a864d11e919f390010bf068a39c352 --- /dev/null +++ b/gateway/node_modules/sift/README.md @@ -0,0 +1,465 @@ +**Installation**: `npm install sift`, or `yarn add sift` + +## Sift is a tiny library for using MongoDB queries in Javascript + +[![Build Status](https://secure.travis-ci.org/crcn/sift.js.png)](https://secure.travis-ci.org/crcn/sift.js) + + + + +**For extended documentation, checkout http://docs.mongodb.org/manual/reference/operator/query/** + +## Features: + +- Supported operators: [\$in](#in), [\$nin](#nin), [\$exists](#exists), [\$gte](#gte), [\$gt](#gt), [\$lte](#lte), [\$lt](#lt), [\$eq](#eq), [\$ne](#ne), [\$mod](#mod), [\$all](#all), [\$and](#and), [\$or](#or), [\$nor](#nor), [\$not](#not), [\$size](#size), [\$type](#type), [\$regex](#regex), [\$where](#where), [\$elemMatch](#elemmatch) +- Regexp searches +- Supports node.js, and web +- Custom Operations +- Tree-shaking (omitting functionality from web app bundles) + +## Examples + +```javascript +import sift from "sift"; + +// intersecting arrays +const result1 = ["hello", "sifted", "array!"].filter( + sift({ $in: ["hello", "world"] }), +); // ['hello'] + +// regexp filter +const result2 = ["craig", "john", "jake"].filter(sift(/^j/)); //['john','jake'] + +// function filter +const testFilter = sift({ + //you can also filter against functions + name: function (value) { + return value.length == 5; + }, +}); + +const result3 = [ + { + name: "craig", + }, + { + name: "john", + }, + { + name: "jake", + }, +].filter(testFilter); // filtered: [{ name: 'craig' }] + +// you can test *single values* against your custom sifter +testFilter({ name: "sarah" }); //true +testFilter({ name: "tim" }); //false +``` + +## API + +### sift(query: MongoQuery, options?: Options): Function + +Creates a filter with all the built-in MongoDB query operations. + +- `query` - the filter to use against the target array +- `options` + - `operations` - [custom operations](#custom-operations) + - `compare` - compares difference between two values + +Example: + +```javascript +import sift from "sift"; + +const test = sift({ $gt: 5 }); + +console.log(test(6)); // true +console.log(test(4)); // false + +[3, 4, 5, 6, 7].filter(test); // [6, 7] +``` + +### createQueryTester(query: Query, options?: Options): Function + +Creates a filter function **without** built-in MongoDB query operations. This is useful +if you're looking to omit certain operations from application bundles. See [Omitting built-in operations](#omitting-built-in-operations) for more info. + +```javascript +import { createQueryTester, $eq, $in } from "sift"; +const filter = createQueryTester({ $eq: 5 }, { operations: { $eq, $in } }); +``` + +### createEqualsOperation(params: any, ownerQuery: Query, options: Options): Operation + +Used for [custom operations](#custom-operations). + +```javascript +import { createQueryTester, createEqualsOperation, $eq, $in } from "sift"; +const filter = createQueryTester( + { $mod: 5 }, + { + operations: { + $something(mod, ownerQuery, options) { + return createEqualsOperation( + (value) => value % mod === 0, + ownerQuery, + options, + ); + }, + }, + }, +); +filter(10); // true +filter(11); // false +``` + +## Supported Operators + +See MongoDB's [advanced queries](http://www.mongodb.org/display/DOCS/Advanced+Queries) for more info. + +### \$in + +array value must be _\$in_ the given query: + +Intersecting two arrays: + +```javascript +// filtered: ['Brazil'] +["Brazil", "Haiti", "Peru", "Chile"].filter( + sift({ $in: ["Costa Rica", "Brazil"] }), +); +``` + +Here's another example. This acts more like the \$or operator: + +```javascript +[{ name: "Craig", location: "Brazil" }].filter( + sift({ location: { $in: ["Costa Rica", "Brazil"] } }), +); +``` + +### \$nin + +Opposite of \$in: + +```javascript +// filtered: ['Haiti','Peru','Chile'] +["Brazil", "Haiti", "Peru", "Chile"].filter( + sift({ $nin: ["Costa Rica", "Brazil"] }), +); +``` + +### \$exists + +Checks if whether a value exists: + +```javascript +// filtered: ['Craig','Tim'] +sift({ $exists: true })(["Craig", null, "Tim"]); +``` + +You can also filter out values that don't exist + +```javascript +// filtered: [{ name: "Tim" }] +[{ name: "Craig", city: "Minneapolis" }, { name: "Tim" }].filter( + sift({ city: { $exists: false } }), +); +``` + +### \$gte + +Checks if a number is >= value: + +```javascript +// filtered: [2, 3] +[0, 1, 2, 3].filter(sift({ $gte: 2 })); +``` + +### \$gt + +Checks if a number is > value: + +```javascript +// filtered: [3] +[0, 1, 2, 3].filter(sift({ $gt: 2 })); +``` + +### \$lte + +Checks if a number is <= value. + +```javascript +// filtered: [0, 1, 2] +[0, 1, 2, 3].filter(sift({ $lte: 2 })); +``` + +### \$lt + +Checks if number is < value. + +```javascript +// filtered: [0, 1] +[0, 1, 2, 3].filter(sift({ $lt: 2 })); +``` + +### \$eq + +Checks if `query === value`. Note that **\$eq can be omitted**. For **\$eq**, and **\$ne** + +```javascript +// filtered: [{ state: 'MN' }] +[{ state: "MN" }, { state: "CA" }, { state: "WI" }].filter( + sift({ state: { $eq: "MN" } }), +); +``` + +Or: + +```javascript +// filtered: [{ state: 'MN' }] +[{ state: "MN" }, { state: "CA" }, { state: "WI" }].filter( + sift({ state: "MN" }), +); +``` + +### \$ne + +Checks if `query !== value`. + +```javascript +// filtered: [{ state: 'CA' }, { state: 'WI'}] +[{ state: "MN" }, { state: "CA" }, { state: "WI" }].filter( + sift({ state: { $ne: "MN" } }), +); +``` + +### \$mod + +Modulus: + +```javascript +// filtered: [300, 600] +[100, 200, 300, 400, 500, 600].filter(sift({ $mod: [3, 0] })); +``` + +### \$all + +values must match **everything** in array: + +```javascript +// filtered: [ { tags: ['books','programming','travel' ]} ] +[ + { tags: ["books", "programming", "travel"] }, + { tags: ["travel", "cooking"] }, +].filter(sift({ tags: { $all: ["books", "programming"] } })); +``` + +### \$and + +ability to use an array of expressions. All expressions must test true. + +```javascript +// filtered: [ { name: 'Craig', state: 'MN' }] + +[ + { name: "Craig", state: "MN" }, + { name: "Tim", state: "MN" }, + { name: "Joe", state: "CA" }, +].filter(sift({ $and: [{ name: "Craig" }, { state: "MN" }] })); +``` + +### \$or + +OR array of expressions. + +```javascript +// filtered: [ { name: 'Craig', state: 'MN' }, { name: 'Tim', state: 'MN' }] +[ + { name: "Craig", state: "MN" }, + { name: "Tim", state: "MN" }, + { name: "Joe", state: "CA" }, +].filter(sift({ $or: [{ name: "Craig" }, { state: "MN" }] })); +``` + +### \$nor + +opposite of or: + +```javascript +// filtered: [{ name: 'Joe', state: 'CA' }] +[ + { name: "Craig", state: "MN" }, + { name: "Tim", state: "MN" }, + { name: "Joe", state: "CA" }, +].filter(sift({ $nor: [{ name: "Craig" }, { state: "MN" }] })); +``` + +### \$size + +Matches an array - must match given size: + +```javascript +// filtered: ['food','cooking'] +[{ tags: ["food", "cooking"] }, { tags: ["traveling"] }].filter( + sift({ tags: { $size: 2 } }), +); +``` + +### \$type + +Matches a values based on the type + +```javascript +[new Date(), 4342, "hello world"].filter(sift({ $type: Date })); // returns single date +[new Date(), 4342, "hello world"].filter(sift({ $type: String })); // returns ['hello world'] +``` + +### \$regex + +Matches values based on the given regular expression + +```javascript +["frank", "fred", "sam", "frost"].filter( + sift({ $regex: /^f/i, $nin: ["frank"] }), +); // ["fred", "frost"] +["frank", "fred", "sam", "frost"].filter( + sift({ $regex: "^f", $options: "i", $nin: ["frank"] }), +); // ["fred", "frost"] +``` + +### \$where + +Matches based on some javascript comparison + +```javascript +[{ name: "frank" }, { name: "joe" }].filter( + sift({ $where: "this.name === 'frank'" }), +); // ["frank"] +[{ name: "frank" }, { name: "joe" }].filter( + sift({ + $where: function () { + return this.name === "frank"; + }, + }), +); // ["frank"] +``` + +### \$elemMatch + +Matches elements of array + +```javascript +var bills = [ + { + month: "july", + casts: [ + { + id: 1, + value: 200, + }, + { + id: 2, + value: 1000, + }, + ], + }, + { + month: "august", + casts: [ + { + id: 3, + value: 1000, + }, + { + id: 4, + value: 4000, + }, + ], + }, +]; + +var result = bills.filter( + sift({ + casts: { + $elemMatch: { + value: { $gt: 1000 }, + }, + }, + }), +); // {month:'august', casts:[{id:3, value: 1000},{id: 4, value: 4000}]} +``` + +### \$not + +Not expression: + +```javascript +["craig", "tim", "jake"].filter(sift({ $not: { $in: ["craig", "tim"] } })); // ['jake'] +["craig", "tim", "jake"].filter(sift({ $not: { $size: 5 } })); // ['tim','jake'] +``` + +### Date comparison + +Mongodb allows you to do date comparisons like so: + +```javascript +db.collection.find({ createdAt: { $gte: "2018-03-22T06:00:00Z" } }); +``` + +In Sift, you'll need to specify a Date object: + +```javascript +collection.find( + sift({ createdAt: { $gte: new Date("2018-03-22T06:00:00Z") } }), +); +``` + +## Custom behavior + +Sift works like MongoDB out of the box, but you're also able to modify the behavior to suite your needs. + +#### Custom operations + +You can register your own custom operations. Here's an example: + +```javascript +import sift, { createEqualsOperation } from "sift"; + +var filter = sift( + { + $customMod: 2, + }, + { + operations: { + $customMod(params, ownerQuery, options) { + return createEqualsOperation( + (value) => value % params !== 0, + ownerQuery, + options, + ); + }, + }, + }, +); + +[1, 2, 3, 4, 5].filter(filter); // [1, 3, 5] +``` + +#### Omitting built-in operations + +You can create a filter function that omits the built-in operations like so: + +```javascript +import { createQueryTester, $in, $all, $nin, $lt } from "sift"; +const test = createQueryTester( + { + $eq: 10, + }, + { operations: { $in, $all, $nin, $lt } }, +); + +[1, 2, 3, 4, 10].filter(test); +``` + +For bundlers like `Webpack` and `Rollup`, operations that aren't used are omitted from application bundles via tree-shaking. diff --git a/gateway/node_modules/sift/es/index.js b/gateway/node_modules/sift/es/index.js new file mode 100644 index 0000000000000000000000000000000000000000..8e0e9f067670a2131827c80f21e3a6ffe825c353 --- /dev/null +++ b/gateway/node_modules/sift/es/index.js @@ -0,0 +1,632 @@ +const typeChecker = (type) => { + const typeString = "[object " + type + "]"; + return function (value) { + return getClassName(value) === typeString; + }; +}; +const getClassName = (value) => Object.prototype.toString.call(value); +const comparable = (value) => { + if (value instanceof Date) { + return value.getTime(); + } + else if (isArray(value)) { + return value.map(comparable); + } + else if (value && typeof value.toJSON === "function") { + return value.toJSON(); + } + return value; +}; +const coercePotentiallyNull = (value) => value == null ? null : value; +const isArray = typeChecker("Array"); +const isObject = typeChecker("Object"); +const isFunction = typeChecker("Function"); +const isProperty = (item, key) => { + return item.hasOwnProperty(key) && !isFunction(item[key]); +}; +const isVanillaObject = (value) => { + return (value && + (value.constructor === Object || + value.constructor === Array || + value.constructor.toString() === "function Object() { [native code] }" || + value.constructor.toString() === "function Array() { [native code] }") && + !value.toJSON); +}; +const equals = (a, b) => { + if (a == null && a == b) { + return true; + } + if (a === b) { + return true; + } + if (Object.prototype.toString.call(a) !== Object.prototype.toString.call(b)) { + return false; + } + if (isArray(a)) { + if (a.length !== b.length) { + return false; + } + for (let i = 0, { length } = a; i < length; i++) { + if (!equals(a[i], b[i])) + return false; + } + return true; + } + else if (isObject(a)) { + if (Object.keys(a).length !== Object.keys(b).length) { + return false; + } + for (const key in a) { + if (!equals(a[key], b[key])) + return false; + } + return true; + } + return false; +}; + +/** + * Walks through each value given the context - used for nested operations. E.g: + * { "person.address": { $eq: "blarg" }} + */ +const walkKeyPathValues = (item, keyPath, next, depth, key, owner) => { + const currentKey = keyPath[depth]; + // if array, then try matching. Might fall through for cases like: + // { $eq: [1, 2, 3] }, [ 1, 2, 3 ]. + if (isArray(item) && + isNaN(Number(currentKey)) && + !isProperty(item, currentKey)) { + for (let i = 0, { length } = item; i < length; i++) { + // if FALSE is returned, then terminate walker. For operations, this simply + // means that the search critera was met. + if (!walkKeyPathValues(item[i], keyPath, next, depth, i, item)) { + return false; + } + } + } + if (depth === keyPath.length || item == null) { + return next(item, key, owner, depth === 0, depth === keyPath.length); + } + return walkKeyPathValues(item[currentKey], keyPath, next, depth + 1, currentKey, item); +}; +class BaseOperation { + constructor(params, owneryQuery, options, name) { + this.params = params; + this.owneryQuery = owneryQuery; + this.options = options; + this.name = name; + this.init(); + } + init() { } + reset() { + this.done = false; + this.keep = false; + } +} +class GroupOperation extends BaseOperation { + constructor(params, owneryQuery, options, children) { + super(params, owneryQuery, options); + this.children = children; + } + /** + */ + reset() { + this.keep = false; + this.done = false; + for (let i = 0, { length } = this.children; i < length; i++) { + this.children[i].reset(); + } + } + /** + */ + childrenNext(item, key, owner, root, leaf) { + let done = true; + let keep = true; + for (let i = 0, { length } = this.children; i < length; i++) { + const childOperation = this.children[i]; + if (!childOperation.done) { + childOperation.next(item, key, owner, root, leaf); + } + if (!childOperation.keep) { + keep = false; + } + if (childOperation.done) { + if (!childOperation.keep) { + break; + } + } + else { + done = false; + } + } + this.done = done; + this.keep = keep; + } +} +class NamedGroupOperation extends GroupOperation { + constructor(params, owneryQuery, options, children, name) { + super(params, owneryQuery, options, children); + this.name = name; + } +} +class QueryOperation extends GroupOperation { + constructor() { + super(...arguments); + this.propop = true; + } + /** + */ + next(item, key, parent, root) { + this.childrenNext(item, key, parent, root); + } +} +class NestedOperation extends GroupOperation { + constructor(keyPath, params, owneryQuery, options, children) { + super(params, owneryQuery, options, children); + this.keyPath = keyPath; + this.propop = true; + /** + */ + this._nextNestedValue = (value, key, owner, root, leaf) => { + this.childrenNext(value, key, owner, root, leaf); + return !this.done; + }; + } + /** + */ + next(item, key, parent) { + walkKeyPathValues(item, this.keyPath, this._nextNestedValue, 0, key, parent); + } +} +const createTester = (a, compare) => { + if (a instanceof Function) { + return a; + } + if (a instanceof RegExp) { + return (b) => { + const result = typeof b === "string" && a.test(b); + a.lastIndex = 0; + return result; + }; + } + const comparableA = comparable(a); + return (b) => compare(comparableA, comparable(b)); +}; +class EqualsOperation extends BaseOperation { + constructor() { + super(...arguments); + this.propop = true; + } + init() { + this._test = createTester(this.params, this.options.compare); + } + next(item, key, parent) { + if (!Array.isArray(parent) || parent.hasOwnProperty(key)) { + if (this._test(item, key, parent)) { + this.done = true; + this.keep = true; + } + } + } +} +const createEqualsOperation = (params, owneryQuery, options) => new EqualsOperation(params, owneryQuery, options); +const numericalOperationCreator = (createNumericalOperation) => (params, owneryQuery, options, name) => { + return createNumericalOperation(params, owneryQuery, options, name); +}; +const numericalOperation = (createTester) => numericalOperationCreator((params, owneryQuery, options, name) => { + const typeofParams = typeof comparable(params); + const test = createTester(params); + return new EqualsOperation((b) => { + const actualValue = coercePotentiallyNull(b); + return (typeof comparable(actualValue) === typeofParams && test(actualValue)); + }, owneryQuery, options, name); +}); +const createNamedOperation = (name, params, parentQuery, options) => { + const operationCreator = options.operations[name]; + if (!operationCreator) { + throwUnsupportedOperation(name); + } + return operationCreator(params, parentQuery, options, name); +}; +const throwUnsupportedOperation = (name) => { + throw new Error(`Unsupported operation: ${name}`); +}; +const containsOperation = (query, options) => { + for (const key in query) { + if (options.operations.hasOwnProperty(key) || key.charAt(0) === "$") + return true; + } + return false; +}; +const createNestedOperation = (keyPath, nestedQuery, parentKey, owneryQuery, options) => { + if (containsOperation(nestedQuery, options)) { + const [selfOperations, nestedOperations] = createQueryOperations(nestedQuery, parentKey, options); + if (nestedOperations.length) { + throw new Error(`Property queries must contain only operations, or exact objects.`); + } + return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, selfOperations); + } + return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, [ + new EqualsOperation(nestedQuery, owneryQuery, options), + ]); +}; +const createQueryOperation = (query, owneryQuery = null, { compare, operations } = {}) => { + const options = { + compare: compare || equals, + operations: Object.assign({}, operations || {}), + }; + const [selfOperations, nestedOperations] = createQueryOperations(query, null, options); + const ops = []; + if (selfOperations.length) { + ops.push(new NestedOperation([], query, owneryQuery, options, selfOperations)); + } + ops.push(...nestedOperations); + if (ops.length === 1) { + return ops[0]; + } + return new QueryOperation(query, owneryQuery, options, ops); +}; +const createQueryOperations = (query, parentKey, options) => { + const selfOperations = []; + const nestedOperations = []; + if (!isVanillaObject(query)) { + selfOperations.push(new EqualsOperation(query, query, options)); + return [selfOperations, nestedOperations]; + } + for (const key in query) { + if (options.operations.hasOwnProperty(key)) { + const op = createNamedOperation(key, query[key], query, options); + if (op) { + if (!op.propop && parentKey && !options.operations[parentKey]) { + throw new Error(`Malformed query. ${key} cannot be matched against property.`); + } + } + // probably just a flag for another operation (like $options) + if (op != null) { + selfOperations.push(op); + } + } + else if (key.charAt(0) === "$") { + throwUnsupportedOperation(key); + } + else { + nestedOperations.push(createNestedOperation(key.split("."), query[key], key, query, options)); + } + } + return [selfOperations, nestedOperations]; +}; +const createOperationTester = (operation) => (item, key, owner) => { + operation.reset(); + operation.next(item, key, owner); + return operation.keep; +}; +const createQueryTester = (query, options = {}) => { + return createOperationTester(createQueryOperation(query, null, options)); +}; + +class $Ne extends BaseOperation { + constructor() { + super(...arguments); + this.propop = true; + } + init() { + this._test = createTester(this.params, this.options.compare); + } + reset() { + super.reset(); + this.keep = true; + } + next(item) { + if (this._test(item)) { + this.done = true; + this.keep = false; + } + } +} +// https://docs.mongodb.com/manual/reference/operator/query/elemMatch/ +class $ElemMatch extends BaseOperation { + constructor() { + super(...arguments); + this.propop = true; + } + init() { + if (!this.params || typeof this.params !== "object") { + throw new Error(`Malformed query. $elemMatch must by an object.`); + } + this._queryOperation = createQueryOperation(this.params, this.owneryQuery, this.options); + } + reset() { + super.reset(); + this._queryOperation.reset(); + } + next(item) { + if (isArray(item)) { + for (let i = 0, { length } = item; i < length; i++) { + // reset query operation since item being tested needs to pass _all_ query + // operations for it to be a success + this._queryOperation.reset(); + const child = item[i]; + this._queryOperation.next(child, i, item, false); + this.keep = this.keep || this._queryOperation.keep; + } + this.done = true; + } + else { + this.done = false; + this.keep = false; + } + } +} +class $Not extends BaseOperation { + constructor() { + super(...arguments); + this.propop = true; + } + init() { + this._queryOperation = createQueryOperation(this.params, this.owneryQuery, this.options); + } + reset() { + super.reset(); + this._queryOperation.reset(); + } + next(item, key, owner, root) { + this._queryOperation.next(item, key, owner, root); + this.done = this._queryOperation.done; + this.keep = !this._queryOperation.keep; + } +} +class $Size extends BaseOperation { + constructor() { + super(...arguments); + this.propop = true; + } + init() { } + next(item) { + if (isArray(item) && item.length === this.params) { + this.done = true; + this.keep = true; + } + // if (parent && parent.length === this.params) { + // this.done = true; + // this.keep = true; + // } + } +} +const assertGroupNotEmpty = (values) => { + if (values.length === 0) { + throw new Error(`$and/$or/$nor must be a nonempty array`); + } +}; +class $Or extends BaseOperation { + constructor() { + super(...arguments); + this.propop = false; + } + init() { + assertGroupNotEmpty(this.params); + this._ops = this.params.map((op) => createQueryOperation(op, null, this.options)); + } + reset() { + this.done = false; + this.keep = false; + for (let i = 0, { length } = this._ops; i < length; i++) { + this._ops[i].reset(); + } + } + next(item, key, owner) { + let done = false; + let success = false; + for (let i = 0, { length } = this._ops; i < length; i++) { + const op = this._ops[i]; + op.next(item, key, owner); + if (op.keep) { + done = true; + success = op.keep; + break; + } + } + this.keep = success; + this.done = done; + } +} +class $Nor extends $Or { + constructor() { + super(...arguments); + this.propop = false; + } + next(item, key, owner) { + super.next(item, key, owner); + this.keep = !this.keep; + } +} +class $In extends BaseOperation { + constructor() { + super(...arguments); + this.propop = true; + } + init() { + const params = Array.isArray(this.params) ? this.params : [this.params]; + this._testers = params.map((value) => { + if (containsOperation(value, this.options)) { + throw new Error(`cannot nest $ under ${this.name.toLowerCase()}`); + } + return createTester(value, this.options.compare); + }); + } + next(item, key, owner) { + let done = false; + let success = false; + for (let i = 0, { length } = this._testers; i < length; i++) { + const test = this._testers[i]; + if (test(item)) { + done = true; + success = true; + break; + } + } + this.keep = success; + this.done = done; + } +} +class $Nin extends BaseOperation { + constructor(params, ownerQuery, options, name) { + super(params, ownerQuery, options, name); + this.propop = true; + this._in = new $In(params, ownerQuery, options, name); + } + next(item, key, owner, root) { + this._in.next(item, key, owner); + if (isArray(owner) && !root) { + if (this._in.keep) { + this.keep = false; + this.done = true; + } + else if (key == owner.length - 1) { + this.keep = true; + this.done = true; + } + } + else { + this.keep = !this._in.keep; + this.done = true; + } + } + reset() { + super.reset(); + this._in.reset(); + } +} +class $Exists extends BaseOperation { + constructor() { + super(...arguments); + this.propop = true; + } + next(item, key, owner, root, leaf) { + if (!leaf) { + this.done = true; + this.keep = !this.params; + } + else if (owner.hasOwnProperty(key) === this.params) { + this.done = true; + this.keep = true; + } + } +} +class $And extends NamedGroupOperation { + constructor(params, owneryQuery, options, name) { + super(params, owneryQuery, options, params.map((query) => createQueryOperation(query, owneryQuery, options)), name); + this.propop = false; + assertGroupNotEmpty(params); + } + next(item, key, owner, root) { + this.childrenNext(item, key, owner, root); + } +} +class $All extends NamedGroupOperation { + constructor(params, owneryQuery, options, name) { + super(params, owneryQuery, options, params.map((query) => createQueryOperation(query, owneryQuery, options)), name); + this.propop = true; + } + next(item, key, owner, root) { + this.childrenNext(item, key, owner, root); + } +} +const $eq = (params, owneryQuery, options) => new EqualsOperation(params, owneryQuery, options); +const $ne = (params, owneryQuery, options, name) => new $Ne(params, owneryQuery, options, name); +const $or = (params, owneryQuery, options, name) => new $Or(params, owneryQuery, options, name); +const $nor = (params, owneryQuery, options, name) => new $Nor(params, owneryQuery, options, name); +const $elemMatch = (params, owneryQuery, options, name) => new $ElemMatch(params, owneryQuery, options, name); +const $nin = (params, owneryQuery, options, name) => new $Nin(params, owneryQuery, options, name); +const $in = (params, owneryQuery, options, name) => { + return new $In(params, owneryQuery, options, name); +}; +const $lt = numericalOperation((params) => (b) => { + return b != null && b < params; +}); +const $lte = numericalOperation((params) => (b) => { + return b === params || b <= params; +}); +const $gt = numericalOperation((params) => (b) => { + return b != null && b > params; +}); +const $gte = numericalOperation((params) => (b) => { + return b === params || b >= params; +}); +const $mod = ([mod, equalsValue], owneryQuery, options) => new EqualsOperation((b) => comparable(b) % mod === equalsValue, owneryQuery, options); +const $exists = (params, owneryQuery, options, name) => new $Exists(params, owneryQuery, options, name); +const $regex = (pattern, owneryQuery, options) => new EqualsOperation(new RegExp(pattern, owneryQuery.$options), owneryQuery, options); +const $not = (params, owneryQuery, options, name) => new $Not(params, owneryQuery, options, name); +const typeAliases = { + number: (v) => typeof v === "number", + string: (v) => typeof v === "string", + bool: (v) => typeof v === "boolean", + array: (v) => Array.isArray(v), + null: (v) => v === null, + timestamp: (v) => v instanceof Date, +}; +const $type = (clazz, owneryQuery, options) => new EqualsOperation((b) => { + if (typeof clazz === "string") { + if (!typeAliases[clazz]) { + throw new Error(`Type alias does not exist`); + } + return typeAliases[clazz](b); + } + return b != null ? b instanceof clazz || b.constructor === clazz : false; +}, owneryQuery, options); +const $and = (params, ownerQuery, options, name) => new $And(params, ownerQuery, options, name); +const $all = (params, ownerQuery, options, name) => new $All(params, ownerQuery, options, name); +const $size = (params, ownerQuery, options) => new $Size(params, ownerQuery, options, "$size"); +const $options = () => null; +const $where = (params, ownerQuery, options) => { + let test; + if (isFunction(params)) { + test = params; + } + else if (!process.env.CSP_ENABLED) { + test = new Function("obj", "return " + params); + } + else { + throw new Error(`In CSP mode, sift does not support strings in "$where" condition`); + } + return new EqualsOperation((b) => test.bind(b)(b), ownerQuery, options); +}; + +var defaultOperations = /*#__PURE__*/Object.freeze({ + __proto__: null, + $Size: $Size, + $all: $all, + $and: $and, + $elemMatch: $elemMatch, + $eq: $eq, + $exists: $exists, + $gt: $gt, + $gte: $gte, + $in: $in, + $lt: $lt, + $lte: $lte, + $mod: $mod, + $ne: $ne, + $nin: $nin, + $nor: $nor, + $not: $not, + $options: $options, + $or: $or, + $regex: $regex, + $size: $size, + $type: $type, + $where: $where +}); + +const createDefaultQueryOperation = (query, ownerQuery, { compare, operations } = {}) => { + return createQueryOperation(query, ownerQuery, { + compare, + operations: Object.assign({}, defaultOperations, operations || {}), + }); +}; +const createDefaultQueryTester = (query, options = {}) => { + const op = createDefaultQueryOperation(query, null, options); + return createOperationTester(op); +}; + +export { $Size, $all, $and, $elemMatch, $eq, $exists, $gt, $gte, $in, $lt, $lte, $mod, $ne, $nin, $nor, $not, $options, $or, $regex, $size, $type, $where, EqualsOperation, createDefaultQueryOperation, createEqualsOperation, createOperationTester, createQueryOperation, createQueryTester, createDefaultQueryTester as default }; +//# sourceMappingURL=index.js.map diff --git a/gateway/node_modules/sift/es/index.js.map b/gateway/node_modules/sift/es/index.js.map new file mode 100644 index 0000000000000000000000000000000000000000..8a9ff1d4db4055a63bb369ad87d9eb1f9bd45f30 --- /dev/null +++ b/gateway/node_modules/sift/es/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sources":["../src/utils.ts","../src/core.ts","../src/operations.ts","../src/index.ts"],"sourcesContent":[null,null,null,null],"names":[],"mappings":"AAEO,MAAM,WAAW,GAAG,CAAQ,IAAI,KAAI;AACzC,IAAA,MAAM,UAAU,GAAG,UAAU,GAAG,IAAI,GAAG,GAAG,CAAC;AAC3C,IAAA,OAAO,UAAU,KAAK,EAAA;AACpB,QAAA,OAAO,YAAY,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC;AAC5C,KAAC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,YAAY,GAAG,CAAC,KAAK,KAAK,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAE/D,MAAM,UAAU,GAAG,CAAC,KAAU,KAAI;AACvC,IAAA,IAAI,KAAK,YAAY,IAAI,EAAE;AACzB,QAAA,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;KACxB;AAAM,SAAA,IAAI,OAAO,CAAC,KAAK,CAAC,EAAE;AACzB,QAAA,OAAO,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;KAC9B;SAAM,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,MAAM,KAAK,UAAU,EAAE;AACtD,QAAA,OAAO,KAAK,CAAC,MAAM,EAAE,CAAC;KACvB;AAED,IAAA,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEK,MAAM,qBAAqB,GAAG,CAAC,KAAU,KAC9C,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,CAAC;AAExB,MAAM,OAAO,GAAG,WAAW,CAAa,OAAO,CAAC,CAAC;AACjD,MAAM,QAAQ,GAAG,WAAW,CAAS,QAAQ,CAAC,CAAC;AAC/C,MAAM,UAAU,GAAG,WAAW,CAAW,UAAU,CAAC,CAAC;AACrD,MAAM,UAAU,GAAG,CAAC,IAAS,EAAE,GAAQ,KAAI;AAChD,IAAA,OAAO,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAC5D,CAAC,CAAC;AACK,MAAM,eAAe,GAAG,CAAC,KAAK,KAAI;AACvC,IAAA,QACE,KAAK;AACL,SAAC,KAAK,CAAC,WAAW,KAAK,MAAM;YAC3B,KAAK,CAAC,WAAW,KAAK,KAAK;AAC3B,YAAA,KAAK,CAAC,WAAW,CAAC,QAAQ,EAAE,KAAK,qCAAqC;AACtE,YAAA,KAAK,CAAC,WAAW,CAAC,QAAQ,EAAE,KAAK,oCAAoC,CAAC;AACxE,QAAA,CAAC,KAAK,CAAC,MAAM,EACb;AACJ,CAAC,CAAC;AAEK,MAAM,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC,KAAI;IAC7B,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,EAAE;AACvB,QAAA,OAAO,IAAI,CAAC;KACb;AACD,IAAA,IAAI,CAAC,KAAK,CAAC,EAAE;AACX,QAAA,OAAO,IAAI,CAAC;KACb;IAED,IAAI,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;AAC3E,QAAA,OAAO,KAAK,CAAC;KACd;AAED,IAAA,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE;QACd,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,EAAE;AACzB,YAAA,OAAO,KAAK,CAAC;SACd;AACD,QAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;AAC/C,YAAA,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAAE,gBAAA,OAAO,KAAK,CAAC;SACvC;AACD,QAAA,OAAO,IAAI,CAAC;KACb;AAAM,SAAA,IAAI,QAAQ,CAAC,CAAC,CAAC,EAAE;AACtB,QAAA,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE;AACnD,YAAA,OAAO,KAAK,CAAC;SACd;AACD,QAAA,KAAK,MAAM,GAAG,IAAI,CAAC,EAAE;AACnB,YAAA,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC;AAAE,gBAAA,OAAO,KAAK,CAAC;SAC3C;AACD,QAAA,OAAO,IAAI,CAAC;KACb;AACD,IAAA,OAAO,KAAK,CAAC;AACf,CAAC;;ACYD;;;AAGG;AAEH,MAAM,iBAAiB,GAAG,CACxB,IAAS,EACT,OAAc,EACd,IAAY,EACZ,KAAa,EACb,GAAQ,EACR,KAAU,KACR;AACF,IAAA,MAAM,UAAU,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;;;IAIlC,IACE,OAAO,CAAC,IAAI,CAAC;AACb,QAAA,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;AACzB,QAAA,CAAC,UAAU,CAAC,IAAI,EAAE,UAAU,CAAC,EAC7B;AACA,QAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;;;AAGlD,YAAA,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE;AAC9D,gBAAA,OAAO,KAAK,CAAC;aACd;SACF;KACF;IAED,IAAI,KAAK,KAAK,OAAO,CAAC,MAAM,IAAI,IAAI,IAAI,IAAI,EAAE;AAC5C,QAAA,OAAO,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,KAAK,CAAC,EAAE,KAAK,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;KACtE;AAED,IAAA,OAAO,iBAAiB,CACtB,IAAI,CAAC,UAAU,CAAC,EAChB,OAAO,EACP,IAAI,EACJ,KAAK,GAAG,CAAC,EACT,UAAU,EACV,IAAI,CACL,CAAC;AACJ,CAAC,CAAC;MAEoB,aAAa,CAAA;AAMjC,IAAA,WAAA,CACW,MAAe,EACf,WAAgB,EAChB,OAAgB,EAChB,IAAa,EAAA;QAHb,IAAM,CAAA,MAAA,GAAN,MAAM,CAAS;QACf,IAAW,CAAA,WAAA,GAAX,WAAW,CAAK;QAChB,IAAO,CAAA,OAAA,GAAP,OAAO,CAAS;QAChB,IAAI,CAAA,IAAA,GAAJ,IAAI,CAAS;QAEtB,IAAI,CAAC,IAAI,EAAE,CAAC;KACb;AACS,IAAA,IAAI,MAAK;IACnB,KAAK,GAAA;AACH,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;AAClB,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;KACnB;AAQF,CAAA;AAED,MAAe,cAAe,SAAQ,aAAkB,CAAA;AAItD,IAAA,WAAA,CACE,MAAW,EACX,WAAgB,EAChB,OAAgB,EACA,QAA0B,EAAA;AAE1C,QAAA,KAAK,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC;QAFpB,IAAQ,CAAA,QAAA,GAAR,QAAQ,CAAkB;KAG3C;AAED;AACG;IAEH,KAAK,GAAA;AACH,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;AAClB,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;QAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;YAC3D,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;SAC1B;KACF;AAID;AACG;IAEO,YAAY,CACpB,IAAS,EACT,GAAQ,EACR,KAAU,EACV,IAAa,EACb,IAAc,EAAA;QAEd,IAAI,IAAI,GAAG,IAAI,CAAC;QAChB,IAAI,IAAI,GAAG,IAAI,CAAC;QAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;YAC3D,MAAM,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AACxC,YAAA,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE;AACxB,gBAAA,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;aACnD;AACD,YAAA,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE;gBACxB,IAAI,GAAG,KAAK,CAAC;aACd;AACD,YAAA,IAAI,cAAc,CAAC,IAAI,EAAE;AACvB,gBAAA,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE;oBACxB,MAAM;iBACP;aACF;iBAAM;gBACL,IAAI,GAAG,KAAK,CAAC;aACd;SACF;AACD,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;KAClB;AACF,CAAA;AAEK,MAAgB,mBACpB,SAAQ,cAAc,CAAA;IAItB,WACE,CAAA,MAAW,EACX,WAAgB,EAChB,OAAgB,EAChB,QAA0B,EACjB,IAAY,EAAA;QAErB,KAAK,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;QAFrC,IAAI,CAAA,IAAA,GAAJ,IAAI,CAAQ;KAGtB;AACF,CAAA;AAEK,MAAO,cAAsB,SAAQ,cAAc,CAAA;AAAzD,IAAA,WAAA,GAAA;;QACW,IAAM,CAAA,MAAA,GAAG,IAAI,CAAC;KAOxB;AANC;AACG;AAEH,IAAA,IAAI,CAAC,IAAW,EAAE,GAAQ,EAAE,MAAW,EAAE,IAAa,EAAA;QACpD,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;KAC5C;AACF,CAAA;AAEK,MAAO,eAAgB,SAAQ,cAAc,CAAA;IAEjD,WACW,CAAA,OAAc,EACvB,MAAW,EACX,WAAgB,EAChB,OAAgB,EAChB,QAA0B,EAAA;QAE1B,KAAK,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;QANrC,IAAO,CAAA,OAAA,GAAP,OAAO,CAAO;QAFhB,IAAM,CAAA,MAAA,GAAG,IAAI,CAAC;AAwBvB;AACG;AAEK,QAAA,IAAA,CAAA,gBAAgB,GAAG,CACzB,KAAU,EACV,GAAQ,EACR,KAAU,EACV,IAAa,EACb,IAAa,KACX;AACF,YAAA,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AACjD,YAAA,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;AACpB,SAAC,CAAC;KA3BD;AACD;AACG;AAEH,IAAA,IAAI,CAAC,IAAS,EAAE,GAAQ,EAAE,MAAW,EAAA;AACnC,QAAA,iBAAiB,CACf,IAAI,EACJ,IAAI,CAAC,OAAO,EACZ,IAAI,CAAC,gBAAgB,EACrB,CAAC,EACD,GAAG,EACH,MAAM,CACP,CAAC;KACH;AAeF,CAAA;AAEM,MAAM,YAAY,GAAG,CAAC,CAAC,EAAE,OAAmB,KAAI;AACrD,IAAA,IAAI,CAAC,YAAY,QAAQ,EAAE;AACzB,QAAA,OAAO,CAAC,CAAC;KACV;AACD,IAAA,IAAI,CAAC,YAAY,MAAM,EAAE;QACvB,OAAO,CAAC,CAAC,KAAI;AACX,YAAA,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClD,YAAA,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC;AAChB,YAAA,OAAO,MAAM,CAAC;AAChB,SAAC,CAAC;KACH;AACD,IAAA,MAAM,WAAW,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;AAClC,IAAA,OAAO,CAAC,CAAC,KAAK,OAAO,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;AACpD,CAAC,CAAC;AAEI,MAAO,eAAwB,SAAQ,aAAqB,CAAA;AAAlE,IAAA,WAAA,GAAA;;QACW,IAAM,CAAA,MAAA,GAAG,IAAI,CAAC;KAaxB;IAXC,IAAI,GAAA;AACF,QAAA,IAAI,CAAC,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;KAC9D;AACD,IAAA,IAAI,CAAC,IAAI,EAAE,GAAQ,EAAE,MAAW,EAAA;AAC9B,QAAA,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE;YACxD,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE;AACjC,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;aAClB;SACF;KACF;AACF,CAAA;MAEY,qBAAqB,GAAG,CACnC,MAAW,EACX,WAAgB,EAChB,OAAgB,KACb,IAAI,eAAe,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE;AAEhD,MAAM,yBAAyB,GACpC,CAAC,wBAA+C,KAChD,CAAC,MAAW,EAAE,WAAgB,EAAE,OAAgB,EAAE,IAAY,KAAI;IAChE,OAAO,wBAAwB,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AACtE,CAAC,CAAC;AAEG,MAAM,kBAAkB,GAAG,CAAC,YAAoC,KACrE,yBAAyB,CACvB,CAAC,MAAW,EAAE,WAAuB,EAAE,OAAgB,EAAE,IAAY,KAAI;AACvE,IAAA,MAAM,YAAY,GAAG,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC;AAC/C,IAAA,MAAM,IAAI,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;AAClC,IAAA,OAAO,IAAI,eAAe,CACxB,CAAC,CAAC,KAAI;AACJ,QAAA,MAAM,WAAW,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAAC;AAC7C,QAAA,QACE,OAAO,UAAU,CAAC,WAAW,CAAC,KAAK,YAAY,IAAI,IAAI,CAAC,WAAW,CAAC,EACpE;AACJ,KAAC,EACD,WAAW,EACX,OAAO,EACP,IAAI,CACL,CAAC;AACJ,CAAC,CACF,CAAC;AASJ,MAAM,oBAAoB,GAAG,CAC3B,IAAY,EACZ,MAAW,EACX,WAAgB,EAChB,OAAgB,KACd;IACF,MAAM,gBAAgB,GAAG,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAClD,IAAI,CAAC,gBAAgB,EAAE;QACrB,yBAAyB,CAAC,IAAI,CAAC,CAAC;KACjC;IACD,OAAO,gBAAgB,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AAC9D,CAAC,CAAC;AAEF,MAAM,yBAAyB,GAAG,CAAC,IAAY,KAAI;AACjD,IAAA,MAAM,IAAI,KAAK,CAAC,0BAA0B,IAAI,CAAA,CAAE,CAAC,CAAC;AACpD,CAAC,CAAC;AAEK,MAAM,iBAAiB,GAAG,CAAC,KAAU,EAAE,OAAgB,KAAI;AAChE,IAAA,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE;AACvB,QAAA,IAAI,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG;AACjE,YAAA,OAAO,IAAI,CAAC;KACf;AACD,IAAA,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AACF,MAAM,qBAAqB,GAAG,CAC5B,OAAc,EACd,WAAgB,EAChB,SAAiB,EACjB,WAAgB,EAChB,OAAgB,KACd;AACF,IAAA,IAAI,iBAAiB,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE;AAC3C,QAAA,MAAM,CAAC,cAAc,EAAE,gBAAgB,CAAC,GAAG,qBAAqB,CAC9D,WAAW,EACX,SAAS,EACT,OAAO,CACR,CAAC;AACF,QAAA,IAAI,gBAAgB,CAAC,MAAM,EAAE;AAC3B,YAAA,MAAM,IAAI,KAAK,CACb,CAAA,gEAAA,CAAkE,CACnE,CAAC;SACH;AACD,QAAA,OAAO,IAAI,eAAe,CACxB,OAAO,EACP,WAAW,EACX,WAAW,EACX,OAAO,EACP,cAAc,CACf,CAAC;KACH;IACD,OAAO,IAAI,eAAe,CAAC,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,OAAO,EAAE;AACrE,QAAA,IAAI,eAAe,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC;AACvD,KAAA,CAAC,CAAC;AACL,CAAC,CAAC;AAEW,MAAA,oBAAoB,GAAG,CAClC,KAAqB,EACrB,WAAA,GAAmB,IAAI,EACvB,EAAE,OAAO,EAAE,UAAU,EAAuB,GAAA,EAAE,KACrB;AACzB,IAAA,MAAM,OAAO,GAAG;QACd,OAAO,EAAE,OAAO,IAAI,MAAM;QAC1B,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,UAAU,IAAI,EAAE,CAAC;KAChD,CAAC;AAEF,IAAA,MAAM,CAAC,cAAc,EAAE,gBAAgB,CAAC,GAAG,qBAAqB,CAC9D,KAAK,EACL,IAAI,EACJ,OAAO,CACR,CAAC;IAEF,MAAM,GAAG,GAAG,EAAE,CAAC;AAEf,IAAA,IAAI,cAAc,CAAC,MAAM,EAAE;AACzB,QAAA,GAAG,CAAC,IAAI,CACN,IAAI,eAAe,CAAC,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,CAAC,CACrE,CAAC;KACH;AAED,IAAA,GAAG,CAAC,IAAI,CAAC,GAAG,gBAAgB,CAAC,CAAC;AAE9B,IAAA,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE;AACpB,QAAA,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC;KACf;IACD,OAAO,IAAI,cAAc,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;AAC9D,EAAE;AAEF,MAAM,qBAAqB,GAAG,CAC5B,KAAU,EACV,SAAiB,EACjB,OAAgB,KACd;IACF,MAAM,cAAc,GAAG,EAAE,CAAC;IAC1B,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAC5B,IAAA,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,EAAE;AAC3B,QAAA,cAAc,CAAC,IAAI,CAAC,IAAI,eAAe,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;AAChE,QAAA,OAAO,CAAC,cAAc,EAAE,gBAAgB,CAAC,CAAC;KAC3C;AACD,IAAA,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE;QACvB,IAAI,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE;AAC1C,YAAA,MAAM,EAAE,GAAG,oBAAoB,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;YAEjE,IAAI,EAAE,EAAE;AACN,gBAAA,IAAI,CAAC,EAAE,CAAC,MAAM,IAAI,SAAS,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE;AAC7D,oBAAA,MAAM,IAAI,KAAK,CACb,oBAAoB,GAAG,CAAA,oCAAA,CAAsC,CAC9D,CAAC;iBACH;aACF;;AAGD,YAAA,IAAI,EAAE,IAAI,IAAI,EAAE;AACd,gBAAA,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;aACzB;SACF;aAAM,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;YAChC,yBAAyB,CAAC,GAAG,CAAC,CAAC;SAChC;aAAM;YACL,gBAAgB,CAAC,IAAI,CACnB,qBAAqB,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,CACvE,CAAC;SACH;KACF;AAED,IAAA,OAAO,CAAC,cAAc,EAAE,gBAAgB,CAAC,CAAC;AAC5C,CAAC,CAAC;AAEW,MAAA,qBAAqB,GAChC,CAAQ,SAA2B,KACnC,CAAC,IAAW,EAAE,GAAS,EAAE,KAAW,KAAI;IACtC,SAAS,CAAC,KAAK,EAAE,CAAC;IAClB,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;IACjC,OAAO,SAAS,CAAC,IAAI,CAAC;AACxB,EAAE;AAES,MAAA,iBAAiB,GAAG,CAC/B,KAAqB,EACrB,OAAA,GAA4B,EAAE,KAC5B;IACF,OAAO,qBAAqB,CAC1B,oBAAoB,CAAiB,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAC3D,CAAC;AACJ;;AC7dA,MAAM,GAAI,SAAQ,aAAkB,CAAA;AAApC,IAAA,WAAA,GAAA;;QACW,IAAM,CAAA,MAAA,GAAG,IAAI,CAAC;KAexB;IAbC,IAAI,GAAA;AACF,QAAA,IAAI,CAAC,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;KAC9D;IACD,KAAK,GAAA;QACH,KAAK,CAAC,KAAK,EAAE,CAAC;AACd,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;KAClB;AACD,IAAA,IAAI,CAAC,IAAS,EAAA;AACZ,QAAA,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;AACpB,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,YAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;SACnB;KACF;AACF,CAAA;AACD;AACA,MAAM,UAAW,SAAQ,aAAyB,CAAA;AAAlD,IAAA,WAAA,GAAA;;QACW,IAAM,CAAA,MAAA,GAAG,IAAI,CAAC;KAiCxB;IA/BC,IAAI,GAAA;AACF,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,EAAE;AACnD,YAAA,MAAM,IAAI,KAAK,CAAC,CAAA,8CAAA,CAAgD,CAAC,CAAC;SACnE;AACD,QAAA,IAAI,CAAC,eAAe,GAAG,oBAAoB,CACzC,IAAI,CAAC,MAAM,EACX,IAAI,CAAC,WAAW,EAChB,IAAI,CAAC,OAAO,CACb,CAAC;KACH;IACD,KAAK,GAAA;QACH,KAAK,CAAC,KAAK,EAAE,CAAC;AACd,QAAA,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;KAC9B;AACD,IAAA,IAAI,CAAC,IAAS,EAAA;AACZ,QAAA,IAAI,OAAO,CAAC,IAAI,CAAC,EAAE;AACjB,YAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;;;AAGlD,gBAAA,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;AAE7B,gBAAA,MAAM,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;AACtB,gBAAA,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;AACjD,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC;aACpD;AACD,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB;aAAM;AACL,YAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;AAClB,YAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;SACnB;KACF;AACF,CAAA;AAED,MAAM,IAAK,SAAQ,aAAyB,CAAA;AAA5C,IAAA,WAAA,GAAA;;QACW,IAAM,CAAA,MAAA,GAAG,IAAI,CAAC;KAkBxB;IAhBC,IAAI,GAAA;AACF,QAAA,IAAI,CAAC,eAAe,GAAG,oBAAoB,CACzC,IAAI,CAAC,MAAM,EACX,IAAI,CAAC,WAAW,EAChB,IAAI,CAAC,OAAO,CACb,CAAC;KACH;IACD,KAAK,GAAA;QACH,KAAK,CAAC,KAAK,EAAE,CAAC;AACd,QAAA,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;KAC9B;AACD,IAAA,IAAI,CAAC,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAA;AACjD,QAAA,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;QAClD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC;KACxC;AACF,CAAA;AAEK,MAAO,KAAM,SAAQ,aAAkB,CAAA;AAA7C,IAAA,WAAA,GAAA;;QACW,IAAM,CAAA,MAAA,GAAG,IAAI,CAAC;KAYxB;AAXC,IAAA,IAAI,MAAK;AACT,IAAA,IAAI,CAAC,IAAI,EAAA;AACP,QAAA,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,EAAE;AAChD,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB;;;;;KAKF;AACF,CAAA;AAED,MAAM,mBAAmB,GAAG,CAAC,MAAa,KAAI;AAC5C,IAAA,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;AACvB,QAAA,MAAM,IAAI,KAAK,CAAC,CAAA,sCAAA,CAAwC,CAAC,CAAC;KAC3D;AACH,CAAC,CAAC;AAEF,MAAM,GAAI,SAAQ,aAAkB,CAAA;AAApC,IAAA,WAAA,GAAA;;QACW,IAAM,CAAA,MAAA,GAAG,KAAK,CAAC;KA+BzB;IA7BC,IAAI,GAAA;AACF,QAAA,mBAAmB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,KAC7B,oBAAoB,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,CAC7C,CAAC;KACH;IACD,KAAK,GAAA;AACH,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;AAClB,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;QAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;YACvD,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;SACtB;KACF;AACD,IAAA,IAAI,CAAC,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAA;QAClC,IAAI,IAAI,GAAG,KAAK,CAAC;QACjB,IAAI,OAAO,GAAG,KAAK,CAAC;QACpB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;YACvD,MAAM,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACxB,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;AAC1B,YAAA,IAAI,EAAE,CAAC,IAAI,EAAE;gBACX,IAAI,GAAG,IAAI,CAAC;AACZ,gBAAA,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC;gBAClB,MAAM;aACP;SACF;AAED,QAAA,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;AACpB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;KAClB;AACF,CAAA;AAED,MAAM,IAAK,SAAQ,GAAG,CAAA;AAAtB,IAAA,WAAA,GAAA;;QACW,IAAM,CAAA,MAAA,GAAG,KAAK,CAAC;KAKzB;AAJC,IAAA,IAAI,CAAC,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAA;QAClC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;AAC7B,QAAA,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;KACxB;AACF,CAAA;AAED,MAAM,GAAI,SAAQ,aAAkB,CAAA;AAApC,IAAA,WAAA,GAAA;;QACW,IAAM,CAAA,MAAA,GAAG,IAAI,CAAC;KA0BxB;IAxBC,IAAI,GAAA;QACF,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACxE,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,KAAI;YACnC,IAAI,iBAAiB,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE;AAC1C,gBAAA,MAAM,IAAI,KAAK,CAAC,CAAA,oBAAA,EAAuB,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,CAAE,CAAA,CAAC,CAAC;aACnE;YACD,OAAO,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AACnD,SAAC,CAAC,CAAC;KACJ;AACD,IAAA,IAAI,CAAC,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAA;QAClC,IAAI,IAAI,GAAG,KAAK,CAAC;QACjB,IAAI,OAAO,GAAG,KAAK,CAAC;QACpB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;YAC3D,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAC9B,YAAA,IAAI,IAAI,CAAC,IAAI,CAAC,EAAE;gBACd,IAAI,GAAG,IAAI,CAAC;gBACZ,OAAO,GAAG,IAAI,CAAC;gBACf,MAAM;aACP;SACF;AAED,QAAA,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;AACpB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;KAClB;AACF,CAAA;AAED,MAAM,IAAK,SAAQ,aAAkB,CAAA;AAGnC,IAAA,WAAA,CAAY,MAAW,EAAE,UAAe,EAAE,OAAgB,EAAE,IAAY,EAAA;QACtE,KAAK,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;QAHlC,IAAM,CAAA,MAAA,GAAG,IAAI,CAAC;AAIrB,QAAA,IAAI,CAAC,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;KACvD;AACD,IAAA,IAAI,CAAC,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAA;QACjD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;QAEhC,IAAI,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE;AAC3B,YAAA,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE;AACjB,gBAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;AAClB,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;aAClB;iBAAM,IAAI,GAAG,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;AAClC,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;aAClB;SACF;aAAM;YACL,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;AAC3B,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB;KACF;IACD,KAAK,GAAA;QACH,KAAK,CAAC,KAAK,EAAE,CAAC;AACd,QAAA,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;KAClB;AACF,CAAA;AAED,MAAM,OAAQ,SAAQ,aAAsB,CAAA;AAA5C,IAAA,WAAA,GAAA;;QACW,IAAM,CAAA,MAAA,GAAG,IAAI,CAAC;KAUxB;IATC,IAAI,CAAC,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAE,IAAc,EAAA;QACjE,IAAI,CAAC,IAAI,EAAE;AACT,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,YAAA,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC;SAC1B;aAAM,IAAI,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC,MAAM,EAAE;AACpD,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB;KACF;AACF,CAAA;AAED,MAAM,IAAK,SAAQ,mBAAmB,CAAA;AAEpC,IAAA,WAAA,CACE,MAAoB,EACpB,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA;AAEZ,QAAA,KAAK,CACH,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,KAAK,oBAAoB,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC,EACxE,IAAI,CACL,CAAC;QAbK,IAAM,CAAA,MAAA,GAAG,KAAK,CAAC;QAetB,mBAAmB,CAAC,MAAM,CAAC,CAAC;KAC7B;AACD,IAAA,IAAI,CAAC,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAA;QACjD,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;KAC3C;AACF,CAAA;AAED,MAAM,IAAK,SAAQ,mBAAmB,CAAA;AAEpC,IAAA,WAAA,CACE,MAAoB,EACpB,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA;AAEZ,QAAA,KAAK,CACH,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,KAAK,oBAAoB,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC,EACxE,IAAI,CACL,CAAC;QAbK,IAAM,CAAA,MAAA,GAAG,IAAI,CAAC;KActB;AACD,IAAA,IAAI,CAAC,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAA;QACjD,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;KAC3C;AACF,CAAA;MAEY,GAAG,GAAG,CAAC,MAAW,EAAE,WAAuB,EAAE,OAAgB,KACxE,IAAI,eAAe,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE;AACvC,MAAA,GAAG,GAAG,CACjB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,KACT,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,EAAE;AACpC,MAAA,GAAG,GAAG,CACjB,MAAoB,EACpB,WAAuB,EACvB,OAAgB,EAChB,IAAY,KACT,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,EAAE;AACpC,MAAA,IAAI,GAAG,CAClB,MAAoB,EACpB,WAAuB,EACvB,OAAgB,EAChB,IAAY,KACT,IAAI,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,EAAE;AACrC,MAAA,UAAU,GAAG,CACxB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,KACT,IAAI,UAAU,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,EAAE;AAC3C,MAAA,IAAI,GAAG,CAClB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,KACT,IAAI,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,EAAE;AAC3C,MAAM,GAAG,GAAG,CACjB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,KACV;IACF,OAAO,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AACrD,EAAE;AAEK,MAAM,GAAG,GAAG,kBAAkB,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,KAAI;AACtD,IAAA,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC,GAAG,MAAM,CAAC;AACjC,CAAC,EAAE;AACI,MAAM,IAAI,GAAG,kBAAkB,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,KAAI;AACvD,IAAA,OAAO,CAAC,KAAK,MAAM,IAAI,CAAC,IAAI,MAAM,CAAC;AACrC,CAAC,EAAE;AACI,MAAM,GAAG,GAAG,kBAAkB,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,KAAI;AACtD,IAAA,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC,GAAG,MAAM,CAAC;AACjC,CAAC,EAAE;AACI,MAAM,IAAI,GAAG,kBAAkB,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,KAAI;AACvD,IAAA,OAAO,CAAC,KAAK,MAAM,IAAI,CAAC,IAAI,MAAM,CAAC;AACrC,CAAC,EAAE;AACU,MAAA,IAAI,GAAG,CAClB,CAAC,GAAG,EAAE,WAAW,CAAW,EAC5B,WAAuB,EACvB,OAAgB,KAEhB,IAAI,eAAe,CACjB,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,GAAG,GAAG,KAAK,WAAW,EAC1C,WAAW,EACX,OAAO,EACP;AACS,MAAA,OAAO,GAAG,CACrB,MAAe,EACf,WAAuB,EACvB,OAAgB,EAChB,IAAY,KACT,IAAI,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,EAAE;AAC9C,MAAM,MAAM,GAAG,CACpB,OAAe,EACf,WAAuB,EACvB,OAAgB,KAEhB,IAAI,eAAe,CACjB,IAAI,MAAM,CAAC,OAAO,EAAE,WAAW,CAAC,QAAQ,CAAC,EACzC,WAAW,EACX,OAAO,EACP;AACS,MAAA,IAAI,GAAG,CAClB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,KACT,IAAI,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,EAAE;AAElD,MAAM,WAAW,GAAG;IAClB,MAAM,EAAE,CAAC,CAAC,KAAK,OAAO,CAAC,KAAK,QAAQ;IACpC,MAAM,EAAE,CAAC,CAAC,KAAK,OAAO,CAAC,KAAK,QAAQ;IACpC,IAAI,EAAE,CAAC,CAAC,KAAK,OAAO,CAAC,KAAK,SAAS;IACnC,KAAK,EAAE,CAAC,CAAC,KAAK,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;IAC9B,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,KAAK,IAAI;IACvB,SAAS,EAAE,CAAC,CAAC,KAAK,CAAC,YAAY,IAAI;CACpC,CAAC;AAEW,MAAA,KAAK,GAAG,CACnB,KAAwB,EACxB,WAAuB,EACvB,OAAgB,KAEhB,IAAI,eAAe,CACjB,CAAC,CAAC,KAAI;AACJ,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AAC7B,QAAA,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE;AACvB,YAAA,MAAM,IAAI,KAAK,CAAC,CAAA,yBAAA,CAA2B,CAAC,CAAC;SAC9C;AAED,QAAA,OAAO,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;KAC9B;AAED,IAAA,OAAO,CAAC,IAAI,IAAI,GAAG,CAAC,YAAY,KAAK,IAAI,CAAC,CAAC,WAAW,KAAK,KAAK,GAAG,KAAK,CAAC;AAC3E,CAAC,EACD,WAAW,EACX,OAAO,EACP;AACS,MAAA,IAAI,GAAG,CAClB,MAAoB,EACpB,UAAsB,EACtB,OAAgB,EAChB,IAAY,KACT,IAAI,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,EAAE;AAEpC,MAAA,IAAI,GAAG,CAClB,MAAoB,EACpB,UAAsB,EACtB,OAAgB,EAChB,IAAY,KACT,IAAI,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,EAAE;AACpC,MAAA,KAAK,GAAG,CACnB,MAAc,EACd,UAAsB,EACtB,OAAgB,KACb,IAAI,KAAK,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE;MACxC,QAAQ,GAAG,MAAM,KAAK;AACtB,MAAA,MAAM,GAAG,CACpB,MAAyB,EACzB,UAAsB,EACtB,OAAgB,KACd;AACF,IAAA,IAAI,IAAI,CAAC;AAET,IAAA,IAAI,UAAU,CAAC,MAAM,CAAC,EAAE;QACtB,IAAI,GAAG,MAAM,CAAC;KACf;AAAM,SAAA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE;QACnC,IAAI,GAAG,IAAI,QAAQ,CAAC,KAAK,EAAE,SAAS,GAAG,MAAM,CAAC,CAAC;KAChD;SAAM;AACL,QAAA,MAAM,IAAI,KAAK,CACb,CAAA,gEAAA,CAAkE,CACnE,CAAC;KACH;IAED,OAAO,IAAI,eAAe,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;AAC1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACpZA,MAAM,2BAA2B,GAAG,CAClC,KAAqB,EACrB,UAAe,EACf,EAAE,OAAO,EAAE,UAAU,EAAuB,GAAA,EAAE,KAC5C;AACF,IAAA,OAAO,oBAAoB,CAAC,KAAK,EAAE,UAAU,EAAE;QAC7C,OAAO;AACP,QAAA,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,iBAAiB,EAAE,UAAU,IAAI,EAAE,CAAC;AACnE,KAAA,CAAC,CAAC;AACL,EAAE;AAEI,MAAA,wBAAwB,GAAG,CAC/B,KAAqB,EACrB,OAA4B,GAAA,EAAE,KAC5B;IACF,MAAM,EAAE,GAAG,2BAA2B,CAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAC7D,IAAA,OAAO,qBAAqB,CAAC,EAAE,CAAC,CAAC;AACnC;;;;"} \ No newline at end of file diff --git a/gateway/node_modules/sift/es5m/index.js b/gateway/node_modules/sift/es5m/index.js new file mode 100644 index 0000000000000000000000000000000000000000..3136cbfc3da3e08316eed87a5501abb8a50894ba --- /dev/null +++ b/gateway/node_modules/sift/es5m/index.js @@ -0,0 +1,743 @@ +/****************************************************************************** +Copyright (c) Microsoft Corporation. + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. +***************************************************************************** */ +/* global Reflect, Promise, SuppressedError, Symbol */ + +var extendStatics = function(d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); +}; + +function __extends(d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +} + +typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}; + +var typeChecker = function (type) { + var typeString = "[object " + type + "]"; + return function (value) { + return getClassName(value) === typeString; + }; +}; +var getClassName = function (value) { return Object.prototype.toString.call(value); }; +var comparable = function (value) { + if (value instanceof Date) { + return value.getTime(); + } + else if (isArray(value)) { + return value.map(comparable); + } + else if (value && typeof value.toJSON === "function") { + return value.toJSON(); + } + return value; +}; +var coercePotentiallyNull = function (value) { + return value == null ? null : value; +}; +var isArray = typeChecker("Array"); +var isObject = typeChecker("Object"); +var isFunction = typeChecker("Function"); +var isProperty = function (item, key) { + return item.hasOwnProperty(key) && !isFunction(item[key]); +}; +var isVanillaObject = function (value) { + return (value && + (value.constructor === Object || + value.constructor === Array || + value.constructor.toString() === "function Object() { [native code] }" || + value.constructor.toString() === "function Array() { [native code] }") && + !value.toJSON); +}; +var equals = function (a, b) { + if (a == null && a == b) { + return true; + } + if (a === b) { + return true; + } + if (Object.prototype.toString.call(a) !== Object.prototype.toString.call(b)) { + return false; + } + if (isArray(a)) { + if (a.length !== b.length) { + return false; + } + for (var i = 0, length_1 = a.length; i < length_1; i++) { + if (!equals(a[i], b[i])) + return false; + } + return true; + } + else if (isObject(a)) { + if (Object.keys(a).length !== Object.keys(b).length) { + return false; + } + for (var key in a) { + if (!equals(a[key], b[key])) + return false; + } + return true; + } + return false; +}; + +/** + * Walks through each value given the context - used for nested operations. E.g: + * { "person.address": { $eq: "blarg" }} + */ +var walkKeyPathValues = function (item, keyPath, next, depth, key, owner) { + var currentKey = keyPath[depth]; + // if array, then try matching. Might fall through for cases like: + // { $eq: [1, 2, 3] }, [ 1, 2, 3 ]. + if (isArray(item) && + isNaN(Number(currentKey)) && + !isProperty(item, currentKey)) { + for (var i = 0, length_1 = item.length; i < length_1; i++) { + // if FALSE is returned, then terminate walker. For operations, this simply + // means that the search critera was met. + if (!walkKeyPathValues(item[i], keyPath, next, depth, i, item)) { + return false; + } + } + } + if (depth === keyPath.length || item == null) { + return next(item, key, owner, depth === 0, depth === keyPath.length); + } + return walkKeyPathValues(item[currentKey], keyPath, next, depth + 1, currentKey, item); +}; +var BaseOperation = /** @class */ (function () { + function BaseOperation(params, owneryQuery, options, name) { + this.params = params; + this.owneryQuery = owneryQuery; + this.options = options; + this.name = name; + this.init(); + } + BaseOperation.prototype.init = function () { }; + BaseOperation.prototype.reset = function () { + this.done = false; + this.keep = false; + }; + return BaseOperation; +}()); +var GroupOperation = /** @class */ (function (_super) { + __extends(GroupOperation, _super); + function GroupOperation(params, owneryQuery, options, children) { + var _this = _super.call(this, params, owneryQuery, options) || this; + _this.children = children; + return _this; + } + /** + */ + GroupOperation.prototype.reset = function () { + this.keep = false; + this.done = false; + for (var i = 0, length_2 = this.children.length; i < length_2; i++) { + this.children[i].reset(); + } + }; + /** + */ + GroupOperation.prototype.childrenNext = function (item, key, owner, root, leaf) { + var done = true; + var keep = true; + for (var i = 0, length_3 = this.children.length; i < length_3; i++) { + var childOperation = this.children[i]; + if (!childOperation.done) { + childOperation.next(item, key, owner, root, leaf); + } + if (!childOperation.keep) { + keep = false; + } + if (childOperation.done) { + if (!childOperation.keep) { + break; + } + } + else { + done = false; + } + } + this.done = done; + this.keep = keep; + }; + return GroupOperation; +}(BaseOperation)); +var NamedGroupOperation = /** @class */ (function (_super) { + __extends(NamedGroupOperation, _super); + function NamedGroupOperation(params, owneryQuery, options, children, name) { + var _this = _super.call(this, params, owneryQuery, options, children) || this; + _this.name = name; + return _this; + } + return NamedGroupOperation; +}(GroupOperation)); +var QueryOperation = /** @class */ (function (_super) { + __extends(QueryOperation, _super); + function QueryOperation() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + /** + */ + QueryOperation.prototype.next = function (item, key, parent, root) { + this.childrenNext(item, key, parent, root); + }; + return QueryOperation; +}(GroupOperation)); +var NestedOperation = /** @class */ (function (_super) { + __extends(NestedOperation, _super); + function NestedOperation(keyPath, params, owneryQuery, options, children) { + var _this = _super.call(this, params, owneryQuery, options, children) || this; + _this.keyPath = keyPath; + _this.propop = true; + /** + */ + _this._nextNestedValue = function (value, key, owner, root, leaf) { + _this.childrenNext(value, key, owner, root, leaf); + return !_this.done; + }; + return _this; + } + /** + */ + NestedOperation.prototype.next = function (item, key, parent) { + walkKeyPathValues(item, this.keyPath, this._nextNestedValue, 0, key, parent); + }; + return NestedOperation; +}(GroupOperation)); +var createTester = function (a, compare) { + if (a instanceof Function) { + return a; + } + if (a instanceof RegExp) { + return function (b) { + var result = typeof b === "string" && a.test(b); + a.lastIndex = 0; + return result; + }; + } + var comparableA = comparable(a); + return function (b) { return compare(comparableA, comparable(b)); }; +}; +var EqualsOperation = /** @class */ (function (_super) { + __extends(EqualsOperation, _super); + function EqualsOperation() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + EqualsOperation.prototype.init = function () { + this._test = createTester(this.params, this.options.compare); + }; + EqualsOperation.prototype.next = function (item, key, parent) { + if (!Array.isArray(parent) || parent.hasOwnProperty(key)) { + if (this._test(item, key, parent)) { + this.done = true; + this.keep = true; + } + } + }; + return EqualsOperation; +}(BaseOperation)); +var createEqualsOperation = function (params, owneryQuery, options) { return new EqualsOperation(params, owneryQuery, options); }; +var numericalOperationCreator = function (createNumericalOperation) { + return function (params, owneryQuery, options, name) { + return createNumericalOperation(params, owneryQuery, options, name); + }; +}; +var numericalOperation = function (createTester) { + return numericalOperationCreator(function (params, owneryQuery, options, name) { + var typeofParams = typeof comparable(params); + var test = createTester(params); + return new EqualsOperation(function (b) { + var actualValue = coercePotentiallyNull(b); + return (typeof comparable(actualValue) === typeofParams && test(actualValue)); + }, owneryQuery, options, name); + }); +}; +var createNamedOperation = function (name, params, parentQuery, options) { + var operationCreator = options.operations[name]; + if (!operationCreator) { + throwUnsupportedOperation(name); + } + return operationCreator(params, parentQuery, options, name); +}; +var throwUnsupportedOperation = function (name) { + throw new Error("Unsupported operation: ".concat(name)); +}; +var containsOperation = function (query, options) { + for (var key in query) { + if (options.operations.hasOwnProperty(key) || key.charAt(0) === "$") + return true; + } + return false; +}; +var createNestedOperation = function (keyPath, nestedQuery, parentKey, owneryQuery, options) { + if (containsOperation(nestedQuery, options)) { + var _a = createQueryOperations(nestedQuery, parentKey, options), selfOperations = _a[0], nestedOperations = _a[1]; + if (nestedOperations.length) { + throw new Error("Property queries must contain only operations, or exact objects."); + } + return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, selfOperations); + } + return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, [ + new EqualsOperation(nestedQuery, owneryQuery, options), + ]); +}; +var createQueryOperation = function (query, owneryQuery, _a) { + if (owneryQuery === void 0) { owneryQuery = null; } + var _b = _a === void 0 ? {} : _a, compare = _b.compare, operations = _b.operations; + var options = { + compare: compare || equals, + operations: Object.assign({}, operations || {}), + }; + var _c = createQueryOperations(query, null, options), selfOperations = _c[0], nestedOperations = _c[1]; + var ops = []; + if (selfOperations.length) { + ops.push(new NestedOperation([], query, owneryQuery, options, selfOperations)); + } + ops.push.apply(ops, nestedOperations); + if (ops.length === 1) { + return ops[0]; + } + return new QueryOperation(query, owneryQuery, options, ops); +}; +var createQueryOperations = function (query, parentKey, options) { + var selfOperations = []; + var nestedOperations = []; + if (!isVanillaObject(query)) { + selfOperations.push(new EqualsOperation(query, query, options)); + return [selfOperations, nestedOperations]; + } + for (var key in query) { + if (options.operations.hasOwnProperty(key)) { + var op = createNamedOperation(key, query[key], query, options); + if (op) { + if (!op.propop && parentKey && !options.operations[parentKey]) { + throw new Error("Malformed query. ".concat(key, " cannot be matched against property.")); + } + } + // probably just a flag for another operation (like $options) + if (op != null) { + selfOperations.push(op); + } + } + else if (key.charAt(0) === "$") { + throwUnsupportedOperation(key); + } + else { + nestedOperations.push(createNestedOperation(key.split("."), query[key], key, query, options)); + } + } + return [selfOperations, nestedOperations]; +}; +var createOperationTester = function (operation) { + return function (item, key, owner) { + operation.reset(); + operation.next(item, key, owner); + return operation.keep; + }; +}; +var createQueryTester = function (query, options) { + if (options === void 0) { options = {}; } + return createOperationTester(createQueryOperation(query, null, options)); +}; + +var $Ne = /** @class */ (function (_super) { + __extends($Ne, _super); + function $Ne() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + $Ne.prototype.init = function () { + this._test = createTester(this.params, this.options.compare); + }; + $Ne.prototype.reset = function () { + _super.prototype.reset.call(this); + this.keep = true; + }; + $Ne.prototype.next = function (item) { + if (this._test(item)) { + this.done = true; + this.keep = false; + } + }; + return $Ne; +}(BaseOperation)); +// https://docs.mongodb.com/manual/reference/operator/query/elemMatch/ +var $ElemMatch = /** @class */ (function (_super) { + __extends($ElemMatch, _super); + function $ElemMatch() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + $ElemMatch.prototype.init = function () { + if (!this.params || typeof this.params !== "object") { + throw new Error("Malformed query. $elemMatch must by an object."); + } + this._queryOperation = createQueryOperation(this.params, this.owneryQuery, this.options); + }; + $ElemMatch.prototype.reset = function () { + _super.prototype.reset.call(this); + this._queryOperation.reset(); + }; + $ElemMatch.prototype.next = function (item) { + if (isArray(item)) { + for (var i = 0, length_1 = item.length; i < length_1; i++) { + // reset query operation since item being tested needs to pass _all_ query + // operations for it to be a success + this._queryOperation.reset(); + var child = item[i]; + this._queryOperation.next(child, i, item, false); + this.keep = this.keep || this._queryOperation.keep; + } + this.done = true; + } + else { + this.done = false; + this.keep = false; + } + }; + return $ElemMatch; +}(BaseOperation)); +var $Not = /** @class */ (function (_super) { + __extends($Not, _super); + function $Not() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + $Not.prototype.init = function () { + this._queryOperation = createQueryOperation(this.params, this.owneryQuery, this.options); + }; + $Not.prototype.reset = function () { + _super.prototype.reset.call(this); + this._queryOperation.reset(); + }; + $Not.prototype.next = function (item, key, owner, root) { + this._queryOperation.next(item, key, owner, root); + this.done = this._queryOperation.done; + this.keep = !this._queryOperation.keep; + }; + return $Not; +}(BaseOperation)); +var $Size = /** @class */ (function (_super) { + __extends($Size, _super); + function $Size() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + $Size.prototype.init = function () { }; + $Size.prototype.next = function (item) { + if (isArray(item) && item.length === this.params) { + this.done = true; + this.keep = true; + } + // if (parent && parent.length === this.params) { + // this.done = true; + // this.keep = true; + // } + }; + return $Size; +}(BaseOperation)); +var assertGroupNotEmpty = function (values) { + if (values.length === 0) { + throw new Error("$and/$or/$nor must be a nonempty array"); + } +}; +var $Or = /** @class */ (function (_super) { + __extends($Or, _super); + function $Or() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = false; + return _this; + } + $Or.prototype.init = function () { + var _this = this; + assertGroupNotEmpty(this.params); + this._ops = this.params.map(function (op) { + return createQueryOperation(op, null, _this.options); + }); + }; + $Or.prototype.reset = function () { + this.done = false; + this.keep = false; + for (var i = 0, length_2 = this._ops.length; i < length_2; i++) { + this._ops[i].reset(); + } + }; + $Or.prototype.next = function (item, key, owner) { + var done = false; + var success = false; + for (var i = 0, length_3 = this._ops.length; i < length_3; i++) { + var op = this._ops[i]; + op.next(item, key, owner); + if (op.keep) { + done = true; + success = op.keep; + break; + } + } + this.keep = success; + this.done = done; + }; + return $Or; +}(BaseOperation)); +var $Nor = /** @class */ (function (_super) { + __extends($Nor, _super); + function $Nor() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = false; + return _this; + } + $Nor.prototype.next = function (item, key, owner) { + _super.prototype.next.call(this, item, key, owner); + this.keep = !this.keep; + }; + return $Nor; +}($Or)); +var $In = /** @class */ (function (_super) { + __extends($In, _super); + function $In() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + $In.prototype.init = function () { + var _this = this; + var params = Array.isArray(this.params) ? this.params : [this.params]; + this._testers = params.map(function (value) { + if (containsOperation(value, _this.options)) { + throw new Error("cannot nest $ under ".concat(_this.name.toLowerCase())); + } + return createTester(value, _this.options.compare); + }); + }; + $In.prototype.next = function (item, key, owner) { + var done = false; + var success = false; + for (var i = 0, length_4 = this._testers.length; i < length_4; i++) { + var test = this._testers[i]; + if (test(item)) { + done = true; + success = true; + break; + } + } + this.keep = success; + this.done = done; + }; + return $In; +}(BaseOperation)); +var $Nin = /** @class */ (function (_super) { + __extends($Nin, _super); + function $Nin(params, ownerQuery, options, name) { + var _this = _super.call(this, params, ownerQuery, options, name) || this; + _this.propop = true; + _this._in = new $In(params, ownerQuery, options, name); + return _this; + } + $Nin.prototype.next = function (item, key, owner, root) { + this._in.next(item, key, owner); + if (isArray(owner) && !root) { + if (this._in.keep) { + this.keep = false; + this.done = true; + } + else if (key == owner.length - 1) { + this.keep = true; + this.done = true; + } + } + else { + this.keep = !this._in.keep; + this.done = true; + } + }; + $Nin.prototype.reset = function () { + _super.prototype.reset.call(this); + this._in.reset(); + }; + return $Nin; +}(BaseOperation)); +var $Exists = /** @class */ (function (_super) { + __extends($Exists, _super); + function $Exists() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + $Exists.prototype.next = function (item, key, owner, root, leaf) { + if (!leaf) { + this.done = true; + this.keep = !this.params; + } + else if (owner.hasOwnProperty(key) === this.params) { + this.done = true; + this.keep = true; + } + }; + return $Exists; +}(BaseOperation)); +var $And = /** @class */ (function (_super) { + __extends($And, _super); + function $And(params, owneryQuery, options, name) { + var _this = _super.call(this, params, owneryQuery, options, params.map(function (query) { return createQueryOperation(query, owneryQuery, options); }), name) || this; + _this.propop = false; + assertGroupNotEmpty(params); + return _this; + } + $And.prototype.next = function (item, key, owner, root) { + this.childrenNext(item, key, owner, root); + }; + return $And; +}(NamedGroupOperation)); +var $All = /** @class */ (function (_super) { + __extends($All, _super); + function $All(params, owneryQuery, options, name) { + var _this = _super.call(this, params, owneryQuery, options, params.map(function (query) { return createQueryOperation(query, owneryQuery, options); }), name) || this; + _this.propop = true; + return _this; + } + $All.prototype.next = function (item, key, owner, root) { + this.childrenNext(item, key, owner, root); + }; + return $All; +}(NamedGroupOperation)); +var $eq = function (params, owneryQuery, options) { + return new EqualsOperation(params, owneryQuery, options); +}; +var $ne = function (params, owneryQuery, options, name) { return new $Ne(params, owneryQuery, options, name); }; +var $or = function (params, owneryQuery, options, name) { return new $Or(params, owneryQuery, options, name); }; +var $nor = function (params, owneryQuery, options, name) { return new $Nor(params, owneryQuery, options, name); }; +var $elemMatch = function (params, owneryQuery, options, name) { return new $ElemMatch(params, owneryQuery, options, name); }; +var $nin = function (params, owneryQuery, options, name) { return new $Nin(params, owneryQuery, options, name); }; +var $in = function (params, owneryQuery, options, name) { + return new $In(params, owneryQuery, options, name); +}; +var $lt = numericalOperation(function (params) { return function (b) { + return b != null && b < params; +}; }); +var $lte = numericalOperation(function (params) { return function (b) { + return b === params || b <= params; +}; }); +var $gt = numericalOperation(function (params) { return function (b) { + return b != null && b > params; +}; }); +var $gte = numericalOperation(function (params) { return function (b) { + return b === params || b >= params; +}; }); +var $mod = function (_a, owneryQuery, options) { + var mod = _a[0], equalsValue = _a[1]; + return new EqualsOperation(function (b) { return comparable(b) % mod === equalsValue; }, owneryQuery, options); +}; +var $exists = function (params, owneryQuery, options, name) { return new $Exists(params, owneryQuery, options, name); }; +var $regex = function (pattern, owneryQuery, options) { + return new EqualsOperation(new RegExp(pattern, owneryQuery.$options), owneryQuery, options); +}; +var $not = function (params, owneryQuery, options, name) { return new $Not(params, owneryQuery, options, name); }; +var typeAliases = { + number: function (v) { return typeof v === "number"; }, + string: function (v) { return typeof v === "string"; }, + bool: function (v) { return typeof v === "boolean"; }, + array: function (v) { return Array.isArray(v); }, + null: function (v) { return v === null; }, + timestamp: function (v) { return v instanceof Date; }, +}; +var $type = function (clazz, owneryQuery, options) { + return new EqualsOperation(function (b) { + if (typeof clazz === "string") { + if (!typeAliases[clazz]) { + throw new Error("Type alias does not exist"); + } + return typeAliases[clazz](b); + } + return b != null ? b instanceof clazz || b.constructor === clazz : false; + }, owneryQuery, options); +}; +var $and = function (params, ownerQuery, options, name) { return new $And(params, ownerQuery, options, name); }; +var $all = function (params, ownerQuery, options, name) { return new $All(params, ownerQuery, options, name); }; +var $size = function (params, ownerQuery, options) { return new $Size(params, ownerQuery, options, "$size"); }; +var $options = function () { return null; }; +var $where = function (params, ownerQuery, options) { + var test; + if (isFunction(params)) { + test = params; + } + else if (!process.env.CSP_ENABLED) { + test = new Function("obj", "return " + params); + } + else { + throw new Error("In CSP mode, sift does not support strings in \"$where\" condition"); + } + return new EqualsOperation(function (b) { return test.bind(b)(b); }, ownerQuery, options); +}; + +var defaultOperations = /*#__PURE__*/Object.freeze({ + __proto__: null, + $Size: $Size, + $all: $all, + $and: $and, + $elemMatch: $elemMatch, + $eq: $eq, + $exists: $exists, + $gt: $gt, + $gte: $gte, + $in: $in, + $lt: $lt, + $lte: $lte, + $mod: $mod, + $ne: $ne, + $nin: $nin, + $nor: $nor, + $not: $not, + $options: $options, + $or: $or, + $regex: $regex, + $size: $size, + $type: $type, + $where: $where +}); + +var createDefaultQueryOperation = function (query, ownerQuery, _a) { + var _b = _a === void 0 ? {} : _a, compare = _b.compare, operations = _b.operations; + return createQueryOperation(query, ownerQuery, { + compare: compare, + operations: Object.assign({}, defaultOperations, operations || {}), + }); +}; +var createDefaultQueryTester = function (query, options) { + if (options === void 0) { options = {}; } + var op = createDefaultQueryOperation(query, null, options); + return createOperationTester(op); +}; + +export { $Size, $all, $and, $elemMatch, $eq, $exists, $gt, $gte, $in, $lt, $lte, $mod, $ne, $nin, $nor, $not, $options, $or, $regex, $size, $type, $where, EqualsOperation, createDefaultQueryOperation, createEqualsOperation, createOperationTester, createQueryOperation, createQueryTester, createDefaultQueryTester as default }; +//# sourceMappingURL=index.js.map diff --git a/gateway/node_modules/sift/es5m/index.js.map b/gateway/node_modules/sift/es5m/index.js.map new file mode 100644 index 0000000000000000000000000000000000000000..8523755e8836b336cb4180054ea4c68d6789691c --- /dev/null +++ b/gateway/node_modules/sift/es5m/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sources":["../node_modules/tslib/tslib.es6.js","../src/utils.ts","../src/core.ts","../src/operations.ts","../src/index.ts"],"sourcesContent":["/******************************************************************************\r\nCopyright (c) Microsoft Corporation.\r\n\r\nPermission to use, copy, modify, and/or distribute this software for any\r\npurpose with or without fee is hereby granted.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\r\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\r\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\r\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\r\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\r\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\r\nPERFORMANCE OF THIS SOFTWARE.\r\n***************************************************************************** */\r\n/* global Reflect, Promise, SuppressedError, Symbol */\r\n\r\nvar extendStatics = function(d, b) {\r\n extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\r\n return extendStatics(d, b);\r\n};\r\n\r\nexport function __extends(d, b) {\r\n if (typeof b !== \"function\" && b !== null)\r\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = function() {\r\n __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n }\r\n return __assign.apply(this, arguments);\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\r\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\r\n t[p[i]] = s[p[i]];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __esDecorate(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {\r\n function accept(f) { if (f !== void 0 && typeof f !== \"function\") throw new TypeError(\"Function expected\"); return f; }\r\n var kind = contextIn.kind, key = kind === \"getter\" ? \"get\" : kind === \"setter\" ? \"set\" : \"value\";\r\n var target = !descriptorIn && ctor ? contextIn[\"static\"] ? ctor : ctor.prototype : null;\r\n var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});\r\n var _, done = false;\r\n for (var i = decorators.length - 1; i >= 0; i--) {\r\n var context = {};\r\n for (var p in contextIn) context[p] = p === \"access\" ? {} : contextIn[p];\r\n for (var p in contextIn.access) context.access[p] = contextIn.access[p];\r\n context.addInitializer = function (f) { if (done) throw new TypeError(\"Cannot add initializers after decoration has completed\"); extraInitializers.push(accept(f || null)); };\r\n var result = (0, decorators[i])(kind === \"accessor\" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);\r\n if (kind === \"accessor\") {\r\n if (result === void 0) continue;\r\n if (result === null || typeof result !== \"object\") throw new TypeError(\"Object expected\");\r\n if (_ = accept(result.get)) descriptor.get = _;\r\n if (_ = accept(result.set)) descriptor.set = _;\r\n if (_ = accept(result.init)) initializers.unshift(_);\r\n }\r\n else if (_ = accept(result)) {\r\n if (kind === \"field\") initializers.unshift(_);\r\n else descriptor[key] = _;\r\n }\r\n }\r\n if (target) Object.defineProperty(target, contextIn.name, descriptor);\r\n done = true;\r\n};\r\n\r\nexport function __runInitializers(thisArg, initializers, value) {\r\n var useValue = arguments.length > 2;\r\n for (var i = 0; i < initializers.length; i++) {\r\n value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);\r\n }\r\n return useValue ? value : void 0;\r\n};\r\n\r\nexport function __propKey(x) {\r\n return typeof x === \"symbol\" ? x : \"\".concat(x);\r\n};\r\n\r\nexport function __setFunctionName(f, name, prefix) {\r\n if (typeof name === \"symbol\") name = name.description ? \"[\".concat(name.description, \"]\") : \"\";\r\n return Object.defineProperty(f, \"name\", { configurable: true, value: prefix ? \"\".concat(prefix, \" \", name) : name });\r\n};\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\r\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [op[0] & 2, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n var desc = Object.getOwnPropertyDescriptor(m, k);\r\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\r\n desc = { enumerable: true, get: function() { return m[k]; } };\r\n }\r\n Object.defineProperty(o, k2, desc);\r\n}) : (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n o[k2] = m[k];\r\n});\r\n\r\nexport function __exportStar(m, o) {\r\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\r\n}\r\n\r\nexport function __values(o) {\r\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\r\n if (m) return m.call(o);\r\n if (o && typeof o.length === \"number\") return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spreadArrays() {\r\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\r\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\r\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\r\n r[k] = a[j];\r\n return r;\r\n}\r\n\r\nexport function __spreadArray(to, from, pack) {\r\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\r\n if (ar || !(i in from)) {\r\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\r\n ar[i] = from[i];\r\n }\r\n }\r\n return to.concat(ar || Array.prototype.slice.call(from));\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator], i;\r\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\r\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\r\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nvar __setModuleDefault = Object.create ? (function(o, v) {\r\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\r\n}) : function(o, v) {\r\n o[\"default\"] = v;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\r\n __setModuleDefault(result, mod);\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n\r\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\r\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\r\n}\r\n\r\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\r\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\r\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\r\n}\r\n\r\nexport function __classPrivateFieldIn(state, receiver) {\r\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\r\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\r\n}\r\n\r\nexport function __addDisposableResource(env, value, async) {\r\n if (value !== null && value !== void 0) {\r\n if (typeof value !== \"object\" && typeof value !== \"function\") throw new TypeError(\"Object expected.\");\r\n var dispose;\r\n if (async) {\r\n if (!Symbol.asyncDispose) throw new TypeError(\"Symbol.asyncDispose is not defined.\");\r\n dispose = value[Symbol.asyncDispose];\r\n }\r\n if (dispose === void 0) {\r\n if (!Symbol.dispose) throw new TypeError(\"Symbol.dispose is not defined.\");\r\n dispose = value[Symbol.dispose];\r\n }\r\n if (typeof dispose !== \"function\") throw new TypeError(\"Object not disposable.\");\r\n env.stack.push({ value: value, dispose: dispose, async: async });\r\n }\r\n else if (async) {\r\n env.stack.push({ async: true });\r\n }\r\n return value;\r\n}\r\n\r\nvar _SuppressedError = typeof SuppressedError === \"function\" ? SuppressedError : function (error, suppressed, message) {\r\n var e = new Error(message);\r\n return e.name = \"SuppressedError\", e.error = error, e.suppressed = suppressed, e;\r\n};\r\n\r\nexport function __disposeResources(env) {\r\n function fail(e) {\r\n env.error = env.hasError ? new _SuppressedError(e, env.error, \"An error was suppressed during disposal.\") : e;\r\n env.hasError = true;\r\n }\r\n function next() {\r\n while (env.stack.length) {\r\n var rec = env.stack.pop();\r\n try {\r\n var result = rec.dispose && rec.dispose.call(rec.value);\r\n if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });\r\n }\r\n catch (e) {\r\n fail(e);\r\n }\r\n }\r\n if (env.hasError) throw env.error;\r\n }\r\n return next();\r\n}\r\n\r\nexport default {\r\n __extends: __extends,\r\n __assign: __assign,\r\n __rest: __rest,\r\n __decorate: __decorate,\r\n __param: __param,\r\n __metadata: __metadata,\r\n __awaiter: __awaiter,\r\n __generator: __generator,\r\n __createBinding: __createBinding,\r\n __exportStar: __exportStar,\r\n __values: __values,\r\n __read: __read,\r\n __spread: __spread,\r\n __spreadArrays: __spreadArrays,\r\n __spreadArray: __spreadArray,\r\n __await: __await,\r\n __asyncGenerator: __asyncGenerator,\r\n __asyncDelegator: __asyncDelegator,\r\n __asyncValues: __asyncValues,\r\n __makeTemplateObject: __makeTemplateObject,\r\n __importStar: __importStar,\r\n __importDefault: __importDefault,\r\n __classPrivateFieldGet: __classPrivateFieldGet,\r\n __classPrivateFieldSet: __classPrivateFieldSet,\r\n __classPrivateFieldIn: __classPrivateFieldIn,\r\n __addDisposableResource: __addDisposableResource,\r\n __disposeResources: __disposeResources,\r\n};\r\n",null,null,null,null],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAI,aAAa,GAAG,SAAS,CAAC,EAAE,CAAC,EAAE;AACnC,IAAI,aAAa,GAAG,MAAM,CAAC,cAAc;AACzC,SAAS,EAAE,SAAS,EAAE,EAAE,EAAE,YAAY,KAAK,IAAI,UAAU,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC,EAAE,CAAC;AACpF,QAAQ,UAAU,CAAC,EAAE,CAAC,EAAE,EAAE,KAAK,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AAC1G,IAAI,OAAO,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAC/B,CAAC,CAAC;AACF;AACO,SAAS,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE;AAChC,IAAI,IAAI,OAAO,CAAC,KAAK,UAAU,IAAI,CAAC,KAAK,IAAI;AAC7C,QAAQ,MAAM,IAAI,SAAS,CAAC,sBAAsB,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,+BAA+B,CAAC,CAAC;AAClG,IAAI,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACxB,IAAI,SAAS,EAAE,GAAG,EAAE,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC,EAAE;AAC3C,IAAI,CAAC,CAAC,SAAS,GAAG,CAAC,KAAK,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,SAAS,GAAG,CAAC,CAAC,SAAS,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;AACzF,CAAC;AA6RD;AACuB,OAAO,eAAe,KAAK,UAAU,GAAG,eAAe,GAAG,UAAU,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE;AACvH,IAAI,IAAI,CAAC,GAAG,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;AAC/B,IAAI,OAAO,CAAC,CAAC,IAAI,GAAG,iBAAiB,EAAE,CAAC,CAAC,KAAK,GAAG,KAAK,EAAE,CAAC,CAAC,UAAU,GAAG,UAAU,EAAE,CAAC,CAAC;AACrF;;AC5TO,IAAM,WAAW,GAAG,UAAQ,IAAI,EAAA;AACrC,IAAA,IAAM,UAAU,GAAG,UAAU,GAAG,IAAI,GAAG,GAAG,CAAC;AAC3C,IAAA,OAAO,UAAU,KAAK,EAAA;AACpB,QAAA,OAAO,YAAY,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC;AAC5C,KAAC,CAAC;AACJ,CAAC,CAAC;AAEF,IAAM,YAAY,GAAG,UAAC,KAAK,EAAK,EAAA,OAAA,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA,EAAA,CAAC;AAE/D,IAAM,UAAU,GAAG,UAAC,KAAU,EAAA;AACnC,IAAA,IAAI,KAAK,YAAY,IAAI,EAAE;AACzB,QAAA,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;KACxB;AAAM,SAAA,IAAI,OAAO,CAAC,KAAK,CAAC,EAAE;AACzB,QAAA,OAAO,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;KAC9B;SAAM,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,MAAM,KAAK,UAAU,EAAE;AACtD,QAAA,OAAO,KAAK,CAAC,MAAM,EAAE,CAAC;KACvB;AAED,IAAA,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEK,IAAM,qBAAqB,GAAG,UAAC,KAAU,EAAA;IAC9C,OAAA,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,CAAA;AAA5B,CAA4B,CAAC;AAExB,IAAM,OAAO,GAAG,WAAW,CAAa,OAAO,CAAC,CAAC;AACjD,IAAM,QAAQ,GAAG,WAAW,CAAS,QAAQ,CAAC,CAAC;AAC/C,IAAM,UAAU,GAAG,WAAW,CAAW,UAAU,CAAC,CAAC;AACrD,IAAM,UAAU,GAAG,UAAC,IAAS,EAAE,GAAQ,EAAA;AAC5C,IAAA,OAAO,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAC5D,CAAC,CAAC;AACK,IAAM,eAAe,GAAG,UAAC,KAAK,EAAA;AACnC,IAAA,QACE,KAAK;AACL,SAAC,KAAK,CAAC,WAAW,KAAK,MAAM;YAC3B,KAAK,CAAC,WAAW,KAAK,KAAK;AAC3B,YAAA,KAAK,CAAC,WAAW,CAAC,QAAQ,EAAE,KAAK,qCAAqC;AACtE,YAAA,KAAK,CAAC,WAAW,CAAC,QAAQ,EAAE,KAAK,oCAAoC,CAAC;AACxE,QAAA,CAAC,KAAK,CAAC,MAAM,EACb;AACJ,CAAC,CAAC;AAEK,IAAM,MAAM,GAAG,UAAC,CAAC,EAAE,CAAC,EAAA;IACzB,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,EAAE;AACvB,QAAA,OAAO,IAAI,CAAC;KACb;AACD,IAAA,IAAI,CAAC,KAAK,CAAC,EAAE;AACX,QAAA,OAAO,IAAI,CAAC;KACb;IAED,IAAI,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;AAC3E,QAAA,OAAO,KAAK,CAAC;KACd;AAED,IAAA,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE;QACd,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,EAAE;AACzB,YAAA,OAAO,KAAK,CAAC;SACd;AACD,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,CAAC,CAAN,MAAA,EAAQ,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;AAC/C,YAAA,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAAE,gBAAA,OAAO,KAAK,CAAC;SACvC;AACD,QAAA,OAAO,IAAI,CAAC;KACb;AAAM,SAAA,IAAI,QAAQ,CAAC,CAAC,CAAC,EAAE;AACtB,QAAA,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE;AACnD,YAAA,OAAO,KAAK,CAAC;SACd;AACD,QAAA,KAAK,IAAM,GAAG,IAAI,CAAC,EAAE;AACnB,YAAA,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC;AAAE,gBAAA,OAAO,KAAK,CAAC;SAC3C;AACD,QAAA,OAAO,IAAI,CAAC;KACb;AACD,IAAA,OAAO,KAAK,CAAC;AACf,CAAC;;ACYD;;;AAGG;AAEH,IAAM,iBAAiB,GAAG,UACxB,IAAS,EACT,OAAc,EACd,IAAY,EACZ,KAAa,EACb,GAAQ,EACR,KAAU,EAAA;AAEV,IAAA,IAAM,UAAU,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;;;IAIlC,IACE,OAAO,CAAC,IAAI,CAAC;AACb,QAAA,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;AACzB,QAAA,CAAC,UAAU,CAAC,IAAI,EAAE,UAAU,CAAC,EAC7B;AACA,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAT,MAAA,EAAW,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;;;AAGlD,YAAA,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE;AAC9D,gBAAA,OAAO,KAAK,CAAC;aACd;SACF;KACF;IAED,IAAI,KAAK,KAAK,OAAO,CAAC,MAAM,IAAI,IAAI,IAAI,IAAI,EAAE;AAC5C,QAAA,OAAO,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,KAAK,CAAC,EAAE,KAAK,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;KACtE;AAED,IAAA,OAAO,iBAAiB,CACtB,IAAI,CAAC,UAAU,CAAC,EAChB,OAAO,EACP,IAAI,EACJ,KAAK,GAAG,CAAC,EACT,UAAU,EACV,IAAI,CACL,CAAC;AACJ,CAAC,CAAC;AAEF,IAAA,aAAA,kBAAA,YAAA;AAME,IAAA,SAAA,aAAA,CACW,MAAe,EACf,WAAgB,EAChB,OAAgB,EAChB,IAAa,EAAA;QAHb,IAAM,CAAA,MAAA,GAAN,MAAM,CAAS;QACf,IAAW,CAAA,WAAA,GAAX,WAAW,CAAK;QAChB,IAAO,CAAA,OAAA,GAAP,OAAO,CAAS;QAChB,IAAI,CAAA,IAAA,GAAJ,IAAI,CAAS;QAEtB,IAAI,CAAC,IAAI,EAAE,CAAC;KACb;IACS,aAAI,CAAA,SAAA,CAAA,IAAA,GAAd,eAAmB,CAAA;AACnB,IAAA,aAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;AACE,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;AAClB,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;KACnB,CAAA;IAQH,OAAC,aAAA,CAAA;AAAD,CAAC,EAAA,CAAA,CAAA;AAED,IAAA,cAAA,kBAAA,UAAA,MAAA,EAAA;IAAsC,SAAkB,CAAA,cAAA,EAAA,MAAA,CAAA,CAAA;AAItD,IAAA,SAAA,cAAA,CACE,MAAW,EACX,WAAgB,EAChB,OAAgB,EACA,QAA0B,EAAA;QAE1C,IAAA,KAAA,GAAA,MAAK,YAAC,MAAM,EAAE,WAAW,EAAE,OAAO,CAAC,IAAC,IAAA,CAAA;QAFpB,KAAQ,CAAA,QAAA,GAAR,QAAQ,CAAkB;;KAG3C;AAED;AACG;AAEH,IAAA,cAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;AACE,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;AAClB,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;AAClB,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAC,QAAQ,CAAlB,MAAA,EAAoB,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;YAC3D,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;SAC1B;KACF,CAAA;AAID;AACG;IAEO,cAAY,CAAA,SAAA,CAAA,YAAA,GAAtB,UACE,IAAS,EACT,GAAQ,EACR,KAAU,EACV,IAAa,EACb,IAAc,EAAA;QAEd,IAAI,IAAI,GAAG,IAAI,CAAC;QAChB,IAAI,IAAI,GAAG,IAAI,CAAC;AAChB,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAC,QAAQ,CAAlB,MAAA,EAAoB,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;YAC3D,IAAM,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AACxC,YAAA,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE;AACxB,gBAAA,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;aACnD;AACD,YAAA,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE;gBACxB,IAAI,GAAG,KAAK,CAAC;aACd;AACD,YAAA,IAAI,cAAc,CAAC,IAAI,EAAE;AACvB,gBAAA,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE;oBACxB,MAAM;iBACP;aACF;iBAAM;gBACL,IAAI,GAAG,KAAK,CAAC;aACd;SACF;AACD,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;KAClB,CAAA;IACH,OAAC,cAAA,CAAA;AAAD,CAzDA,CAAsC,aAAa,CAyDlD,CAAA,CAAA;AAED,IAAA,mBAAA,kBAAA,UAAA,MAAA,EAAA;IACU,SAAc,CAAA,mBAAA,EAAA,MAAA,CAAA,CAAA;IAItB,SACE,mBAAA,CAAA,MAAW,EACX,WAAgB,EAChB,OAAgB,EAChB,QAA0B,EACjB,IAAY,EAAA;QAErB,IAAA,KAAA,GAAA,MAAK,CAAC,IAAA,CAAA,IAAA,EAAA,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,CAAC,IAAC,IAAA,CAAA;QAFrC,KAAI,CAAA,IAAA,GAAJ,IAAI,CAAQ;;KAGtB;IACH,OAAC,mBAAA,CAAA;AAAD,CAdA,CACU,cAAc,CAavB,CAAA,CAAA;AAED,IAAA,cAAA,kBAAA,UAAA,MAAA,EAAA;IAA2C,SAAc,CAAA,cAAA,EAAA,MAAA,CAAA,CAAA;AAAzD,IAAA,SAAA,cAAA,GAAA;;QACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;KAOxB;AANC;AACG;IAEH,cAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAW,EAAE,GAAQ,EAAE,MAAW,EAAE,IAAa,EAAA;QACpD,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;KAC5C,CAAA;IACH,OAAC,cAAA,CAAA;AAAD,CARA,CAA2C,cAAc,CAQxD,CAAA,CAAA;AAED,IAAA,eAAA,kBAAA,UAAA,MAAA,EAAA;IAAqC,SAAc,CAAA,eAAA,EAAA,MAAA,CAAA,CAAA;IAEjD,SACW,eAAA,CAAA,OAAc,EACvB,MAAW,EACX,WAAgB,EAChB,OAAgB,EAChB,QAA0B,EAAA;QAE1B,IAAA,KAAA,GAAA,MAAK,CAAC,IAAA,CAAA,IAAA,EAAA,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,CAAC,IAAC,IAAA,CAAA;QANrC,KAAO,CAAA,OAAA,GAAP,OAAO,CAAO;QAFhB,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;AAwBvB;AACG;QAEK,KAAgB,CAAA,gBAAA,GAAG,UACzB,KAAU,EACV,GAAQ,EACR,KAAU,EACV,IAAa,EACb,IAAa,EAAA;AAEb,YAAA,KAAI,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AACjD,YAAA,OAAO,CAAC,KAAI,CAAC,IAAI,CAAC;AACpB,SAAC,CAAC;;KA3BD;AACD;AACG;AAEH,IAAA,eAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,MAAW,EAAA;AACnC,QAAA,iBAAiB,CACf,IAAI,EACJ,IAAI,CAAC,OAAO,EACZ,IAAI,CAAC,gBAAgB,EACrB,CAAC,EACD,GAAG,EACH,MAAM,CACP,CAAC;KACH,CAAA;IAeH,OAAC,eAAA,CAAA;AAAD,CAtCA,CAAqC,cAAc,CAsClD,CAAA,CAAA;AAEM,IAAM,YAAY,GAAG,UAAC,CAAC,EAAE,OAAmB,EAAA;AACjD,IAAA,IAAI,CAAC,YAAY,QAAQ,EAAE;AACzB,QAAA,OAAO,CAAC,CAAC;KACV;AACD,IAAA,IAAI,CAAC,YAAY,MAAM,EAAE;AACvB,QAAA,OAAO,UAAC,CAAC,EAAA;AACP,YAAA,IAAM,MAAM,GAAG,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClD,YAAA,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC;AAChB,YAAA,OAAO,MAAM,CAAC;AAChB,SAAC,CAAC;KACH;AACD,IAAA,IAAM,WAAW,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;AAClC,IAAA,OAAO,UAAC,CAAC,EAAK,EAAA,OAAA,OAAO,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAA,EAAA,CAAC;AACpD,CAAC,CAAC;AAEF,IAAA,eAAA,kBAAA,UAAA,MAAA,EAAA;IAA6C,SAAqB,CAAA,eAAA,EAAA,MAAA,CAAA,CAAA;AAAlE,IAAA,SAAA,eAAA,GAAA;;QACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;KAaxB;AAXC,IAAA,eAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,YAAA;AACE,QAAA,IAAI,CAAC,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;KAC9D,CAAA;AACD,IAAA,eAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,UAAK,IAAI,EAAE,GAAQ,EAAE,MAAW,EAAA;AAC9B,QAAA,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE;YACxD,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE;AACjC,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;aAClB;SACF;KACF,CAAA;IACH,OAAC,eAAA,CAAA;AAAD,CAdA,CAA6C,aAAa,CAczD,EAAA;IAEY,qBAAqB,GAAG,UACnC,MAAW,EACX,WAAgB,EAChB,OAAgB,EAAA,EACb,OAAA,IAAI,eAAe,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,CAAC,CAAjD,GAAkD;AAEhD,IAAM,yBAAyB,GACpC,UAAC,wBAA+C,EAAA;AAChD,IAAA,OAAA,UAAC,MAAW,EAAE,WAAgB,EAAE,OAAgB,EAAE,IAAY,EAAA;QAC5D,OAAO,wBAAwB,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;KACrE,CAAA;AAFD,CAEC,CAAC;AAEG,IAAM,kBAAkB,GAAG,UAAC,YAAoC,EAAA;IACrE,OAAA,yBAAyB,CACvB,UAAC,MAAW,EAAE,WAAuB,EAAE,OAAgB,EAAE,IAAY,EAAA;AACnE,QAAA,IAAM,YAAY,GAAG,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC;AAC/C,QAAA,IAAM,IAAI,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;AAClC,QAAA,OAAO,IAAI,eAAe,CACxB,UAAC,CAAC,EAAA;AACA,YAAA,IAAM,WAAW,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAAC;AAC7C,YAAA,QACE,OAAO,UAAU,CAAC,WAAW,CAAC,KAAK,YAAY,IAAI,IAAI,CAAC,WAAW,CAAC,EACpE;AACJ,SAAC,EACD,WAAW,EACX,OAAO,EACP,IAAI,CACL,CAAC;AACJ,KAAC,CACF,CAAA;AAhBD,CAgBC,CAAC;AASJ,IAAM,oBAAoB,GAAG,UAC3B,IAAY,EACZ,MAAW,EACX,WAAgB,EAChB,OAAgB,EAAA;IAEhB,IAAM,gBAAgB,GAAG,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAClD,IAAI,CAAC,gBAAgB,EAAE;QACrB,yBAAyB,CAAC,IAAI,CAAC,CAAC;KACjC;IACD,OAAO,gBAAgB,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AAC9D,CAAC,CAAC;AAEF,IAAM,yBAAyB,GAAG,UAAC,IAAY,EAAA;AAC7C,IAAA,MAAM,IAAI,KAAK,CAAC,iCAA0B,IAAI,CAAE,CAAC,CAAC;AACpD,CAAC,CAAC;AAEK,IAAM,iBAAiB,GAAG,UAAC,KAAU,EAAE,OAAgB,EAAA;AAC5D,IAAA,KAAK,IAAM,GAAG,IAAI,KAAK,EAAE;AACvB,QAAA,IAAI,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG;AACjE,YAAA,OAAO,IAAI,CAAC;KACf;AACD,IAAA,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AACF,IAAM,qBAAqB,GAAG,UAC5B,OAAc,EACd,WAAgB,EAChB,SAAiB,EACjB,WAAgB,EAChB,OAAgB,EAAA;AAEhB,IAAA,IAAI,iBAAiB,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE;AACrC,QAAA,IAAA,EAAqC,GAAA,qBAAqB,CAC9D,WAAW,EACX,SAAS,EACT,OAAO,CACR,EAJM,cAAc,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,gBAAgB,QAItC,CAAC;AACF,QAAA,IAAI,gBAAgB,CAAC,MAAM,EAAE;AAC3B,YAAA,MAAM,IAAI,KAAK,CACb,kEAAkE,CACnE,CAAC;SACH;AACD,QAAA,OAAO,IAAI,eAAe,CACxB,OAAO,EACP,WAAW,EACX,WAAW,EACX,OAAO,EACP,cAAc,CACf,CAAC;KACH;IACD,OAAO,IAAI,eAAe,CAAC,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,OAAO,EAAE;AACrE,QAAA,IAAI,eAAe,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC;AACvD,KAAA,CAAC,CAAC;AACL,CAAC,CAAC;IAEW,oBAAoB,GAAG,UAClC,KAAqB,EACrB,WAAuB,EACvB,EAA8C,EAAA;AAD9C,IAAA,IAAA,WAAA,KAAA,KAAA,CAAA,EAAA,EAAA,WAAuB,GAAA,IAAA,CAAA,EAAA;AACvB,IAAA,IAAA,EAAA,GAAA,EAAA,KAAA,KAAA,CAAA,GAA4C,EAAE,GAAA,EAAA,EAA5C,OAAO,GAAA,EAAA,CAAA,OAAA,EAAE,UAAU,GAAA,EAAA,CAAA,UAAA,CAAA;AAErB,IAAA,IAAM,OAAO,GAAG;QACd,OAAO,EAAE,OAAO,IAAI,MAAM;QAC1B,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,UAAU,IAAI,EAAE,CAAC;KAChD,CAAC;AAEI,IAAA,IAAA,EAAqC,GAAA,qBAAqB,CAC9D,KAAK,EACL,IAAI,EACJ,OAAO,CACR,EAJM,cAAc,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,gBAAgB,QAItC,CAAC;IAEF,IAAM,GAAG,GAAG,EAAE,CAAC;AAEf,IAAA,IAAI,cAAc,CAAC,MAAM,EAAE;AACzB,QAAA,GAAG,CAAC,IAAI,CACN,IAAI,eAAe,CAAC,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,CAAC,CACrE,CAAC;KACH;AAED,IAAA,GAAG,CAAC,IAAI,CAAA,KAAA,CAAR,GAAG,EAAS,gBAAgB,CAAE,CAAA;AAE9B,IAAA,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE;AACpB,QAAA,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC;KACf;IACD,OAAO,IAAI,cAAc,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;AAC9D,EAAE;AAEF,IAAM,qBAAqB,GAAG,UAC5B,KAAU,EACV,SAAiB,EACjB,OAAgB,EAAA;IAEhB,IAAM,cAAc,GAAG,EAAE,CAAC;IAC1B,IAAM,gBAAgB,GAAG,EAAE,CAAC;AAC5B,IAAA,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,EAAE;AAC3B,QAAA,cAAc,CAAC,IAAI,CAAC,IAAI,eAAe,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;AAChE,QAAA,OAAO,CAAC,cAAc,EAAE,gBAAgB,CAAC,CAAC;KAC3C;AACD,IAAA,KAAK,IAAM,GAAG,IAAI,KAAK,EAAE;QACvB,IAAI,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE;AAC1C,YAAA,IAAM,EAAE,GAAG,oBAAoB,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;YAEjE,IAAI,EAAE,EAAE;AACN,gBAAA,IAAI,CAAC,EAAE,CAAC,MAAM,IAAI,SAAS,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE;AAC7D,oBAAA,MAAM,IAAI,KAAK,CACb,2BAAoB,GAAG,EAAA,sCAAA,CAAsC,CAC9D,CAAC;iBACH;aACF;;AAGD,YAAA,IAAI,EAAE,IAAI,IAAI,EAAE;AACd,gBAAA,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;aACzB;SACF;aAAM,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;YAChC,yBAAyB,CAAC,GAAG,CAAC,CAAC;SAChC;aAAM;YACL,gBAAgB,CAAC,IAAI,CACnB,qBAAqB,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,CACvE,CAAC;SACH;KACF;AAED,IAAA,OAAO,CAAC,cAAc,EAAE,gBAAgB,CAAC,CAAC;AAC5C,CAAC,CAAC;AAEK,IAAM,qBAAqB,GAChC,UAAQ,SAA2B,EAAA;AACnC,IAAA,OAAA,UAAC,IAAW,EAAE,GAAS,EAAE,KAAW,EAAA;QAClC,SAAS,CAAC,KAAK,EAAE,CAAC;QAClB,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;QACjC,OAAO,SAAS,CAAC,IAAI,CAAC;KACvB,CAAA;AAJD,EAIE;AAES,IAAA,iBAAiB,GAAG,UAC/B,KAAqB,EACrB,OAA8B,EAAA;AAA9B,IAAA,IAAA,OAAA,KAAA,KAAA,CAAA,EAAA,EAAA,OAA8B,GAAA,EAAA,CAAA,EAAA;IAE9B,OAAO,qBAAqB,CAC1B,oBAAoB,CAAiB,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAC3D,CAAC;AACJ;;AC7dA,IAAA,GAAA,kBAAA,UAAA,MAAA,EAAA;IAAkB,SAAkB,CAAA,GAAA,EAAA,MAAA,CAAA,CAAA;AAApC,IAAA,SAAA,GAAA,GAAA;;QACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;KAexB;AAbC,IAAA,GAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,YAAA;AACE,QAAA,IAAI,CAAC,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;KAC9D,CAAA;AACD,IAAA,GAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;QACE,MAAK,CAAA,SAAA,CAAC,KAAK,CAAA,IAAA,CAAA,IAAA,CAAE,CAAC;AACd,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;KAClB,CAAA;IACD,GAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAA;AACZ,QAAA,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;AACpB,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,YAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;SACnB;KACF,CAAA;IACH,OAAC,GAAA,CAAA;AAAD,CAhBA,CAAkB,aAAa,CAgB9B,CAAA,CAAA;AACD;AACA,IAAA,UAAA,kBAAA,UAAA,MAAA,EAAA;IAAyB,SAAyB,CAAA,UAAA,EAAA,MAAA,CAAA,CAAA;AAAlD,IAAA,SAAA,UAAA,GAAA;;QACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;KAiCxB;AA/BC,IAAA,UAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,YAAA;AACE,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,EAAE;AACnD,YAAA,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;SACnE;AACD,QAAA,IAAI,CAAC,eAAe,GAAG,oBAAoB,CACzC,IAAI,CAAC,MAAM,EACX,IAAI,CAAC,WAAW,EAChB,IAAI,CAAC,OAAO,CACb,CAAC;KACH,CAAA;AACD,IAAA,UAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;QACE,MAAK,CAAA,SAAA,CAAC,KAAK,CAAA,IAAA,CAAA,IAAA,CAAE,CAAC;AACd,QAAA,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;KAC9B,CAAA;IACD,UAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAA;AACZ,QAAA,IAAI,OAAO,CAAC,IAAI,CAAC,EAAE;AACjB,YAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAT,MAAA,EAAW,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;;;AAGlD,gBAAA,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;AAE7B,gBAAA,IAAM,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;AACtB,gBAAA,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;AACjD,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC;aACpD;AACD,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB;aAAM;AACL,YAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;AAClB,YAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;SACnB;KACF,CAAA;IACH,OAAC,UAAA,CAAA;AAAD,CAlCA,CAAyB,aAAa,CAkCrC,CAAA,CAAA;AAED,IAAA,IAAA,kBAAA,UAAA,MAAA,EAAA;IAAmB,SAAyB,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAA5C,IAAA,SAAA,IAAA,GAAA;;QACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;KAkBxB;AAhBC,IAAA,IAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,YAAA;AACE,QAAA,IAAI,CAAC,eAAe,GAAG,oBAAoB,CACzC,IAAI,CAAC,MAAM,EACX,IAAI,CAAC,WAAW,EAChB,IAAI,CAAC,OAAO,CACb,CAAC;KACH,CAAA;AACD,IAAA,IAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;QACE,MAAK,CAAA,SAAA,CAAC,KAAK,CAAA,IAAA,CAAA,IAAA,CAAE,CAAC;AACd,QAAA,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;KAC9B,CAAA;IACD,IAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAA;AACjD,QAAA,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;QAClD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC;KACxC,CAAA;IACH,OAAC,IAAA,CAAA;AAAD,CAnBA,CAAmB,aAAa,CAmB/B,CAAA,CAAA;AAED,IAAA,KAAA,kBAAA,UAAA,MAAA,EAAA;IAA2B,SAAkB,CAAA,KAAA,EAAA,MAAA,CAAA,CAAA;AAA7C,IAAA,SAAA,KAAA,GAAA;;QACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;KAYxB;IAXC,KAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,eAAS,CAAA;IACT,KAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAI,EAAA;AACP,QAAA,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,EAAE;AAChD,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB;;;;;KAKF,CAAA;IACH,OAAC,KAAA,CAAA;AAAD,CAbA,CAA2B,aAAa,CAavC,EAAA;AAED,IAAM,mBAAmB,GAAG,UAAC,MAAa,EAAA;AACxC,IAAA,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;AACvB,QAAA,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;KAC3D;AACH,CAAC,CAAC;AAEF,IAAA,GAAA,kBAAA,UAAA,MAAA,EAAA;IAAkB,SAAkB,CAAA,GAAA,EAAA,MAAA,CAAA,CAAA;AAApC,IAAA,SAAA,GAAA,GAAA;;QACW,KAAM,CAAA,MAAA,GAAG,KAAK,CAAC;;KA+BzB;AA7BC,IAAA,GAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,YAAA;QAAA,IAKC,KAAA,GAAA,IAAA,CAAA;AAJC,QAAA,mBAAmB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,UAAC,EAAE,EAAA;YAC7B,OAAA,oBAAoB,CAAC,EAAE,EAAE,IAAI,EAAE,KAAI,CAAC,OAAO,CAAC,CAAA;AAA5C,SAA4C,CAC7C,CAAC;KACH,CAAA;AACD,IAAA,GAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;AACE,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;AAClB,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;AAClB,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAC,IAAI,CAAd,MAAA,EAAgB,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;YACvD,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;SACtB;KACF,CAAA;AACD,IAAA,GAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAA;QAClC,IAAI,IAAI,GAAG,KAAK,CAAC;QACjB,IAAI,OAAO,GAAG,KAAK,CAAC;AACpB,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAC,IAAI,CAAd,MAAA,EAAgB,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;YACvD,IAAM,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACxB,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;AAC1B,YAAA,IAAI,EAAE,CAAC,IAAI,EAAE;gBACX,IAAI,GAAG,IAAI,CAAC;AACZ,gBAAA,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC;gBAClB,MAAM;aACP;SACF;AAED,QAAA,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;AACpB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;KAClB,CAAA;IACH,OAAC,GAAA,CAAA;AAAD,CAhCA,CAAkB,aAAa,CAgC9B,CAAA,CAAA;AAED,IAAA,IAAA,kBAAA,UAAA,MAAA,EAAA;IAAmB,SAAG,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAAtB,IAAA,SAAA,IAAA,GAAA;;QACW,KAAM,CAAA,MAAA,GAAG,KAAK,CAAC;;KAKzB;AAJC,IAAA,IAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAA;QAClC,MAAK,CAAA,SAAA,CAAC,IAAI,CAAC,IAAA,CAAA,IAAA,EAAA,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;AAC7B,QAAA,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;KACxB,CAAA;IACH,OAAC,IAAA,CAAA;AAAD,CANA,CAAmB,GAAG,CAMrB,CAAA,CAAA;AAED,IAAA,GAAA,kBAAA,UAAA,MAAA,EAAA;IAAkB,SAAkB,CAAA,GAAA,EAAA,MAAA,CAAA,CAAA;AAApC,IAAA,SAAA,GAAA,GAAA;;QACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;KA0BxB;AAxBC,IAAA,GAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,YAAA;QAAA,IAQC,KAAA,GAAA,IAAA,CAAA;QAPC,IAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACxE,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,UAAC,KAAK,EAAA;YAC/B,IAAI,iBAAiB,CAAC,KAAK,EAAE,KAAI,CAAC,OAAO,CAAC,EAAE;AAC1C,gBAAA,MAAM,IAAI,KAAK,CAAC,sBAAA,CAAA,MAAA,CAAuB,KAAI,CAAC,IAAI,CAAC,WAAW,EAAE,CAAE,CAAC,CAAC;aACnE;YACD,OAAO,YAAY,CAAC,KAAK,EAAE,KAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AACnD,SAAC,CAAC,CAAC;KACJ,CAAA;AACD,IAAA,GAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAA;QAClC,IAAI,IAAI,GAAG,KAAK,CAAC;QACjB,IAAI,OAAO,GAAG,KAAK,CAAC;AACpB,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAC,QAAQ,CAAlB,MAAA,EAAoB,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;YAC3D,IAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAC9B,YAAA,IAAI,IAAI,CAAC,IAAI,CAAC,EAAE;gBACd,IAAI,GAAG,IAAI,CAAC;gBACZ,OAAO,GAAG,IAAI,CAAC;gBACf,MAAM;aACP;SACF;AAED,QAAA,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;AACpB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;KAClB,CAAA;IACH,OAAC,GAAA,CAAA;AAAD,CA3BA,CAAkB,aAAa,CA2B9B,CAAA,CAAA;AAED,IAAA,IAAA,kBAAA,UAAA,MAAA,EAAA;IAAmB,SAAkB,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAGnC,IAAA,SAAA,IAAA,CAAY,MAAW,EAAE,UAAe,EAAE,OAAgB,EAAE,IAAY,EAAA;QACtE,IAAA,KAAA,GAAA,MAAK,CAAC,IAAA,CAAA,IAAA,EAAA,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,IAAC,IAAA,CAAA;QAHlC,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;AAIrB,QAAA,KAAI,CAAC,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;;KACvD;IACD,IAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAA;QACjD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;QAEhC,IAAI,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE;AAC3B,YAAA,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE;AACjB,gBAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;AAClB,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;aAClB;iBAAM,IAAI,GAAG,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;AAClC,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;aAClB;SACF;aAAM;YACL,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;AAC3B,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB;KACF,CAAA;AACD,IAAA,IAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;QACE,MAAK,CAAA,SAAA,CAAC,KAAK,CAAA,IAAA,CAAA,IAAA,CAAE,CAAC;AACd,QAAA,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;KAClB,CAAA;IACH,OAAC,IAAA,CAAA;AAAD,CA3BA,CAAmB,aAAa,CA2B/B,CAAA,CAAA;AAED,IAAA,OAAA,kBAAA,UAAA,MAAA,EAAA;IAAsB,SAAsB,CAAA,OAAA,EAAA,MAAA,CAAA,CAAA;AAA5C,IAAA,SAAA,OAAA,GAAA;;QACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;KAUxB;IATC,OAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAE,IAAc,EAAA;QACjE,IAAI,CAAC,IAAI,EAAE;AACT,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,YAAA,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC;SAC1B;aAAM,IAAI,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC,MAAM,EAAE;AACpD,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB;KACF,CAAA;IACH,OAAC,OAAA,CAAA;AAAD,CAXA,CAAsB,aAAa,CAWlC,CAAA,CAAA;AAED,IAAA,IAAA,kBAAA,UAAA,MAAA,EAAA;IAAmB,SAAmB,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAEpC,IAAA,SAAA,IAAA,CACE,MAAoB,EACpB,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA;AAEZ,QAAA,IAAA,KAAA,GAAA,MAAK,CAAA,IAAA,CAAA,IAAA,EACH,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,CAAC,GAAG,CAAC,UAAC,KAAK,EAAA,EAAK,OAAA,oBAAoB,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,CAAC,CAAA,EAAA,CAAC,EACxE,IAAI,CACL,IAAC,IAAA,CAAA;QAbK,KAAM,CAAA,MAAA,GAAG,KAAK,CAAC;QAetB,mBAAmB,CAAC,MAAM,CAAC,CAAC;;KAC7B;IACD,IAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAA;QACjD,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;KAC3C,CAAA;IACH,OAAC,IAAA,CAAA;AAAD,CArBA,CAAmB,mBAAmB,CAqBrC,CAAA,CAAA;AAED,IAAA,IAAA,kBAAA,UAAA,MAAA,EAAA;IAAmB,SAAmB,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAEpC,IAAA,SAAA,IAAA,CACE,MAAoB,EACpB,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA;AAEZ,QAAA,IAAA,KAAA,GAAA,MAAK,CAAA,IAAA,CAAA,IAAA,EACH,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,CAAC,GAAG,CAAC,UAAC,KAAK,EAAA,EAAK,OAAA,oBAAoB,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,CAAC,CAAA,EAAA,CAAC,EACxE,IAAI,CACL,IAAC,IAAA,CAAA;QAbK,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;KActB;IACD,IAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAA;QACjD,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;KAC3C,CAAA;IACH,OAAC,IAAA,CAAA;AAAD,CAnBA,CAAmB,mBAAmB,CAmBrC,CAAA,CAAA;IAEY,GAAG,GAAG,UAAC,MAAW,EAAE,WAAuB,EAAE,OAAgB,EAAA;IACxE,OAAA,IAAI,eAAe,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,CAAC,CAAA;AAAjD,EAAkD;AACvC,IAAA,GAAG,GAAG,UACjB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAA3C,GAA4C;AACpC,IAAA,GAAG,GAAG,UACjB,MAAoB,EACpB,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAA3C,GAA4C;AACpC,IAAA,IAAI,GAAG,UAClB,MAAoB,EACpB,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAA5C,GAA6C;AACrC,IAAA,UAAU,GAAG,UACxB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,UAAU,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAAlD,GAAmD;AAC3C,IAAA,IAAI,GAAG,UAClB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAA5C,GAA6C;AACrC,IAAA,GAAG,GAAG,UACjB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA;IAEZ,OAAO,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AACrD,EAAE;AAEW,IAAA,GAAG,GAAG,kBAAkB,CAAC,UAAC,MAAM,EAAA,EAAK,OAAA,UAAC,CAAC,EAAA;AAClD,IAAA,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC,GAAG,MAAM,CAAC;AACjC,CAAC,CAAA,EAAA,EAAE;AACU,IAAA,IAAI,GAAG,kBAAkB,CAAC,UAAC,MAAM,EAAA,EAAK,OAAA,UAAC,CAAC,EAAA;AACnD,IAAA,OAAO,CAAC,KAAK,MAAM,IAAI,CAAC,IAAI,MAAM,CAAC;AACrC,CAAC,CAAA,EAAA,EAAE;AACU,IAAA,GAAG,GAAG,kBAAkB,CAAC,UAAC,MAAM,EAAA,EAAK,OAAA,UAAC,CAAC,EAAA;AAClD,IAAA,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC,GAAG,MAAM,CAAC;AACjC,CAAC,CAAA,EAAA,EAAE;AACU,IAAA,IAAI,GAAG,kBAAkB,CAAC,UAAC,MAAM,EAAA,EAAK,OAAA,UAAC,CAAC,EAAA;AACnD,IAAA,OAAO,CAAC,KAAK,MAAM,IAAI,CAAC,IAAI,MAAM,CAAC;AACrC,CAAC,CAAA,EAAA,EAAE;IACU,IAAI,GAAG,UAClB,EAA4B,EAC5B,WAAuB,EACvB,OAAgB,EAAA;QAFf,GAAG,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,WAAW,GAAA,EAAA,CAAA,CAAA,CAAA,CAAA;IAIjB,OAAA,IAAI,eAAe,CACjB,UAAC,CAAC,EAAK,EAAA,OAAA,UAAU,CAAC,CAAC,CAAC,GAAG,GAAG,KAAK,WAAW,CAAA,EAAA,EAC1C,WAAW,EACX,OAAO,CACR,CAAA;AAJD,EAIE;AACS,IAAA,OAAO,GAAG,UACrB,MAAe,EACf,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAA/C,GAAgD;IACxC,MAAM,GAAG,UACpB,OAAe,EACf,WAAuB,EACvB,OAAgB,EAAA;AAEhB,IAAA,OAAA,IAAI,eAAe,CACjB,IAAI,MAAM,CAAC,OAAO,EAAE,WAAW,CAAC,QAAQ,CAAC,EACzC,WAAW,EACX,OAAO,CACR,CAAA;AAJD,EAIE;AACS,IAAA,IAAI,GAAG,UAClB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAA5C,GAA6C;AAElD,IAAM,WAAW,GAAG;IAClB,MAAM,EAAE,UAAC,CAAC,EAAK,EAAA,OAAA,OAAO,CAAC,KAAK,QAAQ,CAAA,EAAA;IACpC,MAAM,EAAE,UAAC,CAAC,EAAK,EAAA,OAAA,OAAO,CAAC,KAAK,QAAQ,CAAA,EAAA;IACpC,IAAI,EAAE,UAAC,CAAC,EAAK,EAAA,OAAA,OAAO,CAAC,KAAK,SAAS,CAAA,EAAA;AACnC,IAAA,KAAK,EAAE,UAAC,CAAC,EAAA,EAAK,OAAA,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA,EAAA;IAC9B,IAAI,EAAE,UAAC,CAAC,EAAA,EAAK,OAAA,CAAC,KAAK,IAAI,CAAA,EAAA;IACvB,SAAS,EAAE,UAAC,CAAC,EAAA,EAAK,OAAA,CAAC,YAAY,IAAI,CAAA,EAAA;CACpC,CAAC;IAEW,KAAK,GAAG,UACnB,KAAwB,EACxB,WAAuB,EACvB,OAAgB,EAAA;AAEhB,IAAA,OAAA,IAAI,eAAe,CACjB,UAAC,CAAC,EAAA;AACA,QAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AAC7B,YAAA,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE;AACvB,gBAAA,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;aAC9C;AAED,YAAA,OAAO,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;SAC9B;AAED,QAAA,OAAO,CAAC,IAAI,IAAI,GAAG,CAAC,YAAY,KAAK,IAAI,CAAC,CAAC,WAAW,KAAK,KAAK,GAAG,KAAK,CAAC;AAC3E,KAAC,EACD,WAAW,EACX,OAAO,CACR,CAAA;AAdD,EAcE;AACS,IAAA,IAAI,GAAG,UAClB,MAAoB,EACpB,UAAsB,EACtB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,CAA3C,GAA4C;AAEpC,IAAA,IAAI,GAAG,UAClB,MAAoB,EACpB,UAAsB,EACtB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,CAA3C,GAA4C;AACpC,IAAA,KAAK,GAAG,UACnB,MAAc,EACd,UAAsB,EACtB,OAAgB,EACb,EAAA,OAAA,IAAI,KAAK,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,CAA/C,GAAgD;IACxC,QAAQ,GAAG,cAAM,OAAA,IAAI,CAAJ,GAAK;IACtB,MAAM,GAAG,UACpB,MAAyB,EACzB,UAAsB,EACtB,OAAgB,EAAA;AAEhB,IAAA,IAAI,IAAI,CAAC;AAET,IAAA,IAAI,UAAU,CAAC,MAAM,CAAC,EAAE;QACtB,IAAI,GAAG,MAAM,CAAC;KACf;AAAM,SAAA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE;QACnC,IAAI,GAAG,IAAI,QAAQ,CAAC,KAAK,EAAE,SAAS,GAAG,MAAM,CAAC,CAAC;KAChD;SAAM;AACL,QAAA,MAAM,IAAI,KAAK,CACb,oEAAkE,CACnE,CAAC;KACH;IAED,OAAO,IAAI,eAAe,CAAC,UAAC,CAAC,EAAK,EAAA,OAAA,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAf,EAAe,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;AAC1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACpZA,IAAM,2BAA2B,GAAG,UAClC,KAAqB,EACrB,UAAe,EACf,EAA8C,EAAA;AAA9C,IAAA,IAAA,EAAA,GAAA,EAAA,KAAA,KAAA,CAAA,GAA4C,EAAE,GAAA,EAAA,EAA5C,OAAO,GAAA,EAAA,CAAA,OAAA,EAAE,UAAU,GAAA,EAAA,CAAA,UAAA,CAAA;AAErB,IAAA,OAAO,oBAAoB,CAAC,KAAK,EAAE,UAAU,EAAE;AAC7C,QAAA,OAAO,EAAA,OAAA;AACP,QAAA,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,iBAAiB,EAAE,UAAU,IAAI,EAAE,CAAC;AACnE,KAAA,CAAC,CAAC;AACL,EAAE;AAEF,IAAM,wBAAwB,GAAG,UAC/B,KAAqB,EACrB,OAA8B,EAAA;AAA9B,IAAA,IAAA,OAAA,KAAA,KAAA,CAAA,EAAA,EAAA,OAA8B,GAAA,EAAA,CAAA,EAAA;IAE9B,IAAM,EAAE,GAAG,2BAA2B,CAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAC7D,IAAA,OAAO,qBAAqB,CAAC,EAAE,CAAC,CAAC;AACnC;;;;","x_google_ignoreList":[0]} \ No newline at end of file diff --git a/gateway/node_modules/sift/index.d.ts b/gateway/node_modules/sift/index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..2b8257109a7b0cf91bc23612d63647ed615c70b1 --- /dev/null +++ b/gateway/node_modules/sift/index.d.ts @@ -0,0 +1,4 @@ +import sift from "./lib"; + +export default sift; +export * from "./lib"; diff --git a/gateway/node_modules/sift/index.js b/gateway/node_modules/sift/index.js new file mode 100644 index 0000000000000000000000000000000000000000..449294a4cc9729ed0f2ca9110aec888677ecdce2 --- /dev/null +++ b/gateway/node_modules/sift/index.js @@ -0,0 +1,4 @@ +const lib = require("./lib"); + +module.exports = lib.default; +Object.assign(module.exports, lib); diff --git a/gateway/node_modules/sift/package.json b/gateway/node_modules/sift/package.json new file mode 100644 index 0000000000000000000000000000000000000000..0784287ea78d163a5baaaa97a4edc70b26804a86 --- /dev/null +++ b/gateway/node_modules/sift/package.json @@ -0,0 +1,61 @@ +{ + "name": "sift", + "description": "MongoDB query filtering in JavaScript", + "version": "17.1.3", + "repository": "crcn/sift.js", + "sideEffects": false, + "author": { + "name": "Craig Condon", + "email": "craig.j.condon@gmail.com" + }, + "license": "MIT", + "engines": {}, + "typings": "./index.d.ts", + "husky": { + "hooks": { + "pre-commit": "pretty-quick --staged" + } + }, + "devDependencies": { + "@rollup/plugin-replace": "^2.3.2", + "@rollup/plugin-typescript": "8.2.1", + "@types/node": "^13.7.0", + "bson": "^6.6.0", + "eval": "^0.1.8", + "husky": "^9.0.11", + "mocha": "10.4.0", + "mongodb": "^3.6.6", + "prettier": "3.2.5", + "pretty-quick": "^4.0.0", + "rimraf": "^5.0.5", + "rollup": "^4.14.2", + "@rollup/plugin-terser": "^0.4.4", + "tslib": "2.6.2", + "typescript": "5.4.5" + }, + "main": "./index.js", + "module": "./es5m/index.js", + "es2015": "./es/index.js", + "scripts": { + "clean": "rimraf lib es5m es", + "prebuild": "npm run clean && npm run build:types", + "build": "rollup -c", + "build:types": "tsc -p tsconfig.json --emitDeclarationOnly --outDir lib", + "test": "npm run test:spec && npm run test:types", + "test:spec": "mocha ./test -R spec", + "test:types": "cd test && tsc types.ts --noEmit", + "prepublishOnly": "npm run build && npm run test" + }, + "files": [ + "es", + "es5m", + "lib", + "src", + "*.d.ts", + "*.js.map", + "index.js", + "sift.csp.min.js", + "sift.min.js", + "MIT-LICENSE.txt" + ] +} diff --git a/gateway/node_modules/sift/sift.csp.min.js b/gateway/node_modules/sift/sift.csp.min.js new file mode 100644 index 0000000000000000000000000000000000000000..673941c2cde200ff9c927245ecf9f75d2028ca33 --- /dev/null +++ b/gateway/node_modules/sift/sift.csp.min.js @@ -0,0 +1,778 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.sift = {})); +})(this, (function (exports) { 'use strict'; + + /****************************************************************************** + Copyright (c) Microsoft Corporation. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH + REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, + INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM + LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + PERFORMANCE OF THIS SOFTWARE. + ***************************************************************************** */ + /* global Reflect, Promise, SuppressedError, Symbol */ + + var extendStatics = function(d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + + function __extends(d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + } + + typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; + }; + + var typeChecker = function (type) { + var typeString = "[object " + type + "]"; + return function (value) { + return getClassName(value) === typeString; + }; + }; + var getClassName = function (value) { return Object.prototype.toString.call(value); }; + var comparable = function (value) { + if (value instanceof Date) { + return value.getTime(); + } + else if (isArray(value)) { + return value.map(comparable); + } + else if (value && typeof value.toJSON === "function") { + return value.toJSON(); + } + return value; + }; + var coercePotentiallyNull = function (value) { + return value == null ? null : value; + }; + var isArray = typeChecker("Array"); + var isObject = typeChecker("Object"); + var isFunction = typeChecker("Function"); + var isProperty = function (item, key) { + return item.hasOwnProperty(key) && !isFunction(item[key]); + }; + var isVanillaObject = function (value) { + return (value && + (value.constructor === Object || + value.constructor === Array || + value.constructor.toString() === "function Object() { [native code] }" || + value.constructor.toString() === "function Array() { [native code] }") && + !value.toJSON); + }; + var equals = function (a, b) { + if (a == null && a == b) { + return true; + } + if (a === b) { + return true; + } + if (Object.prototype.toString.call(a) !== Object.prototype.toString.call(b)) { + return false; + } + if (isArray(a)) { + if (a.length !== b.length) { + return false; + } + for (var i = 0, length_1 = a.length; i < length_1; i++) { + if (!equals(a[i], b[i])) + return false; + } + return true; + } + else if (isObject(a)) { + if (Object.keys(a).length !== Object.keys(b).length) { + return false; + } + for (var key in a) { + if (!equals(a[key], b[key])) + return false; + } + return true; + } + return false; + }; + + /** + * Walks through each value given the context - used for nested operations. E.g: + * { "person.address": { $eq: "blarg" }} + */ + var walkKeyPathValues = function (item, keyPath, next, depth, key, owner) { + var currentKey = keyPath[depth]; + // if array, then try matching. Might fall through for cases like: + // { $eq: [1, 2, 3] }, [ 1, 2, 3 ]. + if (isArray(item) && + isNaN(Number(currentKey)) && + !isProperty(item, currentKey)) { + for (var i = 0, length_1 = item.length; i < length_1; i++) { + // if FALSE is returned, then terminate walker. For operations, this simply + // means that the search critera was met. + if (!walkKeyPathValues(item[i], keyPath, next, depth, i, item)) { + return false; + } + } + } + if (depth === keyPath.length || item == null) { + return next(item, key, owner, depth === 0, depth === keyPath.length); + } + return walkKeyPathValues(item[currentKey], keyPath, next, depth + 1, currentKey, item); + }; + var BaseOperation = /** @class */ (function () { + function BaseOperation(params, owneryQuery, options, name) { + this.params = params; + this.owneryQuery = owneryQuery; + this.options = options; + this.name = name; + this.init(); + } + BaseOperation.prototype.init = function () { }; + BaseOperation.prototype.reset = function () { + this.done = false; + this.keep = false; + }; + return BaseOperation; + }()); + var GroupOperation = /** @class */ (function (_super) { + __extends(GroupOperation, _super); + function GroupOperation(params, owneryQuery, options, children) { + var _this = _super.call(this, params, owneryQuery, options) || this; + _this.children = children; + return _this; + } + /** + */ + GroupOperation.prototype.reset = function () { + this.keep = false; + this.done = false; + for (var i = 0, length_2 = this.children.length; i < length_2; i++) { + this.children[i].reset(); + } + }; + /** + */ + GroupOperation.prototype.childrenNext = function (item, key, owner, root, leaf) { + var done = true; + var keep = true; + for (var i = 0, length_3 = this.children.length; i < length_3; i++) { + var childOperation = this.children[i]; + if (!childOperation.done) { + childOperation.next(item, key, owner, root, leaf); + } + if (!childOperation.keep) { + keep = false; + } + if (childOperation.done) { + if (!childOperation.keep) { + break; + } + } + else { + done = false; + } + } + this.done = done; + this.keep = keep; + }; + return GroupOperation; + }(BaseOperation)); + var NamedGroupOperation = /** @class */ (function (_super) { + __extends(NamedGroupOperation, _super); + function NamedGroupOperation(params, owneryQuery, options, children, name) { + var _this = _super.call(this, params, owneryQuery, options, children) || this; + _this.name = name; + return _this; + } + return NamedGroupOperation; + }(GroupOperation)); + var QueryOperation = /** @class */ (function (_super) { + __extends(QueryOperation, _super); + function QueryOperation() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + /** + */ + QueryOperation.prototype.next = function (item, key, parent, root) { + this.childrenNext(item, key, parent, root); + }; + return QueryOperation; + }(GroupOperation)); + var NestedOperation = /** @class */ (function (_super) { + __extends(NestedOperation, _super); + function NestedOperation(keyPath, params, owneryQuery, options, children) { + var _this = _super.call(this, params, owneryQuery, options, children) || this; + _this.keyPath = keyPath; + _this.propop = true; + /** + */ + _this._nextNestedValue = function (value, key, owner, root, leaf) { + _this.childrenNext(value, key, owner, root, leaf); + return !_this.done; + }; + return _this; + } + /** + */ + NestedOperation.prototype.next = function (item, key, parent) { + walkKeyPathValues(item, this.keyPath, this._nextNestedValue, 0, key, parent); + }; + return NestedOperation; + }(GroupOperation)); + var createTester = function (a, compare) { + if (a instanceof Function) { + return a; + } + if (a instanceof RegExp) { + return function (b) { + var result = typeof b === "string" && a.test(b); + a.lastIndex = 0; + return result; + }; + } + var comparableA = comparable(a); + return function (b) { return compare(comparableA, comparable(b)); }; + }; + var EqualsOperation = /** @class */ (function (_super) { + __extends(EqualsOperation, _super); + function EqualsOperation() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + EqualsOperation.prototype.init = function () { + this._test = createTester(this.params, this.options.compare); + }; + EqualsOperation.prototype.next = function (item, key, parent) { + if (!Array.isArray(parent) || parent.hasOwnProperty(key)) { + if (this._test(item, key, parent)) { + this.done = true; + this.keep = true; + } + } + }; + return EqualsOperation; + }(BaseOperation)); + var createEqualsOperation = function (params, owneryQuery, options) { return new EqualsOperation(params, owneryQuery, options); }; + var numericalOperationCreator = function (createNumericalOperation) { + return function (params, owneryQuery, options, name) { + return createNumericalOperation(params, owneryQuery, options, name); + }; + }; + var numericalOperation = function (createTester) { + return numericalOperationCreator(function (params, owneryQuery, options, name) { + var typeofParams = typeof comparable(params); + var test = createTester(params); + return new EqualsOperation(function (b) { + var actualValue = coercePotentiallyNull(b); + return (typeof comparable(actualValue) === typeofParams && test(actualValue)); + }, owneryQuery, options, name); + }); + }; + var createNamedOperation = function (name, params, parentQuery, options) { + var operationCreator = options.operations[name]; + if (!operationCreator) { + throwUnsupportedOperation(name); + } + return operationCreator(params, parentQuery, options, name); + }; + var throwUnsupportedOperation = function (name) { + throw new Error("Unsupported operation: ".concat(name)); + }; + var containsOperation = function (query, options) { + for (var key in query) { + if (options.operations.hasOwnProperty(key) || key.charAt(0) === "$") + return true; + } + return false; + }; + var createNestedOperation = function (keyPath, nestedQuery, parentKey, owneryQuery, options) { + if (containsOperation(nestedQuery, options)) { + var _a = createQueryOperations(nestedQuery, parentKey, options), selfOperations = _a[0], nestedOperations = _a[1]; + if (nestedOperations.length) { + throw new Error("Property queries must contain only operations, or exact objects."); + } + return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, selfOperations); + } + return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, [ + new EqualsOperation(nestedQuery, owneryQuery, options), + ]); + }; + var createQueryOperation = function (query, owneryQuery, _a) { + if (owneryQuery === void 0) { owneryQuery = null; } + var _b = _a === void 0 ? {} : _a, compare = _b.compare, operations = _b.operations; + var options = { + compare: compare || equals, + operations: Object.assign({}, operations || {}), + }; + var _c = createQueryOperations(query, null, options), selfOperations = _c[0], nestedOperations = _c[1]; + var ops = []; + if (selfOperations.length) { + ops.push(new NestedOperation([], query, owneryQuery, options, selfOperations)); + } + ops.push.apply(ops, nestedOperations); + if (ops.length === 1) { + return ops[0]; + } + return new QueryOperation(query, owneryQuery, options, ops); + }; + var createQueryOperations = function (query, parentKey, options) { + var selfOperations = []; + var nestedOperations = []; + if (!isVanillaObject(query)) { + selfOperations.push(new EqualsOperation(query, query, options)); + return [selfOperations, nestedOperations]; + } + for (var key in query) { + if (options.operations.hasOwnProperty(key)) { + var op = createNamedOperation(key, query[key], query, options); + if (op) { + if (!op.propop && parentKey && !options.operations[parentKey]) { + throw new Error("Malformed query. ".concat(key, " cannot be matched against property.")); + } + } + // probably just a flag for another operation (like $options) + if (op != null) { + selfOperations.push(op); + } + } + else if (key.charAt(0) === "$") { + throwUnsupportedOperation(key); + } + else { + nestedOperations.push(createNestedOperation(key.split("."), query[key], key, query, options)); + } + } + return [selfOperations, nestedOperations]; + }; + var createOperationTester = function (operation) { + return function (item, key, owner) { + operation.reset(); + operation.next(item, key, owner); + return operation.keep; + }; + }; + var createQueryTester = function (query, options) { + if (options === void 0) { options = {}; } + return createOperationTester(createQueryOperation(query, null, options)); + }; + + var $Ne = /** @class */ (function (_super) { + __extends($Ne, _super); + function $Ne() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + $Ne.prototype.init = function () { + this._test = createTester(this.params, this.options.compare); + }; + $Ne.prototype.reset = function () { + _super.prototype.reset.call(this); + this.keep = true; + }; + $Ne.prototype.next = function (item) { + if (this._test(item)) { + this.done = true; + this.keep = false; + } + }; + return $Ne; + }(BaseOperation)); + // https://docs.mongodb.com/manual/reference/operator/query/elemMatch/ + var $ElemMatch = /** @class */ (function (_super) { + __extends($ElemMatch, _super); + function $ElemMatch() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + $ElemMatch.prototype.init = function () { + if (!this.params || typeof this.params !== "object") { + throw new Error("Malformed query. $elemMatch must by an object."); + } + this._queryOperation = createQueryOperation(this.params, this.owneryQuery, this.options); + }; + $ElemMatch.prototype.reset = function () { + _super.prototype.reset.call(this); + this._queryOperation.reset(); + }; + $ElemMatch.prototype.next = function (item) { + if (isArray(item)) { + for (var i = 0, length_1 = item.length; i < length_1; i++) { + // reset query operation since item being tested needs to pass _all_ query + // operations for it to be a success + this._queryOperation.reset(); + var child = item[i]; + this._queryOperation.next(child, i, item, false); + this.keep = this.keep || this._queryOperation.keep; + } + this.done = true; + } + else { + this.done = false; + this.keep = false; + } + }; + return $ElemMatch; + }(BaseOperation)); + var $Not = /** @class */ (function (_super) { + __extends($Not, _super); + function $Not() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + $Not.prototype.init = function () { + this._queryOperation = createQueryOperation(this.params, this.owneryQuery, this.options); + }; + $Not.prototype.reset = function () { + _super.prototype.reset.call(this); + this._queryOperation.reset(); + }; + $Not.prototype.next = function (item, key, owner, root) { + this._queryOperation.next(item, key, owner, root); + this.done = this._queryOperation.done; + this.keep = !this._queryOperation.keep; + }; + return $Not; + }(BaseOperation)); + var $Size = /** @class */ (function (_super) { + __extends($Size, _super); + function $Size() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + $Size.prototype.init = function () { }; + $Size.prototype.next = function (item) { + if (isArray(item) && item.length === this.params) { + this.done = true; + this.keep = true; + } + // if (parent && parent.length === this.params) { + // this.done = true; + // this.keep = true; + // } + }; + return $Size; + }(BaseOperation)); + var assertGroupNotEmpty = function (values) { + if (values.length === 0) { + throw new Error("$and/$or/$nor must be a nonempty array"); + } + }; + var $Or = /** @class */ (function (_super) { + __extends($Or, _super); + function $Or() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = false; + return _this; + } + $Or.prototype.init = function () { + var _this = this; + assertGroupNotEmpty(this.params); + this._ops = this.params.map(function (op) { + return createQueryOperation(op, null, _this.options); + }); + }; + $Or.prototype.reset = function () { + this.done = false; + this.keep = false; + for (var i = 0, length_2 = this._ops.length; i < length_2; i++) { + this._ops[i].reset(); + } + }; + $Or.prototype.next = function (item, key, owner) { + var done = false; + var success = false; + for (var i = 0, length_3 = this._ops.length; i < length_3; i++) { + var op = this._ops[i]; + op.next(item, key, owner); + if (op.keep) { + done = true; + success = op.keep; + break; + } + } + this.keep = success; + this.done = done; + }; + return $Or; + }(BaseOperation)); + var $Nor = /** @class */ (function (_super) { + __extends($Nor, _super); + function $Nor() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = false; + return _this; + } + $Nor.prototype.next = function (item, key, owner) { + _super.prototype.next.call(this, item, key, owner); + this.keep = !this.keep; + }; + return $Nor; + }($Or)); + var $In = /** @class */ (function (_super) { + __extends($In, _super); + function $In() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + $In.prototype.init = function () { + var _this = this; + var params = Array.isArray(this.params) ? this.params : [this.params]; + this._testers = params.map(function (value) { + if (containsOperation(value, _this.options)) { + throw new Error("cannot nest $ under ".concat(_this.name.toLowerCase())); + } + return createTester(value, _this.options.compare); + }); + }; + $In.prototype.next = function (item, key, owner) { + var done = false; + var success = false; + for (var i = 0, length_4 = this._testers.length; i < length_4; i++) { + var test = this._testers[i]; + if (test(item)) { + done = true; + success = true; + break; + } + } + this.keep = success; + this.done = done; + }; + return $In; + }(BaseOperation)); + var $Nin = /** @class */ (function (_super) { + __extends($Nin, _super); + function $Nin(params, ownerQuery, options, name) { + var _this = _super.call(this, params, ownerQuery, options, name) || this; + _this.propop = true; + _this._in = new $In(params, ownerQuery, options, name); + return _this; + } + $Nin.prototype.next = function (item, key, owner, root) { + this._in.next(item, key, owner); + if (isArray(owner) && !root) { + if (this._in.keep) { + this.keep = false; + this.done = true; + } + else if (key == owner.length - 1) { + this.keep = true; + this.done = true; + } + } + else { + this.keep = !this._in.keep; + this.done = true; + } + }; + $Nin.prototype.reset = function () { + _super.prototype.reset.call(this); + this._in.reset(); + }; + return $Nin; + }(BaseOperation)); + var $Exists = /** @class */ (function (_super) { + __extends($Exists, _super); + function $Exists() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.propop = true; + return _this; + } + $Exists.prototype.next = function (item, key, owner, root, leaf) { + if (!leaf) { + this.done = true; + this.keep = !this.params; + } + else if (owner.hasOwnProperty(key) === this.params) { + this.done = true; + this.keep = true; + } + }; + return $Exists; + }(BaseOperation)); + var $And = /** @class */ (function (_super) { + __extends($And, _super); + function $And(params, owneryQuery, options, name) { + var _this = _super.call(this, params, owneryQuery, options, params.map(function (query) { return createQueryOperation(query, owneryQuery, options); }), name) || this; + _this.propop = false; + assertGroupNotEmpty(params); + return _this; + } + $And.prototype.next = function (item, key, owner, root) { + this.childrenNext(item, key, owner, root); + }; + return $And; + }(NamedGroupOperation)); + var $All = /** @class */ (function (_super) { + __extends($All, _super); + function $All(params, owneryQuery, options, name) { + var _this = _super.call(this, params, owneryQuery, options, params.map(function (query) { return createQueryOperation(query, owneryQuery, options); }), name) || this; + _this.propop = true; + return _this; + } + $All.prototype.next = function (item, key, owner, root) { + this.childrenNext(item, key, owner, root); + }; + return $All; + }(NamedGroupOperation)); + var $eq = function (params, owneryQuery, options) { + return new EqualsOperation(params, owneryQuery, options); + }; + var $ne = function (params, owneryQuery, options, name) { return new $Ne(params, owneryQuery, options, name); }; + var $or = function (params, owneryQuery, options, name) { return new $Or(params, owneryQuery, options, name); }; + var $nor = function (params, owneryQuery, options, name) { return new $Nor(params, owneryQuery, options, name); }; + var $elemMatch = function (params, owneryQuery, options, name) { return new $ElemMatch(params, owneryQuery, options, name); }; + var $nin = function (params, owneryQuery, options, name) { return new $Nin(params, owneryQuery, options, name); }; + var $in = function (params, owneryQuery, options, name) { + return new $In(params, owneryQuery, options, name); + }; + var $lt = numericalOperation(function (params) { return function (b) { + return b != null && b < params; + }; }); + var $lte = numericalOperation(function (params) { return function (b) { + return b === params || b <= params; + }; }); + var $gt = numericalOperation(function (params) { return function (b) { + return b != null && b > params; + }; }); + var $gte = numericalOperation(function (params) { return function (b) { + return b === params || b >= params; + }; }); + var $mod = function (_a, owneryQuery, options) { + var mod = _a[0], equalsValue = _a[1]; + return new EqualsOperation(function (b) { return comparable(b) % mod === equalsValue; }, owneryQuery, options); + }; + var $exists = function (params, owneryQuery, options, name) { return new $Exists(params, owneryQuery, options, name); }; + var $regex = function (pattern, owneryQuery, options) { + return new EqualsOperation(new RegExp(pattern, owneryQuery.$options), owneryQuery, options); + }; + var $not = function (params, owneryQuery, options, name) { return new $Not(params, owneryQuery, options, name); }; + var typeAliases = { + number: function (v) { return typeof v === "number"; }, + string: function (v) { return typeof v === "string"; }, + bool: function (v) { return typeof v === "boolean"; }, + array: function (v) { return Array.isArray(v); }, + null: function (v) { return v === null; }, + timestamp: function (v) { return v instanceof Date; }, + }; + var $type = function (clazz, owneryQuery, options) { + return new EqualsOperation(function (b) { + if (typeof clazz === "string") { + if (!typeAliases[clazz]) { + throw new Error("Type alias does not exist"); + } + return typeAliases[clazz](b); + } + return b != null ? b instanceof clazz || b.constructor === clazz : false; + }, owneryQuery, options); + }; + var $and = function (params, ownerQuery, options, name) { return new $And(params, ownerQuery, options, name); }; + var $all = function (params, ownerQuery, options, name) { return new $All(params, ownerQuery, options, name); }; + var $size = function (params, ownerQuery, options) { return new $Size(params, ownerQuery, options, "$size"); }; + var $options = function () { return null; }; + var $where = function (params, ownerQuery, options) { + var test; + if (isFunction(params)) { + test = params; + } + else { + throw new Error("In CSP mode, sift does not support strings in \"$where\" condition"); + } + return new EqualsOperation(function (b) { return test.bind(b)(b); }, ownerQuery, options); + }; + + var defaultOperations = /*#__PURE__*/Object.freeze({ + __proto__: null, + $Size: $Size, + $all: $all, + $and: $and, + $elemMatch: $elemMatch, + $eq: $eq, + $exists: $exists, + $gt: $gt, + $gte: $gte, + $in: $in, + $lt: $lt, + $lte: $lte, + $mod: $mod, + $ne: $ne, + $nin: $nin, + $nor: $nor, + $not: $not, + $options: $options, + $or: $or, + $regex: $regex, + $size: $size, + $type: $type, + $where: $where + }); + + var createDefaultQueryOperation = function (query, ownerQuery, _a) { + var _b = _a === void 0 ? {} : _a, compare = _b.compare, operations = _b.operations; + return createQueryOperation(query, ownerQuery, { + compare: compare, + operations: Object.assign({}, defaultOperations, operations || {}), + }); + }; + var createDefaultQueryTester = function (query, options) { + if (options === void 0) { options = {}; } + var op = createDefaultQueryOperation(query, null, options); + return createOperationTester(op); + }; + + exports.$Size = $Size; + exports.$all = $all; + exports.$and = $and; + exports.$elemMatch = $elemMatch; + exports.$eq = $eq; + exports.$exists = $exists; + exports.$gt = $gt; + exports.$gte = $gte; + exports.$in = $in; + exports.$lt = $lt; + exports.$lte = $lte; + exports.$mod = $mod; + exports.$ne = $ne; + exports.$nin = $nin; + exports.$nor = $nor; + exports.$not = $not; + exports.$options = $options; + exports.$or = $or; + exports.$regex = $regex; + exports.$size = $size; + exports.$type = $type; + exports.$where = $where; + exports.EqualsOperation = EqualsOperation; + exports.createDefaultQueryOperation = createDefaultQueryOperation; + exports.createEqualsOperation = createEqualsOperation; + exports.createOperationTester = createOperationTester; + exports.createQueryOperation = createQueryOperation; + exports.createQueryTester = createQueryTester; + exports.default = createDefaultQueryTester; + + Object.defineProperty(exports, '__esModule', { value: true }); + +})); +//# sourceMappingURL=sift.csp.min.js.map diff --git a/gateway/node_modules/sift/sift.csp.min.js.map b/gateway/node_modules/sift/sift.csp.min.js.map new file mode 100644 index 0000000000000000000000000000000000000000..681fb85ba5e89cd6fe66bf4eac30e56945b61a4f --- /dev/null +++ b/gateway/node_modules/sift/sift.csp.min.js.map @@ -0,0 +1 @@ +{"version":3,"file":"sift.csp.min.js","sources":["node_modules/tslib/tslib.es6.js","src/utils.ts","src/core.ts","src/operations.ts","src/index.ts"],"sourcesContent":["/******************************************************************************\r\nCopyright (c) Microsoft Corporation.\r\n\r\nPermission to use, copy, modify, and/or distribute this software for any\r\npurpose with or without fee is hereby granted.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\r\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\r\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\r\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\r\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\r\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\r\nPERFORMANCE OF THIS SOFTWARE.\r\n***************************************************************************** */\r\n/* global Reflect, Promise, SuppressedError, Symbol */\r\n\r\nvar extendStatics = function(d, b) {\r\n extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\r\n return extendStatics(d, b);\r\n};\r\n\r\nexport function __extends(d, b) {\r\n if (typeof b !== \"function\" && b !== null)\r\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = function() {\r\n __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n }\r\n return __assign.apply(this, arguments);\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\r\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\r\n t[p[i]] = s[p[i]];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __esDecorate(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {\r\n function accept(f) { if (f !== void 0 && typeof f !== \"function\") throw new TypeError(\"Function expected\"); return f; }\r\n var kind = contextIn.kind, key = kind === \"getter\" ? \"get\" : kind === \"setter\" ? \"set\" : \"value\";\r\n var target = !descriptorIn && ctor ? contextIn[\"static\"] ? ctor : ctor.prototype : null;\r\n var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});\r\n var _, done = false;\r\n for (var i = decorators.length - 1; i >= 0; i--) {\r\n var context = {};\r\n for (var p in contextIn) context[p] = p === \"access\" ? {} : contextIn[p];\r\n for (var p in contextIn.access) context.access[p] = contextIn.access[p];\r\n context.addInitializer = function (f) { if (done) throw new TypeError(\"Cannot add initializers after decoration has completed\"); extraInitializers.push(accept(f || null)); };\r\n var result = (0, decorators[i])(kind === \"accessor\" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);\r\n if (kind === \"accessor\") {\r\n if (result === void 0) continue;\r\n if (result === null || typeof result !== \"object\") throw new TypeError(\"Object expected\");\r\n if (_ = accept(result.get)) descriptor.get = _;\r\n if (_ = accept(result.set)) descriptor.set = _;\r\n if (_ = accept(result.init)) initializers.unshift(_);\r\n }\r\n else if (_ = accept(result)) {\r\n if (kind === \"field\") initializers.unshift(_);\r\n else descriptor[key] = _;\r\n }\r\n }\r\n if (target) Object.defineProperty(target, contextIn.name, descriptor);\r\n done = true;\r\n};\r\n\r\nexport function __runInitializers(thisArg, initializers, value) {\r\n var useValue = arguments.length > 2;\r\n for (var i = 0; i < initializers.length; i++) {\r\n value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);\r\n }\r\n return useValue ? value : void 0;\r\n};\r\n\r\nexport function __propKey(x) {\r\n return typeof x === \"symbol\" ? x : \"\".concat(x);\r\n};\r\n\r\nexport function __setFunctionName(f, name, prefix) {\r\n if (typeof name === \"symbol\") name = name.description ? \"[\".concat(name.description, \"]\") : \"\";\r\n return Object.defineProperty(f, \"name\", { configurable: true, value: prefix ? \"\".concat(prefix, \" \", name) : name });\r\n};\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\r\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [op[0] & 2, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n var desc = Object.getOwnPropertyDescriptor(m, k);\r\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\r\n desc = { enumerable: true, get: function() { return m[k]; } };\r\n }\r\n Object.defineProperty(o, k2, desc);\r\n}) : (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n o[k2] = m[k];\r\n});\r\n\r\nexport function __exportStar(m, o) {\r\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\r\n}\r\n\r\nexport function __values(o) {\r\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\r\n if (m) return m.call(o);\r\n if (o && typeof o.length === \"number\") return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spreadArrays() {\r\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\r\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\r\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\r\n r[k] = a[j];\r\n return r;\r\n}\r\n\r\nexport function __spreadArray(to, from, pack) {\r\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\r\n if (ar || !(i in from)) {\r\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\r\n ar[i] = from[i];\r\n }\r\n }\r\n return to.concat(ar || Array.prototype.slice.call(from));\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator], i;\r\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\r\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\r\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nvar __setModuleDefault = Object.create ? (function(o, v) {\r\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\r\n}) : function(o, v) {\r\n o[\"default\"] = v;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\r\n __setModuleDefault(result, mod);\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n\r\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\r\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\r\n}\r\n\r\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\r\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\r\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\r\n}\r\n\r\nexport function __classPrivateFieldIn(state, receiver) {\r\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\r\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\r\n}\r\n\r\nexport function __addDisposableResource(env, value, async) {\r\n if (value !== null && value !== void 0) {\r\n if (typeof value !== \"object\" && typeof value !== \"function\") throw new TypeError(\"Object expected.\");\r\n var dispose;\r\n if (async) {\r\n if (!Symbol.asyncDispose) throw new TypeError(\"Symbol.asyncDispose is not defined.\");\r\n dispose = value[Symbol.asyncDispose];\r\n }\r\n if (dispose === void 0) {\r\n if (!Symbol.dispose) throw new TypeError(\"Symbol.dispose is not defined.\");\r\n dispose = value[Symbol.dispose];\r\n }\r\n if (typeof dispose !== \"function\") throw new TypeError(\"Object not disposable.\");\r\n env.stack.push({ value: value, dispose: dispose, async: async });\r\n }\r\n else if (async) {\r\n env.stack.push({ async: true });\r\n }\r\n return value;\r\n}\r\n\r\nvar _SuppressedError = typeof SuppressedError === \"function\" ? SuppressedError : function (error, suppressed, message) {\r\n var e = new Error(message);\r\n return e.name = \"SuppressedError\", e.error = error, e.suppressed = suppressed, e;\r\n};\r\n\r\nexport function __disposeResources(env) {\r\n function fail(e) {\r\n env.error = env.hasError ? new _SuppressedError(e, env.error, \"An error was suppressed during disposal.\") : e;\r\n env.hasError = true;\r\n }\r\n function next() {\r\n while (env.stack.length) {\r\n var rec = env.stack.pop();\r\n try {\r\n var result = rec.dispose && rec.dispose.call(rec.value);\r\n if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });\r\n }\r\n catch (e) {\r\n fail(e);\r\n }\r\n }\r\n if (env.hasError) throw env.error;\r\n }\r\n return next();\r\n}\r\n\r\nexport default {\r\n __extends: __extends,\r\n __assign: __assign,\r\n __rest: __rest,\r\n __decorate: __decorate,\r\n __param: __param,\r\n __metadata: __metadata,\r\n __awaiter: __awaiter,\r\n __generator: __generator,\r\n __createBinding: __createBinding,\r\n __exportStar: __exportStar,\r\n __values: __values,\r\n __read: __read,\r\n __spread: __spread,\r\n __spreadArrays: __spreadArrays,\r\n __spreadArray: __spreadArray,\r\n __await: __await,\r\n __asyncGenerator: __asyncGenerator,\r\n __asyncDelegator: __asyncDelegator,\r\n __asyncValues: __asyncValues,\r\n __makeTemplateObject: __makeTemplateObject,\r\n __importStar: __importStar,\r\n __importDefault: __importDefault,\r\n __classPrivateFieldGet: __classPrivateFieldGet,\r\n __classPrivateFieldSet: __classPrivateFieldSet,\r\n __classPrivateFieldIn: __classPrivateFieldIn,\r\n __addDisposableResource: __addDisposableResource,\r\n __disposeResources: __disposeResources,\r\n};\r\n",null,null,null,null],"names":[],"mappings":";;;;;;IAAA;IACA;AACA;IACA;IACA;AACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;AACA;IACA,IAAI,aAAa,GAAG,SAAS,CAAC,EAAE,CAAC,EAAE;IACnC,IAAI,aAAa,GAAG,MAAM,CAAC,cAAc;IACzC,SAAS,EAAE,SAAS,EAAE,EAAE,EAAE,YAAY,KAAK,IAAI,UAAU,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC,EAAE,CAAC;IACpF,QAAQ,UAAU,CAAC,EAAE,CAAC,EAAE,EAAE,KAAK,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC1G,IAAI,OAAO,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC/B,CAAC,CAAC;AACF;IACO,SAAS,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE;IAChC,IAAI,IAAI,OAAO,CAAC,KAAK,UAAU,IAAI,CAAC,KAAK,IAAI;IAC7C,QAAQ,MAAM,IAAI,SAAS,CAAC,sBAAsB,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,+BAA+B,CAAC,CAAC;IAClG,IAAI,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACxB,IAAI,SAAS,EAAE,GAAG,EAAE,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC,EAAE;IAC3C,IAAI,CAAC,CAAC,SAAS,GAAG,CAAC,KAAK,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,SAAS,GAAG,CAAC,CAAC,SAAS,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;IACzF,CAAC;AA6RD;IACuB,OAAO,eAAe,KAAK,UAAU,GAAG,eAAe,GAAG,UAAU,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE;IACvH,IAAI,IAAI,CAAC,GAAG,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;IAC/B,IAAI,OAAO,CAAC,CAAC,IAAI,GAAG,iBAAiB,EAAE,CAAC,CAAC,KAAK,GAAG,KAAK,EAAE,CAAC,CAAC,UAAU,GAAG,UAAU,EAAE,CAAC,CAAC;IACrF;;IC5TO,IAAM,WAAW,GAAG,UAAQ,IAAI,EAAA;IACrC,IAAA,IAAM,UAAU,GAAG,UAAU,GAAG,IAAI,GAAG,GAAG,CAAC;IAC3C,IAAA,OAAO,UAAU,KAAK,EAAA;IACpB,QAAA,OAAO,YAAY,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC;IAC5C,KAAC,CAAC;IACJ,CAAC,CAAC;IAEF,IAAM,YAAY,GAAG,UAAC,KAAK,EAAK,EAAA,OAAA,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA,EAAA,CAAC;IAE/D,IAAM,UAAU,GAAG,UAAC,KAAU,EAAA;IACnC,IAAA,IAAI,KAAK,YAAY,IAAI,EAAE;IACzB,QAAA,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;SACxB;IAAM,SAAA,IAAI,OAAO,CAAC,KAAK,CAAC,EAAE;IACzB,QAAA,OAAO,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;SAC9B;aAAM,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,MAAM,KAAK,UAAU,EAAE;IACtD,QAAA,OAAO,KAAK,CAAC,MAAM,EAAE,CAAC;SACvB;IAED,IAAA,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;IAEK,IAAM,qBAAqB,GAAG,UAAC,KAAU,EAAA;QAC9C,OAAA,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,CAAA;IAA5B,CAA4B,CAAC;IAExB,IAAM,OAAO,GAAG,WAAW,CAAa,OAAO,CAAC,CAAC;IACjD,IAAM,QAAQ,GAAG,WAAW,CAAS,QAAQ,CAAC,CAAC;IAC/C,IAAM,UAAU,GAAG,WAAW,CAAW,UAAU,CAAC,CAAC;IACrD,IAAM,UAAU,GAAG,UAAC,IAAS,EAAE,GAAQ,EAAA;IAC5C,IAAA,OAAO,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5D,CAAC,CAAC;IACK,IAAM,eAAe,GAAG,UAAC,KAAK,EAAA;IACnC,IAAA,QACE,KAAK;IACL,SAAC,KAAK,CAAC,WAAW,KAAK,MAAM;gBAC3B,KAAK,CAAC,WAAW,KAAK,KAAK;IAC3B,YAAA,KAAK,CAAC,WAAW,CAAC,QAAQ,EAAE,KAAK,qCAAqC;IACtE,YAAA,KAAK,CAAC,WAAW,CAAC,QAAQ,EAAE,KAAK,oCAAoC,CAAC;IACxE,QAAA,CAAC,KAAK,CAAC,MAAM,EACb;IACJ,CAAC,CAAC;IAEK,IAAM,MAAM,GAAG,UAAC,CAAC,EAAE,CAAC,EAAA;QACzB,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,EAAE;IACvB,QAAA,OAAO,IAAI,CAAC;SACb;IACD,IAAA,IAAI,CAAC,KAAK,CAAC,EAAE;IACX,QAAA,OAAO,IAAI,CAAC;SACb;QAED,IAAI,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;IAC3E,QAAA,OAAO,KAAK,CAAC;SACd;IAED,IAAA,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE;YACd,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,EAAE;IACzB,YAAA,OAAO,KAAK,CAAC;aACd;IACD,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,CAAC,CAAN,MAAA,EAAQ,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;IAC/C,YAAA,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAAE,gBAAA,OAAO,KAAK,CAAC;aACvC;IACD,QAAA,OAAO,IAAI,CAAC;SACb;IAAM,SAAA,IAAI,QAAQ,CAAC,CAAC,CAAC,EAAE;IACtB,QAAA,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE;IACnD,YAAA,OAAO,KAAK,CAAC;aACd;IACD,QAAA,KAAK,IAAM,GAAG,IAAI,CAAC,EAAE;IACnB,YAAA,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC;IAAE,gBAAA,OAAO,KAAK,CAAC;aAC3C;IACD,QAAA,OAAO,IAAI,CAAC;SACb;IACD,IAAA,OAAO,KAAK,CAAC;IACf,CAAC;;ICYD;;;IAGG;IAEH,IAAM,iBAAiB,GAAG,UACxB,IAAS,EACT,OAAc,EACd,IAAY,EACZ,KAAa,EACb,GAAQ,EACR,KAAU,EAAA;IAEV,IAAA,IAAM,UAAU,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;;;QAIlC,IACE,OAAO,CAAC,IAAI,CAAC;IACb,QAAA,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IACzB,QAAA,CAAC,UAAU,CAAC,IAAI,EAAE,UAAU,CAAC,EAC7B;IACA,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAT,MAAA,EAAW,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;;;IAGlD,YAAA,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE;IAC9D,gBAAA,OAAO,KAAK,CAAC;iBACd;aACF;SACF;QAED,IAAI,KAAK,KAAK,OAAO,CAAC,MAAM,IAAI,IAAI,IAAI,IAAI,EAAE;IAC5C,QAAA,OAAO,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,KAAK,CAAC,EAAE,KAAK,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;SACtE;IAED,IAAA,OAAO,iBAAiB,CACtB,IAAI,CAAC,UAAU,CAAC,EAChB,OAAO,EACP,IAAI,EACJ,KAAK,GAAG,CAAC,EACT,UAAU,EACV,IAAI,CACL,CAAC;IACJ,CAAC,CAAC;IAEF,IAAA,aAAA,kBAAA,YAAA;IAME,IAAA,SAAA,aAAA,CACW,MAAe,EACf,WAAgB,EAChB,OAAgB,EAChB,IAAa,EAAA;YAHb,IAAM,CAAA,MAAA,GAAN,MAAM,CAAS;YACf,IAAW,CAAA,WAAA,GAAX,WAAW,CAAK;YAChB,IAAO,CAAA,OAAA,GAAP,OAAO,CAAS;YAChB,IAAI,CAAA,IAAA,GAAJ,IAAI,CAAS;YAEtB,IAAI,CAAC,IAAI,EAAE,CAAC;SACb;QACS,aAAI,CAAA,SAAA,CAAA,IAAA,GAAd,eAAmB,CAAA;IACnB,IAAA,aAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;IACE,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;IAClB,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;SACnB,CAAA;QAQH,OAAC,aAAA,CAAA;IAAD,CAAC,EAAA,CAAA,CAAA;IAED,IAAA,cAAA,kBAAA,UAAA,MAAA,EAAA;QAAsC,SAAkB,CAAA,cAAA,EAAA,MAAA,CAAA,CAAA;IAItD,IAAA,SAAA,cAAA,CACE,MAAW,EACX,WAAgB,EAChB,OAAgB,EACA,QAA0B,EAAA;YAE1C,IAAA,KAAA,GAAA,MAAK,YAAC,MAAM,EAAE,WAAW,EAAE,OAAO,CAAC,IAAC,IAAA,CAAA;YAFpB,KAAQ,CAAA,QAAA,GAAR,QAAQ,CAAkB;;SAG3C;IAED;IACG;IAEH,IAAA,cAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;IACE,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;IAClB,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;IAClB,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAC,QAAQ,CAAlB,MAAA,EAAoB,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;gBAC3D,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;aAC1B;SACF,CAAA;IAID;IACG;QAEO,cAAY,CAAA,SAAA,CAAA,YAAA,GAAtB,UACE,IAAS,EACT,GAAQ,EACR,KAAU,EACV,IAAa,EACb,IAAc,EAAA;YAEd,IAAI,IAAI,GAAG,IAAI,CAAC;YAChB,IAAI,IAAI,GAAG,IAAI,CAAC;IAChB,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAC,QAAQ,CAAlB,MAAA,EAAoB,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;gBAC3D,IAAM,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IACxC,YAAA,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE;IACxB,gBAAA,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;iBACnD;IACD,YAAA,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE;oBACxB,IAAI,GAAG,KAAK,CAAC;iBACd;IACD,YAAA,IAAI,cAAc,CAAC,IAAI,EAAE;IACvB,gBAAA,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE;wBACxB,MAAM;qBACP;iBACF;qBAAM;oBACL,IAAI,GAAG,KAAK,CAAC;iBACd;aACF;IACD,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACjB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB,CAAA;QACH,OAAC,cAAA,CAAA;IAAD,CAzDA,CAAsC,aAAa,CAyDlD,CAAA,CAAA;IAED,IAAA,mBAAA,kBAAA,UAAA,MAAA,EAAA;QACU,SAAc,CAAA,mBAAA,EAAA,MAAA,CAAA,CAAA;QAItB,SACE,mBAAA,CAAA,MAAW,EACX,WAAgB,EAChB,OAAgB,EAChB,QAA0B,EACjB,IAAY,EAAA;YAErB,IAAA,KAAA,GAAA,MAAK,CAAC,IAAA,CAAA,IAAA,EAAA,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,CAAC,IAAC,IAAA,CAAA;YAFrC,KAAI,CAAA,IAAA,GAAJ,IAAI,CAAQ;;SAGtB;QACH,OAAC,mBAAA,CAAA;IAAD,CAdA,CACU,cAAc,CAavB,CAAA,CAAA;IAED,IAAA,cAAA,kBAAA,UAAA,MAAA,EAAA;QAA2C,SAAc,CAAA,cAAA,EAAA,MAAA,CAAA,CAAA;IAAzD,IAAA,SAAA,cAAA,GAAA;;YACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;SAOxB;IANC;IACG;QAEH,cAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAW,EAAE,GAAQ,EAAE,MAAW,EAAE,IAAa,EAAA;YACpD,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;SAC5C,CAAA;QACH,OAAC,cAAA,CAAA;IAAD,CARA,CAA2C,cAAc,CAQxD,CAAA,CAAA;IAED,IAAA,eAAA,kBAAA,UAAA,MAAA,EAAA;QAAqC,SAAc,CAAA,eAAA,EAAA,MAAA,CAAA,CAAA;QAEjD,SACW,eAAA,CAAA,OAAc,EACvB,MAAW,EACX,WAAgB,EAChB,OAAgB,EAChB,QAA0B,EAAA;YAE1B,IAAA,KAAA,GAAA,MAAK,CAAC,IAAA,CAAA,IAAA,EAAA,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,CAAC,IAAC,IAAA,CAAA;YANrC,KAAO,CAAA,OAAA,GAAP,OAAO,CAAO;YAFhB,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;IAwBvB;IACG;YAEK,KAAgB,CAAA,gBAAA,GAAG,UACzB,KAAU,EACV,GAAQ,EACR,KAAU,EACV,IAAa,EACb,IAAa,EAAA;IAEb,YAAA,KAAI,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IACjD,YAAA,OAAO,CAAC,KAAI,CAAC,IAAI,CAAC;IACpB,SAAC,CAAC;;SA3BD;IACD;IACG;IAEH,IAAA,eAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,MAAW,EAAA;IACnC,QAAA,iBAAiB,CACf,IAAI,EACJ,IAAI,CAAC,OAAO,EACZ,IAAI,CAAC,gBAAgB,EACrB,CAAC,EACD,GAAG,EACH,MAAM,CACP,CAAC;SACH,CAAA;QAeH,OAAC,eAAA,CAAA;IAAD,CAtCA,CAAqC,cAAc,CAsClD,CAAA,CAAA;IAEM,IAAM,YAAY,GAAG,UAAC,CAAC,EAAE,OAAmB,EAAA;IACjD,IAAA,IAAI,CAAC,YAAY,QAAQ,EAAE;IACzB,QAAA,OAAO,CAAC,CAAC;SACV;IACD,IAAA,IAAI,CAAC,YAAY,MAAM,EAAE;IACvB,QAAA,OAAO,UAAC,CAAC,EAAA;IACP,YAAA,IAAM,MAAM,GAAG,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClD,YAAA,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC;IAChB,YAAA,OAAO,MAAM,CAAC;IAChB,SAAC,CAAC;SACH;IACD,IAAA,IAAM,WAAW,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;IAClC,IAAA,OAAO,UAAC,CAAC,EAAK,EAAA,OAAA,OAAO,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAA,EAAA,CAAC;IACpD,CAAC,CAAC;AAEF,QAAA,eAAA,kBAAA,UAAA,MAAA,EAAA;QAA6C,SAAqB,CAAA,eAAA,EAAA,MAAA,CAAA,CAAA;IAAlE,IAAA,SAAA,eAAA,GAAA;;YACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;SAaxB;IAXC,IAAA,eAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,YAAA;IACE,QAAA,IAAI,CAAC,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;SAC9D,CAAA;IACD,IAAA,eAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,UAAK,IAAI,EAAE,GAAQ,EAAE,MAAW,EAAA;IAC9B,QAAA,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE;gBACxD,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE;IACjC,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACjB,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;iBAClB;aACF;SACF,CAAA;QACH,OAAC,eAAA,CAAA;IAAD,CAdA,CAA6C,aAAa,CAczD,EAAA;QAEY,qBAAqB,GAAG,UACnC,MAAW,EACX,WAAgB,EAChB,OAAgB,EAAA,EACb,OAAA,IAAI,eAAe,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,CAAC,CAAjD,GAAkD;IAEhD,IAAM,yBAAyB,GACpC,UAAC,wBAA+C,EAAA;IAChD,IAAA,OAAA,UAAC,MAAW,EAAE,WAAgB,EAAE,OAAgB,EAAE,IAAY,EAAA;YAC5D,OAAO,wBAAwB,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;SACrE,CAAA;IAFD,CAEC,CAAC;IAEG,IAAM,kBAAkB,GAAG,UAAC,YAAoC,EAAA;QACrE,OAAA,yBAAyB,CACvB,UAAC,MAAW,EAAE,WAAuB,EAAE,OAAgB,EAAE,IAAY,EAAA;IACnE,QAAA,IAAM,YAAY,GAAG,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC;IAC/C,QAAA,IAAM,IAAI,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;IAClC,QAAA,OAAO,IAAI,eAAe,CACxB,UAAC,CAAC,EAAA;IACA,YAAA,IAAM,WAAW,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAAC;IAC7C,YAAA,QACE,OAAO,UAAU,CAAC,WAAW,CAAC,KAAK,YAAY,IAAI,IAAI,CAAC,WAAW,CAAC,EACpE;IACJ,SAAC,EACD,WAAW,EACX,OAAO,EACP,IAAI,CACL,CAAC;IACJ,KAAC,CACF,CAAA;IAhBD,CAgBC,CAAC;IASJ,IAAM,oBAAoB,GAAG,UAC3B,IAAY,EACZ,MAAW,EACX,WAAgB,EAChB,OAAgB,EAAA;QAEhB,IAAM,gBAAgB,GAAG,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAClD,IAAI,CAAC,gBAAgB,EAAE;YACrB,yBAAyB,CAAC,IAAI,CAAC,CAAC;SACjC;QACD,OAAO,gBAAgB,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IAC9D,CAAC,CAAC;IAEF,IAAM,yBAAyB,GAAG,UAAC,IAAY,EAAA;IAC7C,IAAA,MAAM,IAAI,KAAK,CAAC,iCAA0B,IAAI,CAAE,CAAC,CAAC;IACpD,CAAC,CAAC;IAEK,IAAM,iBAAiB,GAAG,UAAC,KAAU,EAAE,OAAgB,EAAA;IAC5D,IAAA,KAAK,IAAM,GAAG,IAAI,KAAK,EAAE;IACvB,QAAA,IAAI,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG;IACjE,YAAA,OAAO,IAAI,CAAC;SACf;IACD,IAAA,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;IACF,IAAM,qBAAqB,GAAG,UAC5B,OAAc,EACd,WAAgB,EAChB,SAAiB,EACjB,WAAgB,EAChB,OAAgB,EAAA;IAEhB,IAAA,IAAI,iBAAiB,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE;IACrC,QAAA,IAAA,EAAqC,GAAA,qBAAqB,CAC9D,WAAW,EACX,SAAS,EACT,OAAO,CACR,EAJM,cAAc,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,gBAAgB,QAItC,CAAC;IACF,QAAA,IAAI,gBAAgB,CAAC,MAAM,EAAE;IAC3B,YAAA,MAAM,IAAI,KAAK,CACb,kEAAkE,CACnE,CAAC;aACH;IACD,QAAA,OAAO,IAAI,eAAe,CACxB,OAAO,EACP,WAAW,EACX,WAAW,EACX,OAAO,EACP,cAAc,CACf,CAAC;SACH;QACD,OAAO,IAAI,eAAe,CAAC,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,OAAO,EAAE;IACrE,QAAA,IAAI,eAAe,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC;IACvD,KAAA,CAAC,CAAC;IACL,CAAC,CAAC;QAEW,oBAAoB,GAAG,UAClC,KAAqB,EACrB,WAAuB,EACvB,EAA8C,EAAA;IAD9C,IAAA,IAAA,WAAA,KAAA,KAAA,CAAA,EAAA,EAAA,WAAuB,GAAA,IAAA,CAAA,EAAA;IACvB,IAAA,IAAA,EAAA,GAAA,EAAA,KAAA,KAAA,CAAA,GAA4C,EAAE,GAAA,EAAA,EAA5C,OAAO,GAAA,EAAA,CAAA,OAAA,EAAE,UAAU,GAAA,EAAA,CAAA,UAAA,CAAA;IAErB,IAAA,IAAM,OAAO,GAAG;YACd,OAAO,EAAE,OAAO,IAAI,MAAM;YAC1B,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,UAAU,IAAI,EAAE,CAAC;SAChD,CAAC;IAEI,IAAA,IAAA,EAAqC,GAAA,qBAAqB,CAC9D,KAAK,EACL,IAAI,EACJ,OAAO,CACR,EAJM,cAAc,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,gBAAgB,QAItC,CAAC;QAEF,IAAM,GAAG,GAAG,EAAE,CAAC;IAEf,IAAA,IAAI,cAAc,CAAC,MAAM,EAAE;IACzB,QAAA,GAAG,CAAC,IAAI,CACN,IAAI,eAAe,CAAC,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,CAAC,CACrE,CAAC;SACH;IAED,IAAA,GAAG,CAAC,IAAI,CAAA,KAAA,CAAR,GAAG,EAAS,gBAAgB,CAAE,CAAA;IAE9B,IAAA,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE;IACpB,QAAA,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC;SACf;QACD,OAAO,IAAI,cAAc,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IAC9D,EAAE;IAEF,IAAM,qBAAqB,GAAG,UAC5B,KAAU,EACV,SAAiB,EACjB,OAAgB,EAAA;QAEhB,IAAM,cAAc,GAAG,EAAE,CAAC;QAC1B,IAAM,gBAAgB,GAAG,EAAE,CAAC;IAC5B,IAAA,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,EAAE;IAC3B,QAAA,cAAc,CAAC,IAAI,CAAC,IAAI,eAAe,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;IAChE,QAAA,OAAO,CAAC,cAAc,EAAE,gBAAgB,CAAC,CAAC;SAC3C;IACD,IAAA,KAAK,IAAM,GAAG,IAAI,KAAK,EAAE;YACvB,IAAI,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE;IAC1C,YAAA,IAAM,EAAE,GAAG,oBAAoB,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;gBAEjE,IAAI,EAAE,EAAE;IACN,gBAAA,IAAI,CAAC,EAAE,CAAC,MAAM,IAAI,SAAS,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE;IAC7D,oBAAA,MAAM,IAAI,KAAK,CACb,2BAAoB,GAAG,EAAA,sCAAA,CAAsC,CAC9D,CAAC;qBACH;iBACF;;IAGD,YAAA,IAAI,EAAE,IAAI,IAAI,EAAE;IACd,gBAAA,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;iBACzB;aACF;iBAAM,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;gBAChC,yBAAyB,CAAC,GAAG,CAAC,CAAC;aAChC;iBAAM;gBACL,gBAAgB,CAAC,IAAI,CACnB,qBAAqB,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,CACvE,CAAC;aACH;SACF;IAED,IAAA,OAAO,CAAC,cAAc,EAAE,gBAAgB,CAAC,CAAC;IAC5C,CAAC,CAAC;AAEK,QAAM,qBAAqB,GAChC,UAAQ,SAA2B,EAAA;IACnC,IAAA,OAAA,UAAC,IAAW,EAAE,GAAS,EAAE,KAAW,EAAA;YAClC,SAAS,CAAC,KAAK,EAAE,CAAC;YAClB,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;YACjC,OAAO,SAAS,CAAC,IAAI,CAAC;SACvB,CAAA;IAJD,EAIE;AAES,QAAA,iBAAiB,GAAG,UAC/B,KAAqB,EACrB,OAA8B,EAAA;IAA9B,IAAA,IAAA,OAAA,KAAA,KAAA,CAAA,EAAA,EAAA,OAA8B,GAAA,EAAA,CAAA,EAAA;QAE9B,OAAO,qBAAqB,CAC1B,oBAAoB,CAAiB,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAC3D,CAAC;IACJ;;IC7dA,IAAA,GAAA,kBAAA,UAAA,MAAA,EAAA;QAAkB,SAAkB,CAAA,GAAA,EAAA,MAAA,CAAA,CAAA;IAApC,IAAA,SAAA,GAAA,GAAA;;YACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;SAexB;IAbC,IAAA,GAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,YAAA;IACE,QAAA,IAAI,CAAC,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;SAC9D,CAAA;IACD,IAAA,GAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;YACE,MAAK,CAAA,SAAA,CAAC,KAAK,CAAA,IAAA,CAAA,IAAA,CAAE,CAAC;IACd,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB,CAAA;QACD,GAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAA;IACZ,QAAA,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;IACpB,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACjB,YAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;aACnB;SACF,CAAA;QACH,OAAC,GAAA,CAAA;IAAD,CAhBA,CAAkB,aAAa,CAgB9B,CAAA,CAAA;IACD;IACA,IAAA,UAAA,kBAAA,UAAA,MAAA,EAAA;QAAyB,SAAyB,CAAA,UAAA,EAAA,MAAA,CAAA,CAAA;IAAlD,IAAA,SAAA,UAAA,GAAA;;YACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;SAiCxB;IA/BC,IAAA,UAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,YAAA;IACE,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,EAAE;IACnD,YAAA,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;aACnE;IACD,QAAA,IAAI,CAAC,eAAe,GAAG,oBAAoB,CACzC,IAAI,CAAC,MAAM,EACX,IAAI,CAAC,WAAW,EAChB,IAAI,CAAC,OAAO,CACb,CAAC;SACH,CAAA;IACD,IAAA,UAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;YACE,MAAK,CAAA,SAAA,CAAC,KAAK,CAAA,IAAA,CAAA,IAAA,CAAE,CAAC;IACd,QAAA,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;SAC9B,CAAA;QACD,UAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAA;IACZ,QAAA,IAAI,OAAO,CAAC,IAAI,CAAC,EAAE;IACjB,YAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAT,MAAA,EAAW,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;;;IAGlD,gBAAA,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;IAE7B,gBAAA,IAAM,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IACtB,gBAAA,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;IACjD,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC;iBACpD;IACD,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;aAClB;iBAAM;IACL,YAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;IAClB,YAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;aACnB;SACF,CAAA;QACH,OAAC,UAAA,CAAA;IAAD,CAlCA,CAAyB,aAAa,CAkCrC,CAAA,CAAA;IAED,IAAA,IAAA,kBAAA,UAAA,MAAA,EAAA;QAAmB,SAAyB,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;IAA5C,IAAA,SAAA,IAAA,GAAA;;YACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;SAkBxB;IAhBC,IAAA,IAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,YAAA;IACE,QAAA,IAAI,CAAC,eAAe,GAAG,oBAAoB,CACzC,IAAI,CAAC,MAAM,EACX,IAAI,CAAC,WAAW,EAChB,IAAI,CAAC,OAAO,CACb,CAAC;SACH,CAAA;IACD,IAAA,IAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;YACE,MAAK,CAAA,SAAA,CAAC,KAAK,CAAA,IAAA,CAAA,IAAA,CAAE,CAAC;IACd,QAAA,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;SAC9B,CAAA;QACD,IAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAA;IACjD,QAAA,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;YAClD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC;YACtC,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC;SACxC,CAAA;QACH,OAAC,IAAA,CAAA;IAAD,CAnBA,CAAmB,aAAa,CAmB/B,CAAA,CAAA;AAED,QAAA,KAAA,kBAAA,UAAA,MAAA,EAAA;QAA2B,SAAkB,CAAA,KAAA,EAAA,MAAA,CAAA,CAAA;IAA7C,IAAA,SAAA,KAAA,GAAA;;YACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;SAYxB;QAXC,KAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,eAAS,CAAA;QACT,KAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAI,EAAA;IACP,QAAA,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,EAAE;IAChD,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACjB,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;aAClB;;;;;SAKF,CAAA;QACH,OAAC,KAAA,CAAA;IAAD,CAbA,CAA2B,aAAa,CAavC,EAAA;IAED,IAAM,mBAAmB,GAAG,UAAC,MAAa,EAAA;IACxC,IAAA,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;IACvB,QAAA,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;SAC3D;IACH,CAAC,CAAC;IAEF,IAAA,GAAA,kBAAA,UAAA,MAAA,EAAA;QAAkB,SAAkB,CAAA,GAAA,EAAA,MAAA,CAAA,CAAA;IAApC,IAAA,SAAA,GAAA,GAAA;;YACW,KAAM,CAAA,MAAA,GAAG,KAAK,CAAC;;SA+BzB;IA7BC,IAAA,GAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,YAAA;YAAA,IAKC,KAAA,GAAA,IAAA,CAAA;IAJC,QAAA,mBAAmB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,UAAC,EAAE,EAAA;gBAC7B,OAAA,oBAAoB,CAAC,EAAE,EAAE,IAAI,EAAE,KAAI,CAAC,OAAO,CAAC,CAAA;IAA5C,SAA4C,CAC7C,CAAC;SACH,CAAA;IACD,IAAA,GAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;IACE,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;IAClB,QAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;IAClB,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAC,IAAI,CAAd,MAAA,EAAgB,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;gBACvD,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;aACtB;SACF,CAAA;IACD,IAAA,GAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAA;YAClC,IAAI,IAAI,GAAG,KAAK,CAAC;YACjB,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAC,IAAI,CAAd,MAAA,EAAgB,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;gBACvD,IAAM,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;gBACxB,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;IAC1B,YAAA,IAAI,EAAE,CAAC,IAAI,EAAE;oBACX,IAAI,GAAG,IAAI,CAAC;IACZ,gBAAA,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC;oBAClB,MAAM;iBACP;aACF;IAED,QAAA,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;IACpB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB,CAAA;QACH,OAAC,GAAA,CAAA;IAAD,CAhCA,CAAkB,aAAa,CAgC9B,CAAA,CAAA;IAED,IAAA,IAAA,kBAAA,UAAA,MAAA,EAAA;QAAmB,SAAG,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;IAAtB,IAAA,SAAA,IAAA,GAAA;;YACW,KAAM,CAAA,MAAA,GAAG,KAAK,CAAC;;SAKzB;IAJC,IAAA,IAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAA;YAClC,MAAK,CAAA,SAAA,CAAC,IAAI,CAAC,IAAA,CAAA,IAAA,EAAA,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;IAC7B,QAAA,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;SACxB,CAAA;QACH,OAAC,IAAA,CAAA;IAAD,CANA,CAAmB,GAAG,CAMrB,CAAA,CAAA;IAED,IAAA,GAAA,kBAAA,UAAA,MAAA,EAAA;QAAkB,SAAkB,CAAA,GAAA,EAAA,MAAA,CAAA,CAAA;IAApC,IAAA,SAAA,GAAA,GAAA;;YACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;SA0BxB;IAxBC,IAAA,GAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,YAAA;YAAA,IAQC,KAAA,GAAA,IAAA,CAAA;YAPC,IAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACxE,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,UAAC,KAAK,EAAA;gBAC/B,IAAI,iBAAiB,CAAC,KAAK,EAAE,KAAI,CAAC,OAAO,CAAC,EAAE;IAC1C,gBAAA,MAAM,IAAI,KAAK,CAAC,sBAAA,CAAA,MAAA,CAAuB,KAAI,CAAC,IAAI,CAAC,WAAW,EAAE,CAAE,CAAC,CAAC;iBACnE;gBACD,OAAO,YAAY,CAAC,KAAK,EAAE,KAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACnD,SAAC,CAAC,CAAC;SACJ,CAAA;IACD,IAAA,GAAA,CAAA,SAAA,CAAA,IAAI,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAA;YAClC,IAAI,IAAI,GAAG,KAAK,CAAC;YACjB,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,QAAA,KAAS,IAAA,CAAC,GAAG,CAAC,EAAI,QAAM,GAAK,IAAI,CAAC,QAAQ,CAAlB,MAAA,EAAoB,CAAC,GAAG,QAAM,EAAE,CAAC,EAAE,EAAE;gBAC3D,IAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC9B,YAAA,IAAI,IAAI,CAAC,IAAI,CAAC,EAAE;oBACd,IAAI,GAAG,IAAI,CAAC;oBACZ,OAAO,GAAG,IAAI,CAAC;oBACf,MAAM;iBACP;aACF;IAED,QAAA,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;IACpB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB,CAAA;QACH,OAAC,GAAA,CAAA;IAAD,CA3BA,CAAkB,aAAa,CA2B9B,CAAA,CAAA;IAED,IAAA,IAAA,kBAAA,UAAA,MAAA,EAAA;QAAmB,SAAkB,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;IAGnC,IAAA,SAAA,IAAA,CAAY,MAAW,EAAE,UAAe,EAAE,OAAgB,EAAE,IAAY,EAAA;YACtE,IAAA,KAAA,GAAA,MAAK,CAAC,IAAA,CAAA,IAAA,EAAA,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,IAAC,IAAA,CAAA;YAHlC,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;IAIrB,QAAA,KAAI,CAAC,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;;SACvD;QACD,IAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAA;YACjD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;YAEhC,IAAI,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE;IAC3B,YAAA,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE;IACjB,gBAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;IAClB,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;iBAClB;qBAAM,IAAI,GAAG,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;IAClC,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACjB,gBAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;iBAClB;aACF;iBAAM;gBACL,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;IAC3B,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;aAClB;SACF,CAAA;IACD,IAAA,IAAA,CAAA,SAAA,CAAA,KAAK,GAAL,YAAA;YACE,MAAK,CAAA,SAAA,CAAC,KAAK,CAAA,IAAA,CAAA,IAAA,CAAE,CAAC;IACd,QAAA,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;SAClB,CAAA;QACH,OAAC,IAAA,CAAA;IAAD,CA3BA,CAAmB,aAAa,CA2B/B,CAAA,CAAA;IAED,IAAA,OAAA,kBAAA,UAAA,MAAA,EAAA;QAAsB,SAAsB,CAAA,OAAA,EAAA,MAAA,CAAA,CAAA;IAA5C,IAAA,SAAA,OAAA,GAAA;;YACW,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;SAUxB;QATC,OAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAE,IAAc,EAAA;YACjE,IAAI,CAAC,IAAI,EAAE;IACT,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACjB,YAAA,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC;aAC1B;iBAAM,IAAI,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC,MAAM,EAAE;IACpD,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACjB,YAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;aAClB;SACF,CAAA;QACH,OAAC,OAAA,CAAA;IAAD,CAXA,CAAsB,aAAa,CAWlC,CAAA,CAAA;IAED,IAAA,IAAA,kBAAA,UAAA,MAAA,EAAA;QAAmB,SAAmB,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;IAEpC,IAAA,SAAA,IAAA,CACE,MAAoB,EACpB,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA;IAEZ,QAAA,IAAA,KAAA,GAAA,MAAK,CAAA,IAAA,CAAA,IAAA,EACH,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,CAAC,GAAG,CAAC,UAAC,KAAK,EAAA,EAAK,OAAA,oBAAoB,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,CAAC,CAAA,EAAA,CAAC,EACxE,IAAI,CACL,IAAC,IAAA,CAAA;YAbK,KAAM,CAAA,MAAA,GAAG,KAAK,CAAC;YAetB,mBAAmB,CAAC,MAAM,CAAC,CAAC;;SAC7B;QACD,IAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAA;YACjD,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;SAC3C,CAAA;QACH,OAAC,IAAA,CAAA;IAAD,CArBA,CAAmB,mBAAmB,CAqBrC,CAAA,CAAA;IAED,IAAA,IAAA,kBAAA,UAAA,MAAA,EAAA;QAAmB,SAAmB,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;IAEpC,IAAA,SAAA,IAAA,CACE,MAAoB,EACpB,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA;IAEZ,QAAA,IAAA,KAAA,GAAA,MAAK,CAAA,IAAA,CAAA,IAAA,EACH,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,CAAC,GAAG,CAAC,UAAC,KAAK,EAAA,EAAK,OAAA,oBAAoB,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,CAAC,CAAA,EAAA,CAAC,EACxE,IAAI,CACL,IAAC,IAAA,CAAA;YAbK,KAAM,CAAA,MAAA,GAAG,IAAI,CAAC;;SActB;QACD,IAAI,CAAA,SAAA,CAAA,IAAA,GAAJ,UAAK,IAAS,EAAE,GAAQ,EAAE,KAAU,EAAE,IAAa,EAAA;YACjD,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;SAC3C,CAAA;QACH,OAAC,IAAA,CAAA;IAAD,CAnBA,CAAmB,mBAAmB,CAmBrC,CAAA,CAAA;QAEY,GAAG,GAAG,UAAC,MAAW,EAAE,WAAuB,EAAE,OAAgB,EAAA;QACxE,OAAA,IAAI,eAAe,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,CAAC,CAAA;IAAjD,EAAkD;AACvC,QAAA,GAAG,GAAG,UACjB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAA3C,GAA4C;AACpC,QAAA,GAAG,GAAG,UACjB,MAAoB,EACpB,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAA3C,GAA4C;AACpC,QAAA,IAAI,GAAG,UAClB,MAAoB,EACpB,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAA5C,GAA6C;AACrC,QAAA,UAAU,GAAG,UACxB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,UAAU,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAAlD,GAAmD;AAC3C,QAAA,IAAI,GAAG,UAClB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAA5C,GAA6C;AACrC,QAAA,GAAG,GAAG,UACjB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA;QAEZ,OAAO,IAAI,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IACrD,EAAE;AAEW,QAAA,GAAG,GAAG,kBAAkB,CAAC,UAAC,MAAM,EAAA,EAAK,OAAA,UAAC,CAAC,EAAA;IAClD,IAAA,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC,GAAG,MAAM,CAAC;IACjC,CAAC,CAAA,EAAA,EAAE;AACU,QAAA,IAAI,GAAG,kBAAkB,CAAC,UAAC,MAAM,EAAA,EAAK,OAAA,UAAC,CAAC,EAAA;IACnD,IAAA,OAAO,CAAC,KAAK,MAAM,IAAI,CAAC,IAAI,MAAM,CAAC;IACrC,CAAC,CAAA,EAAA,EAAE;AACU,QAAA,GAAG,GAAG,kBAAkB,CAAC,UAAC,MAAM,EAAA,EAAK,OAAA,UAAC,CAAC,EAAA;IAClD,IAAA,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC,GAAG,MAAM,CAAC;IACjC,CAAC,CAAA,EAAA,EAAE;AACU,QAAA,IAAI,GAAG,kBAAkB,CAAC,UAAC,MAAM,EAAA,EAAK,OAAA,UAAC,CAAC,EAAA;IACnD,IAAA,OAAO,CAAC,KAAK,MAAM,IAAI,CAAC,IAAI,MAAM,CAAC;IACrC,CAAC,CAAA,EAAA,EAAE;QACU,IAAI,GAAG,UAClB,EAA4B,EAC5B,WAAuB,EACvB,OAAgB,EAAA;YAFf,GAAG,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,WAAW,GAAA,EAAA,CAAA,CAAA,CAAA,CAAA;QAIjB,OAAA,IAAI,eAAe,CACjB,UAAC,CAAC,EAAK,EAAA,OAAA,UAAU,CAAC,CAAC,CAAC,GAAG,GAAG,KAAK,WAAW,CAAA,EAAA,EAC1C,WAAW,EACX,OAAO,CACR,CAAA;IAJD,EAIE;AACS,QAAA,OAAO,GAAG,UACrB,MAAe,EACf,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAA/C,GAAgD;QACxC,MAAM,GAAG,UACpB,OAAe,EACf,WAAuB,EACvB,OAAgB,EAAA;IAEhB,IAAA,OAAA,IAAI,eAAe,CACjB,IAAI,MAAM,CAAC,OAAO,EAAE,WAAW,CAAC,QAAQ,CAAC,EACzC,WAAW,EACX,OAAO,CACR,CAAA;IAJD,EAIE;AACS,QAAA,IAAI,GAAG,UAClB,MAAW,EACX,WAAuB,EACvB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAA5C,GAA6C;IAElD,IAAM,WAAW,GAAG;QAClB,MAAM,EAAE,UAAC,CAAC,EAAK,EAAA,OAAA,OAAO,CAAC,KAAK,QAAQ,CAAA,EAAA;QACpC,MAAM,EAAE,UAAC,CAAC,EAAK,EAAA,OAAA,OAAO,CAAC,KAAK,QAAQ,CAAA,EAAA;QACpC,IAAI,EAAE,UAAC,CAAC,EAAK,EAAA,OAAA,OAAO,CAAC,KAAK,SAAS,CAAA,EAAA;IACnC,IAAA,KAAK,EAAE,UAAC,CAAC,EAAA,EAAK,OAAA,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA,EAAA;QAC9B,IAAI,EAAE,UAAC,CAAC,EAAA,EAAK,OAAA,CAAC,KAAK,IAAI,CAAA,EAAA;QACvB,SAAS,EAAE,UAAC,CAAC,EAAA,EAAK,OAAA,CAAC,YAAY,IAAI,CAAA,EAAA;KACpC,CAAC;QAEW,KAAK,GAAG,UACnB,KAAwB,EACxB,WAAuB,EACvB,OAAgB,EAAA;IAEhB,IAAA,OAAA,IAAI,eAAe,CACjB,UAAC,CAAC,EAAA;IACA,QAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;IAC7B,YAAA,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE;IACvB,gBAAA,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;iBAC9C;IAED,YAAA,OAAO,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;aAC9B;IAED,QAAA,OAAO,CAAC,IAAI,IAAI,GAAG,CAAC,YAAY,KAAK,IAAI,CAAC,CAAC,WAAW,KAAK,KAAK,GAAG,KAAK,CAAC;IAC3E,KAAC,EACD,WAAW,EACX,OAAO,CACR,CAAA;IAdD,EAcE;AACS,QAAA,IAAI,GAAG,UAClB,MAAoB,EACpB,UAAsB,EACtB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,CAA3C,GAA4C;AAEpC,QAAA,IAAI,GAAG,UAClB,MAAoB,EACpB,UAAsB,EACtB,OAAgB,EAChB,IAAY,EAAA,EACT,OAAA,IAAI,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,CAA3C,GAA4C;AACpC,QAAA,KAAK,GAAG,UACnB,MAAc,EACd,UAAsB,EACtB,OAAgB,EACb,EAAA,OAAA,IAAI,KAAK,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,CAA/C,GAAgD;QACxC,QAAQ,GAAG,cAAM,OAAA,IAAI,CAAJ,GAAK;QACtB,MAAM,GAAG,UACpB,MAAyB,EACzB,UAAsB,EACtB,OAAgB,EAAA;IAEhB,IAAA,IAAI,IAAI,CAAC;IAET,IAAA,IAAI,UAAU,CAAC,MAAM,CAAC,EAAE;YACtB,IAAI,GAAG,MAAM,CAAC;SACf;IAAM,SAEA;IACL,QAAA,MAAM,IAAI,KAAK,CACb,oEAAkE,CACnE,CAAC;SACH;QAED,OAAO,IAAI,eAAe,CAAC,UAAC,CAAC,EAAK,EAAA,OAAA,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAf,EAAe,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IAC1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACpZA,QAAM,2BAA2B,GAAG,UAClC,KAAqB,EACrB,UAAe,EACf,EAA8C,EAAA;IAA9C,IAAA,IAAA,EAAA,GAAA,EAAA,KAAA,KAAA,CAAA,GAA4C,EAAE,GAAA,EAAA,EAA5C,OAAO,GAAA,EAAA,CAAA,OAAA,EAAE,UAAU,GAAA,EAAA,CAAA,UAAA,CAAA;IAErB,IAAA,OAAO,oBAAoB,CAAC,KAAK,EAAE,UAAU,EAAE;IAC7C,QAAA,OAAO,EAAA,OAAA;IACP,QAAA,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,iBAAiB,EAAE,UAAU,IAAI,EAAE,CAAC;IACnE,KAAA,CAAC,CAAC;IACL,EAAE;AAEF,QAAM,wBAAwB,GAAG,UAC/B,KAAqB,EACrB,OAA8B,EAAA;IAA9B,IAAA,IAAA,OAAA,KAAA,KAAA,CAAA,EAAA,EAAA,OAA8B,GAAA,EAAA,CAAA,EAAA;QAE9B,IAAM,EAAE,GAAG,2BAA2B,CAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IAC7D,IAAA,OAAO,qBAAqB,CAAC,EAAE,CAAC,CAAC;IACnC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","x_google_ignoreList":[0]} \ No newline at end of file diff --git a/gateway/node_modules/sift/sift.min.js b/gateway/node_modules/sift/sift.min.js new file mode 100644 index 0000000000000000000000000000000000000000..035d448a30582a4cb52335e873f5177dffdc3a1a --- /dev/null +++ b/gateway/node_modules/sift/sift.min.js @@ -0,0 +1,2 @@ +!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).sift={})}(this,(function(n){"use strict";var t=function(n,r){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,t){n.__proto__=t}||function(n,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(n[r]=t[r])},t(n,r)};function r(n,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function i(){this.constructor=n}t(n,r),n.prototype=null===r?Object.create(r):(i.prototype=r.prototype,new i)}"function"==typeof SuppressedError&&SuppressedError;var i=function(n){var t="[object "+n+"]";return function(n){return u(n)===t}},u=function(n){return Object.prototype.toString.call(n)},e=function(n){return n instanceof Date?n.getTime():o(n)?n.map(e):n&&"function"==typeof n.toJSON?n.toJSON():n},o=i("Array"),f=i("Object"),s=i("Function"),c=function(n,t){if(null==n&&n==t)return!0;if(n===t)return!0;if(Object.prototype.toString.call(n)!==Object.prototype.toString.call(t))return!1;if(o(n)){if(n.length!==t.length)return!1;for(var r=0,i=n.length;rn}})),Q=d((function(n){return function(t){return t===n||t>=n}})),V=function(n,t,r){var i=n[0],u=n[1];return new y((function(n){return e(n)%i===u}),t,r)},W=function(n,t,r,i){return new N(n,t,r,i)},X=function(n,t,r){return new y(new RegExp(n,t.$options),t,r)},Y=function(n,t,r,i){return new M(n,t,r,i)},Z={number:function(n){return"number"==typeof n},string:function(n){return"string"==typeof n},bool:function(n){return"boolean"==typeof n},array:function(n){return Array.isArray(n)},null:function(n){return null===n},timestamp:function(n){return n instanceof Date}},nn=function(n,t,r){return new y((function(t){if("string"==typeof n){if(!Z[n])throw new Error("Type alias does not exist");return Z[n](t)}return null!=t&&(t instanceof n||t.constructor===n)}),t,r)},tn=function(n,t,r,i){return new C(n,t,r,i)},rn=function(n,t,r,i){return new D(n,t,r,i)},un=function(n,t,r){return new S(n,t,r,"$size")},en=function(){return null},on=function(n,t,r){var i;if(s(n))i=n;else{if(process.env.CSP_ENABLED)throw new Error('In CSP mode, sift does not support strings in "$where" condition');i=new Function("obj","return "+n)}return new y((function(n){return i.bind(n)(n)}),t,r)},fn=Object.freeze({__proto__:null,$Size:S,$all:rn,$and:tn,$elemMatch:B,$eq:P,$exists:W,$gt:L,$gte:Q,$in:H,$lt:J,$lte:K,$mod:V,$ne:R,$nin:G,$nor:U,$not:Y,$options:en,$or:I,$regex:X,$size:un,$type:nn,$where:on}),sn=function(n,t,r){var i=void 0===r?{}:r,u=i.compare,e=i.operations;return g(n,t,{compare:u,operations:Object.assign({},fn,e||{})})};n.$Size=S,n.$all=rn,n.$and=tn,n.$elemMatch=B,n.$eq=P,n.$exists=W,n.$gt=L,n.$gte=Q,n.$in=H,n.$lt=J,n.$lte=K,n.$mod=V,n.$ne=R,n.$nin=G,n.$nor=U,n.$not=Y,n.$options=en,n.$or=I,n.$regex=X,n.$size=un,n.$type=nn,n.$where=on,n.EqualsOperation=y,n.createDefaultQueryOperation=sn,n.createEqualsOperation=function(n,t,r){return new y(n,t,r)},n.createOperationTester=x,n.createQueryOperation=g,n.createQueryTester=function(n,t){return void 0===t&&(t={}),x(g(n,null,t))},n.default=function(n,t){void 0===t&&(t={});var r=sn(n,null,t);return x(r)},Object.defineProperty(n,"v",{value:!0})})); +//# sourceMappingURL=sift.min.js.map diff --git a/gateway/node_modules/sift/sift.min.js.map b/gateway/node_modules/sift/sift.min.js.map new file mode 100644 index 0000000000000000000000000000000000000000..fd7253781fa98245d6aa677fda8edd70fc5141e0 --- /dev/null +++ b/gateway/node_modules/sift/sift.min.js.map @@ -0,0 +1 @@ +{"version":3,"file":"sift.min.js","sources":["node_modules/tslib/tslib.es6.js","src/utils.ts","src/core.ts","src/operations.ts","src/index.ts"],"sourcesContent":["/******************************************************************************\r\nCopyright (c) Microsoft Corporation.\r\n\r\nPermission to use, copy, modify, and/or distribute this software for any\r\npurpose with or without fee is hereby granted.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\r\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\r\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\r\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\r\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\r\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\r\nPERFORMANCE OF THIS SOFTWARE.\r\n***************************************************************************** */\r\n/* global Reflect, Promise, SuppressedError, Symbol */\r\n\r\nvar extendStatics = function(d, b) {\r\n extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\r\n return extendStatics(d, b);\r\n};\r\n\r\nexport function __extends(d, b) {\r\n if (typeof b !== \"function\" && b !== null)\r\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = function() {\r\n __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n }\r\n return __assign.apply(this, arguments);\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\r\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\r\n t[p[i]] = s[p[i]];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __esDecorate(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {\r\n function accept(f) { if (f !== void 0 && typeof f !== \"function\") throw new TypeError(\"Function expected\"); return f; }\r\n var kind = contextIn.kind, key = kind === \"getter\" ? \"get\" : kind === \"setter\" ? \"set\" : \"value\";\r\n var target = !descriptorIn && ctor ? contextIn[\"static\"] ? ctor : ctor.prototype : null;\r\n var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});\r\n var _, done = false;\r\n for (var i = decorators.length - 1; i >= 0; i--) {\r\n var context = {};\r\n for (var p in contextIn) context[p] = p === \"access\" ? {} : contextIn[p];\r\n for (var p in contextIn.access) context.access[p] = contextIn.access[p];\r\n context.addInitializer = function (f) { if (done) throw new TypeError(\"Cannot add initializers after decoration has completed\"); extraInitializers.push(accept(f || null)); };\r\n var result = (0, decorators[i])(kind === \"accessor\" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);\r\n if (kind === \"accessor\") {\r\n if (result === void 0) continue;\r\n if (result === null || typeof result !== \"object\") throw new TypeError(\"Object expected\");\r\n if (_ = accept(result.get)) descriptor.get = _;\r\n if (_ = accept(result.set)) descriptor.set = _;\r\n if (_ = accept(result.init)) initializers.unshift(_);\r\n }\r\n else if (_ = accept(result)) {\r\n if (kind === \"field\") initializers.unshift(_);\r\n else descriptor[key] = _;\r\n }\r\n }\r\n if (target) Object.defineProperty(target, contextIn.name, descriptor);\r\n done = true;\r\n};\r\n\r\nexport function __runInitializers(thisArg, initializers, value) {\r\n var useValue = arguments.length > 2;\r\n for (var i = 0; i < initializers.length; i++) {\r\n value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);\r\n }\r\n return useValue ? value : void 0;\r\n};\r\n\r\nexport function __propKey(x) {\r\n return typeof x === \"symbol\" ? x : \"\".concat(x);\r\n};\r\n\r\nexport function __setFunctionName(f, name, prefix) {\r\n if (typeof name === \"symbol\") name = name.description ? \"[\".concat(name.description, \"]\") : \"\";\r\n return Object.defineProperty(f, \"name\", { configurable: true, value: prefix ? \"\".concat(prefix, \" \", name) : name });\r\n};\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\r\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [op[0] & 2, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n var desc = Object.getOwnPropertyDescriptor(m, k);\r\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\r\n desc = { enumerable: true, get: function() { return m[k]; } };\r\n }\r\n Object.defineProperty(o, k2, desc);\r\n}) : (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n o[k2] = m[k];\r\n});\r\n\r\nexport function __exportStar(m, o) {\r\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\r\n}\r\n\r\nexport function __values(o) {\r\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\r\n if (m) return m.call(o);\r\n if (o && typeof o.length === \"number\") return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spreadArrays() {\r\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\r\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\r\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\r\n r[k] = a[j];\r\n return r;\r\n}\r\n\r\nexport function __spreadArray(to, from, pack) {\r\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\r\n if (ar || !(i in from)) {\r\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\r\n ar[i] = from[i];\r\n }\r\n }\r\n return to.concat(ar || Array.prototype.slice.call(from));\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator], i;\r\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\r\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\r\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nvar __setModuleDefault = Object.create ? (function(o, v) {\r\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\r\n}) : function(o, v) {\r\n o[\"default\"] = v;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\r\n __setModuleDefault(result, mod);\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n\r\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\r\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\r\n}\r\n\r\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\r\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\r\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\r\n}\r\n\r\nexport function __classPrivateFieldIn(state, receiver) {\r\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\r\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\r\n}\r\n\r\nexport function __addDisposableResource(env, value, async) {\r\n if (value !== null && value !== void 0) {\r\n if (typeof value !== \"object\" && typeof value !== \"function\") throw new TypeError(\"Object expected.\");\r\n var dispose;\r\n if (async) {\r\n if (!Symbol.asyncDispose) throw new TypeError(\"Symbol.asyncDispose is not defined.\");\r\n dispose = value[Symbol.asyncDispose];\r\n }\r\n if (dispose === void 0) {\r\n if (!Symbol.dispose) throw new TypeError(\"Symbol.dispose is not defined.\");\r\n dispose = value[Symbol.dispose];\r\n }\r\n if (typeof dispose !== \"function\") throw new TypeError(\"Object not disposable.\");\r\n env.stack.push({ value: value, dispose: dispose, async: async });\r\n }\r\n else if (async) {\r\n env.stack.push({ async: true });\r\n }\r\n return value;\r\n}\r\n\r\nvar _SuppressedError = typeof SuppressedError === \"function\" ? SuppressedError : function (error, suppressed, message) {\r\n var e = new Error(message);\r\n return e.name = \"SuppressedError\", e.error = error, e.suppressed = suppressed, e;\r\n};\r\n\r\nexport function __disposeResources(env) {\r\n function fail(e) {\r\n env.error = env.hasError ? new _SuppressedError(e, env.error, \"An error was suppressed during disposal.\") : e;\r\n env.hasError = true;\r\n }\r\n function next() {\r\n while (env.stack.length) {\r\n var rec = env.stack.pop();\r\n try {\r\n var result = rec.dispose && rec.dispose.call(rec.value);\r\n if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });\r\n }\r\n catch (e) {\r\n fail(e);\r\n }\r\n }\r\n if (env.hasError) throw env.error;\r\n }\r\n return next();\r\n}\r\n\r\nexport default {\r\n __extends: __extends,\r\n __assign: __assign,\r\n __rest: __rest,\r\n __decorate: __decorate,\r\n __param: __param,\r\n __metadata: __metadata,\r\n __awaiter: __awaiter,\r\n __generator: __generator,\r\n __createBinding: __createBinding,\r\n __exportStar: __exportStar,\r\n __values: __values,\r\n __read: __read,\r\n __spread: __spread,\r\n __spreadArrays: __spreadArrays,\r\n __spreadArray: __spreadArray,\r\n __await: __await,\r\n __asyncGenerator: __asyncGenerator,\r\n __asyncDelegator: __asyncDelegator,\r\n __asyncValues: __asyncValues,\r\n __makeTemplateObject: __makeTemplateObject,\r\n __importStar: __importStar,\r\n __importDefault: __importDefault,\r\n __classPrivateFieldGet: __classPrivateFieldGet,\r\n __classPrivateFieldSet: __classPrivateFieldSet,\r\n __classPrivateFieldIn: __classPrivateFieldIn,\r\n __addDisposableResource: __addDisposableResource,\r\n __disposeResources: __disposeResources,\r\n};\r\n",null,null,null,null],"names":["extendStatics","d","b","Object","setPrototypeOf","__proto__","Array","p","prototype","hasOwnProperty","call","__extends","TypeError","String","__","this","constructor","create","SuppressedError","typeChecker","type","typeString","value","getClassName","toString","comparable","Date","getTime","isArray","map","toJSON","isObject","isFunction","equals","a","length","i","length_1","keys","key","walkKeyPathValues","item","keyPath","next","depth","owner","currentKey","isNaN","Number","isProperty","BaseOperation","params","owneryQuery","options","name","init","reset","done","keep","GroupOperation","_super","children","_this","length_2","childrenNext","root","leaf","length_3","childOperation","NamedGroupOperation","QueryOperation","propop","parent","NestedOperation","_nextNestedValue","createTester","compare","Function","RegExp","result","test","lastIndex","comparableA","EqualsOperation","_test","numericalOperation","createNumericalOperation","typeofParams","actualValue","createNamedOperation","parentQuery","operationCreator","operations","throwUnsupportedOperation","Error","containsOperation","query","charAt","createNestedOperation","nestedQuery","parentKey","_a","createQueryOperations","selfOperations","createQueryOperation","_b","assign","_c","nestedOperations","ops","push","apply","op","split","createOperationTester","operation","$Ne","$ElemMatch","_queryOperation","child","$Not","$Size","assertGroupNotEmpty","values","$Or","_ops","success","$Nor","$In","_testers","concat","toLowerCase","length_4","$Nin","ownerQuery","_in","$Exists","$And","$All","$eq","$ne","$or","$nor","$elemMatch","$nin","$in","$lt","$lte","$gt","$gte","$mod","mod","equalsValue","$exists","$regex","pattern","$options","$not","typeAliases","number","v","string","bool","array","null","timestamp","$type","clazz","$and","$all","$size","$where","process","env","CSP_ENABLED","bind","createDefaultQueryOperation","defaultOperations"],"mappings":"4OAgBA,IAAIA,EAAgB,SAASC,EAAGC,GAI5B,OAHAF,EAAgBG,OAAOC,gBAClB,CAAEC,UAAW,cAAgBC,OAAS,SAAUL,EAAGC,GAAKD,EAAEI,UAAYH,CAAE,GACzE,SAAUD,EAAGC,GAAK,IAAK,IAAIK,KAAKL,EAAOC,OAAOK,UAAUC,eAAeC,KAAKR,EAAGK,KAAIN,EAAEM,GAAKL,EAAEK,KACzFP,EAAcC,EAAGC,EAC5B,EAEO,SAASS,EAAUV,EAAGC,GACzB,GAAiB,mBAANA,GAA0B,OAANA,EAC3B,MAAM,IAAIU,UAAU,uBAAyBC,OAAOX,GAAK,iCAE7D,SAASY,IAAOC,KAAKC,YAAcf,CAAI,CADvCD,EAAcC,EAAGC,GAEjBD,EAAEO,UAAkB,OAANN,EAAaC,OAAOc,OAAOf,IAAMY,EAAGN,UAAYN,EAAEM,UAAW,IAAIM,EACnF,CA8RkD,mBAApBI,iBAAiCA,gBCzTxD,IAAMC,EAAc,SAAQC,GACjC,IAAMC,EAAa,WAAaD,EAAO,IACvC,OAAO,SAAUE,GACf,OAAOC,EAAaD,KAAWD,CACjC,CACF,EAEME,EAAe,SAACD,GAAU,OAAAnB,OAAOK,UAAUgB,SAASd,KAAKY,IAElDG,EAAa,SAACH,GACzB,OAAIA,aAAiBI,KACZJ,EAAMK,UACJC,EAAQN,GACVA,EAAMO,IAAIJ,GACRH,GAAiC,mBAAjBA,EAAMQ,OACxBR,EAAMQ,SAGRR,CACT,EAKaM,EAAUT,EAAwB,SAClCY,EAAWZ,EAAoB,UAC/Ba,EAAab,EAAsB,YAenCc,EAAS,SAACC,EAAGhC,GACxB,GAAS,MAALgC,GAAaA,GAAKhC,EACpB,OAAO,EAET,GAAIgC,IAAMhC,EACR,OAAO,EAGT,GAAIC,OAAOK,UAAUgB,SAASd,KAAKwB,KAAO/B,OAAOK,UAAUgB,SAASd,KAAKR,GACvE,OAAO,EAGT,GAAI0B,EAAQM,GAAI,CACd,GAAIA,EAAEC,SAAWjC,EAAEiC,OACjB,OAAO,EAET,IAAS,IAAAC,EAAI,EAAKC,EAAWH,EAALC,OAAQC,EAAIC,EAAQD,IAC1C,IAAKH,EAAOC,EAAEE,GAAIlC,EAAEkC,IAAK,OAAO,EAElC,OAAO,CACR,CAAM,GAAIL,EAASG,GAAI,CACtB,GAAI/B,OAAOmC,KAAKJ,GAAGC,SAAWhC,OAAOmC,KAAKpC,GAAGiC,OAC3C,OAAO,EAET,IAAK,IAAMI,KAAOL,EAChB,IAAKD,EAAOC,EAAEK,GAAMrC,EAAEqC,IAAO,OAAO,EAEtC,OAAO,CACR,CACD,OAAO,CACT,ECiBMC,EAAoB,SACxBC,EACAC,EACAC,EACAC,EACAL,EACAM,GAEA,IAAMC,EAAaJ,EAAQE,GAI3B,GACEhB,EAAQa,IACRM,MAAMC,OAAOF,MD3ES,SAACL,EAAWF,GACpC,OAAOE,EAAKhC,eAAe8B,KAASP,EAAWS,EAAKF,GACtD,CC0EKU,CAAWR,EAAMK,GAElB,IAAS,IAAAV,EAAI,EAAKC,EAAWI,EAALN,OAAWC,EAAIC,EAAQD,IAG7C,IAAKI,EAAkBC,EAAKL,GAAIM,EAASC,EAAMC,EAAOR,EAAGK,GACvD,OAAO,EAKb,OAAIG,IAAUF,EAAQP,QAAkB,MAARM,EACvBE,EAAKF,EAAMF,EAAKM,EAAiB,IAAVD,EAAaA,IAAUF,EAAQP,QAGxDK,EACLC,EAAKK,GACLJ,EACAC,EACAC,EAAQ,EACRE,EACAL,EAEJ,EAEAS,EAAA,WAME,SAAAA,EACWC,EACAC,EACAC,EACAC,GAHAvC,KAAMoC,OAANA,EACApC,KAAWqC,YAAXA,EACArC,KAAOsC,QAAPA,EACAtC,KAAIuC,KAAJA,EAETvC,KAAKwC,MACN,CAaH,OAZYL,EAAI1C,UAAA+C,KAAd,aACAL,EAAA1C,UAAAgD,MAAA,WACEzC,KAAK0C,MAAO,EACZ1C,KAAK2C,MAAO,GASfR,CAAD,IAEAS,EAAA,SAAAC,GAIE,SAAAD,EACER,EACAC,EACAC,EACgBQ,GAEhB,IAAAC,EAAAF,YAAMT,EAAQC,EAAaC,IAAStC,YAFpB+C,EAAQD,SAARA,GAGjB,CA8CH,OAzDsClD,EAAkBgD,EAAAC,GAgBtDD,EAAAnD,UAAAgD,MAAA,WACEzC,KAAK2C,MAAO,EACZ3C,KAAK0C,MAAO,EACZ,IAAS,IAAArB,EAAI,EAAK2B,EAAWhD,KAAK8C,SAAV1B,OAAoBC,EAAI2B,EAAQ3B,IACtDrB,KAAK8C,SAASzB,GAAGoB,SASXG,EAAYnD,UAAAwD,aAAtB,SACEvB,EACAF,EACAM,EACAoB,EACAC,GAIA,IAFA,IAAIT,GAAO,EACPC,GAAO,EACFtB,EAAI,EAAK+B,EAAWpD,KAAK8C,SAAV1B,OAAoBC,EAAI+B,EAAQ/B,IAAK,CAC3D,IAAMgC,EAAiBrD,KAAK8C,SAASzB,GAOrC,GANKgC,EAAeX,MAClBW,EAAezB,KAAKF,EAAMF,EAAKM,EAAOoB,EAAMC,GAEzCE,EAAeV,OAClBA,GAAO,GAELU,EAAeX,MACjB,IAAKW,EAAeV,KAClB,WAGFD,GAAO,CAEV,CACD1C,KAAK0C,KAAOA,EACZ1C,KAAK2C,KAAOA,GAEfC,CAAD,CAzDA,CAAsCT,GA2DtCmB,EAAA,SAAAT,GAKE,SACES,EAAAlB,EACAC,EACAC,EACAQ,EACSP,GAET,IAAAQ,EAAAF,EAAMlD,KAAAK,KAAAoC,EAAQC,EAAaC,EAASQ,IAAU9C,YAFrC+C,EAAIR,KAAJA,GAGV,CACH,OAbU3C,EAAc0D,EAAAT,GAavBS,CAAD,CAdA,CACUV,GAeVW,EAAA,SAAAV,GAAA,SAAAU,yDACWR,EAAMS,QAAG,GAOnB,CAAD,OAR2C5D,EAAc2D,EAAAV,GAKvDU,EAAI9D,UAAAmC,KAAJ,SAAKF,EAAaF,EAAUiC,EAAaP,GACvClD,KAAKiD,aAAavB,EAAMF,EAAKiC,EAAQP,IAExCK,CAAD,CARA,CAA2CX,GAU3Cc,EAAA,SAAAb,GAEE,SACWa,EAAA/B,EACTS,EACAC,EACAC,EACAQ,GAEA,IAAAC,EAAAF,EAAMlD,KAAAK,KAAAoC,EAAQC,EAAaC,EAASQ,IAAU9C,YANrC+C,EAAOpB,QAAPA,EAFFoB,EAAMS,QAAG,EA2BVT,EAAgBY,EAAG,SACzBpD,EACAiB,EACAM,EACAoB,EACAC,GAGA,OADAJ,EAAKE,aAAa1C,EAAOiB,EAAKM,EAAOoB,EAAMC,IACnCJ,EAAKL,IACf,GA3BC,CA4BH,OAtCqC9C,EAAc8D,EAAAb,GAcjDa,EAAAjE,UAAAmC,KAAA,SAAKF,EAAWF,EAAUiC,GACxBhC,EACEC,EACA1B,KAAK2B,QACL3B,KAAK2D,EACL,EACAnC,EACAiC,IAiBLC,CAAD,CAtCA,CAAqCd,GAwCxBgB,EAAe,SAACzC,EAAG0C,GAC9B,GAAI1C,aAAa2C,SACf,OAAO3C,EAET,GAAIA,aAAa4C,OACf,OAAO,SAAC5E,GACN,IAAM6E,EAAsB,iBAAN7E,GAAkBgC,EAAE8C,KAAK9E,GAE/C,OADAgC,EAAE+C,UAAY,EACPF,CACT,EAEF,IAAMG,EAAczD,EAAWS,GAC/B,OAAO,SAAChC,GAAM,OAAA0E,EAAQM,EAAazD,EAAWvB,IAChD,EAEAiF,EAAA,SAAAvB,GAAA,SAAAuB,yDACWrB,EAAMS,QAAG,GAanB,CAAD,OAd6C5D,EAAqBwE,EAAAvB,GAGhEuB,EAAA3E,UAAA+C,KAAA,WACExC,KAAKqE,EAAQT,EAAa5D,KAAKoC,OAAQpC,KAAKsC,QAAQuB,UAEtDO,EAAA3E,UAAAmC,KAAA,SAAKF,EAAMF,EAAUiC,GACdlE,MAAMsB,QAAQ4C,KAAWA,EAAO/D,eAAe8B,IAC9CxB,KAAKqE,EAAM3C,EAAMF,EAAKiC,KACxBzD,KAAK0C,MAAO,EACZ1C,KAAK2C,MAAO,IAInByB,CAAD,CAdA,CAA6CjC,GA4BhCmC,EAAqB,SAACV,GACjC,OANCW,EAOC,SAACnC,EAAaC,EAAyBC,EAAkBC,GACvD,IAAMiC,SAAsB9D,EAAW0B,GACjC6B,EAAOL,EAAaxB,GAC1B,OAAO,IAAIgC,GACT,SAACjF,GACC,IDtT4BoB,ECsTtBkE,EDrTL,OAD2BlE,ECsTcpB,GDrTlC,KAAOoB,ECsTf,cACSG,EAAW+D,KAAiBD,GAAgBP,EAAKQ,EAE5D,GACApC,EACAC,EACAC,EAEJ,EApBF,SAACH,EAAaC,EAAkBC,EAAkBC,GAChD,OAAOgC,EAAyBnC,EAAQC,EAAaC,EAASC,IAFhE,IAACgC,CAMD,EAyBIG,EAAuB,SAC3BnC,EACAH,EACAuC,EACArC,GAEA,IAAMsC,EAAmBtC,EAAQuC,WAAWtC,GAI5C,OAHKqC,GACHE,EAA0BvC,GAErBqC,EAAiBxC,EAAQuC,EAAarC,EAASC,EACxD,EAEMuC,EAA4B,SAACvC,GACjC,MAAM,IAAIwC,MAAM,iCAA0BxC,GAC5C,EAEayC,EAAoB,SAACC,EAAY3C,GAC5C,IAAK,IAAMd,KAAOyD,EAChB,GAAI3C,EAAQuC,WAAWnF,eAAe8B,IAA0B,MAAlBA,EAAI0D,OAAO,GACvD,OAAO,EAEX,OAAO,CACT,EACMC,EAAwB,SAC5BxD,EACAyD,EACAC,EACAhD,EACAC,GAEA,GAAI0C,EAAkBI,EAAa9C,GAAU,CACrC,IAAAgD,EAAqCC,EACzCH,EACAC,EACA/C,GAHKkD,EAAcF,EAAA,GAKrB,QAAqBlE,OACnB,MAAM,IAAI2D,MACR,oEAGJ,OAAO,IAAIrB,EACT/B,EACAyD,EACA/C,EACAC,EACAkD,EAEH,CACD,OAAO,IAAI9B,EAAgB/B,EAASyD,EAAa/C,EAAaC,EAAS,CACrE,IAAI8B,EAAgBgB,EAAa/C,EAAaC,IAElD,EAEamD,EAAuB,SAClCR,EACA5C,EACAiD,QADA,IAAAjD,IAAAA,EAAuB,MACvB,IAAAqD,OAAA,IAAAJ,EAA4C,CAAA,EAAEA,EAA5CzB,EAAO6B,EAAA7B,QAAEgB,EAAUa,EAAAb,WAEfvC,EAAU,CACduB,QAASA,GAAW3C,EACpB2D,WAAYzF,OAAOuG,OAAO,CAAA,EAAId,GAAc,CAAA,IAGxCe,EAAqCL,EACzCN,EACA,KACA3C,GAHKkD,EAAcI,EAAA,GAAEC,OAMjBC,EAAM,GAUZ,OARIN,EAAepE,QACjB0E,EAAIC,KACF,IAAIrC,EAAgB,GAAIuB,EAAO5C,EAAaC,EAASkD,IAIzDM,EAAIC,KAAIC,MAARF,EAAYD,GAEO,IAAfC,EAAI1E,OACC0E,EAAI,GAEN,IAAIvC,EAAe0B,EAAO5C,EAAaC,EAASwD,EACzD,EAEMP,EAAwB,SAC5BN,EACAI,EACA/C,GAEA,ID5Z8B/B,EC4ZxBiF,EAAiB,GACjBK,EAAmB,GACzB,KD9Z8BtF,EC8ZT0E,ID3ZlB1E,EAAMN,cAAgBb,QACrBmB,EAAMN,cAAgBV,OACW,wCAAjCgB,EAAMN,YAAYQ,YACe,uCAAjCF,EAAMN,YAAYQ,YACnBF,EAAMQ,OCyZP,OADAyE,EAAeO,KAAK,IAAI3B,EAAgBa,EAAOA,EAAO3C,IAC/C,CAACkD,EAAgBK,GAE1B,IAAK,IAAMrE,KAAOyD,EAChB,GAAI3C,EAAQuC,WAAWnF,eAAe8B,GAAM,CAC1C,IAAMyE,EAAKvB,EAAqBlD,EAAKyD,EAAMzD,GAAMyD,EAAO3C,GAExD,GAAI2D,IACGA,EAAGzC,QAAU6B,IAAc/C,EAAQuC,WAAWQ,GACjD,MAAM,IAAIN,MACR,2BAAoBvD,EAAG,yCAMnB,MAANyE,GACFT,EAAeO,KAAKE,EAEvB,KAA4B,MAAlBzE,EAAI0D,OAAO,GACpBJ,EAA0BtD,GAE1BqE,EAAiBE,KACfZ,EAAsB3D,EAAI0E,MAAM,KAAMjB,EAAMzD,GAAMA,EAAKyD,EAAO3C,IAKpE,MAAO,CAACkD,EAAgBK,EAC1B,EAEaM,EACX,SAAQC,GACR,OAAA,SAAC1E,EAAaF,EAAWM,GAGvB,OAFAsE,EAAU3D,QACV2D,EAAUxE,KAAKF,EAAMF,EAAKM,GACnBsE,EAAUzD,KAHnB,EChdF0D,EAAA,SAAAxD,GAAA,SAAAwD,yDACWtD,EAAMS,QAAG,GAenB,CAAD,OAhBkB5D,EAAkByG,EAAAxD,GAGlCwD,EAAA5G,UAAA+C,KAAA,WACExC,KAAKqE,EAAQT,EAAa5D,KAAKoC,OAAQpC,KAAKsC,QAAQuB,UAEtDwC,EAAA5G,UAAAgD,MAAA,WACEI,EAAKpD,UAACgD,MAAK9C,KAAAK,MACXA,KAAK2C,MAAO,GAEd0D,EAAI5G,UAAAmC,KAAJ,SAAKF,GACC1B,KAAKqE,EAAM3C,KACb1B,KAAK0C,MAAO,EACZ1C,KAAK2C,MAAO,IAGjB0D,CAAD,CAhBA,CAAkBlE,GAkBlBmE,EAAA,SAAAzD,GAAA,SAAAyD,yDACWvD,EAAMS,QAAG,GAiCnB,CAAD,OAlCyB5D,EAAyB0G,EAAAzD,GAGhDyD,EAAA7G,UAAA+C,KAAA,WACE,IAAKxC,KAAKoC,QAAiC,iBAAhBpC,KAAKoC,OAC9B,MAAM,IAAI2C,MAAM,kDAElB/E,KAAKuG,EAAkBd,EACrBzF,KAAKoC,OACLpC,KAAKqC,YACLrC,KAAKsC,UAGTgE,EAAA7G,UAAAgD,MAAA,WACEI,EAAKpD,UAACgD,MAAK9C,KAAAK,MACXA,KAAKuG,EAAgB9D,SAEvB6D,EAAI7G,UAAAmC,KAAJ,SAAKF,GACH,GAAIb,EAAQa,GAAO,CACjB,IAAS,IAAAL,EAAI,EAAKC,EAAWI,EAALN,OAAWC,EAAIC,EAAQD,IAAK,CAGlDrB,KAAKuG,EAAgB9D,QAErB,IAAM+D,EAAQ9E,EAAKL,GACnBrB,KAAKuG,EAAgB3E,KAAK4E,EAAOnF,EAAGK,GAAM,GAC1C1B,KAAK2C,KAAO3C,KAAK2C,MAAQ3C,KAAKuG,EAAgB5D,IAC/C,CACD3C,KAAK0C,MAAO,CACb,MACC1C,KAAK0C,MAAO,EACZ1C,KAAK2C,MAAO,GAGjB2D,CAAD,CAlCA,CAAyBnE,GAoCzBsE,EAAA,SAAA5D,GAAA,SAAA4D,yDACW1D,EAAMS,QAAG,GAkBnB,CAAD,OAnBmB5D,EAAyB6G,EAAA5D,GAG1C4D,EAAAhH,UAAA+C,KAAA,WACExC,KAAKuG,EAAkBd,EACrBzF,KAAKoC,OACLpC,KAAKqC,YACLrC,KAAKsC,UAGTmE,EAAAhH,UAAAgD,MAAA,WACEI,EAAKpD,UAACgD,MAAK9C,KAAAK,MACXA,KAAKuG,EAAgB9D,SAEvBgE,EAAIhH,UAAAmC,KAAJ,SAAKF,EAAWF,EAAUM,EAAYoB,GACpClD,KAAKuG,EAAgB3E,KAAKF,EAAMF,EAAKM,EAAOoB,GAC5ClD,KAAK0C,KAAO1C,KAAKuG,EAAgB7D,KACjC1C,KAAK2C,MAAQ3C,KAAKuG,EAAgB5D,MAErC8D,CAAD,CAnBA,CAAmBtE,GAqBnBuE,EAAA,SAAA7D,GAAA,SAAA6D,yDACW3D,EAAMS,QAAG,GAYnB,CAAD,OAb2B5D,EAAkB8G,EAAA7D,GAE3C6D,EAAIjH,UAAA+C,KAAJ,aACAkE,EAAIjH,UAAAmC,KAAJ,SAAKF,GACCb,EAAQa,IAASA,EAAKN,SAAWpB,KAAKoC,SACxCpC,KAAK0C,MAAO,EACZ1C,KAAK2C,MAAO,IAOjB+D,CAAD,CAbA,CAA2BvE,GAerBwE,EAAsB,SAACC,GAC3B,GAAsB,IAAlBA,EAAOxF,OACT,MAAM,IAAI2D,MAAM,yCAEpB,EAEA8B,EAAA,SAAAhE,GAAA,SAAAgE,yDACW9D,EAAMS,QAAG,GA+BnB,CAAD,OAhCkB5D,EAAkBiH,EAAAhE,GAGlCgE,EAAApH,UAAA+C,KAAA,WAAA,IAKCO,EAAA/C,KAJC2G,EAAoB3G,KAAKoC,QACzBpC,KAAK8G,EAAO9G,KAAKoC,OAAOtB,KAAI,SAACmF,GAC3B,OAAAR,EAAqBQ,EAAI,KAAMlD,EAAKT,QAApC,KAGJuE,EAAApH,UAAAgD,MAAA,WACEzC,KAAK0C,MAAO,EACZ1C,KAAK2C,MAAO,EACZ,IAAS,IAAAtB,EAAI,EAAK2B,EAAWhD,KAAK8G,EAAV1F,OAAgBC,EAAI2B,EAAQ3B,IAClDrB,KAAK8G,EAAKzF,GAAGoB,SAGjBoE,EAAApH,UAAAmC,KAAA,SAAKF,EAAWF,EAAUM,GAGxB,IAFA,IAAIY,GAAO,EACPqE,GAAU,EACL1F,EAAI,EAAK+B,EAAWpD,KAAK8G,EAAV1F,OAAgBC,EAAI+B,EAAQ/B,IAAK,CACvD,IAAM4E,EAAKjG,KAAK8G,EAAKzF,GAErB,GADA4E,EAAGrE,KAAKF,EAAMF,EAAKM,GACfmE,EAAGtD,KAAM,CACXD,GAAO,EACPqE,EAAUd,EAAGtD,KACb,KACD,CACF,CAED3C,KAAK2C,KAAOoE,EACZ/G,KAAK0C,KAAOA,GAEfmE,CAAD,CAhCA,CAAkB1E,GAkClB6E,EAAA,SAAAnE,GAAA,SAAAmE,yDACWjE,EAAMS,QAAG,GAKnB,CAAD,OANmB5D,EAAGoH,EAAAnE,GAEpBmE,EAAAvH,UAAAmC,KAAA,SAAKF,EAAWF,EAAUM,GACxBe,EAAKpD,UAACmC,KAAKjC,KAAAK,KAAA0B,EAAMF,EAAKM,GACtB9B,KAAK2C,MAAQ3C,KAAK2C,MAErBqE,CAAD,CANA,CAAmBH,GAQnBI,EAAA,SAAApE,GAAA,SAAAoE,yDACWlE,EAAMS,QAAG,GA0BnB,CAAD,OA3BkB5D,EAAkBqH,EAAApE,GAGlCoE,EAAAxH,UAAA+C,KAAA,WAAA,IAQCO,EAAA/C,KAPOoC,EAAS7C,MAAMsB,QAAQb,KAAKoC,QAAUpC,KAAKoC,OAAS,CAACpC,KAAKoC,QAChEpC,KAAKkH,EAAW9E,EAAOtB,KAAI,SAACP,GAC1B,GAAIyE,EAAkBzE,EAAOwC,EAAKT,SAChC,MAAM,IAAIyC,MAAM,uBAAAoC,OAAuBpE,EAAKR,KAAK6E,gBAEnD,OAAOxD,EAAarD,EAAOwC,EAAKT,QAAQuB,QAC1C,KAEFoD,EAAAxH,UAAAmC,KAAA,SAAKF,EAAWF,EAAUM,GAGxB,IAFA,IAAIY,GAAO,EACPqE,GAAU,EACL1F,EAAI,EAAKgG,EAAWrH,KAAKkH,EAAV9F,OAAoBC,EAAIgG,EAAQhG,IAAK,CAE3D,IAAI4C,EADSjE,KAAKkH,EAAS7F,IAClBK,GAAO,CACdgB,GAAO,EACPqE,GAAU,EACV,KACD,CACF,CAED/G,KAAK2C,KAAOoE,EACZ/G,KAAK0C,KAAOA,GAEfuE,CAAD,CA3BA,CAAkB9E,GA6BlBmF,EAAA,SAAAzE,GAGE,SAAAyE,EAAYlF,EAAamF,EAAiBjF,EAAkBC,GAC1D,IAAAQ,EAAAF,EAAMlD,KAAAK,KAAAoC,EAAQmF,EAAYjF,EAASC,IAAMvC,YAHlC+C,EAAMS,QAAG,EAIhBT,EAAKyE,EAAM,IAAIP,EAAI7E,EAAQmF,EAAYjF,EAASC,IACjD,CAqBH,OA3BmB3C,EAAkB0H,EAAAzE,GAOnCyE,EAAI7H,UAAAmC,KAAJ,SAAKF,EAAWF,EAAUM,EAAYoB,GACpClD,KAAKwH,EAAI5F,KAAKF,EAAMF,EAAKM,GAErBjB,EAAQiB,KAAWoB,EACjBlD,KAAKwH,EAAI7E,MACX3C,KAAK2C,MAAO,EACZ3C,KAAK0C,MAAO,GACHlB,GAAOM,EAAMV,OAAS,IAC/BpB,KAAK2C,MAAO,EACZ3C,KAAK0C,MAAO,IAGd1C,KAAK2C,MAAQ3C,KAAKwH,EAAI7E,KACtB3C,KAAK0C,MAAO,IAGhB4E,EAAA7H,UAAAgD,MAAA,WACEI,EAAKpD,UAACgD,MAAK9C,KAAAK,MACXA,KAAKwH,EAAI/E,SAEZ6E,CAAD,CA3BA,CAAmBnF,GA6BnBsF,EAAA,SAAA5E,GAAA,SAAA4E,yDACW1E,EAAMS,QAAG,GAUnB,CAAD,OAXsB5D,EAAsB6H,EAAA5E,GAE1C4E,EAAIhI,UAAAmC,KAAJ,SAAKF,EAAWF,EAAUM,EAAYoB,EAAeC,GAC9CA,EAGMrB,EAAMpC,eAAe8B,KAASxB,KAAKoC,SAC5CpC,KAAK0C,MAAO,EACZ1C,KAAK2C,MAAO,IAJZ3C,KAAK0C,MAAO,EACZ1C,KAAK2C,MAAQ3C,KAAKoC,SAMvBqF,CAAD,CAXA,CAAsBtF,GAatBuF,EAAA,SAAA7E,GAEE,SAAA6E,EACEtF,EACAC,EACAC,EACAC,GAEA,IAAAQ,EAAAF,EAAKlD,KAAAK,KACHoC,EACAC,EACAC,EACAF,EAAOtB,KAAI,SAACmE,GAAU,OAAAQ,EAAqBR,EAAO5C,EAAaC,MAC/DC,IACAvC,YAbK+C,EAAMS,QAAG,EAehBmD,EAAoBvE,IACrB,CAIH,OArBmBxC,EAAmB8H,EAAA7E,GAkBpC6E,EAAIjI,UAAAmC,KAAJ,SAAKF,EAAWF,EAAUM,EAAYoB,GACpClD,KAAKiD,aAAavB,EAAMF,EAAKM,EAAOoB,IAEvCwE,CAAD,CArBA,CAAmBpE,GAuBnBqE,EAAA,SAAA9E,GAEE,SAAA8E,EACEvF,EACAC,EACAC,EACAC,GAEA,IAAAQ,EAAAF,EAAKlD,KAAAK,KACHoC,EACAC,EACAC,EACAF,EAAOtB,KAAI,SAACmE,GAAU,OAAAQ,EAAqBR,EAAO5C,EAAaC,MAC/DC,IACAvC,YAbK+C,EAAMS,QAAG,GAcjB,CAIH,OAnBmB5D,EAAmB+H,EAAA9E,GAgBpC8E,EAAIlI,UAAAmC,KAAJ,SAAKF,EAAWF,EAAUM,EAAYoB,GACpClD,KAAKiD,aAAavB,EAAMF,EAAKM,EAAOoB,IAEvCyE,CAAD,CAnBA,CAAmBrE,GAqBNsE,EAAM,SAACxF,EAAaC,EAAyBC,GACxD,OAAA,IAAI8B,EAAgBhC,EAAQC,EAAaC,EAAzC,EACWuF,EAAM,SACjBzF,EACAC,EACAC,EACAC,GACG,OAAA,IAAI8D,EAAIjE,EAAQC,EAAaC,EAASC,EAAM,EACpCuF,EAAM,SACjB1F,EACAC,EACAC,EACAC,GACG,OAAA,IAAIsE,EAAIzE,EAAQC,EAAaC,EAASC,EAAM,EACpCwF,EAAO,SAClB3F,EACAC,EACAC,EACAC,GACG,OAAA,IAAIyE,EAAK5E,EAAQC,EAAaC,EAASC,EAAM,EACrCyF,EAAa,SACxB5F,EACAC,EACAC,EACAC,GACG,OAAA,IAAI+D,EAAWlE,EAAQC,EAAaC,EAASC,EAAM,EAC3C0F,EAAO,SAClB7F,EACAC,EACAC,EACAC,GACG,OAAA,IAAI+E,EAAKlF,EAAQC,EAAaC,EAASC,EAAM,EACrC2F,EAAM,SACjB9F,EACAC,EACAC,EACAC,GAEA,OAAO,IAAI0E,EAAI7E,EAAQC,EAAaC,EAASC,EAC/C,EAEa4F,EAAM7D,GAAmB,SAAClC,GAAW,OAAA,SAACjD,GACjD,OAAY,MAALA,GAAaA,EAAIiD,CAC1B,KACagG,EAAO9D,GAAmB,SAAClC,GAAW,OAAA,SAACjD,GAClD,OAAOA,IAAMiD,GAAUjD,GAAKiD,CAC9B,KACaiG,EAAM/D,GAAmB,SAAClC,GAAW,OAAA,SAACjD,GACjD,OAAY,MAALA,GAAaA,EAAIiD,CAC1B,KACakG,EAAOhE,GAAmB,SAAClC,GAAW,OAAA,SAACjD,GAClD,OAAOA,IAAMiD,GAAUjD,GAAKiD,CAC9B,KACamG,EAAO,SAClBjD,EACAjD,EACAC,OAFCkG,EAAGlD,EAAA,GAAEmD,EAAWnD,EAAA,GAIjB,OAAA,IAAIlB,GACF,SAACjF,GAAM,OAAAuB,EAAWvB,GAAKqJ,IAAQC,CAAW,GAC1CpG,EACAC,EAHF,EAKWoG,EAAU,SACrBtG,EACAC,EACAC,EACAC,GACG,OAAA,IAAIkF,EAAQrF,EAAQC,EAAaC,EAASC,EAAM,EACxCoG,EAAS,SACpBC,EACAvG,EACAC,GAEA,OAAA,IAAI8B,EACF,IAAIL,OAAO6E,EAASvG,EAAYwG,UAChCxG,EACAC,EAHF,EAKWwG,EAAO,SAClB1G,EACAC,EACAC,EACAC,GACG,OAAA,IAAIkE,EAAKrE,EAAQC,EAAaC,EAASC,EAAM,EAE5CwG,EAAc,CAClBC,OAAQ,SAACC,GAAM,MAAa,iBAANA,CAAc,EACpCC,OAAQ,SAACD,GAAM,MAAa,iBAANA,CAAc,EACpCE,KAAM,SAACF,GAAM,MAAa,kBAANA,CAAe,EACnCG,MAAO,SAACH,GAAM,OAAA1J,MAAMsB,QAAQoI,EAAE,EAC9BI,KAAM,SAACJ,GAAM,OAAM,OAANA,CAAU,EACvBK,UAAW,SAACL,GAAM,OAAAA,aAAatI,IAAI,GAGxB4I,GAAQ,SACnBC,EACAnH,EACAC,GAEA,OAAA,IAAI8B,GACF,SAACjF,GACC,GAAqB,iBAAVqK,EAAoB,CAC7B,IAAKT,EAAYS,GACf,MAAM,IAAIzE,MAAM,6BAGlB,OAAOgE,EAAYS,GAAOrK,EAC3B,CAED,OAAY,MAALA,IAAYA,aAAaqK,GAASrK,EAAEc,cAAgBuJ,EAC7D,GACAnH,EACAC,EAbF,EAeWmH,GAAO,SAClBrH,EACAmF,EACAjF,EACAC,GACG,OAAA,IAAImF,EAAKtF,EAAQmF,EAAYjF,EAASC,EAAM,EAEpCmH,GAAO,SAClBtH,EACAmF,EACAjF,EACAC,GACG,OAAA,IAAIoF,EAAKvF,EAAQmF,EAAYjF,EAASC,EAAM,EACpCoH,GAAQ,SACnBvH,EACAmF,EACAjF,GACG,OAAA,IAAIoE,EAAMtE,EAAQmF,EAAYjF,EAAS,QAAS,EACxCuG,GAAW,WAAM,OAAA,IAAK,EACtBe,GAAS,SACpBxH,EACAmF,EACAjF,GAEA,IAAI2B,EAEJ,GAAIhD,EAAWmB,GACb6B,EAAO7B,MACF,IAAKyH,QAAQC,IAAIC,YAGtB,MAAM,IAAIhF,MACR,oEAHFd,EAAO,IAAIH,SAAS,MAAO,UAAY1B,EAKxC,CAED,OAAO,IAAIgC,GAAgB,SAACjF,GAAM,OAAA8E,EAAK+F,KAAK7K,EAAV8E,CAAa9E,EAAE,GAAEoI,EAAYjF,EACjE,mNCpZM2H,GAA8B,SAClChF,EACAsC,EACAjC,GAAA,IAAAI,OAAA,IAAAJ,EAA4C,CAAA,EAAEA,EAA5CzB,EAAO6B,EAAA7B,QAAEgB,EAAUa,EAAAb,WAErB,OAAOY,EAAqBR,EAAOsC,EAAY,CAC7C1D,QAAOA,EACPgB,WAAYzF,OAAOuG,OAAO,CAAE,EAAEuE,GAAmBrF,GAAc,KAEnE,ySFgSqC,SACnCzC,EACAC,EACAC,GACG,OAAA,IAAI8B,EAAgBhC,EAAQC,EAAaC,EAAS,yEAwKtB,SAC/B2C,EACA3C,GAEA,YAFA,IAAAA,IAAAA,EAA8B,CAAA,GAEvB6D,EACLV,EAAqCR,EAAO,KAAM3C,GAEtD,YEjdiC,SAC/B2C,EACA3C,QAAA,IAAAA,IAAAA,EAA8B,CAAA,GAE9B,IAAM2D,EAAKgE,GAA4BhF,EAAO,KAAM3C,GACpD,OAAO6D,EAAsBF,EAC/B","x_google_ignoreList":[0]} \ No newline at end of file diff --git a/gateway/node_modules/sift/src/core.ts b/gateway/node_modules/sift/src/core.ts new file mode 100644 index 0000000000000000000000000000000000000000..68ecea9babf063341b61dcb4fc3d893ecabe15b8 --- /dev/null +++ b/gateway/node_modules/sift/src/core.ts @@ -0,0 +1,494 @@ +import { + isArray, + Key, + Comparator, + isVanillaObject, + comparable, + equals, + coercePotentiallyNull, + isProperty, +} from "./utils"; + +export interface Operation { + readonly keep: boolean; + readonly done: boolean; + propop: boolean; + reset(); + next(item: TItem, key?: Key, owner?: any, root?: boolean, leaf?: boolean); +} + +export type Tester = ( + item: any, + key?: Key, + owner?: any, + root?: boolean, + leaf?: boolean, +) => boolean; + +export interface NamedOperation { + name: string; +} + +export type OperationCreator = ( + params: any, + parentQuery: any, + options: Options, + name: string, +) => Operation; + +export type BasicValueQuery = { + $eq?: TValue; + $ne?: TValue; + $lt?: TValue; + $gt?: TValue; + $lte?: TValue; + $gte?: TValue; + $in?: TValue[]; + $nin?: TValue[]; + $all?: TValue[]; + $mod?: [number, number]; + $exists?: boolean; + $regex?: string | RegExp; + $size?: number; + $where?: ((this: TValue, obj: TValue) => boolean) | string; + $options?: "i" | "g" | "m" | "u"; + $type?: Function; + $not?: NestedQuery; + $or?: NestedQuery[]; + $nor?: NestedQuery[]; + $and?: NestedQuery[]; +}; + +export type ArrayValueQuery = { + $elemMatch?: Query; +} & BasicValueQuery; +type Unpacked = T extends (infer U)[] ? U : T; + +export type ValueQuery = + TValue extends Array + ? ArrayValueQuery> + : BasicValueQuery; + +type NotObject = string | number | Date | boolean | Array; +export type ShapeQuery = TItemSchema extends NotObject + ? {} + : { [k in keyof TItemSchema]?: TItemSchema[k] | ValueQuery }; + +export type NestedQuery = ValueQuery & + ShapeQuery; +export type Query = + | TItemSchema + | RegExp + | NestedQuery; + +export type QueryOperators = keyof ValueQuery; + +/** + * Walks through each value given the context - used for nested operations. E.g: + * { "person.address": { $eq: "blarg" }} + */ + +const walkKeyPathValues = ( + item: any, + keyPath: Key[], + next: Tester, + depth: number, + key: Key, + owner: any, +) => { + const currentKey = keyPath[depth]; + + // if array, then try matching. Might fall through for cases like: + // { $eq: [1, 2, 3] }, [ 1, 2, 3 ]. + if ( + isArray(item) && + isNaN(Number(currentKey)) && + !isProperty(item, currentKey) + ) { + for (let i = 0, { length } = item; i < length; i++) { + // if FALSE is returned, then terminate walker. For operations, this simply + // means that the search critera was met. + if (!walkKeyPathValues(item[i], keyPath, next, depth, i, item)) { + return false; + } + } + } + + if (depth === keyPath.length || item == null) { + return next(item, key, owner, depth === 0, depth === keyPath.length); + } + + return walkKeyPathValues( + item[currentKey], + keyPath, + next, + depth + 1, + currentKey, + item, + ); +}; + +export abstract class BaseOperation + implements Operation +{ + keep: boolean; + done: boolean; + abstract propop: boolean; + constructor( + readonly params: TParams, + readonly owneryQuery: any, + readonly options: Options, + readonly name?: string, + ) { + this.init(); + } + protected init() {} + reset() { + this.done = false; + this.keep = false; + } + abstract next( + item: any, + key: Key, + parent: any, + root: boolean, + leaf?: boolean, + ); +} + +abstract class GroupOperation extends BaseOperation { + keep: boolean; + done: boolean; + + constructor( + params: any, + owneryQuery: any, + options: Options, + public readonly children: Operation[], + ) { + super(params, owneryQuery, options); + } + + /** + */ + + reset() { + this.keep = false; + this.done = false; + for (let i = 0, { length } = this.children; i < length; i++) { + this.children[i].reset(); + } + } + + abstract next(item: any, key: Key, owner: any, root: boolean); + + /** + */ + + protected childrenNext( + item: any, + key: Key, + owner: any, + root: boolean, + leaf?: boolean, + ) { + let done = true; + let keep = true; + for (let i = 0, { length } = this.children; i < length; i++) { + const childOperation = this.children[i]; + if (!childOperation.done) { + childOperation.next(item, key, owner, root, leaf); + } + if (!childOperation.keep) { + keep = false; + } + if (childOperation.done) { + if (!childOperation.keep) { + break; + } + } else { + done = false; + } + } + this.done = done; + this.keep = keep; + } +} + +export abstract class NamedGroupOperation + extends GroupOperation + implements NamedOperation +{ + abstract propop: boolean; + constructor( + params: any, + owneryQuery: any, + options: Options, + children: Operation[], + readonly name: string, + ) { + super(params, owneryQuery, options, children); + } +} + +export class QueryOperation extends GroupOperation { + readonly propop = true; + /** + */ + + next(item: TItem, key: Key, parent: any, root: boolean) { + this.childrenNext(item, key, parent, root); + } +} + +export class NestedOperation extends GroupOperation { + readonly propop = true; + constructor( + readonly keyPath: Key[], + params: any, + owneryQuery: any, + options: Options, + children: Operation[], + ) { + super(params, owneryQuery, options, children); + } + /** + */ + + next(item: any, key: Key, parent: any) { + walkKeyPathValues( + item, + this.keyPath, + this._nextNestedValue, + 0, + key, + parent, + ); + } + + /** + */ + + private _nextNestedValue = ( + value: any, + key: Key, + owner: any, + root: boolean, + leaf: boolean, + ) => { + this.childrenNext(value, key, owner, root, leaf); + return !this.done; + }; +} + +export const createTester = (a, compare: Comparator) => { + if (a instanceof Function) { + return a; + } + if (a instanceof RegExp) { + return (b) => { + const result = typeof b === "string" && a.test(b); + a.lastIndex = 0; + return result; + }; + } + const comparableA = comparable(a); + return (b) => compare(comparableA, comparable(b)); +}; + +export class EqualsOperation extends BaseOperation { + readonly propop = true; + private _test: Tester; + init() { + this._test = createTester(this.params, this.options.compare); + } + next(item, key: Key, parent: any) { + if (!Array.isArray(parent) || parent.hasOwnProperty(key)) { + if (this._test(item, key, parent)) { + this.done = true; + this.keep = true; + } + } + } +} + +export const createEqualsOperation = ( + params: any, + owneryQuery: any, + options: Options, +) => new EqualsOperation(params, owneryQuery, options); + +export const numericalOperationCreator = + (createNumericalOperation: OperationCreator) => + (params: any, owneryQuery: any, options: Options, name: string) => { + return createNumericalOperation(params, owneryQuery, options, name); + }; + +export const numericalOperation = (createTester: (value: any) => Tester) => + numericalOperationCreator( + (params: any, owneryQuery: Query, options: Options, name: string) => { + const typeofParams = typeof comparable(params); + const test = createTester(params); + return new EqualsOperation( + (b) => { + const actualValue = coercePotentiallyNull(b); + return ( + typeof comparable(actualValue) === typeofParams && test(actualValue) + ); + }, + owneryQuery, + options, + name, + ); + }, + ); + +export type Options = { + operations: { + [identifier: string]: OperationCreator; + }; + compare: (a, b) => boolean; +}; + +const createNamedOperation = ( + name: string, + params: any, + parentQuery: any, + options: Options, +) => { + const operationCreator = options.operations[name]; + if (!operationCreator) { + throwUnsupportedOperation(name); + } + return operationCreator(params, parentQuery, options, name); +}; + +const throwUnsupportedOperation = (name: string) => { + throw new Error(`Unsupported operation: ${name}`); +}; + +export const containsOperation = (query: any, options: Options) => { + for (const key in query) { + if (options.operations.hasOwnProperty(key) || key.charAt(0) === "$") + return true; + } + return false; +}; +const createNestedOperation = ( + keyPath: Key[], + nestedQuery: any, + parentKey: string, + owneryQuery: any, + options: Options, +) => { + if (containsOperation(nestedQuery, options)) { + const [selfOperations, nestedOperations] = createQueryOperations( + nestedQuery, + parentKey, + options, + ); + if (nestedOperations.length) { + throw new Error( + `Property queries must contain only operations, or exact objects.`, + ); + } + return new NestedOperation( + keyPath, + nestedQuery, + owneryQuery, + options, + selfOperations, + ); + } + return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, [ + new EqualsOperation(nestedQuery, owneryQuery, options), + ]); +}; + +export const createQueryOperation = ( + query: Query, + owneryQuery: any = null, + { compare, operations }: Partial = {}, +): QueryOperation => { + const options = { + compare: compare || equals, + operations: Object.assign({}, operations || {}), + }; + + const [selfOperations, nestedOperations] = createQueryOperations( + query, + null, + options, + ); + + const ops = []; + + if (selfOperations.length) { + ops.push( + new NestedOperation([], query, owneryQuery, options, selfOperations), + ); + } + + ops.push(...nestedOperations); + + if (ops.length === 1) { + return ops[0]; + } + return new QueryOperation(query, owneryQuery, options, ops); +}; + +const createQueryOperations = ( + query: any, + parentKey: string, + options: Options, +) => { + const selfOperations = []; + const nestedOperations = []; + if (!isVanillaObject(query)) { + selfOperations.push(new EqualsOperation(query, query, options)); + return [selfOperations, nestedOperations]; + } + for (const key in query) { + if (options.operations.hasOwnProperty(key)) { + const op = createNamedOperation(key, query[key], query, options); + + if (op) { + if (!op.propop && parentKey && !options.operations[parentKey]) { + throw new Error( + `Malformed query. ${key} cannot be matched against property.`, + ); + } + } + + // probably just a flag for another operation (like $options) + if (op != null) { + selfOperations.push(op); + } + } else if (key.charAt(0) === "$") { + throwUnsupportedOperation(key); + } else { + nestedOperations.push( + createNestedOperation(key.split("."), query[key], key, query, options), + ); + } + } + + return [selfOperations, nestedOperations]; +}; + +export const createOperationTester = + (operation: Operation) => + (item: TItem, key?: Key, owner?: any) => { + operation.reset(); + operation.next(item, key, owner); + return operation.keep; + }; + +export const createQueryTester = ( + query: Query, + options: Partial = {}, +) => { + return createOperationTester( + createQueryOperation(query, null, options), + ); +}; diff --git a/gateway/node_modules/sift/src/index.ts b/gateway/node_modules/sift/src/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..ea733b9a487843d020c840e328a4abd28524f8b9 --- /dev/null +++ b/gateway/node_modules/sift/src/index.ts @@ -0,0 +1,54 @@ +import * as defaultOperations from "./operations"; +import { + Query, + QueryOperators, + BasicValueQuery, + ArrayValueQuery, + ValueQuery, + NestedQuery, + ShapeQuery, + Options, + createQueryTester, + EqualsOperation, + createQueryOperation, + createEqualsOperation, + createOperationTester, +} from "./core"; + +const createDefaultQueryOperation = ( + query: Query, + ownerQuery: any, + { compare, operations }: Partial = {}, +) => { + return createQueryOperation(query, ownerQuery, { + compare, + operations: Object.assign({}, defaultOperations, operations || {}), + }); +}; + +const createDefaultQueryTester = ( + query: Query, + options: Partial = {}, +) => { + const op = createDefaultQueryOperation(query, null, options); + return createOperationTester(op); +}; + +export { + Query, + QueryOperators, + BasicValueQuery, + ArrayValueQuery, + ValueQuery, + NestedQuery, + ShapeQuery, + EqualsOperation, + createQueryTester, + createOperationTester, + createDefaultQueryOperation, + createEqualsOperation, + createQueryOperation, +}; +export * from "./operations"; + +export default createDefaultQueryTester; diff --git a/gateway/node_modules/sift/src/operations.ts b/gateway/node_modules/sift/src/operations.ts new file mode 100644 index 0000000000000000000000000000000000000000..68177b1a73ef778ffd35124451163244c1eb071c --- /dev/null +++ b/gateway/node_modules/sift/src/operations.ts @@ -0,0 +1,422 @@ +import { + BaseOperation, + EqualsOperation, + Options, + createTester, + Tester, + createQueryOperation, + QueryOperation, + Operation, + Query, + NamedGroupOperation, + numericalOperation, + containsOperation, +} from "./core"; +import { Key, comparable, isFunction, isArray } from "./utils"; + +class $Ne extends BaseOperation { + readonly propop = true; + private _test: Tester; + init() { + this._test = createTester(this.params, this.options.compare); + } + reset() { + super.reset(); + this.keep = true; + } + next(item: any) { + if (this._test(item)) { + this.done = true; + this.keep = false; + } + } +} +// https://docs.mongodb.com/manual/reference/operator/query/elemMatch/ +class $ElemMatch extends BaseOperation> { + readonly propop = true; + private _queryOperation: QueryOperation; + init() { + if (!this.params || typeof this.params !== "object") { + throw new Error(`Malformed query. $elemMatch must by an object.`); + } + this._queryOperation = createQueryOperation( + this.params, + this.owneryQuery, + this.options, + ); + } + reset() { + super.reset(); + this._queryOperation.reset(); + } + next(item: any) { + if (isArray(item)) { + for (let i = 0, { length } = item; i < length; i++) { + // reset query operation since item being tested needs to pass _all_ query + // operations for it to be a success + this._queryOperation.reset(); + + const child = item[i]; + this._queryOperation.next(child, i, item, false); + this.keep = this.keep || this._queryOperation.keep; + } + this.done = true; + } else { + this.done = false; + this.keep = false; + } + } +} + +class $Not extends BaseOperation> { + readonly propop = true; + private _queryOperation: QueryOperation; + init() { + this._queryOperation = createQueryOperation( + this.params, + this.owneryQuery, + this.options, + ); + } + reset() { + super.reset(); + this._queryOperation.reset(); + } + next(item: any, key: Key, owner: any, root: boolean) { + this._queryOperation.next(item, key, owner, root); + this.done = this._queryOperation.done; + this.keep = !this._queryOperation.keep; + } +} + +export class $Size extends BaseOperation { + readonly propop = true; + init() {} + next(item) { + if (isArray(item) && item.length === this.params) { + this.done = true; + this.keep = true; + } + // if (parent && parent.length === this.params) { + // this.done = true; + // this.keep = true; + // } + } +} + +const assertGroupNotEmpty = (values: any[]) => { + if (values.length === 0) { + throw new Error(`$and/$or/$nor must be a nonempty array`); + } +}; + +class $Or extends BaseOperation { + readonly propop = false; + private _ops: Operation[]; + init() { + assertGroupNotEmpty(this.params); + this._ops = this.params.map((op) => + createQueryOperation(op, null, this.options), + ); + } + reset() { + this.done = false; + this.keep = false; + for (let i = 0, { length } = this._ops; i < length; i++) { + this._ops[i].reset(); + } + } + next(item: any, key: Key, owner: any) { + let done = false; + let success = false; + for (let i = 0, { length } = this._ops; i < length; i++) { + const op = this._ops[i]; + op.next(item, key, owner); + if (op.keep) { + done = true; + success = op.keep; + break; + } + } + + this.keep = success; + this.done = done; + } +} + +class $Nor extends $Or { + readonly propop = false; + next(item: any, key: Key, owner: any) { + super.next(item, key, owner); + this.keep = !this.keep; + } +} + +class $In extends BaseOperation { + readonly propop = true; + private _testers: Tester[]; + init() { + const params = Array.isArray(this.params) ? this.params : [this.params]; + this._testers = params.map((value) => { + if (containsOperation(value, this.options)) { + throw new Error(`cannot nest $ under ${this.name.toLowerCase()}`); + } + return createTester(value, this.options.compare); + }); + } + next(item: any, key: Key, owner: any) { + let done = false; + let success = false; + for (let i = 0, { length } = this._testers; i < length; i++) { + const test = this._testers[i]; + if (test(item)) { + done = true; + success = true; + break; + } + } + + this.keep = success; + this.done = done; + } +} + +class $Nin extends BaseOperation { + readonly propop = true; + private _in: $In; + constructor(params: any, ownerQuery: any, options: Options, name: string) { + super(params, ownerQuery, options, name); + this._in = new $In(params, ownerQuery, options, name); + } + next(item: any, key: Key, owner: any, root: boolean) { + this._in.next(item, key, owner); + + if (isArray(owner) && !root) { + if (this._in.keep) { + this.keep = false; + this.done = true; + } else if (key == owner.length - 1) { + this.keep = true; + this.done = true; + } + } else { + this.keep = !this._in.keep; + this.done = true; + } + } + reset() { + super.reset(); + this._in.reset(); + } +} + +class $Exists extends BaseOperation { + readonly propop = true; + next(item: any, key: Key, owner: any, root: boolean, leaf?: boolean) { + if (!leaf) { + this.done = true; + this.keep = !this.params; + } else if (owner.hasOwnProperty(key) === this.params) { + this.done = true; + this.keep = true; + } + } +} + +class $And extends NamedGroupOperation { + readonly propop = false; + constructor( + params: Query[], + owneryQuery: Query, + options: Options, + name: string, + ) { + super( + params, + owneryQuery, + options, + params.map((query) => createQueryOperation(query, owneryQuery, options)), + name, + ); + + assertGroupNotEmpty(params); + } + next(item: any, key: Key, owner: any, root: boolean) { + this.childrenNext(item, key, owner, root); + } +} + +class $All extends NamedGroupOperation { + readonly propop = true; + constructor( + params: Query[], + owneryQuery: Query, + options: Options, + name: string, + ) { + super( + params, + owneryQuery, + options, + params.map((query) => createQueryOperation(query, owneryQuery, options)), + name, + ); + } + next(item: any, key: Key, owner: any, root: boolean) { + this.childrenNext(item, key, owner, root); + } +} + +export const $eq = (params: any, owneryQuery: Query, options: Options) => + new EqualsOperation(params, owneryQuery, options); +export const $ne = ( + params: any, + owneryQuery: Query, + options: Options, + name: string, +) => new $Ne(params, owneryQuery, options, name); +export const $or = ( + params: Query[], + owneryQuery: Query, + options: Options, + name: string, +) => new $Or(params, owneryQuery, options, name); +export const $nor = ( + params: Query[], + owneryQuery: Query, + options: Options, + name: string, +) => new $Nor(params, owneryQuery, options, name); +export const $elemMatch = ( + params: any, + owneryQuery: Query, + options: Options, + name: string, +) => new $ElemMatch(params, owneryQuery, options, name); +export const $nin = ( + params: any, + owneryQuery: Query, + options: Options, + name: string, +) => new $Nin(params, owneryQuery, options, name); +export const $in = ( + params: any, + owneryQuery: Query, + options: Options, + name: string, +) => { + return new $In(params, owneryQuery, options, name); +}; + +export const $lt = numericalOperation((params) => (b) => { + return b != null && b < params; +}); +export const $lte = numericalOperation((params) => (b) => { + return b === params || b <= params; +}); +export const $gt = numericalOperation((params) => (b) => { + return b != null && b > params; +}); +export const $gte = numericalOperation((params) => (b) => { + return b === params || b >= params; +}); +export const $mod = ( + [mod, equalsValue]: number[], + owneryQuery: Query, + options: Options, +) => + new EqualsOperation( + (b) => comparable(b) % mod === equalsValue, + owneryQuery, + options, + ); +export const $exists = ( + params: boolean, + owneryQuery: Query, + options: Options, + name: string, +) => new $Exists(params, owneryQuery, options, name); +export const $regex = ( + pattern: string, + owneryQuery: Query, + options: Options, +) => + new EqualsOperation( + new RegExp(pattern, owneryQuery.$options), + owneryQuery, + options, + ); +export const $not = ( + params: any, + owneryQuery: Query, + options: Options, + name: string, +) => new $Not(params, owneryQuery, options, name); + +const typeAliases = { + number: (v) => typeof v === "number", + string: (v) => typeof v === "string", + bool: (v) => typeof v === "boolean", + array: (v) => Array.isArray(v), + null: (v) => v === null, + timestamp: (v) => v instanceof Date, +}; + +export const $type = ( + clazz: Function | string, + owneryQuery: Query, + options: Options, +) => + new EqualsOperation( + (b) => { + if (typeof clazz === "string") { + if (!typeAliases[clazz]) { + throw new Error(`Type alias does not exist`); + } + + return typeAliases[clazz](b); + } + + return b != null ? b instanceof clazz || b.constructor === clazz : false; + }, + owneryQuery, + options, + ); +export const $and = ( + params: Query[], + ownerQuery: Query, + options: Options, + name: string, +) => new $And(params, ownerQuery, options, name); + +export const $all = ( + params: Query[], + ownerQuery: Query, + options: Options, + name: string, +) => new $All(params, ownerQuery, options, name); +export const $size = ( + params: number, + ownerQuery: Query, + options: Options, +) => new $Size(params, ownerQuery, options, "$size"); +export const $options = () => null; +export const $where = ( + params: string | Function, + ownerQuery: Query, + options: Options, +) => { + let test; + + if (isFunction(params)) { + test = params; + } else if (!process.env.CSP_ENABLED) { + test = new Function("obj", "return " + params); + } else { + throw new Error( + `In CSP mode, sift does not support strings in "$where" condition`, + ); + } + + return new EqualsOperation((b) => test.bind(b)(b), ownerQuery, options); +}; diff --git a/gateway/node_modules/sift/src/utils.ts b/gateway/node_modules/sift/src/utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..02e2a758027978b65e13340e2a9d7648cadbd5b8 --- /dev/null +++ b/gateway/node_modules/sift/src/utils.ts @@ -0,0 +1,74 @@ +export type Key = string | number; +export type Comparator = (a, b) => boolean; +export const typeChecker = (type) => { + const typeString = "[object " + type + "]"; + return function (value): value is TType { + return getClassName(value) === typeString; + }; +}; + +const getClassName = (value) => Object.prototype.toString.call(value); + +export const comparable = (value: any) => { + if (value instanceof Date) { + return value.getTime(); + } else if (isArray(value)) { + return value.map(comparable); + } else if (value && typeof value.toJSON === "function") { + return value.toJSON(); + } + + return value; +}; + +export const coercePotentiallyNull = (value: any) => + value == null ? null : value; + +export const isArray = typeChecker>("Array"); +export const isObject = typeChecker("Object"); +export const isFunction = typeChecker("Function"); +export const isProperty = (item: any, key: any) => { + return item.hasOwnProperty(key) && !isFunction(item[key]); +}; +export const isVanillaObject = (value) => { + return ( + value && + (value.constructor === Object || + value.constructor === Array || + value.constructor.toString() === "function Object() { [native code] }" || + value.constructor.toString() === "function Array() { [native code] }") && + !value.toJSON + ); +}; + +export const equals = (a, b) => { + if (a == null && a == b) { + return true; + } + if (a === b) { + return true; + } + + if (Object.prototype.toString.call(a) !== Object.prototype.toString.call(b)) { + return false; + } + + if (isArray(a)) { + if (a.length !== b.length) { + return false; + } + for (let i = 0, { length } = a; i < length; i++) { + if (!equals(a[i], b[i])) return false; + } + return true; + } else if (isObject(a)) { + if (Object.keys(a).length !== Object.keys(b).length) { + return false; + } + for (const key in a) { + if (!equals(a[key], b[key])) return false; + } + return true; + } + return false; +}; diff --git a/gateway/node_modules/sparse-bitfield/.npmignore b/gateway/node_modules/sparse-bitfield/.npmignore new file mode 100644 index 0000000000000000000000000000000000000000..3c3629e647f5ddf82548912e337bea9826b434af --- /dev/null +++ b/gateway/node_modules/sparse-bitfield/.npmignore @@ -0,0 +1 @@ +node_modules diff --git a/gateway/node_modules/sparse-bitfield/.travis.yml b/gateway/node_modules/sparse-bitfield/.travis.yml new file mode 100644 index 0000000000000000000000000000000000000000..c0428217037e472e73fc90e620d9f71bae940c31 --- /dev/null +++ b/gateway/node_modules/sparse-bitfield/.travis.yml @@ -0,0 +1,6 @@ +language: node_js +node_js: + - '0.10' + - '0.12' + - '4.0' + - '5.0' diff --git a/gateway/node_modules/sparse-bitfield/LICENSE b/gateway/node_modules/sparse-bitfield/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..bae9da7bfae2b5e22cfb0945b362b23ca822c8bb --- /dev/null +++ b/gateway/node_modules/sparse-bitfield/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Mathias Buus + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/gateway/node_modules/sparse-bitfield/README.md b/gateway/node_modules/sparse-bitfield/README.md new file mode 100644 index 0000000000000000000000000000000000000000..7b6b8f9ed33bc995571f0138ac1450b7420a6642 --- /dev/null +++ b/gateway/node_modules/sparse-bitfield/README.md @@ -0,0 +1,62 @@ +# sparse-bitfield + +Bitfield implementation that allocates a series of 1kb buffers to support sparse bitfields +without allocating a massive buffer. If you want to simple implementation of a flat bitfield +see the [bitfield](https://github.com/fb55/bitfield) module. + +This module is mostly useful if you need a big bitfield where you won't nessecarily set every bit. + +``` +npm install sparse-bitfield +``` + +[![build status](http://img.shields.io/travis/mafintosh/sparse-bitfield.svg?style=flat)](http://travis-ci.org/mafintosh/sparse-bitfield) + +## Usage + +``` js +var bitfield = require('sparse-bitfield') +var bits = bitfield() + +bits.set(0, true) // set first bit +bits.set(1, true) // set second bit +bits.set(1000000000000, true) // set the 1.000.000.000.000th bit +``` + +Running the above example will allocate two 1kb buffers internally. +Each 1kb buffer can hold information about 8192 bits so the first one will be used to store information about the first two bits and the second will be used to store the 1.000.000.000.000th bit. + +## API + +#### `var bits = bitfield([options])` + +Create a new bitfield. Options include + +``` js +{ + pageSize: 1024, // how big should the partial buffers be + buffer: anExistingBitfield, + trackUpdates: false // track when pages are being updated in the pager +} +``` + +#### `bits.set(index, value)` + +Set a bit to true or false. + +#### `bits.get(index)` + +Get the value of a bit. + +#### `bits.pages` + +A [memory-pager](https://github.com/mafintosh/memory-pager) instance that is managing the underlying memory. +If you set `trackUpdates` to true in the constructor you can use `.lastUpdate()` on this instance to get the last updated memory page. + +#### `var buffer = bits.toBuffer()` + +Get a single buffer representing the entire bitfield. + +## License + +MIT diff --git a/gateway/node_modules/sparse-bitfield/index.js b/gateway/node_modules/sparse-bitfield/index.js new file mode 100644 index 0000000000000000000000000000000000000000..ff458c974fcd4e6051b44b3ae2e4674c3ce70b01 --- /dev/null +++ b/gateway/node_modules/sparse-bitfield/index.js @@ -0,0 +1,95 @@ +var pager = require('memory-pager') + +module.exports = Bitfield + +function Bitfield (opts) { + if (!(this instanceof Bitfield)) return new Bitfield(opts) + if (!opts) opts = {} + if (Buffer.isBuffer(opts)) opts = {buffer: opts} + + this.pageOffset = opts.pageOffset || 0 + this.pageSize = opts.pageSize || 1024 + this.pages = opts.pages || pager(this.pageSize) + + this.byteLength = this.pages.length * this.pageSize + this.length = 8 * this.byteLength + + if (!powerOfTwo(this.pageSize)) throw new Error('The page size should be a power of two') + + this._trackUpdates = !!opts.trackUpdates + this._pageMask = this.pageSize - 1 + + if (opts.buffer) { + for (var i = 0; i < opts.buffer.length; i += this.pageSize) { + this.pages.set(i / this.pageSize, opts.buffer.slice(i, i + this.pageSize)) + } + this.byteLength = opts.buffer.length + this.length = 8 * this.byteLength + } +} + +Bitfield.prototype.get = function (i) { + var o = i & 7 + var j = (i - o) / 8 + + return !!(this.getByte(j) & (128 >> o)) +} + +Bitfield.prototype.getByte = function (i) { + var o = i & this._pageMask + var j = (i - o) / this.pageSize + var page = this.pages.get(j, true) + + return page ? page.buffer[o + this.pageOffset] : 0 +} + +Bitfield.prototype.set = function (i, v) { + var o = i & 7 + var j = (i - o) / 8 + var b = this.getByte(j) + + return this.setByte(j, v ? b | (128 >> o) : b & (255 ^ (128 >> o))) +} + +Bitfield.prototype.toBuffer = function () { + var all = alloc(this.pages.length * this.pageSize) + + for (var i = 0; i < this.pages.length; i++) { + var next = this.pages.get(i, true) + var allOffset = i * this.pageSize + if (next) next.buffer.copy(all, allOffset, this.pageOffset, this.pageOffset + this.pageSize) + } + + return all +} + +Bitfield.prototype.setByte = function (i, b) { + var o = i & this._pageMask + var j = (i - o) / this.pageSize + var page = this.pages.get(j, false) + + o += this.pageOffset + + if (page.buffer[o] === b) return false + page.buffer[o] = b + + if (i >= this.byteLength) { + this.byteLength = i + 1 + this.length = this.byteLength * 8 + } + + if (this._trackUpdates) this.pages.updated(page) + + return true +} + +function alloc (n) { + if (Buffer.alloc) return Buffer.alloc(n) + var b = new Buffer(n) + b.fill(0) + return b +} + +function powerOfTwo (x) { + return !(x & (x - 1)) +} diff --git a/gateway/node_modules/sparse-bitfield/package.json b/gateway/node_modules/sparse-bitfield/package.json new file mode 100644 index 0000000000000000000000000000000000000000..092a23f61733e43de499a599f591362523243032 --- /dev/null +++ b/gateway/node_modules/sparse-bitfield/package.json @@ -0,0 +1,27 @@ +{ + "name": "sparse-bitfield", + "version": "3.0.3", + "description": "Bitfield that allocates a series of small buffers to support sparse bits without allocating a massive buffer", + "main": "index.js", + "dependencies": { + "memory-pager": "^1.0.2" + }, + "devDependencies": { + "buffer-alloc": "^1.1.0", + "standard": "^9.0.0", + "tape": "^4.6.3" + }, + "scripts": { + "test": "standard && tape test.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/mafintosh/sparse-bitfield.git" + }, + "author": "Mathias Buus (@mafintosh)", + "license": "MIT", + "bugs": { + "url": "https://github.com/mafintosh/sparse-bitfield/issues" + }, + "homepage": "https://github.com/mafintosh/sparse-bitfield" +} diff --git a/gateway/node_modules/sparse-bitfield/test.js b/gateway/node_modules/sparse-bitfield/test.js new file mode 100644 index 0000000000000000000000000000000000000000..ae42ef467309493e2a01cc51d567fae98382b5d4 --- /dev/null +++ b/gateway/node_modules/sparse-bitfield/test.js @@ -0,0 +1,79 @@ +var alloc = require('buffer-alloc') +var tape = require('tape') +var bitfield = require('./') + +tape('set and get', function (t) { + var bits = bitfield() + + t.same(bits.get(0), false, 'first bit is false') + bits.set(0, true) + t.same(bits.get(0), true, 'first bit is true') + t.same(bits.get(1), false, 'second bit is false') + bits.set(0, false) + t.same(bits.get(0), false, 'first bit is reset') + t.end() +}) + +tape('set large and get', function (t) { + var bits = bitfield() + + t.same(bits.get(9999999999999), false, 'large bit is false') + bits.set(9999999999999, true) + t.same(bits.get(9999999999999), true, 'large bit is true') + t.same(bits.get(9999999999999 + 1), false, 'large bit + 1 is false') + bits.set(9999999999999, false) + t.same(bits.get(9999999999999), false, 'large bit is reset') + t.end() +}) + +tape('get and set buffer', function (t) { + var bits = bitfield({trackUpdates: true}) + + t.same(bits.pages.get(0, true), undefined) + t.same(bits.pages.get(Math.floor(9999999999999 / 8 / 1024), true), undefined) + bits.set(9999999999999, true) + + var bits2 = bitfield() + var upd = bits.pages.lastUpdate() + bits2.pages.set(Math.floor(upd.offset / 1024), upd.buffer) + t.same(bits2.get(9999999999999), true, 'bit is set') + t.end() +}) + +tape('toBuffer', function (t) { + var bits = bitfield() + + t.same(bits.toBuffer(), alloc(0)) + + bits.set(0, true) + + t.same(bits.toBuffer(), bits.pages.get(0).buffer) + + bits.set(9000, true) + + t.same(bits.toBuffer(), Buffer.concat([bits.pages.get(0).buffer, bits.pages.get(1).buffer])) + t.end() +}) + +tape('pass in buffer', function (t) { + var bits = bitfield() + + bits.set(0, true) + bits.set(9000, true) + + var clone = bitfield(bits.toBuffer()) + + t.same(clone.get(0), true) + t.same(clone.get(9000), true) + t.end() +}) + +tape('set small buffer', function (t) { + var buf = alloc(1) + buf[0] = 255 + var bits = bitfield(buf) + + t.same(bits.get(0), true) + t.same(bits.pages.get(0).buffer.length, bits.pageSize) + t.end() +}) diff --git a/gateway/node_modules/tr46/LICENSE.md b/gateway/node_modules/tr46/LICENSE.md new file mode 100644 index 0000000000000000000000000000000000000000..62c0de28a830d3ae3a006c9e1e7df19067e409a7 --- /dev/null +++ b/gateway/node_modules/tr46/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Sebastian Mayr + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/gateway/node_modules/tr46/README.md b/gateway/node_modules/tr46/README.md new file mode 100644 index 0000000000000000000000000000000000000000..7bd9ffda03f883c787a68e6fd1b7870f438457a2 --- /dev/null +++ b/gateway/node_modules/tr46/README.md @@ -0,0 +1,76 @@ +# tr46 + +An JavaScript implementation of [Unicode Technical Standard #46: Unicode IDNA Compatibility Processing](https://unicode.org/reports/tr46/). + +## API + +### `toASCII(domainName[, options])` + +Converts a string of Unicode symbols to a case-folded Punycode string of ASCII symbols. + +Available options: + +* [`checkBidi`](#checkbidi) +* [`checkHyphens`](#checkhyphens) +* [`checkJoiners`](#checkjoiners) +* [`ignoreInvalidPunycode`](#ignoreinvalidpunycode) +* [`transitionalProcessing`](#transitionalprocessing) +* [`useSTD3ASCIIRules`](#usestd3asciirules) +* [`verifyDNSLength`](#verifydnslength) + +### `toUnicode(domainName[, options])` + +Converts a case-folded Punycode string of ASCII symbols to a string of Unicode symbols. + +Available options: + +* [`checkBidi`](#checkbidi) +* [`checkHyphens`](#checkhyphens) +* [`checkJoiners`](#checkjoiners) +* [`ignoreInvalidPunycode`](#ignoreinvalidpunycode) +* [`transitionalProcessing`](#transitionalprocessing) +* [`useSTD3ASCIIRules`](#usestd3asciirules) + +## Options + +### `checkBidi` + +Type: `boolean` +Default value: `false` +When set to `true`, any bi-directional text within the input will be checked for validation. + +### `checkHyphens` + +Type: `boolean` +Default value: `false` +When set to `true`, the positions of any hyphen characters within the input will be checked for validation. + +### `checkJoiners` + +Type: `boolean` +Default value: `false` +When set to `true`, any word joiner characters within the input will be checked for validation. + +### `ignoreInvalidPunycode` + +Type: `boolean` +Default value: `false` +When set to `true`, invalid Punycode strings within the input will be allowed. + +### `transitionalProcessing` + +Type: `boolean` +Default value: `false` +When set to `true`, uses [transitional (compatibility) processing](https://unicode.org/reports/tr46/#Compatibility_Processing) of the deviation characters. + +### `useSTD3ASCIIRules` + +Type: `boolean` +Default value: `false` +When set to `true`, input will be validated according to [STD3 Rules](http://unicode.org/reports/tr46/#STD3_Rules). + +### `verifyDNSLength` + +Type: `boolean` +Default value: `false` +When set to `true`, the length of each DNS label within the input will be checked for validation. diff --git a/gateway/node_modules/tr46/index.js b/gateway/node_modules/tr46/index.js new file mode 100644 index 0000000000000000000000000000000000000000..9e53f0580b47260d8d272fa832d50f61589656e3 --- /dev/null +++ b/gateway/node_modules/tr46/index.js @@ -0,0 +1,344 @@ +"use strict"; + +const punycode = require("punycode/"); +const regexes = require("./lib/regexes.js"); +const mappingTable = require("./lib/mappingTable.json"); +const { STATUS_MAPPING } = require("./lib/statusMapping.js"); + +function containsNonASCII(str) { + return /[^\x00-\x7F]/u.test(str); +} + +function findStatus(val) { + let start = 0; + let end = mappingTable.length - 1; + + while (start <= end) { + const mid = Math.floor((start + end) / 2); + + const target = mappingTable[mid]; + const min = Array.isArray(target[0]) ? target[0][0] : target[0]; + const max = Array.isArray(target[0]) ? target[0][1] : target[0]; + + if (min <= val && max >= val) { + return target.slice(1); + } else if (min > val) { + end = mid - 1; + } else { + start = mid + 1; + } + } + + return null; +} + +function mapChars(domainName, { transitionalProcessing }) { + let processed = ""; + + for (const ch of domainName) { + const [status, mapping] = findStatus(ch.codePointAt(0)); + + switch (status) { + case STATUS_MAPPING.disallowed: + processed += ch; + break; + case STATUS_MAPPING.ignored: + break; + case STATUS_MAPPING.mapped: + if (transitionalProcessing && ch === "ẞ") { + processed += "ss"; + } else { + processed += mapping; + } + break; + case STATUS_MAPPING.deviation: + if (transitionalProcessing) { + processed += mapping; + } else { + processed += ch; + } + break; + case STATUS_MAPPING.valid: + processed += ch; + break; + } + } + + return processed; +} + +function validateLabel(label, { + checkHyphens, + checkBidi, + checkJoiners, + transitionalProcessing, + useSTD3ASCIIRules, + isBidi +}) { + // "must be satisfied for a non-empty label" + if (label.length === 0) { + return true; + } + + // "1. The label must be in Unicode Normalization Form NFC." + if (label.normalize("NFC") !== label) { + return false; + } + + const codePoints = Array.from(label); + + // "2. If CheckHyphens, the label must not contain a U+002D HYPHEN-MINUS character in both the + // third and fourth positions." + // + // "3. If CheckHyphens, the label must neither begin nor end with a U+002D HYPHEN-MINUS character." + if (checkHyphens) { + if ((codePoints[2] === "-" && codePoints[3] === "-") || + (label.startsWith("-") || label.endsWith("-"))) { + return false; + } + } + + // "4. If not CheckHyphens, the label must not begin with “xn--”." + if (!checkHyphens) { + if (label.startsWith("xn--")) { + return false; + } + } + + // "5. The label must not contain a U+002E ( . ) FULL STOP." + if (label.includes(".")) { + return false; + } + + // "6. The label must not begin with a combining mark, that is: General_Category=Mark." + if (regexes.combiningMarks.test(codePoints[0])) { + return false; + } + + // "7. Each code point in the label must only have certain Status values according to Section 5" + for (const ch of codePoints) { + const codePoint = ch.codePointAt(0); + const [status] = findStatus(codePoint); + if (transitionalProcessing) { + // "For Transitional Processing (deprecated), each value must be valid." + if (status !== STATUS_MAPPING.valid) { + return false; + } + } else if (status !== STATUS_MAPPING.valid && status !== STATUS_MAPPING.deviation) { + // "For Nontransitional Processing, each value must be either valid or deviation." + return false; + } + // "In addition, if UseSTD3ASCIIRules=true and the code point is an ASCII code point (U+0000..U+007F), then it must + // be a lowercase letter (a-z), a digit (0-9), or a hyphen-minus (U+002D). (Note: This excludes uppercase ASCII + // A-Z which are mapped in UTS #46 and disallowed in IDNA2008.)" + if (useSTD3ASCIIRules && codePoint <= 0x7F) { + if (!/^(?:[a-z]|[0-9]|-)$/u.test(ch)) { + return false; + } + } + } + + // "8. If CheckJoiners, the label must satisify the ContextJ rules" + // https://tools.ietf.org/html/rfc5892#appendix-A + if (checkJoiners) { + let last = 0; + for (const [i, ch] of codePoints.entries()) { + if (ch === "\u200C" || ch === "\u200D") { + if (i > 0) { + if (regexes.combiningClassVirama.test(codePoints[i - 1])) { + continue; + } + if (ch === "\u200C") { + // TODO: make this more efficient + const next = codePoints.indexOf("\u200C", i + 1); + const test = next < 0 ? codePoints.slice(last) : codePoints.slice(last, next); + if (regexes.validZWNJ.test(test.join(""))) { + last = i + 1; + continue; + } + } + } + return false; + } + } + } + + // "9. If CheckBidi, and if the domain name is a Bidi domain name, then the label must satisfy..." + // https://tools.ietf.org/html/rfc5893#section-2 + if (checkBidi && isBidi) { + let rtl; + + // 1 + if (regexes.bidiS1LTR.test(codePoints[0])) { + rtl = false; + } else if (regexes.bidiS1RTL.test(codePoints[0])) { + rtl = true; + } else { + return false; + } + + if (rtl) { + // 2-4 + if (!regexes.bidiS2.test(label) || + !regexes.bidiS3.test(label) || + (regexes.bidiS4EN.test(label) && regexes.bidiS4AN.test(label))) { + return false; + } + } else if (!regexes.bidiS5.test(label) || + !regexes.bidiS6.test(label)) { // 5-6 + return false; + } + } + + return true; +} + +function isBidiDomain(labels) { + const domain = labels.map(label => { + if (label.startsWith("xn--")) { + try { + return punycode.decode(label.substring(4)); + } catch { + return ""; + } + } + return label; + }).join("."); + return regexes.bidiDomain.test(domain); +} + +function processing(domainName, options) { + // 1. Map. + let string = mapChars(domainName, options); + + // 2. Normalize. + string = string.normalize("NFC"); + + // 3. Break. + const labels = string.split("."); + const isBidi = isBidiDomain(labels); + + // 4. Convert/Validate. + let error = false; + for (const [i, origLabel] of labels.entries()) { + let label = origLabel; + let transitionalProcessingForThisLabel = options.transitionalProcessing; + if (label.startsWith("xn--")) { + if (containsNonASCII(label)) { + error = true; + continue; + } + + try { + label = punycode.decode(label.substring(4)); + } catch { + if (!options.ignoreInvalidPunycode) { + error = true; + continue; + } + } + labels[i] = label; + + if (label === "" || !containsNonASCII(label)) { + error = true; + } + + transitionalProcessingForThisLabel = false; + } + + // No need to validate if we already know there is an error. + if (error) { + continue; + } + const validation = validateLabel(label, { + ...options, + transitionalProcessing: transitionalProcessingForThisLabel, + isBidi + }); + if (!validation) { + error = true; + } + } + + return { + string: labels.join("."), + error + }; +} + +function toASCII(domainName, { + checkHyphens = false, + checkBidi = false, + checkJoiners = false, + useSTD3ASCIIRules = false, + verifyDNSLength = false, + transitionalProcessing = false, + ignoreInvalidPunycode = false +} = {}) { + const result = processing(domainName, { + checkHyphens, + checkBidi, + checkJoiners, + useSTD3ASCIIRules, + transitionalProcessing, + ignoreInvalidPunycode + }); + let labels = result.string.split("."); + labels = labels.map(l => { + if (containsNonASCII(l)) { + try { + return `xn--${punycode.encode(l)}`; + } catch { + result.error = true; + } + } + return l; + }); + + if (verifyDNSLength) { + const total = labels.join(".").length; + if (total > 253 || total === 0) { + result.error = true; + } + + for (let i = 0; i < labels.length; ++i) { + if (labels[i].length > 63 || labels[i].length === 0) { + result.error = true; + break; + } + } + } + + if (result.error) { + return null; + } + return labels.join("."); +} + +function toUnicode(domainName, { + checkHyphens = false, + checkBidi = false, + checkJoiners = false, + useSTD3ASCIIRules = false, + transitionalProcessing = false, + ignoreInvalidPunycode = false +} = {}) { + const result = processing(domainName, { + checkHyphens, + checkBidi, + checkJoiners, + useSTD3ASCIIRules, + transitionalProcessing, + ignoreInvalidPunycode + }); + + return { + domain: result.string, + error: result.error + }; +} + +module.exports = { + toASCII, + toUnicode +}; diff --git a/gateway/node_modules/tr46/package.json b/gateway/node_modules/tr46/package.json new file mode 100644 index 0000000000000000000000000000000000000000..bf5560a48a2446b1d314cdf9c7b48cc04c1613c0 --- /dev/null +++ b/gateway/node_modules/tr46/package.json @@ -0,0 +1,44 @@ +{ + "name": "tr46", + "version": "5.1.1", + "engines": { + "node": ">=18" + }, + "description": "An implementation of the Unicode UTS #46: Unicode IDNA Compatibility Processing", + "main": "index.js", + "files": [ + "index.js", + "lib/" + ], + "scripts": { + "test": "node --test", + "lint": "eslint", + "pretest": "node scripts/getLatestTests.js", + "prepublish": "node scripts/generateMappingTable.js && node scripts/generateRegexes.js" + }, + "repository": "https://github.com/jsdom/tr46", + "keywords": [ + "unicode", + "tr46", + "uts46", + "punycode", + "url", + "whatwg" + ], + "author": "Sebastian Mayr ", + "contributors": [ + "Timothy Gu " + ], + "license": "MIT", + "dependencies": { + "punycode": "^2.3.1" + }, + "devDependencies": { + "@domenic/eslint-config": "^4.0.1", + "@unicode/unicode-16.0.0": "^1.6.5", + "eslint": "^9.22.0", + "globals": "^16.0.0", + "regenerate": "^1.4.2" + }, + "unicodeVersion": "16.0.0" +} diff --git a/gateway/node_modules/webidl-conversions/LICENSE.md b/gateway/node_modules/webidl-conversions/LICENSE.md new file mode 100644 index 0000000000000000000000000000000000000000..d4a994f50bfa1ecc19489f03822a6e728d3e2a49 --- /dev/null +++ b/gateway/node_modules/webidl-conversions/LICENSE.md @@ -0,0 +1,12 @@ +# The BSD 2-Clause License + +Copyright (c) 2014, Domenic Denicola +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/gateway/node_modules/webidl-conversions/README.md b/gateway/node_modules/webidl-conversions/README.md new file mode 100644 index 0000000000000000000000000000000000000000..16cc393157c6513a4e69f6453b7dcaf14516ffe4 --- /dev/null +++ b/gateway/node_modules/webidl-conversions/README.md @@ -0,0 +1,99 @@ +# Web IDL Type Conversions on JavaScript Values + +This package implements, in JavaScript, the algorithms to convert a given JavaScript value according to a given [Web IDL](http://heycam.github.io/webidl/) [type](http://heycam.github.io/webidl/#idl-types). + +The goal is that you should be able to write code like + +```js +"use strict"; +const conversions = require("webidl-conversions"); + +function doStuff(x, y) { + x = conversions["boolean"](x); + y = conversions["unsigned long"](y); + // actual algorithm code here +} +``` + +and your function `doStuff` will behave the same as a Web IDL operation declared as + +```webidl +undefined doStuff(boolean x, unsigned long y); +``` + +## API + +This package's main module's default export is an object with a variety of methods, each corresponding to a different Web IDL type. Each method, when invoked on a JavaScript value, will give back the new JavaScript value that results after passing through the Web IDL conversion rules. (See below for more details on what that means.) Alternately, the method could throw an error, if the Web IDL algorithm is specified to do so: for example `conversions["float"](NaN)` [will throw a `TypeError`](http://heycam.github.io/webidl/#es-float). + +Each method also accepts a second, optional, parameter for miscellaneous options. For conversion methods that throw errors, a string option `{ context }` may be provided to provide more information in the error message. (For example, `conversions["float"](NaN, { context: "Argument 1 of Interface's operation" })` will throw an error with message `"Argument 1 of Interface's operation is not a finite floating-point value."`) + +If we are dealing with multiple JavaScript realms (such as those created using Node.js' [vm](https://nodejs.org/api/vm.html) module or the HTML `iframe` element), and exceptions from another realm need to be thrown, one can supply an object option `globals` containing the following properties: + +```js +{ + globals: { + Number, + String, + TypeError + } +} +``` + +Those specific functions will be used when throwing exceptions. + +Specific conversions may also accept other options, the details of which can be found below. + +## Conversions implemented + +Conversions for all of the basic types from the Web IDL specification are implemented: + +- [`any`](https://heycam.github.io/webidl/#es-any) +- [`undefined`](https://heycam.github.io/webidl/#es-undefined) +- [`boolean`](https://heycam.github.io/webidl/#es-boolean) +- [Integer types](https://heycam.github.io/webidl/#es-integer-types), which can additionally be provided the boolean options `{ clamp, enforceRange }` as a second parameter +- [`float`](https://heycam.github.io/webidl/#es-float), [`unrestricted float`](https://heycam.github.io/webidl/#es-unrestricted-float) +- [`double`](https://heycam.github.io/webidl/#es-double), [`unrestricted double`](https://heycam.github.io/webidl/#es-unrestricted-double) +- [`DOMString`](https://heycam.github.io/webidl/#es-DOMString), which can additionally be provided the boolean option `{ treatNullAsEmptyString }` as a second parameter +- [`ByteString`](https://heycam.github.io/webidl/#es-ByteString), [`USVString`](https://heycam.github.io/webidl/#es-USVString) +- [`object`](https://heycam.github.io/webidl/#es-object) +- [Buffer source types](https://heycam.github.io/webidl/#es-buffer-source-types), which can additionally be provided with the boolean option `{ allowShared }` as a second parameter + +Additionally, for convenience, the following derived type definitions are implemented: + +- [`ArrayBufferView`](https://heycam.github.io/webidl/#ArrayBufferView), which can additionally be provided with the boolean option `{ allowShared }` as a second parameter +- [`BufferSource`](https://heycam.github.io/webidl/#BufferSource) +- [`DOMTimeStamp`](https://heycam.github.io/webidl/#DOMTimeStamp) + +Derived types, such as nullable types, promise types, sequences, records, etc. are not handled by this library. You may wish to investigate the [webidl2js](https://github.com/jsdom/webidl2js) project. + +### A note on the `long long` types + +The `long long` and `unsigned long long` Web IDL types can hold values that cannot be stored in JavaScript numbers. Conversions are still accurate as we make use of BigInt in the conversion process, but in the case of `unsigned long long` we simply cannot represent some possible output values in JavaScript. For example, converting the JavaScript number `-1` to a Web IDL `unsigned long long` is supposed to produce the Web IDL value `18446744073709551615`. Since we are representing our Web IDL values in JavaScript, we can't represent `18446744073709551615`, so we instead the best we could do is `18446744073709551616` as the output. + +To mitigate this, we could return the raw BigInt value from the conversion function, but right now it is not implemented. If your use case requires such precision, [file an issue](https://github.com/jsdom/webidl-conversions/issues/new). + +On the other hand, `long long` conversion is always accurate, since the input value can never be more precise than the output value. + +### A note on `BufferSource` types + +All of the `BufferSource` types will throw when the relevant `ArrayBuffer` has been detached. This technically is not part of the [specified conversion algorithm](https://heycam.github.io/webidl/#es-buffer-source-types), but instead part of the [getting a reference/getting a copy](https://heycam.github.io/webidl/#ref-for-dfn-get-buffer-source-reference%E2%91%A0) algorithms. We've consolidated them here for convenience and ease of implementation, but if there is a need to separate them in the future, please open an issue so we can investigate. + +## Background + +What's actually going on here, conceptually, is pretty weird. Let's try to explain. + +Web IDL, as part of its madness-inducing design, has its own type system. When people write algorithms in web platform specs, they usually operate on Web IDL values, i.e. instances of Web IDL types. For example, if they were specifying the algorithm for our `doStuff` operation above, they would treat `x` as a Web IDL value of [Web IDL type `boolean`](http://heycam.github.io/webidl/#idl-boolean). Crucially, they would _not_ treat `x` as a JavaScript variable whose value is either the JavaScript `true` or `false`. They're instead working in a different type system altogether, with its own rules. + +Separately from its type system, Web IDL defines a ["binding"](http://heycam.github.io/webidl/#ecmascript-binding) of the type system into JavaScript. This contains rules like: when you pass a JavaScript value to the JavaScript method that manifests a given Web IDL operation, how does that get converted into a Web IDL value? For example, a JavaScript `true` passed in the position of a Web IDL `boolean` argument becomes a Web IDL `true`. But, a JavaScript `true` passed in the position of a [Web IDL `unsigned long`](http://heycam.github.io/webidl/#idl-unsigned-long) becomes a Web IDL `1`. And so on. + +Finally, we have the actual implementation code. This is usually C++, although these days [some smart people are using Rust](https://github.com/servo/servo). The implementation, of course, has its own type system. So when they implement the Web IDL algorithms, they don't actually use Web IDL values, since those aren't "real" outside of specs. Instead, implementations apply the Web IDL binding rules in such a way as to convert incoming JavaScript values into C++ values. For example, if code in the browser called `doStuff(true, true)`, then the implementation code would eventually receive a C++ `bool` containing `true` and a C++ `uint32_t` containing `1`. + +The upside of all this is that implementations can abstract all the conversion logic away, letting Web IDL handle it, and focus on implementing the relevant methods in C++ with values of the correct type already provided. That is payoff of Web IDL, in a nutshell. + +And getting to that payoff is the goal of _this_ project—but for JavaScript implementations, instead of C++ ones. That is, this library is designed to make it easier for JavaScript developers to write functions that behave like a given Web IDL operation. So conceptually, the conversion pipeline, which in its general form is JavaScript values ↦ Web IDL values ↦ implementation-language values, in this case becomes JavaScript values ↦ Web IDL values ↦ JavaScript values. And that intermediate step is where all the logic is performed: a JavaScript `true` becomes a Web IDL `1` in an unsigned long context, which then becomes a JavaScript `1`. + +## Don't use this + +Seriously, why would you ever use this? You really shouldn't. Web IDL is … strange, and you shouldn't be emulating its semantics. If you're looking for a generic argument-processing library, you should find one with better rules than those from Web IDL. In general, your JavaScript should not be trying to become more like Web IDL; if anything, we should fix Web IDL to make it more like JavaScript. + +The _only_ people who should use this are those trying to create faithful implementations (or polyfills) of web platform interfaces defined in Web IDL. Its main consumer is the [jsdom](https://github.com/jsdom/jsdom) project. diff --git a/gateway/node_modules/webidl-conversions/package.json b/gateway/node_modules/webidl-conversions/package.json new file mode 100644 index 0000000000000000000000000000000000000000..20747bb420aa2f97f0068421866b6a5be72b692c --- /dev/null +++ b/gateway/node_modules/webidl-conversions/package.json @@ -0,0 +1,35 @@ +{ + "name": "webidl-conversions", + "version": "7.0.0", + "description": "Implements the WebIDL algorithms for converting to and from JavaScript values", + "main": "lib/index.js", + "scripts": { + "lint": "eslint .", + "test": "mocha test/*.js", + "test-no-sab": "mocha --parallel --jobs 2 --require test/helpers/delete-sab.js test/*.js", + "coverage": "nyc mocha test/*.js" + }, + "_scripts_comments": { + "test-no-sab": "Node.js internals are broken by deleting SharedArrayBuffer if you run tests on the main thread. Using Mocha's parallel mode avoids this." + }, + "repository": "jsdom/webidl-conversions", + "keywords": [ + "webidl", + "web", + "types" + ], + "files": [ + "lib/" + ], + "author": "Domenic Denicola (https://domenic.me/)", + "license": "BSD-2-Clause", + "devDependencies": { + "@domenic/eslint-config": "^1.3.0", + "eslint": "^7.32.0", + "mocha": "^9.1.1", + "nyc": "^15.1.0" + }, + "engines": { + "node": ">=12" + } +} diff --git a/gateway/node_modules/whatwg-url/LICENSE.txt b/gateway/node_modules/whatwg-url/LICENSE.txt new file mode 100644 index 0000000000000000000000000000000000000000..8e8c25c3a5d49b03f37fe4b66b0d84fde2b055dc --- /dev/null +++ b/gateway/node_modules/whatwg-url/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Sebastian Mayr + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/gateway/node_modules/whatwg-url/README.md b/gateway/node_modules/whatwg-url/README.md new file mode 100644 index 0000000000000000000000000000000000000000..322766fbb157883fc70dbd912bf2311416e0a569 --- /dev/null +++ b/gateway/node_modules/whatwg-url/README.md @@ -0,0 +1,106 @@ +# whatwg-url + +whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spec.whatwg.org/). It can be used standalone, but it also exposes a lot of the internal algorithms that are useful for integrating a URL parser into a project like [jsdom](https://github.com/jsdom/jsdom). + +## Specification conformance + +whatwg-url is currently up to date with the URL spec up to commit [6c78200](https://github.com/whatwg/url/commit/6c782003a2d53b1feecd072d1006eb8f1d65fb2d). + +For `file:` URLs, whose [origin is left unspecified](https://url.spec.whatwg.org/#concept-url-origin), whatwg-url chooses to use a new opaque origin (which serializes to `"null"`). + +whatwg-url does not yet implement any encoding handling beyond UTF-8. That is, the _encoding override_ parameter does not exist in our API. + +## API + +### The `URL` and `URLSearchParams` classes + +The main API is provided by the [`URL`](https://url.spec.whatwg.org/#url-class) and [`URLSearchParams`](https://url.spec.whatwg.org/#interface-urlsearchparams) exports, which follows the spec's behavior in all ways (including e.g. `USVString` conversion). Most consumers of this library will want to use these. + +### Low-level URL Standard API + +The following methods are exported for use by places like jsdom that need to implement things like [`HTMLHyperlinkElementUtils`](https://html.spec.whatwg.org/#htmlhyperlinkelementutils). They mostly operate on or return an "internal URL" or ["URL record"](https://url.spec.whatwg.org/#concept-url) type. + +- [URL parser](https://url.spec.whatwg.org/#concept-url-parser): `parseURL(input, { baseURL })` +- [Basic URL parser](https://url.spec.whatwg.org/#concept-basic-url-parser): `basicURLParse(input, { baseURL, url, stateOverride })` +- [URL serializer](https://url.spec.whatwg.org/#concept-url-serializer): `serializeURL(urlRecord, excludeFragment)` +- [Host serializer](https://url.spec.whatwg.org/#concept-host-serializer): `serializeHost(hostFromURLRecord)` +- [URL path serializer](https://url.spec.whatwg.org/#url-path-serializer): `serializePath(urlRecord)` +- [Serialize an integer](https://url.spec.whatwg.org/#serialize-an-integer): `serializeInteger(number)` +- [Origin](https://url.spec.whatwg.org/#concept-url-origin) [serializer](https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin): `serializeURLOrigin(urlRecord)` +- [Set the username](https://url.spec.whatwg.org/#set-the-username): `setTheUsername(urlRecord, usernameString)` +- [Set the password](https://url.spec.whatwg.org/#set-the-password): `setThePassword(urlRecord, passwordString)` +- [Has an opaque path](https://url.spec.whatwg.org/#url-opaque-path): `hasAnOpaquePath(urlRecord)` +- [Cannot have a username/password/port](https://url.spec.whatwg.org/#cannot-have-a-username-password-port): `cannotHaveAUsernamePasswordPort(urlRecord)` +- [Percent decode bytes](https://url.spec.whatwg.org/#percent-decode): `percentDecodeBytes(uint8Array)` +- [Percent decode a string](https://url.spec.whatwg.org/#string-percent-decode): `percentDecodeString(string)` + +The `stateOverride` parameter is one of the following strings: + +- [`"scheme start"`](https://url.spec.whatwg.org/#scheme-start-state) +- [`"scheme"`](https://url.spec.whatwg.org/#scheme-state) +- [`"no scheme"`](https://url.spec.whatwg.org/#no-scheme-state) +- [`"special relative or authority"`](https://url.spec.whatwg.org/#special-relative-or-authority-state) +- [`"path or authority"`](https://url.spec.whatwg.org/#path-or-authority-state) +- [`"relative"`](https://url.spec.whatwg.org/#relative-state) +- [`"relative slash"`](https://url.spec.whatwg.org/#relative-slash-state) +- [`"special authority slashes"`](https://url.spec.whatwg.org/#special-authority-slashes-state) +- [`"special authority ignore slashes"`](https://url.spec.whatwg.org/#special-authority-ignore-slashes-state) +- [`"authority"`](https://url.spec.whatwg.org/#authority-state) +- [`"host"`](https://url.spec.whatwg.org/#host-state) +- [`"hostname"`](https://url.spec.whatwg.org/#hostname-state) +- [`"port"`](https://url.spec.whatwg.org/#port-state) +- [`"file"`](https://url.spec.whatwg.org/#file-state) +- [`"file slash"`](https://url.spec.whatwg.org/#file-slash-state) +- [`"file host"`](https://url.spec.whatwg.org/#file-host-state) +- [`"path start"`](https://url.spec.whatwg.org/#path-start-state) +- [`"path"`](https://url.spec.whatwg.org/#path-state) +- [`"opaque path"`](https://url.spec.whatwg.org/#cannot-be-a-base-url-path-state) +- [`"query"`](https://url.spec.whatwg.org/#query-state) +- [`"fragment"`](https://url.spec.whatwg.org/#fragment-state) + +The URL record type has the following API: + +- [`scheme`](https://url.spec.whatwg.org/#concept-url-scheme) +- [`username`](https://url.spec.whatwg.org/#concept-url-username) +- [`password`](https://url.spec.whatwg.org/#concept-url-password) +- [`host`](https://url.spec.whatwg.org/#concept-url-host) +- [`port`](https://url.spec.whatwg.org/#concept-url-port) +- [`path`](https://url.spec.whatwg.org/#concept-url-path) (as an array of strings, or a string) +- [`query`](https://url.spec.whatwg.org/#concept-url-query) +- [`fragment`](https://url.spec.whatwg.org/#concept-url-fragment) + +These properties should be treated with care, as in general changing them will cause the URL record to be in an inconsistent state until the appropriate invocation of `basicURLParse` is used to fix it up. You can see examples of this in the URL Standard, where there are many step sequences like "4. Set context object’s url’s fragment to the empty string. 5. Basic URL parse _input_ with context object’s url as _url_ and fragment state as _state override_." In between those two steps, a URL record is in an unusable state. + +The return value of "failure" in the spec is represented by `null`. That is, functions like `parseURL` and `basicURLParse` can return _either_ a URL record _or_ `null`. + +### `whatwg-url/webidl2js-wrapper` module + +This module exports the `URL` and `URLSearchParams` [interface wrappers API](https://github.com/jsdom/webidl2js#for-interfaces) generated by [webidl2js](https://github.com/jsdom/webidl2js). + +## Development instructions + +First, install [Node.js](https://nodejs.org/). Then, fetch the dependencies of whatwg-url, by running from this directory: + + npm install + +To run tests: + + npm test + +To generate a coverage report: + + npm run coverage + +To build and run the live viewer: + + npm run prepare + npm run build-live-viewer + +Serve the contents of the `live-viewer` directory using any web server. + +## Supporting whatwg-url + +The jsdom project (including whatwg-url) is a community-driven project maintained by a team of [volunteers](https://github.com/orgs/jsdom/people). You could support us by: + +- [Getting professional support for whatwg-url](https://tidelift.com/subscription/pkg/npm-whatwg-url?utm_source=npm-whatwg-url&utm_medium=referral&utm_campaign=readme) as part of a Tidelift subscription. Tidelift helps making open source sustainable for us while giving teams assurances for maintenance, licensing, and security. +- Contributing directly to the project. diff --git a/gateway/node_modules/whatwg-url/index.js b/gateway/node_modules/whatwg-url/index.js new file mode 100644 index 0000000000000000000000000000000000000000..c470e48e4eb027ed07abf42a263e042e14a3f6bd --- /dev/null +++ b/gateway/node_modules/whatwg-url/index.js @@ -0,0 +1,27 @@ +"use strict"; + +const { URL, URLSearchParams } = require("./webidl2js-wrapper"); +const urlStateMachine = require("./lib/url-state-machine"); +const percentEncoding = require("./lib/percent-encoding"); + +const sharedGlobalObject = { Array, Object, Promise, String, TypeError }; +URL.install(sharedGlobalObject, ["Window"]); +URLSearchParams.install(sharedGlobalObject, ["Window"]); + +exports.URL = sharedGlobalObject.URL; +exports.URLSearchParams = sharedGlobalObject.URLSearchParams; + +exports.parseURL = urlStateMachine.parseURL; +exports.basicURLParse = urlStateMachine.basicURLParse; +exports.serializeURL = urlStateMachine.serializeURL; +exports.serializePath = urlStateMachine.serializePath; +exports.serializeHost = urlStateMachine.serializeHost; +exports.serializeInteger = urlStateMachine.serializeInteger; +exports.serializeURLOrigin = urlStateMachine.serializeURLOrigin; +exports.setTheUsername = urlStateMachine.setTheUsername; +exports.setThePassword = urlStateMachine.setThePassword; +exports.cannotHaveAUsernamePasswordPort = urlStateMachine.cannotHaveAUsernamePasswordPort; +exports.hasAnOpaquePath = urlStateMachine.hasAnOpaquePath; + +exports.percentDecodeString = percentEncoding.percentDecodeString; +exports.percentDecodeBytes = percentEncoding.percentDecodeBytes; diff --git a/gateway/node_modules/whatwg-url/package.json b/gateway/node_modules/whatwg-url/package.json new file mode 100644 index 0000000000000000000000000000000000000000..d0ef78aea37bc54bb4bb78aed75ca4089f975ff6 --- /dev/null +++ b/gateway/node_modules/whatwg-url/package.json @@ -0,0 +1,53 @@ +{ + "name": "whatwg-url", + "version": "14.2.0", + "description": "An implementation of the WHATWG URL Standard's URL API and parsing machinery", + "main": "index.js", + "files": [ + "index.js", + "webidl2js-wrapper.js", + "lib/*.js" + ], + "author": "Sebastian Mayr ", + "license": "MIT", + "repository": "jsdom/whatwg-url", + "dependencies": { + "tr46": "^5.1.0", + "webidl-conversions": "^7.0.0" + }, + "devDependencies": { + "@domenic/eslint-config": "^4.0.1", + "benchmark": "^2.1.4", + "c8": "^10.1.3", + "esbuild": "^0.25.1", + "eslint": "^9.22.0", + "globals": "^16.0.0", + "webidl2js": "^18.0.0" + }, + "engines": { + "node": ">=18" + }, + "scripts": { + "coverage": "c8 node --test --experimental-test-coverage test/*.js", + "lint": "eslint", + "prepare": "node scripts/transform.js", + "pretest": "node scripts/get-latest-platform-tests.js && node scripts/transform.js", + "build-live-viewer": "esbuild --bundle --format=esm --sourcemap --outfile=live-viewer/whatwg-url.mjs index.js", + "test": "node --test test/*.js", + "bench": "node scripts/benchmark.js" + }, + "c8": { + "reporter": [ + "text", + "html" + ], + "exclude": [ + "lib/Function.js", + "lib/URL.js", + "lib/URLSearchParams.js", + "lib/utils.js", + "scripts/", + "test/" + ] + } +} diff --git a/gateway/node_modules/whatwg-url/webidl2js-wrapper.js b/gateway/node_modules/whatwg-url/webidl2js-wrapper.js new file mode 100644 index 0000000000000000000000000000000000000000..b731ace5f49f0afd309c120d6410f7dc2c4aeb60 --- /dev/null +++ b/gateway/node_modules/whatwg-url/webidl2js-wrapper.js @@ -0,0 +1,7 @@ +"use strict"; + +const URL = require("./lib/URL"); +const URLSearchParams = require("./lib/URLSearchParams"); + +exports.URL = URL; +exports.URLSearchParams = URLSearchParams; diff --git a/gateway/package-lock.json b/gateway/package-lock.json index 5abe0f4176a81b8479660c21ee2d1a4670412ce2..57a45030e05741b0599bc8204a5ea504aa635bf7 100644 --- a/gateway/package-lock.json +++ b/gateway/package-lock.json @@ -16,11 +16,42 @@ "express": "^5.2.1", "express-rate-limit": "^8.6.2", "jsonwebtoken": "^9.0.3", + "mongoose": "^9.9.3", "multer": "^2.2.0", "razorpay": "^2.9.8", "stripe": "^22.5.0" } }, + "node_modules/@mongodb-js/saslprep": { + "version": "1.4.13", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.4.13.tgz", + "integrity": "sha512-E3Sv4eCYAlKYUTx8S3ioQcDUscOif+8zZ5OnW1IzJ+Tt+EO+ke8mn+Y3FX6N1H79picwbdOavVOb1jPi2EOyrg==", + "license": "MIT", + "dependencies": { + "sparse-bitfield": "^3.0.3" + } + }, + "node_modules/@standard-schema/spec": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz", + "integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==", + "license": "MIT" + }, + "node_modules/@types/webidl-conversions": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz", + "integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA==", + "license": "MIT" + }, + "node_modules/@types/whatwg-url": { + "version": "13.0.0", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-13.0.0.tgz", + "integrity": "sha512-N8WXpbE6Wgri7KUSvrmQcqrMllKZ9uxkYWMt+mCSGwNc0Hsw9VQTW7ApqI4XNrx6/SaM2QQJCzMPDEXE058s+Q==", + "license": "MIT", + "dependencies": { + "@types/webidl-conversions": "*" + } + }, "node_modules/accepts": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz", @@ -116,6 +147,15 @@ "url": "https://opencollective.com/express" } }, + "node_modules/bson": { + "version": "7.3.2", + "resolved": "https://registry.npmjs.org/bson/-/bson-7.3.2.tgz", + "integrity": "sha512-1w0ra+ho1cuE+w8jzwgzTFIimFtCfZeCoOsvIPQg6uyFCsp8M29U7bNNf5GrFh88TXbYg1g3TyKUGpd6O7q6zA==", + "license": "Apache-2.0", + "engines": { + "node": ">=20.19.0" + } + }, "node_modules/buffer-equal-constant-time": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", @@ -783,6 +823,15 @@ "safe-buffer": "^5.0.1" } }, + "node_modules/kareem": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-3.3.0.tgz", + "integrity": "sha512-kpSuLD3/7RenBnjnJdOHXCKC8dTd1JzeOiJhN0necWWci6cC+qX+VuwPnMVgb+a4+KNJSfgqahpnfWaeDXCimw==", + "license": "Apache-2.0", + "engines": { + "node": ">=18.0.0" + } + }, "node_modules/lodash.includes": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", @@ -847,6 +896,12 @@ "url": "https://opencollective.com/express" } }, + "node_modules/memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "license": "MIT" + }, "node_modules/merge-descriptors": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-2.0.0.tgz", @@ -884,6 +939,105 @@ "url": "https://opencollective.com/express" } }, + "node_modules/mongodb": { + "version": "7.5.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-7.5.0.tgz", + "integrity": "sha512-5FnrEDLnvp6ycUOGLNLLU33BfCx2qmp2mJjGPDwKLruYsVzXVSK5fsGpoDXvsXJwBfBsD7ebMRdawbDxC2814g==", + "license": "Apache-2.0", + "dependencies": { + "@mongodb-js/saslprep": "^1.4.11", + "bson": "^7.2.0", + "mongodb-connection-string-url": "^7.0.1" + }, + "engines": { + "node": ">=20.19.0" + }, + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.806.0", + "@mongodb-js/zstd": "^7.0.0", + "gcp-metadata": "^7.0.1", + "kerberos": "^7.0.0", + "mongodb-client-encryption": "^7.2.0", + "snappy": "^7.3.2", + "socks": "^2.8.6" + }, + "peerDependenciesMeta": { + "@aws-sdk/credential-providers": { + "optional": true + }, + "@mongodb-js/zstd": { + "optional": true + }, + "gcp-metadata": { + "optional": true + }, + "kerberos": { + "optional": true + }, + "mongodb-client-encryption": { + "optional": true + }, + "snappy": { + "optional": true + }, + "socks": { + "optional": true + } + } + }, + "node_modules/mongodb-connection-string-url": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-7.0.2.tgz", + "integrity": "sha512-ZoS07RoFqpKYQwAk59qmrx8+jJHNHU30UjlU96QktiGn1ltvDr+vCznLX5DiUBLEpMAHatHNWV1nM/74ul66kA==", + "license": "Apache-2.0", + "dependencies": { + "@types/whatwg-url": "^13.0.0", + "whatwg-url": "^14.1.0" + }, + "engines": { + "node": ">=20.19.0" + } + }, + "node_modules/mongoose": { + "version": "9.9.3", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-9.9.3.tgz", + "integrity": "sha512-9dQaKct05LNgVkunDzFPZsGGBhinobl3Qc5B5KYJkLNG5lBLgqESUEItl1EUIkVaCwZJGBgTuvJiFAjjIATCiw==", + "license": "MIT", + "dependencies": { + "@standard-schema/spec": "^1.1.0", + "kareem": "3.3.0", + "mongodb": "~7.5", + "mpath": "0.9.0", + "mquery": "6.0.0", + "ms": "2.1.3", + "sift": "17.1.3" + }, + "engines": { + "node": ">=20.19.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/mongoose" + } + }, + "node_modules/mpath": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz", + "integrity": "sha512-ikJRQTk8hw5DEoFVxHG1Gn9T/xcjtdnOKIU1JTmGjZZlg9LST2mBLmcX3/ICIbgJydT2GOc15RnNy5mHmzfSew==", + "license": "MIT", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/mquery": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-6.0.0.tgz", + "integrity": "sha512-b2KQNsmgtkscfeDgkYMcWGn9vZI9YoXh802VDEwE6qc50zxBFQ0Oo8ROkawbPAsXCY1/Z1yp0MagqsZStPWJjw==", + "license": "MIT", + "engines": { + "node": ">=20.19.0" + } + }, "node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", @@ -1044,6 +1198,15 @@ "node": ">=10" } }, + "node_modules/punycode": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "license": "MIT", + "engines": { + "node": ">=6" + } + }, "node_modules/qs": { "version": "6.15.3", "resolved": "https://registry.npmjs.org/qs/-/qs-6.15.3.tgz", @@ -1288,6 +1451,21 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/sift": { + "version": "17.1.3", + "resolved": "https://registry.npmjs.org/sift/-/sift-17.1.3.tgz", + "integrity": "sha512-Rtlj66/b0ICeFzYTuNvX/EF1igRbbnGSvEyT79McoZa/DeGhMyC5pWKOEsZKnpkqtSeovd5FL/bjHWC3CIIvCQ==", + "license": "MIT" + }, + "node_modules/sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", + "license": "MIT", + "dependencies": { + "memory-pager": "^1.0.2" + } + }, "node_modules/statuses": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz", @@ -1340,6 +1518,18 @@ "node": ">=0.6" } }, + "node_modules/tr46": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-5.1.1.tgz", + "integrity": "sha512-hdF5ZgjTqgAntKkklYw0R03MG2x/bSzTtkxmIRw/sTNV8YXsCJ1tfLAX23lhxhHJlEf3CRCOCGGWw3vI3GaSPw==", + "license": "MIT", + "dependencies": { + "punycode": "^2.3.1" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/type-is": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/type-is/-/type-is-2.1.0.tgz", @@ -1401,6 +1591,28 @@ "node": ">= 0.8" } }, + "node_modules/webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + } + }, + "node_modules/whatwg-url": { + "version": "14.2.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-14.2.0.tgz", + "integrity": "sha512-De72GdQZzNTUBBChsXueQUnPKDkg/5A5zp7pFDuQAj5UFoENpiACU0wlCvzpAGnTkj++ihpKwKyYewn/XNUbKw==", + "license": "MIT", + "dependencies": { + "tr46": "^5.1.0", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/gateway/package.json b/gateway/package.json index be0a7435d6281539e4bb01dbaf1bb1ddb8c16cec..84580af0b7427385125b1b7224a67c2b0fdd6728 100644 --- a/gateway/package.json +++ b/gateway/package.json @@ -20,6 +20,7 @@ "express": "^5.2.1", "express-rate-limit": "^8.6.2", "jsonwebtoken": "^9.0.3", + "mongoose": "^9.9.3", "multer": "^2.2.0", "razorpay": "^2.9.8", "stripe": "^22.5.0" diff --git a/gateway/server.js b/gateway/server.js index 434cf57ccbf3ec8b7a667b1aa48c0969e153176e..8ee4b86552c14ae3a7403fd26f3e4993cba3759a 100644 --- a/gateway/server.js +++ b/gateway/server.js @@ -11,6 +11,15 @@ const jwt = require('jsonwebtoken'); // Configuration environment variables loading dotenv.config(); +const mongoose = require('mongoose'); +const User = require('./models/User'); +const ProjectChat = require('./models/Chat'); + +// Connect to MongoDB +mongoose.connect(process.env.MONGODB_URI) + .then(() => console.log('🌿 Connected securely to MongoDB Database')) + .catch(err => console.error('❌ Failed to connect to MongoDB:', err.message)); + const app = express(); const stripe = stripeLib(process.env.STRIPE_SECRET_KEY); const razorpay = new Razorpay({ @@ -18,15 +27,13 @@ const razorpay = new Razorpay({ key_secret: process.env.RAZORPAY_KEY_SECRET || 'placeholderSecret' }); const PORT = process.env.PORT || 5000; -const USERS_FILE = path.join(__dirname, 'users.json'); -const CHATS_FILE = path.join(__dirname, 'chats.json'); const multer = require('multer'); const FormData = require('form-data'); const bcrypt = require('bcryptjs'); const rateLimit = require('express-rate-limit'); // Configure secure file upload boundaries -const upload = multer({ +const upload = multer({ storage: multer.memoryStorage(), limits: { fileSize: 10 * 1024 * 1024 // Enforce 10MB maximum file size limit @@ -42,19 +49,6 @@ const upload = multer({ } }); -const readChatsDB = () => { - try { - const data = fs.readFileSync(CHATS_FILE, 'utf8'); - return JSON.parse(data); - } catch (err) { - return {}; - } -}; - -const writeChatsDB = (chats) => { - fs.writeFileSync(CHATS_FILE, JSON.stringify(chats, null, 2)); -}; - // SaaS limits config plan const PLAN_LIMITS = { free: { @@ -108,20 +102,7 @@ app.use((req, res, next) => { } }); -// Helper function to read local users JSON database -const readUsersDB = () => { - try { - const data = fs.readFileSync(USERS_FILE, 'utf8'); - return JSON.parse(data); - } catch (err) { - return []; - } -}; -// Helper function to write local users JSON database -const writeUsersDB = (users) => { - fs.writeFileSync(USERS_FILE, JSON.stringify(users, null, 2)); -}; // ======================================================== // 🛡️ AUTHENTICATION MIDDLEWARE @@ -151,54 +132,53 @@ const authenticateToken = (req, res, next) => { // ======================================================== app.post('/api/login', authLimiter, async (req, res) => { const { email, password } = req.body; - const users = readUsersDB(); - - // Find user by email - const user = users.find(u => u.email.toLowerCase() === email.toLowerCase()); - if (!user) { - return res.status(401).json({ error: "Invalid email or password." }); - } - // Password comparison with migration fallback - let validPassword = false; try { + // Find user by email in MongoDB + const user = await User.findOne({ email: email.toLowerCase() }); + if (!user) { + return res.status(401).json({ error: "Invalid email or password." }); + } + + // Compare password (bcrypt check with plain-text fallback) + let validPassword = false; if (user.password.startsWith('$2a$') || user.password.startsWith('$2b$')) { validPassword = await bcrypt.compare(password, user.password); } else { - // Plain text check & auto-migrate to hash + // Plain text check & auto-migrate to hash in MongoDB if (user.password === password) { validPassword = true; const saltRounds = 10; user.password = await bcrypt.hash(password, saltRounds); - writeUsersDB(users); - console.log(`[PASSWORD SECURITY MIGRATION] Migrated plain-text credentials to bcrypt hash for user: ${user.email}`); + await user.save(); // Save hashed password directly to MongoDB + console.log(`[PASSWORD MIGRATION] Migrated credentials to bcrypt for: ${user.email}`); } } - } catch (err) { - console.error("Encryption verification failed during login:", err.message); - return res.status(500).json({ error: "Internal encryption failure." }); - } - if (!validPassword) { - return res.status(401).json({ error: "Invalid email or password." }); - } + if (!validPassword) { + return res.status(401).json({ error: "Invalid email or password." }); + } + + // Generate JWT token containing user metadata + const token = jwt.sign( + { userId: user.userId, email: user.email, name: user.name, isPremium: user.isPremium }, + process.env.JWT_SECRET, + { expiresIn: '2h' } + ); - // Generate JWT token containing user metadata - const token = jwt.sign( - { userId: user.userId, email: user.email, name: user.name, isPremium: user.isPremium }, - process.env.JWT_SECRET, - { expiresIn: '2h' } - ); - - res.json({ - token: token, - user: { - userId: user.userId, - name: user.name, - email: user.email, - isPremium: user.isPremium - } - }); + res.json({ + token: token, + user: { + userId: user.userId, + name: user.name, + email: user.email, + isPremium: user.isPremium + } + }); + } catch (err) { + console.error("Login route error:", err.message); + res.status(500).json({ error: "Internal server error during login." }); + } }); // ======================================================== @@ -206,43 +186,42 @@ app.post('/api/login', authLimiter, async (req, res) => { // ======================================================== app.post('/api/register', authLimiter, async (req, res) => { const { email, password, name, profilePhoto } = req.body; - + if (!email || !password) { return res.status(400).json({ error: "Email and password are required." }); } - const users = readUsersDB(); - - // Check if email already registered - const exists = users.find(u => u.email.toLowerCase() === email.toLowerCase()); - if (exists) { - return res.status(400).json({ error: "Email address is already in use." }); - } - try { - // Enforce password hashing + // Check if email already registered in MongoDB + const exists = await User.findOne({ email: email.toLowerCase() }); + if (exists) { + return res.status(400).json({ error: "Email address is already in use." }); + } + + // Hash password const saltRounds = 10; const hashedPassword = await bcrypt.hash(password, saltRounds); const userId = 'usr_' + Date.now() + '_' + Math.random().toString(36).substr(2, 5); const displayName = name || email.split('@')[0]; - const newUser = { + + // Create new user instance and save to MongoDB + const newUser = new User({ userId: userId, name: displayName, - email: email, + email: email.toLowerCase(), password: hashedPassword, isPremium: false, queryCount: 0, totalStorageBytes: 0, uploadedDocuments: [], profilePhoto: profilePhoto || '👨‍💻' - }; + }); - users.push(newUser); - writeUsersDB(users); - console.log(`[USER REGISTRATION] Created new secure user: ${displayName} (${email})`); + await newUser.save(); + console.log(`[USER REGISTRATION] Created new secure user in MongoDB: ${displayName}`); - // Generate JWT token containing new user metadata + // Generate JWT token const token = jwt.sign( { userId: newUser.userId, email: newUser.email, name: newUser.name, isPremium: newUser.isPremium }, process.env.JWT_SECRET, @@ -259,11 +238,10 @@ app.post('/api/register', authLimiter, async (req, res) => { } }); } catch (err) { - console.error("Failed to register new secure user:", err.message); - res.status(500).json({ error: "Registration encryption failed." }); + console.error("Registration error:", err.message); + res.status(500).json({ error: "Failed to register new secure user." }); } }); - // ======================================================== // 🔍 ROUTE 1: Proxy Query (Protected by Auth) // ======================================================== @@ -271,82 +249,79 @@ app.post('/api/query', authenticateToken, async (req, res) => { const { query, documentId = null, projectId = null, chatId = null } = req.body; const userId = req.user.userId; - const users = readUsersDB(); - const user = users.find(u => u.userId === userId); + try { + const user = await User.findOne({ userId }); + if (!user) { + return res.status(404).json({ error: "User profile not found." }); + } - if (!user) { - return res.status(404).json({ error: "User profile not found." }); - } + const plan = user.isPremium ? 'premium' : 'free'; + const limits = PLAN_LIMITS[plan]; - const plan = user.isPremium ? 'premium' : 'free'; - const limits = PLAN_LIMITS[plan]; + // 1. Check monthly query limits + if ((user.queryCount || 0) >= limits.max_queries) { + return res.status(403).json({ + is_blocked: true, + message: `⚠️ Monthly query limit reached (${limits.max_queries} queries). Upgrade to Premium for 5,000 queries/month!` + }); + } - // 1. Check monthly query limits - if ((user.queryCount || 0) >= limits.max_queries) { - return res.status(403).json({ - is_blocked: true, - message: `⚠️ Monthly query limit reached (${limits.max_queries} queries). Upgrade to Premium for 5,000 queries/month!` - }); - } + // 2. Perform Paywall word length check for Free tier + const queryLength = query.trim().split(/\s+/).length; + if (plan === 'free' && queryLength > 4) { + return res.status(403).json({ + is_blocked: true, + message: "⚠️ Free plan only allows quick query search of up to 4 words. Upgrade to Premium for unlimited prompt lengths and advanced reasoning!" + }); + } - // 2. Perform Paywall word length check for Free tier - const queryLength = query.trim().split(/\s+/).length; - if (plan === 'free' && queryLength > 4) { - return res.status(403).json({ - is_blocked: true, - message: "⚠️ Free plan only allows quick query search of up to 4 words. Upgrade to Premium for unlimited prompt lengths and advanced reasoning!" - }); - } + // 3. Increment usage query counter in database + user.queryCount = (user.queryCount || 0) + 1; + await user.save(); - // 3. Increment usage query counter in database - user.queryCount = (user.queryCount || 0) + 1; - writeUsersDB(users); - - // 4. Construct payload overrides based on subscription plan capabilities - // Under premium, if the user checks "query all documents", they pass projectId = 'all' - const targetDocId = plan === 'premium' ? (projectId === 'all' ? null : (projectId || documentId)) : (projectId || documentId); - - const payload = { - query: query, - use_reranking: limits.reranking, - use_nli_guard: limits.nli_guard, - limit_document_id: targetDocId - }; + // 4. Construct payload overrides based on subscription plan capabilities + const targetDocId = plan === 'premium' ? (projectId === 'all' ? null : (projectId || documentId)) : (projectId || documentId); + + const payload = { + query: query, + use_reranking: limits.reranking, + use_nli_guard: limits.nli_guard, + limit_document_id: targetDocId + }; - try { console.log(`Forwarding query to RAG (Plan: ${plan}): "${query}"`); const response = await axios.post(`${process.env.PYTHON_BACKEND_URL}/api/query`, payload); const ragResult = response.data; // { answer, telemetry } - // 5. Store message in chat session history + // 5. Store message in MongoDB chat session history if (projectId && chatId) { - const chatsDb = readChatsDB(); - if (!chatsDb[userId]) chatsDb[userId] = {}; - if (!chatsDb[userId][projectId]) chatsDb[userId][projectId] = { chats: [] }; - - const chats = chatsDb[userId][projectId].chats; - const chat = chats.find(c => c.chatId === chatId); + let projectChat = await ProjectChat.findOne({ userId, projectId }); + if (!projectChat) { + projectChat = new ProjectChat({ userId, projectId, chats: [] }); + } + + const chat = projectChat.chats.find(c => c.chatId === chatId); if (chat) { chat.messages.push({ sender: 'user', text: query, - timestamp: new Date().toISOString() + timestamp: new Date() }); chat.messages.push({ sender: 'ai', text: ragResult.answer, telemetry: ragResult.telemetry, - timestamp: new Date().toISOString() + timestamp: new Date() }); - writeChatsDB(chatsDb); + await projectChat.save(); } } res.json(ragResult); } catch (err) { - console.error("RAG Engine unreachable:", err.message); + console.error("Query proxy error:", err.message); res.status(502).json({ - error: "Bad Gateway. Private Python RAG microservice is offline.", + error: "Bad Gateway. Private Python RAG microservice is offline or returned an error.", details: err.message }); } @@ -359,53 +334,51 @@ app.post('/api/ingest', authenticateToken, async (req, res) => { const { doc_id, title, text, metadata = {} } = req.body; const userId = req.user.userId; - const users = readUsersDB(); - const user = users.find(u => u.userId === userId); - - if (!user) { - return res.status(404).json({ error: "User profile not found." }); - } + try { + const user = await User.findOne({ userId }); + if (!user) { + return res.status(404).json({ error: "User profile not found." }); + } - const plan = user.isPremium ? 'premium' : 'free'; - const limits = PLAN_LIMITS[plan]; + const plan = user.isPremium ? 'premium' : 'free'; + const limits = PLAN_LIMITS[plan]; - user.uploadedDocuments = user.uploadedDocuments || []; - user.totalStorageBytes = user.totalStorageBytes || 0; + user.uploadedDocuments = user.uploadedDocuments || []; + user.totalStorageBytes = user.totalStorageBytes || 0; - // 1. Check document upload count limits - if (user.uploadedDocuments.length >= limits.max_documents) { - return res.status(403).json({ - error: "Document Limit Exceeded", - message: `⚠️ Free plan only allows up to ${limits.max_documents} documents. Please upgrade to Premium to index up to 100 documents!` - }); - } + // 1. Check document upload count limits + if (user.uploadedDocuments.length >= limits.max_documents) { + return res.status(403).json({ + error: "Document Limit Exceeded", + message: `⚠️ Free plan only allows up to ${limits.max_documents} documents. Please upgrade to Premium to index up to 100 documents!` + }); + } - // 2. Check total storage size limits - const sizeBytes = Buffer.byteLength(text, 'utf8'); - const incomingStorageMb = (user.totalStorageBytes + sizeBytes) / (1024 * 1024); - if (incomingStorageMb > limits.max_storage_mb) { - return res.status(403).json({ - error: "Storage Limit Exceeded", - message: `⚠️ Storage limit of ${limits.max_storage_mb} MB would be exceeded. Upgrade to Premium for 5 GB storage!` - }); - } + // 2. Check total storage size limits + const sizeBytes = Buffer.byteLength(text, 'utf8'); + const incomingStorageMb = (user.totalStorageBytes + sizeBytes) / (1024 * 1024); + if (incomingStorageMb > limits.max_storage_mb) { + return res.status(403).json({ + error: "Storage Limit Exceeded", + message: `⚠️ Storage limit of ${limits.max_storage_mb} MB would be exceeded. Upgrade to Premium for 5 GB storage!` + }); + } - // 3. Proxy ingestion requests to private Python indexing service - try { + // 3. Proxy ingestion requests to private Python indexing service console.log(`Forwarding ingestion request for document: "${title}"`); const response = await axios.post(`${process.env.PYTHON_BACKEND_URL}/api/ingest`, { doc_id, title, text, metadata }); - // 4. Update local user storage statistics metadata + // 4. Update MongoDB user storage statistics metadata user.uploadedDocuments.push({ id: doc_id, name: title, sizeBytes }); user.totalStorageBytes += sizeBytes; - writeUsersDB(users); + await user.save(); res.json(response.data); } catch (err) { - console.error("Python ingestion server failed:", err.message); - res.status(502).json({ error: "Python indexing server is offline." }); + console.error("Ingest error:", err.message); + res.status(502).json({ error: "Python indexing server is offline or returned an error." }); } }); @@ -420,38 +393,36 @@ app.post('/api/ingest-file', authenticateToken, upload.single('file'), async (re return res.status(400).json({ error: "No file was uploaded." }); } - const users = readUsersDB(); - const user = users.find(u => u.userId === userId); - - if (!user) { - return res.status(404).json({ error: "User profile not found." }); - } + try { + const user = await User.findOne({ userId }); + if (!user) { + return res.status(404).json({ error: "User profile not found." }); + } - const plan = user.isPremium ? 'premium' : 'free'; - const limits = PLAN_LIMITS[plan]; + const plan = user.isPremium ? 'premium' : 'free'; + const limits = PLAN_LIMITS[plan]; - user.uploadedDocuments = user.uploadedDocuments || []; - user.totalStorageBytes = user.totalStorageBytes || 0; + user.uploadedDocuments = user.uploadedDocuments || []; + user.totalStorageBytes = user.totalStorageBytes || 0; - // 1. Check document upload count limits - if (user.uploadedDocuments.length >= limits.max_documents) { - return res.status(403).json({ - error: "Document Limit Exceeded", - message: `⚠️ Free plan only allows up to ${limits.max_documents} documents. Please upgrade to Premium to index up to 100 documents!` - }); - } + // 1. Check document upload count limits + if (user.uploadedDocuments.length >= limits.max_documents) { + return res.status(403).json({ + error: "Document Limit Exceeded", + message: `⚠️ Free plan only allows up to ${limits.max_documents} documents. Please upgrade to Premium to index up to 100 documents!` + }); + } - // 2. Check total storage size limits - const sizeBytes = req.file.size; - const incomingStorageMb = (user.totalStorageBytes + sizeBytes) / (1024 * 1024); - if (incomingStorageMb > limits.max_storage_mb) { - return res.status(403).json({ - error: "Storage Limit Exceeded", - message: `⚠️ Storage limit of ${limits.max_storage_mb} MB would be exceeded. Upgrade to Premium for 5 GB storage!` - }); - } + // 2. Check total storage size limits + const sizeBytes = req.file.size; + const incomingStorageMb = (user.totalStorageBytes + sizeBytes) / (1024 * 1024); + if (incomingStorageMb > limits.max_storage_mb) { + return res.status(403).json({ + error: "Storage Limit Exceeded", + message: `⚠️ Storage limit of ${limits.max_storage_mb} MB would be exceeded. Upgrade to Premium for 5 GB storage!` + }); + } - try { console.log(`Forwarding file to Python parsing engine: "${title}" (${req.file.originalname})`); // Forward as FormData to Python @@ -467,10 +438,10 @@ app.post('/api/ingest-file', authenticateToken, upload.single('file'), async (re headers: form.getHeaders() }); - // 3. Update local user storage statistics metadata + // 3. Update MongoDB user storage statistics metadata user.uploadedDocuments.push({ id: doc_id, name: title, sizeBytes }); user.totalStorageBytes += sizeBytes; - writeUsersDB(users); + await user.save(); res.json(response.data); } catch (err) { @@ -482,68 +453,89 @@ app.post('/api/ingest-file', authenticateToken, upload.single('file'), async (re // ======================================================== // 💬 ROUTES 1D: Project Chat Thread Management (Protected by Auth) // ======================================================== -app.get('/api/projects/:projectId/chats', authenticateToken, (req, res) => { +app.get('/api/projects/:projectId/chats', authenticateToken, async (req, res) => { const userId = req.user.userId; const { projectId } = req.params; - const chatsDb = readChatsDB(); - if (chatsDb[userId] && chatsDb[userId][projectId]) { - res.json(chatsDb[userId][projectId].chats); - } else { - res.json([]); + try { + const projectChat = await ProjectChat.findOne({ userId, projectId }); + if (projectChat) { + res.json(projectChat.chats); + } else { + res.json([]); + } + } catch (err) { + console.error("Fetch chats error:", err.message); + res.status(500).json({ error: "Failed to fetch chat threads." }); } }); -app.post('/api/projects/:projectId/chats', authenticateToken, (req, res) => { +app.post('/api/projects/:projectId/chats', authenticateToken, async (req, res) => { const userId = req.user.userId; const { projectId } = req.params; const { title } = req.body; - const chatsDb = readChatsDB(); - if (!chatsDb[userId]) chatsDb[userId] = {}; - if (!chatsDb[userId][projectId]) chatsDb[userId][projectId] = { chats: [] }; + try { + let projectChat = await ProjectChat.findOne({ userId, projectId }); + if (!projectChat) { + projectChat = new ProjectChat({ userId, projectId, chats: [] }); + } - const newChat = { - chatId: 'chat_' + Date.now() + '_' + Math.random().toString(36).substr(2, 5), - chatTitle: title || "New Chat Thread", - messages: [] - }; + const newChat = { + chatId: 'chat_' + Date.now() + '_' + Math.random().toString(36).substr(2, 5), + chatTitle: title || "New Chat Thread", + messages: [] + }; - chatsDb[userId][projectId].chats.push(newChat); - writeChatsDB(chatsDb); + projectChat.chats.push(newChat); + await projectChat.save(); - res.status(201).json(newChat); + res.status(201).json(newChat); + } catch (err) { + console.error("Create chat error:", err.message); + res.status(500).json({ error: "Failed to create new chat thread." }); + } }); -app.delete('/api/projects/:projectId/chats/:chatId', authenticateToken, (req, res) => { +app.delete('/api/projects/:projectId/chats/:chatId', authenticateToken, async (req, res) => { const userId = req.user.userId; const { projectId, chatId } = req.params; - const chatsDb = readChatsDB(); - if (chatsDb[userId] && chatsDb[userId][projectId]) { - chatsDb[userId][projectId].chats = chatsDb[userId][projectId].chats.filter(c => c.chatId !== chatId); - writeChatsDB(chatsDb); - res.json({ success: true }); - } else { - res.status(404).json({ error: "Chat thread not found." }); + try { + const projectChat = await ProjectChat.findOne({ userId, projectId }); + if (projectChat) { + projectChat.chats = projectChat.chats.filter(c => c.chatId !== chatId); + await projectChat.save(); + res.json({ success: true }); + } else { + res.status(404).json({ error: "Chat thread not found." }); + } + } catch (err) { + console.error("Delete chat error:", err.message); + res.status(500).json({ error: "Failed to delete chat thread." }); } }); -app.put('/api/projects/:projectId/chats/:chatId', authenticateToken, (req, res) => { +app.put('/api/projects/:projectId/chats/:chatId', authenticateToken, async (req, res) => { const userId = req.user.userId; const { projectId, chatId } = req.params; const { title } = req.body; - const chatsDb = readChatsDB(); - if (chatsDb[userId] && chatsDb[userId][projectId]) { - const chat = chatsDb[userId][projectId].chats.find(c => c.chatId === chatId); - if (chat) { - chat.chatTitle = title || chat.chatTitle; - writeChatsDB(chatsDb); - return res.json(chat); + try { + const projectChat = await ProjectChat.findOne({ userId, projectId }); + if (projectChat) { + const chat = projectChat.chats.find(c => c.chatId === chatId); + if (chat) { + chat.chatTitle = title || chat.chatTitle; + await projectChat.save(); + return res.json(chat); + } } + res.status(404).json({ error: "Chat thread not found." }); + } catch (err) { + console.error("Rename chat error:", err.message); + res.status(500).json({ error: "Failed to rename chat thread." }); } - res.status(404).json({ error: "Chat thread not found." }); }); // ======================================================== @@ -551,14 +543,13 @@ app.put('/api/projects/:projectId/chats/:chatId', authenticateToken, (req, res) // ======================================================== app.post('/api/create-checkout-session', authenticateToken, async (req, res) => { const userId = req.user.userId; - const users = readUsersDB(); - const user = users.find(u => u.userId === userId); - - if (!user) { - return res.status(404).json({ error: "User profile not found." }); - } try { + const user = await User.findOne({ userId: userId }); + if (!user) { + return res.status(404).json({ error: "User profile not found." }); + } + console.log(`Creating Stripe session for user: ${user.name}`); const session = await stripe.checkout.sessions.create({ payment_method_types: ['card'], @@ -585,13 +576,16 @@ app.post('/api/create-checkout-session', authenticateToken, async (req, res) => } catch (err) { console.log("Stripe call failed. Falling back to mock session upgrade for dev mode:", err.message); - // Dynamic mock database update to make the user Premium immediately - const users = readUsersDB(); - const userIndex = users.findIndex(u => u.userId === userId); - if (userIndex !== -1) { - users[userIndex].isPremium = true; - writeUsersDB(users); - console.log(`[MOCK UPGRADE] Successfully upgraded user ${users[userIndex].name} to Premium status!`); + // Dynamic MongoDB update to make the user Premium immediately + try { + const user = await User.findOne({ userId: userId }); + if (user) { + user.isPremium = true; + await user.save(); + console.log(`[MOCK UPGRADE] Successfully upgraded user ${user.name} to Premium status in MongoDB!`); + } + } catch (dbErr) { + console.error("Mock database upgrade failed:", dbErr.message); } // Return local checkout success redirect url to React @@ -599,17 +593,20 @@ app.post('/api/create-checkout-session', authenticateToken, async (req, res) => } }); -app.post('/api/mock-upgrade', authenticateToken, (req, res) => { +app.post('/api/mock-upgrade', authenticateToken, async (req, res) => { const userId = req.user.userId; - const users = readUsersDB(); - const userIndex = users.findIndex(u => u.userId === userId); - if (userIndex !== -1) { - users[userIndex].isPremium = true; - writeUsersDB(users); - console.log(`[MOCK UPGRADE] Successfully upgraded user ${users[userIndex].name} via checkout page!`); - return res.json({ success: true, message: "Upgraded successfully!" }); + try { + const user = await User.findOne({ userId: userId }); + if (user) { + user.isPremium = true; + await user.save(); + console.log(`[MOCK UPGRADE] Successfully upgraded user ${user.name} via checkout page in MongoDB!`); + return res.json({ success: true, message: "Upgraded successfully!" }); + } + res.status(404).json({ error: "User not found." }); + } catch (err) { + res.status(500).json({ error: "Failed to upgrade user." }); } - res.status(404).json({ error: "User not found." }); }); // ======================================================== @@ -617,13 +614,12 @@ app.post('/api/mock-upgrade', authenticateToken, (req, res) => { // ======================================================== app.post('/api/create-razorpay-order', authenticateToken, async (req, res) => { const userId = req.user.userId; - const users = readUsersDB(); - const user = users.find(u => u.userId === userId); - if (!user) { - return res.status(404).json({ error: "User profile not found." }); - } - try { + const user = await User.findOne({ userId: userId }); + if (!user) { + return res.status(404).json({ error: "User profile not found." }); + } + const options = { amount: 1000, // Amount is in INR paise (1000 paise = 10 INR) currency: "INR", @@ -642,7 +638,7 @@ app.post('/api/create-razorpay-order', authenticateToken, async (req, res) => { }); } catch (err) { console.error("Razorpay order creation failed:", err.message); - + // Mock fallback order creation if Razorpay API keys are placeholders console.log("⚠️ Falling back to Mock Razorpay Order Creation for Dev Mode..."); res.json({ @@ -655,13 +651,13 @@ app.post('/api/create-razorpay-order', authenticateToken, async (req, res) => { } }); -app.post('/api/verify-razorpay-payment', authenticateToken, (req, res) => { +app.post('/api/verify-razorpay-payment', authenticateToken, async (req, res) => { const userId = req.user.userId; const { razorpay_order_id, razorpay_payment_id, razorpay_signature, isMock } = req.body; - + const crypto = require("crypto"); const secret = process.env.RAZORPAY_KEY_SECRET || 'placeholderSecret'; - + let isSignatureValid = false; if (isMock) { @@ -675,16 +671,20 @@ app.post('/api/verify-razorpay-payment', authenticateToken, (req, res) => { } if (isSignatureValid) { - const users = readUsersDB(); - const userIndex = users.findIndex(u => u.userId === userId); - if (userIndex !== -1) { - users[userIndex].isPremium = true; - users[userIndex].razorpayPaymentId = razorpay_payment_id || "pay_mock_" + Date.now(); - writeUsersDB(users); - console.log(`[RAZORPAY SUCCESS] Upgraded user ${users[userIndex].name} to Premium status!`); - return res.json({ success: true, message: "Payment verified, upgraded successfully." }); - } - return res.status(404).json({ error: "User not found." }); + try { + const user = await User.findOne({ userId: userId }); + if (user) { + user.isPremium = true; + user.razorpayPaymentId = razorpay_payment_id || "pay_mock_" + Date.now(); + await user.save(); + console.log(`[RAZORPAY SUCCESS] Upgraded user ${user.name} to Premium status in MongoDB!`); + return res.json({ success: true, message: "Payment verified, upgraded successfully." }); + } + return res.status(404).json({ error: "User not found." }); + } catch (err) { + console.error("Razorpay verification DB error:", err.message); + return res.status(500).json({ error: "Failed to verify database status." }); + } } else { console.error("Razorpay signature verification failed."); return res.status(400).json({ error: "Payment verification failed. Invalid signature." }); @@ -694,46 +694,53 @@ app.post('/api/verify-razorpay-payment', authenticateToken, (req, res) => { // ======================================================== // 👤 USER PROFILE & BILLING ENDPOINTS (Protected by Auth) // ======================================================== -app.put('/api/user-profile', authenticateToken, (req, res) => { +app.put('/api/user-profile', authenticateToken, async (req, res) => { const userId = req.user.userId; const { name, email, phoneNumber, profilePhoto } = req.body; - + if (!name || !email) { return res.status(400).json({ error: "Name and email are required fields." }); } - const users = readUsersDB(); - const userIndex = users.findIndex(u => u.userId === userId); - - if (userIndex !== -1) { - users[userIndex].name = name; - users[userIndex].email = email; - if (phoneNumber !== undefined) users[userIndex].phoneNumber = phoneNumber; - if (profilePhoto !== undefined) users[userIndex].profilePhoto = profilePhoto; - - writeUsersDB(users); - console.log(`[PROFILE UPDATE] Updated user details for ${name}`); - return res.json({ success: true, user: users[userIndex] }); + try { + // Find and update user in MongoDB + const user = await User.findOne({ userId: userId }); + if (!user) { + return res.status(404).json({ error: "User profile not found." }); + } + + user.name = name; + user.email = email.toLowerCase(); + if (phoneNumber !== undefined) user.phoneNumber = phoneNumber; + if (profilePhoto !== undefined) user.profilePhoto = profilePhoto; + + await user.save(); + console.log(`[PROFILE UPDATE] Updated user details in MongoDB for ${name}`); + return res.json({ success: true, user }); + } catch (err) { + console.error("Profile update error:", err.message); + res.status(500).json({ error: "Failed to update user profile." }); } - - res.status(404).json({ error: "User profile not found." }); }); - -app.post('/api/cancel-subscription', authenticateToken, (req, res) => { +app.post('/api/cancel-subscription', authenticateToken, async (req, res) => { const userId = req.user.userId; - const users = readUsersDB(); - const userIndex = users.findIndex(u => u.userId === userId); - - if (userIndex !== -1) { - users[userIndex].isPremium = false; - delete users[userIndex].razorpayPaymentId; - - writeUsersDB(users); - console.log(`[SUBSCRIPTION CANCEL] Downgraded user ${users[userIndex].name} to Free plan.`); + + try { + const user = await User.findOne({ userId: userId }); + if (!user) { + return res.status(404).json({ error: "User not found." }); + } + + user.isPremium = false; + user.razorpayPaymentId = undefined; // Remove billing reference + + await user.save(); + console.log(`[SUBSCRIPTION CANCEL] Downgraded user ${user.name} to Free plan in MongoDB.`); return res.json({ success: true, message: "Subscription cancelled successfully." }); + } catch (err) { + console.error("Subscription cancel error:", err.message); + res.status(500).json({ error: "Failed to cancel subscription." }); } - - res.status(404).json({ error: "User not found." }); }); // ROUTE 3: Stripe Webhook (Stays unauthenticated since it is external Stripe server callback) @@ -754,13 +761,16 @@ app.post('/api/webhook', express.raw({ type: 'application/json' }), async (req, console.log(`💳 Webhook Triggered! Payment confirmed for User ID: ${userId}`); - const users = readUsersDB(); - const userIndex = users.findIndex(u => u.userId === userId); - if (userIndex !== -1) { - users[userIndex].isPremium = true; - users[userIndex].stripeCustomerId = session.customer; - writeUsersDB(users); - console.log(` SUCCESS: User ${users[userIndex].name} is now Premium!`); + try { + const user = await User.findOne({ userId: userId }); + if (user) { + user.isPremium = true; + user.stripeCustomerId = session.customer; + await user.save(); + console.log(` SUCCESS: User ${user.name} is now Premium in MongoDB!`); + } + } catch (err) { + console.error("Webhook DB update failed:", err.message); } } @@ -770,33 +780,38 @@ app.post('/api/webhook', express.raw({ type: 'application/json' }), async (req, // ======================================================== // 📊 ROUTE 4: Fetch Current User Status (Protected by Auth) // ======================================================== -app.get('/api/user-status', authenticateToken, (req, res) => { - const users = readUsersDB(); - const user = users.find(u => u.userId === req.user.userId); - if (user) { - res.json({ - isPremium: user.isPremium, - name: user.name, - queryCount: user.queryCount, - totalStorageBytes: user.totalStorageBytes, - uploadedDocuments: user.uploadedDocuments - }); - } else { - res.status(404).json({ error: "User not found" }); +app.get('/api/user-status', authenticateToken, async (req, res) => { + try { + const user = await User.findOne({ userId: req.user.userId }); + if (user) { + res.json({ + isPremium: user.isPremium, + name: user.name, + email: user.email, + phoneNumber: user.phoneNumber || '', + profilePhoto: user.profilePhoto || '👨‍💻', + queryCount: user.queryCount, + totalStorageBytes: user.totalStorageBytes, + uploadedDocuments: user.uploadedDocuments + }); + } else { + res.status(404).json({ error: "User not found" }); + } + } catch (err) { + console.error("User status error:", err.message); + res.status(500).json({ error: "Failed to fetch user status." }); } }); - // ROUTE 5: Fetch list of ingested documents (Private list proxying, filtered by ownership) app.get('/api/documents', authenticateToken, async (req, res) => { const userId = req.user.userId; - const users = readUsersDB(); - const user = users.find(u => u.userId === userId); - - if (!user) { - return res.status(404).json({ error: "User not found" }); - } try { + const user = await User.findOne({ userId: userId }); + if (!user) { + return res.status(404).json({ error: "User not found" }); + } + const response = await axios.get(`${process.env.PYTHON_BACKEND_URL}/api/documents`); // Filter list to only contain document records that belong to the current authenticated user const userDocIds = new Set((user.uploadedDocuments || []).map(d => d.id)); @@ -812,20 +827,19 @@ app.get('/api/documents', authenticateToken, async (req, res) => { app.get('/api/documents/:docId', authenticateToken, async (req, res) => { const { docId } = req.params; const userId = req.user.userId; - const users = readUsersDB(); - const user = users.find(u => u.userId === userId); - if (!user) { - return res.status(404).json({ error: "User not found" }); - } + try { + const user = await User.findOne({ userId: userId }); + if (!user) { + return res.status(404).json({ error: "User not found" }); + } - // Verify ownership of the requested document ID - const hasDoc = (user.uploadedDocuments || []).some(d => d.id === docId); - if (!hasDoc) { - return res.status(403).json({ error: "Access Denied. You do not own this document." }); - } + // Verify ownership of the requested document ID + const hasDoc = (user.uploadedDocuments || []).some(d => d.id === docId); + if (!hasDoc) { + return res.status(403).json({ error: "Access Denied. You do not own this document." }); + } - try { const response = await axios.get(`${process.env.PYTHON_BACKEND_URL}/api/documents/${docId}`); res.json(response.data); } catch (err) { @@ -839,34 +853,29 @@ app.delete('/api/documents/:docId', authenticateToken, async (req, res) => { const { docId } = req.params; const userId = req.user.userId; - const users = readUsersDB(); - const user = users.find(u => u.userId === userId); - if (!user) { - return res.status(404).json({ error: "User not found" }); - } + try { + const user = await User.findOne({ userId: userId }); + if (!user) { + return res.status(404).json({ error: "User not found" }); + } - // Verify ownership before deleting - const docIndex = (user.uploadedDocuments || []).findIndex(d => d.id === docId); - if (docIndex === -1) { - return res.status(403).json({ error: "Access Denied. You do not own this document." }); - } + // Verify ownership before deleting + const docIndex = (user.uploadedDocuments || []).findIndex(d => d.id === docId); + if (docIndex === -1) { + return res.status(403).json({ error: "Access Denied. You do not own this document." }); + } - try { console.log(`Forwarding deletion request to Python: "${docId}"`); const response = await axios.delete(`${process.env.PYTHON_BACKEND_URL}/api/documents/${docId}`); - // Update user statistics in users.json + // Update user statistics in MongoDB const deletedDoc = user.uploadedDocuments[docIndex]; user.totalStorageBytes = Math.max(0, user.totalStorageBytes - (deletedDoc.sizeBytes || 0)); user.uploadedDocuments.splice(docIndex, 1); - writeUsersDB(users); + await user.save(); - // Clean chats.json (delete all threads related to this project document) - const chatsDb = readChatsDB(); - if (chatsDb[userId] && chatsDb[userId][docId]) { - delete chatsDb[userId][docId]; - writeChatsDB(chatsDb); - } + // Clean chats/messages related to this project document in MongoDB + await ProjectChat.deleteOne({ userId: userId, projectId: docId }); res.json(response.data); } catch (err) { @@ -875,6 +884,14 @@ app.delete('/api/documents/:docId', authenticateToken, async (req, res) => { } }); +// Serve frontend React static build files +app.use(express.static(path.join(__dirname, 'public'))); + +// Catch-all route to serve React index.html for client-side routing +app.get('*', (req, res) => { + res.sendFile(path.join(__dirname, 'public', 'index.html')); +}); + app.listen(PORT, () => { console.log(`⚡ Node.js API Gateway is active on http://localhost:${PORT}`); }); \ No newline at end of file diff --git a/start.sh b/start.sh new file mode 100644 index 0000000000000000000000000000000000000000..04e8bd65b87e01aea2f67c0151513df63a6cfde0 --- /dev/null +++ b/start.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Exit immediately if any command fails +set -e + +echo "🚀 Launching Python RAG FastAPI Engine on port 8000..." +# Bind Python to loopback interface 127.0.0.1:8000 for secure isolation +python -m uvicorn app:app --host 127.0.0.1 --port 8000 & + +echo "⚡ Starting Node.js API Gateway on exposed port $PORT..." +# Bind Node Gateway to $PORT (injected by Hugging Face Spaces, default 7860) +cd gateway +exec node server.js