Spaces:
Runtime error
Runtime error
File size: 4,117 Bytes
23ac194 | 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 | 'use strict'
const http = require('node:http')
const Request = require('../lib/request')
const Response = require('../lib/response')
const inject = require('..')
const parseURL = require('../lib/parse-url')
const { Readable } = require('node:stream')
const { assert } = require('node:console')
const { Bench } = require('tinybench')
const suite = new Bench()
const mockReq = {
url: 'http://localhost',
method: 'GET',
headers: {
foo: 'bar',
'content-type': 'html',
accepts: 'json',
authorization: 'granted'
}
}
const mockCustomReq = {
url: 'http://localhost',
method: 'GET',
headers: {
foo: 'bar',
'content-type': 'html',
accepts: 'json',
authorization: 'granted'
},
Request: http.IncomingMessage
}
const mockReqCookies = {
url: 'http://localhost',
method: 'GET',
cookies: { foo: 'bar', grass: 'àìùòlé' },
headers: {
foo: 'bar',
'content-type': 'html',
accepts: 'json',
authorization: 'granted'
}
}
const mockReqCookiesPayload = {
url: 'http://localhost',
method: 'GET',
headers: {
foo: 'bar',
'content-type': 'html',
accepts: 'json',
authorization: 'granted'
},
payload: {
foo: { bar: 'fiz' },
bim: { bar: { boom: 'paf' } }
}
}
const mockReqCookiesPayloadBuffer = {
url: 'http://localhost',
method: 'GET',
headers: {
foo: 'bar',
'content-type': 'html',
accepts: 'json',
authorization: 'granted'
},
payload: Buffer.from('foo')
}
const mockReqCookiesPayloadReadable = () => ({
url: 'http://localhost',
method: 'GET',
headers: {
foo: 'bar',
'content-type': 'html',
accepts: 'json',
authorization: 'granted'
},
payload: Readable.from(['foo', 'bar', 'baz'])
})
suite
.add('Request', function () {
new Request(mockReq) // eslint-disable-line no-new
})
.add('Custom Request', function () {
new Request.CustomRequest(mockCustomReq) // eslint-disable-line no-new
})
.add('Request With Cookies', function () {
new Request(mockReqCookies) // eslint-disable-line no-new
})
.add('Request With Cookies n payload', function () {
new Request(mockReqCookiesPayload) // eslint-disable-line no-new
})
.add('ParseUrl', function () {
parseURL('http://example.com:8080/hello')
})
.add('ParseUrl and query', function () {
parseURL('http://example.com:8080/hello', {
foo: 'bar',
message: 'OK',
xs: ['foo', 'bar']
})
})
.add('read request body JSON', function () {
return new Promise((resolve) => {
const req = new Request(mockReqCookiesPayload)
req.prepare(() => {
req.on('data', () => {})
req.on('end', resolve)
})
})
})
.add('read request body buffer', function () {
return new Promise((resolve) => {
const req = new Request(mockReqCookiesPayloadBuffer)
req.prepare(() => {
req.on('data', () => {})
req.on('end', resolve)
})
})
})
.add('read request body readable', function () {
return new Promise((resolve) => {
const req = new Request(mockReqCookiesPayloadReadable())
req.prepare(() => {
req.on('data', () => {})
req.on('end', resolve)
})
})
})
.add('Response write end', function () {
const req = new Request(mockReq)
return new Promise((resolve) => {
const res = new Response(req, resolve)
res.write('foo')
res.end()
})
})
.add('Response writeHead end', function () {
const req = new Request(mockReq)
return new Promise((resolve) => {
const res = new Response(req, resolve)
res.writeHead(400, { 'content-length': 200 })
res.end()
})
})
.add('base inject', async function () {
const d = await inject((req, res) => {
req.on('data', () => {})
req.on('end', () => { res.end('1') })
}, mockReqCookiesPayload)
assert(d.payload === '1')
})
.run()
.then((tasks) => {
const errors = tasks.map(t => t.result?.error).filter((t) => t)
if (errors.length) {
errors.map((e) => console.error(e))
} else {
console.table(suite.table())
}
})
|