pixl-server-web 2.0.12 → 2.0.13

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
@@ -98,6 +98,7 @@ This module is a component for use in [pixl-server](https://www.github.com/jhuck
98
98
  + [args.perf](#argsperf)
99
99
  + [args.server](#argsserver)
100
100
  + [args.id](#argsid)
101
+ + [args.setCookie](#argssetcookie)
101
102
  * [Request Filters](#request-filters)
102
103
  - [Transaction Logging](#transaction-logging)
103
104
  * [Request Detail Logging](#request-detail-logging)
@@ -1166,6 +1167,14 @@ This is a reference to the pixl-server object which handled the request.
1166
1167
 
1167
1168
  This is an internal ID string used by the server to track and log individual requests.
1168
1169
 
1170
+ ### args.setCookie
1171
+
1172
+ A utility function used to serialize cookies into the proper format, and set or append them to the `Set-Cookie` response header. It accepts a name, a value, and an optional set of options. Example use:
1173
+
1174
+ ```js
1175
+ args.setCookie( 'session', 'ABDEF01234567890', { path: '/', maxAge: 86400, secure: true, httpOnly: true, sameSite: 'Lax' } );
1176
+ ```
1177
+
1169
1178
  ## Request Filters
1170
1179
 
1171
1180
  Filters allow you to preprocess a request, before any handlers get their hands on it. They can pass data through, manipulate it, or even interrupt and abort requests. Filters are attached to particular URIs or URI patterns, and multiple may be applied to one request, depending on your rules. They can be asynchronous, and can also pass data between one another if desired.
package/lib/args.js ADDED
@@ -0,0 +1,48 @@
1
+ // Simple HTTP / HTTPS Web Server
2
+ // A component for the pixl-server daemon framework.
3
+ // Copyright (c) 2015 - 2025 Joseph Huckaby
4
+ // Released under the MIT License
5
+
6
+ module.exports = class Args {
7
+
8
+ constructor(args = {}) {
9
+ // import k/v pairs
10
+ for (var key in args) this[key] = args[key];
11
+ }
12
+
13
+ setCookie(name, value, opts) {
14
+ // set cookie in response headers
15
+ // opts: { maxAge?, expires?, domain?, path?, secure?, httpOnly?, sameSite? }
16
+ const res = this.response;
17
+ const enc = (s) => encodeURIComponent(s);
18
+ const parts = [`${enc(name)}=${value === '' ? '' : enc(value)}`];
19
+
20
+ // serialize cookie to string
21
+ if (opts.maxAge != null) parts.push(`Max-Age=${Math.floor(opts.maxAge)}`);
22
+ if (opts.expires) parts.push(`Expires=${opts.expires.toUTCString()}`);
23
+ if (opts.domain) parts.push(`Domain=${opts.domain}`);
24
+ parts.push(`Path=${opts.path || '/'}`);
25
+ if (opts.secure) {
26
+ if (opts.secure === 'auto') {
27
+ if (this.request.headers.ssl) parts.push('Secure');
28
+ }
29
+ else parts.push('Secure');
30
+ }
31
+ if (opts.httpOnly !== false) parts.push('HttpOnly'); // default on
32
+ if (opts.sameSite) {
33
+ const ss = String(opts.sameSite).toLowerCase();
34
+ const token = ss === 'strict' ? 'Strict' : ss === 'none' ? 'None' : 'Lax';
35
+ parts.push(`SameSite=${token}`);
36
+ } else {
37
+ parts.push('SameSite=Lax');
38
+ }
39
+ const cookieStr = parts.join('; ');
40
+
41
+ // append to previous set-cookie or set as solo header
42
+ const prev = res.getHeader('Set-Cookie');
43
+ if (!prev) res.setHeader('Set-Cookie', cookieStr);
44
+ else if (Array.isArray(prev)) res.setHeader('Set-Cookie', prev.concat(cookieStr));
45
+ else res.setHeader('Set-Cookie', [prev, cookieStr]);
46
+ }
47
+
48
+ };
package/lib/request.js CHANGED
@@ -7,19 +7,20 @@ const async = require('async');
7
7
  const Formidable = require('formidable');
8
8
  const Querystring = require('querystring');
9
9
  const Perf = require('pixl-perf');
10
+ const Args = require('./args.js');
10
11
 
11
12
  module.exports = class Request {
12
13
 
13
14
  enqueueHTTPRequest(request, response) {
14
15
  // enqueue request for handling as soon as concurrency limits allow
15
- var args = {
16
+ var args = new Args({
16
17
  id: this.getNextId('r'),
17
18
  date: Date.now() / 1000,
18
19
  request: request,
19
20
  response: response,
20
21
  state: 'queued',
21
22
  perf: new Perf()
22
- };
23
+ });
23
24
 
24
25
  // take snapshot of req and socket counts at start of request, used by perf logger at end
25
26
  if (this.logPerfEnabled) {
@@ -252,7 +253,8 @@ module.exports = class Request {
252
253
  var pairs = request.headers['cookie'].split(/\;\s*/);
253
254
  for (var idx = 0, len = pairs.length; idx < len; idx++) {
254
255
  if (pairs[idx].match(/^([^\=]+)\=(.*)$/)) {
255
- args.cookies[ RegExp.$1 ] = RegExp.$2;
256
+ var key = RegExp.$1, value = RegExp.$2;
257
+ args.cookies[ decodeURIComponent(key) ] = decodeURIComponent(value);
256
258
  }
257
259
  } // foreach cookie
258
260
  } // headers.cookie
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pixl-server-web",
3
- "version": "2.0.12",
3
+ "version": "2.0.13",
4
4
  "description": "A web server component for the pixl-server framework.",
5
5
  "author": "Joseph Huckaby <jhuckaby@gmail.com>",
6
6
  "homepage": "https://github.com/jhuckaby/pixl-server-web",