@zero-server/sdk 0.9.1 → 0.9.3

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.
Files changed (128) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +460 -443
  3. package/index.js +414 -412
  4. package/lib/app.js +1172 -1172
  5. package/lib/auth/authorize.js +399 -399
  6. package/lib/auth/enrollment.js +367 -367
  7. package/lib/auth/index.js +57 -57
  8. package/lib/auth/jwt.js +731 -731
  9. package/lib/auth/oauth.js +362 -362
  10. package/lib/auth/session.js +588 -588
  11. package/lib/auth/trustedDevice.js +409 -409
  12. package/lib/auth/twoFactor.js +1150 -1150
  13. package/lib/auth/webauthn.js +946 -946
  14. package/lib/body/index.js +14 -14
  15. package/lib/body/json.js +109 -109
  16. package/lib/body/multipart.js +440 -440
  17. package/lib/body/raw.js +71 -71
  18. package/lib/body/rawBuffer.js +160 -160
  19. package/lib/body/sendError.js +25 -25
  20. package/lib/body/text.js +75 -75
  21. package/lib/body/typeMatch.js +41 -41
  22. package/lib/body/urlencoded.js +235 -235
  23. package/lib/cli.js +845 -845
  24. package/lib/cluster.js +666 -666
  25. package/lib/debug.js +372 -372
  26. package/lib/env/index.js +465 -465
  27. package/lib/errors.js +683 -683
  28. package/lib/fetch/index.js +256 -256
  29. package/lib/grpc/balancer.js +378 -378
  30. package/lib/grpc/call.js +708 -708
  31. package/lib/grpc/client.js +764 -764
  32. package/lib/grpc/codec.js +1221 -1221
  33. package/lib/grpc/credentials.js +398 -398
  34. package/lib/grpc/frame.js +262 -262
  35. package/lib/grpc/health.js +287 -287
  36. package/lib/grpc/index.js +121 -121
  37. package/lib/grpc/metadata.js +461 -461
  38. package/lib/grpc/proto.js +821 -821
  39. package/lib/grpc/reflection.js +590 -590
  40. package/lib/grpc/server.js +445 -445
  41. package/lib/grpc/status.js +118 -118
  42. package/lib/grpc/watch.js +173 -173
  43. package/lib/http/index.js +10 -10
  44. package/lib/http/request.js +727 -727
  45. package/lib/http/response.js +799 -799
  46. package/lib/lifecycle.js +557 -557
  47. package/lib/middleware/compress.js +230 -230
  48. package/lib/middleware/cookieParser.js +237 -237
  49. package/lib/middleware/cors.js +93 -93
  50. package/lib/middleware/csrf.js +137 -137
  51. package/lib/middleware/errorHandler.js +101 -101
  52. package/lib/middleware/helmet.js +175 -175
  53. package/lib/middleware/index.js +19 -17
  54. package/lib/middleware/logger.js +74 -74
  55. package/lib/middleware/rateLimit.js +88 -88
  56. package/lib/middleware/requestId.js +53 -53
  57. package/lib/middleware/static.js +326 -326
  58. package/lib/middleware/timeout.js +71 -71
  59. package/lib/middleware/validator.js +255 -255
  60. package/lib/observe/health.js +326 -326
  61. package/lib/observe/index.js +50 -50
  62. package/lib/observe/logger.js +359 -359
  63. package/lib/observe/metrics.js +805 -805
  64. package/lib/observe/tracing.js +592 -592
  65. package/lib/orm/adapters/json.js +290 -290
  66. package/lib/orm/adapters/memory.js +764 -764
  67. package/lib/orm/adapters/mongo.js +764 -764
  68. package/lib/orm/adapters/mysql.js +933 -933
  69. package/lib/orm/adapters/postgres.js +1144 -1144
  70. package/lib/orm/adapters/redis.js +1534 -1534
  71. package/lib/orm/adapters/sql-base.js +212 -212
  72. package/lib/orm/adapters/sqlite.js +858 -858
  73. package/lib/orm/audit.js +649 -649
  74. package/lib/orm/cache.js +394 -394
  75. package/lib/orm/geo.js +387 -387
  76. package/lib/orm/index.js +784 -784
  77. package/lib/orm/migrate.js +432 -432
  78. package/lib/orm/model.js +1706 -1706
  79. package/lib/orm/plugin.js +375 -375
  80. package/lib/orm/procedures.js +836 -836
  81. package/lib/orm/profiler.js +233 -233
  82. package/lib/orm/query.js +1772 -1772
  83. package/lib/orm/replicas.js +241 -241
  84. package/lib/orm/schema.js +307 -307
  85. package/lib/orm/search.js +380 -380
  86. package/lib/orm/seed/data/commerce.js +136 -136
  87. package/lib/orm/seed/data/internet.js +111 -111
  88. package/lib/orm/seed/data/locations.js +204 -204
  89. package/lib/orm/seed/data/names.js +338 -338
  90. package/lib/orm/seed/data/person.js +128 -128
  91. package/lib/orm/seed/data/phone.js +211 -211
  92. package/lib/orm/seed/data/words.js +134 -134
  93. package/lib/orm/seed/factory.js +178 -178
  94. package/lib/orm/seed/fake.js +1186 -1186
  95. package/lib/orm/seed/index.js +18 -18
  96. package/lib/orm/seed/rng.js +70 -70
  97. package/lib/orm/seed/seeder.js +124 -124
  98. package/lib/orm/seed/unique.js +68 -68
  99. package/lib/orm/snapshot.js +366 -366
  100. package/lib/orm/tenancy.js +605 -605
  101. package/lib/orm/views.js +350 -350
  102. package/lib/router/index.js +436 -436
  103. package/lib/sse/index.js +8 -8
  104. package/lib/sse/stream.js +349 -349
  105. package/lib/ws/connection.js +451 -451
  106. package/lib/ws/handshake.js +125 -125
  107. package/lib/ws/index.js +14 -14
  108. package/lib/ws/room.js +223 -223
  109. package/package.json +73 -73
  110. package/types/app.d.ts +223 -223
  111. package/types/auth.d.ts +520 -520
  112. package/types/body.d.ts +14 -0
  113. package/types/cli.d.ts +2 -0
  114. package/types/cluster.d.ts +75 -75
  115. package/types/env.d.ts +80 -80
  116. package/types/errors.d.ts +316 -316
  117. package/types/fetch.d.ts +43 -43
  118. package/types/grpc.d.ts +432 -432
  119. package/types/index.d.ts +384 -384
  120. package/types/lifecycle.d.ts +60 -60
  121. package/types/middleware.d.ts +320 -320
  122. package/types/observe.d.ts +304 -304
  123. package/types/orm.d.ts +1887 -1887
  124. package/types/request.d.ts +109 -109
  125. package/types/response.d.ts +157 -157
  126. package/types/router.d.ts +78 -78
  127. package/types/sse.d.ts +78 -78
  128. package/types/websocket.d.ts +126 -126
