@chubbyts/chubbyts-http-uwebsockets-bridge
Version:
A uwebsockets req/res http bridge.
83 lines (63 loc) • 5.04 kB
Markdown
# chubbyts-http-uwebsockets-bridge
[](https://github.com/chubbyts/chubbyts-http-uwebsockets-bridge/actions?query=workflow%3ACI)
[](https://coveralls.io/github/chubbyts/chubbyts-http-uwebsockets-bridge?branch=master)
[](https://dashboard.stryker-mutator.io/reports/github.com/chubbyts/chubbyts-http-uwebsockets-bridge/master)
[](https://www.npmjs.com/package/@chubbyts/chubbyts-http-uwebsockets-bridge)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-uwebsockets-bridge)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-uwebsockets-bridge)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-uwebsockets-bridge)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-uwebsockets-bridge)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-uwebsockets-bridge)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-uwebsockets-bridge)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-uwebsockets-bridge)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-uwebsockets-bridge)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-uwebsockets-bridge)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-uwebsockets-bridge)
[](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-uwebsockets-bridge)
## Description
A uwebsockets req/res http bridge.
## Requirements
* node: 18
* [@chubbyts/chubbyts-http-types][2]: ^3.0.0
* [uWebSockets.js][3]: github:uNetworking/uWebSockets.js#v20.52.0
## Installation
Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-http-uwebsockets-bridge][1].
```ts
npm i @chubbyts/chubbyts-http-uwebsockets-bridge@^2.0.1
```
## Usage
```ts
import {
createServerRequestFactory,
createStreamFromResourceFactory,
createUriFactory,
} from '@chubbyts/chubbyts-http/dist/message-factory';
import { createResponseToUwebsocketsEmitter, createUwebsocketsToServerRequestFactory } from '@chubbyts/chubbyts-http-uwebsockets-bridge/dist/uwebsocket-http';
import { App, HttpRequest, HttpResponse } from 'uWebSockets.js';
const app = ...;
const uwebsocketsToServerRequestFactory = createUwebsocketsToServerRequestFactory(
createUriFactory(),
createServerRequestFactory(),
createStreamFromResourceFactory(),
);
const responseToUwebsocketsEmitter = createResponseToUwebsocketsEmitter();
const host = '0.0.0.0';
const port = 8080;
App()
.any('/*', async (res: HttpResponse, req: HttpRequest) => {
// function gets excuted on abort
// empty function means the request/response gets executed to its end
res.onAborted(() => {});
responseToUwebsocketsEmitter(await app(uwebsocketsToServerRequestFactory(req, res)), res);
})
.listen(host, port, (listenSocket: unknown) => {
if (listenSocket) {
console.log(`Listening to ${host}:${port}`);
}
});
```
## Copyright
2025 Dominik Zogg
[1]: https://www.npmjs.com/package/@chubbyts/chubbyts-http-uwebsockets-bridge
[2]: https://www.npmjs.com/package/@chubbyts/chubbyts-http-types
[3]: https://github.com/uNetworking/uWebSockets.js