Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Mar 29, 2024
0 parents commit c19771e
Show file tree
Hide file tree
Showing 16 changed files with 503 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@chubbyts/chubbyts-eslint/dist/eslintrc').default;
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI

on:
push:
pull_request:
branches:
- master
schedule:
- cron: '0 0 * * *'

jobs:
node18:
name: Node 18
runs-on: ubuntu-22.04
steps:
- name: checkout
uses: actions/checkout@v4
- name: checkout node
uses: actions/setup-node@v4
with:
node-version: '18'
- run: npm install
- run: npm test
node20:
name: Node 20
runs-on: ubuntu-22.04
steps:
- name: checkout
uses: actions/checkout@v4
- name: checkout node
uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm install
- run: npm run lint
- 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 }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.stryker-tmp
coverage
dist
node_modules
package-lock.json
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024 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.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# chubbyts-http-undici-bridge

[![CI](https://github.com/chubbyts/chubbyts-http-undici-bridge/actions/workflows/ci.yml/badge.svg)](https://github.com/chubbyts/chubbyts-http-undici-bridge/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/chubbyts/chubbyts-http-undici-bridge/badge.svg?branch=master)](https://coveralls.io/github/chubbyts/chubbyts-http-undici-bridge?branch=master)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchubbyts%2Fchubbyts-http-undici-bridge%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/chubbyts/chubbyts-http-undici-bridge/master)
[![npm-version](https://img.shields.io/npm/v/@chubbyts/chubbyts-http-undici-bridge.svg)](https://www.npmjs.com/package/@chubbyts/chubbyts-http-undici-bridge)

[![bugs](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-http-undici-bridge&metric=bugs)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-undici-bridge)
[![code_smells](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-http-undici-bridge&metric=code_smells)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-undici-bridge)
[![coverage](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-http-undici-bridge&metric=coverage)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-undici-bridge)
[![duplicated_lines_density](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-http-undici-bridge&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-undici-bridge)
[![ncloc](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-http-undici-bridge&metric=ncloc)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-undici-bridge)
[![sqale_rating](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-http-undici-bridge&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-undici-bridge)
[![alert_status](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-http-undici-bridge&metric=alert_status)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-undici-bridge)
[![reliability_rating](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-http-undici-bridge&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-undici-bridge)
[![security_rating](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-http-undici-bridge&metric=security_rating)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-undici-bridge)
[![sqale_index](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-http-undici-bridge&metric=sqale_index)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-undici-bridge)
[![vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-http-undici-bridge&metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-http-undici-bridge)

## Description

A undici req/res http bridge.

## Requirements

* node: 18
* [@chubbyts/chubbyts-http-types][2]: ^1.2.3
* [undici][3]:^6.10.2

## Installation

Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-http-undici-bridge][1].

```ts
npm i @chubbyts/chubbyts-http-undici-bridge@^1.0
```

## Usage

```ts
import {
createServerRequestFactory,
createUriFactory,
} from '@chubbyts/chubbyts-http/dist/message-factory';
import { createUndiciToServerRequestFactory, createResponseToUndiciFactory } from '@chubbyts/chubbyts-http-undici-bridge/dist/undici-http';

const app = ...;

const undiciToServerRequestFactory = createUndiciToServerRequestFactory(
createUriFactory(),
createServerRequestFactory(),
);

const responseToUndiciFactory = createResponseToUndiciFactory();

const res = responseToUndiciFactory(await app(undiciToServerRequestFactory(req)));
```

## Copyright

2024 Dominik Zogg

[1]: https://www.npmjs.com/package/@chubbyts/chubbyts-http-undici-bridge
[2]: https://www.npmjs.com/package/@chubbyts/chubbyts-http-types
[3]: https://www.npmjs.com/package/undici
1 change: 1 addition & 0 deletions build.js
15 changes: 15 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable no-undef */
/* eslint-disable functional/immutable-data */
module.exports = {
'preset': 'ts-jest',
'testEnvironment': 'node',
'collectCoverageFrom': [
'src/**/*.ts'
],
'coverageThreshold': {
'global': {
'lines': 100
}
},
prettierPath: require.resolve('prettier-2'),
};
66 changes: 66 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "@chubbyts/chubbyts-http-undici-bridge",
"type": "module",
"version": "1.0.0",
"description": "A undici req/res http bridge.",
"keywords": [
"chubbyts",
"http",
"node"
],
"author": "Dominik Zogg",
"license": "MIT",
"repository": "chubbyts/chubbyts-http-undici-bridge",
"scripts": {
"build": "node ./build.js",
"cs-fix": "./node_modules/prettier/bin/prettier.cjs --write src tests",
"cs": "./node_modules/prettier/bin/prettier.cjs --check src tests",
"infection": "stryker run",
"lint-fix": "eslint src tests --fix",
"lint": "eslint src tests",
"prepare": "npm run build",
"test": "jest"
},
"prettier": {
"printWidth": 120,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "all"
},
"files": [
"dist"
],
"exports": {
"./*": {
"types": "./*.d.ts",
"require": "./*.cjs",
"import": "./*.mjs",
"default": "./*.mjs"
}
},
"engines": {
"node": ">=18"
},
"dependencies": {
"@chubbyts/chubbyts-http-types": "^1.2.3",
"undici": "^6.10.2"
},
"devDependencies": {
"@chubbyts/chubbyts-eslint": "^2.0.3",
"@chubbyts/chubbyts-function-mock": "^1.4.1",
"@chubbyts/chubbyts-packaging": "^2.0.6",
"@stryker-mutator/core": "^8.2.6",
"@stryker-mutator/jest-runner": "^8.2.6",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.30",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"prettier-2": "npm:prettier@^2.8.8",
"ts-jest": "^29.1.2",
"typescript": "^5.4.3",
"undici-types": "^6.10.1"
},
"publishConfig": {
"access": "public"
}
}
9 changes: 9 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sonar.organization=chubbyts
sonar.projectKey=chubbyts_chubbyts-http-undici-bridge
sonar.projectName=chubbyts-http-undici-bridge

sonar.sources=src
sonar.tests=tests
sonar.language=typescript
sonar.sourceEncoding=UTF-8
sonar.javascript.lcov.reportPaths=coverage/lcov.info
46 changes: 46 additions & 0 deletions src/undici-http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Duplex } from 'stream';
import type { Method, ServerRequest, Response } from '@chubbyts/chubbyts-http-types/dist/message';
import type { UriFactory, ServerRequestFactory } from '@chubbyts/chubbyts-http-types/dist/message-factory';
import type { Request as UndiciRequest } from 'undici';
import { Response as UndiciResponse } from 'undici';

type UndiciToServerRequestFactory = (req: UndiciRequest) => ServerRequest;

export const createUndiciToServerRequestFactory = (
uriFactory: UriFactory,
serverRequestFactory: ServerRequestFactory,
): UndiciToServerRequestFactory => {
return (req: UndiciRequest): ServerRequest => {
const uri = uriFactory(req.url);

const headers = Object.fromEntries(
Array.from(req.headers.entries())
.map(([name, value]) => [
name,
value
.split(',')
.map((part) => part.trim())
.filter((part) => part),
])
.filter(([_, value]) => value.length),
);

return {
...serverRequestFactory(req.method.toUpperCase() as Method, uri),
headers,
...(req.body ? { body: Duplex.from(req.body) } : {}),
};
};
};

type ResponseToUndiciFactory = (response: Response) => UndiciResponse;

export const createResponseToUndiciFactory =
(): ResponseToUndiciFactory =>
(response: Response): UndiciResponse => {
return new UndiciResponse(response.body.iterator(), {
status: response.status,
statusText: response.reasonPhrase,
headers: response.headers,
});
};
10 changes: 10 additions & 0 deletions stryker.conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "./node_modules/@stryker-mutator/core/schema/stryker-schema.json",
"testRunner": "jest",
"coverageAnalysis": "off",
"reporters": [
"clear-text",
"progress",
"dashboard"
]
}
Loading

0 comments on commit c19771e

Please sign in to comment.