onehitter 2.0.8 → 2.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -2
- package/dist/cjs/db/sqlite-functions.js +19 -1
- package/dist/esm/db/sqlite-functions.js +19 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ It generates an OTP, stores a hashed record, emails it to the user, then validat
|
|
|
28
28
|
- create(...): persist a hashed OTP document
|
|
29
29
|
- send(to, otp): email the OTP via SES (customizable template)
|
|
30
30
|
- validate / validateStatus(...): consume once and return success or a detailed status
|
|
31
|
-
- Storage adapter: MongoDB (default) or SQLite (experimental). Choose with `
|
|
31
|
+
- Storage adapter: MongoDB (default) or SQLite (experimental). Choose with `OTP_DB_DRIVER` (`mongodb` or `sqlite`).
|
|
32
32
|
- Expiry: checked at validation time; MongoDB users should also create a TTL index on `createdAt` for cleanup.
|
|
33
33
|
- Rate limiting: bring your own limiter or enable a built-in in-memory limiter via env flags.
|
|
34
34
|
|
|
@@ -102,7 +102,12 @@ if (status === 'ok') {
|
|
|
102
102
|
|
|
103
103
|
## Databases
|
|
104
104
|
- Default: MongoDB. Your app owns the `MongoClient` (construct, connect/close, pass to `create`/`validate`).
|
|
105
|
-
- Optional: SQLite (`
|
|
105
|
+
- Optional: SQLite (`OTP_DB_DRIVER=sqlite`, optional `SQLITE_PATH`); good for tests/small apps.
|
|
106
|
+
|
|
107
|
+
### Database driver env
|
|
108
|
+
- `OTP_DB_DRIVER` (optional): selects the storage driver.
|
|
109
|
+
- `mongodb` (default): uses the MongoDB adapter and only requires the `mongodb` dependency.
|
|
110
|
+
- `sqlite`: uses the built-in SQLite adapter and requires the host app to install `sqlite3` (for example, `npm install sqlite3`). When `OTP_DB_DRIVER=mongodb`, `sqlite3` is not required and is not loaded.
|
|
106
111
|
|
|
107
112
|
Details and tradeoffs: docs/DB.md
|
|
108
113
|
|
|
@@ -4,11 +4,29 @@ exports.otpValidateWithStatus = exports.otpCreate = void 0;
|
|
|
4
4
|
const shared_1 = require("./shared");
|
|
5
5
|
let sqlite3;
|
|
6
6
|
let db = null;
|
|
7
|
+
// Bundler-safe loader for optional sqlite3 dependency.
|
|
8
|
+
// Using eval('require') prevents bundlers from eagerly resolving the sqlite3
|
|
9
|
+
// module when the SQLite driver is not used (e.g. when OTP_DB_DRIVER=mongodb).
|
|
10
|
+
function loadSqlite3() {
|
|
11
|
+
try {
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
13
|
+
const req = eval('require');
|
|
14
|
+
return req('sqlite3');
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
// Provide a clear error when the SQLite driver is selected but sqlite3
|
|
18
|
+
// is not installed in the host application.
|
|
19
|
+
const message = err && err.code === 'MODULE_NOT_FOUND'
|
|
20
|
+
? 'The sqlite3 package is not installed. Install it with "npm install sqlite3" to use OTP_DB_DRIVER=sqlite.'
|
|
21
|
+
: `Failed to load sqlite3 driver: ${String(err)}`;
|
|
22
|
+
throw new Error(message);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
7
25
|
function getDb() {
|
|
8
26
|
if (db)
|
|
9
27
|
return db;
|
|
10
28
|
// Lazy-load sqlite3 only when the SQLite driver is actually used
|
|
11
|
-
const s = sqlite3 ?? (sqlite3 =
|
|
29
|
+
const s = sqlite3 ?? (sqlite3 = loadSqlite3());
|
|
12
30
|
db = new s.Database(shared_1.SQLITE_PATH);
|
|
13
31
|
db.serialize(() => {
|
|
14
32
|
db.run('CREATE TABLE IF NOT EXISTS otp (\n' +
|
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
import { SQLITE_PATH, computeOtpHash, computeContactId } from './shared';
|
|
2
2
|
let sqlite3;
|
|
3
3
|
let db = null;
|
|
4
|
+
// Bundler-safe loader for optional sqlite3 dependency.
|
|
5
|
+
// Using eval('require') prevents bundlers from eagerly resolving the sqlite3
|
|
6
|
+
// module when the SQLite driver is not used (e.g. when OTP_DB_DRIVER=mongodb).
|
|
7
|
+
function loadSqlite3() {
|
|
8
|
+
try {
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
10
|
+
const req = eval('require');
|
|
11
|
+
return req('sqlite3');
|
|
12
|
+
}
|
|
13
|
+
catch (err) {
|
|
14
|
+
// Provide a clear error when the SQLite driver is selected but sqlite3
|
|
15
|
+
// is not installed in the host application.
|
|
16
|
+
const message = err && err.code === 'MODULE_NOT_FOUND'
|
|
17
|
+
? 'The sqlite3 package is not installed. Install it with "npm install sqlite3" to use OTP_DB_DRIVER=sqlite.'
|
|
18
|
+
: `Failed to load sqlite3 driver: ${String(err)}`;
|
|
19
|
+
throw new Error(message);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
4
22
|
function getDb() {
|
|
5
23
|
if (db)
|
|
6
24
|
return db;
|
|
7
25
|
// Lazy-load sqlite3 only when the SQLite driver is actually used
|
|
8
|
-
const s = sqlite3 ?? (sqlite3 =
|
|
26
|
+
const s = sqlite3 ?? (sqlite3 = loadSqlite3());
|
|
9
27
|
db = new s.Database(SQLITE_PATH);
|
|
10
28
|
db.serialize(() => {
|
|
11
29
|
db.run('CREATE TABLE IF NOT EXISTS otp (\n' +
|