Skip to content

Support Bun with patch changes #2582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import path from "node:path";
import fs from "node:fs";

export const BASE_DIR = new URL("..", import.meta.url);
export const RUNTIME_IDS_WITH_PATCH_VERSIONING = new Set(["bun"]); // List of browsers/runtimes that might add features in patches

/**
* Tests a specified path to see if it's a local checkout of mdn/browser-compat-data
Expand Down
41 changes: 41 additions & 0 deletions lib/ua-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {assert} from "chai";
import {getMajorMinorVersion, parseUA} from "./ua-parser.js";

const browsers = {
bun: {name: "Bun", releases: {"1.0.0": {}, "1.1.15": {}, "1.2.0": {}}},
chrome: {name: "Chrome", releases: {82: {}, 83: {}, 84: {}, 85: {}}},
chrome_android: {name: "Chrome Android", releases: {85: {}}},
deno: {name: "Deno", releases: {1.42: {}}},
Expand Down Expand Up @@ -522,6 +523,46 @@ describe("parseUA", () => {
});
});

it("Bun 1.0.0 (preserves patch version)", () => {
assert.deepEqual(parseUA("!! bun/1.0.0", browsers), {
browser: {id: "bun", name: "Bun"},
version: "1.0.0",
fullVersion: "1.0.0",
os: {name: "", version: ""},
inBcd: true,
});
});

it("Bun 1.1.15 (preserves patch version)", () => {
assert.deepEqual(parseUA("!! bun/1.1.15", browsers), {
browser: {id: "bun", name: "Bun"},
version: "1.1.15",
fullVersion: "1.1.15",
os: {name: "", version: ""},
inBcd: true,
});
});

it("Bun 1.2.0 (preserves patch version)", () => {
assert.deepEqual(parseUA("!! bun/1.2.0", browsers), {
browser: {id: "bun", name: "Bun"},
version: "1.2.0",
fullVersion: "1.2.0",
os: {name: "", version: ""},
inBcd: true,
});
});

it("Bun 1.1.16 (not in BCD)", () => {
assert.deepEqual(parseUA("!! bun/1.1.16", browsers), {
browser: {id: "bun", name: "Bun"},
version: "1.1.16",
fullVersion: "1.1.16",
os: {name: "", version: ""},
inBcd: false,
});
});

it("Chrome on iOS (not in BCD)", () => {
assert.deepEqual(
parseUA(
Expand Down
62 changes: 39 additions & 23 deletions lib/ua-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "compare-versions";
import {UAParser} from "ua-parser-js";
import {ParsedUserAgent} from "../types/types";
import {RUNTIME_IDS_WITH_PATCH_VERSIONING} from "./constants";

/**
* Returns the major version from a given version string.
Expand Down Expand Up @@ -110,7 +111,12 @@ const parseUA = (userAgent: string, browsers: Browsers): ParsedUserAgent => {
}

data.fullVersion = data.fullVersion || ua.browser.version || "0";
data.version = getMajorMinorVersion(data.fullVersion);

if (RUNTIME_IDS_WITH_PATCH_VERSIONING.has(data.browser.id)) {
data.version = data.fullVersion;
} else {
data.version = getMajorMinorVersion(data.fullVersion);
}

if (data.browser.id == "bun") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line and the following two lines were my quick hack to do something similar, but I like your solution much better as it doesn't special-case Bun!

data.version = data.fullVersion;
Expand Down Expand Up @@ -154,36 +160,46 @@ const parseUA = (userAgent: string, browsers: Browsers): ParsedUserAgent => {
// with this, find the pair of versions in |versions| that sandwiches
// |version|, and use the first of this pair. For example, given |version|
// "10.1" and |versions| entries "10.0" and "10.2", return "10.0".
for (let i = 0; i < versions.length - 1; i++) {
const current = versions[i];
const next = versions[i + 1];
if (
compareVersions(data.version, current, ">=") &&
compareVersions(data.version, next, "<")
) {
// However, for Bun, we need exact version matches because patch versions can add features.
if (RUNTIME_IDS_WITH_PATCH_VERSIONING.has(data.browser.id)) {
// For Bun, only mark as inBcd if exact version exists
if (versions.includes(data.version)) {
data.inBcd = true;
data.version = current;
break;
}
} else {
for (let i = 0; i < versions.length - 1; i++) {
const current = versions[i];
const next = versions[i + 1];
if (
compareVersions(data.version, current, ">=") &&
compareVersions(data.version, next, "<")
) {
data.inBcd = true;
data.version = current;
break;
}
}
}

// We reached the last entry in |versions|. With no |next| to compare against
// we have to check if it looks like a significant release or not. By default
// that means a new major version, but for Safari and Samsung Internet the
// major and minor version are significant.
let normalize = getMajorVersion;
if (
data.browser.id.startsWith("safari") ||
data.browser.id === "samsunginternet_android"
) {
normalize = getMajorMinorVersion;
}
if (
data.inBcd == false &&
normalize(data.version) === normalize(versions[versions.length - 1])
) {
data.inBcd = true;
data.version = versions[versions.length - 1];
if (!RUNTIME_IDS_WITH_PATCH_VERSIONING.has(data.browser.id)) {
let normalize = getMajorVersion;
if (
data.browser.id.startsWith("safari") ||
data.browser.id === "samsunginternet_android"
) {
normalize = getMajorMinorVersion;
}
if (
data.inBcd == false &&
normalize(data.version) === normalize(versions[versions.length - 1])
) {
data.inBcd = true;
data.version = versions[versions.length - 1];
}
}

return data;
Expand Down
2 changes: 1 addition & 1 deletion scripts/find-missing-reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const generateReportMap = (filter: string) => {
for (const browser of Object.keys(browsers)) {
if (
filter !== "all" &&
["ie", "nodejs", "deno", "oculus"].includes(browser)
["ie", "nodejs", "deno", "bun", "oculus"].includes(browser)
) {
continue;
}
Expand Down