File size: 3,758 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* eslint-disable jest/valid-expect-in-promise */
import { createNext } from 'e2e-utils'
import { NextInstance } from 'e2e-utils'
import { fetchViaHTTP } from 'next-test-utils'

describe('edge api can use async local storage', () => {
  let next: NextInstance

  const cases = [
    {
      title: 'a single instance',
      code: `
        export const config = { runtime: 'edge' }
        const storage = new AsyncLocalStorage()
  
        export default async function handler(request) {
          const id = request.headers.get('req-id')
          return storage.run({ id }, async () => {
            await getSomeData()
            return Response.json(storage.getStore())
          })
        }
  
        async function getSomeData() {
          try {
            const response = await fetch('https://example.vercel.sh')
            await response.text()
          } finally {
            return true
          }
        }
      `,
      expectResponse: (response, id) =>
        expect(response).toMatchObject({ status: 200, json: { id } }),
    },
    {
      title: 'multiple instances',
      code: `
        export const config = { runtime: 'edge' }
        const topStorage = new AsyncLocalStorage()
  
        export default async function handler(request) {
          const id = request.headers.get('req-id')
          return topStorage.run({ id }, async () => {
            const nested = await getSomeData(id)
            return Response.json({ ...nested, ...topStorage.getStore() })
          })
        }
  
        async function getSomeData(id) {
          const nestedStorage = new AsyncLocalStorage()
          return nestedStorage.run('nested-' + id, async () => {
            try {
              const response = await fetch('https://example.vercel.sh')
              await response.text()
            } finally {
              return { nestedId: nestedStorage.getStore() }
            }
          })
        }
      `,
      expectResponse: (response, id) =>
        expect(response).toMatchObject({
          status: 200,
          json: { id: id, nestedId: `nested-${id}` },
        }),
    },
  ]

  afterEach(() => next.destroy())

  it.each(cases)(
    'cans use $title per request',
    async ({ code, expectResponse }) => {
      next = await createNext({
        files: {
          'pages/index.js': `
            export default function () { return <div>Hello, world!</div> }
          `,
          'pages/api/async.js': code,
        },
      })
      const ids = Array.from({ length: 100 }, (_, i) => `req-${i}`)

      const responses = await Promise.all(
        ids.map((id) =>
          fetchViaHTTP(
            next.url,
            '/api/async',
            {},
            { headers: { 'req-id': id } }
          ).then((response) =>
            response.headers.get('content-type')?.startsWith('application/json')
              ? response.json().then((json) => ({
                  status: response.status,
                  json,
                  text: null,
                }))
              : response.text().then((text) => ({
                  status: response.status,
                  json: null,
                  text,
                }))
          )
        )
      )
      const rankById = new Map(ids.map((id, rank) => [id, rank]))

      const errors: Error[] = []
      for (const [rank, response] of responses.entries()) {
        try {
          expectResponse(response, ids[rank])
        } catch (error) {
          const received = response.json?.id
          console.log(
            `response #${rank} has id from request #${rankById.get(received)}`
          )
          errors.push(error as Error)
        }
      }
      if (errors.length) {
        throw errors[0]
      }
    }
  )
})