Skip to content

Commit ee7d056

Browse files
authored
Fix Node.js library path resolution for packaged Electron apps (#797)
1 parent 9c03d2a commit ee7d056

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

nodejs_package/brainflow/board_shim.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
LogLevels,
1313
} from './brainflow.types';
1414
import {BoardControllerCLikeFunctions as CLike, BoardControllerFunctions} from './functions.types';
15+
import {resolveLibPath} from './lib_path';
1516

1617
export class BrainFlowInputParams
1718
{
@@ -60,7 +61,7 @@ class BoardControllerDLL extends BoardControllerFunctions
6061
private constructor()
6162
{
6263
super ();
63-
this.libPath = `${__dirname}/../brainflow/lib`;
64+
this.libPath = resolveLibPath();
6465
this.dllPath = this.getDLLPath();
6566
this.lib = this.getLib();
6667

@@ -705,4 +706,4 @@ export class BoardShim
705706
}
706707
return JSON.parse(out[0].substring(0, len[0]));
707708
}
708-
}
709+
}

nodejs_package/brainflow/data_filter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from './brainflow.types';
2020
import {complex} from './complex';
2121
import {DataHandlerCLikeFunctions as CLike, DataHandlerFunctions} from './functions.types';
22+
import {resolveLibPath} from './lib_path';
2223

2324
class DataHandlerDLL extends DataHandlerFunctions
2425
{
@@ -32,7 +33,7 @@ class DataHandlerDLL extends DataHandlerFunctions
3233
private constructor()
3334
{
3435
super ();
35-
this.libPath = `${__dirname}/../brainflow/lib`;
36+
this.libPath = resolveLibPath();
3637
this.dllPath = this.getDLLPath();
3738
this.lib = this.getLib();
3839

@@ -606,4 +607,4 @@ export class DataFilter
606607
}
607608
return output[0];
608609
}
609-
}
610+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
const appAsarRegexp = new RegExp(`\\${path.sep}app\\.asar\\${path.sep}`, 'g');
5+
6+
export function resolveLibPath(): string
7+
{
8+
const defaultLibPath = path.resolve(__dirname, '..', 'brainflow', 'lib');
9+
const electronProcess = process as NodeJS.Process & {resourcesPath?: string};
10+
const searchRoots = [
11+
__dirname,
12+
__dirname.replace(appAsarRegexp, `${path.sep}app.asar.unpacked${path.sep}`),
13+
typeof electronProcess.resourcesPath === 'string' ? electronProcess.resourcesPath : '',
14+
].filter(Boolean);
15+
16+
const libPathCandidates = [
17+
defaultLibPath,
18+
...searchRoots.flatMap((root) => [
19+
path.resolve(root, '..', 'brainflow', 'lib'),
20+
path.resolve(root, '..', 'node_modules', 'brainflow', 'brainflow', 'lib'),
21+
path.resolve(root, 'node_modules', 'brainflow', 'brainflow', 'lib'),
22+
path.resolve(root, 'app.asar.unpacked', 'node_modules', 'brainflow', 'brainflow', 'lib'),
23+
]),
24+
];
25+
26+
const existingPath = libPathCandidates.find((candidate) => fs.existsSync(candidate));
27+
return existingPath || defaultLibPath;
28+
}

nodejs_package/brainflow/ml_model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
LogLevels,
1212
} from './brainflow.types';
1313
import {MLModuleCLikeFunctions as CLike, MLModuleFunctions} from './functions.types';
14+
import {resolveLibPath} from './lib_path';
1415

1516
export class BrainFlowModelParams
1617
{
@@ -52,7 +53,7 @@ class MLModuleDLL extends MLModuleFunctions
5253
private constructor()
5354
{
5455
super ();
55-
this.libPath = `${__dirname}/../brainflow/lib`;
56+
this.libPath = resolveLibPath();
5657
this.dllPath = this.getDLLPath();
5758
this.lib = this.getLib();
5859

0 commit comments

Comments
 (0)