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
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
const { RateLimiter } = require('../lib/rate-limiter.js');
|
|
2
|
+
const { MetricsCollector } = require('../lib/metrics.js');
|
|
3
|
+
const {
|
|
4
|
+
PluginSystem,
|
|
5
|
+
loggingPlugin,
|
|
6
|
+
analyticsPlugin,
|
|
7
|
+
} = require('../lib/plugin-system.js');
|
|
8
|
+
|
|
9
|
+
describe('Advanced Features', () => {
|
|
10
|
+
describe('RateLimiter', () => {
|
|
11
|
+
let rateLimiter;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
rateLimiter = new RateLimiter({
|
|
15
|
+
maxRequests: 5,
|
|
16
|
+
windowMs: 1000, // 1 second for faster tests
|
|
17
|
+
strategy: 'token-bucket',
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('should initialize with correct settings', () => {
|
|
22
|
+
expect(rateLimiter.maxRequests).toBe(5);
|
|
23
|
+
expect(rateLimiter.windowMs).toBe(1000);
|
|
24
|
+
expect(rateLimiter.strategy).toBe('token-bucket');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('should allow requests within limit', async () => {
|
|
28
|
+
const startTime = Date.now();
|
|
29
|
+
|
|
30
|
+
// Should allow 5 requests immediately
|
|
31
|
+
for (let i = 0; i < 5; i++) {
|
|
32
|
+
await rateLimiter.waitForSlot();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const endTime = Date.now();
|
|
36
|
+
expect(endTime - startTime).toBeLessThan(100); // Should be very fast
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('should throttle requests beyond limit', async () => {
|
|
40
|
+
// Use up all tokens
|
|
41
|
+
for (let i = 0; i < 5; i++) {
|
|
42
|
+
await rateLimiter.waitForSlot();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const startTime = Date.now();
|
|
46
|
+
await rateLimiter.waitForSlot(); // This should wait
|
|
47
|
+
const endTime = Date.now();
|
|
48
|
+
|
|
49
|
+
expect(endTime - startTime).toBeGreaterThan(800); // Should wait close to window time
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('should handle rate limit responses', async () => {
|
|
53
|
+
const startTime = Date.now();
|
|
54
|
+
await rateLimiter.handleRateLimitResponse(2); // 2 seconds
|
|
55
|
+
const endTime = Date.now();
|
|
56
|
+
|
|
57
|
+
expect(endTime - startTime).toBeGreaterThan(1900); // Should wait ~2 seconds
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('should provide status information', () => {
|
|
61
|
+
const status = rateLimiter.getStatus();
|
|
62
|
+
expect(status).toHaveProperty('available');
|
|
63
|
+
expect(status).toHaveProperty('max');
|
|
64
|
+
expect(status).toHaveProperty('refillRate');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('should work with different strategies', async () => {
|
|
68
|
+
const slidingWindowLimiter = new RateLimiter({
|
|
69
|
+
maxRequests: 3,
|
|
70
|
+
windowMs: 1000,
|
|
71
|
+
strategy: 'sliding-window',
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const startTime = Date.now();
|
|
75
|
+
|
|
76
|
+
for (let i = 0; i < 3; i++) {
|
|
77
|
+
await slidingWindowLimiter.waitForSlot();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const endTime = Date.now();
|
|
81
|
+
expect(endTime - startTime).toBeLessThan(100);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('MetricsCollector', () => {
|
|
86
|
+
let metrics;
|
|
87
|
+
|
|
88
|
+
beforeEach(() => {
|
|
89
|
+
metrics = new MetricsCollector({
|
|
90
|
+
enabled: true,
|
|
91
|
+
retentionMs: 1000, // 1 second for faster tests
|
|
92
|
+
maxDataPoints: 10,
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('should initialize with correct settings', () => {
|
|
97
|
+
expect(metrics.enabled).toBe(true);
|
|
98
|
+
expect(metrics.retentionMs).toBe(1000);
|
|
99
|
+
expect(metrics.maxDataPoints).toBe(10);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('should record successful requests', () => {
|
|
103
|
+
const startTime = Date.now();
|
|
104
|
+
metrics.recordRequest({
|
|
105
|
+
url: 'https://example.com',
|
|
106
|
+
startTime,
|
|
107
|
+
endTime: startTime + 100,
|
|
108
|
+
success: true,
|
|
109
|
+
statusCode: 200,
|
|
110
|
+
platform: 'spotify',
|
|
111
|
+
country: 'US',
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const summary = metrics.getSummary();
|
|
115
|
+
expect(summary.counters.totalRequests).toBe(1);
|
|
116
|
+
expect(summary.counters.successfulRequests).toBe(1);
|
|
117
|
+
expect(summary.counters.failedRequests).toBe(0);
|
|
118
|
+
expect(summary.rates.successRate).toBe(1);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('should record failed requests', () => {
|
|
122
|
+
const startTime = Date.now();
|
|
123
|
+
metrics.recordRequest({
|
|
124
|
+
url: 'https://example.com',
|
|
125
|
+
startTime,
|
|
126
|
+
endTime: startTime + 100,
|
|
127
|
+
success: false,
|
|
128
|
+
error: new Error('Network error'),
|
|
129
|
+
platform: 'spotify',
|
|
130
|
+
country: 'US',
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const summary = metrics.getSummary();
|
|
134
|
+
expect(summary.counters.totalRequests).toBe(1);
|
|
135
|
+
expect(summary.counters.successfulRequests).toBe(0);
|
|
136
|
+
expect(summary.counters.failedRequests).toBe(1);
|
|
137
|
+
expect(summary.rates.successRate).toBe(0);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test('should record cache hits and misses', () => {
|
|
141
|
+
metrics.recordRequest({ cacheHit: true });
|
|
142
|
+
metrics.recordRequest({ cacheHit: false });
|
|
143
|
+
metrics.recordRequest({ cacheHit: true });
|
|
144
|
+
|
|
145
|
+
const summary = metrics.getSummary();
|
|
146
|
+
expect(summary.counters.cacheHits).toBe(2);
|
|
147
|
+
expect(summary.counters.cacheMisses).toBe(1);
|
|
148
|
+
expect(summary.rates.cacheHitRate).toBeCloseTo(0.67, 2);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('should record rate limit hits', () => {
|
|
152
|
+
metrics.recordRateLimit(1000);
|
|
153
|
+
metrics.recordRateLimit(2000);
|
|
154
|
+
|
|
155
|
+
const summary = metrics.getSummary();
|
|
156
|
+
expect(summary.counters.rateLimitHits).toBe(2);
|
|
157
|
+
expect(summary.rateLimits.hits).toBe(2);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test('should record errors', () => {
|
|
161
|
+
const error = new Error('Test error');
|
|
162
|
+
metrics.recordError(error, { url: 'https://example.com' });
|
|
163
|
+
|
|
164
|
+
const detailed = metrics.getDetailedMetrics();
|
|
165
|
+
expect(detailed).toBeDefined();
|
|
166
|
+
expect(typeof detailed).toBe('object');
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('should group metrics by platform', () => {
|
|
170
|
+
metrics.recordRequest({
|
|
171
|
+
url: 'https://spotify.com/track/123',
|
|
172
|
+
platform: 'spotify',
|
|
173
|
+
success: true,
|
|
174
|
+
});
|
|
175
|
+
metrics.recordRequest({
|
|
176
|
+
url: 'https://youtube.com/watch?v=abc',
|
|
177
|
+
platform: 'youtube',
|
|
178
|
+
success: true,
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const platformMetrics = metrics.getDetailedMetrics({
|
|
182
|
+
groupBy: 'platform',
|
|
183
|
+
});
|
|
184
|
+
expect(platformMetrics.spotify.requests).toBe(1);
|
|
185
|
+
expect(platformMetrics.youtube.requests).toBe(1);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test('should clean up old data', () => {
|
|
189
|
+
// Add some old data
|
|
190
|
+
metrics.metrics.requests.push({
|
|
191
|
+
timestamp: Date.now() - 2000, // 2 seconds ago
|
|
192
|
+
url: 'old-request',
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
metrics.cleanup();
|
|
196
|
+
expect(metrics.metrics.requests).toHaveLength(0);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test('should export metrics', () => {
|
|
200
|
+
metrics.recordRequest({ success: true });
|
|
201
|
+
|
|
202
|
+
const exported = metrics.export();
|
|
203
|
+
expect(exported).toHaveProperty('summary');
|
|
204
|
+
expect(exported).toHaveProperty('detailed');
|
|
205
|
+
expect(exported).toHaveProperty('raw');
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test('should reset metrics', () => {
|
|
209
|
+
metrics.recordRequest({ success: true });
|
|
210
|
+
expect(metrics.counters.totalRequests).toBe(1);
|
|
211
|
+
|
|
212
|
+
metrics.reset();
|
|
213
|
+
expect(metrics.counters.totalRequests).toBe(0);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe('PluginSystem', () => {
|
|
218
|
+
let pluginSystem;
|
|
219
|
+
|
|
220
|
+
beforeEach(() => {
|
|
221
|
+
pluginSystem = new PluginSystem();
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test('should initialize with built-in hooks', () => {
|
|
225
|
+
const hooks = Array.from(pluginSystem.hooks.keys());
|
|
226
|
+
expect(hooks).toContain('beforeRequest');
|
|
227
|
+
expect(hooks).toContain('afterRequest');
|
|
228
|
+
expect(hooks).toContain('onError');
|
|
229
|
+
expect(hooks).toContain('onRateLimit');
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test('should register plugins', () => {
|
|
233
|
+
pluginSystem.registerPlugin('test', {
|
|
234
|
+
name: 'test',
|
|
235
|
+
version: '1.0.0',
|
|
236
|
+
description: 'Test plugin',
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
expect(pluginSystem.hasPlugin('test')).toBe(true);
|
|
240
|
+
expect(pluginSystem.getPlugins()).toContain('test');
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test('should not register duplicate plugins', () => {
|
|
244
|
+
pluginSystem.registerPlugin('test', { name: 'test' });
|
|
245
|
+
|
|
246
|
+
expect(() => {
|
|
247
|
+
pluginSystem.registerPlugin('test', { name: 'test' });
|
|
248
|
+
}).toThrow('Plugin "test" is already registered');
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
test('should unregister plugins', () => {
|
|
252
|
+
pluginSystem.registerPlugin('test', { name: 'test' });
|
|
253
|
+
expect(pluginSystem.hasPlugin('test')).toBe(true);
|
|
254
|
+
|
|
255
|
+
pluginSystem.unregisterPlugin('test');
|
|
256
|
+
expect(pluginSystem.hasPlugin('test')).toBe(false);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test('should execute hooks', async () => {
|
|
260
|
+
let executed = false;
|
|
261
|
+
|
|
262
|
+
pluginSystem.registerHookHandler('beforeRequest', async context => {
|
|
263
|
+
executed = true;
|
|
264
|
+
expect(context.url).toBe('https://example.com');
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
await pluginSystem.executeHook('beforeRequest', {
|
|
268
|
+
url: 'https://example.com',
|
|
269
|
+
});
|
|
270
|
+
expect(executed).toBe(true);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test('should handle hook errors gracefully', async () => {
|
|
274
|
+
pluginSystem.registerHookHandler('beforeRequest', async () => {
|
|
275
|
+
throw new Error('Hook error');
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
let errorExecuted = false;
|
|
279
|
+
pluginSystem.registerHookHandler('onError', async context => {
|
|
280
|
+
errorExecuted = true;
|
|
281
|
+
expect(context.error.message).toBe('Hook error');
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
await pluginSystem.executeHook('beforeRequest', {});
|
|
285
|
+
expect(errorExecuted).toBe(true);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
test('should execute middleware chain', async () => {
|
|
289
|
+
const middleware1 = async (context, next) => {
|
|
290
|
+
context.value = (context.value || 0) + 1;
|
|
291
|
+
return await next();
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
const middleware2 = async (context, next) => {
|
|
295
|
+
context.value = context.value * 2;
|
|
296
|
+
return await next();
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
pluginSystem.middleware.push({ name: 'test1', middleware: middleware1 });
|
|
300
|
+
pluginSystem.middleware.push({ name: 'test2', middleware: middleware2 });
|
|
301
|
+
|
|
302
|
+
const context = { value: 0 };
|
|
303
|
+
const result = await pluginSystem.executeMiddleware(context, async () => {
|
|
304
|
+
return context.value + 10;
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
expect(result).toBe(12); // (0 + 1) * 2 + 10
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test('should transform data', async () => {
|
|
311
|
+
const transformer = async (data, _context) => {
|
|
312
|
+
data.transformed = true;
|
|
313
|
+
return data;
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
pluginSystem.transformers.push({
|
|
317
|
+
name: 'test',
|
|
318
|
+
transformers: { song: transformer },
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
const originalData = { title: 'Test Song' };
|
|
322
|
+
const transformed = await pluginSystem.transformData(
|
|
323
|
+
originalData,
|
|
324
|
+
'song'
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
expect(transformed.transformed).toBe(true);
|
|
328
|
+
expect(transformed.title).toBe('Test Song');
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test('should provide plugin information', () => {
|
|
332
|
+
const plugin = {
|
|
333
|
+
name: 'test',
|
|
334
|
+
version: '1.0.0',
|
|
335
|
+
description: 'Test plugin',
|
|
336
|
+
hooks: { beforeRequest: () => {} },
|
|
337
|
+
middleware: async () => {},
|
|
338
|
+
transformers: { song: async () => {} },
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
pluginSystem.registerPlugin('test', plugin);
|
|
342
|
+
const info = pluginSystem.getPluginInfo('test');
|
|
343
|
+
|
|
344
|
+
expect(info.name).toBe('test');
|
|
345
|
+
expect(info.version).toBe('1.0.0');
|
|
346
|
+
expect(info.description).toBe('Test plugin');
|
|
347
|
+
expect(info.hooks).toContain('beforeRequest');
|
|
348
|
+
expect(info.hasMiddleware).toBe(true);
|
|
349
|
+
expect(info.hasTransformers).toBe(true);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
test('should work with built-in plugins', () => {
|
|
353
|
+
pluginSystem.registerPlugin('logging', loggingPlugin);
|
|
354
|
+
pluginSystem.registerPlugin('analytics', analyticsPlugin);
|
|
355
|
+
|
|
356
|
+
expect(pluginSystem.hasPlugin('logging')).toBe(true);
|
|
357
|
+
expect(pluginSystem.hasPlugin('analytics')).toBe(true);
|
|
358
|
+
expect(pluginSystem.getPlugins()).toHaveLength(2);
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
describe('Integration Tests', () => {
|
|
363
|
+
test('should work together - rate limiting with metrics', async () => {
|
|
364
|
+
const rateLimiter = new RateLimiter({
|
|
365
|
+
maxRequests: 3,
|
|
366
|
+
windowMs: 100,
|
|
367
|
+
strategy: 'token-bucket',
|
|
368
|
+
});
|
|
369
|
+
const metrics = new MetricsCollector({ enabled: true });
|
|
370
|
+
|
|
371
|
+
for (let i = 0; i < 5; i++) {
|
|
372
|
+
await rateLimiter.waitForSlot();
|
|
373
|
+
const startTime = Date.now();
|
|
374
|
+
metrics.recordRequest({
|
|
375
|
+
url: `https://example.com/test${i}`,
|
|
376
|
+
startTime,
|
|
377
|
+
endTime: Date.now(),
|
|
378
|
+
success: true,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const summary = metrics.getSummary();
|
|
383
|
+
expect(summary.counters.totalRequests).toBe(5);
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
test('should work together - plugins with metrics', async () => {
|
|
387
|
+
const pluginSystem = new PluginSystem();
|
|
388
|
+
const metrics = new MetricsCollector({ enabled: true });
|
|
389
|
+
|
|
390
|
+
// Create a custom plugin that records metrics
|
|
391
|
+
const metricsPlugin = {
|
|
392
|
+
name: 'metrics-plugin',
|
|
393
|
+
hooks: {
|
|
394
|
+
beforeRequest: async context => {
|
|
395
|
+
context.startTime = Date.now();
|
|
396
|
+
},
|
|
397
|
+
afterRequest: async context => {
|
|
398
|
+
metrics.recordRequest({
|
|
399
|
+
url: context.url,
|
|
400
|
+
startTime: context.startTime,
|
|
401
|
+
endTime: Date.now(),
|
|
402
|
+
success: context.success || true,
|
|
403
|
+
});
|
|
404
|
+
},
|
|
405
|
+
},
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
pluginSystem.registerPlugin('metrics', metricsPlugin);
|
|
409
|
+
|
|
410
|
+
await pluginSystem.executeHook('beforeRequest', {
|
|
411
|
+
url: 'https://example.com',
|
|
412
|
+
});
|
|
413
|
+
await pluginSystem.executeHook('afterRequest', {
|
|
414
|
+
url: 'https://example.com',
|
|
415
|
+
success: true,
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
const summary = metrics.getSummary();
|
|
419
|
+
expect(summary.counters.totalRequests).toBe(1);
|
|
420
|
+
expect(summary.counters.successfulRequests).toBe(1);
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
});
|