-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Load native module dynamically. So the in-compatible electr…
…on won't break directly
- Loading branch information
Showing
9 changed files
with
194 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { NativeModuleLoader } from '~/util/NativeModule' | ||
import { dependencies } from '../package.json' | ||
|
||
type NodeDataChannel = typeof import('node-datachannel') | ||
|
||
const version = dependencies['node-datachannel'] | ||
const os = process.platform | ||
const arch = process.arch | ||
const url = `https://github.com/murat-dogan/node-datachannel/releases/download/v${version}/node-datachannel-v${version}-napi-v8-${os}-${arch}.tar.gz` | ||
|
||
export const NodeDataChannelModule = new NativeModuleLoader('node_datachannel.node', () => [url, url], (_, binding) => { | ||
if (!binding) { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
return require('node-datachannel') as NodeDataChannel | ||
} | ||
return binding as NodeDataChannel | ||
}) |
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
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,14 @@ | ||
import SQLite from 'better-sqlite3' | ||
import { join } from 'path' | ||
import { NativeModuleLoader } from '~/util/NativeModule' | ||
import { dependencies } from '../package.json' | ||
|
||
const version = dependencies['better-sqlite3'] | ||
const os = process.platform | ||
const arch = process.arch | ||
const modules = process.versions.modules | ||
const url = `https://github.com/WiseLibs/better-sqlite3/releases/download/v${version}/better-sqlite3-v${version}-electron-v${modules}-${os}-${arch}.tar.gz` | ||
|
||
export const SQLiteModule = new NativeModuleLoader('better_sqlite3.node', () => [url, url], (root, nativeBinding) => new SQLite(join(root, 'resources.sqlite'), { | ||
nativeBinding, | ||
})) |
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,103 @@ | ||
import { createPromiseSignal } from '@xmcl/runtime-api' | ||
import { writeFile } from 'fs-extra' | ||
import { unlink } from 'fs/promises' | ||
import { join } from 'path' | ||
import { PassThrough } from 'stream' | ||
import { extract } from 'tar-stream' | ||
import { stream } from 'undici' | ||
import { createGunzip } from 'zlib' | ||
import { AnyError } from '~/util/error' | ||
|
||
export class NativeModuleLoader<T> { | ||
#retryCount = 0 | ||
#signal = createPromiseSignal<T>() | ||
#initPromise: Promise<void> | undefined | ||
|
||
constructor( | ||
readonly nodeFileName: string, | ||
readonly getUrl: () => [string, string], | ||
readonly loader: (root: string, mod: any) => T, | ||
) { } | ||
|
||
#tryResolve = async (root: string): Promise<void> => { | ||
try { | ||
const nativeModule = getDependencyIfExists(root, this.nodeFileName) | ||
const result = this.loader(root, nativeModule) | ||
this.#signal.resolve(result) | ||
} catch { | ||
if (this.#retryCount > 3) { | ||
this.#signal.reject(new AnyError('NativeInitError', 'Failed to load ' + this.nodeFileName)) | ||
return | ||
} | ||
await downloadNative(root, ...this.getUrl(), this.nodeFileName) | ||
this.#retryCount++ | ||
return this.#tryResolve(root) | ||
} | ||
} | ||
|
||
init(root: string) { | ||
if (this.#initPromise) return this.#initPromise | ||
this.#initPromise = this.#tryResolve(root) | ||
} | ||
|
||
get retryCount() { | ||
return this.#retryCount | ||
} | ||
|
||
getInstance = () => { | ||
return this.#signal.promise | ||
} | ||
} | ||
|
||
function getDependencyIfExists(dir: string, fileName: string) { | ||
const dest = join(dir, fileName) | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const mod = require(dest) | ||
return mod | ||
} catch { | ||
unlink(dest).catch(() => { }) | ||
return undefined | ||
} | ||
} | ||
|
||
async function downloadNative(dir: string, primary: string, fallback: string, fileName: string) { | ||
// Download the tarball and extract it to the specified directory | ||
const dest = join(dir, fileName) | ||
|
||
const unzip = createGunzip() | ||
const extractStream = extract() | ||
unzip.pipe(extractStream) | ||
|
||
const download = (u: string) => stream(u, { opaque: { unzip }, method: 'GET' }, ({ opaque, headers, statusCode }) => { | ||
if (statusCode === 200) return (opaque as any).unzip | ||
Object.assign(opaque as any, { | ||
failed: true, | ||
headers, | ||
statusCode, | ||
}) | ||
return new PassThrough() | ||
}) | ||
|
||
const { opaque } = await download(primary) | ||
|
||
if ((opaque as any).failed) { | ||
const { opaque } = await download(fallback) | ||
|
||
if ((opaque as any).failed) { | ||
throw new AnyError('NativeDownloadError', 'Failed to download ' + fileName) | ||
} | ||
} | ||
|
||
for await (const e of extractStream) { | ||
if (e.header.name.endsWith(fileName)) { | ||
const bufs = [] as Buffer[] | ||
for await (const d of e) { | ||
bufs.push(d) | ||
} | ||
await writeFile(dest, Buffer.concat(bufs)) | ||
} | ||
} | ||
|
||
return dest | ||
} |