Skip to content
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

Update npm package webextension-polyfill to v0.12.0 #4911

Merged
merged 10 commits into from
Dec 20, 2024
Merged
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 .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ blocks/**/.env
**/dist
**/build
**/target
**/pkg

# vscode config
**/.vscode/settings.json
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ blocks/**/.env
**/dist
**/build
**/target
**/pkg

# vscode config
**/.vscode/settings.json
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions apps/plugin-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"react": "19.0.0",
"react-dom": "19.0.0",
"uuid": "11.0.3",
"webextension-polyfill": "0.10.0",
"webextension-polyfill": "0.12.0",
"ws": "8.18.0"
},
"devDependencies": {
Expand All @@ -65,7 +65,7 @@
"@types/lodash.debounce": "4.0.9",
"@types/react": "19.0.1",
"@types/react-dom": "19.0.2",
"@types/webextension-polyfill": "patch:@types/webextension-polyfill@npm%3A0.10.4#~/.yarn/patches/@types-webextension-polyfill-npm-0.10.4-291152c615.patch",
"@types/webextension-polyfill": "0.12.0",
"@types/ws": "8.5.13",
"babel-loader": "9.2.1",
"babel-preset-react-app": "10.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ export const InferEntitiesAction = ({
};

try {
const sourceWebPage = await (browser.tabs.sendMessage(
activeTab.id,
message,
) as Promise<GetTabContentReturn>);
const sourceWebPage = await browser.tabs.sendMessage<
GetTabContentRequest,
GetTabContentReturn
>(activeTab.id, message);

void sendMessageToBackground({
createAs,
Expand Down
15 changes: 11 additions & 4 deletions apps/plugin-browser/src/scripts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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<GetTabContentReturn>);
});

const applicableRules = automaticInferenceConfig.rules.filter(
({ restrictToDomains }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetTabContentReturn>);
});

webPages.push(webPage);

Expand Down
16 changes: 13 additions & 3 deletions apps/plugin-browser/src/scripts/content.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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");
Expand Down
5 changes: 5 additions & 0 deletions apps/plugin-browser/src/shared/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
4 changes: 2 additions & 2 deletions libs/@local/status/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
4 changes: 2 additions & 2 deletions libs/@local/status/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<StatusCode, number> = {
OK: 200,
Expand Down
2 changes: 1 addition & 1 deletion libs/@local/status/typescript/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "./tsconfig.json",
"include": ["pkg", "type-defs"],
"include": ["src", "type-defs"],
"compilerOptions": {
"declaration": true,
"outDir": "dist",
Expand Down
2 changes: 1 addition & 1 deletion libs/@local/status/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "@local/tsconfig/legacy-base-tsconfig-to-refactor.json",
"include": ["./pkg/src/", "./scripts", "./type-defs", "eslint.config.js"],
"include": ["./src/", "./scripts", "./type-defs", "eslint.config.js"],
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext"
Expand Down
3 changes: 2 additions & 1 deletion libs/@local/tsconfig/legacy-base-tsconfig-to-refactor.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"@local/hash-graph-sdk/*": ["../graph/sdk/typescript/src/*.ts"],
"@local/hash-graph-types/*": ["../graph/types/typescript/src/*.ts"],
"@local/hash-isomorphic-utils/*": ["../hash-isomorphic-utils/src/*.ts"],
"@local/status": ["../status/typescript/pkg/src/main.ts"],
"@local/status": ["../status/typescript/src/main.ts"],
"@local/status/type-defs/*": ["../status/typescript/src/type-defs/*.ts"],
"@local/hash-subgraph": ["../hash-subgraph/src/main.ts"],
"@local/hash-subgraph/*": ["../hash-subgraph/src/*.ts"],
"@local/harpc-client": ["../harpc/client/typescript/src/index.ts"],
Expand Down
27 changes: 10 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ __metadata:
"@types/lodash.debounce": "npm:4.0.9"
"@types/react": "npm:19.0.1"
"@types/react-dom": "npm:19.0.2"
"@types/webextension-polyfill": "patch:@types/webextension-polyfill@npm%3A0.10.4#~/.yarn/patches/@types-webextension-polyfill-npm-0.10.4-291152c615.patch"
"@types/webextension-polyfill": "npm:0.12.0"
"@types/ws": "npm:8.5.13"
babel-loader: "npm:9.2.1"
babel-preset-react-app: "npm:10.0.1"
Expand Down Expand Up @@ -847,7 +847,7 @@ __metadata:
type-fest: "npm:3.13.1"
typescript: "npm:5.7.2"
uuid: "npm:11.0.3"
webextension-polyfill: "npm:0.10.0"
webextension-polyfill: "npm:0.12.0"
webpack: "npm:5.97.1"
webpack-cli: "npm:4.10.0"
webpack-dev-server: "npm:4.15.2"
Expand Down Expand Up @@ -18613,17 +18613,10 @@ __metadata:
languageName: node
linkType: hard

"@types/webextension-polyfill@npm:0.10.4":
version: 0.10.4
resolution: "@types/webextension-polyfill@npm:0.10.4"
checksum: 10c0/6ae62b0d0b61878c0c7f65ac484b436d8a07e3561ade441a12e363bdc7db7b2675db9f94e3d1e7efd5ad2c0a5ee0818ea5942f06246cc18384b14ec698371253
languageName: node
linkType: hard

"@types/webextension-polyfill@patch:@types/webextension-polyfill@npm%3A0.10.4#~/.yarn/patches/@types-webextension-polyfill-npm-0.10.4-291152c615.patch":
version: 0.10.4
resolution: "@types/webextension-polyfill@patch:@types/webextension-polyfill@npm%3A0.10.4#~/.yarn/patches/@types-webextension-polyfill-npm-0.10.4-291152c615.patch::version=0.10.4&hash=ff32d6"
checksum: 10c0/86fb2c10a438117d9310f3924a5d64f1ce0100a7f539bd6208864af4b7f5a795d8bf27fd25587cb939ea35689ba9daacd15bd07aed4241ec711a7db5812065ad
"@types/webextension-polyfill@npm:0.12.0":
version: 0.12.0
resolution: "@types/webextension-polyfill@npm:0.12.0"
checksum: 10c0/d8b69620e9af41802a144ddbefcaf1f627856ed1b1ccf42af0b4d546906a738fea01f45efcf4eb9c2e08761e1b3333105026eb7fd4b51cafb5b4e591079faa8c
languageName: node
linkType: hard

Expand Down Expand Up @@ -47734,10 +47727,10 @@ __metadata:
languageName: node
linkType: hard

"webextension-polyfill@npm:0.10.0":
version: 0.10.0
resolution: "webextension-polyfill@npm:0.10.0"
checksum: 10c0/6a45278f1fed8fbd5355f9b19a7b0b3fadc91fa3a6eef69125a1706bb3efa2181235eefbfb3f538443bb396cfcb97512361551888ce8465c08914431cb2d5b6d
"webextension-polyfill@npm:0.12.0":
version: 0.12.0
resolution: "webextension-polyfill@npm:0.12.0"
checksum: 10c0/5ace2aaaf6a203515bdd2fb948622f186a5fbb50099b539ce9c0ad54896f9cc1fcc3c0e2a71d1f7071dd7236d7daebba1e0cbcf43bfdfe54361addf0333ee7d1
languageName: node
linkType: hard

Expand Down
Loading