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

wip: Add ts-check on ReactNativeLauncher #966

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
17 changes: 0 additions & 17 deletions src/libs/Launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,6 @@ export default class Launcher {
return result
}

/**
* Get content script logs. This function is called by the content script via the bridge
*
* @param {ContentScriptLogMessage} message - log message
*/
log(message) {
Minilog('ContentScript').info(message)
}

/**
* Calls cozy-konnector-libs' saveIdentifier function
*
Expand Down Expand Up @@ -543,14 +534,6 @@ export default class Launcher {
}
}

/**
* @typedef ContentScriptLogMessage
* @property {'debug'|'info'|'warning'|'error'|'critical'} level - Log level
* @property {any} message - message content
* @property {string | null} label - user defined label
* @property {string | null} namespace - user defined namespace
*/

/**
* @typedef LauncherStartContext
* @property {import('cozy-client').default} client - CozyClient instance
Expand Down
61 changes: 49 additions & 12 deletions src/libs/ReactNativeLauncher.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @ts-check
import Minilog from 'cozy-minilog'

import CookieManager from '@react-native-cookies/cookies'
import debounce from 'lodash/debounce'
// @ts-ignore no type declaration for microee
import MicroEE from 'microee'
import semverCompare from 'semver/functions/compare'

Expand All @@ -15,6 +17,7 @@
import { updateCozyAppBundle } from '/libs/cozyAppBundle/cozyAppBundle'
import { sendKonnectorsLogs } from '/libs/konnectors/sendKonnectorsLogs'

// @ts-ignore "cozy-clisk has no exported member wrapTimerFactory" but it does
import { wrapTimerFactory } from 'cozy-clisk'

import {
Expand All @@ -31,6 +34,7 @@
'ReactNativeLauncher.setWorkerState took more than 30000 ms'
}

// @ts-ignore
Minilog.enable()

function LauncherEvent() {}
Expand All @@ -39,6 +43,7 @@

const MIN_CLISK_SUPPORTED_VERSION = '0.10.0'

// @ts-ignore
export const launcherEvent = new LauncherEvent()

/**
Expand All @@ -58,7 +63,13 @@
this.controller = new AbortController()

const wrapTimer = wrapTimerFactory({
logFn: msg => this.log({ level: 'info', msg })
logFn: (/** @type {String} */ msg) =>
this.log({
namespace: 'ReactNativeLauncher',
label: 'Timer',
level: 'info',
msg
})
})

this.getExistingFilesIndex = wrapTimer(this, 'getExistingFilesIndex')
Expand All @@ -72,14 +83,15 @@
)
this.restartWorkerConnection = wrapTimer(this, 'restartWorkerConnection')
this.waitForWorkerEvent = wrapTimer(this, 'waitForWorkerEvent', {
suffixFn: args => args?.[0]
suffixFn: (/** @type {String[]} */ args) => args?.[0]
})
this.waitForWorkerVisible = wrapTimer(this, 'waitForWorkerVisible')
this.ensureAccountName = wrapTimer(this, 'ensureAccountName')
this.ensureAccountTriggerAndLaunch = wrapTimer(
this,
'ensureAccountTriggerAndLaunch'
)
// @ts-ignore
this.onWorkerWillReload = debounce(this.onWorkerWillReload.bind(this))
}

Expand Down Expand Up @@ -111,15 +123,18 @@
if (context.job) {
jobId = context.job.id
}
this.logger({
...logContent,
slug,
jobId,
level: newLevel,
timestamp: timestamp ?? new Date().toISOString()
})
if (this.logger) {
this.logger({
...logContent,
slug,
jobId,
level: newLevel,
timestamp: timestamp ?? new Date().toISOString()
})
}
}

