-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathFetchOpaqueInterceptor.ts
More file actions
41 lines (36 loc) · 1.45 KB
/
FetchOpaqueInterceptor.ts
File metadata and controls
41 lines (36 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// const { AsyncLocalStorage } = require('node:async_hooks');
import { AsyncLocalStorage } from 'node:async_hooks';
import symbols from './symbols.js';
import { Dispatcher } from 'undici';
// const RedirectHandler = require('../handler/redirect-handler')
export interface FetchOpaque {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
[symbols.kRequestId]: number;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
[symbols.kRequestStartTime]: number;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
[symbols.kEnableRequestTiming]: number;
}
// const internalOpaque = {
// [symbols.kRequestId]: requestId,
// [symbols.kRequestStartTime]: requestStartTime,
// [symbols.kEnableRequestTiming]: !!(init.timing ?? true),
// [symbols.kRequestTiming]: timing,
// // [symbols.kRequestOriginalOpaque]: originalOpaque,
// };
export interface OpaqueInterceptorOptions {
opaqueLocalStorage: AsyncLocalStorage<FetchOpaque>;
}
export function fetchOpaqueInterceptor(opts: OpaqueInterceptorOptions) {
const opaqueLocalStorage = opts?.opaqueLocalStorage;
return (dispatch: Dispatcher['dispatch']): Dispatcher['dispatch'] => {
return function redirectInterceptor(opts: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandler) {
const opaque = opaqueLocalStorage?.getStore();
(handler as any).opaque = opaque;
return dispatch(opts, handler);
};
};
}