Skip to content

Commit

Permalink
Use @types/firefox-webext-browser for WebExtension types
Browse files Browse the repository at this point in the history
  • Loading branch information
lieser committed Apr 2, 2023
1 parent 7365a32 commit 3262e3a
Show file tree
Hide file tree
Showing 21 changed files with 66 additions and 118 deletions.
77 changes: 2 additions & 75 deletions WebExtensions.d.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,6 @@
/// <reference types="firefox-webext-browser" />

declare module browser {
interface Event<EventListener> {
readonly addListener(listener: EventListener),
readonly removeListener(listener: EventListener),
readonly hasListener(listener: EventListener),
}

declare module accounts {
interface MailAccount {
id: string,
identities: MailIdentity[],
name: string,
type: string,
}

interface MailIdentity {
email: string,
}

const list: () => Promise<MailAccount[]>;
const get: (accountId: string) => Promise<MailAccount?>;
}

declare module folder {
interface MailFolder {
accountId: string,
type?: string | undefined,
}
}

declare module messages {
interface MessagePart {
body?: string,
contentType?: string,
headers?: { [x: string]: string[] },
name?: string,
partNam?: string,
parts?: MessagePart[],
size?: number,
}

const getFull: (messageId: number) => MessagePart;
const getRaw: (messageId: number) => string;
}

declare module messageDisplay {
interface MessageHeader {
author: string,
bccList: string[],
ccList: string[],
date: Date,
flagged: boolean,
folder?: folder.MailFolder,
id: number,
junk: boolean,
junkScore: number,
read?: boolean,
recipients: string[],
subject: string,
tags: string[],
}

const getDisplayedMessage: (tabId: number) => Promise<MessageHeader>;
const onMessageDisplayed: Event<(tabId: messenger.tabs.Tab, message: MessageHeader) => void>,
const onMessagesDisplayed: Event<(tabId: messenger.tabs.Tab, message: MessageHeader[]) => void>,
}
}

declare module messenger {
declare module tabs {
interface Tab {
id: number,
windowID: number,
url?: string,
}
// https://github.com/thundernest/webext-docs/issues/56
var get: (accountId: string) => Promise<MailAccount?>;
}
}
24 changes: 18 additions & 6 deletions content/background.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

