Skip to content

Commit

Permalink
Prepare for next release
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed May 15, 2024
1 parent ab02352 commit eeaf1c3
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async function createFirefoxAddonsTab(initialTab, url) {
}
}

// eslint-disable-next-line @typescript-eslint/naming-convention
const __IS_FIRST_RUN__ = false

async function handleTabOnExtensionLoad() {
Expand All @@ -37,16 +38,16 @@ async function handleTabOnExtensionLoad() {
// This check is generated at runtime by a hypothetical function,
// handle accordingly if it's the first run
if (__IS_FIRST_RUN__) {
setTimeout(async () => {
await browser.tabs.create({url: './pages/welcome.html'})
setTimeout(() => {
browser.tabs.create({url: './pages/welcome.html'})
}, 1000)
}
} else {
createFirefoxAddonsTab(initialTab, 'about:blank')
await createFirefoxAddonsTab(initialTab, 'about:blank')
}
} catch (error) {
console.error('Error handling tabs on extension load:', error)
}
}

handleTabOnExtensionLoad()
handleTabOnExtensionLoad().catch(console.error)
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ async function onStartup() {
})
}

onStartup()
onStartup().catch(console.error)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const TEN_SECONDS_MS = 10 * 1000
let webSocket = null

browser.runtime.onInstalled.addListener(async () => {
Expand Down Expand Up @@ -27,6 +26,7 @@ async function connect() {
}

webSocket.onmessage = async (event) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const message = JSON.parse(event.data)

if (message.status === 'serverReady') {
Expand Down Expand Up @@ -63,8 +63,7 @@ async function getDevExtensions() {
// Do not include itself
extension.id !== browser.runtime.id &&
// Manager extension
extension.browser_specific_settings?.gecko?.id !==
'manager@extension-js' &&
extension.name !== 'Add-On Manager' &&
// Show only unpackaged extensions
extension.installType === 'development'
)
Expand All @@ -78,6 +77,7 @@ async function messageAllExtensions(changedFile) {
if (isExtensionReady) {
const devExtensions = await getDevExtensions()
for (const extension of devExtensions) {
console.log({extension})
try {
await browser.runtime.sendMessage(extension.id, {changedFile})
console.info('[Reload Service] Add-On reloaded and ready.')
Expand All @@ -98,9 +98,11 @@ async function requestInitialLoadData() {
const responses = await Promise.all(
devExtensions.map(async (extension) => {
try {
return await browser.runtime.sendMessage(extension.id, {
const result = await browser.runtime.sendMessage(extension.id, {
initialLoadData: true
})

return result
} catch (error) {
console.error(
`Error sending message to ${extension.id}: ${error.message}`
Expand Down Expand Up @@ -129,6 +131,12 @@ async function checkExtensionReadiness() {
return true
}

function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
async function delay(ms) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return await new Promise((resolve) => setTimeout(resolve, ms)).catch(
(error) => {
console.error(`Error delaying: ${error.message}`)
}
)
}
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/run-firefox-addon/helpers/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import prefersYarn from 'prefers-yarn'
import getDirectorySize from '../steps/calculateDirSize'
import {type ManifestBase} from '../manifest-types'
import browser from 'webextension-polyfill-ts'
import type browser from 'webextension-polyfill-ts'

interface Data {
id: string
Expand Down
6 changes: 5 additions & 1 deletion packages/run-firefox-addon/minimum-background-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {browser} from 'webextension-polyfill-ts'
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async (request: any, _sender: any, sendResponse: any) => {
const managementInfo = await new Promise(() => {
browser.management.getSelf()
browser.management.getSelf().catch((error: any) => {
console.error(error)
})
}).catch((error: any) => {
console.error(error)
})

// Ping-pong between the user extension background page(this)
Expand Down
2 changes: 1 addition & 1 deletion packages/run-firefox-addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"node": ">=18"
},
"name": "webpack-run-firefox-addon",
"version": "1.1.7",
"version": "1.0.0",
"description": "Run your add-on on Firefox with auto-reload support",
"main": "./dist/module.js",
"types": "./dist/module.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {type StatsPreset} from '../../../types'
import {type ManifestBase} from '../../../manifest-types'
import httpsServer from './httpsServer'
import isFirstRun from '../../RunFirefoxPlugin/firefox/isFirstRun'
import browser from 'webextension-polyfill-ts'
import type browser from 'webextension-polyfill-ts'

interface Data {
id: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ function configureProfile(
const preferenceKeys = Object.keys(preferences)

preferenceKeys.forEach((preference) => {
profile.setPreference(preference, preferences[preference])
profile.setPreference(preference, preferences[preference] as string)
})

const customPreferenceKeys = Object.keys(customPreferences)

if (customPreferenceKeys.length > 0) {
customPreferenceKeys.forEach((custom) => {
profile.setPreference(custom, customPreferences[custom])
profile.setPreference(custom, customPreferences[custom] as string)
})
}

Expand Down
23 changes: 19 additions & 4 deletions packages/run-firefox-addon/steps/RunFirefoxPlugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export default class FirefoxExtensionLauncherPlugin {
private async launchFirefox() {
const firefoxLaunchPath = `fx-runner start --binary "${firefox}" --foreground --no-remote`

if (!fs.existsSync(firefox as string) || '') {
if (!fs.existsSync(firefox!) || '') {
console.error(
`${bgWhite(red(` firefox-browser `))} ${red(`✖︎✖︎✖︎`)} ` +
`Firefox browser ${firefox == 'null' ? 'is not installed.' : `is not found at ${firefox}`}. ` +
`Firefox browser ${firefox !== 'null' ? `is not found at ${firefox}` : 'is not installed.'}. ` +
// `Either install Firefox or set the FIREFOX environment variable to the path of the Firefox executable.`
`Either install Firefox or choose a different browser via ${blue('--browser')}.`
)
Expand All @@ -40,7 +40,14 @@ export default class FirefoxExtensionLauncherPlugin {
const cmd = `${firefoxLaunchPath} ${firefoxConfig}`

// Inject the add-ons code into Firefox profile.
new RemoteFirefox(this.options).installAddons()
new RemoteFirefox(this.options).installAddons().catch((error) => {
console.error(
`${bgWhite(red(bold(` firefox-browser `)))} ${red(`✖︎✖︎✖︎`)} ` +
`Error injecting add-ons code into Firefox profile.`
)
console.error(error)
process.exit()
})

const child = exec(cmd, (error, _stdout, stderr) => {
if (error != null) throw error
Expand Down Expand Up @@ -80,7 +87,15 @@ export default class FirefoxExtensionLauncherPlugin {
done()
return
}
this.launchFirefox()
this.launchFirefox().catch((error) => {
console.error(
`${bgWhite(red(bold(` firefox-browser `)))} ${red(`✖︎✖︎✖︎`)} ` +
`Error launching Firefox.`
)
console.error(error)
process.exit()
})

firefoxDidLaunch = true
done()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface Deferred {

function parseMessage(data: Buffer): {
remainingData: Buffer
parsedMessage?: any
parsedMessage?: Message
error?: Error
fatal?: boolean
} {
Expand Down Expand Up @@ -62,15 +62,17 @@ function parseMessage(data: Buffer): {

export default class MessagingClient extends EventEmitter {
private incomingData: Buffer = Buffer.alloc(0)
private pendingRequests: {request: any; deferred: Deferred}[] = []
private activeRequests: Map<string, Deferred> = new Map()
private pendingRequests: Array<{request: any; deferred: Deferred}> = []
private readonly activeRequests = new Map<string, Deferred>()
private connection?: net.Socket

connect(port: number): Promise<void> {
return new Promise((resolve, reject) => {
async connect(port: number): Promise<void> {
await new Promise<void>((resolve, reject) => {
try {
const connectionOptions = {port, host: '127.0.0.1'}
const conn = net.createConnection(connectionOptions, () => resolve())
const conn = net.createConnection(connectionOptions, () => {
resolve()
})
this.connection = conn

conn.on('data', this.onData.bind(this))
Expand All @@ -96,13 +98,17 @@ export default class MessagingClient extends EventEmitter {
}

private rejectAllRequests(error: Error): void {
this.activeRequests.forEach((deferred) => deferred.reject(error))
this.activeRequests.forEach((deferred) => {
deferred.reject(error)
})
this.activeRequests.clear()
this.pendingRequests.forEach(({deferred}) => deferred.reject(error))
this.pendingRequests.forEach(({deferred}) => {
deferred.reject(error)
})
this.pendingRequests = []
}

request(requestProps: any): Promise<any> {
async request(requestProps: any): Promise<any> {
const request =
typeof requestProps === 'string'
? {to: 'root', type: requestProps}
Expand All @@ -114,7 +120,7 @@ export default class MessagingClient extends EventEmitter {
`Unexpected MessagingClient request without target actor: ${request.type}`
)
}
return new Promise((resolve, reject) => {
return await new Promise((resolve, reject) => {
const deferred = {resolve, reject}
this.pendingRequests.push({request, deferred})
this.flushPendingRequests()
Expand All @@ -123,7 +129,7 @@ export default class MessagingClient extends EventEmitter {

private flushPendingRequests(): void {
this.pendingRequests = this.pendingRequests.filter(
({request, deferred}) => {
({request, deferred}: {request: {to: string}; deferred: Deferred}) => {
if (this.activeRequests.has(request.to)) return true
if (!this.connection) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import {bgWhite, red, bold} from '@colors/colors/safe'
import MessagingClient from './MessagingClient'
import {RunFirefoxExtensionInterface} from '../../../types'
import {type RunFirefoxExtensionInterface} from '../../../types'
import {isErrorWithCode, requestErrorToMessage} from './messageUtils'

const MAX_RETRIES = 150
Expand All @@ -19,7 +19,7 @@ const reloadExtension = path.resolve(
)

export default class RemoteFirefox {
private options: RunFirefoxExtensionInterface
private readonly options: RunFirefoxExtensionInterface

constructor(configOptions: RunFirefoxExtensionInterface) {
this.options = configOptions
Expand All @@ -28,7 +28,8 @@ export default class RemoteFirefox {
private async connectClient(port: number) {
let lastError

for (const _retries of Array.from({length: MAX_RETRIES})) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const _ of Array.from({length: MAX_RETRIES})) {
try {
const client = new MessagingClient()
await client.connect(port)
Expand Down Expand Up @@ -73,7 +74,7 @@ export default class RemoteFirefox {
await client.request({
to: addons.addonsActor,
type: 'installTemporaryAddon',
addonPath: addonPath,
addonPath,
openDevTools: isDevtoolsEnabled
})
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function requestErrorToMessage(err: any) {
}

export function isErrorWithCode(codeWanted: any, error: any) {
if (Array.isArray(codeWanted) && codeWanted.indexOf(error.code) !== -1) {
if (Array.isArray(codeWanted) && codeWanted.includes(error.code)) {
return true
} else if (error.code === codeWanted) {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import patchBackground from './patchBackground'
import patchExternallyConnectable from './patchExternallyConnectable'
import * as utils from '../../../helpers/utils'
import {type RunFirefoxExtensionInterface} from '../../../types'
import {ManifestBase} from '../../../manifest-types'
import {type ManifestBase} from '../../../manifest-types'

class ApplyManifestDevDefaultsPlugin {
private readonly manifestPath?: string
Expand Down
2 changes: 1 addition & 1 deletion programs/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"node": ">=18"
},
"name": "extension",
"version": "1.4.6",
"version": "1.5.0",
"description": "Create cross-browser extensions with no build configuration.",
"main": "./dist/cli.js",
"types": "./dist/cli.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion programs/develop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"node": ">=18"
},
"name": "@extension-create/develop",
"version": "1.4.6",
"version": "1.5.0",
"description": "The develop step of Extension",
"main": "./dist/module.js",
"types": "./dist/module.d.ts",
Expand Down

0 comments on commit eeaf1c3

Please sign in to comment.