Skip to content

Commit 3400cb0

Browse files
destykmceachen
andauthored
feat(database): now nativeBinding supports addon object (#974)
* 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 <[email protected]>
1 parent ccf0b35 commit 3400cb0

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

lib/database.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,45 @@ function Database(filenameGiven, options) {
3333
const fileMustExist = util.getBooleanOption(options, 'fileMustExist');
3434
const timeout = 'timeout' in options ? options.timeout : 5000;
3535
const verbose = 'verbose' in options ? options.verbose : null;
36-
const nativeBindingPath = 'nativeBinding' in options ? options.nativeBinding : null;
36+
const nativeBinding = 'nativeBinding' in options ? options.nativeBinding : null;
3737

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

4545
// Load the native addon
4646
let addon;
47-
if (nativeBindingPath == null) {
48-
addon = DEFAULT_ADDON || (DEFAULT_ADDON = require('bindings')('better_sqlite3.node'));
49-
} else {
50-
addon = require(path.resolve(nativeBindingPath).replace(/(\.node)?$/, '.node'));
47+
switch(true) {
48+
case nativeBinding == null: {
49+
addon = DEFAULT_ADDON || (DEFAULT_ADDON = require('bindings')('better_sqlite3.node'));
50+
break;
51+
}
52+
53+
case typeof nativeBinding === 'string': {
54+
// If webpack is used to build the application, we must use `__non_webpack_require__`.
55+
// See <https://webpack.js.org/api/module-variables/#__non_webpack_require__-webpack-specific>
56+
const requireFunc =
57+
typeof __non_webpack_require__ === 'function' ? __non_webpack_require__ : require;
58+
59+
addon = requireFunc(path.resolve(nativeBinding).replace(/(\.node)?$/, '.node'));
60+
break;
61+
}
62+
63+
case typeof nativeBinding === 'object': {
64+
// If `nativeBinding` is addon object.
65+
// See <https://github.com/WiseLibs/better-sqlite3/issues/972>
66+
addon = nativeBinding;
67+
break;
68+
}
69+
70+
default: {
71+
throw new TypeError('nativeBinding has an invalid value passed');
72+
}
5173
}
74+
5275
if (!addon.isInitialized) {
5376
addon.setErrorConstructor(SqliteError);
5477
addon.isInitialized = true;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
"window functions",
5858
"database"
5959
]
60-
}
60+
}

0 commit comments

Comments
 (0)