undici 7.0.0-alpha.2 → 7.0.0-alpha.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 (37) hide show
  1. package/README.md +1 -1
  2. package/docs/docs/api/CacheStore.md +116 -0
  3. package/docs/docs/api/Dispatcher.md +10 -0
  4. package/index.js +6 -1
  5. package/lib/api/api-request.js +1 -1
  6. package/lib/api/readable.js +6 -6
  7. package/lib/cache/memory-cache-store.js +417 -0
  8. package/lib/core/constants.js +24 -1
  9. package/lib/core/util.js +41 -2
  10. package/lib/dispatcher/client-h1.js +100 -87
  11. package/lib/dispatcher/client-h2.js +127 -75
  12. package/lib/dispatcher/pool-base.js +3 -3
  13. package/lib/handler/cache-handler.js +359 -0
  14. package/lib/handler/cache-revalidation-handler.js +119 -0
  15. package/lib/interceptor/cache.js +171 -0
  16. package/lib/util/cache.js +224 -0
  17. package/lib/web/cache/cache.js +1 -0
  18. package/lib/web/cache/cachestorage.js +2 -0
  19. package/lib/web/eventsource/eventsource.js +2 -0
  20. package/lib/web/fetch/constants.js +12 -5
  21. package/lib/web/fetch/data-url.js +2 -2
  22. package/lib/web/fetch/formdata.js +3 -1
  23. package/lib/web/fetch/headers.js +2 -0
  24. package/lib/web/fetch/request.js +3 -1
  25. package/lib/web/fetch/response.js +3 -1
  26. package/lib/web/fetch/util.js +171 -47
  27. package/lib/web/fetch/webidl.js +16 -12
  28. package/lib/web/websocket/constants.js +67 -6
  29. package/lib/web/websocket/events.js +4 -0
  30. package/lib/web/websocket/stream/websocketerror.js +1 -1
  31. package/lib/web/websocket/websocket.js +2 -0
  32. package/package.json +7 -3
  33. package/types/cache-interceptor.d.ts +97 -0
  34. package/types/fetch.d.ts +9 -8
  35. package/types/index.d.ts +3 -0
  36. package/types/interceptors.d.ts +4 -0
  37. package/types/webidl.d.ts +7 -1
@@ -0,0 +1,97 @@
1
+ import { Readable, Writable } from 'node:stream'
2
+ import Dispatcher from './dispatcher'
3
+
4
+ export default CacheHandler
5
+
6
+ declare namespace CacheHandler {
7
+ export type CacheMethods = 'GET' | 'HEAD' | 'OPTIONS' | 'TRACE'
8
+
9
+ export interface CacheOptions {
10
+ store?: CacheStore
11
+
12
+ /**
13
+ * The methods to cache
14
+ * Note we can only cache safe methods. Unsafe methods (i.e. PUT, POST)
15
+ * invalidate the cache for a origin.
16
+ * @see https://www.rfc-editor.org/rfc/rfc9111.html#name-invalidating-stored-respons
17
+ * @see https://www.rfc-editor.org/rfc/rfc9110#section-9.2.1
18
+ */
19
+ methods?: CacheMethods[]
20
+ }
21
+
22
+ /**
23
+ * Underlying storage provider for cached responses
24
+ */
25
+ export interface CacheStore {
26
+ /**
27
+ * Whether or not the cache is full and can not store any more responses
28
+ */
29
+ get isFull(): boolean
30
+
31
+ createReadStream(req: Dispatcher.RequestOptions): CacheStoreReadable | Promise<CacheStoreReadable | undefined> | undefined
32
+
33
+ createWriteStream(req: Dispatcher.RequestOptions, value: Omit<CacheStoreValue, 'rawTrailers'>): CacheStoreWriteable | undefined
34
+
35
+ /**
36
+ * Delete all of the cached responses from a certain origin (host)
37
+ */
38
+ deleteByOrigin(origin: string): void | Promise<void>
39
+ }
40
+
41
+ export interface CacheStoreReadable extends Readable {
42
+ get value(): CacheStoreValue
43
+ }
44
+
45
+ export interface CacheStoreWriteable extends Writable {
46
+ set rawTrailers(rawTrailers: string[] | undefined)
47
+ }
48
+
49
+ export interface CacheStoreValue {
50
+ statusCode: number;
51
+ statusMessage: string;
52
+ rawHeaders: (Buffer | Buffer[])[];
53
+ rawTrailers?: string[];
54
+ /**
55
+ * Headers defined by the Vary header and their respective values for
56
+ * later comparison
57
+ */
58
+ vary?: Record<string, string>;
59
+ /**
60
+ * Time in millis that this value was cached
61
+ */
62
+ cachedAt: number;
63
+ /**
64
+ * Time in millis that this value is considered stale
65
+ */
66
+ staleAt: number;
67
+ /**
68
+ * Time in millis that this value is to be deleted from the cache. This is
69
+ * either the same as staleAt or the `max-stale` caching directive.
70
+ */
71
+ deleteAt: number;
72
+ }
73
+
74
+ export interface MemoryCacheStoreOpts {
75
+ /**
76
+ * @default Infinity
77
+ */
78
+ maxEntries?: number
79
+ /**
80
+ * @default Infinity
81
+ */
82
+ maxEntrySize?: number
83
+ errorCallback?: (err: Error) => void
84
+ }
85
+
86
+ export class MemoryCacheStore implements CacheStore {
87
+ constructor (opts?: MemoryCacheStoreOpts)
88
+
89
+ get isFull (): boolean
90
+
91
+ createReadStream (req: Dispatcher.RequestOptions): CacheStoreReadable | undefined
92
+
93
+ createWriteStream (req: Dispatcher.RequestOptions, value: CacheStoreValue): CacheStoreWriteable
94
+
95
+ deleteByOrigin (origin: string): void
96
+ }
97
+ }
package/types/fetch.d.ts CHANGED
@@ -119,20 +119,21 @@ type RequestDestination =
119
119
  | 'xslt'