// @ts-ignore
async init({ bridgeOptions, contentScript }) {
const promises = [
this.initPilotContentScriptBridge({
Expand Down Expand Up @@ -155,6 +170,7 @@

// we subscribe to this event only once both bridges are initialized or else we will
// receive this event also for the first worker page load
// @ts-ignore
this.on('NEW_WORKER_INITIALIZING', webviewRef =>
this.onWorkerWillReload(webviewRef)
)
Expand All @@ -166,7 +182,7 @@
* @param {object} options - options object
* @param {String} options.slug - konnector slug
* @param {import('cozy-client').default} options.client - CozyClient instance
* @returns {getKonnectorBundle}

Check failure on line 185 in src/libs/ReactNativeLauncher.js

View workflow job for this annotation

GitHub Actions / Quality Checks

Type '({ client, slug }: AppInfo) => Promise<KonnectorBundle>' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
*/
async ensureKonnectorIsInstalled({ slug, client }) {
try {
Expand All @@ -188,11 +204,14 @@

if (!bundle) throw new Error('No konnector bundle found')

return bundle

Check failure on line 207 in src/libs/ReactNativeLauncher.js

View workflow job for this annotation

GitHub Actions / Quality Checks

Type 'KonnectorBundle' is not assignable to type '({ client, slug }: AppInfo) => Promise<KonnectorBundle>'.
} catch (error) {
throw new Error(
`Critical error while ensuring "${slug}" konnector is installed.
The konnector will not be able to run: ${error.message}`
The konnector will not be able to run: ${
// @ts-ignore
error.message
}`
)
}
}
Expand All @@ -209,7 +228,12 @@
const { client, job } = this.getStartContext()

if (message) {
this.log({ level: 'error', message })
this.log({
namespace: 'ReactNativeLauncher',
label: 'Konnector Error Result',
level: 'error',
msg: message
})
}
await sendKonnectorsLogs(client)
if (job) {
Expand All @@ -222,30 +246,30 @@
} else {
await this.updateJobResult()
}
this.emit('STOPPED_JOB')

Check failure on line 249 in src/libs/ReactNativeLauncher.js

View workflow job for this annotation

GitHub Actions / Quality Checks

Property 'emit' does not exist on type 'ReactNativeLauncher'.
} else {
launcherEvent.emit('launchResult', { errorMessage: message })
}
this.close()
}

async start(...args) {

Check failure on line 256 in src/libs/ReactNativeLauncher.js

View workflow job for this annotation

GitHub Actions / Quality Checks

Rest parameter 'args' implicitly has an 'any[]' type.
return new Promise(resolve => {
this.controller.signal.addEventListener('abort', () => {
log('info', `Konnector launch was aborted`)

Check failure on line 259 in src/libs/ReactNativeLauncher.js

View workflow job for this annotation

GitHub Actions / Quality Checks

This expression is not callable.
resolve('abort')
})
this._start(...args)
.then(() => {
return resolve()

Check failure on line 264 in src/libs/ReactNativeLauncher.js

View workflow job for this annotation

GitHub Actions / Quality Checks

Expected 1 argument, but got 0. 'new Promise()' needs a JSDoc hint to produce a 'resolve' that can be called without arguments.
})
.catch(err => {
log('err', 'An error in launcher.start was not caught : ', err)

Check failure on line 267 in src/libs/ReactNativeLauncher.js

View workflow job for this annotation

GitHub Actions / Quality Checks

This expression is not callable.
})
})
}

async _start({ initKonnectorError } = {}) {

Check failure on line 272 in src/libs/ReactNativeLauncher.js

View workflow job for this annotation

GitHub Actions / Quality Checks

Initializer provides no value for this binding element and the binding element has no default value.

Check failure on line 272 in src/libs/ReactNativeLauncher.js

View workflow job for this annotation

GitHub Actions / Quality Checks

Binding element 'initKonnectorError' implicitly has an 'any' type.
activateKeepAwake('clisk')
const { account: prevAccount, konnector } = this.getStartContext()
try {
Expand All @@ -253,7 +277,7 @@
log.info('Got initKonnectorError ' + initKonnectorError.message)
throw initKonnectorError
}
await this.pilot.call('setContentScriptType', 'pilot')

Check failure on line 280 in src/libs/ReactNativeLauncher.js

View workflow job for this annotation

GitHub Actions / Quality Checks

Object is possibly 'undefined'.
await this.worker.call('setContentScriptType', 'worker')
const shouldLogout = await this.cleanCredentialsAccounts(konnector.slug)
if (shouldLogout) {
Expand Down Expand Up @@ -404,7 +428,11 @@
* @param {*} obj
*/
async sendToPilot(obj) {
await this.pilot.call('storeFromWorker', obj)
if (this.pilot) {
await this.pilot.call('storeFromWorker', obj)
} else {
throw new Error('ReactNativeLauncher.sendToPilot: no pilot available')
}
}

/**
Expand Down Expand Up @@ -665,3 +693,12 @@

MicroEE.mixin(ReactNativeLauncher)
export default ReactNativeLauncher

/**
* @typedef ContentScriptLogMessage
* @property {'debug'|'info'|'warning'|'error'|'critical'} level - Log level
* @property {string} msg - message content
* @property {string | null} label - user defined label
* @property {string | null} namespace - user defined namespace
* @property {String} [timestamp] - DateTime iso string when the log message was created
*/
Loading