File size: 3,124 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
// Unsupported in edge
import { Writable } from 'stream'
import path from 'path'
import crypto from 'crypto'
import vm from 'vm'
import constants from 'constants'
import domain from 'domain'
import http from 'http'
import https from 'https'
import os from 'os'
// TODO: These are accidentally polyfilled in edge runtime currently.
// import punycode from 'punycode'
// import process from 'process'
// import querystring from 'querystring'
// import stringDecoder from 'string_decoder'
import sys from 'sys'
import timers from 'timers'
import tty from 'tty'
import zlib from 'zlib'
import 'setimmediate'
// Supported in edge
import { Buffer } from 'buffer'
import assert from 'assert'
import util from 'util'
import { EventEmitter } from 'events'

// Other imports
import { NextResponse } from 'next/server'

export default async function middleware(request) {
  if (request.nextUrl.pathname !== '/middleware-test') {
    return
  }
  const response = NextResponse.next()

  let emitted = false
  class MyEmitter extends EventEmitter {}
  const myEmitter = new MyEmitter()
  // Only do this once so we don't loop forever
  myEmitter.once('myEvent', (_event, _listener) => {
    emitted = true
  })
  myEmitter.emit('myEvent')

  assert.ok(emitted)
  assert.ok(!!util.promisify)
  assert.ok(true)

  const supportedResult = {
    assert: true,
    buffer: Buffer.from('hello world').toString('utf8'),
    eventEmitter: true,
    util: true,
  }

  response.headers.set('supported-result', JSON.stringify(supportedResult))

  // TODO: These are accidentally polyfilled in edge runtime currently.
  // assert.throws(() => {
  //   console.log(punycode)
  // })
  // assert.throws(() => {
  //   console.log(process.title)
  // })
  // assert.throws(() => {
  //   console.log(querystring)
  // })
  // assert.throws(() => {
  //   console.log(stringDecoder)
  // })

  assert.throws(() => {
    console.log(domain)
  })
  assert.throws(() => {
    console.log(http)
  })
  assert.throws(() => {
    console.log(https)
  })
  assert.throws(() => {
    console.log(zlib.Gzip)
  })
  assert.throws(() => {
    console.log(constants.E2BIG)
  })
  assert.throws(() => {
    console.log(crypto.createHash)
  })
  assert.throws(() => {
    console.log(os.hostname)
  })
  assert.throws(() => {
    console.log(path.join)
  })
  assert.throws(() => {
    console.log(vm)
  })
  assert.throws(() => {
    console.log(tty.isatty)
  })
  assert.throws(() => {
    console.log(timers.clearImmediate)
  })
  assert.throws(() => {
    console.log(Writable)
  })
  assert.throws(() => {
    console.log(sys)
  })

  const unsupportedResult = {
    // TODO: these are accidentally polyfilled in edge runtime currently.
    // punycode: false,
    // process: false,
    // querystring: false,
    // stringDecoder: false,

    domain: false,
    http: false,
    https: false,
    zlib: false,
    constants: false,
    crypto: false,
    os: false,
    path: false,

    vm: false,
    tty: false,
    timers: false,
    stream: false,
  }

  response.headers.set('unsupported-result', JSON.stringify(unsupportedResult))

  return response
}