120
120
 
121
121
  export interface RequestInit {
122
- method?: string
123
- keepalive?: boolean
124
- headers?: HeadersInit
125
122
  body?: BodyInit | null
126
- redirect?: RequestRedirect
127
- integrity?: string
128
- signal?: AbortSignal | null
123
+ cache?: RequestCache
129
124
  credentials?: RequestCredentials
125
+ dispatcher?: Dispatcher
126
+ duplex?: RequestDuplex
127
+ headers?: HeadersInit
128
+ integrity?: string
129
+ keepalive?: boolean
130
+ method?: string
130
131
  mode?: RequestMode
132
+ redirect?: RequestRedirect
131
133
  referrer?: string
132
134
  referrerPolicy?: ReferrerPolicy
135
+ signal?: AbortSignal | null
133
136
  window?: null
134
- dispatcher?: Dispatcher
135
- duplex?: RequestDuplex
136
137
  }
137
138
 
138
139
  export type ReferrerPolicy =
package/types/index.d.ts CHANGED
@@ -64,4 +64,7 @@ declare namespace Undici {
64
64
  const FormData: typeof import('./formdata').FormData
65
65
  const caches: typeof import('./cache').caches
66
66
  const interceptors: typeof import('./interceptors').default
67
+ const cacheStores: {
68
+ MemoryCacheStore: typeof import('./cache-interceptor').default.MemoryCacheStore
69
+ }
67
70
  }
@@ -1,3 +1,4 @@
1
+ import CacheHandler from './cache-interceptor'
1
2
  import Dispatcher from './dispatcher'
2
3
  import RetryHandler from './retry-handler'
3
4
  import { LookupOptions } from 'node:dns'
@@ -10,6 +11,8 @@ declare namespace Interceptors {
10
11
  export type RedirectInterceptorOpts = { maxRedirections?: number }
11
12
 
12
13
  export type ResponseErrorInterceptorOpts = { throwOnError: boolean }
14
+ export type CacheInterceptorOpts = CacheHandler.CacheOptions
15
+
13
16
  // DNS interceptor
14
17
  export type DNSInterceptorRecord = { address: string, ttl: number, family: 4 | 6 }
15
18
  export type DNSInterceptorOriginRecords = { 4: { ips: DNSInterceptorRecord[] } | null, 6: { ips: DNSInterceptorRecord[] } | null }
@@ -28,4 +31,5 @@ declare namespace Interceptors {
28
31
  export function redirect (opts?: RedirectInterceptorOpts): Dispatcher.DispatcherComposeInterceptor
29
32
  export function responseError (opts?: ResponseErrorInterceptorOpts): Dispatcher.DispatcherComposeInterceptor
30
33
  export function dns (opts?: DNSInterceptorOpts): Dispatcher.DispatcherComposeInterceptor
34
+ export function cache (opts?: CacheInterceptorOpts): Dispatcher.DispatcherComposeInterceptor
31
35
  }
package/types/webidl.d.ts CHANGED
@@ -84,7 +84,13 @@ interface WebidlUtil {
84
84
  */
85
85
  Stringify (V: any): string
86
86
 
87
- MakeTypeAssertion <T extends { prototype: T }>(Prototype: T['prototype']): (arg: any) => arg is T
87
+ MakeTypeAssertion <I>(I: I): (arg: any) => arg is I
88
+
89
+ /**
90
+ * Mark a value as uncloneable for Node.js.
91
+ * This is only effective in some newer Node.js versions.
92
+ */
93
+ markAsUncloneable (V: any): void
88
94
  }
89
95
 
90
96
  interface WebidlConverters {