// @ts-check
///<reference path="../WebExtensions.d.ts" />
///<reference path="../RuntimeMessage.d.ts" />
///<reference path="../experiments/dkimHeader.d.ts" />
/* eslint-env webextensions */
Expand Down Expand Up @@ -69,7 +68,7 @@ const verifier = new AuthVerifier();
* Verify a message in a specific tab and display the result.
*
* @param {number} tabId
* @param {browser.messageDisplay.MessageHeader} message
* @param {browser.messages.MessageHeader} message
* @returns {Promise<void>}
*/
// eslint-disable-next-line complexity
Expand Down Expand Up @@ -151,6 +150,10 @@ async function verifyMessage(tabId, message) {
* Will start the verification if needed.
*/
browser.messageDisplay.onMessageDisplayed.addListener(async (tab, message) => {
if (!tab.id) {
console.warn(`onMessageDisplayed called for message ${message.id} without a tab id`);
return;
}
try {
await isInitialized;
if (tab.url?.startsWith("chrome://conversations/")) {
Expand Down Expand Up @@ -246,7 +249,7 @@ class DisplayAction {
*
* @private
* @param {number} tabId
* @param {browser.messageDisplay.MessageHeader} message
* @param {browser.messages.MessageHeader} message
* @returns {Promise<void>}
*/
static async _reverifyMessage(tabId, message) {
Expand All @@ -263,7 +266,9 @@ class DisplayAction {
*/
static async reverifyDKIMSignature(tabId) {
const message = await browser.messageDisplay.getDisplayedMessage(tabId);
await DisplayAction._reverifyMessage(tabId, message);
if (message) {
await DisplayAction._reverifyMessage(tabId, message);
}
}

/**
Expand All @@ -274,6 +279,9 @@ class DisplayAction {
*/
static async policyAddUserException(tabId) {
const message = await browser.messageDisplay.getDisplayedMessage(tabId);
if (!message) {
return;
}

const from = MsgParser.parseFromHeader(`From: ${message.author}\r\n`, prefs["internationalized.enable"]);
await SignRules.addException(from);
Expand All @@ -298,7 +306,9 @@ class DisplayAction {
await KeyDb.markAsSecure(sdid, selector);

const message = await browser.messageDisplay.getDisplayedMessage(tabId);
await DisplayAction._reverifyMessage(tabId, message);
if (message) {
await DisplayAction._reverifyMessage(tabId, message);
}
}

/**
Expand All @@ -314,7 +324,9 @@ class DisplayAction {
}

const message = await browser.messageDisplay.getDisplayedMessage(tabId);
await DisplayAction._reverifyMessage(tabId, message);
if (message) {
await DisplayAction._reverifyMessage(tabId, message);
}
}
}

Expand Down
1 change: 0 additions & 1 deletion content/displayAction.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

// @ts-check
///<reference path="../WebExtensions.d.ts" />
///<reference path="../RuntimeMessage.d.ts" />
/* eslint-env webextensions */

Expand Down
1 change: 0 additions & 1 deletion content/options.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

// @ts-check
///<reference path="../WebExtensions.d.ts" />
/* eslint-env webextensions */
/* eslint-disable no-magic-numbers */

Expand Down
13 changes: 6 additions & 7 deletions modules/authVerifier.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

// @ts-check
///<reference path="./authVerifier.d.ts" />
///<reference path="../WebExtensions.d.ts" />
///<reference path="../experiments/storageMessage.d.ts" />
/* eslint-env webextensions */
/* eslint-disable camelcase */
Expand Down Expand Up @@ -102,7 +101,7 @@ export default class AuthVerifier {
/**
* Verifies the authentication of the msg.
*
* @param {browser.messageDisplay.MessageHeader} message
* @param {browser.messages.MessageHeader} message
* @returns {Promise<AuthResult>}
*/
async verify(message) {
Expand Down Expand Up @@ -200,7 +199,7 @@ export default class AuthVerifier {
/**
* Resets the stored authentication result of the msg.
*
* @param {browser.messageDisplay.MessageHeader} message
* @param {browser.messages.MessageHeader} message
* @returns {Promise<void>}
*/
static resetResult(message) {
Expand All @@ -211,7 +210,7 @@ export default class AuthVerifier {
/**
* Get the Authentication-Results header as an SavedAuthResult.
*
* @param {browser.messageDisplay.MessageHeader} message
* @param {browser.messages.MessageHeader} message
* @param {Map<string, string[]>} headers
* @param {string} from
* @param {string?} listId
Expand Down Expand Up @@ -322,7 +321,7 @@ async function getARHResult(message, headers, from, listId, account, dmarc) {
/**
* Save authentication result.
*
* @param {browser.messageDisplay.MessageHeader} message
* @param {browser.messages.MessageHeader} message
* @param {SavedAuthResult|null} savedAuthResult
* @returns {Promise<void>}
*/
Expand All @@ -349,7 +348,7 @@ async function saveAuthResult(message, savedAuthResult) {
/**
* Get saved authentication result.
*
* @param {browser.messageDisplay.MessageHeader} message
* @param {browser.messages.MessageHeader} message
* @returns {Promise<SavedAuthResult|null>} savedAuthResult
*/
async function loadAuthResult(message) {
Expand Down Expand Up @@ -421,7 +420,7 @@ async function loadAuthResult(message) {
/**
* Checks the DKIM results against the sign rules.
*
* @param {browser.messageDisplay.MessageHeader} message
* @param {browser.messages.MessageHeader} message
* @param {VerifierModule.dkimSigResultV2[]} dkimResults
* @param {string} from
* @param {string?} listId
Expand Down
3 changes: 1 addition & 2 deletions modules/conversation.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

// @ts-check
///<reference path="../WebExtensions.d.ts" />
/* eslint-env webextensions */

import AuthVerifier from "../modules/authVerifier.mjs.js";
Expand Down Expand Up @@ -70,7 +69,7 @@ const verifier = new AuthVerifier();
/**
* Verify a message and display the result in Conversation.
*
* @param {browser.messageDisplay.MessageHeader} MessageHeader
* @param {browser.messages.MessageHeader} MessageHeader
* @returns {Promise<void>}
*/
export default async function verifyMessage(MessageHeader) {
Expand Down
1 change: 0 additions & 1 deletion modules/dkim/favicon.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

// @ts-check
///<reference path="../../WebExtensions.d.ts" />
/* eslint-env webextensions */

import ExtensionUtils from "../extensionUtils.mjs.js";
Expand Down
1 change: 0 additions & 1 deletion modules/dkim/keyDbProxy.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/

// @ts-check
///<reference path="../../WebExtensions.d.ts" />
///<reference path="../../RuntimeMessage.d.ts" />
/* eslint-env webextensions */

Expand Down
1 change: 0 additions & 1 deletion modules/dkim/keyStore.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/

// @ts-check
///<reference path="../../WebExtensions.d.ts" />
///<reference path="../../RuntimeMessage.d.ts" />
///<reference path="../dns.d.ts" />
/* eslint-env webextensions */
Expand Down
1 change: 0 additions & 1 deletion modules/dkim/signRules.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

// @ts-check
///<reference path="../../WebExtensions.d.ts" />
///<reference path="../../RuntimeMessage.d.ts" />
///<reference path="../../experiments/mailUtils.d.ts" />
/* eslint-env webextensions */
Expand Down
1 change: 0 additions & 1 deletion modules/dkim/signRulesProxy.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/

// @ts-check
///<reference path="../../WebExtensions.d.ts" />
///<reference path="../../RuntimeMessage.d.ts" />
/* eslint-env webextensions */

Expand Down
1 change: 0 additions & 1 deletion modules/dns.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/

// @ts-check
///<reference path="../WebExtensions.d.ts" />
///<reference path="../experiments/jsdns.d.ts" />
///<reference path="../experiments/libunbound.d.ts" />
/* eslint-env webextensions */
Expand Down
3 changes: 1 addition & 2 deletions modules/extensionUtils.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

// @ts-check
///<reference path="../WebExtensions.d.ts" />
/* eslint-env browser, webextensions */

import { dateToString, promiseWithTimeout, sleep } from "./utils.mjs.js";
Expand Down Expand Up @@ -68,7 +67,7 @@ function downloadDataAsJSON(data, dataName) {
/**
* Checks if a message is outgoing.
*
* @param {browser.messageDisplay.MessageHeader} message
* @param {browser.messages.MessageHeader} message
* @param {string} fromAddr
* @returns {Promise<boolean>}
*/
Expand Down
1 change: 0 additions & 1 deletion modules/migration.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

// @ts-check
///<reference path="../WebExtensions.d.ts" />
///<reference path="../experiments/migration.d.ts" />
/* eslint-env webextensions */

Expand Down
1 change: 0 additions & 1 deletion modules/preferences.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

// @ts-check
///<reference path="../WebExtensions.d.ts" />
/* eslint-env webextensions */
/* eslint no-magic-numbers: "off" */
/* eslint-disable jsdoc/match-description */
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
},
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/firefox-webext-browser": "^111.0.1",
"@types/mocha": "^10.0.1",
"@types/node": "^18.15.11",
"@types/sinon": "^10.0.13",
"@types/thunderbird-webext-browser": "^109.0.0",
"chai": "^4.3.7",
"chalk": "^5.2.0",
"eslint": "^8.37.0",
Expand Down
18 changes: 18 additions & 0 deletions test/helpers/initWebExtensions.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ before(async function () {
globalThis.browser.accounts = {
list: sinon.fake.resolves([]),
get: sinon.fake.resolves(null),
getDefault: sinon.fake.throws("no fake for browser.accounts.getDefault"),
setDefaultIdentity: sinon.fake.throws("no fake for browser.accounts.setDefaultIdentity"),
getDefaultIdentity: sinon.fake.throws("no fake for browser.accounts.getDefaultIdentity"),
onCreated: {
addListener: sinon.fake.throws("no fake for browser.accounts.onCreated.addListener"),
removeListener: sinon.fake.throws("no fake for browser.accounts.onCreated.removeListener"),
hasListener: sinon.fake.throws("no fake for browser.accounts.onCreated.hasListener"),
},
onDeleted: {
addListener: sinon.fake.throws("no fake for browser.accounts.onDeleted.addListener"),
removeListener: sinon.fake.throws("no fake for browser.accounts.onDeleted.removeListener"),
hasListener: sinon.fake.throws("no fake for browser.accounts.onDeleted.hasListener"),
},
onUpdated: {
addListener: sinon.fake.throws("no fake for browser.accounts.onUpdated.addListener"),
removeListener: sinon.fake.throws("no fake for browser.accounts.onUpdated.removeListener"),
hasListener: sinon.fake.throws("no fake for browser.accounts.onUpdated.hasListener"),
},
};

globalThis.browser.mailUtils = {
Expand Down
Loading

0 comments on commit 3262e3a

Please sign in to comment.