From 8846286b3b1ffb33d2c2735ceb775d6a9b1bfd51 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Wed, 6 Dec 2023 01:52:52 -0800 Subject: [PATCH 1/7] Don't use @mdn/bcd-utils-api middleware in testing --- .env.testing | 2 ++ server/index.ts | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/.env.testing b/.env.testing index bc293a0f019b..fb650e118031 100644 --- a/.env.testing +++ b/.env.testing @@ -34,3 +34,5 @@ BUILD_ALWAYS_ALLOW_ROBOTS=true REACT_APP_ENABLE_PLUS=true REACT_APP_FXA_SIGNIN_URL=/users/fxa/login/authenticate/ + +REACT_APP_BCD_BASE_URL = "" diff --git a/server/index.ts b/server/index.ts index 21539484f193..1dfda8fd8fe7 100644 --- a/server/index.ts +++ b/server/index.ts @@ -9,7 +9,6 @@ import send from "send"; import { createProxyMiddleware } from "http-proxy-middleware"; import cookieParser from "cookie-parser"; import openEditor from "open-editor"; -import { getBCDDataForPath } from "@mdn/bcd-utils-api"; import sanitizeFilename from "sanitize-filename"; import { @@ -54,6 +53,10 @@ import { findPostPathBySlug, } from "../build/blog.js"; +const { default: bcd } = await import("@mdn/browser-compat-data", { + assert: { type: "json" }, +}); + async function buildDocumentFromURL(url: string) { const document = Document.findByURL(url); if (!document) { @@ -78,8 +81,22 @@ const bcdRouter = express.Router({ caseSensitive: true }); // Note that this route will only get hit if .env has this: REACT_APP_BCD_BASE_URL="" bcdRouter.get("/api/v0/current/:path.json", async (req, res) => { - const data = getBCDDataForPath(req.params.path); - return data ? res.json(data) : res.status(404).send("BCD path not found"); + const bcdPath = req.params.path; + const bcdPathParts = bcdPath.split("."); + + let data = bcd; + + for (const part of bcdPathParts) { + if (part in data) { + data = data[part]; + } else { + res.status(404).send("BCD path not found"); + } + } + + return data + ? res.json({ browsers: bcd.browsers, data, query: bcdPath }) + : res.status(404).send("BCD path not found"); }); bcdRouter.use( From 3ea9c1847278d9ce1a2c240e34038265231984ac Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Thu, 14 Dec 2023 13:46:45 -0800 Subject: [PATCH 2/7] Get release date from browser data --- .../browser-compatibility-table/feature-row.tsx | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/client/src/document/ingredients/browser-compatibility-table/feature-row.tsx b/client/src/document/ingredients/browser-compatibility-table/feature-row.tsx index cde7ec7e0511..b182919a0fea 100644 --- a/client/src/document/ingredients/browser-compatibility-table/feature-row.tsx +++ b/client/src/document/ingredients/browser-compatibility-table/feature-row.tsx @@ -46,12 +46,17 @@ function getSupportClassName( } function getSupportBrowserReleaseDate( - support: SupportStatementExtended | undefined + currentSupport: BCD.SimpleSupportStatement | undefined, + browser: BCD.BrowserStatement ): string | undefined { - if (!support) { + if (!currentSupport) { return undefined; } - return getCurrentSupport(support)!.release_date; + const version = currentSupport.version_added; + if (typeof version !== "string" || version === "preview") { + return undefined; + } + return browser.releases[version].release_date; } function StatusIcons({ status }: { status: BCD.StatusBlock }) { @@ -136,7 +141,10 @@ const CellText = React.memo( const added = currentSupport?.version_added ?? null; const lastVersion = currentSupport?.version_last ?? null; - const browserReleaseDate = getSupportBrowserReleaseDate(support); + const browserReleaseDate = getSupportBrowserReleaseDate( + currentSupport, + browser + ); const supportClassName = getSupportClassName(support, browser); let status: From 81529750a8cdef22f7bd3bbf3160697369c0eb7b Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Thu, 14 Dec 2023 13:47:00 -0800 Subject: [PATCH 3/7] Remove [Simple]SupportStatementExtended types --- .../feature-row.tsx | 3 +-- .../browser-compatibility-table/utils.ts | 19 ++----------------- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/client/src/document/ingredients/browser-compatibility-table/feature-row.tsx b/client/src/document/ingredients/browser-compatibility-table/feature-row.tsx index b182919a0fea..532979928195 100644 --- a/client/src/document/ingredients/browser-compatibility-table/feature-row.tsx +++ b/client/src/document/ingredients/browser-compatibility-table/feature-row.tsx @@ -10,12 +10,11 @@ import { isNotSupportedAtAll, isTruthy, versionIsPreview, - SupportStatementExtended, } from "./utils"; import { LEGEND_LABELS } from "./legend"; function getSupportClassName( - support: SupportStatementExtended | undefined, + support: BCD.SupportStatement | undefined, browser: BCD.BrowserStatement ): "no" | "yes" | "partial" | "preview" | "removed-partial" | "unknown" { if (!support) { diff --git a/client/src/document/ingredients/browser-compatibility-table/utils.ts b/client/src/document/ingredients/browser-compatibility-table/utils.ts index 6b20a7060e19..2583e27d9257 100644 --- a/client/src/document/ingredients/browser-compatibility-table/utils.ts +++ b/client/src/document/ingredients/browser-compatibility-table/utils.ts @@ -1,20 +1,5 @@ import type BCD from "@mdn/browser-compat-data/types"; -// Extended for the fields, beyond the bcd types, that are extra-added -// exclusively in Yari. -interface SimpleSupportStatementExtended extends BCD.SimpleSupportStatement { - // Known for some support statements where the browser *version* is known, - // as opposed to just "true" and if the version release date is known. - release_date?: string; - // The version before the version_removed if the *version* removed is known, - // as opposed to just "true". Otherwise the version_removed. - version_last?: BCD.VersionValue; -} - -export type SupportStatementExtended = - | SimpleSupportStatementExtended - | SimpleSupportStatementExtended[]; - export function getFirst(a: T | T[]): T; export function getFirst(a: T | T[] | undefined): T | undefined { return Array.isArray(a) ? a[0] : a; @@ -153,8 +138,8 @@ function isFullySupportedWithoutMajorLimitation( // Prioritizes support items export function getCurrentSupport( - support: SupportStatementExtended | undefined -): SimpleSupportStatementExtended | undefined { + support: BCD.SupportStatement | undefined +): BCD.SimpleSupportStatement | undefined { if (!support) return undefined; // Full support without limitation From b698c6602b3b26863e1439c77d10cfcb27a528fd Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Thu, 14 Dec 2023 13:58:08 -0800 Subject: [PATCH 4/7] Use import statement instead of import method This removes the mitigation placed due to an issue in SWC 1.3.81+ and ts-node <10.9.1 --- server/index.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/server/index.ts b/server/index.ts index 1dfda8fd8fe7..56b1178e3653 100644 --- a/server/index.ts +++ b/server/index.ts @@ -11,6 +11,8 @@ import cookieParser from "cookie-parser"; import openEditor from "open-editor"; import sanitizeFilename from "sanitize-filename"; +import bcd from "@mdn/browser-compat-data" assert { type: "json" }; + import { buildDocument, buildLiveSamplePageFromURL, @@ -53,10 +55,6 @@ import { findPostPathBySlug, } from "../build/blog.js"; -const { default: bcd } = await import("@mdn/browser-compat-data", { - assert: { type: "json" }, -}); - async function buildDocumentFromURL(url: string) { const document = Document.findByURL(url); if (!document) { From e6542cf2ae775f5b8923d85cbbc2c73c4b694bfb Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Thu, 14 Dec 2023 14:33:30 -0800 Subject: [PATCH 5/7] Remove @mdn/bcd-utils-api from ai-help-macros --- scripts/ai-help-macros.ts | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/scripts/ai-help-macros.ts b/scripts/ai-help-macros.ts index 3098df6d669b..524e390ea22c 100644 --- a/scripts/ai-help-macros.ts +++ b/scripts/ai-help-macros.ts @@ -14,11 +14,8 @@ import { SUPABASE_SERVICE_ROLE_KEY, SUPABASE_URL, } from "../libs/env/index.js"; -import { - getBCDDataForPath, - SimpleSupportStatementExtended, -} from "@mdn/bcd-utils-api"; import path from "node:path"; +import bcd from "@mdn/browser-compat-data" assert { type: "json" }; import { BrowserStatement, SimpleSupportStatement, @@ -291,18 +288,26 @@ async function* builtDocs(directory: string) { } function buildBCDTable(query: string) { - const bcdData = getBCDDataForPath(query); - if (!bcdData) return ""; - const { browsers, data } = bcdData; + const bcdPathParts = query.split("."); + + let data: any = bcd; + for (const part of bcdPathParts) { + if (!(part in data)) { + return ""; + } + data = data[part]; + } + if (!data) return ""; + return data.__compat?.support ? ` -${Object.entries(data.__compat?.support) +${Object.entries(data.__compat.support) .map( ([browser, support]) => - `` ) @@ -314,10 +319,13 @@ ${Object.entries(data.__compat?.support) function buildBCDSupportString( browser: BrowserStatement, - support: (SimpleSupportStatement & SimpleSupportStatementExtended)[] + support: SimpleSupportStatement[] ) { return support .flatMap((item) => { + const releaseDate = + typeof item.version_added === "string" && + browser.releases[item.version_added]?.release_date; return [ item.version_removed && !support.some( @@ -345,7 +353,7 @@ function buildBCDSupportString( isFullySupportedWithoutLimitation(item) && !versionIsPreview(item.version_added, browser) ? `Full support since version ${item.version_added}${ - item.release_date ? ` (released ${item.release_date})` : "" + releaseDate ? ` (released ${releaseDate})` : "" }` : isNotSupportedAtAll(item) ? "No support" From 282cfc150a7bbaf0b05cc21ecb63437686ab0ba0 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Thu, 14 Dec 2023 14:33:53 -0800 Subject: [PATCH 6/7] Remove @mdn/bcd-utils-api altogether --- package.json | 1 - yarn.lock | 5 ----- 2 files changed, 6 deletions(-) diff --git a/package.json b/package.json index f2048f22b377..934f11be30ed 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,6 @@ "@codemirror/state": "^6.3.3", "@codemirror/theme-one-dark": "^6.1.2", "@fast-csv/parse": "^4.3.6", - "@mdn/bcd-utils-api": "^0.0.5", "@mdn/browser-compat-data": "^5.4.5", "@mozilla/glean": "2.0.5", "@sentry/integrations": "^7.87.0", diff --git a/yarn.lock b/yarn.lock index 00e38af9e9ce..224a18fbc3ff 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2214,11 +2214,6 @@ dependencies: call-bind "^1.0.2" -"@mdn/bcd-utils-api@^0.0.5": - version "0.0.5" - resolved "https://registry.yarnpkg.com/@mdn/bcd-utils-api/-/bcd-utils-api-0.0.5.tgz#9587c6b592e87f8a5ccab80ccdd8b5184f867922" - integrity sha512-+8zfCGZzBoAoLK6jYnOm9Xa2LcdNRDVrUXatuBNuGJHyt7tpR+k/+de2LSGHiU34TYrfAXk7g9x8/IpXR+4zhw== - "@mdn/browser-compat-data@^5.4.5": version "5.4.5" resolved "https://registry.yarnpkg.com/@mdn/browser-compat-data/-/browser-compat-data-5.4.5.tgz#9ac6b967ef372098a802dd8efa4766cf25c525d9" From 790962a0a81e84ab95b2d16cec0490c49eb40464 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Mon, 18 Dec 2023 09:12:03 -0800 Subject: [PATCH 7/7] Fix TypeScript error --- scripts/ai-help-macros.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ai-help-macros.ts b/scripts/ai-help-macros.ts index 524e390ea22c..d847d9612933 100644 --- a/scripts/ai-help-macros.ts +++ b/scripts/ai-help-macros.ts @@ -308,7 +308,7 @@ ${Object.entries(data.__compat.support) ([browser, support]) => `` ) .join("\n")}
BrowserSupport
${browsers[browser].name}${buildBCDSupportString( - browsers[browser], + `
${bcd.browsers[browser].name}${buildBCDSupportString( + bcd.browsers[browser], support )}
${bcd.browsers[browser].name}${buildBCDSupportString( bcd.browsers[browser], - support + support as SimpleSupportStatement[] )}