Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
CMCDragonkai committed Jun 25, 2023
1 parent b1bcc6c commit d015023
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"gypfile": true,
"scripts": {
"prepare": "tsc -p ./tsconfig.build.json",
"install": "node-gyp-build",
"prebuild": "node ./scripts/prebuild.js",
"build": "shx rm -rf ./dist && tsc -p ./tsconfig.build.json",
"version": "node ./scripts/version.js",
Expand All @@ -35,7 +34,6 @@
"@matrixai/logger": "^3.1.0",
"@matrixai/resources": "^1.1.5",
"@matrixai/workers": "^1.3.7",
"node-gyp-build": "4.4.0",
"threads": "^1.6.5"
},
"optionalDependencies": {
Expand Down
99 changes: 96 additions & 3 deletions src/native/rocksdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type {
RocksDBCountOptions,
} from './types';
import path from 'path';
import nodeGypBuild from 'node-gyp-build';

interface RocksDB {
dbInit(): RocksDBDatabase;
Expand Down Expand Up @@ -271,8 +270,102 @@ interface RocksDB {
): void;
}

const rocksdb: RocksDB = nodeGypBuild(path.join(__dirname, '../../'));
const projectRoot = path.join(__dirname, '../../');
const prebuildPath = path.join(projectRoot, 'prebuild');

export default rocksdb;
/**
* Try require on all prebuild targets first, then
* try require on all npm targets second.
*/
function requireBinding(targets: Array<string>): RocksDB {
const prebuildTargets = targets.map((target) =>
path.join(prebuildPath, `db-${target}.node`),
);
for (const prebuildTarget of prebuildTargets) {
try {
return require(prebuildTarget);
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') throw e;
}
}
const npmTargets = targets.map((target) => `@matrixai/db-${target}`);
for (const npmTarget of npmTargets) {
try {
return require(npmTarget);
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') throw e;
}
}
throw new Error(
`Failed requiring possible native bindings: ${prebuildTargets.concat(
npmTargets,
)}`,
);
}

let nativeBinding: RocksDB;

/**
* For desktop we only support win32, darwin and linux.
* Mobile OS support is pending.
*/
switch (process.platform) {
case 'win32':
switch (process.arch) {
case 'x64':
nativeBinding = requireBinding(['win32-x64']);
break;
case 'ia32':
nativeBinding = requireBinding(['win32-ia32']);
break;
case 'arm64':
nativeBinding = requireBinding(['win32-arm64']);
break;
default:
throw new Error(`Unsupported architecture on Windows: ${process.arch}`);
}
break;
case 'darwin':
switch (process.arch) {
case 'x64':
nativeBinding = requireBinding([
'darwin-x64',
'darwin-x64+arm64',
'darwin-arm64+x64',
]);
break;
case 'arm64':
nativeBinding = requireBinding([
'darwin-arm64',
'darwin-arm64+x64',
'darwin-x64+arm64',
]);
break;
default:
throw new Error(`Unsupported architecture on macOS: ${process.arch}`);
}
break;
case 'linux':
switch (process.arch) {
case 'x64':
nativeBinding = requireBinding(['linux-x64']);
break;
case 'arm64':
nativeBinding = requireBinding(['linux-arm64']);
break;
case 'arm':
nativeBinding = requireBinding(['linux-arm']);
break;
default:
throw new Error(`Unsupported architecture on Linux: ${process.arch}`);
}
break;
default:
throw new Error(
`Unsupported OS: ${process.platform}, architecture: ${process.arch}`,
);
}

export default nativeBinding;

export type { RocksDB };

0 comments on commit d015023

Please sign in to comment.