vin-mcp 1.0.0
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/LICENSE +21 -0
- package/README.md +191 -0
- package/lib/auth.mjs +48 -0
- package/lib/cache.mjs +65 -0
- package/lib/db.mjs +100 -0
- package/lib/epa.mjs +157 -0
- package/lib/nhtsa.mjs +392 -0
- package/lib/photo.mjs +67 -0
- package/lib/validate.mjs +471 -0
- package/package.json +51 -0
- package/public/app.js +1146 -0
- package/public/index.html +382 -0
- package/public/style.css +1235 -0
- package/server.mjs +934 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 keptlive
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# VIN MCP
|
|
2
|
+
|
|
3
|
+
**Free VIN decoder for humans and AI. No API keys. No accounts. No limits.**
|
|
4
|
+
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](https://nodejs.org)
|
|
7
|
+
[](https://modelcontextprotocol.io)
|
|
8
|
+
[](https://smithery.ai/server/@keptlive/vin-mcp)
|
|
9
|
+
|
|
10
|
+
[mcp.vin](https://mcp.vin) -- Try it now
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## What It Does
|
|
15
|
+
|
|
16
|
+
Enter a 17-character VIN and get a comprehensive vehicle report by aggregating six free data sources into a single response:
|
|
17
|
+
|
|
18
|
+
| Source | Data Provided |
|
|
19
|
+
|--------|--------------|
|
|
20
|
+
| **NHTSA vPIC** | Make, model, year, trim, body class, engine specs, transmission, drive type, weight, plant info, 140+ decoded fields |
|
|
21
|
+
| **NHTSA Recalls** | Open recalls with campaign number, component, summary, consequence, and remedy |
|
|
22
|
+
| **NHTSA Complaints** | Consumer complaints with crash, fire, injury, and death statistics |
|
|
23
|
+
| **NHTSA Safety Ratings** | NCAP star ratings for overall, frontal, side, and rollover crash tests |
|
|
24
|
+
| **EPA Fuel Economy** | City/highway/combined MPG, annual fuel cost, CO2 emissions, EV range and charge time |
|
|
25
|
+
| **IMAGIN.studio** | Stock vehicle photos from multiple angles |
|
|
26
|
+
|
|
27
|
+
Additionally, VIN validation (checksum, WMI country/manufacturer decode, model year) is computed locally with zero API calls.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Quick Start -- Connect via MCP
|
|
32
|
+
|
|
33
|
+
### Smithery (one command)
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npx -y @smithery/cli install @keptlive/vin-mcp --client claude
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Claude Code (stdio)
|
|
40
|
+
|
|
41
|
+
Add to your `.mcp.json`:
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
{
|
|
45
|
+
"mcpServers": {
|
|
46
|
+
"vin": {
|
|
47
|
+
"command": "npx",
|
|
48
|
+
"args": ["-y", "vin-mcp"]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Or if you have the repo cloned locally:
|
|
55
|
+
|
|
56
|
+
```json
|
|
57
|
+
{
|
|
58
|
+
"mcpServers": {
|
|
59
|
+
"vin": {
|
|
60
|
+
"command": "node",
|
|
61
|
+
"args": ["/path/to/vin-mcp/server.mjs"]
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Claude Desktop / claude.ai (HTTP)
|
|
68
|
+
|
|
69
|
+
Use the hosted MCP endpoint:
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
https://mcp.vin/mcp
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Add to your Claude Desktop config:
|
|
76
|
+
|
|
77
|
+
```json
|
|
78
|
+
{
|
|
79
|
+
"mcpServers": {
|
|
80
|
+
"vin": {
|
|
81
|
+
"url": "https://mcp.vin/mcp"
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## MCP Tools
|
|
90
|
+
|
|
91
|
+
| Tool | Description | Input |
|
|
92
|
+
|------|-------------|-------|
|
|
93
|
+
| `decode_vin` | Full VIN decode with specs, recalls, complaints, safety ratings, fuel economy, and photos | `{ vin: string }` |
|
|
94
|
+
| `validate_vin` | Quick local validation -- checksum, WMI country/manufacturer, model year. No external API calls | `{ vin: string }` |
|
|
95
|
+
| `lookup_recalls` | Look up recalls by VIN or by make/model/year | `{ vin?: string, make?: string, model?: string, year?: number }` |
|
|
96
|
+
| `batch_decode` | Decode up to 50 VINs in a single request via NHTSA batch API | `{ vins: string[] }` |
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## REST API
|
|
101
|
+
|
|
102
|
+
All endpoints are available at `https://mcp.vin` or on your self-hosted instance.
|
|
103
|
+
|
|
104
|
+
| Method | Endpoint | Description |
|
|
105
|
+
|--------|----------|-------------|
|
|
106
|
+
| `GET` | `/api/vin/:vin` | Full decode -- all 6 sources aggregated |
|
|
107
|
+
| `GET` | `/api/vin/:vin/validate` | Quick checksum and format validation |
|
|
108
|
+
| `GET` | `/api/vin/:vin/recalls` | Recall data only |
|
|
109
|
+
| `GET` | `/api/vin/:vin/complaints` | Consumer complaints only |
|
|
110
|
+
| `GET` | `/api/vin/:vin/safety` | NCAP safety ratings only |
|
|
111
|
+
| `GET` | `/api/vin/:vin/fuel` | EPA fuel economy only |
|
|
112
|
+
| `GET` | `/api/vin/:vin/photo` | Redirects to vehicle photo URL |
|
|
113
|
+
| `POST` | `/api/batch` | Batch decode (body: `{ "vins": ["VIN1", "VIN2", ...] }`, max 50) |
|
|
114
|
+
|
|
115
|
+
Rate limits: 30 requests/minute per IP for most endpoints, 60/minute for validation and photos, 5/minute for batch.
|
|
116
|
+
|
|
117
|
+
**Example:**
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
curl https://mcp.vin/api/vin/1HGCM82633A004352
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Direct VIN URLs also work -- visit `https://mcp.vin/1HGCM82633A004352` to see the web report.
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## Self-Hosting
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
git clone https://github.com/keptlive/vin-mcp.git
|
|
131
|
+
cd vin-mcp
|
|
132
|
+
npm install
|
|
133
|
+
node server.mjs --http --port 3200
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
The server starts in HTTP mode with:
|
|
137
|
+
- Web frontend at `http://localhost:3200`
|
|
138
|
+
- REST API at `http://localhost:3200/api/vin/{vin}`
|
|
139
|
+
- MCP endpoint at `http://localhost:3200/mcp`
|
|
140
|
+
|
|
141
|
+
For stdio mode (Claude Code integration without a web server):
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
node server.mjs
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## How VINs Work
|
|
150
|
+
|
|
151
|
+
A VIN (Vehicle Identification Number) is a 17-character code assigned to every vehicle manufactured since 1981. Each position encodes specific information:
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
1 H G C M 8 2 6 3 3 A 0 0 4 3 5 2
|
|
155
|
+
|_____| |___| | | | |_| |_________|
|
|
156
|
+
WMI VDS | | | |yr Sequential
|
|
157
|
+
| | | plant
|
|
158
|
+
| | check digit
|
|
159
|
+
| vehicle attributes
|
|
160
|
+
manufacturer ID
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
- **Positions 1-3 (WMI):** World Manufacturer Identifier -- country and manufacturer
|
|
164
|
+
- **Positions 4-8 (VDS):** Vehicle Descriptor Section -- model, body, engine, transmission
|
|
165
|
+
- **Position 9:** Check digit -- validates the VIN using a weighted algorithm
|
|
166
|
+
- **Position 10:** Model year code (A-Y, 1-9 on a 30-year cycle)
|
|
167
|
+
- **Position 11:** Assembly plant
|
|
168
|
+
- **Positions 12-17:** Sequential production number
|
|
169
|
+
|
|
170
|
+
The letters I, O, and Q are never used in VINs to avoid confusion with 1, 0, and 9.
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
## Tech Stack
|
|
175
|
+
|
|
176
|
+
- **Runtime:** Node.js 18+
|
|
177
|
+
- **Server:** Express 5
|
|
178
|
+
- **MCP SDK:** `@modelcontextprotocol/sdk` with stdio and Streamable HTTP transports
|
|
179
|
+
- **Frontend:** Vanilla HTML, CSS, and JavaScript
|
|
180
|
+
- **Caching:** In-memory LRU with TTL (1h for decodes, 6h for recalls, 24h for ratings and fuel data)
|
|
181
|
+
- **External dependencies:** Zero API keys required -- all data sources are free public APIs
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## License
|
|
186
|
+
|
|
187
|
+
MIT
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
Built for [mcp.vin](https://mcp.vin)
|
package/lib/auth.mjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
const SCRYPT_OPTS = { N: 16384, r: 8, p: 1 };
|
|
4
|
+
|
|
5
|
+
export function hashPassword(password) {
|
|
6
|
+
const salt = crypto.randomBytes(16).toString('hex');
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
crypto.scrypt(password, salt, 64, SCRYPT_OPTS, (err, key) => {
|
|
9
|
+
if (err) reject(err);
|
|
10
|
+
else resolve({ hash: key.toString('hex'), salt });
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function verifyPassword(password, hash, salt) {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
crypto.scrypt(password, salt, 64, SCRYPT_OPTS, (err, key) => {
|
|
18
|
+
if (err) reject(err);
|
|
19
|
+
else resolve(crypto.timingSafeEqual(Buffer.from(hash, 'hex'), key));
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createToken(payload, secret, expiresInSec = 86400) {
|
|
25
|
+
const header = Buffer.from(JSON.stringify({ alg: 'HS256', typ: 'JWT' })).toString('base64url');
|
|
26
|
+
const now = Math.floor(Date.now() / 1000);
|
|
27
|
+
const body = Buffer.from(JSON.stringify({ ...payload, iat: now, exp: now + expiresInSec })).toString('base64url');
|
|
28
|
+
const sig = crypto.createHmac('sha256', secret).update(`${header}.${body}`).digest('base64url');
|
|
29
|
+
return `${header}.${body}.${sig}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function verifyJwt(token, secret) {
|
|
33
|
+
try {
|
|
34
|
+
const parts = token.split('.');
|
|
35
|
+
if (parts.length !== 3) return null;
|
|
36
|
+
const [header, body, sig] = parts;
|
|
37
|
+
const expected = crypto.createHmac('sha256', secret).update(`${header}.${body}`).digest('base64url');
|
|
38
|
+
const sigBuf = Buffer.from(sig, 'base64url');
|
|
39
|
+
const expectedBuf = Buffer.from(expected, 'base64url');
|
|
40
|
+
if (sigBuf.length !== expectedBuf.length) return null;
|
|
41
|
+
if (!crypto.timingSafeEqual(sigBuf, expectedBuf)) return null;
|
|
42
|
+
const payload = JSON.parse(Buffer.from(body, 'base64url').toString());
|
|
43
|
+
if (payload.exp < Math.floor(Date.now() / 1000)) return null;
|
|
44
|
+
return payload;
|
|
45
|
+
} catch {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
package/lib/cache.mjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// LRU Cache with TTL for VIN decode results
|
|
2
|
+
|
|
3
|
+
const DEFAULT_MAX = 1000;
|
|
4
|
+
const DEFAULT_TTL = 60 * 60 * 1000; // 1 hour
|
|
5
|
+
|
|
6
|
+
export class LRUCache {
|
|
7
|
+
constructor(max = DEFAULT_MAX, ttl = DEFAULT_TTL) {
|
|
8
|
+
this.max = max;
|
|
9
|
+
this.ttl = ttl;
|
|
10
|
+
this.map = new Map(); // key → { value, expires }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get(key) {
|
|
14
|
+
const entry = this.map.get(key);
|
|
15
|
+
if (!entry) return undefined;
|
|
16
|
+
if (Date.now() > entry.expires) {
|
|
17
|
+
this.map.delete(key);
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
// Move to end (most recent)
|
|
21
|
+
this.map.delete(key);
|
|
22
|
+
this.map.set(key, entry);
|
|
23
|
+
return entry.value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
set(key, value) {
|
|
27
|
+
this.map.delete(key); // remove old position
|
|
28
|
+
this.map.set(key, { value, expires: Date.now() + this.ttl });
|
|
29
|
+
// Evict oldest if over capacity
|
|
30
|
+
if (this.map.size > this.max) {
|
|
31
|
+
const oldest = this.map.keys().next().value;
|
|
32
|
+
this.map.delete(oldest);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
has(key) {
|
|
37
|
+
return this.get(key) !== undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
delete(key) {
|
|
41
|
+
return this.map.delete(key);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
clear() {
|
|
45
|
+
this.map.clear();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get size() {
|
|
49
|
+
return this.map.size;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Prune all expired entries
|
|
53
|
+
prune() {
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
for (const [key, entry] of this.map) {
|
|
56
|
+
if (now > entry.expires) this.map.delete(key);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Shared cache instances
|
|
62
|
+
export const vinCache = new LRUCache(1000, 60 * 60 * 1000); // 1hr for full decodes
|
|
63
|
+
export const recallCache = new LRUCache(500, 6 * 60 * 60 * 1000); // 6hr for recalls (change less often)
|
|
64
|
+
export const ratingCache = new LRUCache(500, 24 * 60 * 60 * 1000); // 24hr for safety ratings (static)
|
|
65
|
+
export const fuelCache = new LRUCache(500, 24 * 60 * 60 * 1000); // 24hr for fuel economy (static)
|
package/lib/db.mjs
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import Database from 'better-sqlite3';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
|
|
6
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
const dataDir = path.join(__dirname, '..', 'data');
|
|
8
|
+
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true });
|
|
9
|
+
|
|
10
|
+
const db = new Database(path.join(dataDir, 'vin.db'));
|
|
11
|
+
db.pragma('journal_mode = WAL');
|
|
12
|
+
db.pragma('foreign_keys = ON');
|
|
13
|
+
|
|
14
|
+
db.exec(`
|
|
15
|
+
CREATE TABLE IF NOT EXISTS users (
|
|
16
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
17
|
+
email TEXT UNIQUE NOT NULL COLLATE NOCASE,
|
|
18
|
+
password_hash TEXT NOT NULL,
|
|
19
|
+
salt TEXT NOT NULL,
|
|
20
|
+
display_name TEXT,
|
|
21
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
22
|
+
updated_at TEXT DEFAULT (datetime('now'))
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
CREATE TABLE IF NOT EXISTS saved_vins (
|
|
26
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
27
|
+
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
28
|
+
vin TEXT NOT NULL,
|
|
29
|
+
label TEXT,
|
|
30
|
+
year INTEGER,
|
|
31
|
+
make TEXT,
|
|
32
|
+
model TEXT,
|
|
33
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
34
|
+
UNIQUE(user_id, vin)
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
CREATE INDEX IF NOT EXISTS idx_saved_vins_user ON saved_vins(user_id);
|
|
38
|
+
|
|
39
|
+
CREATE TABLE IF NOT EXISTS output_preferences (
|
|
40
|
+
user_id INTEGER PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
|
|
41
|
+
show_overview INTEGER DEFAULT 1,
|
|
42
|
+
show_engine INTEGER DEFAULT 1,
|
|
43
|
+
show_safety_ratings INTEGER DEFAULT 1,
|
|
44
|
+
show_fuel_economy INTEGER DEFAULT 1,
|
|
45
|
+
show_recalls INTEGER DEFAULT 1,
|
|
46
|
+
show_complaints INTEGER DEFAULT 1,
|
|
47
|
+
show_safety_equipment INTEGER DEFAULT 1,
|
|
48
|
+
show_photos INTEGER DEFAULT 1,
|
|
49
|
+
show_raw_nhtsa INTEGER DEFAULT 0,
|
|
50
|
+
updated_at TEXT DEFAULT (datetime('now'))
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
-- Observability: request log
|
|
54
|
+
CREATE TABLE IF NOT EXISTS request_log (
|
|
55
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
56
|
+
ts TEXT DEFAULT (datetime('now')),
|
|
57
|
+
ip TEXT,
|
|
58
|
+
method TEXT,
|
|
59
|
+
path TEXT,
|
|
60
|
+
status INTEGER,
|
|
61
|
+
duration_ms INTEGER,
|
|
62
|
+
user_agent TEXT,
|
|
63
|
+
user_id INTEGER,
|
|
64
|
+
bytes INTEGER DEFAULT 0
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
CREATE INDEX IF NOT EXISTS idx_reqlog_ts ON request_log(ts);
|
|
68
|
+
CREATE INDEX IF NOT EXISTS idx_reqlog_ip ON request_log(ip);
|
|
69
|
+
CREATE INDEX IF NOT EXISTS idx_reqlog_path ON request_log(path);
|
|
70
|
+
|
|
71
|
+
-- Observability: security events
|
|
72
|
+
CREATE TABLE IF NOT EXISTS security_events (
|
|
73
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
74
|
+
ts TEXT DEFAULT (datetime('now')),
|
|
75
|
+
event_type TEXT NOT NULL,
|
|
76
|
+
ip TEXT,
|
|
77
|
+
detail TEXT,
|
|
78
|
+
severity TEXT DEFAULT 'info'
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
CREATE INDEX IF NOT EXISTS idx_secevents_ts ON security_events(ts);
|
|
82
|
+
CREATE INDEX IF NOT EXISTS idx_secevents_type ON security_events(event_type);
|
|
83
|
+
CREATE INDEX IF NOT EXISTS idx_secevents_ip ON security_events(ip);
|
|
84
|
+
`);
|
|
85
|
+
|
|
86
|
+
// Prepared statements for hot-path logging (avoid re-parsing SQL)
|
|
87
|
+
export const logRequest = db.prepare(
|
|
88
|
+
'INSERT INTO request_log (ip, method, path, status, duration_ms, user_agent, user_id, bytes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
|
|
89
|
+
);
|
|
90
|
+
export const logSecurityEvent = db.prepare(
|
|
91
|
+
'INSERT INTO security_events (event_type, ip, detail, severity) VALUES (?, ?, ?, ?)'
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// Prune old logs (keep 30 days)
|
|
95
|
+
export function pruneOldLogs() {
|
|
96
|
+
db.prepare("DELETE FROM request_log WHERE ts < datetime('now', '-30 days')").run();
|
|
97
|
+
db.prepare("DELETE FROM security_events WHERE ts < datetime('now', '-90 days')").run();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export default db;
|
package/lib/epa.mjs
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EPA Fuel Economy API integration.
|
|
3
|
+
* Free, no API key required.
|
|
4
|
+
* Docs: https://www.fueleconomy.gov/feg/ws/
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const BASE = 'https://fueleconomy.gov/ws/rest/vehicle';
|
|
8
|
+
const FETCH_TIMEOUT = 10_000;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Create an AbortSignal that times out after the specified ms.
|
|
12
|
+
*/
|
|
13
|
+
function timeoutSignal(ms = FETCH_TIMEOUT) {
|
|
14
|
+
return AbortSignal.timeout(ms);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Parse a numeric string, returning null if not valid.
|
|
19
|
+
*/
|
|
20
|
+
function num(value) {
|
|
21
|
+
if (value === undefined || value === null || value === '') return null;
|
|
22
|
+
const n = Number(value);
|
|
23
|
+
return Number.isNaN(n) ? null : n;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Clean a string value, returning null for empty/missing.
|
|
28
|
+
*/
|
|
29
|
+
function clean(value) {
|
|
30
|
+
if (value === undefined || value === null) return null;
|
|
31
|
+
if (typeof value === 'string') {
|
|
32
|
+
const trimmed = value.trim();
|
|
33
|
+
return trimmed === '' ? null : trimmed;
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Build the empty/unavailable response object.
|
|
40
|
+
*/
|
|
41
|
+
function unavailable() {
|
|
42
|
+
return {
|
|
43
|
+
available: false,
|
|
44
|
+
city_mpg: null,
|
|
45
|
+
highway_mpg: null,
|
|
46
|
+
combined_mpg: null,
|
|
47
|
+
annual_fuel_cost: null,
|
|
48
|
+
co2_grams_per_mile: null,
|
|
49
|
+
fuel_type: null,
|
|
50
|
+
fuel_type2: null,
|
|
51
|
+
is_ev: false,
|
|
52
|
+
ev_range: null,
|
|
53
|
+
ev_charge_time_240v: null,
|
|
54
|
+
phev_combined: null,
|
|
55
|
+
cylinders: null,
|
|
56
|
+
displacement: null,
|
|
57
|
+
drive: null,
|
|
58
|
+
transmission: null,
|
|
59
|
+
vehicle_class: null,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Get fuel economy data for a vehicle.
|
|
65
|
+
* Two-step: first get menu options for the year/make/model, then fetch full data.
|
|
66
|
+
*
|
|
67
|
+
* @param {number|string} year - Model year
|
|
68
|
+
* @param {string} make - Make (e.g., "Toyota")
|
|
69
|
+
* @param {string} model - Model (e.g., "Camry")
|
|
70
|
+
* @returns {object} Fuel economy data
|
|
71
|
+
*/
|
|
72
|
+
export async function getFuelEconomy(year, make, model) {
|
|
73
|
+
try {
|
|
74
|
+
// Step 1: Get available options (trims/variants) for this vehicle
|
|
75
|
+
const menuParams = new URLSearchParams({
|
|
76
|
+
year: String(year),
|
|
77
|
+
make: String(make),
|
|
78
|
+
model: String(model),
|
|
79
|
+
});
|
|
80
|
+
const menuUrl = `${BASE}/menu/options?${menuParams}`;
|
|
81
|
+
|
|
82
|
+
const menuRes = await fetch(menuUrl, {
|
|
83
|
+
headers: { Accept: 'application/json' },
|
|
84
|
+
signal: timeoutSignal(),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
if (!menuRes.ok) {
|
|
88
|
+
console.error(`[epa] getFuelEconomy menu HTTP ${menuRes.status}`);
|
|
89
|
+
return unavailable();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const menuData = await menuRes.json();
|
|
93
|
+
|
|
94
|
+
// The API returns { menuItem: { value, text } } for single result
|
|
95
|
+
// or { menuItem: [{ value, text }, ...] } for multiple results
|
|
96
|
+
let items = menuData?.menuItem;
|
|
97
|
+
if (!items) {
|
|
98
|
+
return unavailable();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Normalize to array
|
|
102
|
+
if (!Array.isArray(items)) {
|
|
103
|
+
items = [items];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (items.length === 0) {
|
|
107
|
+
return unavailable();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Step 2: Get full data for the first option
|
|
111
|
+
const vehicleId = items[0].value;
|
|
112
|
+
if (!vehicleId) {
|
|
113
|
+
return unavailable();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const detailUrl = `${BASE}/${vehicleId}`;
|
|
117
|
+
const detailRes = await fetch(detailUrl, {
|
|
118
|
+
headers: { Accept: 'application/json' },
|
|
119
|
+
signal: timeoutSignal(),
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
if (!detailRes.ok) {
|
|
123
|
+
console.error(`[epa] getFuelEconomy detail HTTP ${detailRes.status}`);
|
|
124
|
+
return unavailable();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const v = await detailRes.json();
|
|
128
|
+
|
|
129
|
+
// Determine if this is an EV
|
|
130
|
+
const fuelType1 = clean(v.fuelType) || clean(v.fuelType1);
|
|
131
|
+
const fuelType2 = clean(v.fuelType2);
|
|
132
|
+
const isEv = fuelType1 === 'Electricity' && !fuelType2;
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
available: true,
|
|
136
|
+
city_mpg: num(v.city08) || num(v.cityA08),
|
|
137
|
+
highway_mpg: num(v.highway08) || num(v.highwayA08),
|
|
138
|
+
combined_mpg: num(v.comb08) || num(v.combA08),
|
|
139
|
+
annual_fuel_cost: num(v.fuelCost08) || num(v.fuelCostA08),
|
|
140
|
+
co2_grams_per_mile: num(v.co2TailpipeGpm) || num(v.co2TailpipeAGpm),
|
|
141
|
+
fuel_type: fuelType1,
|
|
142
|
+
fuel_type2: fuelType2,
|
|
143
|
+
is_ev: isEv,
|
|
144
|
+
ev_range: num(v.range) || num(v.rangeCity),
|
|
145
|
+
ev_charge_time_240v: num(v.charge240),
|
|
146
|
+
phev_combined: num(v.combE),
|
|
147
|
+
cylinders: num(v.cylinders),
|
|
148
|
+
displacement: num(v.displ),
|
|
149
|
+
drive: clean(v.drive),
|
|
150
|
+
transmission: clean(v.trany),
|
|
151
|
+
vehicle_class: clean(v.VClass),
|
|
152
|
+
};
|
|
153
|
+
} catch (err) {
|
|
154
|
+
console.error(`[epa] getFuelEconomy error:`, err.message);
|
|
155
|
+
return unavailable();
|
|
156
|
+
}
|
|
157
|
+
}
|