@@ -1,233 +1,233 @@
1
- /**
2
- * @module orm/profiler
3
- * @description Query profiling, slow query detection, and automatic N+1 detection.
4
- * Attach to a Database instance via `db.enableProfiling()`.
5
- *
6
- * @example
7
- * const { Database, QueryProfiler } = require('@zero-server/sdk');
8
- *
9
- * const db = Database.connect('memory');
10
- * const profiler = db.enableProfiling({ slowThreshold: 100 });
11
- *
12
- * // ... run queries ...
13
- *
14
- * console.log(profiler.metrics());
15
- * console.log(profiler.slowQueries());
16
- * console.log(profiler.n1Detections());
17
- */
18
-
19
- const log = require('../debug')('zero:profiler');
20
-
21
- class QueryProfiler
22
- {
23
- /**
24
- * @constructor
25
- * @param {object} [options] - Configuration options.
26
- * @param {boolean} [options.enabled=true] - Enable/disable profiling.
27
- * @param {number} [options.slowThreshold=100] - Duration (ms) above which a query is "slow".
28
- * @param {number} [options.maxHistory=1000] - Maximum recorded query entries.
29
- * @param {Function} [options.onSlow] - Callback on slow query: (entry) => {}.
30
- * @param {number} [options.n1Threshold=5] - Minimum rapid same-table SELECTs to flag N+1.
31
- * @param {number} [options.n1Window=100] - Time window (ms) for N+1 detection.
32
- * @param {Function} [options.onN1] - Callback on N+1 detection: (info) => {}.
33
- */
34
- constructor(options = {})
35
- {
36
- this._enabled = options.enabled !== false;
37
- this._slowThreshold = Math.max(0, options.slowThreshold != null ? Number(options.slowThreshold) : 100);
38
- this._maxHistory = Math.max(1, Math.floor(Number(options.maxHistory) || 1000));
39
- this._onSlow = typeof options.onSlow === 'function' ? options.onSlow : null;
40
-
41
- // N+1 detection
42
- this._n1Threshold = Math.max(2, Math.floor(Number(options.n1Threshold) || 5));
43
- this._n1Window = Math.max(10, Number(options.n1Window) || 100);
44
- this._onN1 = typeof options.onN1 === 'function' ? options.onN1 : null;
45
- this._maxN1History = Math.max(1, Math.floor(Number(options.maxN1History) || 100));
46
- this._n1Detected = [];
47
-
48
- // Per-table SELECT counters for O(1) N+1 detection
49
- /** @private Map<table, { count, firstTs }> */
50
- this._selectCounters = new Map();
51
-
52
- // Query history
53
- this._queries = [];
54
-
55
- // Aggregate stats
56
- this._totalQueries = 0;
57
- this._totalTime = 0;
58
- this._slowCount = 0;
59
- this._startTime = Date.now();
60
- }
61
-
62
- /**
63
- * Record a query execution.
64
- *
65
- * @param {object} entry - Profiler entry object.
66
- * @param {string} entry.table - Table name.
67
- * @param {string} entry.action - Query action (select, insert, update, delete, count).
68
- * @param {number} entry.duration - Execution time in milliseconds.
69
- */
70
- record(entry)
71
- {
72
- if (!this._enabled) return;
73
-
74
- // Sanitize
75
- const record = {
76
- table: String(entry.table || ''),
77
- action: String(entry.action || 'unknown'),
78
- duration: Number(entry.duration) || 0,
79
- timestamp: Date.now(),
80
- };
81
-
82
- this._totalQueries++;
83
- this._totalTime += record.duration;
84
-
85
- // Enforce history limit (evict oldest)
86
- if (this._queries.length >= this._maxHistory)
87
- {
88
- this._queries.shift();
89
- }
90
- this._queries.push(record);
91
-
92
- // Slow query detection
93
- if (record.duration > this._slowThreshold)
94
- {
95
- this._slowCount++;
96
- log.warn('Slow query: %s %s (%dms)', record.action, record.table, Math.round(record.duration));
97
- if (this._onSlow) this._onSlow(record);
98
- }
99
-
100
- // N+1 detection (only for SELECTs)
101
- this._detectN1(record.table, record.action);
102
- }
103
-
104
- /**
105
- * Detect potential N+1 query patterns.
106
- * Uses per-table sliding window counters for O(1) detection instead of scanning history.
107
- * @param {string} table - Table involved.
108
- * @param {string} action - CRUD action performed.
109
- * @private
110
- */
111
- _detectN1(table, action)
112
- {
113
- if (action !== 'select') return;
114
-
115
- const now = Date.now();
116
- let counter = this._selectCounters.get(table);
117
-
118
- if (!counter || (now - counter.firstTs) >= this._n1Window)
119
- {
120
- // Window expired or first query — start a new window
121
- counter = { count: 1, firstTs: now };
122
- this._selectCounters.set(table, counter);
123
- return;
124
- }
125
-
126
- counter.count++;
127
-
128
- if (counter.count >= this._n1Threshold)
129
- {
130
- // Prevent duplicate detection for same burst
131
- const last = this._n1Detected.find(d => d.table === table);
132
- if (last && (now - last.timestamp) < this._n1Window) return;
133
-
134
- const detection = {
135
- table,
136
- count: counter.count,
137
- timestamp: now,
138
- message: `Potential N+1: ${counter.count} SELECT queries to "${table}" in ${this._n1Window}ms`,
139
- };
140
- // Cap N+1 detection history to prevent memory exhaustion
141
- if (this._n1Detected.length >= this._maxN1History)
142
- {
143
- this._n1Detected.shift();
144
- }
145
- this._n1Detected.push(detection);
146
- log.warn(detection.message);
147
- if (this._onN1) this._onN1(detection);
148
- }
149
- }
150
-
151
- /**
152
- * Get aggregate profiling metrics.
153
- *
154
- * @returns {{
155
- * totalQueries: number,
156
- * totalTime: number,
157
- * avgLatency: number,
158
- * queriesPerSecond: number,
159
- * slowQueries: number,
160
- * n1Detections: number,
161
- * }}
162
- */
163
- metrics()
164
- {
165
- const elapsed = (Date.now() - this._startTime) / 1000 || 1;
166
- return {
167
- totalQueries: this._totalQueries,
168
- totalTime: Math.round(this._totalTime * 100) / 100,
169
- avgLatency: this._totalQueries > 0
170
- ? Math.round((this._totalTime / this._totalQueries) * 100) / 100
171
- : 0,
172
- queriesPerSecond: Math.round((this._totalQueries / elapsed) * 100) / 100,
173
- slowQueries: this._slowCount,
174
- n1Detections: this._n1Detected.length,
175
- };
176
- }
177
-
178
- /**
179
- * Get all slow queries from history.
180
- * @returns {Array<{ table: string, action: string, duration: number, timestamp: number }>}
181
- */
182
- slowQueries()
183
- {
184
- return this._queries.filter(q => q.duration > this._slowThreshold);
185
- }
186
-
187
- /**
188
- * Get all N+1 detections.
189
- * @returns {Array<{ table: string, count: number, timestamp: number, message: string }>}
190
- */
191
- n1Detections()
192
- {
193
- return [...this._n1Detected];
194
- }
195
-
196
- /**
197
- * Get filtered query history.
198
- *
199
- * @param {object} [options] - Configuration options.
200
- * @param {string} [options.table] - Filter by table name.
201
- * @param {string} [options.action] - Filter by action type.
202
- * @param {number} [options.minDuration] - Minimum duration filter.
203
- * @returns {Array<{ table: string, action: string, duration: number, timestamp: number }>} Filtered query history entries.
204
- */
205
- getQueries(options = {})
206
- {
207
- let results = [...this._queries];
208
- if (options.table) results = results.filter(q => q.table === options.table);
209
- if (options.action) results = results.filter(q => q.action === options.action);
210
- if (options.minDuration !== undefined) results = results.filter(q => q.duration >= options.minDuration);
211
- return results;
212
- }
213
-
214
- /**
215
- * Reset all profiling state.
216
- */
217
- reset()
218
- {
219
- this._queries = [];
220
- this._totalQueries = 0;
221
- this._totalTime = 0;
222
- this._slowCount = 0;
223
- this._startTime = Date.now();
224
- this._n1Detected = [];
225
- this._selectCounters.clear();
226
- }
227
-
228
- /** @type {boolean} */
229
- get enabled() { return this._enabled; }
230
- set enabled(val) { this._enabled = !!val; }
231
- }
232
-
233
- module.exports = { QueryProfiler };
1
+ /**
2
+ * @module orm/profiler
3
+ * @description Query profiling, slow query detection, and automatic N+1 detection.
4
+ * Attach to a Database instance via `db.enableProfiling()`.
5
+ *
6
+ * @example
7
+ * const { Database, QueryProfiler } = require('@zero-server/sdk');
8
+ *
9
+ * const db = Database.connect('memory');
10
+ * const profiler = db.enableProfiling({ slowThreshold: 100 });
11
+ *
12
+ * // ... run queries ...
13
+ *
14
+ * console.log(profiler.metrics());
15
+ * console.log(profiler.slowQueries());
16
+ * console.log(profiler.n1Detections());
17
+ */
18
+
19
+ const log = require('../debug')('zero:profiler');
20
+
21
+ class QueryProfiler
22
+ {
23
+ /**
24
+ * @constructor
25
+ * @param {object} [options] - Configuration options.
26
+ * @param {boolean} [options.enabled=true] - Enable/disable profiling.
27
+ * @param {number} [options.slowThreshold=100] - Duration (ms) above which a query is "slow".
28
+ * @param {number} [options.maxHistory=1000] - Maximum recorded query entries.
29
+ * @param {Function} [options.onSlow] - Callback on slow query: (entry) => {}.
30
+ * @param {number} [options.n1Threshold=5] - Minimum rapid same-table SELECTs to flag N+1.
31
+ * @param {number} [options.n1Window=100] - Time window (ms) for N+1 detection.
32
+ * @param {Function} [options.onN1] - Callback on N+1 detection: (info) => {}.
33
+ */
34
+ constructor(options = {})
35
+ {
36
+ this._enabled = options.enabled !== false;
37
+ this._slowThreshold = Math.max(0, options.slowThreshold != null ? Number(options.slowThreshold) : 100);
38
+ this._maxHistory = Math.max(1, Math.floor(Number(options.maxHistory) || 1000));
39
+ this._onSlow = typeof options.onSlow === 'function' ? options.onSlow : null;
40
+
41
+ // N+1 detection
42
+ this._n1Threshold = Math.max(2, Math.floor(Number(options.n1Threshold) || 5));
43
+ this._n1Window = Math.max(10, Number(options.n1Window) || 100);
44
+ this._onN1 = typeof options.onN1 === 'function' ? options.onN1 : null;
45
+ this._maxN1History = Math.max(1, Math.floor(Number(options.maxN1History) || 100));
46
+ this._n1Detected = [];
47
+
48
+ // Per-table SELECT counters for O(1) N+1 detection
49
+ /** @private Map<table, { count, firstTs }> */
50
+ this._selectCounters = new Map();
51
+
52
+ // Query history
53
+ this._queries = [];
54
+
55
+ // Aggregate stats
56
+ this._totalQueries = 0;
57
+ this._totalTime = 0;
58
+ this._slowCount = 0;
59
+ this._startTime = Date.now();
60
+ }
61
+
62
+ /**
63
+ * Record a query execution.
64
+ *
65
+ * @param {object} entry - Profiler entry object.
66
+ * @param {string} entry.table - Table name.
67
+ * @param {string} entry.action - Query action (select, insert, update, delete, count).
68
+ * @param {number} entry.duration - Execution time in milliseconds.
69
+ */
70
+ record(entry)
71
+ {
72
+ if (!this._enabled) return;
73
+
74
+ // Sanitize
75
+ const record = {
76
+ table: String(entry.table || ''),
77
+ action: String(entry.action || 'unknown'),
78
+ duration: Number(entry.duration) || 0,
79
+ timestamp: Date.now(),
80
+ };
81
+
82
+ this._totalQueries++;
83
+ this._totalTime += record.duration;
84
+
85
+ // Enforce history limit (evict oldest)
86
+ if (this._queries.length >= this._maxHistory)
87
+ {
88
+ this._queries.shift();
89
+ }
90
+ this._queries.push(record);
91
+
92
+ // Slow query detection
93
+ if (record.duration > this._slowThreshold)
94
+ {
95
+ this._slowCount++;
96
+ log.warn('Slow query: %s %s (%dms)', record.action, record.table, Math.round(record.duration));
97
+ if (this._onSlow) this._onSlow(record);
98
+ }
99
+
100
+ // N+1 detection (only for SELECTs)
101
+ this._detectN1(record.table, record.action);
102
+ }
103
+
104
+ /**
105
+ * Detect potential N+1 query patterns.
106
+ * Uses per-table sliding window counters for O(1) detection instead of scanning history.
107
+ * @param {string} table - Table involved.
108
+ * @param {string} action - CRUD action performed.
109
+ * @private
110
+ */
111
+ _detectN1(table, action)
112
+ {
113
+ if (action !== 'select') return;
114
+
115
+ const now = Date.now();
116
+ let counter = this._selectCounters.get(table);
117
+
118
+ if (!counter || (now - counter.firstTs) >= this._n1Window)
119
+ {
120
+ // Window expired or first query — start a new window
121
+ counter = { count: 1, firstTs: now };
122
+ this._selectCounters.set(table, counter);
123
+ return;
124
+ }
125
+
126
+ counter.count++;
127
+
128
+ if (counter.count >= this._n1Threshold)
129
+ {
130
+ // Prevent duplicate detection for same burst
131
+ const last = this._n1Detected.find(d => d.table === table);
132
+ if (last && (now - last.timestamp) < this._n1Window) return;
133
+
134
+ const detection = {
135
+ table,
136
+ count: counter.count,
137
+ timestamp: now,
138
+ message: `Potential N+1: ${counter.count} SELECT queries to "${table}" in ${this._n1Window}ms`,
139
+ };
140
+ // Cap N+1 detection history to prevent memory exhaustion
141
+ if (this._n1Detected.length >= this._maxN1History)
142
+ {
143
+ this._n1Detected.shift();
144
+ }
145
+ this._n1Detected.push(detection);
146
+ log.warn(detection.message);
147
+ if (this._onN1) this._onN1(detection);
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Get aggregate profiling metrics.
153
+ *
154
+ * @returns {{
155
+ * totalQueries: number,
156
+ * totalTime: number,
157
+ * avgLatency: number,
158
+ * queriesPerSecond: number,
159
+ * slowQueries: number,
160
+ * n1Detections: number,
161
+ * }}
162
+ */
163
+ metrics()
164
+ {
165
+ const elapsed = (Date.now() - this._startTime) / 1000 || 1;
166
+ return {
167
+ totalQueries: this._totalQueries,
168
+ totalTime: Math.round(this._totalTime * 100) / 100,
169
+ avgLatency: this._totalQueries > 0
170
+ ? Math.round((this._totalTime / this._totalQueries) * 100) / 100
171
+ : 0,
172
+ queriesPerSecond: Math.round((this._totalQueries / elapsed) * 100) / 100,
173
+ slowQueries: this._slowCount,
174
+ n1Detections: this._n1Detected.length,
175
+ };
176
+ }
177
+
178
+ /**
179
+ * Get all slow queries from history.
180
+ * @returns {Array<{ table: string, action: string, duration: number, timestamp: number }>}
181
+ */
182
+ slowQueries()
183
+ {
184
+ return this._queries.filter(q => q.duration > this._slowThreshold);
185
+ }
186
+
187
+ /**
188
+ * Get all N+1 detections.
189
+ * @returns {Array<{ table: string, count: number, timestamp: number, message: string }>}
190
+ */
191
+ n1Detections()
192
+ {
193
+ return [...this._n1Detected];
194
+ }
195
+
196
+ /**
197
+ * Get filtered query history.
198
+ *
199
+ * @param {object} [options] - Configuration options.
200
+ * @param {string} [options.table] - Filter by table name.
201
+ * @param {string} [options.action] - Filter by action type.
202
+ * @param {number} [options.minDuration] - Minimum duration filter.
203
+ * @returns {Array<{ table: string, action: string, duration: number, timestamp: number }>} Filtered query history entries.
204
+ */
205
+ getQueries(options = {})
206
+ {
207
+ let results = [...this._queries];
208
+ if (options.table) results = results.filter(q => q.table === options.table);
209
+ if (options.action) results = results.filter(q => q.action === options.action);
210
+ if (options.minDuration !== undefined) results = results.filter(q => q.duration >= options.minDuration);
211
+ return results;
212
+ }
213
+
214
+ /**
215
+ * Reset all profiling state.
216
+ */
217
+ reset()
218
+ {
219
+ this._queries = [];
220
+ this._totalQueries = 0;
221
+ this._totalTime = 0;
222
+ this._slowCount = 0;
223
+ this._startTime = Date.now();
224
+ this._n1Detected = [];
225
+ this._selectCounters.clear();
226
+ }
227
+
228
+ /** @type {boolean} */
229
+ get enabled() { return this._enabled; }
230
+ set enabled(val) { this._enabled = !!val; }
231
+ }
232
+
233
+ module.exports = { QueryProfiler };