Skip to content

Commit fba16ad

Browse files
nbbeekendurran
andauthored
feat(NODE-5199): add alternative runtime detection to client metadata (#3647)
Co-authored-by: Durran Jordan <durran@gmail.com>
1 parent e0b20f1 commit fba16ad

File tree

4 files changed

+134
-10
lines changed

4 files changed

+134
-10
lines changed

.evergreen/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,7 +2343,7 @@ tasks:
23432343
commands:
23442344
- func: install dependencies
23452345
vars:
2346-
NODE_LTS_NAME: erbium
2346+
NODE_LTS_NAME: hydrogen
23472347
- func: check types
23482348
vars:
23492349
TS_VERSION: next
@@ -2363,7 +2363,7 @@ tasks:
23632363
commands:
23642364
- func: install dependencies
23652365
vars:
2366-
NODE_LTS_NAME: erbium
2366+
NODE_LTS_NAME: hydrogen
23672367
- func: check types
23682368
vars:
23692369
TS_VERSION: current
@@ -2373,7 +2373,7 @@ tasks:
23732373
commands:
23742374
- func: install dependencies
23752375
vars:
2376-
NODE_LTS_NAME: erbium
2376+
NODE_LTS_NAME: hydrogen
23772377
- func: check types
23782378
vars:
23792379
TS_VERSION: 4.1.6

.evergreen/generate_evergreen_tasks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ function* makeTypescriptTasks() {
495495
{
496496
func: 'install dependencies',
497497
vars: {
498-
NODE_LTS_NAME: LOWEST_LTS
498+
NODE_LTS_NAME: LATEST_LTS
499499
}
500500
},
501501
{
@@ -514,7 +514,7 @@ function* makeTypescriptTasks() {
514514
{
515515
func: 'install dependencies',
516516
vars: {
517-
NODE_LTS_NAME: LOWEST_LTS
517+
NODE_LTS_NAME: LATEST_LTS
518518
}
519519
},
520520
{ func: 'run typescript next' }

src/cmap/handshake/client_metadata.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ export function makeClientMetadata(options: MakeClientMetadataOptions): ClientMe
116116
);
117117
}
118118

119-
const platformInfo =
120-
platform.length > 0
121-
? `Node.js ${process.version}, ${os.endianness()}|${platform}`
122-
: `Node.js ${process.version}, ${os.endianness()}`;
119+
let runtimeInfo = getRuntimeInfo();
120+
if (platform.length > 0) {
121+
runtimeInfo = `${runtimeInfo}|${platform}`;
122+
}
123123

124-
if (!metadataDocument.ifItFitsItSits('platform', platformInfo)) {
124+
if (!metadataDocument.ifItFitsItSits('platform', runtimeInfo)) {
125125
throw new MongoInvalidArgumentError(
126126
'Unable to include driverInfo platform, metadata cannot exceed 512 bytes'
127127
);
@@ -234,3 +234,39 @@ export function getFAASEnv(): Map<string, string | Int32> | null {
234234

235235
return null;
236236
}
237+
238+
/**
239+
* @internal
240+
* This type represents the global Deno object and the minimal type contract we expect it to satisfy.
241+
*/
242+
declare const Deno: { version?: { deno?: string } } | undefined;
243+
244+
/**
245+
* @internal
246+
* This type represents the global Bun object and the minimal type contract we expect it to satisfy.
247+
*/
248+
declare const Bun: { (): void; version?: string } | undefined;
249+
250+
/**
251+
* @internal
252+
* Get current JavaScript runtime platform
253+
*
254+
* NOTE: The version information fetching is intentionally written defensively
255+
* to avoid having a released driver version that becomes incompatible
256+
* with a future change to these global objects.
257+
*/
258+
function getRuntimeInfo(): string {
259+
if ('Deno' in globalThis) {
260+
const version = typeof Deno?.version?.deno === 'string' ? Deno?.version?.deno : '0.0.0-unknown';
261+
262+
return `Deno v${version}, ${os.endianness()}`;
263+
}
264+
265+
if ('Bun' in globalThis) {
266+
const version = typeof Bun?.version === 'string' ? Bun?.version : '0.0.0-unknown';
267+
268+
return `Bun v${version}, ${os.endianness()}`;
269+
}
270+
271+
return `Node.js ${process.version}, ${os.endianness()}`;
272+
}

test/unit/cmap/handshake/client_metadata.test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,94 @@ describe('client metadata module', () => {
288288
});
289289
});
290290
});
291+
292+
context('when globalThis indicates alternative runtime', () => {
293+
context('deno', () => {
294+
afterEach(() => {
295+
expect(delete globalThis.Deno, 'failed to delete Deno global').to.be.true;
296+
});
297+
298+
it('sets platform to Deno', () => {
299+
globalThis.Deno = { version: { deno: '1.2.3' } };
300+
const metadata = makeClientMetadata({ driverInfo: {} });
301+
expect(metadata.platform).to.equal('Deno v1.2.3, LE');
302+
});
303+
304+
it('sets platform to Deno with driverInfo.platform', () => {
305+
globalThis.Deno = { version: { deno: '1.2.3' } };
306+
const metadata = makeClientMetadata({ driverInfo: { platform: 'myPlatform' } });
307+
expect(metadata.platform).to.equal('Deno v1.2.3, LE|myPlatform');
308+
});
309+
310+
it('ignores version if Deno.version.deno is not a string', () => {
311+
globalThis.Deno = { version: { deno: 1 } };
312+
const metadata = makeClientMetadata({ driverInfo: {} });
313+
expect(metadata.platform).to.equal('Deno v0.0.0-unknown, LE');
314+
});
315+
316+
it('ignores version if Deno.version does not have a deno property', () => {
317+
globalThis.Deno = { version: { somethingElse: '1.2.3' } };
318+
const metadata = makeClientMetadata({ driverInfo: {} });
319+
expect(metadata.platform).to.equal('Deno v0.0.0-unknown, LE');
320+
});
321+
322+
it('ignores version if Deno.version is null', () => {
323+
globalThis.Deno = { version: null };
324+
const metadata = makeClientMetadata({ driverInfo: {} });
325+
expect(metadata.platform).to.equal('Deno v0.0.0-unknown, LE');
326+
});
327+
328+
it('ignores version if Deno is nullish', () => {
329+
globalThis.Deno = null;
330+
const metadata = makeClientMetadata({ driverInfo: {} });
331+
expect(metadata.platform).to.equal('Deno v0.0.0-unknown, LE');
332+
});
333+
});
334+
335+
context('bun', () => {
336+
afterEach(() => {
337+
expect(delete globalThis.Bun, 'failed to delete Bun global').to.be.true;
338+
});
339+
340+
it('sets platform to Bun', () => {
341+
globalThis.Bun = class {
342+
static version = '1.2.3';
343+
};
344+
const metadata = makeClientMetadata({ driverInfo: {} });
345+
expect(metadata.platform).to.equal('Bun v1.2.3, LE');
346+
});
347+
348+
it('sets platform to Bun with driverInfo.platform', () => {
349+
globalThis.Bun = class {
350+
static version = '1.2.3';
351+
};
352+
const metadata = makeClientMetadata({ driverInfo: { platform: 'myPlatform' } });
353+
expect(metadata.platform).to.equal('Bun v1.2.3, LE|myPlatform');
354+
});
355+
356+
it('ignores version if Bun.version is not a string', () => {
357+
globalThis.Bun = class {
358+
static version = 1;
359+
};
360+
const metadata = makeClientMetadata({ driverInfo: {} });
361+
expect(metadata.platform).to.equal('Bun v0.0.0-unknown, LE');
362+
});
363+
364+
it('ignores version if Bun.version is not a string and sets driverInfo.platform', () => {
365+
globalThis.Bun = class {
366+
static version = 1;
367+
};
368+
const metadata = makeClientMetadata({ driverInfo: { platform: 'myPlatform' } });
369+
expect(metadata.platform).to.equal('Bun v0.0.0-unknown, LE|myPlatform');
370+
});
371+
372+
it('ignores version if Bun is nullish', () => {
373+
globalThis.Bun = null;
374+
const metadata = makeClientMetadata({ driverInfo: { platform: 'myPlatform' } });
375+
expect(metadata.platform).to.equal('Bun v0.0.0-unknown, LE|myPlatform');
376+
});
377+
});
378+
});
291379
});
292380

293381
describe('FAAS metadata application to handshake', () => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy