mppx 0.3.9 → 0.3.12
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/README.md +3 -3
- package/dist/Challenge.d.ts.map +1 -1
- package/dist/Challenge.js +2 -0
- package/dist/Challenge.js.map +1 -1
- package/dist/Errors.d.ts +0 -2
- package/dist/Errors.d.ts.map +1 -1
- package/dist/Errors.js +1 -3
- package/dist/Errors.js.map +1 -1
- package/dist/client/Mppx.d.ts +1 -1
- package/dist/client/Mppx.d.ts.map +1 -1
- package/dist/client/internal/Fetch.d.ts +1 -1
- package/dist/client/internal/Fetch.d.ts.map +1 -1
- package/dist/client/internal/Fetch.js +23 -4
- package/dist/client/internal/Fetch.js.map +1 -1
- package/dist/internal/constantTimeEqual.d.ts.map +1 -1
- package/dist/internal/constantTimeEqual.js +4 -6
- package/dist/internal/constantTimeEqual.js.map +1 -1
- package/dist/internal/env.d.ts +2 -2
- package/dist/internal/env.d.ts.map +1 -1
- package/dist/internal/env.js +1 -2
- package/dist/internal/env.js.map +1 -1
- package/dist/middlewares/internal/mppx.d.ts.map +1 -1
- package/dist/middlewares/internal/mppx.js +6 -2
- package/dist/middlewares/internal/mppx.js.map +1 -1
- package/dist/server/Mppx.d.ts +13 -3
- package/dist/server/Mppx.d.ts.map +1 -1
- package/dist/server/Mppx.js +46 -3
- package/dist/server/Mppx.js.map +1 -1
- package/dist/tempo/client/Charge.d.ts +10 -0
- package/dist/tempo/client/Charge.d.ts.map +1 -1
- package/dist/tempo/client/Charge.js +23 -9
- package/dist/tempo/client/Charge.js.map +1 -1
- package/dist/tempo/client/Methods.d.ts +1 -0
- package/dist/tempo/client/Methods.d.ts.map +1 -1
- package/dist/tempo/internal/auto-swap.d.ts +49 -0
- package/dist/tempo/internal/auto-swap.d.ts.map +1 -0
- package/dist/tempo/internal/auto-swap.js +89 -0
- package/dist/tempo/internal/auto-swap.js.map +1 -0
- package/dist/tempo/internal/fee-payer.d.ts +15 -0
- package/dist/tempo/internal/fee-payer.d.ts.map +1 -0
- package/dist/tempo/internal/fee-payer.js +41 -0
- package/dist/tempo/internal/fee-payer.js.map +1 -0
- package/dist/tempo/internal/selectors.d.ts +5 -0
- package/dist/tempo/internal/selectors.d.ts.map +1 -0
- package/dist/tempo/internal/selectors.js +7 -0
- package/dist/tempo/internal/selectors.js.map +1 -0
- package/dist/tempo/internal/simulate.d.ts +21 -0
- package/dist/tempo/internal/simulate.d.ts.map +1 -0
- package/dist/tempo/internal/simulate.js +31 -0
- package/dist/tempo/internal/simulate.js.map +1 -0
- package/dist/tempo/server/Charge.d.ts +12 -0
- package/dist/tempo/server/Charge.d.ts.map +1 -1
- package/dist/tempo/server/Charge.js +36 -12
- package/dist/tempo/server/Charge.js.map +1 -1
- package/dist/tempo/server/Session.d.ts +14 -0
- package/dist/tempo/server/Session.d.ts.map +1 -1
- package/dist/tempo/server/Session.js +59 -40
- package/dist/tempo/server/Session.js.map +1 -1
- package/dist/tempo/session/Chain.d.ts +3 -0
- package/dist/tempo/session/Chain.d.ts.map +1 -1
- package/dist/tempo/session/Chain.js +27 -6
- package/dist/tempo/session/Chain.js.map +1 -1
- package/package.json +1 -1
- package/src/Challenge.ts +2 -0
- package/src/Errors.test.ts +43 -18
- package/src/Errors.ts +1 -4
- package/src/client/Mppx.test-d.ts +28 -0
- package/src/client/Mppx.test.ts +1 -0
- package/src/client/Mppx.ts +3 -3
- package/src/client/internal/Fetch.test.ts +410 -0
- package/src/client/internal/Fetch.ts +25 -7
- package/src/internal/constantTimeEqual.ts +5 -4
- package/src/internal/env.test.ts +2 -2
- package/src/internal/env.ts +4 -5
- package/src/middlewares/express.test.ts +5 -0
- package/src/middlewares/hono.test.ts +5 -0
- package/src/middlewares/internal/mppx.ts +5 -2
- package/src/middlewares/nextjs.test.ts +5 -0
- package/src/proxy/Proxy.test.ts +3 -0
- package/src/proxy/services/openai.test.ts +3 -0
- package/src/server/Mppx.test.ts +93 -2
- package/src/server/Mppx.ts +81 -6
- package/src/tempo/client/Charge.ts +40 -9
- package/src/tempo/internal/auto-swap.test.ts +113 -0
- package/src/tempo/internal/auto-swap.ts +141 -0
- package/src/tempo/internal/fee-payer.test.ts +223 -0
- package/src/tempo/internal/fee-payer.ts +53 -0
- package/src/tempo/internal/selectors.ts +10 -0
- package/src/tempo/internal/simulate.ts +49 -0
- package/src/tempo/server/Charge.test.ts +436 -3
- package/src/tempo/server/Charge.ts +52 -23
- package/src/tempo/server/Session.test.ts +49 -0
- package/src/tempo/server/Session.ts +76 -34
- package/src/tempo/session/Chain.test.ts +36 -0
- package/src/tempo/session/Chain.ts +38 -2
|
@@ -268,6 +268,374 @@ describe('Fetch.from', () => {
|
|
|
268
268
|
})
|
|
269
269
|
})
|
|
270
270
|
|
|
271
|
+
// Minimal mock method — createCredential is only invoked on the 402 retry path.
|
|
272
|
+
const noopMethod = {
|
|
273
|
+
name: 'test',
|
|
274
|
+
intent: 'test',
|
|
275
|
+
context: undefined,
|
|
276
|
+
createCredential: async () => 'credential',
|
|
277
|
+
} as any
|
|
278
|
+
|
|
279
|
+
/** Builds a valid 402 response with a WWW-Authenticate header. */
|
|
280
|
+
function make402(overrides?: { method?: string; intent?: string }) {
|
|
281
|
+
const method = overrides?.method ?? 'test'
|
|
282
|
+
const intent = overrides?.intent ?? 'test'
|
|
283
|
+
const request = btoa(JSON.stringify({ amount: '1' }))
|
|
284
|
+
.replace(/\+/g, '-')
|
|
285
|
+
.replace(/\//g, '_')
|
|
286
|
+
.replace(/=+$/, '')
|
|
287
|
+
const header = `Payment id="abc", realm="test", method="${method}", intent="${intent}", request="${request}"`
|
|
288
|
+
return new Response(null, {
|
|
289
|
+
status: 402,
|
|
290
|
+
headers: { 'WWW-Authenticate': header },
|
|
291
|
+
})
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
describe('Fetch.from: init passthrough (non-402)', () => {
|
|
295
|
+
test('passes unmodified init to underlying fetch for non-402 responses', async () => {
|
|
296
|
+
const receivedInits: (RequestInit | undefined)[] = []
|
|
297
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
298
|
+
receivedInits.push(init)
|
|
299
|
+
return new Response('OK', { status: 200 })
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const fetch = Fetch.from({
|
|
303
|
+
fetch: mockFetch,
|
|
304
|
+
methods: [noopMethod],
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
const customInit = {
|
|
308
|
+
method: 'POST',
|
|
309
|
+
headers: { 'X-Custom': 'value' },
|
|
310
|
+
body: JSON.stringify({ data: 'test' }),
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
await fetch('https://example.com/ws-upgrade', customInit)
|
|
314
|
+
|
|
315
|
+
expect(receivedInits[0]).toBe(customInit)
|
|
316
|
+
})
|
|
317
|
+
|
|
318
|
+
test('preserves extra properties on init for non-402 responses', async () => {
|
|
319
|
+
const receivedInits: (RequestInit | undefined)[] = []
|
|
320
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
321
|
+
receivedInits.push(init)
|
|
322
|
+
return new Response('OK', { status: 200 })
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const fetch = Fetch.from({
|
|
326
|
+
fetch: mockFetch,
|
|
327
|
+
methods: [noopMethod],
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
const customInit = {
|
|
331
|
+
method: 'GET',
|
|
332
|
+
headers: { Authorization: 'Bearer token123' },
|
|
333
|
+
signal: AbortSignal.timeout(5000),
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
await fetch('https://example.com/api', customInit)
|
|
337
|
+
|
|
338
|
+
const received = receivedInits[0]!
|
|
339
|
+
expect(received.method).toBe('GET')
|
|
340
|
+
expect((received.headers as Record<string, string>).Authorization).toBe('Bearer token123')
|
|
341
|
+
expect(received.signal).toBe(customInit.signal)
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
test('passes through undefined init', async () => {
|
|
345
|
+
const receivedInits: (RequestInit | undefined)[] = []
|
|
346
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
347
|
+
receivedInits.push(init)
|
|
348
|
+
return new Response('OK', { status: 200 })
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const fetch = Fetch.from({
|
|
352
|
+
fetch: mockFetch,
|
|
353
|
+
methods: [noopMethod],
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
await fetch('https://example.com/api')
|
|
357
|
+
expect(receivedInits[0]).toBeUndefined()
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
test('passes init with context through untouched', async () => {
|
|
361
|
+
const receivedInits: (RequestInit | undefined)[] = []
|
|
362
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
363
|
+
receivedInits.push(init)
|
|
364
|
+
return new Response('OK', { status: 200 })
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const fetch = Fetch.from({
|
|
368
|
+
fetch: mockFetch,
|
|
369
|
+
methods: [noopMethod],
|
|
370
|
+
})
|
|
371
|
+
|
|
372
|
+
const customInit = { method: 'POST', context: { account: '0xabc' } }
|
|
373
|
+
await fetch('https://example.com/api', customInit as any)
|
|
374
|
+
|
|
375
|
+
expect(receivedInits[0]).toBe(customInit)
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
test('preserves object identity across all non-402 status codes', async () => {
|
|
379
|
+
for (const status of [200, 201, 204, 301, 400, 401, 403, 404, 500, 503]) {
|
|
380
|
+
const receivedInits: (RequestInit | undefined)[] = []
|
|
381
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
382
|
+
receivedInits.push(init)
|
|
383
|
+
return new Response(null, { status })
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const fetch = Fetch.from({
|
|
387
|
+
fetch: mockFetch,
|
|
388
|
+
methods: [noopMethod],
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
const customInit = { method: 'GET' }
|
|
392
|
+
await fetch('https://example.com/api', customInit)
|
|
393
|
+
expect(receivedInits[0]).toBe(customInit)
|
|
394
|
+
}
|
|
395
|
+
})
|
|
396
|
+
})
|
|
397
|
+
|
|
398
|
+
describe('Fetch.from: 402 retry path', () => {
|
|
399
|
+
test('strips context from init on retry', async () => {
|
|
400
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
401
|
+
let callCount = 0
|
|
402
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
403
|
+
calls.push({ init })
|
|
404
|
+
callCount++
|
|
405
|
+
if (callCount === 1) return make402()
|
|
406
|
+
return new Response('OK', { status: 200 })
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const fetch = Fetch.from({
|
|
410
|
+
fetch: mockFetch,
|
|
411
|
+
methods: [noopMethod],
|
|
412
|
+
})
|
|
413
|
+
|
|
414
|
+
await fetch('https://example.com/api', {
|
|
415
|
+
method: 'POST',
|
|
416
|
+
context: { account: '0xabc' },
|
|
417
|
+
} as any)
|
|
418
|
+
|
|
419
|
+
expect(calls).toHaveLength(2)
|
|
420
|
+
const retryInit = calls[1]!.init as Record<string, unknown>
|
|
421
|
+
expect(retryInit).not.toHaveProperty('context')
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
test('adds Authorization header on retry', async () => {
|
|
425
|
+
let callCount = 0
|
|
426
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
427
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
428
|
+
calls.push({ init })
|
|
429
|
+
callCount++
|
|
430
|
+
if (callCount === 1) return make402()
|
|
431
|
+
return new Response('OK', { status: 200 })
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const fetch = Fetch.from({
|
|
435
|
+
fetch: mockFetch,
|
|
436
|
+
methods: [noopMethod],
|
|
437
|
+
})
|
|
438
|
+
|
|
439
|
+
await fetch('https://example.com/api')
|
|
440
|
+
|
|
441
|
+
const retryInit = calls[1]!.init as Record<string, unknown>
|
|
442
|
+
const headers = retryInit.headers as Record<string, string>
|
|
443
|
+
expect(headers.Authorization).toBe('credential')
|
|
444
|
+
})
|
|
445
|
+
|
|
446
|
+
test('preserves existing headers on retry', async () => {
|
|
447
|
+
let callCount = 0
|
|
448
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
449
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
450
|
+
calls.push({ init })
|
|
451
|
+
callCount++
|
|
452
|
+
if (callCount === 1) return make402()
|
|
453
|
+
return new Response('OK', { status: 200 })
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const fetch = Fetch.from({
|
|
457
|
+
fetch: mockFetch,
|
|
458
|
+
methods: [noopMethod],
|
|
459
|
+
})
|
|
460
|
+
|
|
461
|
+
await fetch('https://example.com/api', {
|
|
462
|
+
headers: { 'X-Custom': 'value', 'Content-Type': 'application/json' },
|
|
463
|
+
})
|
|
464
|
+
|
|
465
|
+
const retryInit = calls[1]!.init as Record<string, unknown>
|
|
466
|
+
const headers = retryInit.headers as Record<string, string>
|
|
467
|
+
expect(headers['X-Custom']).toBe('value')
|
|
468
|
+
expect(headers['Content-Type']).toBe('application/json')
|
|
469
|
+
expect(headers.Authorization).toBe('credential')
|
|
470
|
+
})
|
|
471
|
+
|
|
472
|
+
test('preserves method and other init properties on retry', async () => {
|
|
473
|
+
let callCount = 0
|
|
474
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
475
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
476
|
+
calls.push({ init })
|
|
477
|
+
callCount++
|
|
478
|
+
if (callCount === 1) return make402()
|
|
479
|
+
return new Response('OK', { status: 200 })
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const fetch = Fetch.from({
|
|
483
|
+
fetch: mockFetch,
|
|
484
|
+
methods: [noopMethod],
|
|
485
|
+
})
|
|
486
|
+
|
|
487
|
+
await fetch('https://example.com/api', {
|
|
488
|
+
method: 'PUT',
|
|
489
|
+
body: JSON.stringify({ data: 'test' }),
|
|
490
|
+
credentials: 'include',
|
|
491
|
+
mode: 'cors',
|
|
492
|
+
})
|
|
493
|
+
|
|
494
|
+
const retryInit = calls[1]!.init as Record<string, unknown>
|
|
495
|
+
expect(retryInit.method).toBe('PUT')
|
|
496
|
+
expect(retryInit.body).toBe(JSON.stringify({ data: 'test' }))
|
|
497
|
+
expect(retryInit.credentials).toBe('include')
|
|
498
|
+
expect(retryInit.mode).toBe('cors')
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
test('handles undefined init on 402 retry', async () => {
|
|
502
|
+
let callCount = 0
|
|
503
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
504
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
505
|
+
calls.push({ init })
|
|
506
|
+
callCount++
|
|
507
|
+
if (callCount === 1) return make402()
|
|
508
|
+
return new Response('OK', { status: 200 })
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const fetch = Fetch.from({
|
|
512
|
+
fetch: mockFetch,
|
|
513
|
+
methods: [noopMethod],
|
|
514
|
+
})
|
|
515
|
+
|
|
516
|
+
await fetch('https://example.com/api')
|
|
517
|
+
|
|
518
|
+
expect(calls).toHaveLength(2)
|
|
519
|
+
const retryInit = calls[1]!.init as Record<string, unknown>
|
|
520
|
+
expect(retryInit.headers).toEqual({ Authorization: 'credential' })
|
|
521
|
+
})
|
|
522
|
+
|
|
523
|
+
test('throws when no matching method for 402 challenge', async () => {
|
|
524
|
+
const mockFetch: typeof globalThis.fetch = async () =>
|
|
525
|
+
make402({ method: 'stripe', intent: 'charge' })
|
|
526
|
+
|
|
527
|
+
const fetch = Fetch.from({
|
|
528
|
+
fetch: mockFetch,
|
|
529
|
+
methods: [noopMethod],
|
|
530
|
+
})
|
|
531
|
+
|
|
532
|
+
await expect(fetch('https://example.com/api')).rejects.toThrow(
|
|
533
|
+
'No method found for "stripe.charge"',
|
|
534
|
+
)
|
|
535
|
+
})
|
|
536
|
+
|
|
537
|
+
test('retries exactly once — does not loop on repeated 402', async () => {
|
|
538
|
+
let callCount = 0
|
|
539
|
+
const mockFetch: typeof globalThis.fetch = async () => {
|
|
540
|
+
callCount++
|
|
541
|
+
return make402()
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const fetch = Fetch.from({
|
|
545
|
+
fetch: mockFetch,
|
|
546
|
+
methods: [noopMethod],
|
|
547
|
+
})
|
|
548
|
+
|
|
549
|
+
const response = await fetch('https://example.com/api')
|
|
550
|
+
expect(callCount).toBe(2)
|
|
551
|
+
expect(response.status).toBe(402)
|
|
552
|
+
})
|
|
553
|
+
})
|
|
554
|
+
|
|
555
|
+
describe('Fetch.from: 402 retry headers normalization', () => {
|
|
556
|
+
test('preserves headers when passed as a Headers instance', async () => {
|
|
557
|
+
let callCount = 0
|
|
558
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
559
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
560
|
+
calls.push({ init })
|
|
561
|
+
callCount++
|
|
562
|
+
if (callCount === 1) return make402()
|
|
563
|
+
return new Response('OK', { status: 200 })
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
const fetch = Fetch.from({
|
|
567
|
+
fetch: mockFetch,
|
|
568
|
+
methods: [noopMethod],
|
|
569
|
+
})
|
|
570
|
+
|
|
571
|
+
const headers = new Headers({ 'X-Custom': 'value', 'Content-Type': 'application/json' })
|
|
572
|
+
await fetch('https://example.com/api', { headers })
|
|
573
|
+
|
|
574
|
+
const retryHeaders = (calls[1]!.init as Record<string, unknown>).headers as Record<
|
|
575
|
+
string,
|
|
576
|
+
string
|
|
577
|
+
>
|
|
578
|
+
expect(retryHeaders['x-custom']).toBe('value')
|
|
579
|
+
expect(retryHeaders['content-type']).toBe('application/json')
|
|
580
|
+
expect(retryHeaders.Authorization).toBe('credential')
|
|
581
|
+
})
|
|
582
|
+
|
|
583
|
+
test('preserves headers when passed as array of tuples', async () => {
|
|
584
|
+
let callCount = 0
|
|
585
|
+
const calls: { init: RequestInit | undefined }[] = []
|
|
586
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
587
|
+
calls.push({ init })
|
|
588
|
+
callCount++
|
|
589
|
+
if (callCount === 1) return make402()
|
|
590
|
+
return new Response('OK', { status: 200 })
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const fetch = Fetch.from({
|
|
594
|
+
fetch: mockFetch,
|
|
595
|
+
methods: [noopMethod],
|
|
596
|
+
})
|
|
597
|
+
|
|
598
|
+
await fetch('https://example.com/api', {
|
|
599
|
+
headers: [
|
|
600
|
+
['X-Custom', 'value'],
|
|
601
|
+
['Accept', 'application/json'],
|
|
602
|
+
],
|
|
603
|
+
})
|
|
604
|
+
|
|
605
|
+
const retryHeaders = (calls[1]!.init as Record<string, unknown>).headers as Record<
|
|
606
|
+
string,
|
|
607
|
+
string
|
|
608
|
+
>
|
|
609
|
+
expect(retryHeaders['X-Custom']).toBe('value')
|
|
610
|
+
expect(retryHeaders.Accept).toBe('application/json')
|
|
611
|
+
expect(retryHeaders.Authorization).toBe('credential')
|
|
612
|
+
})
|
|
613
|
+
})
|
|
614
|
+
|
|
615
|
+
describe('Fetch.from: input passthrough', () => {
|
|
616
|
+
test('passes URL input through on both initial and retry calls', async () => {
|
|
617
|
+
let callCount = 0
|
|
618
|
+
const receivedInputs: (RequestInfo | URL)[] = []
|
|
619
|
+
const mockFetch: typeof globalThis.fetch = async (input, _init) => {
|
|
620
|
+
receivedInputs.push(input)
|
|
621
|
+
callCount++
|
|
622
|
+
if (callCount === 1) return make402()
|
|
623
|
+
return new Response('OK', { status: 200 })
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
const fetch = Fetch.from({
|
|
627
|
+
fetch: mockFetch,
|
|
628
|
+
methods: [noopMethod],
|
|
629
|
+
})
|
|
630
|
+
|
|
631
|
+
const url = new URL('https://example.com/resource')
|
|
632
|
+
await fetch(url)
|
|
633
|
+
|
|
634
|
+
expect(receivedInputs[0]).toBe(url)
|
|
635
|
+
expect(receivedInputs[1]).toBe(url)
|
|
636
|
+
})
|
|
637
|
+
})
|
|
638
|
+
|
|
271
639
|
describe('Fetch.polyfill', () => {
|
|
272
640
|
test('default', async () => {
|
|
273
641
|
Fetch.polyfill({
|
|
@@ -313,3 +681,45 @@ describe('Fetch.polyfill', () => {
|
|
|
313
681
|
Fetch.restore()
|
|
314
682
|
})
|
|
315
683
|
})
|
|
684
|
+
|
|
685
|
+
describe('Fetch.polyfill / restore', () => {
|
|
686
|
+
test('restore is a no-op when polyfill was never called', () => {
|
|
687
|
+
const before = globalThis.fetch
|
|
688
|
+
Fetch.restore()
|
|
689
|
+
expect(globalThis.fetch).toBe(before)
|
|
690
|
+
})
|
|
691
|
+
|
|
692
|
+
test('restore reverts to original fetch', () => {
|
|
693
|
+
const originalFetch = globalThis.fetch
|
|
694
|
+
|
|
695
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
696
|
+
expect(globalThis.fetch).not.toBe(originalFetch)
|
|
697
|
+
|
|
698
|
+
Fetch.restore()
|
|
699
|
+
expect(globalThis.fetch).toBe(originalFetch)
|
|
700
|
+
})
|
|
701
|
+
|
|
702
|
+
test('stacked polyfill calls preserve the true original fetch', () => {
|
|
703
|
+
const originalFetch = globalThis.fetch
|
|
704
|
+
|
|
705
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
706
|
+
const firstPolyfill = globalThis.fetch
|
|
707
|
+
|
|
708
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
709
|
+
expect(globalThis.fetch).not.toBe(firstPolyfill)
|
|
710
|
+
|
|
711
|
+
Fetch.restore()
|
|
712
|
+
expect(globalThis.fetch).toBe(originalFetch)
|
|
713
|
+
})
|
|
714
|
+
|
|
715
|
+
test('double restore does not clobber fetch', () => {
|
|
716
|
+
const originalFetch = globalThis.fetch
|
|
717
|
+
|
|
718
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
719
|
+
Fetch.restore()
|
|
720
|
+
expect(globalThis.fetch).toBe(originalFetch)
|
|
721
|
+
|
|
722
|
+
Fetch.restore()
|
|
723
|
+
expect(globalThis.fetch).toBe(originalFetch)
|
|
724
|
+
})
|
|
725
|
+
})
|
|
@@ -31,11 +31,15 @@ export function from<const methods extends readonly Method.AnyClient[]>(
|
|
|
31
31
|
const { fetch = globalThis.fetch, methods, onChallenge } = config
|
|
32
32
|
|
|
33
33
|
return async (input, init) => {
|
|
34
|
-
|
|
35
|
-
const response = await fetch(input,
|
|
34
|
+
// Pass init through untouched to preserve object identity for non-402 responses.
|
|
35
|
+
const response = await fetch(input, init)
|
|
36
36
|
|
|
37
37
|
if (response.status !== 402) return response
|
|
38
38
|
|
|
39
|
+
// Only extract context for payment handling after confirming 402.
|
|
40
|
+
const context = (init as Record<string, unknown> | undefined)?.context
|
|
41
|
+
const { context: _, ...fetchInit } = (init ?? {}) as Record<string, unknown>
|
|
42
|
+
|
|
39
43
|
const challenge = Challenge.fromResponse(response)
|
|
40
44
|
|
|
41
45
|
const mi = methods.find((m) => m.name === challenge.method && m.intent === challenge.intent)
|
|
@@ -55,7 +59,7 @@ export function from<const methods extends readonly Method.AnyClient[]>(
|
|
|
55
59
|
return fetch(input, {
|
|
56
60
|
...fetchInit,
|
|
57
61
|
headers: {
|
|
58
|
-
...fetchInit.headers,
|
|
62
|
+
...normalizeHeaders(fetchInit.headers),
|
|
59
63
|
Authorization: credential,
|
|
60
64
|
},
|
|
61
65
|
})
|
|
@@ -64,9 +68,9 @@ export function from<const methods extends readonly Method.AnyClient[]>(
|
|
|
64
68
|
|
|
65
69
|
/** Union of all context types from all methods that have context schemas. */
|
|
66
70
|
type AnyContextFor<methods extends readonly Method.AnyClient[]> = {
|
|
67
|
-
[K in keyof methods]: methods[K] extends
|
|
68
|
-
?
|
|
69
|
-
? z.input<
|
|
71
|
+
[K in keyof methods]: NonNullable<methods[K]['context']> extends infer ctx
|
|
72
|
+
? ctx extends z.ZodMiniType
|
|
73
|
+
? z.input<ctx>
|
|
70
74
|
: undefined
|
|
71
75
|
: undefined
|
|
72
76
|
}[number]
|
|
@@ -123,7 +127,7 @@ export declare namespace from {
|
|
|
123
127
|
export function polyfill<const methods extends readonly Method.AnyClient[]>(
|
|
124
128
|
config: polyfill.Config<methods>,
|
|
125
129
|
): void {
|
|
126
|
-
originalFetch = globalThis.fetch
|
|
130
|
+
if (!originalFetch) originalFetch = globalThis.fetch
|
|
127
131
|
globalThis.fetch = from(config) as typeof globalThis.fetch
|
|
128
132
|
}
|
|
129
133
|
|
|
@@ -153,6 +157,20 @@ export function restore(): void {
|
|
|
153
157
|
}
|
|
154
158
|
}
|
|
155
159
|
|
|
160
|
+
/** @internal Normalizes headers to a plain object for spreading. */
|
|
161
|
+
function normalizeHeaders(headers: unknown): Record<string, string> {
|
|
162
|
+
if (!headers) return {}
|
|
163
|
+
if (headers instanceof Headers) {
|
|
164
|
+
const result: Record<string, string> = {}
|
|
165
|
+
headers.forEach((value, key) => {
|
|
166
|
+
result[key] = value
|
|
167
|
+
})
|
|
168
|
+
return result
|
|
169
|
+
}
|
|
170
|
+
if (Array.isArray(headers)) return Object.fromEntries(headers)
|
|
171
|
+
return headers as Record<string, string>
|
|
172
|
+
}
|
|
173
|
+
|
|
156
174
|
/** @internal */
|
|
157
175
|
async function resolveCredential(
|
|
158
176
|
challenge: Challenge.Challenge,
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from 'node:crypto'
|
|
2
|
+
|
|
1
3
|
/** Constant-time string comparison to prevent timing attacks. */
|
|
2
4
|
export function constantTimeEqual(a: string, b: string): boolean {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return result === 0
|
|
5
|
+
const hashA = createHash('sha256').update(a).digest()
|
|
6
|
+
const hashB = createHash('sha256').update(b).digest()
|
|
7
|
+
return timingSafeEqual(hashA, hashB)
|
|
7
8
|
}
|
package/src/internal/env.test.ts
CHANGED
|
@@ -9,8 +9,8 @@ describe('Env.get', () => {
|
|
|
9
9
|
expect(Env.get('realm')).toBe('MPP Payment')
|
|
10
10
|
})
|
|
11
11
|
|
|
12
|
-
test('returns
|
|
13
|
-
expect(Env.get('secretKey')).
|
|
12
|
+
test('returns undefined when MPP_SECRET_KEY is not set', () => {
|
|
13
|
+
expect(Env.get('secretKey')).toBeUndefined()
|
|
14
14
|
})
|
|
15
15
|
|
|
16
16
|
test('returns MPP_SECRET_KEY when set', () => {
|
package/src/internal/env.ts
CHANGED
|
@@ -17,13 +17,12 @@ const variables = {
|
|
|
17
17
|
/** Fallback values when no environment variable is set. */
|
|
18
18
|
const defaults = {
|
|
19
19
|
realm: 'MPP Payment',
|
|
20
|
-
|
|
21
|
-
} as const satisfies Record<keyof typeof variables, string>
|
|
20
|
+
} as const satisfies Partial<Record<keyof typeof variables, string>>
|
|
22
21
|
|
|
23
22
|
/**
|
|
24
23
|
* Resolves a configuration value from environment variables.
|
|
25
24
|
*
|
|
26
|
-
* Checks platform-specific env vars in order, falling back to a default.
|
|
25
|
+
* Checks platform-specific env vars in order, falling back to a default if one exists.
|
|
27
26
|
*
|
|
28
27
|
* @example
|
|
29
28
|
* ```ts
|
|
@@ -31,12 +30,12 @@ const defaults = {
|
|
|
31
30
|
* Env.get('secretKey') // e.g. value of MPP_SECRET_KEY
|
|
32
31
|
* ```
|
|
33
32
|
*/
|
|
34
|
-
export function get(key: keyof typeof variables): string {
|
|
33
|
+
export function get(key: keyof typeof variables): string | undefined {
|
|
35
34
|
for (const name of variables[key]) {
|
|
36
35
|
const value = read(name)
|
|
37
36
|
if (value) return value
|
|
38
37
|
}
|
|
39
|
-
return defaults[key]
|
|
38
|
+
return (defaults as Record<string, string | undefined>)[key]
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
/** Reads a single environment variable, probing available runtime APIs. */
|
|
@@ -21,6 +21,8 @@ function createServer(app: express.Express) {
|
|
|
21
21
|
})
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
const secretKey = 'test-secret-key'
|
|
25
|
+
|
|
24
26
|
describe('charge', () => {
|
|
25
27
|
const mppx = Mppx.create({
|
|
26
28
|
methods: [
|
|
@@ -30,6 +32,7 @@ describe('charge', () => {
|
|
|
30
32
|
recipient: accounts[0].address,
|
|
31
33
|
}),
|
|
32
34
|
],
|
|
35
|
+
secretKey,
|
|
33
36
|
})
|
|
34
37
|
|
|
35
38
|
const { fetch } = Mppx_client.create({
|
|
@@ -99,6 +102,7 @@ describe('session', () => {
|
|
|
99
102
|
escrowContract,
|
|
100
103
|
}),
|
|
101
104
|
],
|
|
105
|
+
secretKey,
|
|
102
106
|
})
|
|
103
107
|
|
|
104
108
|
const app = express()
|
|
@@ -125,6 +129,7 @@ describe('session', () => {
|
|
|
125
129
|
feePayer: accounts[0],
|
|
126
130
|
}),
|
|
127
131
|
],
|
|
132
|
+
secretKey,
|
|
128
133
|
})
|
|
129
134
|
|
|
130
135
|
const { fetch } = Mppx_client.create({
|
|
@@ -21,6 +21,8 @@ function createServer(app: Hono) {
|
|
|
21
21
|
})
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
const secretKey = 'test-secret-key'
|
|
25
|
+
|
|
24
26
|
describe('charge', () => {
|
|
25
27
|
const mppx = Mppx.create({
|
|
26
28
|
methods: [
|
|
@@ -30,6 +32,7 @@ describe('charge', () => {
|
|
|
30
32
|
recipient: accounts[0].address,
|
|
31
33
|
}),
|
|
32
34
|
],
|
|
35
|
+
secretKey,
|
|
33
36
|
})
|
|
34
37
|
|
|
35
38
|
const { fetch } = Mppx_client.create({
|
|
@@ -92,6 +95,7 @@ describe('session', () => {
|
|
|
92
95
|
escrowContract,
|
|
93
96
|
}),
|
|
94
97
|
],
|
|
98
|
+
secretKey,
|
|
95
99
|
})
|
|
96
100
|
|
|
97
101
|
const app = new Hono()
|
|
@@ -118,6 +122,7 @@ describe('session', () => {
|
|
|
118
122
|
feePayer: accounts[0],
|
|
119
123
|
}),
|
|
120
124
|
],
|
|
125
|
+
secretKey,
|
|
121
126
|
})
|
|
122
127
|
|
|
123
128
|
const { fetch } = Mppx_client.create({
|
|
@@ -23,8 +23,11 @@ export function wrap<mppx extends Mppx.Mppx<any, any>, handler>(
|
|
|
23
23
|
): Wrap<mppx, handler> {
|
|
24
24
|
const result: Record<string, unknown> = { ...mppx }
|
|
25
25
|
for (const mi of mppx.methods as readonly Method.AnyServer[]) {
|
|
26
|
-
const
|
|
27
|
-
|
|
26
|
+
const key = `${mi.name}/${mi.intent}`
|
|
27
|
+
const methodFn = (mppx as any)[key]
|
|
28
|
+
result[key] = (options: any) => wrapper(methodFn, options)
|
|
29
|
+
// Also set shorthand intent key if Mppx registered it (no collision)
|
|
30
|
+
if ((mppx as any)[mi.intent]) result[mi.intent] = (options: any) => wrapper(methodFn, options)
|
|
28
31
|
}
|
|
29
32
|
return result as never
|
|
30
33
|
}
|
|
@@ -33,6 +33,8 @@ function createServer(handler: (request: Request) => Promise<Response> | Respons
|
|
|
33
33
|
})
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
const secretKey = 'test-secret-key'
|
|
37
|
+
|
|
36
38
|
describe('charge', () => {
|
|
37
39
|
const mppx = Mppx.create({
|
|
38
40
|
methods: [
|
|
@@ -42,6 +44,7 @@ describe('charge', () => {
|
|
|
42
44
|
recipient: accounts[0].address,
|
|
43
45
|
}),
|
|
44
46
|
],
|
|
47
|
+
secretKey,
|
|
45
48
|
})
|
|
46
49
|
|
|
47
50
|
const { fetch } = Mppx_client.create({
|
|
@@ -106,6 +109,7 @@ describe('session', () => {
|
|
|
106
109
|
escrowContract,
|
|
107
110
|
}),
|
|
108
111
|
],
|
|
112
|
+
secretKey,
|
|
109
113
|
})
|
|
110
114
|
|
|
111
115
|
const handler = mppx.session({ amount: '1', unitType: 'token' })(() =>
|
|
@@ -131,6 +135,7 @@ describe('session', () => {
|
|
|
131
135
|
feePayer: accounts[0],
|
|
132
136
|
}),
|
|
133
137
|
],
|
|
138
|
+
secretKey,
|
|
134
139
|
})
|
|
135
140
|
|
|
136
141
|
const { fetch } = Mppx_client.create({
|
package/src/proxy/Proxy.test.ts
CHANGED
|
@@ -9,6 +9,8 @@ import * as Service from './Service.js'
|
|
|
9
9
|
import { anthropic } from './services/anthropic.js'
|
|
10
10
|
import { openai } from './services/openai.js'
|
|
11
11
|
|
|
12
|
+
const secretKey = 'test-secret-key'
|
|
13
|
+
|
|
12
14
|
const mppx_server = Mppx_server.create({
|
|
13
15
|
methods: [
|
|
14
16
|
tempo_server({
|
|
@@ -18,6 +20,7 @@ const mppx_server = Mppx_server.create({
|
|
|
18
20
|
feePayer: true,
|
|
19
21
|
}),
|
|
20
22
|
],
|
|
23
|
+
secretKey,
|
|
21
24
|
})
|
|
22
25
|
|
|
23
26
|
const mppx_client = Mppx_client.create({
|
|
@@ -10,6 +10,8 @@ import { openai } from './openai.js'
|
|
|
10
10
|
const apiKey = process.env.VITE_OPENAI_API_KEY
|
|
11
11
|
if (!apiKey) console.warn('OPENAI_API_KEY not set — openai proxy tests will be skipped')
|
|
12
12
|
|
|
13
|
+
const secretKey = 'test-secret-key'
|
|
14
|
+
|
|
13
15
|
const mppx_server = Mppx_server.create({
|
|
14
16
|
methods: [
|
|
15
17
|
tempo_server({
|
|
@@ -18,6 +20,7 @@ const mppx_server = Mppx_server.create({
|
|
|
18
20
|
getClient: () => client,
|
|
19
21
|
}),
|
|
20
22
|
],
|
|
23
|
+
secretKey,
|
|
21
24
|
})
|
|
22
25
|
|
|
23
26
|
const mppx_client = Mppx_client.create({
|