odesli.js 1.1.2 → 1.3.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.
- package/.github/workflows/ci.yml +0 -8
- package/.github/workflows/publish-github-packages.yml +8 -12
- package/.github/workflows/publish-npm.yml +1 -1
- package/API_IMPROVEMENTS.md +318 -0
- package/CHANGELOG.md +83 -56
- package/README.md +201 -156
- package/examples/README.md +197 -26
- package/examples/advanced-features-example.js +205 -0
- package/examples/advanced-usage.js +217 -36
- package/examples/basic-usage.js +92 -17
- package/examples/cache-test.js +119 -0
- package/examples/test-apple-music-extraction.js +60 -0
- package/examples/user-agent-example.js +46 -0
- package/jest.config.js +3 -1
- package/lib/index.d.ts +476 -297
- package/lib/index.js +1041 -157
- package/lib/index.mjs +3 -0
- package/lib/metrics.js +390 -0
- package/lib/metrics.mjs +3 -0
- package/lib/plugin-system.js +382 -0
- package/lib/plugin-system.mjs +3 -0
- package/lib/rate-limiter.js +148 -0
- package/lib/rate-limiter.mjs +3 -0
- package/package.json +2 -1
- package/tests/advanced-features.test.js +423 -0
- package/tests/odesli.test.js +246 -92
- package/tests/setup.js +2 -0
package/lib/index.mjs
ADDED
package/lib/metrics.js
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metrics Collection System for Odesli API
|
|
3
|
+
* Tracks performance, usage, and error metrics
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
class MetricsCollector {
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
this.enabled = options.enabled !== false
|
|
9
|
+
this.retentionMs = options.retentionMs || 24 * 60 * 60 * 1000 // 24 hours
|
|
10
|
+
this.maxDataPoints = options.maxDataPoints || 10000
|
|
11
|
+
|
|
12
|
+
// Metrics storage
|
|
13
|
+
this.metrics = {
|
|
14
|
+
requests: [],
|
|
15
|
+
errors: [],
|
|
16
|
+
cache: {
|
|
17
|
+
hits: 0,
|
|
18
|
+
misses: 0,
|
|
19
|
+
size: 0
|
|
20
|
+
},
|
|
21
|
+
performance: {
|
|
22
|
+
responseTimes: [],
|
|
23
|
+
throughput: []
|
|
24
|
+
},
|
|
25
|
+
rateLimits: {
|
|
26
|
+
hits: 0,
|
|
27
|
+
delays: []
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Real-time counters
|
|
32
|
+
this.counters = {
|
|
33
|
+
totalRequests: 0,
|
|
34
|
+
successfulRequests: 0,
|
|
35
|
+
failedRequests: 0,
|
|
36
|
+
cacheHits: 0,
|
|
37
|
+
cacheMisses: 0,
|
|
38
|
+
rateLimitHits: 0
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Cleanup interval
|
|
42
|
+
if (this.enabled) {
|
|
43
|
+
const interval = setInterval(() => this.cleanup(), 60000); // Clean up every minute
|
|
44
|
+
interval.unref();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Record a request
|
|
50
|
+
*/
|
|
51
|
+
recordRequest(options = {}) {
|
|
52
|
+
if (!this.enabled) return
|
|
53
|
+
|
|
54
|
+
const {
|
|
55
|
+
url,
|
|
56
|
+
method = 'GET',
|
|
57
|
+
startTime = Date.now(),
|
|
58
|
+
endTime,
|
|
59
|
+
success = true,
|
|
60
|
+
statusCode,
|
|
61
|
+
error,
|
|
62
|
+
responseTime,
|
|
63
|
+
platform,
|
|
64
|
+
country,
|
|
65
|
+
cacheHit = false
|
|
66
|
+
} = options
|
|
67
|
+
|
|
68
|
+
const request = {
|
|
69
|
+
timestamp: startTime,
|
|
70
|
+
url,
|
|
71
|
+
method,
|
|
72
|
+
success,
|
|
73
|
+
statusCode,
|
|
74
|
+
error: error?.message,
|
|
75
|
+
responseTime: endTime ? endTime - startTime : responseTime,
|
|
76
|
+
platform,
|
|
77
|
+
country,
|
|
78
|
+
cacheHit
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this.metrics.requests.push(request)
|
|
82
|
+
this.counters.totalRequests++
|
|
83
|
+
|
|
84
|
+
if (success) {
|
|
85
|
+
this.counters.successfulRequests++
|
|
86
|
+
} else {
|
|
87
|
+
this.counters.failedRequests++
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (cacheHit) {
|
|
91
|
+
this.counters.cacheHits++
|
|
92
|
+
this.metrics.cache.hits++
|
|
93
|
+
} else {
|
|
94
|
+
this.counters.cacheMisses++
|
|
95
|
+
this.metrics.cache.misses++
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Record performance metrics
|
|
99
|
+
if (request.responseTime) {
|
|
100
|
+
this.metrics.performance.responseTimes.push({
|
|
101
|
+
timestamp: startTime,
|
|
102
|
+
responseTime: request.responseTime
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Record an error
|
|
109
|
+
*/
|
|
110
|
+
recordError(error, context = {}) {
|
|
111
|
+
if (!this.enabled) return
|
|
112
|
+
|
|
113
|
+
const errorRecord = {
|
|
114
|
+
timestamp: Date.now(),
|
|
115
|
+
message: error.message,
|
|
116
|
+
stack: error.stack,
|
|
117
|
+
type: error.constructor.name,
|
|
118
|
+
...context
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
this.metrics.errors.push(errorRecord)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Record rate limit hit
|
|
126
|
+
*/
|
|
127
|
+
recordRateLimit(delayMs) {
|
|
128
|
+
if (!this.enabled) return
|
|
129
|
+
|
|
130
|
+
this.counters.rateLimitHits++
|
|
131
|
+
this.metrics.rateLimits.hits++
|
|
132
|
+
this.metrics.rateLimits.delays.push({
|
|
133
|
+
timestamp: Date.now(),
|
|
134
|
+
delayMs
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Update cache metrics
|
|
140
|
+
*/
|
|
141
|
+
updateCacheMetrics(size) {
|
|
142
|
+
if (!this.enabled) return
|
|
143
|
+
|
|
144
|
+
this.metrics.cache.size = size
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Get current metrics summary
|
|
149
|
+
*/
|
|
150
|
+
getSummary() {
|
|
151
|
+
const now = Date.now()
|
|
152
|
+
const windowMs = 60 * 60 * 1000 // 1 hour window
|
|
153
|
+
const recentRequests = this.metrics.requests.filter(
|
|
154
|
+
req => now - req.timestamp < windowMs
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
const recentErrors = this.metrics.errors.filter(
|
|
158
|
+
err => now - err.timestamp < windowMs
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
const avgResponseTime = recentRequests.length > 0
|
|
162
|
+
? recentRequests.reduce((sum, req) => sum + (req.responseTime || 0), 0) / recentRequests.length
|
|
163
|
+
: 0
|
|
164
|
+
|
|
165
|
+
const successRate = this.counters.totalRequests > 0
|
|
166
|
+
? this.counters.successfulRequests / this.counters.totalRequests
|
|
167
|
+
: 1
|
|
168
|
+
|
|
169
|
+
const cacheHitRate = (this.counters.cacheHits + this.counters.cacheMisses) > 0
|
|
170
|
+
? this.counters.cacheHits / (this.counters.cacheHits + this.counters.cacheMisses)
|
|
171
|
+
: 0
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
counters: { ...this.counters },
|
|
175
|
+
recent: {
|
|
176
|
+
requests: recentRequests.length,
|
|
177
|
+
errors: recentErrors.length,
|
|
178
|
+
avgResponseTime: Math.round(avgResponseTime),
|
|
179
|
+
requestsPerMinute: Math.round(recentRequests.length / 60)
|
|
180
|
+
},
|
|
181
|
+
rates: {
|
|
182
|
+
successRate: Math.round(successRate * 100) / 100,
|
|
183
|
+
cacheHitRate: Math.round(cacheHitRate * 100) / 100,
|
|
184
|
+
errorRate: Math.round((1 - successRate) * 100) / 100
|
|
185
|
+
},
|
|
186
|
+
cache: { ...this.metrics.cache },
|
|
187
|
+
rateLimits: {
|
|
188
|
+
hits: this.counters.rateLimitHits,
|
|
189
|
+
avgDelay: this.metrics.rateLimits.delays.length > 0
|
|
190
|
+
? Math.round(this.metrics.rateLimits.delays.reduce((sum, d) => sum + d.delayMs, 0) / this.metrics.rateLimits.delays.length)
|
|
191
|
+
: 0
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Get detailed metrics for analysis
|
|
198
|
+
*/
|
|
199
|
+
getDetailedMetrics(options = {}) {
|
|
200
|
+
const {
|
|
201
|
+
startTime = Date.now() - this.retentionMs,
|
|
202
|
+
endTime = Date.now(),
|
|
203
|
+
groupBy = 'hour' // hour, minute, platform, country
|
|
204
|
+
} = options
|
|
205
|
+
|
|
206
|
+
const filteredRequests = this.metrics.requests.filter(
|
|
207
|
+
req => req.timestamp >= startTime && req.timestamp <= endTime
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
switch (groupBy) {
|
|
211
|
+
case 'hour':
|
|
212
|
+
return this.groupByHour(filteredRequests)
|
|
213
|
+
case 'minute':
|
|
214
|
+
return this.groupByMinute(filteredRequests)
|
|
215
|
+
case 'platform':
|
|
216
|
+
return this.groupByPlatform(filteredRequests)
|
|
217
|
+
case 'country':
|
|
218
|
+
return this.groupByCountry(filteredRequests)
|
|
219
|
+
default:
|
|
220
|
+
return filteredRequests
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Group requests by hour
|
|
226
|
+
*/
|
|
227
|
+
groupByHour(requests) {
|
|
228
|
+
const groups = {}
|
|
229
|
+
|
|
230
|
+
requests.forEach(req => {
|
|
231
|
+
const hour = new Date(req.timestamp).toISOString().slice(0, 13) + ':00:00.000Z'
|
|
232
|
+
if (!groups[hour]) {
|
|
233
|
+
groups[hour] = {
|
|
234
|
+
requests: 0,
|
|
235
|
+
errors: 0,
|
|
236
|
+
avgResponseTime: 0,
|
|
237
|
+
totalResponseTime: 0
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
groups[hour].requests++
|
|
242
|
+
if (!req.success) groups[hour].errors++
|
|
243
|
+
if (req.responseTime) {
|
|
244
|
+
groups[hour].totalResponseTime += req.responseTime
|
|
245
|
+
groups[hour].avgResponseTime = groups[hour].totalResponseTime / groups[hour].requests
|
|
246
|
+
}
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
return groups
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Group requests by platform
|
|
254
|
+
*/
|
|
255
|
+
groupByPlatform(requests) {
|
|
256
|
+
const groups = {}
|
|
257
|
+
|
|
258
|
+
requests.forEach(req => {
|
|
259
|
+
const platform = req.platform || 'unknown'
|
|
260
|
+
if (!groups[platform]) {
|
|
261
|
+
groups[platform] = {
|
|
262
|
+
requests: 0,
|
|
263
|
+
errors: 0,
|
|
264
|
+
avgResponseTime: 0,
|
|
265
|
+
totalResponseTime: 0
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
groups[platform].requests++
|
|
270
|
+
if (!req.success) groups[platform].errors++
|
|
271
|
+
if (req.responseTime) {
|
|
272
|
+
groups[platform].totalResponseTime += req.responseTime
|
|
273
|
+
groups[platform].avgResponseTime = groups[platform].totalResponseTime / groups[platform].requests
|
|
274
|
+
}
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
return groups
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
groupByMinute(requests) {
|
|
281
|
+
const groups = {}
|
|
282
|
+
|
|
283
|
+
requests.forEach(req => {
|
|
284
|
+
const minute = new Date(req.timestamp).toISOString().slice(0, 16) + ':00.000Z'
|
|
285
|
+
if (!groups[minute]) {
|
|
286
|
+
groups[minute] = {
|
|
287
|
+
requests: 0,
|
|
288
|
+
errors: 0,
|
|
289
|
+
avgResponseTime: 0,
|
|
290
|
+
totalResponseTime: 0
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
groups[minute].requests++
|
|
295
|
+
if (!req.success) groups[minute].errors++
|
|
296
|
+
if (req.responseTime) {
|
|
297
|
+
groups[minute].totalResponseTime += req.responseTime
|
|
298
|
+
groups[minute].avgResponseTime = groups[minute].totalResponseTime / groups[minute].requests
|
|
299
|
+
}
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
return groups
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
groupByCountry(requests) {
|
|
306
|
+
const groups = {}
|
|
307
|
+
|
|
308
|
+
requests.forEach(req => {
|
|
309
|
+
const country = req.country || 'unknown'
|
|
310
|
+
if (!groups[country]) {
|
|
311
|
+
groups[country] = {
|
|
312
|
+
requests: 0,
|
|
313
|
+
errors: 0,
|
|
314
|
+
avgResponseTime: 0,
|
|
315
|
+
totalResponseTime: 0
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
groups[country].requests++
|
|
320
|
+
if (!req.success) groups[country].errors++
|
|
321
|
+
if (req.responseTime) {
|
|
322
|
+
groups[country].totalResponseTime += req.responseTime
|
|
323
|
+
groups[country].avgResponseTime = groups[country].totalResponseTime / groups[country].requests
|
|
324
|
+
}
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
return groups
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Clean up old data
|
|
332
|
+
*/
|
|
333
|
+
cleanup() {
|
|
334
|
+
const cutoff = Date.now() - this.retentionMs
|
|
335
|
+
|
|
336
|
+
this.metrics.requests = this.metrics.requests.filter(req => req.timestamp > cutoff)
|
|
337
|
+
this.metrics.errors = this.metrics.errors.filter(err => err.timestamp > cutoff)
|
|
338
|
+
this.metrics.performance.responseTimes = this.metrics.performance.responseTimes.filter(
|
|
339
|
+
perf => perf.timestamp > cutoff
|
|
340
|
+
)
|
|
341
|
+
this.metrics.rateLimits.delays = this.metrics.rateLimits.delays.filter(
|
|
342
|
+
delay => delay.timestamp > cutoff
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
// Limit data points
|
|
346
|
+
if (this.metrics.requests.length > this.maxDataPoints) {
|
|
347
|
+
this.metrics.requests = this.metrics.requests.slice(-this.maxDataPoints)
|
|
348
|
+
}
|
|
349
|
+
if (this.metrics.errors.length > this.maxDataPoints) {
|
|
350
|
+
this.metrics.errors = this.metrics.errors.slice(-this.maxDataPoints)
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Reset all metrics
|
|
356
|
+
*/
|
|
357
|
+
reset() {
|
|
358
|
+
this.metrics = {
|
|
359
|
+
requests: [],
|
|
360
|
+
errors: [],
|
|
361
|
+
cache: { hits: 0, misses: 0, size: 0 },
|
|
362
|
+
performance: { responseTimes: [], throughput: [] },
|
|
363
|
+
rateLimits: { hits: 0, delays: [] }
|
|
364
|
+
}
|
|
365
|
+
this.counters = {
|
|
366
|
+
totalRequests: 0,
|
|
367
|
+
successfulRequests: 0,
|
|
368
|
+
failedRequests: 0,
|
|
369
|
+
cacheHits: 0,
|
|
370
|
+
cacheMisses: 0,
|
|
371
|
+
rateLimitHits: 0
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Export metrics for external systems
|
|
377
|
+
*/
|
|
378
|
+
export() {
|
|
379
|
+
return {
|
|
380
|
+
summary: this.getSummary(),
|
|
381
|
+
detailed: this.getDetailedMetrics(),
|
|
382
|
+
raw: {
|
|
383
|
+
metrics: this.metrics,
|
|
384
|
+
counters: this.counters
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
module.exports = { MetricsCollector }
|
package/lib/metrics.mjs
ADDED