Skip to content

Commit

Permalink
much cleaner api
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Mar 2, 2018
1 parent 3f34d92 commit f23c0d8
Show file tree
Hide file tree
Showing 4 changed files with 379 additions and 69 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ On my laptop I can serve simple hello world payloads at around 100k requests/sec
const turbo = require('turbo-http')

const server = turbo.createServer(function (req, res) {
res.setStatus(200, 'OK')
res.setHeader('Content-Length', '11')
res.write(Buffer.from('hello world'))
})
Expand All @@ -35,7 +34,7 @@ Create a new http server. Inherits from [the turbo-net tcp server](https://githu

Emitted when a new http request is received.

#### `res.setStatus(code, msg)`
#### `res.statusCode = code`

Set the http status

Expand All @@ -49,6 +48,14 @@ Write a buffer. When the callback is called, the buffer
has been *completely* flushed to the underlying socket and is safe to
reuse for other purposes

#### `res.writev(buffers, [lengths], [callback])`

Write more that one buffer at once.

#### `res.end([buf], [length], [callback]`)

End the request. Only needed if you do not provide a `Content-Length`.

#### `req.url`

Request url
Expand All @@ -57,6 +64,14 @@ Request url

Request method

#### `value = req.getHeader(name)`

Get a request header.

#### `headers = req.getAllHeaders()`

Get all request headers as a map.

#### `req.ondata(buffer, start, length)`

Called when there is data read. If you use the buffer outside of this function
Expand Down
18 changes: 3 additions & 15 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
const turbo = require('./')

const reply = Buffer.from(`
HTTP/1.1 200 OK
Content-Length: 11
Connection: keep-alive
hello world
`.trim().replace(/\r?\n/g, '\r\n'))

const hello = Buffer.from('hello world')
var headers = null
const hello = Buffer.from('hello world\n')

turbo.createServer(function (req, res) {
req.onend = function () {
res.setStatus(200, 'OK')
res.setHeader('Content-Length', '11')
res.write(hello)
}
res.setHeader('Content-Length', hello.length)
res.write(hello)
}).listen(8080)
69 changes: 69 additions & 0 deletions http-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const status = module.exports = new Array(512)

set(100, 'Continue')
set(101, 'Switching Protocols')
set(102, 'Processing')
set(103, 'Early Hints')
set(200, 'OK')
set(201, 'Created')
set(202, 'Accepted')
set(203, 'Non-Authoritative Information')
set(204, 'No Content')
set(205, 'Reset Content')
set(206, 'Partial Content')
set(207, 'Multi-Status')
set(208, 'Already Reported')
set(226, 'IM Used')
set(300, 'Multiple Choices')
set(301, 'Moved Permanently')
set(302, 'Found')
set(303, 'See Other')
set(304, 'Not Modified')
set(305, 'Use Proxy')
set(307, 'Temporary Redirect')
set(308, 'Permanent Redirect')
set(400, 'Bad Request')
set(401, 'Unauthorized')
set(402, 'Payment Required')
set(403, 'Forbidden')
set(404, 'Not Found')
set(405, 'Method Not Allowed')
set(406, 'Not Acceptable')
set(407, 'Proxy Authentication Required')
set(408, 'Request Timeout')
set(409, 'Conflict')
set(410, 'Gone')
set(411, 'Length Required')
set(412, 'Precondition Failed')
set(413, 'Payload Too Large')
set(414, 'URI Too Long')
set(415, 'Unsupported Media Type')
set(416, 'Range Not Satisfiable')
set(417, 'Expectation Failed')
set(418, 'I\'m a teapot')
set(421, 'Misdirected Request')
set(422, 'Unprocessable Entity')
set(423, 'Locked')
set(424, 'Failed Dependency')
set(425, 'Unordered Collection')
set(426, 'Upgrade Required')
set(428, 'Precondition Required')
set(429, 'Too Many Requests')
set(431, 'Request Header Fields Too Large')
set(451, 'Unavailable For Legal Reasons')
set(500, 'Internal Server Error')
set(501, 'Not Implemented')
set(502, 'Bad Gateway')
set(503, 'Service Unavailable')
set(504, 'Gateway Timeout')
set(505, 'HTTP Version Not Supported')
set(506, 'Variant Also Negotiates')
set(507, 'Insufficient Storage')
set(508, 'Loop Detected')
set(509, 'Bandwidth Limit Exceeded')
set(510, 'Not Extended')
set(511, 'Network Authentication Required')

function set (code, msg) {
status[code] = Buffer.from('HTTP/1.1 ' + code + ' ' + msg + '\r\n')
}
Loading

0 comments on commit f23c0d8

Please sign in to comment.