1
1
import type { Request , Response } from "express" ;
2
- import { createHmac , randomBytes } from "node: crypto" ;
3
- import type { Transform } from "node: stream" ;
4
- import { Readable } from "node: stream" ;
2
+ import crypto from "crypto" ;
3
+ import type { Transform } from "stream" ;
4
+ import { Readable } from "stream" ;
5
5
import type { ComponentType } from "react" ;
6
6
import React from "react" ;
7
7
import type { PipeableStream } from "react-dom/server" ;
8
8
import { renderToPipeableStream } from "react-dom/server" ;
9
9
10
10
export const path = process . env . REACT_ESI_PATH || "/_fragment" ;
11
- const secret = process . env . REACT_ESI_SECRET || randomBytes ( 64 ) . toString ( "hex" ) ;
11
+ const secret =
12
+ process . env . REACT_ESI_SECRET || crypto . randomBytes ( 64 ) . toString ( "hex" ) ;
12
13
13
14
/**
14
15
* Signs the ESI URL with a secret key using the HMAC-SHA256 algorithm.
15
16
*/
16
17
function sign ( url : URL ) {
17
- const hmac = createHmac ( "sha256" , secret ) ;
18
+ const hmac = crypto . createHmac ( "sha256" , secret ) ;
18
19
hmac . update ( url . pathname + url . search ) ;
19
20
return hmac . digest ( "hex" ) ;
20
21
}
@@ -53,11 +54,11 @@ interface IServeFragmentOptions {
53
54
pipeStream ?: ( stream : PipeableStream ) => InstanceType < typeof Transform > ;
54
55
}
55
56
56
- type resolver <
57
+ type Resolver <
57
58
TProps =
58
59
| Record < string , unknown >
59
60
| Promise < unknown >
60
- | Promise < Record < string , unknown > >
61
+ | Promise < Record < string , unknown > > ,
61
62
> = (
62
63
fragmentID : string ,
63
64
props : object ,
@@ -72,7 +73,7 @@ type resolver<
72
73
export async function serveFragment < TProps > (
73
74
req : Request ,
74
75
res : Response ,
75
- resolve : resolver < TProps > ,
76
+ resolve : Resolver < TProps > ,
76
77
options : IServeFragmentOptions = { }
77
78
) {
78
79
const url = new URL ( req . url , "http://example.com" ) ;
@@ -100,7 +101,7 @@ export async function serveFragment<TProps>(
100
101
? await Component . getInitialProps ( {
101
102
props : baseChildProps ,
102
103
req,
103
- res
104
+ res,
104
105
} )
105
106
: baseChildProps ;
106
107
0 commit comments