File size: 3,616 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
import { nextTestSetup } from 'e2e-utils'
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
import path from 'path'
import { promises as fs } from 'fs'
import { readJson } from 'fs-extra'

describe('Edge Compiler can import asset assets', () => {
  const { next, isTurbopack, isNextDeploy } = nextTestSetup({
    files: path.join(__dirname, './app'),
  })

  it('allows to fetch a remote URL', async () => {
    const response = await fetchViaHTTP(next.url, '/api/edge', {
      handler: 'remote-full',
    })
    expect(await response.text()).toContain('Example Domain')
  })

  it('allows to fetch a remote URL with a path and basename', async () => {
    const response = await fetchViaHTTP(next.url, '/api/edge', {
      handler: 'remote-with-base',
    })
    expect(await response.text()).toContain('Example Domain')
  })

  it('allows to fetch text assets', async () => {
    const html = await renderViaHTTP(next.url, '/api/edge', {
      handler: 'text-file',
    })
    expect(html).toContain('Hello, from text-file.txt!')
  })

  it('allows to fetch image assets', async () => {
    const response = await fetchViaHTTP(next.url, '/api/edge', {
      handler: 'image-file',
    })
    const buffer: Buffer = await response.buffer()
    const image = await fs.readFile(
      path.join(__dirname, './app/src/vercel.png')
    )
    expect(buffer.equals(image)).toBeTrue()
  })

  it('allows to assets from node_modules', async () => {
    const response = await fetchViaHTTP(next.url, '/api/edge', {
      handler: 'from-node-module',
    })
    const json = await response.json()
    expect(json).toEqual({
      'i am': 'a node dependency',
    })
  })

  // Cannot read the file on deployment
  if (!isNextDeploy) {
    it('extracts all the assets from the bundle', async () => {
      const manifestPath = path.join(
        next.testDir,
        '.next/server/middleware-manifest.json'
      )
      const manifest = await readJson(manifestPath)
      const orderedAssets = manifest.functions['/api/edge'].assets.sort(
        (a, z) => {
          return String(a.name).localeCompare(z.name)
        }
      )

      if (isTurbopack) {
        expect(orderedAssets).toMatchObject([
          {
            name: expect.stringMatching(
              /server\/edge\/assets\/text-file\.[0-9a-f]{8}\.txt$/
            ),
            filePath: expect.stringMatching(/^server\/edge\/assets\/text-file/),
          },
          {
            name: expect.stringMatching(
              /^server\/edge\/assets\/vercel\.[0-9a-f]{8}\.png$/
            ),
            filePath: expect.stringMatching(/^server\/edge\/assets\/vercel/),
          },
          {
            name: expect.stringMatching(
              /^server\/edge\/assets\/world\.[0-9a-f]{8}\.json/
            ),
            filePath: expect.stringMatching(/^server\/edge\/assets\/world/),
          },
        ])
      } else {
        expect(orderedAssets).toMatchObject([
          {
            name: expect.stringMatching(/^text-file\.[0-9a-f]{16}\.txt$/),
            filePath: expect.stringMatching(
              /^server\/edge-chunks\/asset_text-file/
            ),
          },
          {
            name: expect.stringMatching(/^vercel\.[0-9a-f]{16}\.png$/),
            filePath: expect.stringMatching(
              /^server\/edge-chunks\/asset_vercel/
            ),
          },
          {
            name: expect.stringMatching(/^world\.[0-9a-f]{16}\.json/),
            filePath: expect.stringMatching(
              /^server\/edge-chunks\/asset_world/
            ),
          },
        ])
      }
    })
  }
})