Skip to content
Open
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
5 changes: 3 additions & 2 deletions nodejs_package/brainflow/board_shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
LogLevels,
} from './brainflow.types';
import {BoardControllerCLikeFunctions as CLike, BoardControllerFunctions} from './functions.types';
import {resolveLibPath} from './lib_path';

export class BrainFlowInputParams
{
Expand Down Expand Up @@ -60,7 +61,7 @@ class BoardControllerDLL extends BoardControllerFunctions
private constructor()
{
super ();
this.libPath = `${__dirname}/../brainflow/lib`;
this.libPath = resolveLibPath();
this.dllPath = this.getDLLPath();
this.lib = this.getLib();

Expand Down Expand Up @@ -705,4 +706,4 @@ export class BoardShim
}
return JSON.parse(out[0].substring(0, len[0]));
}
}
}
5 changes: 3 additions & 2 deletions nodejs_package/brainflow/data_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from './brainflow.types';
import {complex} from './complex';
import {DataHandlerCLikeFunctions as CLike, DataHandlerFunctions} from './functions.types';
import {resolveLibPath} from './lib_path';

class DataHandlerDLL extends DataHandlerFunctions
{
Expand All @@ -32,7 +33,7 @@ class DataHandlerDLL extends DataHandlerFunctions
private constructor()
{
super ();
this.libPath = `${__dirname}/../brainflow/lib`;
this.libPath = resolveLibPath();
this.dllPath = this.getDLLPath();
this.lib = this.getLib();

Expand Down Expand Up @@ -606,4 +607,4 @@ export class DataFilter
}
return output[0];
}
}
}
28 changes: 28 additions & 0 deletions nodejs_package/brainflow/lib_path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from 'fs';
import path from 'path';

const appAsarRegexp = new RegExp(`\\${path.sep}app\\.asar\\${path.sep}`, 'g');

export function resolveLibPath(): string
{
const defaultLibPath = path.resolve(__dirname, '..', 'brainflow', 'lib');
const electronProcess = process as NodeJS.Process & {resourcesPath?: string};
const searchRoots = [
__dirname,
__dirname.replace(appAsarRegexp, `${path.sep}app.asar.unpacked${path.sep}`),
typeof electronProcess.resourcesPath === 'string' ? electronProcess.resourcesPath : '',
].filter(Boolean);

const libPathCandidates = [
defaultLibPath,
...searchRoots.flatMap((root) => [
Comment on lines +16 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Prioritize unpacked lib paths over app.asar paths

resolveLibPath() checks defaultLibPath first, so in Electron builds that use app.asar.unpacked it can still return an .../app.asar/... path when fs.existsSync reports that virtual ASAR entry as present. koffi.load/native dlopen requires a real filesystem path, so this selection causes native library loading to fail even when the unpacked copy exists; the unpacked candidates need to be searched before the default ASAR path.

Useful? React with 👍 / 👎.

path.resolve(root, '..', 'brainflow', 'lib'),
path.resolve(root, '..', 'node_modules', 'brainflow', 'brainflow', 'lib'),
path.resolve(root, 'node_modules', 'brainflow', 'brainflow', 'lib'),
path.resolve(root, 'app.asar.unpacked', 'node_modules', 'brainflow', 'brainflow', 'lib'),
]),
];

const existingPath = libPathCandidates.find((candidate) => fs.existsSync(candidate));
return existingPath || defaultLibPath;
}
3 changes: 2 additions & 1 deletion nodejs_package/brainflow/ml_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
LogLevels,
} from './brainflow.types';
import {MLModuleCLikeFunctions as CLike, MLModuleFunctions} from './functions.types';
import {resolveLibPath} from './lib_path';

export class BrainFlowModelParams
{
Expand Down Expand Up @@ -52,7 +53,7 @@ class MLModuleDLL extends MLModuleFunctions
private constructor()
{
super ();
this.libPath = `${__dirname}/../brainflow/lib`;
this.libPath = resolveLibPath();
this.dllPath = this.getDLLPath();
this.lib = this.getLib();

Expand Down
Loading