divhunt 2.0.10 → 2.0.11

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.
@@ -25,8 +25,8 @@ commands.Fn('http.server', async function(port = 3000, callbacks = {})
25
25
 
26
26
  if(!command || !command.Get('exposed'))
27
27
  {
28
- http.response.code = 404;
29
- http.response.message = 'Command not found.';
28
+ http.respond.code = 404;
29
+ http.respond.message = 'Command not found.';
30
30
 
31
31
  callbacks['onResponse'] && callbacks['onResponse'](http);
32
32
  return;
@@ -47,7 +47,7 @@ commands.Fn('http.server', async function(port = 3000, callbacks = {})
47
47
  {
48
48
  http.data.streaming = 1;
49
49
  http.prevent = true;
50
- http.raw.writeHead(200, {
50
+ http.response.writeHead(200, {
51
51
  'Content-Type': type.contentType,
52
52
  'Transfer-Encoding': 'chunked'
53
53
  });
@@ -58,7 +58,7 @@ commands.Fn('http.server', async function(port = 3000, callbacks = {})
58
58
  if(http.data.streaming)
59
59
  {
60
60
  http.data.streaming++;
61
- http.raw.write(type.formatter(chunk.data) + '\n');
61
+ http.response.write(type.formatter(chunk.data) + '\n');
62
62
  }
63
63
 
64
64
  callbacks['onChunk'] && callbacks['onChunk'](http, chunk);
@@ -68,16 +68,16 @@ commands.Fn('http.server', async function(port = 3000, callbacks = {})
68
68
  {
69
69
  if(http.data.streaming === 1)
70
70
  {
71
- http.raw.write(type.formatter(response.data) + '\n');
71
+ http.response.write(type.formatter(response.data) + '\n');
72
72
  }
73
73
 
74
- http.raw.end();
74
+ http.response.end();
75
75
  }
76
76
 
77
- http.response.type = command.Get('type');
78
- http.response.data = response.data;
79
- http.response.message = response.message;
80
- http.response.code = response.code;
77
+ http.respond.type = command.Get('type');
78
+ http.respond.data = response.data;
79
+ http.respond.message = response.message;
80
+ http.respond.code = response.code;
81
81
 
82
82
  callbacks['onResponse'] && callbacks['onResponse'](http);
83
83
  },
@@ -27,11 +27,11 @@ serversHTTP.Fn('item.start', function(item)
27
27
  user: this.methods.user(request),
28
28
  time: performance.now(),
29
29
  error: null,
30
- raw: response,
30
+ response,
31
31
  streaming: false,
32
32
  types: this.methods.types,
33
33
  prevent: false,
34
- response: {
34
+ respond: {
35
35
  type: 'JSON',
36
36
  data: null,
37
37
  message: 'Request processed',
@@ -64,7 +64,7 @@ serversHTTP.Fn('item.start', function(item)
64
64
  return;
65
65
  }
66
66
 
67
- const type = http.types[http.response.type] || http.types.JSON;
67
+ const type = http.types[http.respond.type] || http.types.JSON;
68
68
 
69
69
  http.time = (performance.now() - http.time).toFixed(2);
70
70
 
@@ -83,14 +83,14 @@ serversHTTP.Fn('item.start', function(item)
83
83
  return;
84
84
  }
85
85
 
86
- const content = http.response.type === 'JSON' ? {
87
- data: http.response.data || {},
88
- message: http.response.message || 'No response message provided.',
89
- code: http.response.code,
86
+ const content = http.respond.type === 'JSON' ? {
87
+ data: http.respond.data || {},
88
+ message: http.respond.message || 'No response message provided.',
89
+ code: http.respond.code,
90
90
  time: http.time
91
- } : http.response.data || '';
91
+ } : http.respond.data || '';
92
92
 
93
- response.writeHead(http.response.code, { 'Content-Type': type.contentType });
93
+ response.writeHead(http.respond.code, { 'Content-Type': type.contentType });
94
94
  response.end(type.formatter(content));
95
95
 
96
96
  item.Get('onComplete') && item.Get('onComplete')(http);
@@ -3,12 +3,12 @@
3
3
  ## File structure
4
4
 
5
5
  ```
6
- lib/ Core Divhunt class + 16 mixins
6
+ lib/ Core Divhunt class + 17 mixins
7
7
  load.js Entry point — creates Divhunt instance, handles signals
8
8
  src/
9
9
  divhunt.js Main class (mixin-composed)
10
10
  mixins/ Addons, Emitter, Middleware, Data, DOM, Route,
11
- Function, Generate, Binaries, Helper, and more
11
+ Function, Generate, Binaries, Helper, Cookie, and more
12
12
  classes/
13
13
  addon/ DivhuntAddon + mixins (fields, items, functions, find, render, store)
14
14
  classes/
package/docs/servers.md CHANGED
@@ -20,6 +20,60 @@ commands.Fn('http.server', 3000, {
20
20
  });
21
21
  ```
22
22
 
23
+ ## HTTP object
24
+
25
+ Every request creates an `http` object passed to `onRequest` and command callbacks (via `this.http`):
26
+
27
+ | Property | Type | Description |
28
+ |---|---|---|
29
+ | `request` | IncomingMessage | Node.js request (headers, method, url) |
30
+ | `response` | ServerResponse | Node.js response (writeHead, setHeader, end) |
31
+ | `data` | object | Parsed request data (query params + body) |
32
+ | `url` | URL | Parsed request URL |
33
+ | `user` | object | `{ ip, agent, forwarded, referrer }` |
34
+ | `respond` | object | `{ type, data, message, code }` — response payload |
35
+ | `prevent` | boolean | Set `true` to skip automatic response |
36
+
37
+ ## Cookies
38
+
39
+ Built-in cookie support via `divhunt.Cookie*` methods. Works on both front (browser) and back (server) — detected via `divhunt.environment`.
40
+
41
+ ### Back (server)
42
+
43
+ ```js
44
+ // Read from request
45
+ const token = divhunt.CookieGet('token', this.http.request);
46
+
47
+ // Set on response (HttpOnly + Secure by default)
48
+ divhunt.CookieSet('token', value, {
49
+ path: '/',
50
+ maxAge: 86400,
51
+ sameSite: 'Strict'
52
+ }, this.http.response);
53
+
54
+ // Clear
55
+ divhunt.CookieClear('token', { path: '/' }, this.http.response);
56
+ ```
57
+
58
+ ### Front (browser)
59
+
60
+ ```js
61
+ divhunt.CookieSet('theme', 'dark', { path: '/', maxAge: 86400 });
62
+ const theme = divhunt.CookieGet('theme');
63
+ divhunt.CookieClear('theme');
64
+ ```
65
+
66
+ ### Options
67
+
68
+ | Option | Back default | Front default | Description |
69
+ |---|---|---|---|
70
+ | `path` | — | — | Cookie path |
71
+ | `maxAge` | — | — | Lifetime in seconds |
72
+ | `domain` | — | — | Cookie domain |
73
+ | `httpOnly` | `true` | N/A | JS can't read the cookie |
74
+ | `secure` | `true` | `false` | HTTPS only |
75
+ | `sameSite` | — | — | `Strict`, `Lax`, or `None` |
76
+
23
77
  ## gRPC server
24
78
 
25
79
  Bidirectional streaming with automatic reconnection and binary data transport.
@@ -17,11 +17,13 @@ import DivhuntRoute from './mixins/route.js';
17
17
  import DivhuntError from './mixins/error.js';
18
18
  import DivhuntCrypto from './mixins/crypto.js';
19
19
  import DivhuntForm from './mixins/form.js';
20
+ import DivhuntCookie from './mixins/cookie.js';
20
21
 
21
22
  class Divhunt
22
23
  {
23
24
  constructor()
24
25
  {
26
+ this.environment = typeof window !== 'undefined' ? 'front' : 'back';
25
27
  this.emitters = {};
26
28
  this.middleware = {};
27
29
 
@@ -84,5 +86,6 @@ Object.assign(Divhunt.prototype, DivhuntRoute);
84
86
  Object.assign(Divhunt.prototype, DivhuntError);
85
87
  Object.assign(Divhunt.prototype, DivhuntCrypto);
86
88
  Object.assign(Divhunt.prototype, DivhuntForm);
89
+ Object.assign(Divhunt.prototype, DivhuntCookie);
87
90
 
88
91
  export default Divhunt;
@@ -0,0 +1,73 @@
1
+ const DivhuntCookie =
2
+ {
3
+ CookieGet(name, request)
4
+ {
5
+ const header = this.environment === 'front' ? document.cookie : (request?.headers?.cookie || '');
6
+ const match = header.match(new RegExp('(?:^|;\\s*)' + name + '=([^;]*)'));
7
+
8
+ if(!match)
9
+ {
10
+ return null;
11
+ }
12
+
13
+ return decodeURIComponent(match[1]);
14
+ },
15
+
16
+ CookieSet(name, value, options, response)
17
+ {
18
+ const parts = [encodeURIComponent(name) + '=' + encodeURIComponent(value)];
19
+
20
+ if(options?.path)
21
+ {
22
+ parts.push('Path=' + options.path);
23
+ }
24
+
25
+ if(options?.maxAge)
26
+ {
27
+ parts.push('Max-Age=' + options.maxAge);
28
+ }
29
+
30
+ if(options?.domain)
31
+ {
32
+ parts.push('Domain=' + options.domain);
33
+ }
34
+
35
+ if(options?.sameSite)
36
+ {
37
+ parts.push('SameSite=' + options.sameSite);
38
+ }
39
+
40
+ if(this.environment === 'front')
41
+ {
42
+ if(options?.secure)
43
+ {
44
+ parts.push('Secure');
45
+ }
46
+
47
+ document.cookie = parts.join('; ');
48
+ return;
49
+ }
50
+
51
+ if(options?.httpOnly !== false)
52
+ {
53
+ parts.push('HttpOnly');
54
+ }
55
+
56
+ if(options?.secure !== false)
57
+ {
58
+ parts.push('Secure');
59
+ }
60
+
61
+ const existing = response.getHeader('Set-Cookie') || [];
62
+ const cookies = Array.isArray(existing) ? existing : [existing];
63
+ cookies.push(parts.join('; '));
64
+ response.setHeader('Set-Cookie', cookies);
65
+ },
66
+
67
+ CookieClear(name, options, response)
68
+ {
69
+ this.CookieSet(name, '', { path: options?.path || '/', maxAge: 0 }, response);
70
+ }
71
+ };
72
+
73
+ export default DivhuntCookie;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "divhunt",
3
- "version": "2.0.10",
3
+ "version": "2.0.11",
4
4
  "description": "Full-stack isomorphic JavaScript framework built from scratch. One addon abstraction powers databases, servers, commands, pages, directives, queues, and more.",
5
5
  "type": "module",
6
6
  "main": "lib/load.js",
package/test.js CHANGED
@@ -42,7 +42,6 @@ commands.Item({
42
42
  }
43
43
  });
44
44
 
45
-
46
45
  commands.Fn('expose', 'commands:run', '/api/commands/run');
47
46
 
48
47
  await commands.Fn('http.server', 3000, {