odesli.js 2.0.0 → 2.0.1

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/lib/index.js CHANGED
@@ -291,10 +291,19 @@ function getCachedResponse (key, metrics) {
291
291
  clearExpiredCache()
292
292
  const cached = cache.get(key)
293
293
  if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
294
- if (metrics) metrics.recordRequest({ cacheHit: true })
294
+ if (metrics && typeof metrics.recordRequest === 'function') {
295
+ const now = Date.now()
296
+ metrics.recordRequest({
297
+ url: key,
298
+ startTime: now,
299
+ endTime: now,
300
+ success: true,
301
+ statusCode: 200,
302
+ cacheHit: true
303
+ })
304
+ }
295
305
  return cached.data
296
306
  }
297
- if (metrics) metrics.recordRequest({ cacheHit: false })
298
307
  return null
299
308
  }
300
309
 
@@ -434,8 +443,10 @@ class Odesli {
434
443
 
435
444
  if (metrics === false) {
436
445
  this.metrics = new MetricsCollector({ enabled: false })
446
+ } else if (metrics && typeof metrics === 'object') {
447
+ this.metrics = metrics
437
448
  } else {
438
- this.metrics = metrics || new MetricsCollector()
449
+ this.metrics = new MetricsCollector()
439
450
  }
440
451
  }
441
452
 
@@ -534,6 +545,7 @@ class Odesli {
534
545
  }
535
546
  }
536
547
 
548
+ const requestStartTime = Date.now()
537
549
  let lastError
538
550
  for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
539
551
  try {
@@ -598,6 +610,17 @@ class Odesli {
598
610
  cacheResponse(cacheKey, result)
599
611
  }
600
612
 
613
+ if (this.metrics && typeof this.metrics.recordRequest === 'function') {
614
+ this.metrics.recordRequest({
615
+ url: cacheKey,
616
+ startTime: requestStartTime,
617
+ endTime: Date.now(),
618
+ success: true,
619
+ statusCode: response.status,
620
+ cacheHit: false
621
+ })
622
+ }
623
+
601
624
  this._log(`Request successful for ${path}`, 'debug')
602
625
  return result
603
626
  } catch (err) {
@@ -636,6 +659,13 @@ class Odesli {
636
659
  typeof lastError.retryAfterMs === 'number'
637
660
  ? lastError.retryAfterMs
638
661
  : this.retryDelay * Math.pow(2, attempt - 1)
662
+ if (
663
+ lastError.statusCode === 429 &&
664
+ this.metrics &&
665
+ typeof this.metrics.recordRateLimit === 'function'
666
+ ) {
667
+ this.metrics.recordRateLimit(delay)
668
+ }
639
669
  this._log(
640
670
  `Request failed, retrying in ${delay}ms: ${lastError.message}`,
641
671
  'warn'
@@ -644,6 +674,24 @@ class Odesli {
644
674
  }
645
675
  }
646
676
 
677
+ if (this.metrics && typeof this.metrics.recordRequest === 'function') {
678
+ this.metrics.recordRequest({
679
+ url: cacheKey,
680
+ startTime: requestStartTime,
681
+ endTime: Date.now(),
682
+ success: false,
683
+ statusCode: lastError.statusCode,
684
+ error: lastError,
685
+ cacheHit: false
686
+ })
687
+ }
688
+ if (this.metrics && typeof this.metrics.recordError === 'function') {
689
+ this.metrics.recordError(lastError, {
690
+ url: cacheKey,
691
+ statusCode: lastError.statusCode
692
+ })
693
+ }
694
+
647
695
  throw lastError
648
696
  }
649
697
 
package/lib/metrics.js CHANGED
@@ -96,7 +96,7 @@ class MetricsCollector {
96
96
  }
97
97
 
98
98
  // Record performance metrics
99
- if (request.responseTime) {
99
+ if (typeof request.responseTime === 'number') {
100
100
  this.metrics.performance.responseTimes.push({
101
101
  timestamp: startTime,
102
102
  responseTime: request.responseTime
@@ -30,19 +30,23 @@ class RateLimiter {
30
30
  while (true) {
31
31
  const now = Date.now()
32
32
  const timePassed = now - this.lastRefill
33
- const tokensToAdd = Math.floor(timePassed / this.windowMs) * this.maxRequests
34
-
35
- this.tokens = Math.min(this.maxRequests, this.tokens + tokensToAdd)
36
- this.lastRefill = now
37
-
33
+ const windowsElapsed = Math.floor(timePassed / this.windowMs)
34
+ const tokensToAdd = windowsElapsed * this.maxRequests
35
+
36
+ // Only advance lastRefill when tokens are actually refilled. Resetting it
37
+ // on every check with tokensToAdd === 0 permanently stalls the bucket.
38
+ if (tokensToAdd > 0) {
39
+ this.tokens = Math.min(this.maxRequests, this.tokens + tokensToAdd)
40
+ this.lastRefill += windowsElapsed * this.windowMs
41
+ }
42
+
38
43
  if (this.tokens >= 1) {
39
44
  this.tokens--
40
45
  return true
41
46
  }
42
-
43
- // Calculate wait time
47
+
44
48
  const waitTime = this.windowMs - (timePassed % this.windowMs)
45
- await new Promise(resolve => setTimeout(resolve, waitTime))
49
+ await new Promise(resolve => setTimeout(resolve, Math.max(waitTime, 1)))
46
50
  }
47
51
  }
48
52
 
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "odesli.js",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Node.js/TypeScript client for the Songlink/Odesli API (requires allowlisted API key; public API retired July 2026)",
5
5
  "main": "lib/index.js",
6
- "module": "lib/index.js",
6
+ "module": "lib/index.mjs",
7
7
  "types": "lib/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
10
  "types": "./lib/index.d.ts",
11
- "import": "./lib/index.js",
11
+ "import": "./lib/index.mjs",
12
12
  "require": "./lib/index.js"
13
13
  },
14
14
  "./rate-limiter": {
@@ -97,7 +97,20 @@
97
97
  "typescript": "^5.9.3"
98
98
  },
99
99
  "overrides": {
100
- "js-yaml": "^4.1.1"
100
+ "js-yaml": "^4.1.1",
101
+ "@jest/reporters": {
102
+ "glob": "^14.0.6"
103
+ },
104
+ "jest-config": {
105
+ "glob": "^14.0.6"
106
+ },
107
+ "jest-runtime": {
108
+ "glob": "^14.0.6"
109
+ },
110
+ "test-exclude": {
111
+ "glob": "^14.0.6",
112
+ "minimatch": "^11.0.9"
113
+ }
101
114
  },
102
115
  "homepage": "https://github.com/MattrAus/odesli.js#readme"
103
116
  }