Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: simplify version checking
  • Loading branch information
nbbeeken committed Apr 13, 2023
commit be8900787cbcc0ccafe32fdf0268f3c8c5266e32
19 changes: 2 additions & 17 deletions src/cmap/handshake/client_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,28 +261,13 @@ declare const Bun: { (): void; version?: string } | undefined;
*/
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';
const version = typeof Deno?.version?.deno === 'string' ? Deno?.version?.deno : '0.0.0-unknown';

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

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

return `Bun v${version}, ${os.endianness()}`;
}
Expand Down
28 changes: 11 additions & 17 deletions test/unit/cmap/handshake/client_metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,31 +311,25 @@ describe('client metadata module', () => {
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');
expect(metadata.platform).to.equal('Deno v0.0.0-unknown, LE');
});

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');
expect(metadata.platform).to.equal('Deno v0.0.0-unknown, 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');
expect(metadata.platform).to.equal('Deno v0.0.0-unknown, LE');
});

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

Expand Down Expand Up @@ -365,21 +359,21 @@ describe('client metadata module', () => {
static version = 1;
};
const metadata = makeClientMetadata({ driverInfo: {} });
expect(metadata.platform).to.equal('Bun v0.0.0, LE');
expect(metadata.platform).to.equal('Bun v0.0.0-unknown, LE');
});

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

it('ignores version if Bun is not a function', () => {
globalThis.Bun = { version: '1.2.3' };
it('ignores version if Bun is nullish', () => {
globalThis.Bun = null;
const metadata = makeClientMetadata({ driverInfo: { platform: 'myPlatform' } });
expect(metadata.platform).to.equal('Bun v0.0.0, LE|myPlatform');
expect(metadata.platform).to.equal('Bun v0.0.0-unknown, LE|myPlatform');
});
});
});
Expand Down