-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathmongodb.ts
218 lines (209 loc) · 9.04 KB
/
mongodb.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* eslint-disable @typescript-eslint/no-restricted-imports */
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as process from 'node:process';
import * as vm from 'node:vm';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function printExports() {
function* walk(root: string): Generator<string> {
const directoryContents = fs.readdirSync(root);
for (const filepath of directoryContents) {
const fullPath = path.join(root, filepath);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
yield* walk(fullPath);
} else if (stat.isFile()) {
yield fullPath;
}
}
}
const driverSourceFiles = Array.from(walk(path.resolve(__dirname, '..', 'src')));
for (const srcFile of driverSourceFiles) {
console.log(`export * from '${path.relative(__dirname, srcFile)}';`);
}
}
/**
* Using node's require resolution logic this function will locate the entrypoint for the `'mongodb-legacy'` module,
* then execute the `mongodb-legacy` module in a `vm` context that replaces the global require function with a custom
* implementation. The custom version of `require` will return the local instance of the driver import (magically compiled by ts-node) when
* the module specifier is 'mongodb' and otherwise defer to the normal require behavior to import relative files and stdlib modules.
* Each of the legacy module's patched classes are placed on the input object.
*
* @param exportsToOverride - An object that is an import of the MongoDB driver to be modified by this function
*/
function importMongoDBLegacy(exportsToOverride: Record<string, unknown>) {
const mongodbLegacyEntryPoint = require.resolve('mongodb-legacy');
const mongodbLegacyLocation = path.dirname(mongodbLegacyEntryPoint);
const mongodbLegacyIndex = fs.readFileSync(mongodbLegacyEntryPoint, {
encoding: 'utf8'
});
// eslint-disable-next-line @typescript-eslint/no-var-requires
const localMongoDB = require('../src/index');
const ctx = vm.createContext({
module: { exports: null },
require: (mod: string) => {
if (mod === 'mongodb') {
return localMongoDB;
} else if (mod.startsWith('.')) {
return require(path.join(mongodbLegacyLocation, mod));
}
return require(mod);
}
});
vm.runInContext(mongodbLegacyIndex, ctx);
const mongodbLegacy = ctx.module.exports;
Object.defineProperty(exportsToOverride, 'Admin', { get: () => mongodbLegacy.Admin });
Object.defineProperty(exportsToOverride, 'FindCursor', { get: () => mongodbLegacy.FindCursor });
Object.defineProperty(exportsToOverride, 'ListCollectionsCursor', {
get: () => mongodbLegacy.ListCollectionsCursor
});
Object.defineProperty(exportsToOverride, 'ListIndexesCursor', {
get: () => mongodbLegacy.ListIndexesCursor
});
Object.defineProperty(exportsToOverride, 'AggregationCursor', {
get: () => mongodbLegacy.AggregationCursor
});
Object.defineProperty(exportsToOverride, 'ChangeStream', {
get: () => mongodbLegacy.ChangeStream
});
Object.defineProperty(exportsToOverride, 'Collection', { get: () => mongodbLegacy.Collection });
Object.defineProperty(exportsToOverride, 'Db', { get: () => mongodbLegacy.Db });
Object.defineProperty(exportsToOverride, 'GridFSBucket', {
get: () => mongodbLegacy.GridFSBucket
});
Object.defineProperty(exportsToOverride, 'ClientSession', {
get: () => mongodbLegacy.ClientSession
});
Object.defineProperty(exportsToOverride, 'MongoClient', { get: () => mongodbLegacy.MongoClient });
Object.defineProperty(exportsToOverride, 'ClientSession', {
get: () => mongodbLegacy.ClientSession
});
Object.defineProperty(exportsToOverride, 'GridFSBucketWriteStream', {
get: () => mongodbLegacy.GridFSBucketWriteStream
});
Object.defineProperty(exportsToOverride, 'OrderedBulkOperation', {
get: () => mongodbLegacy.OrderedBulkOperation
});
Object.defineProperty(exportsToOverride, 'UnorderedBulkOperation', {
get: () => mongodbLegacy.UnorderedBulkOperation
});
}
export * from '../src/admin';
export * from '../src/bson';
export * from '../src/bulk/common';
export * from '../src/bulk/ordered';
export * from '../src/bulk/unordered';
export * from '../src/change_stream';
export * from '../src/cmap/auth/auth_provider';
export * from '../src/cmap/auth/gssapi';
export * from '../src/cmap/auth/mongo_credentials';
export * from '../src/cmap/auth/mongocr';
export * from '../src/cmap/auth/mongodb_aws';
export * from '../src/cmap/auth/mongodb_oidc';
export * from '../src/cmap/auth/mongodb_oidc/aws_service_workflow';
export * from '../src/cmap/auth/mongodb_oidc/azure_service_workflow';
export * from '../src/cmap/auth/mongodb_oidc/azure_token_cache';
export * from '../src/cmap/auth/mongodb_oidc/callback_lock_cache';
export * from '../src/cmap/auth/mongodb_oidc/callback_workflow';
export * from '../src/cmap/auth/mongodb_oidc/service_workflow';
export * from '../src/cmap/auth/mongodb_oidc/token_entry_cache';
export * from '../src/cmap/auth/plain';
export * from '../src/cmap/auth/providers';
export * from '../src/cmap/auth/scram';
export * from '../src/cmap/auth/x509';
export * from '../src/cmap/command_monitoring_events';
export * from '../src/cmap/commands';
export * from '../src/cmap/connect';
export * from '../src/cmap/connection';
export * from '../src/cmap/connection_pool';
export * from '../src/cmap/connection_pool_events';
export * from '../src/cmap/errors';
export * from '../src/cmap/handshake/client_metadata';
export * from '../src/cmap/message_stream';
export * from '../src/cmap/metrics';
export * from '../src/cmap/stream_description';
export * from '../src/cmap/wire_protocol/compression';
export * from '../src/cmap/wire_protocol/constants';
export * from '../src/cmap/wire_protocol/shared';
export * from '../src/collection';
export * from '../src/connection_string';
export * from '../src/constants';
export * from '../src/cursor/abstract_cursor';
export * from '../src/cursor/aggregation_cursor';
export * from '../src/cursor/change_stream_cursor';
export * from '../src/cursor/find_cursor';
export * from '../src/cursor/list_collections_cursor';
export * from '../src/cursor/list_indexes_cursor';
export * from '../src/cursor/run_command_cursor';
export * from '../src/db';
export * from '../src/deps';
export * from '../src/encrypter';
export * from '../src/error';
export * from '../src/explain';
export * from '../src/gridfs/download';
export * from '../src/gridfs/index';
export * from '../src/gridfs/upload';
export * from '../src/mongo_client';
export * from '../src/mongo_logger';
export * from '../src/mongo_types';
export * from '../src/operations/aggregate';
export * from '../src/operations/bulk_write';
export * from '../src/operations/collections';
export * from '../src/operations/command';
export * from '../src/operations/common_functions';
export * from '../src/operations/count';
export * from '../src/operations/count_documents';
export * from '../src/operations/create_collection';
export * from '../src/operations/delete';
export * from '../src/operations/distinct';
export * from '../src/operations/drop';
export * from '../src/operations/estimated_document_count';
export * from '../src/operations/execute_operation';
export * from '../src/operations/find';
export * from '../src/operations/find_and_modify';
export * from '../src/operations/get_more';
export * from '../src/operations/indexes';
export * from '../src/operations/insert';
export * from '../src/operations/is_capped';
export * from '../src/operations/kill_cursors';
export * from '../src/operations/list_collections';
export * from '../src/operations/list_databases';
export * from '../src/operations/operation';
export * from '../src/operations/options_operation';
export * from '../src/operations/profiling_level';
export * from '../src/operations/remove_user';
export * from '../src/operations/rename';
export * from '../src/operations/run_command';
export * from '../src/operations/set_profiling_level';
export * from '../src/operations/stats';
export * from '../src/operations/update';
export * from '../src/operations/validate_collection';
export * from '../src/read_concern';
export * from '../src/read_preference';
export * from '../src/sdam/common';
export * from '../src/sdam/events';
export * from '../src/sdam/monitor';
export * from '../src/sdam/server';
export * from '../src/sdam/server_description';
export * from '../src/sdam/server_selection';
export * from '../src/sdam/srv_polling';
export * from '../src/sdam/topology';
export * from '../src/sdam/topology_description';
export * from '../src/sessions';
export * from '../src/sort';
export * from '../src/transactions';
export * from '../src/utils';
export * from '../src/write_concern';
// Must be last for precedence
export * from '../src/index';
/**
* TODO(NODE-4979): ENABLE_MONGODB_LEGACY is 'true' by default for now
*/
const ENABLE_MONGODB_LEGACY =
typeof process.env.ENABLE_MONGODB_LEGACY === 'string' && process.env.ENABLE_MONGODB_LEGACY !== ''
? process.env.ENABLE_MONGODB_LEGACY
: 'true';
if (ENABLE_MONGODB_LEGACY === 'true') {
// Override our own exports with the legacy patched ones
importMongoDBLegacy(module.exports);
}