-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fetchCJS to webworker on desktop
- Loading branch information
Showing
10 changed files
with
48 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters