Skip to content

Commit

Permalink
feat(NODE-5188): add alternative runtime detection to client metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Apr 12, 2023
1 parent 0424080 commit 5f10d3d
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 5 deletions.
62 changes: 57 additions & 5 deletions src/cmap/handshake/client_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ export function makeClientMetadata(options: MakeClientMetadataOptions): ClientMe
);
}

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

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

return null;
}

/**
* @internal
* This type represents the global Deno object and the minimal type contract we
* expect it to satisfy. In order to not ship code in the driver that would break
* future versions of this runtime assume all properties are nullish.
*/
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. In order to not ship code in the driver that would break
* future versions of this runtime assume all properties are nullish.
*/
declare const Bun: { version?: string } | undefined;

/**
* @internal
* Get current JavaScript runtime platform
*
* NOTE: The version information fetching is intentionally written aggressively defensive
* to avoid having a released driver version that becomes incompatible
* with a future change to these global objects
*/
function getRuntimeInfo(): string {
if ('Deno' in globalThis) {
const version =
Deno != null &&
typeof Deno === 'object' &&
'version' in Deno &&
Deno.version != null &&
typeof Deno.version === 'object' &&
'deno' in Deno.version &&
typeof Deno.version.deno === 'string'
? Deno.version.deno
: '0.0.0';

return `Deno v${version}, ${os.endianness()}`;
}

if ('Bun' in globalThis) {
const version =
Bun != null && typeof Bun === 'object' && 'version' in Bun && typeof Bun.version === 'string'
? Bun.version
: '0.0.0';

return `Bun v${version}, ${os.endianness()}`;
}

return `Node.js ${process.version}, ${os.endianness()}`;
}
80 changes: 80 additions & 0 deletions test/unit/cmap/handshake/client_metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,86 @@ describe('client metadata module', () => {
});
});
});

context('when globalThis indicates alternative runtime', () => {
context('deno', () => {
afterEach(() => {
expect(delete globalThis.Deno, 'failed to delete Deno global').to.be.true;
});

it('sets platform to Deno', () => {
globalThis.Deno = { version: { deno: '1.2.3' } };
const metadata = makeClientMetadata({ driverInfo: {} });
expect(metadata.platform).to.equal('Deno v1.2.3, LE');
});

it('sets platform to Deno with driverInfo.platform', () => {
globalThis.Deno = { version: { deno: '1.2.3' } };
const metadata = makeClientMetadata({ driverInfo: { platform: 'myPlatform' } });
expect(metadata.platform).to.equal('Deno v1.2.3, LE|myPlatform');
});

it('ignores version if Deno.version.deno is not a string', () => {
globalThis.Deno = { version: { deno: 1 } };
const metadata = makeClientMetadata({ driverInfo: {} });
expect(metadata.platform).to.equal('Deno v0.0.0, LE');
});

it('ignores version if Deno.version.deno is not a string and sets driverInfo.platform', () => {
globalThis.Deno = { version: { deno: 1 } };
const metadata = makeClientMetadata({ driverInfo: { platform: 'myPlatform' } });
expect(metadata.platform).to.equal('Deno v0.0.0, LE|myPlatform');
});

it('ignores version if Deno.version does not have a deno property', () => {
globalThis.Deno = { version: { somethingElse: '1.2.3' } };
const metadata = makeClientMetadata({ driverInfo: {} });
expect(metadata.platform).to.equal('Deno v0.0.0, LE');
});

it('ignores version if Deno.version is null', () => {
globalThis.Deno = { version: null };
const metadata = makeClientMetadata({ driverInfo: {} });
expect(metadata.platform).to.equal('Deno v0.0.0, LE');
});

it('ignores version if Deno does not have a version property', () => {
globalThis.Deno = { version: null };
const metadata = makeClientMetadata({ driverInfo: {} });
expect(metadata.platform).to.equal('Deno v0.0.0, LE');
});
});

context('bun', () => {
afterEach(() => {
expect(delete globalThis.Bun, 'failed to delete Bun global').to.be.true;
});

it('sets platform to Bun', () => {
globalThis.Bun = { version: '1.2.3' };
const metadata = makeClientMetadata({ driverInfo: {} });
expect(metadata.platform).to.equal('Bun v1.2.3, LE');
});

it('sets platform to Bun with driverInfo.platform', () => {
globalThis.Bun = { version: '1.2.3' };
const metadata = makeClientMetadata({ driverInfo: { platform: 'myPlatform' } });
expect(metadata.platform).to.equal('Bun v1.2.3, LE|myPlatform');
});

it('ignores version if Bun.version is not a string', () => {
globalThis.Bun = { version: 1 };
const metadata = makeClientMetadata({ driverInfo: {} });
expect(metadata.platform).to.equal('Bun v0.0.0, LE');
});

it('ignores version if Bun.version is not a string and sets driverInfo.platform', () => {
globalThis.Bun = { version: 1 };
const metadata = makeClientMetadata({ driverInfo: { platform: 'myPlatform' } });
expect(metadata.platform).to.equal('Bun v0.0.0, LE|myPlatform');
});
});
});
});

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

0 comments on commit 5f10d3d

Please sign in to comment.