Skip to content

Commit

Permalink
Add fetchCJS to webworker on desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Jul 6, 2023
1 parent 3744490 commit 948b000
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 60 deletions.
5 changes: 4 additions & 1 deletion packages/product-core/src/rpcWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ function wrapForRpc(func: RpcFunc) {

export async function initializeWorker(
corePlugins: PluginConstructor[],
opts: { fetchESM?: (url: string) => Promise<LoadedPlugin> },
opts: {
fetchESM?: (url: string) => Promise<LoadedPlugin>
fetchCJS?: (url: string) => Promise<LoadedPlugin>
},
) {
const { serializeError } = await import('serialize-error')
try {
Expand Down
54 changes: 4 additions & 50 deletions products/jbrowse-desktop/src/components/StartScreen/util.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import PluginManager from '@jbrowse/core/PluginManager'
import PluginLoader, { LoadedPlugin } from '@jbrowse/core/PluginLoader'
import PluginLoader from '@jbrowse/core/PluginLoader'
import { readConfObject } from '@jbrowse/core/configuration'
import deepmerge from 'deepmerge'
import sanitize from 'sanitize-filename'

import {
writeAWSAnalytics,
Expand All @@ -13,6 +12,7 @@ import {
import JBrowseRootModelFactory from '../../rootModel'
import corePlugins from '../../corePlugins'
import sessionModelFactory from '../../sessionModel'
import { fetchCJS } from '../../util'

const { ipcRenderer } = window.require('electron')

Expand Down Expand Up @@ -57,46 +57,7 @@ export async function createPluginManager(
) {
const pluginLoader = new PluginLoader(configSnapshot.plugins, {
fetchESM: url => import(/* webpackIgnore:true */ url),
fetchCJS: async url => {
const fs: typeof import('fs') = window.require('fs')
const path: typeof import('path') = window.require('path')
const os: typeof import('os') = window.require('os')
const http: typeof import('http') = window.require('http')
const fsPromises = fs.promises
// On macOS `os.tmpdir()` returns the path to a symlink, see:
// https://github.com/nodejs/node/issues/11422
const tmpDir = await fsPromises.mkdtemp(
path.join(await fsPromises.realpath(os.tmpdir()), 'jbrowse-plugin-'),
)
let plugin: LoadedPlugin | undefined = undefined
try {
const pluginLocation = path.join(tmpDir, sanitize(url))
const pluginLocationRelative = path.relative('.', pluginLocation)

await new Promise((resolve, reject) => {
const file = fs.createWriteStream(pluginLocation)
http
.get(url, res => {
res.pipe(file)
file.on('finish', resolve)
})
.on('error', err => {
fs.unlinkSync(pluginLocation)
reject(err)
})
})
plugin = window.require(pluginLocationRelative) as
| LoadedPlugin
| undefined
} finally {
await fsPromises.rmdir(tmpDir, { recursive: true })
}

if (!plugin) {
throw new Error(`Could not load CJS plugin: ${url}`)
}
return plugin
},
fetchCJS,
})
pluginLoader.installGlobalReExports(window)
const runtimePlugins = await pluginLoader.load(window.location.href)
Expand Down Expand Up @@ -140,14 +101,7 @@ export async function createPluginManager(
acct => acct.internetAccountId,
)

const rootModel = JBrowseRootModel.create(
{
jbrowse,
jobsManager: {},
},
{ pluginManager },
)

const rootModel = JBrowseRootModel.create({ jbrowse }, { pluginManager })
const config = rootModel.jbrowse.configuration
const { rpc } = config
rpc.defaultDriver.set('WebWorkerRpcDriver')
Expand Down
2 changes: 1 addition & 1 deletion products/jbrowse-desktop/src/rootModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function rootModelFactory({
/**
* #property
*/
jobsManager: types.maybe(JobsManager),
jobsManager: types.optional(JobsManager, {}),
})
.volatile(self => ({
version: packageJSON.version,
Expand Down
1 change: 0 additions & 1 deletion products/jbrowse-desktop/src/rpcWorker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-restricted-globals */
import './workerPolyfill'
import { initializeWorker } from '@jbrowse/product-core'
import { enableStaticRendering } from 'mobx-react'
Expand Down
2 changes: 1 addition & 1 deletion products/jbrowse-desktop/src/sessionModel/TrackMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function DesktopSessionTrackMenuMixin(pluginManager: PluginManager) {
trackSnapshot
const indexName = `${name}-index`
// TODO: open jobs list widget
jobsManager?.queueJob({
jobsManager.queueJob({
indexingParams: {
attributes: textSearching?.indexingAttributes || [
'Name',
Expand Down
35 changes: 35 additions & 0 deletions products/jbrowse-desktop/src/util.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { LoadedPlugin } from '@jbrowse/core/PluginLoader'
import sanitize from 'sanitize-filename'

export async function fetchCJS(url: string): Promise<LoadedPlugin> {
const fs: typeof import('fs') = window.require('fs')
const path: typeof import('path') = window.require('path')
const os: typeof import('os') = window.require('os')
const http: typeof import('http') = window.require('http')
const fsPromises = fs.promises
// On macOS `os.tmpdir()` returns the path to a symlink, see:
// https://github.com/nodejs/node/issues/11422
const tmpDir = await fsPromises.mkdtemp(
path.join(await fsPromises.realpath(os.tmpdir()), 'jbrowse-plugin-'),
)
try {
const pluginLocation = path.join(tmpDir, sanitize(url))
const pluginLocationRelative = path.relative('.', pluginLocation)

await new Promise((resolve, reject) => {
const file = fs.createWriteStream(pluginLocation)
http
.get(url, res => {
res.pipe(file)
file.on('finish', resolve)
})
.on('error', err => {
fs.unlinkSync(pluginLocation)
reject(err)
})
})
return window.require(pluginLocationRelative) as LoadedPlugin
} finally {
await fsPromises.rmdir(tmpDir, { recursive: true })
}
}
1 change: 0 additions & 1 deletion products/jbrowse-react-app/src/rpcWorker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-restricted-globals */
import './workerPolyfill'
import { initializeWorker } from '@jbrowse/product-core'
import { enableStaticRendering } from 'mobx-react'
Expand Down
1 change: 0 additions & 1 deletion products/jbrowse-react-linear-genome-view/src/rpcWorker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-restricted-globals */
import './workerPolyfill'
import { initializeWorker } from '@jbrowse/product-core'
import { enableStaticRendering } from 'mobx-react'
Expand Down
1 change: 0 additions & 1 deletion products/jbrowse-web/src/rpcWorker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-restricted-globals */
import './workerPolyfill'
import { initializeWorker } from '@jbrowse/product-core'
import { enableStaticRendering } from 'mobx-react'
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13470,9 +13470,9 @@ libnpmpublish@7.1.4:
ssri "^10.0.1"

librpc-web-mod@^1.0.0, librpc-web-mod@^1.1.5:
version "1.1.7"
resolved "https://registry.yarnpkg.com/librpc-web-mod/-/librpc-web-mod-1.1.7.tgz#df0a33dcbd04767bfb514563f763c38cc5cc11f6"
integrity sha512-pNhk5DMOv+fzHk4JBruzmv18qmz5WAoAbfcM2SDfrwl4GTs5KLvhC5SoGW5pHKjOppsKJyvlaly44k6Pe9U8Rw==
version "1.1.8"
resolved "https://registry.yarnpkg.com/librpc-web-mod/-/librpc-web-mod-1.1.8.tgz#d3e08c5ac1f1f59a9690601fec52083500644b9a"
integrity sha512-hbldn0Ax6Ambo6x+xYxLA3e1/umAvIOHv+ajChZNR3v4McGUciHcpEOxCq048sItmrh/RpB0/hOkc72cGGZCjw==
dependencies:
"@librpc/ee" "1.0.4"
serialize-error "^8.1.0"
Expand Down

0 comments on commit 948b000

Please sign in to comment.