22
33import assert from 'node:assert'
44import { promisify } from 'node:util'
5+ import { AsyncLocalStorage } from 'node:async_hooks'
56
67import express from 'express'
78import FormData from 'form-data'
@@ -10,6 +11,7 @@ import _onFinished from 'on-finished'
1011
1112import * as util from './_util.js'
1213import multer from '../index.js'
14+ import http from 'node:http'
1315
1416const onFinished = promisify ( _onFinished )
1517
@@ -105,4 +107,49 @@ describe('Express Integration', () => {
105107 assert . strictEqual ( result . body . toString ( ) , 'SUCCESS' )
106108 assert . strictEqual ( result . res . statusCode , 200 )
107109 } )
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+ } )
108155} )
0 commit comments