@joint-ops/hitlimit-bun 1.0.1 → 1.0.2

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.
@@ -0,0 +1,6 @@
1
+ import type { HitLimitStore } from '@joint-ops/hitlimit-types';
2
+ export interface SqliteStoreOptions {
3
+ path?: string;
4
+ }
5
+ export declare function sqliteStore(options?: SqliteStoreOptions): HitLimitStore;
6
+ //# sourceMappingURL=sqlite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../../src/stores/sqlite.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAe,MAAM,2BAA2B,CAAA;AAE3E,MAAM,WAAW,kBAAkB;IACjC,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAuDD,wBAAgB,WAAW,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,aAAa,CAEvE"}
@@ -0,0 +1,81 @@
1
+ // @bun
2
+ var __create = Object.create;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __toESM = (mod, isNodeMode, target) => {
8
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
9
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
10
+ for (let key of __getOwnPropNames(mod))
11
+ if (!__hasOwnProp.call(to, key))
12
+ __defProp(to, key, {
13
+ get: () => mod[key],
14
+ enumerable: true
15
+ });
16
+ return to;
17
+ };
18
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
19
+ var __export = (target, all) => {
20
+ for (var name in all)
21
+ __defProp(target, name, {
22
+ get: all[name],
23
+ enumerable: true,
24
+ configurable: true,
25
+ set: (newValue) => all[name] = () => newValue
26
+ });
27
+ };
28
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
29
+ var __require = import.meta.require;
30
+
31
+ // src/stores/sqlite.ts
32
+ import { Database } from "bun:sqlite";
33
+
34
+ class BunSqliteStore {
35
+ db;
36
+ hitStmt;
37
+ getStmt;
38
+ resetStmt;
39
+ cleanupTimer;
40
+ constructor(options = {}) {
41
+ this.db = new Database(options.path ?? ":memory:");
42
+ this.db.exec(`
43
+ CREATE TABLE IF NOT EXISTS hitlimit (
44
+ key TEXT PRIMARY KEY,
45
+ count INTEGER NOT NULL,
46
+ reset_at INTEGER NOT NULL
47
+ )
48
+ `);
49
+ this.hitStmt = this.db.prepare(`
50
+ INSERT INTO hitlimit (key, count, reset_at) VALUES (?1, 1, ?2)
51
+ ON CONFLICT(key) DO UPDATE SET
52
+ count = CASE WHEN reset_at <= ?3 THEN 1 ELSE count + 1 END,
53
+ reset_at = CASE WHEN reset_at <= ?3 THEN ?2 ELSE reset_at END
54
+ `);
55
+ this.getStmt = this.db.prepare("SELECT count, reset_at FROM hitlimit WHERE key = ?");
56
+ this.resetStmt = this.db.prepare("DELETE FROM hitlimit WHERE key = ?");
57
+ this.cleanupTimer = setInterval(() => {
58
+ this.db.prepare("DELETE FROM hitlimit WHERE reset_at <= ?").run(Date.now());
59
+ }, 60000);
60
+ }
61
+ hit(key, windowMs, _limit) {
62
+ const now = Date.now();
63
+ const resetAt = now + windowMs;
64
+ this.hitStmt.run(key, resetAt, now);
65
+ const row = this.getStmt.get(key);
66
+ return { count: row.count, resetAt: row.reset_at };
67
+ }
68
+ reset(key) {
69
+ this.resetStmt.run(key);
70
+ }
71
+ shutdown() {
72
+ clearInterval(this.cleanupTimer);
73
+ this.db.close();
74
+ }
75
+ }
76
+ function sqliteStore(options) {
77
+ return new BunSqliteStore(options);
78
+ }
79
+ export {
80
+ sqliteStore
81
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@joint-ops/hitlimit-bun",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Fast Bun-native rate limiting for Bun.serve & Elysia - API throttling with bun:sqlite, high performance request limiting",
5
5
  "author": {
6
6
  "name": "Shayan M Hussain",
@@ -93,6 +93,11 @@
93
93
  "types": "./dist/stores/redis.d.ts",
94
94
  "bun": "./dist/stores/redis.js",
95
95
  "import": "./dist/stores/redis.js"
96
+ },
97
+ "./stores/sqlite": {
98
+ "types": "./dist/stores/sqlite.d.ts",
99
+ "bun": "./dist/stores/sqlite.js",
100
+ "import": "./dist/stores/sqlite.js"
96
101
  }
97
102
  },
98
103
  "files": [
@@ -100,13 +105,13 @@
100
105
  ],
101
106
  "sideEffects": false,
102
107
  "scripts": {
103
- "build": "bun build ./src/index.ts ./src/elysia.ts ./src/stores/memory.ts ./src/stores/redis.ts --outdir=./dist --target=bun && tsc --emitDeclarationOnly",
108
+ "build": "bun build ./src/index.ts ./src/elysia.ts ./src/stores/memory.ts ./src/stores/redis.ts ./src/stores/sqlite.ts --outdir=./dist --target=bun && tsc --emitDeclarationOnly",
104
109
  "clean": "rm -rf dist",
105
110
  "test": "bun test",
106
111
  "test:watch": "bun test --watch"
107
112
  },
108
113
  "dependencies": {
109
- "@joint-ops/hitlimit-types": "1.0.1"
114
+ "@joint-ops/hitlimit-types": "1.0.2"
110
115
  },
111
116
  "peerDependencies": {
112
117
  "elysia": ">=1.0.0",