@jitar-plugins/http 0.1.2 → 0.1.5

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 CHANGED
@@ -6,9 +6,9 @@ This package provides plugins for integrating the HTTP protocol in Jitar applica
6
6
  It contains two types of middleware:
7
7
 
8
8
  * **CORS** - configures cross-origin requests.
9
- * **Origin** - ensures the avaiability of the origin header.
9
+ * **Origin** - stores the origin in a cookie to ensure availability across requests.
10
10
 
11
- Both can be used indenpendently.
11
+ Both can be used independently.
12
12
 
13
13
  ## Installation
14
14
 
@@ -29,7 +29,7 @@ Follow the following steps to configure and use the middleware.
29
29
 
30
30
  import { CorsMiddleware } from '@jitar-plugins/http';
31
31
 
32
- const origin = '*'; // allowed orgins (optional, default: *)
32
+ const origin = '*'; // allowed origins (optional, default: *)
33
33
  const headers = '*'; // allowed headers (optional, default: *)
34
34
 
35
35
  export default new CorsMiddleware(origin, headers);
@@ -42,12 +42,18 @@ export default new CorsMiddleware(origin, headers);
42
42
 
43
43
  import { OriginMiddleware } from '@jitar-plugins/http';
44
44
 
45
- export default new OriginMiddleware(); // no configuration options
45
+ const options = {
46
+ path: '/rpc', // the path the cookie is valid for (optional, default: '/')
47
+ sameSite: 'Strict' | 'Lax' | 'None', // the SameSite attribute of the cookie (optional, default: 'Strict')
48
+ secure: true // the Secure attribute of the cookie (optional, default: true)
49
+ };
50
+
51
+ export default new OriginMiddleware(options);
46
52
  ```
47
53
 
48
54
  ### Step 2 - Activate the middleware
49
55
 
50
- With the health check in place, it needs to be activated by registering it to the proxy / standalone / worker service.
56
+ With the middleware in place, it needs to be activated by registering it to the proxy / standalone / worker service.
51
57
 
52
58
  ```json
53
59
  /* services/proxy.json */
@@ -1,5 +1,12 @@
1
1
  import type { Middleware, NextHandler, Request, Response } from 'jitar';
2
+ type Options = {
3
+ path?: string;
4
+ sameSite?: string;
5
+ secure?: boolean;
6
+ };
2
7
  export default class OriginMiddleware implements Middleware {
3
8
  #private;
9
+ constructor(options?: Options);
4
10
  handle(request: Request, next: NextHandler): Promise<Response>;
5
11
  }
12
+ export {};
@@ -1,6 +1,14 @@
1
1
  import { BadRequest } from 'jitar';
2
2
  const ORIGIN_COOKIE_NAME = 'x-client-origin';
3
3
  export default class OriginMiddleware {
4
+ #path;
5
+ #sameSite;
6
+ #secure;
7
+ constructor(options) {
8
+ this.#path = options?.path ?? '/rpc';
9
+ this.#sameSite = options?.sameSite ?? 'Strict';
10
+ this.#secure = options?.secure ?? true;
11
+ }
4
12
  async handle(request, next) {
5
13
  let fromCookie = true;
6
14
  let origin = this.#getOriginFromCookie(request);
@@ -44,6 +52,10 @@ export default class OriginMiddleware {
44
52
  }
45
53
  }
46
54
  #setOriginCookie(response, origin) {
47
- response.setHeader('Set-Cookie', `${ORIGIN_COOKIE_NAME}=${origin}; Path=/; HttpOnly=true; SameSite=None; Secure`);
55
+ let cookie = `${ORIGIN_COOKIE_NAME}=${origin}; Path=${this.#path}; HttpOnly; SameSite=${this.#sameSite}`;
56
+ if (this.#secure) {
57
+ cookie += ' Secure';
58
+ }
59
+ response.setHeader('Set-Cookie', cookie);
48
60
  }
49
61
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jitar-plugins/http",
3
3
  "private": false,
4
- "version": "0.1.2",
4
+ "version": "0.1.5",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "url": "git+https://github.com/MaskingTechnology/jitar-plugins.git"
@@ -20,6 +20,6 @@
20
20
  "types": "dist/index.d.ts",
21
21
  "exports": "./dist/index.js",
22
22
  "peerDependencies": {
23
- "jitar": "^0.10.4"
23
+ "jitar": "^0.10.4 || ^0.11.0"
24
24
  }
25
25
  }