Skip to content

Commit

Permalink
feat(database): now nativeBinding supports addon object (#974)
Browse files Browse the repository at this point in the history
* Added `webpack` support

* changed from `webpack_require` to `non_webpack_require`

* Update database.js

Added URL to webpack documentation

* feat(database): now `nativeBinding` supports addon object

* style(package): line break removed

---------

Co-authored-by: Matthew McEachen <matthew@photostructure.com>
  • Loading branch information
destyk and mceachen committed Sep 2, 2023
1 parent ccf0b35 commit 3400cb0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
35 changes: 29 additions & 6 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,45 @@ function Database(filenameGiven, options) {
const fileMustExist = util.getBooleanOption(options, 'fileMustExist');
const timeout = 'timeout' in options ? options.timeout : 5000;
const verbose = 'verbose' in options ? options.verbose : null;
const nativeBindingPath = 'nativeBinding' in options ? options.nativeBinding : null;
const nativeBinding = 'nativeBinding' in options ? options.nativeBinding : null;

// Validate interpreted options
if (readonly && anonymous && !buffer) throw new TypeError('In-memory/temporary databases cannot be readonly');
if (!Number.isInteger(timeout) || timeout < 0) throw new TypeError('Expected the "timeout" option to be a positive integer');
if (timeout > 0x7fffffff) throw new RangeError('Option "timeout" cannot be greater than 2147483647');
if (verbose != null && typeof verbose !== 'function') throw new TypeError('Expected the "verbose" option to be a function');
if (nativeBindingPath != null && typeof nativeBindingPath !== 'string') throw new TypeError('Expected the "nativeBinding" option to be a string');
if (nativeBinding != null && typeof nativeBinding !== 'string' && typeof nativeBinding !== 'object') throw new TypeError('Expected the "nativeBinding" option to be a string or addon object');

// Load the native addon
let addon;
if (nativeBindingPath == null) {
addon = DEFAULT_ADDON || (DEFAULT_ADDON = require('bindings')('better_sqlite3.node'));
} else {
addon = require(path.resolve(nativeBindingPath).replace(/(\.node)?$/, '.node'));
switch(true) {
case nativeBinding == null: {
addon = DEFAULT_ADDON || (DEFAULT_ADDON = require('bindings')('better_sqlite3.node'));
break;
}

case typeof nativeBinding === 'string': {
// If webpack is used to build the application, we must use `__non_webpack_require__`.
// See <https://webpack.js.org/api/module-variables/#__non_webpack_require__-webpack-specific>
const requireFunc =
typeof __non_webpack_require__ === 'function' ? __non_webpack_require__ : require;

addon = requireFunc(path.resolve(nativeBinding).replace(/(\.node)?$/, '.node'));
break;
}

case typeof nativeBinding === 'object': {
// If `nativeBinding` is addon object.
// See <https://github.com/WiseLibs/better-sqlite3/issues/972>
addon = nativeBinding;
break;
}

default: {
throw new TypeError('nativeBinding has an invalid value passed');
}
}

if (!addon.isInitialized) {
addon.setErrorConstructor(SqliteError);
addon.isInitialized = true;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@
"window functions",
"database"
]
}
}

0 comments on commit 3400cb0

Please sign in to comment.