2
2
3
3
import assert from 'node:assert'
4
4
import { promisify } from 'node:util'
5
+ import { AsyncLocalStorage } from 'node:async_hooks'
5
6
6
7
import express from 'express'
7
8
import FormData from 'form-data'
@@ -10,6 +11,7 @@ import _onFinished from 'on-finished'
10
11
11
12
import * as util from './_util.js'
12
13
import multer from '../index.js'
14
+ import http from 'node:http'
13
15
14
16
const onFinished = promisify ( _onFinished )
15
17
@@ -105,4 +107,49 @@ describe('Express Integration', () => {
105
107
assert . strictEqual ( result . body . toString ( ) , 'SUCCESS' )
106
108
assert . strictEqual ( result . res . statusCode , 200 )
107
109
} )
110
+
111
+ it ( 'should handle async local storage' , async ( ) => {
112
+ const upload = multer ( )
113
+ const router = new express . Router ( )
114
+ const form = new FormData ( )
115
+
116
+ const als = new AsyncLocalStorage ( )
117
+
118
+ form . append ( 'avatar' , util . file ( 'large' ) )
119
+
120
+ router . use ( ( _req , _res , next ) => {
121
+ als . run ( { hello : 'world' } , ( ) => {
122
+ next ( )
123
+ } )
124
+ } )
125
+
126
+ router . post ( '/profile' , upload . single ( 'avatar' ) , ( _ , res ) => {
127
+ res . status ( 200 ) . end ( 'SUCCESS' )
128
+ } )
129
+
130
+ router . get ( '/hello' , ( _ , res ) => {
131
+ const store = als . getStore ( )
132
+ res . status ( 200 ) . json ( store )
133
+ } )
134
+
135
+ app . use ( '/t3' , router )
136
+
137
+ const result = await submitForm ( form , '/t3/profile' )
138
+
139
+ assert . strictEqual ( result . body . toString ( ) , 'SUCCESS' )
140
+ assert . strictEqual ( result . res . statusCode , 200 )
141
+
142
+ http . get ( `http://localhost:${ port } /t3/hello` , ( res ) => {
143
+ let data = ''
144
+
145
+ res . on ( 'data' , ( chunk ) => {
146
+ data += chunk
147
+ } )
148
+
149
+ res . on ( 'end' , ( ) => {
150
+ const store = JSON . parse ( data )
151
+ assert . deepStrictEqual ( store , { hello : 'world' } )
152
+ } )
153
+ } )
154
+ } )
108
155
} )
0 commit comments