@jitar-plugins/http 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/dist/OriginMiddleware.d.ts +5 -0
- package/dist/OriginMiddleware.js +53 -0
- package/dist/index.d.ts +0 -0
- package/dist/index.js +1 -0
- package/package.json +23 -0
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
|
|
2
|
+
# HTTP | Jitar Plugins
|
|
3
|
+
|
|
4
|
+
This package provides plugins for integrating the HTTP protocol in Jitar applications.
|
|
5
|
+
|
|
6
|
+
It contains a single middleware for ensuring the avaiability of the origin header.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @jitar-plugins/http
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
Follow the following steps to configure and use the provided middleware.
|
|
17
|
+
|
|
18
|
+
### Step 1 - Configure the middleware
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
// src/middleware/originMiddleware.ts
|
|
22
|
+
|
|
23
|
+
import OriginMiddleware from '@jitar-plugins/http';
|
|
24
|
+
|
|
25
|
+
export default new OriginMiddleware();
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Step 2 - Activate the middleware
|
|
29
|
+
|
|
30
|
+
With the health check in place, it needs to be activated by registering it to the proxy / standalone / worker service.
|
|
31
|
+
|
|
32
|
+
```json
|
|
33
|
+
/* services/proxy.json */
|
|
34
|
+
{
|
|
35
|
+
"url": "http://example.com:3000",
|
|
36
|
+
"middleware": [ /* add middleware here */
|
|
37
|
+
"./middleware/originMiddleware"
|
|
38
|
+
],
|
|
39
|
+
"proxy":
|
|
40
|
+
{
|
|
41
|
+
/* service configuration */
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { BadRequest } from 'jitar';
|
|
2
|
+
import validator from '@theshelf/validation';
|
|
3
|
+
const ORIGIN_COOKIE_NAME = 'x-client-origin';
|
|
4
|
+
const schema = {
|
|
5
|
+
origin: {
|
|
6
|
+
message: 'Invalid origin',
|
|
7
|
+
URL: {
|
|
8
|
+
required: true
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
export default class OriginMiddleware {
|
|
13
|
+
async handle(request, next) {
|
|
14
|
+
let fromCookie = true;
|
|
15
|
+
let origin = this.#getOriginFromCookie(request);
|
|
16
|
+
if (origin === undefined) {
|
|
17
|
+
fromCookie = false;
|
|
18
|
+
origin = this.#getOriginFromHeader(request);
|
|
19
|
+
}
|
|
20
|
+
this.#validateOriginValue(origin);
|
|
21
|
+
// The origin header is validated and set here for use in other middlewares
|
|
22
|
+
request.setHeader('origin', origin);
|
|
23
|
+
const response = await next();
|
|
24
|
+
if (fromCookie === false) {
|
|
25
|
+
this.#setOriginCookie(response, origin);
|
|
26
|
+
}
|
|
27
|
+
return response;
|
|
28
|
+
}
|
|
29
|
+
#getOriginFromHeader(request) {
|
|
30
|
+
return request.getHeader('origin');
|
|
31
|
+
}
|
|
32
|
+
#getOriginFromCookie(request) {
|
|
33
|
+
const header = request.getHeader('cookie');
|
|
34
|
+
if (header === undefined) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
for (const cookie of header.split(';')) {
|
|
38
|
+
const [key, value] = cookie.split('=');
|
|
39
|
+
if (key.trim() === ORIGIN_COOKIE_NAME) {
|
|
40
|
+
return value?.trim();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
#validateOriginValue(value) {
|
|
45
|
+
const result = validator.validate({ origin: value }, schema);
|
|
46
|
+
if (result.invalid) {
|
|
47
|
+
throw new BadRequest('Invalid origin');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
#setOriginCookie(response, origin) {
|
|
51
|
+
response.setHeader('Set-Cookie', `${ORIGIN_COOKIE_NAME}=${origin}; Path=/; HttpOnly=true; SameSite=None; Secure`);
|
|
52
|
+
}
|
|
53
|
+
}
|
package/dist/index.d.ts
ADDED
|
File without changes
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jitar-plugins/http",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"clean": "rimraf dist",
|
|
9
|
+
"lint": "eslint",
|
|
10
|
+
"review": "npm run build && npm run lint",
|
|
11
|
+
"prepublishOnly": "npm run build"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"README.md",
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
18
|
+
"exports": "./dist/index.js",
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@theshelf/validation": "^0.0.2",
|
|
21
|
+
"jitar": "^0.10.3"
|
|
22
|
+
}
|
|
23
|
+
}
|