File size: 7,101 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { join } from 'path'
import { nextTestSetup } from 'e2e-utils'

describe('node builtins', () => {
  const { next } = nextTestSetup({
    files: join(__dirname, 'node-builtins'),
  })

  it('should have polyfilled node.js builtins for the browser correctly', async () => {
    const browser = await next.browser('/')

    await browser.waitForCondition('window.didRender', 15000)

    const data = await browser
      .waitForElementByCss('#node-browser-polyfills')
      .text()
    const parsedData = JSON.parse(data)

    expect(parsedData.vm).toBe(105)
    expect(parsedData.hash).toBe(
      'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'
    )
    expect(parsedData.buffer).toBe('hello world')
    expect(parsedData.stream).toBe(true)
    expect(parsedData.assert).toBe(true)
    expect(parsedData.constants).toBe(7)
    expect(parsedData.domain).toBe(true)
    expect(parsedData.http).toBe(true)
    expect(parsedData.https).toBe(true)
    expect(parsedData.os).toBe('\n')
    expect(parsedData.path).toBe('/hello/world/test.txt')
    expect(parsedData.process).toBe('browser')
    expect(parsedData.querystring).toBe('a=b')
    expect(parsedData.stringDecoder).toBe(true)
    expect(parsedData.sys).toBe(true)
    expect(parsedData.timers).toBe(true)
  })

  it('should have polyfilled node.js builtins for the browser correctly in client component', async () => {
    const browser = await next.browser('/client-component')

    await browser.waitForCondition('window.didRender', 15000)

    const data = await browser
      .waitForElementByCss('#node-browser-polyfills')
      .text()
    const parsedData = JSON.parse(data)

    expect(parsedData.vm).toBe(105)
    expect(parsedData.hash).toBe(
      'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'
    )
    expect(parsedData.buffer).toBe('hello world')
    expect(parsedData.stream).toBe(true)
    expect(parsedData.assert).toBe(true)
    expect(parsedData.constants).toBe(7)
    expect(parsedData.domain).toBe(true)
    expect(parsedData.http).toBe(true)
    expect(parsedData.https).toBe(true)
    expect(parsedData.os).toBe('\n')
    expect(parsedData.path).toBe('/hello/world/test.txt')
    expect(parsedData.process).toBe('browser')
    expect(parsedData.querystring).toBe('a=b')
    expect(parsedData.stringDecoder).toBe(true)
    expect(parsedData.sys).toBe(true)
    expect(parsedData.timers).toBe(true)
  })

  it('should support node.js builtins', async () => {
    const $ = await next.render$('/server')

    const data = $('#node-browser-polyfills').text()
    const parsedData = JSON.parse(data)

    expect(parsedData.vm).toBe(105)
    expect(parsedData.hash).toBe(
      'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'
    )
    expect(parsedData.buffer).toBe('hello world')
    expect(parsedData.stream).toBe(true)
    expect(parsedData.assert).toBe(true)
    expect(parsedData.constants).toBe(7)
    expect(parsedData.domain).toBe(true)
    expect(parsedData.http).toBe(true)
    expect(parsedData.https).toBe(true)
    expect(parsedData.os).toBe('\n')
    expect(parsedData.path).toBe('/hello/world/test.txt')
    expect(parsedData.process).toInclude('next-server')
    expect(parsedData.querystring).toBe('a=b')
    expect(parsedData.stringDecoder).toBe(true)
    expect(parsedData.sys).toBe(true)
    expect(parsedData.timers).toBe(true)
  })

  it('should support node.js builtins prefixed by node:', async () => {
    const $ = await next.render$('/server-node-schema')

    const data = $('#node-browser-polyfills').text()
    const parsedData = JSON.parse(data)

    expect(parsedData.vm).toBe(105)
    expect(parsedData.hash).toBe(
      'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'
    )
    expect(parsedData.buffer).toBe('hello world')
    expect(parsedData.stream).toBe(true)
    expect(parsedData.assert).toBe(true)
    expect(parsedData.constants).toBe(7)
    expect(parsedData.domain).toBe(true)
    expect(parsedData.http).toBe(true)
    expect(parsedData.https).toBe(true)
    expect(parsedData.os).toBe('\n')
    expect(parsedData.path).toBe('/hello/world/test.txt')
    expect(parsedData.process).toInclude('next-server')
    expect(parsedData.querystring).toBe('a=b')
    expect(parsedData.stringDecoder).toBe(true)
    expect(parsedData.sys).toBe(true)
    expect(parsedData.timers).toBe(true)
  })

  it('should support node.js builtins in server component', async () => {
    const $ = await next.render$('/server-component')

    const data = $('#node-browser-polyfills').text()
    const parsedData = JSON.parse(data)

    expect(parsedData.vm).toBe(105)
    expect(parsedData.hash).toBe(
      'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'
    )
    expect(parsedData.buffer).toBe('hello world')
    expect(parsedData.stream).toBe(true)
    expect(parsedData.assert).toBe(true)
    expect(parsedData.constants).toBe(7)
    expect(parsedData.domain).toBe(true)
    expect(parsedData.http).toBe(true)
    expect(parsedData.https).toBe(true)
    expect(parsedData.os).toBe('\n')
    expect(parsedData.path).toBe('/hello/world/test.txt')
    expect(parsedData.querystring).toBe('a=b')
    expect(parsedData.stringDecoder).toBe(true)
    expect(parsedData.sys).toBe(true)
    expect(parsedData.timers).toBe(true)
  })

  it('should support node.js builtins prefixed by node: in server component', async () => {
    const $ = await next.render$('/server-component/node-schema')

    const data = $('#node-browser-polyfills').text()
    const parsedData = JSON.parse(data)

    expect(parsedData.vm).toBe(105)
    expect(parsedData.hash).toBe(
      'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'
    )
    expect(parsedData.buffer).toBe('hello world')
    expect(parsedData.stream).toBe(true)
    expect(parsedData.assert).toBe(true)
    expect(parsedData.constants).toBe(7)
    expect(parsedData.domain).toBe(true)
    expect(parsedData.http).toBe(true)
    expect(parsedData.https).toBe(true)
    expect(parsedData.os).toBe('\n')
    expect(parsedData.path).toBe('/hello/world/test.txt')
    expect(parsedData.querystring).toBe('a=b')
    expect(parsedData.stringDecoder).toBe(true)
    expect(parsedData.sys).toBe(true)
    expect(parsedData.timers).toBe(true)
  })

  it('should throw when unsupported builtins are used in middleware', async () => {
    const res = await next.fetch('/middleware-test')
    expect(res.status).toBe(200)
    expect(JSON.parse(res.headers.get('supported-result')))
      .toMatchInlineSnapshot(`
      {
        "assert": true,
        "buffer": "hello world",
        "eventEmitter": true,
        "util": true,
      }
    `)
    expect(JSON.parse(res.headers.get('unsupported-result')))
      .toMatchInlineSnapshot(`
      {
        "constants": false,
        "crypto": false,
        "domain": false,
        "http": false,
        "https": false,
        "os": false,
        "path": false,
        "stream": false,
        "timers": false,
        "tty": false,
        "vm": false,
        "zlib": false,
      }
    `)
  })
})