I'm not sure if it does something for Polka, but take a look at: https://github.com/mafintosh/turbo-http
Hey,
It wouldn't be replaced, but you could use it instead of native HTTP server. There's a few examples that already show how you can do this with https server and uws server.
In fact, there's a branch here that already has a turbo example.
Be warned though -- turbo is still heavily alpha and doesn't claim (or try) 100% compatibility with Node.js HTTP. This means that a lot of middleware or other http-based packages won't work with it.
If you're looking to swap, I'd personally go for UWS' http server. It's incomplete, but at least it tries to be compatible. It's also 20k req/s faster on my machine.
Updated with links~! Going to go ahead & close this since there _is_ a way to do this without changing Polka code.
Thanks!
Thank you for this great investigation!
No problemo~!
@lukeed What about having a separate branch for turbo-http?
npm install --save polka#turbo
@DanielSokil there already is an example that shows how you can incorporate turbo. Polka won't offer an alternate version of itself just to accommodate turbo 1) because it already allows custom servers no problem and 2) turbo is extremely buggy.
Here's old example: https://github.com/lukeed/polka/blob/example/turbo/examples/with-turbo-http/index.js
Current and ongoing versions of Polka will always allow you to do this:
const polka = require('polka');
const server = /* init server, eg, turbo */;
polka({ server }).listen()
@lukeed Thank You, I did not know I could do this.
No problem 👍 bummer though, sounds like a documentation problem 😣 Would you say it's because the readme is too long?
How did I miss https://github.com/lukeed/polka#optionsserver this!?
The readme is the longest I have ever seen (:
I think grouping by color would help a lot when going through docs: https://stackoverflow.com/a/41247934
@lukeed I see you have mentioned uws in this issue, but since it has been depreciated. I was wondering if you came across uWebSockets.js?
When trying it out using polka as a handler, I'm getting this issue:
/home/danielsokil/Desktop/Lab/Node/uws-demo/node_modules/polka/index.js:72
let fns=[], arr=this.wares, obj=this.find(req.method, info.pathname);
^
TypeError: Cannot read property 'pathname' of undefined
at Polka.handler (/home/danielsokil/Desktop/Lab/Node/uws-demo/node_modules/polka/index.js:72:62)
at uWS.App.any (/home/danielsokil/Desktop/Lab/Node/uws-demo/src/both.js:27:10)
node: ../deps/uv/src/uv-common.c:686: uv_loop_delete: Assertion `err == 0' failed.
Aborted (core dumped)
const uWS = require('uWebSockets.js');
const polka = require('polka');
const port = 8000;
function one(req, res, next) {
req.one = true;
next();
}
function two(req, res, next) {
req.two = true;
next();
}
const { handler } = polka()
.use(one, two)
.get('/favicon.ico', _ => { })
.get('/', (req, res) => res.end('Hello'))
.get('/user/:id', (req, res) => {
res.end(`User: ${req.params.id}`);
});
uWS.App({
/* cert, key and such specified here, or use uWS.App */
}).any('/*', (res, req) => {
return handler(res, req);
}).listen(port, (token) => {
if (token) {
console.log('Listening to port ' + port);
} else {
console.log('Failed to listen to port ' + port);
}
});
My conclusion is that uWebSockets.js is not compatible with express API so they won't be able to work together. Do you think that's the issue?
Hahah, the taskr readme is longer :P
Thanks, good to know~! I'd never seen that before.
Polka will be getting a docs website for its 1.0 – it's WIP now.
It looks like you accidentally swapped req and res in the handler:
- return handler(res, req);
+ return handler(req, res);
I have tried your suggestion, it doesn't resolve the issue.
But I noticed that handler takes in 3 parameters req, res, info, and I only provided 2.
@lukeed Any ideas?
Do you get any errors this time? How does it not work?
TBH, the new uws has been on my radar & I really wanna play with it but haven't had time to yet. I know its implementing a custom HTTP engine, so I'm not sure if it's included a 1:1 match of what Polka expects to be in its IncomingRequest implementation.
The third argument handles itself so that shouldn't be an issue.
The error is the same as above:
/home/danielsokil/Desktop/Lab/Node/uws-demo/node_modules/.registry.npmjs.org/polka/0.5.1/node_modules/polka/index.js:72
let fns=[], arr=this.wares, obj=this.find(req.method, info.pathname);
^
TypeError: Cannot read property 'pathname' of undefined
at Polka.handler (/home/danielsokil/Desktop/Lab/Node/uws-demo/node_modules/.registry.npmjs.org/polka/0.5.1/node_modules/polka/index.js:72:62)
at uWS.App.any (/home/danielsokil/Desktop/Lab/Node/uws-demo/src/both.js:26:28)
node: ../deps/uv/src/uv-common.c:686: uv_loop_delete: Assertion `err == 0' failed.
Aborted (core dumped)
I have spotted that @polka/url requires req.url, but uWS does not have this API.
That's...odd? What does uws supply instead?
We can make it work, but this is definitely a case of its custom HTTP engine not matching the Node APIs
These are the only req methods that I could find in the uWS repo:
req.getMethod()
req.getHeader()
req.getParameter()
req.getUrl()
and res:
res.end()
res.close()
res.onData()
res.onAborted()
res.getWriteOffset()
res.tryEnd()
res.onWritable()
I have tried req.url = req.getUrl() and the server does not crash, but trying any route results in: Not Found
Cool, was going to suggest that. You'll have to add req.method support too
Most helpful comment
Updated with links~! Going to go ahead & close this since there _is_ a way to do this without changing Polka code.
Thanks!