This repository has been archived by the owner on Jul 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 11fcf18
Showing
19 changed files
with
937 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
pull_request: | ||
schedule: | ||
- cron: '0 0 * * *' | ||
|
||
jobs: | ||
node12: | ||
name: Node 12 | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v2 | ||
- name: checkout node | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '12' | ||
- run: npm install | ||
- run: npm run build | ||
- run: npm run cs | ||
- run: npm test -- --coverage --no-cache | ||
- run: npm run infection | ||
env: | ||
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} | ||
node14: | ||
name: Node 14 | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v2 | ||
- name: checkout node | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '14' | ||
- run: npm install | ||
- run: npm run build | ||
- run: npm run cs | ||
- run: npm test -- --coverage --no-cache | ||
- run: npm run infection | ||
env: | ||
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} | ||
node16: | ||
name: Node 16 | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v2 | ||
- name: checkout node | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '16' | ||
- run: npm install | ||
- run: npm run build | ||
- run: npm run cs | ||
- run: npm test -- --coverage --no-cache | ||
- run: npm run infection | ||
env: | ||
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} | ||
- name: coveralls.io | ||
uses: coverallsapp/github-action@master | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
- name: sonarcloud.io | ||
uses: sonarsource/sonarcloud-github-action@master | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
coverage | ||
dist | ||
node_modules | ||
package-lock.json | ||
|
||
# stryker temp files | ||
.stryker-tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2021 Dominik Zogg | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# chubbyjs-uwebsockets-psr-http-message-bridge | ||
|
||
[![CI](https://github.com/chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge/workflows/CI/badge.svg?branch=master)](https://github.com/chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge/actions?query=workflow%3ACI) | ||
[![Coverage Status](https://coveralls.io/repos/github/chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge/badge.svg?branch=master)](https://coveralls.io/github/chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge?branch=master) | ||
[![Infection MSI](https://badge.stryker-mutator.io/github.com/chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge/master)](https://dashboard.stryker-mutator.io/reports/github.com/chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge/master) | ||
|
||
[![bugs](https://sonarcloud.io/api/project_badges/measure?project=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge&metric=bugs)](https://sonarcloud.io/dashboard?id=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge) | ||
[![code_smells](https://sonarcloud.io/api/project_badges/measure?project=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge&metric=code_smells)](https://sonarcloud.io/dashboard?id=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge) | ||
[![coverage](https://sonarcloud.io/api/project_badges/measure?project=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge&metric=coverage)](https://sonarcloud.io/dashboard?id=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge) | ||
[![duplicated_lines_density](https://sonarcloud.io/api/project_badges/measure?project=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge) | ||
[![ncloc](https://sonarcloud.io/api/project_badges/measure?project=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge&metric=ncloc)](https://sonarcloud.io/dashboard?id=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge) | ||
[![sqale_rating](https://sonarcloud.io/api/project_badges/measure?project=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge) | ||
[![alert_status](https://sonarcloud.io/api/project_badges/measure?project=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge&metric=alert_status)](https://sonarcloud.io/dashboard?id=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge) | ||
[![reliability_rating](https://sonarcloud.io/api/project_badges/measure?project=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge) | ||
[![security_rating](https://sonarcloud.io/api/project_badges/measure?project=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge&metric=security_rating)](https://sonarcloud.io/dashboard?id=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge) | ||
[![sqale_index](https://sonarcloud.io/api/project_badges/measure?project=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge&metric=sqale_index)](https://sonarcloud.io/dashboard?id=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge) | ||
[![vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge&metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge) | ||
|
||
## Description | ||
|
||
A uWebSockets.js req/res psr-htt-message bridge. | ||
|
||
## Requirements | ||
|
||
* node: 12 | ||
* [@chubbyjs/psr-http-factory][2]: ^1.1.0 | ||
* [@chubbyjs/psr-http-message][3]: ^1.2.1 | ||
* [cookie][4]: ^0.4.1 | ||
* [qs][5]: ^6.10.1 | ||
* [uWebSockets.js][6]: github:uNetworking/uWebSockets.js#v19.3.0 | ||
|
||
## Installation | ||
|
||
Through [NPM](https://www.npmjs.com) as [@chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge][1]. | ||
|
||
```sh | ||
npm i @chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge@1.0.0 \ | ||
@chubbyjs/chubbyjs-http-message@1.1.0 // or any other psr-http-factory implementation | ||
``` | ||
|
||
## Usage | ||
|
||
```ts | ||
import PsrRequestFactory from '@chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge/dist/PsrRequestFactory'; | ||
import ResponseFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/ResponseFactory'; | ||
import ServerRequestFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/ServerRequestFactory'; | ||
import UriFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/UriFactory'; | ||
import UwebsocketResponseEmitter from '@chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge/dist/UwebsocketResponseEmitter'; | ||
import { HttpRequest, HttpResponse } from 'uWebSockets.js'; | ||
|
||
const responseFactory = new ResponseFactory(); | ||
|
||
const psrRequestFactory = new PsrRequestFactory(new ServerRequestFactory(), new UriFactory()); | ||
const uwebsocketResponseEmitter = new UwebsocketResponseEmitter(); | ||
|
||
require('uWebSockets.js') | ||
.App() | ||
.any('/*', (res: HttpResponse, req: HttpRequest) => { | ||
const request = psrRequestFactory.create(req, res); | ||
const response = responseFactory.createResponse(200); | ||
|
||
console.log(request); | ||
|
||
request.getBody().pipe(response.getBody()); | ||
|
||
uwebsocketResponseEmitter.emit(response, res); | ||
}) | ||
.listen('0.0.0.0', 8080, (listenSocket: unknown) => { | ||
if (listenSocket) { | ||
console.log('Listening to port 0.0.0.0:8080'); | ||
} | ||
}); | ||
``` | ||
|
||
## Copyright | ||
|
||
Dominik Zogg 2021 | ||
|
||
[1]: https://www.npmjs.com/package/@chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge | ||
|
||
[2]: https://www.npmjs.com/package/@chubbyjs/psr-http-factory | ||
[3]: https://www.npmjs.com/package/@chubbyjs/psr-http-message | ||
[4]: https://www.npmjs.com/package/cookie | ||
[5]: https://www.npmjs.com/package/qs | ||
[6]: https://www.npmjs.com/package/uwebsockets.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{ | ||
"name": "@chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge", | ||
"version": "1.0.0", | ||
"description": "A uwebsockets req/res psr-htt-message bridge.", | ||
"keywords": [ | ||
"chubbyjs", | ||
"http-message", | ||
"psr-7", | ||
"uwebsockets", | ||
"req", | ||
"res" | ||
], | ||
"author": "Dominik Zogg", | ||
"license": "MIT", | ||
"repository": "chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge", | ||
"scripts": { | ||
"cs-fix": "prettier --write src tests", | ||
"cs": "prettier --check src tests", | ||
"test": "jest", | ||
"infection": "stryker run", | ||
"build": "tsc", | ||
"prepare": "rm -Rf dist && npm run build && npm run cs && npm run test" | ||
}, | ||
"jest": { | ||
"preset": "ts-jest", | ||
"testEnvironment": "node", | ||
"collectCoverageFrom": [ | ||
"src/**/*.ts" | ||
], | ||
"coverageThreshold": { | ||
"global": { | ||
"lines": 100 | ||
} | ||
} | ||
}, | ||
"prettier": { | ||
"printWidth": 120, | ||
"tabWidth": 4, | ||
"singleQuote": true, | ||
"trailingComma": "all" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"dependencies": { | ||
"@chubbyjs/psr-http-factory": "^1.1.0", | ||
"@chubbyjs/psr-http-message": "^1.2.1", | ||
"@types/cookie": "^0.4.1", | ||
"@types/node": "^12", | ||
"@types/qs": "^6.9.7", | ||
"cookie": "^0.4.1", | ||
"qs": "^6.10.1", | ||
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v19.3.0" | ||
}, | ||
"devDependencies": { | ||
"@chubbyjs/chubbyjs-mock": "^1.1.1", | ||
"@stryker-mutator/core": "^5.3.0", | ||
"@stryker-mutator/jest-runner": "^5.3.0", | ||
"@types/jest": "^27.0.1", | ||
"jest": "^27.1.0", | ||
"prettier": "2.3.2", | ||
"ts-jest": "^27.0.5", | ||
"typescript": "^4.4.2" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
sonar.organization=chubbyjs | ||
sonar.projectKey=chubbyjs_chubbyjs-uwebsockets-psr-http-message-bridge | ||
sonar.projectName=chubbyjs-uwebsockets-psr-http-message-bridge | ||
|
||
sonar.sources=src | ||
sonar.tests=tests | ||
sonar.language=typescript | ||
sonar.sourceEncoding=UTF-8 | ||
sonar.javascript.lcov.reportPaths=coverage/lcov.info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import ServerRequestFactoryInterface from '@chubbyjs/psr-http-factory/dist/ServerRequestFactoryInterface'; | ||
import UriFactoryInterface from '@chubbyjs/psr-http-factory/dist/UriFactoryInterface'; | ||
import { Method } from '@chubbyjs/psr-http-message/dist/RequestInterface'; | ||
import { QueryParams } from '@chubbyjs/psr-http-message/dist/ServerRequestInterface'; | ||
import { parse as cookieParser } from 'cookie'; | ||
import { parse as queryParser } from 'qs'; | ||
import { Duplex, PassThrough } from 'stream'; | ||
import { HttpRequest, HttpResponse } from 'uWebSockets.js'; | ||
|
||
class PsrRequestFactory { | ||
public constructor( | ||
private serverRequestFactory: ServerRequestFactoryInterface, | ||
private uriFactory: UriFactoryInterface, | ||
) {} | ||
|
||
public create(req: HttpRequest, res: HttpResponse) { | ||
const query = req.getQuery(); | ||
const host = req.getHeader('host'); | ||
|
||
const uri = this.uriFactory.createUri( | ||
'http://' + (host ? host : 'localhost') + req.getUrl() + (query ? '?' + query : ''), | ||
); | ||
|
||
let serverRequest = this.serverRequestFactory | ||
.createServerRequest(req.getMethod().toUpperCase() as Method, uri) | ||
.withProtocolVersion('1.1') | ||
.withBody(this.getStream(res)); | ||
|
||
const rawCookie = req.getHeader('cookie'); | ||
|
||
if (rawCookie) { | ||
serverRequest = serverRequest.withCookieParams(new Map(Object.entries(cookieParser(rawCookie)))); | ||
} | ||
|
||
if (query) { | ||
serverRequest = serverRequest.withQueryParams(queryParser(query) as QueryParams); | ||
} | ||
|
||
req.forEach((name, value) => { | ||
serverRequest = serverRequest.withHeader(name, value); | ||
}); | ||
|
||
return serverRequest; | ||
} | ||
|
||
private getStream(res: HttpResponse): Duplex { | ||
const stream = new PassThrough(); | ||
|
||
res.onData((chunk: ArrayBuffer, isLast: boolean) => { | ||
stream.write(Buffer.from(chunk)); | ||
|
||
if (isLast) { | ||
stream.end(); | ||
} | ||
}); | ||
|
||
return stream; | ||
} | ||
} | ||
|
||
export default PsrRequestFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import ResponseInterface from '@chubbyjs/psr-http-message/dist/ResponseInterface'; | ||
import { HttpResponse } from 'uWebSockets.js'; | ||
|
||
class UwebsocketResponseEmitter { | ||
public emit(response: ResponseInterface, res: HttpResponse): void { | ||
res.writeStatus(`${response.getStatusCode()} ${response.getReasonPhrase()}`); | ||
|
||
response.getHeaders().forEach((value, name) => { | ||
res.writeHeader(name, value.join(', ')); | ||
}); | ||
|
||
res.onAborted(() => undefined); | ||
|
||
const body = response.getBody(); | ||
|
||
body.on('data', (data: Buffer) => res.write(data)); | ||
body.on('end', () => res.end()); | ||
} | ||
} | ||
|
||
export default UwebsocketResponseEmitter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"$schema": "./node_modules/@stryker-mutator/core/schema/stryker-schema.json", | ||
"packageManager": "npm", | ||
"reporters": [ | ||
"clear-text", | ||
"dashboard" | ||
], | ||
"testRunner": "jest", | ||
"coverageAnalysis": "off" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { HttpRequest, RecognizedString } from 'uWebSockets.js'; | ||
|
||
class HttpRequestDouble implements HttpRequest { | ||
getHeader(lowerCaseKey: RecognizedString): string { | ||
throw new Error('Method not implemented.'); | ||
} | ||
getParameter(index: number): string { | ||
throw new Error('Method not implemented.'); | ||
} | ||
getUrl(): string { | ||
throw new Error('Method not implemented.'); | ||
} | ||
getMethod(): string { | ||
throw new Error('Method not implemented.'); | ||
} | ||
getQuery(): string; | ||
getQuery(key: string): string; | ||
getQuery(key?: any): string { | ||
throw new Error('Method not implemented.'); | ||
} | ||
forEach(cb: (key: string, value: string) => void): void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
setYield(_yield: boolean): HttpRequest { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} | ||
|
||
export default HttpRequestDouble; |
Oops, something went wrong.