From 2189fb2502e3b36d4eaa9c8025748c6c0f67c106 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Sun, 25 Aug 2024 14:15:10 +0000 Subject: [PATCH 1/5] Update npm package `webextension-polyfill` to v0.12.0 --- apps/plugin-browser/package.json | 4 ++-- yarn.lock | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/apps/plugin-browser/package.json b/apps/plugin-browser/package.json index 0acfade7f78..8083165366c 100755 --- a/apps/plugin-browser/package.json +++ b/apps/plugin-browser/package.json @@ -44,7 +44,7 @@ "react": "18.2.0", "react-dom": "18.2.0", "uuid": "9.0.1", - "webextension-polyfill": "0.10.0", + "webextension-polyfill": "0.12.0", "ws": "8.17.1" }, "devDependencies": { @@ -65,7 +65,7 @@ "@types/lodash.debounce": "4.0.9", "@types/react": "18.2.68", "@types/react-dom": "18.2.25", - "@types/webextension-polyfill": "0.10.4", + "@types/webextension-polyfill": "0.12.0", "@types/ws": "8.5.12", "babel-loader": "9.1.2", "babel-preset-react-app": "10.0.1", diff --git a/yarn.lock b/yarn.lock index 442a9d6f8ba..1a0880774a6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11220,10 +11220,10 @@ dependencies: "@types/node" "*" -"@types/webextension-polyfill@0.10.4": - version "0.10.4" - resolved "https://registry.yarnpkg.com/@types/webextension-polyfill/-/webextension-polyfill-0.10.4.tgz#09feeb2d8f04ac0a28818ade8aeeb4ab9fbafebb" - integrity sha512-pvEIqAZEbJRzaqTaWq3xlUoMWa3+euZHHz+VZHCzHWW+jOf8qLOq9wXy38U+WiPG3108SJC/wNc1X6vPC5TcjQ== +"@types/webextension-polyfill@0.12.0": + version "0.12.0" + resolved "https://registry.yarnpkg.com/@types/webextension-polyfill/-/webextension-polyfill-0.12.0.tgz#eac57362d021d08d8d30fe0b7bae447896346aa5" + integrity sha512-0d1V0jw5wswj11ijrPoUUP+kV2pHCNKDDgQSzNdD1PK5i3vQ0eHiA1xzT5mFwaqdwatrXdgkxgC4BzCQ6C9eUw== "@types/webidl-conversions@*": version "7.0.3" @@ -31398,10 +31398,10 @@ webcrypto-core@^1.7.4: pvtsutils "^1.3.2" tslib "^2.4.0" -webextension-polyfill@0.10.0: - version "0.10.0" - resolved "https://registry.yarnpkg.com/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz#ccb28101c910ba8cf955f7e6a263e662d744dbb8" - integrity sha512-c5s35LgVa5tFaHhrZDnr3FpQpjj1BB+RXhLTYUxGqBVN460HkbM8TBtEqdXWbpTKfzwCcjAZVF7zXCYSKtcp9g== +webextension-polyfill@0.12.0: + version "0.12.0" + resolved "https://registry.yarnpkg.com/webextension-polyfill/-/webextension-polyfill-0.12.0.tgz#f62c57d2cd42524e9fbdcee494c034cae34a3d69" + integrity sha512-97TBmpoWJEE+3nFBQ4VocyCdLKfw54rFaJ6EVQYLBCXqCIpLSZkwGgASpv4oPt9gdKCJ80RJlcmNzNn008Ag6Q== webidl-conversions@^3.0.0: version "3.0.1" From 6e10492ef62aeb82283a199281c5c2a627d93293 Mon Sep 17 00:00:00 2001 From: Ciaran Morinan Date: Mon, 26 Aug 2024 11:02:30 +0100 Subject: [PATCH 2/5] add /pkg/ folders to lint/docker ignores --- .dockerignore | 1 + .gitignore | 1 + .markdownlintignore | 1 + .prettierignore | 1 + 4 files changed, 4 insertions(+) diff --git a/.dockerignore b/.dockerignore index 327fbd7d728..d5b0e0e285b 100644 --- a/.dockerignore +++ b/.dockerignore @@ -99,6 +99,7 @@ blocks/**/.env **/dist **/build **/target +**/pkg # vscode config **/.vscode/settings.json diff --git a/.gitignore b/.gitignore index a4d01ae697c..1a545d2c523 100644 --- a/.gitignore +++ b/.gitignore @@ -83,6 +83,7 @@ blocks/**/.env **/dist **/build **/target +**/pkg # vscode config **/.vscode/settings.json diff --git a/.markdownlintignore b/.markdownlintignore index 4ef93d4f6e2..781d73fd4a5 100644 --- a/.markdownlintignore +++ b/.markdownlintignore @@ -92,6 +92,7 @@ blocks/**/.env **/dist **/build **/target +**/pkg # vscode config **/.vscode/settings.json diff --git a/.prettierignore b/.prettierignore index d8826714338..7e1d635c07f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -109,6 +109,7 @@ blocks/**/.env **/dist **/build **/target +**/pkg # vscode config **/.vscode/settings.json From 22130360ecfc87360d75e7eadd30e6c5cfc0beda Mon Sep 17 00:00:00 2001 From: Ciaran Morinan Date: Mon, 26 Aug 2024 11:02:40 +0100 Subject: [PATCH 3/5] remove unused types patch file --- .../@types+webextension-polyfill+0.10.4.patch | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 patches/@types+webextension-polyfill+0.10.4.patch diff --git a/patches/@types+webextension-polyfill+0.10.4.patch b/patches/@types+webextension-polyfill+0.10.4.patch deleted file mode 100644 index e4f393ee8c7..00000000000 --- a/patches/@types+webextension-polyfill+0.10.4.patch +++ /dev/null @@ -1,22 +0,0 @@ -diff --git a/node_modules/@types/webextension-polyfill/namespaces/runtime.d.ts b/node_modules/@types/webextension-polyfill/namespaces/runtime.d.ts -index eaeaf91..54b2b4e 100644 ---- a/node_modules/@types/webextension-polyfill/namespaces/runtime.d.ts -+++ b/node_modules/@types/webextension-polyfill/namespaces/runtime.d.ts -@@ -448,7 +448,7 @@ export namespace Runtime { - * will keep the message channel open to the other end until sendResponse is called). - */ - onMessage: Events.Event< -- (message: any, sender: MessageSender, sendResponse: () => void) => Promise | true | void -+ (message: any, sender: MessageSender, sendResponse: (response: any) => void) => Promise | true | void - >; - - /** -@@ -463,7 +463,7 @@ export namespace Runtime { - * will keep the message channel open to the other end until sendResponse is called). - */ - onMessageExternal: Events.Event< -- (message: any, sender: MessageSender, sendResponse: () => void) => Promise | true | void -+ (message: any, sender: MessageSender, sendResponse: (response: any) => void) => Promise | true | void - >; - - /** From 067cef98d32647c290281747ec940aea1ca36274 Mon Sep 17 00:00:00 2001 From: Ciaran Morinan Date: Mon, 26 Aug 2024 11:02:53 +0100 Subject: [PATCH 4/5] update browser plugin code for new webextension-polyfill types --- .../one-off/infer-entities-action.tsx | 8 ++++---- apps/plugin-browser/src/scripts/background.ts | 15 +++++++++++---- .../infer-entities/get-website-content.ts | 7 +++++-- apps/plugin-browser/src/scripts/content.ts | 16 +++++++++++++--- apps/plugin-browser/src/shared/messages.ts | 5 +++++ 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/apps/plugin-browser/src/pages/popup/popup-contents/action-center/one-off/infer-entities-action.tsx b/apps/plugin-browser/src/pages/popup/popup-contents/action-center/one-off/infer-entities-action.tsx index dfdeccdfff4..21665f029ea 100644 --- a/apps/plugin-browser/src/pages/popup/popup-contents/action-center/one-off/infer-entities-action.tsx +++ b/apps/plugin-browser/src/pages/popup/popup-contents/action-center/one-off/infer-entities-action.tsx @@ -62,10 +62,10 @@ export const InferEntitiesAction = ({ }; try { - const sourceWebPage = await (browser.tabs.sendMessage( - activeTab.id, - message, - ) as Promise); + const sourceWebPage = await browser.tabs.sendMessage< + GetTabContentRequest, + GetTabContentReturn + >(activeTab.id, message); void sendMessageToBackground({ createAs, diff --git a/apps/plugin-browser/src/scripts/background.ts b/apps/plugin-browser/src/scripts/background.ts index 6dd14195db0..fc7adda01ce 100644 --- a/apps/plugin-browser/src/scripts/background.ts +++ b/apps/plugin-browser/src/scripts/background.ts @@ -5,8 +5,8 @@ import { getUser } from "../shared/get-user"; import type { GetTabContentRequest, GetTabContentReturn, - Message, } from "../shared/messages"; +import { isWellFormattedMessage } from "../shared/messages"; import { clearLocalStorage, getFromLocalStorage, @@ -36,7 +36,11 @@ browser.runtime.onInstalled.addListener(({ reason }) => { } }); -browser.runtime.onMessage.addListener(async (message: Message, sender) => { +browser.runtime.onMessage.addListener(async (message, sender) => { + if (!isWellFormattedMessage(message)) { + return `Unrecognised message format ${String(message)}`; + } + if (sender.tab) { // We are not expecting any messages from the content script return; @@ -63,9 +67,12 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo) => { setTimeout(resolve, 2_000); }); - const webPage = await (browser.tabs.sendMessage(tabId, { + const webPage = await browser.tabs.sendMessage< + GetTabContentRequest, + GetTabContentReturn + >(tabId, { type: "get-tab-content", - } satisfies GetTabContentRequest) as Promise); + }); const applicableRules = automaticInferenceConfig.rules.filter( ({ restrictToDomains }) => { diff --git a/apps/plugin-browser/src/scripts/background/infer-entities/get-website-content.ts b/apps/plugin-browser/src/scripts/background/infer-entities/get-website-content.ts index 331e9bbf5f4..56b7aa8ca2b 100644 --- a/apps/plugin-browser/src/scripts/background/infer-entities/get-website-content.ts +++ b/apps/plugin-browser/src/scripts/background/infer-entities/get-website-content.ts @@ -45,9 +45,12 @@ export const getWebsiteContent = async (urls: string[]) => { browser.tabs.onUpdated.addListener(tabChangeListener); }); - const webPage = await (browser.tabs.sendMessage(tab.id, { + const webPage = await browser.tabs.sendMessage< + GetTabContentRequest, + GetTabContentReturn + >(tab.id, { type: "get-tab-content", - } satisfies GetTabContentRequest) as Promise); + }); webPages.push(webPage); diff --git a/apps/plugin-browser/src/scripts/content.ts b/apps/plugin-browser/src/scripts/content.ts index 19fe32bb204..50679f092b6 100644 --- a/apps/plugin-browser/src/scripts/content.ts +++ b/apps/plugin-browser/src/scripts/content.ts @@ -1,6 +1,12 @@ import browser from "webextension-polyfill"; +import { + type GetTabContentReturn, + isWellFormattedMessage, +} from "../shared/messages"; + /** + * @file * Content scripts operate in the context of the webpage itself, for reading and manipulating context. * * They have access to a limited set of browser APIs @@ -10,11 +16,15 @@ import browser from "webextension-polyfill"; * * You must update the extension if you amend this file, from the extensions manager page in the browser. */ -import type { GetTabContentReturn, Message } from "../shared/messages"; -// Promise based API (see: https://github.com/mozilla/webextension-polyfill/?tab=readme-ov-file#using-the-promise-based-apis) +// Promise based API (see: +// https://github.com/mozilla/webextension-polyfill/?tab=readme-ov-file#using-the-promise-based-apis) // eslint-disable-next-line @typescript-eslint/require-await -browser.runtime.onMessage.addListener(async (message: Message, _sender) => { +browser.runtime.onMessage.addListener(async (message) => { + if (!isWellFormattedMessage(message)) { + return `Unrecognised message format ${String(message)}`; + } + if (message.type === "get-tab-content") { const docContent = document.querySelector("main") ?? document.querySelector("body"); diff --git a/apps/plugin-browser/src/shared/messages.ts b/apps/plugin-browser/src/shared/messages.ts index acb8d5c1e24..ab3adeec31a 100644 --- a/apps/plugin-browser/src/shared/messages.ts +++ b/apps/plugin-browser/src/shared/messages.ts @@ -27,3 +27,8 @@ export type Message = | InferEntitiesRequest | CancelInferEntitiesRequest | GetTabContentRequest; + +export const isWellFormattedMessage = (message: unknown): message is Message => + typeof message === "object" && + message !== null && + typeof (message as { type: unknown }).type === "string"; From 9ca832316be22840f06719441346bf13013f0811 Mon Sep 17 00:00:00 2001 From: Ciaran Morinan Date: Mon, 26 Aug 2024 11:51:52 +0100 Subject: [PATCH 5/5] remove pkg folder from @local/status/typescript --- libs/@local/status/typescript/README.md | 4 ++-- libs/@local/status/typescript/package.json | 4 ++-- libs/@local/status/typescript/{pkg => }/src/main.ts | 4 ++-- libs/@local/status/typescript/{pkg => }/src/status-code.ts | 4 ++-- libs/@local/status/typescript/tsconfig.build.json | 2 +- libs/@local/status/typescript/tsconfig.json | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) rename libs/@local/status/typescript/{pkg => }/src/main.ts (87%) rename libs/@local/status/typescript/{pkg => }/src/status-code.ts (98%) diff --git a/libs/@local/status/typescript/README.md b/libs/@local/status/typescript/README.md index fab1a370869..eb52767b484 100644 --- a/libs/@local/status/typescript/README.md +++ b/libs/@local/status/typescript/README.md @@ -84,9 +84,9 @@ This process works through code-generation using the [`quicktype`](https://githu This package is structured into two main areas: -- [`pkg`](./pkg) contains the TypeScript package that exports the `Status`, `StatusCode` types, and helper functions. - [`rust`](./rust) contains the Rust crate that defines the `Status` and `StatusCode` types. -- [`type-defs`](./type-defs) contains the plain type definitions for `Status`, `StatusCode`, and associated status payloads. These types are defined in TypeScript at the moment but could easily be represented in another schema format. +- [`typescript`](./src) contains the TypeScript package that exports the `Status`, `StatusCode` types, and helper functions. +- [`typescript/type-defs`](./type-defs) contains the plain type definitions for `Status`, `StatusCode`, and associated status payloads. These types are defined in TypeScript at the moment but could easily be represented in another schema format. Note: despite the `type-defs` being in TypeScript, we define them separately to keep a better separation of concerns, and to avoid `quicktype` breaking when it encounters non-type code (e.g. `const` definitions). diff --git a/libs/@local/status/typescript/package.json b/libs/@local/status/typescript/package.json index 33b875c97ec..8874e485b21 100644 --- a/libs/@local/status/typescript/package.json +++ b/libs/@local/status/typescript/package.json @@ -5,10 +5,10 @@ "license": "(MIT OR Apache-2.0)", "type": "module", "exports": { - ".": "./dist/pkg/src/main.js", + ".": "./dist/src/main.js", "./type-defs/*": "./dist/type-defs/*.js" }, - "types": "./dist/pkg/src/main.d.ts", + "types": "./dist/src/main.d.ts", "scripts": { "build": "rimraf dist && tsc --build tsconfig.build.json", "fix:eslint": "eslint --fix .", diff --git a/libs/@local/status/typescript/pkg/src/main.ts b/libs/@local/status/typescript/src/main.ts similarity index 87% rename from libs/@local/status/typescript/pkg/src/main.ts rename to libs/@local/status/typescript/src/main.ts index 4cfbd8bd692..ffc0d056921 100644 --- a/libs/@local/status/typescript/pkg/src/main.ts +++ b/libs/@local/status/typescript/src/main.ts @@ -3,10 +3,10 @@ * and RPC APIs. */ -import type { Status } from "../../type-defs/status.js"; +import type { Status } from "../type-defs/status.js"; import { StatusCode } from "./status-code.js"; -export type { Status } from "../../type-defs/status.js"; +export type { Status } from "../type-defs/status.js"; export { convertHttpCodeToStatusCode, convertStatusCodeToHttpCode, diff --git a/libs/@local/status/typescript/pkg/src/status-code.ts b/libs/@local/status/typescript/src/status-code.ts similarity index 98% rename from libs/@local/status/typescript/pkg/src/status-code.ts rename to libs/@local/status/typescript/src/status-code.ts index 05a0ac284cc..459ce488326 100644 --- a/libs/@local/status/typescript/pkg/src/status-code.ts +++ b/libs/@local/status/typescript/src/status-code.ts @@ -1,6 +1,6 @@ -import { StatusCode } from "../../type-defs/status-code.js"; +import { StatusCode } from "../type-defs/status-code.js"; -export { StatusCode } from "../../type-defs/status-code.js"; +export { StatusCode } from "../type-defs/status-code.js"; const STATUS_CODE_TO_HTTP_CODE: Record = { OK: 200, diff --git a/libs/@local/status/typescript/tsconfig.build.json b/libs/@local/status/typescript/tsconfig.build.json index df24c1073a1..21689d29105 100644 --- a/libs/@local/status/typescript/tsconfig.build.json +++ b/libs/@local/status/typescript/tsconfig.build.json @@ -1,6 +1,6 @@ { "extends": "./tsconfig.json", - "include": ["pkg", "type-defs"], + "include": ["src", "type-defs"], "compilerOptions": { "declaration": true, "outDir": "dist", diff --git a/libs/@local/status/typescript/tsconfig.json b/libs/@local/status/typescript/tsconfig.json index 57af11e8ed8..ea95ce498a8 100644 --- a/libs/@local/status/typescript/tsconfig.json +++ b/libs/@local/status/typescript/tsconfig.json @@ -1,6 +1,6 @@ { "extends": "@local/tsconfig/legacy-base-tsconfig-to-refactor.json", - "include": ["./pkg/src/", "./scripts", "./type-defs"], + "include": ["./src/", "./scripts", "./type-defs"], "compilerOptions": { "module": "NodeNext", "moduleResolution": "NodeNext"