Skip to content

Commit 0c36b12

Browse files
committed
chore: prepare for BSON fix
1 parent 0b952f6 commit 0c36b12

File tree

2 files changed

+35
-47
lines changed

2 files changed

+35
-47
lines changed

src/cmap/connect.ts

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,31 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
390390
const connectEvent = useTLS ? 'secureConnect' : 'connect';
391391
socket
392392
.once(connectEvent, () => resolve(socket))
393-
.once('error', error => reject(connectionFailureError('error', error)))
393+
.once('error', cause =>
394+
reject(new MongoNetworkError(MongoError.buildErrorMessage(cause), { cause }))
395+
)
394396
.once('timeout', () => {
395397
reject(
396398
new MongoNetworkTimeoutError(
397399
`Socket '${connectEvent}' timed out after ${(performance.now() - start) | 0}ms (connectTimeoutMS: ${connectTimeoutMS})`
398400
)
399401
);
400402
})
401-
.once('close', () => reject(connectionFailureError('close')));
403+
.once('close', () =>
404+
reject(
405+
new MongoNetworkError(
406+
`Socket closed after ${(performance.now() - start) | 0} during connection establishment`
407+
)
408+
)
409+
);
402410

403411
if (options.cancellationToken != null) {
404-
cancellationHandler = () => reject(connectionFailureError('cancel'));
412+
cancellationHandler = () =>
413+
reject(
414+
new MongoNetworkError(
415+
`Socket connection establishment was cancelled after ${(performance.now() - start) | 0}`
416+
)
417+
);
405418
options.cancellationToken.once('cancel', cancellationHandler);
406419
}
407420
}
@@ -454,9 +467,11 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
454467

455468
socks ??= loadSocks();
456469

470+
let existingSocket: Stream;
471+
457472
try {
458473
// Then, establish the Socks5 proxy connection:
459-
const { socket } = await socks.SocksClient.createConnection({
474+
const connection = await socks.SocksClient.createConnection({
460475
existing_socket: rawSocket,
461476
timeout: options.connectTimeoutMS,
462477
command: 'connect',
@@ -473,35 +488,12 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
473488
password: options.proxyPassword || undefined
474489
}
475490
});
476-
477-
// Finally, now treat the resulting duplex stream as the
478-
// socket over which we send and receive wire protocol messages:
479-
return await makeSocket({
480-
...options,
481-
existingSocket: socket,
482-
proxyHost: undefined
483-
});
484-
} catch (error) {
485-
throw connectionFailureError('error', error);
491+
existingSocket = connection.socket;
492+
} catch (cause) {
493+
throw new MongoNetworkError(MongoError.buildErrorMessage(cause), { cause });
486494
}
487-
}
488495

489-
function connectionFailureError(type: 'error', cause: Error): MongoNetworkError;
490-
function connectionFailureError(type: 'close' | 'timeout' | 'cancel'): MongoNetworkError;
491-
function connectionFailureError(
492-
type: 'error' | 'close' | 'timeout' | 'cancel',
493-
cause?: Error
494-
): MongoNetworkError {
495-
switch (type) {
496-
case 'error':
497-
return new MongoNetworkError(MongoError.buildErrorMessage(cause), { cause });
498-
case 'timeout':
499-
return new MongoNetworkTimeoutError('connection timed out');
500-
case 'close':
501-
return new MongoNetworkError('connection closed');
502-
case 'cancel':
503-
return new MongoNetworkError('connection establishment was cancelled');
504-
default:
505-
return new MongoNetworkError('unknown network error');
506-
}
496+
// Finally, now treat the resulting duplex stream as the
497+
// socket over which we send and receive wire protocol messages:
498+
return await makeSocket({ ...options, existingSocket, proxyHost: undefined });
507499
}

test/benchmarks/driverBench/index.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const MongoBench = require('../mongoBench');
44
const os = require('node:os');
5-
const util = require('node:util');
65
const process = require('node:process');
76

87
const Runner = MongoBench.Runner;
@@ -33,10 +32,7 @@ const systemInfo = () =>
3332
`- arch: ${os.arch()}`,
3433
`- os: ${process.platform} (${os.release()})`,
3534
`- ram: ${platform.ram}`,
36-
`- node: ${process.version}`,
37-
`- driver: ${MONGODB_DRIVER_VERSION} (${MONGODB_DRIVER_REVISION}): ${MONGODB_DRIVER_PATH}`,
38-
` - options ${util.inspect(MONGODB_CLIENT_OPTIONS)}`,
39-
`- bson: ${MONGODB_BSON_VERSION} (${MONGODB_BSON_REVISION}): (${MONGODB_BSON_PATH})\n`
35+
`- node: ${process.version}\n`
4036
].join('\n');
4137
console.log(systemInfo());
4238

@@ -67,18 +63,18 @@ benchmarkRunner
6763
]);
6864

6965
const readBench = average([
70-
// microBench.singleBench.findOne,
71-
// microBench.multiBench.findManyAndEmptyCursor,
72-
// microBench.multiBench.gridFsDownload,
66+
microBench.singleBench.findOne,
67+
microBench.multiBench.findManyAndEmptyCursor,
68+
microBench.multiBench.gridFsDownload,
7369
microBench.parallel.gridfsMultiFileDownload,
7470
microBench.parallel.ldjsonMultiFileExport
7571
]);
7672
const writeBench = average([
77-
// microBench.singleBench.smallDocInsertOne,
78-
// microBench.singleBench.largeDocInsertOne,
79-
// microBench.multiBench.smallDocBulkInsert,
80-
// microBench.multiBench.largeDocBulkInsert,
81-
// microBench.multiBench.gridFsUpload,
73+
microBench.singleBench.smallDocInsertOne,
74+
microBench.singleBench.largeDocInsertOne,
75+
microBench.multiBench.smallDocBulkInsert,
76+
microBench.multiBench.largeDocBulkInsert,
77+
microBench.multiBench.gridFsUpload,
8278
microBench.parallel.ldjsonMultiFileUpload,
8379
microBench.parallel.gridfsMultiFileUpload
8480
]);
@@ -121,6 +117,6 @@ benchmarkRunner
121117
return writeFile('results.json', results);
122118
})
123119
.catch(err => {
124-
console.error('failure: ', err.name, err.message, err.stack);
120+
console.error('failure: ', err.name, err.message);
125121
process.exit(1);
126122
});

0 commit comments

Comments
 (0)