toiljs 0.0.129 → 0.0.131

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## [v0.0.131] - 2026-10-19
4
+
5
+ - Preserve the browser Host header through the development Vite proxy so application origin-specific response policies, including localhost popup handoffs, receive the public origin instead of the internal loopback address. Retain multiple Set-Cookie headers, manual redirects, binary bodies, and HEAD handling.
6
+
7
+ ## [v0.0.130] - 2026-10-19
8
+
9
+ - Format browser security deployment instructions with colored headings and actions, aligned hosting guidance, and spaced rule blocks. Preserve complete copyable expressions and header values, generic project rules, and plain output when terminal colors are disabled.
10
+
3
11
  ## [v0.0.129] - 2026-10-19
4
12
 
5
13
  - Print generic Cloudflare deployment instructions after successful client builds, using the final generated header rules, configured hostname, complete policy values, and project-defined route exceptions.
@@ -1,3 +1,3 @@
1
1
  import type { ResolvedToilConfig } from './config.js';
2
- export declare function securityDeploymentInstructions(headers: string, siteUrl?: string): string | null;
2
+ export declare function securityDeploymentInstructions(headers: string, siteUrl?: string, color?: boolean): string | null;
3
3
  export declare function logSecurityDeployment(cfg: ResolvedToilConfig): void;
@@ -1,5 +1,6 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
+ import pc from 'picocolors';
3
4
  function headerRules(source) {
4
5
  const rules = [];
5
6
  let current;
@@ -37,7 +38,7 @@ function expression(pattern, hostname) {
37
38
  const pathMatch = match('http.request.uri.path', pathname);
38
39
  return host ? `(${match('http.host', host)}) and (${pathMatch})` : pathMatch;
39
40
  }
40
- export function securityDeploymentInstructions(headers, siteUrl) {
41
+ export function securityDeploymentInstructions(headers, siteUrl, color = pc.isColorSupported) {
41
42
  const rules = headerRules(headers);
42
43
  if (!rules.some((rule) => rule.headers.some((header) => /^(content-security-policy|cross-origin-opener-policy)(-report-only)?$/i.test(header.name) &&
43
44
  header.value !== null &&
@@ -45,37 +46,51 @@ export function securityDeploymentInstructions(headers, siteUrl) {
45
46
  return null;
46
47
  const site = siteUrl && URL.canParse(siteUrl) ? new URL(siteUrl) : undefined;
47
48
  const hostname = site && ['https:', 'http:'].includes(site.protocol) ? site.hostname : undefined;
49
+ const colors = pc.createColors(color);
50
+ const separator = ` ${colors.dim('─'.repeat(64))}`;
48
51
  const lines = [
49
- '\nBrowser security deployment setup',
50
- 'Cloudflare Pages static hosting: deploy the generated _headers with the client output.',
51
- 'Other hosts must send the generated response headers; uploading _headers alone does not apply them.',
52
- 'toiljs start applies the framework headers automatically. Pages Functions / Workers must set headers in their handlers.',
53
52
  '',
54
- 'Cloudflare: Rules > Transform Rules > Modify Response Header > Create rule.',
55
- 'Create the following rules in order using Custom filter expression > Edit expression.',
56
- 'Choose Set static for each header value, or Remove where indicated, then Deploy.',
53
+ ` ${colors.bold(colors.cyan('Browser security deployment setup'))}`,
54
+ separator,
55
+ '',
56
+ ` ${colors.bold('Hosting')}`,
57
+ ...[
58
+ ['Cloudflare Pages', 'Deploy _headers with the client output.'],
59
+ ['toiljs start', 'Framework headers apply automatically.'],
60
+ ['Other hosts', 'Configure response headers on your host.'],
61
+ ['', 'Uploading _headers alone does not apply them.'],
62
+ ['Functions / Workers', 'Set response headers in your handlers.'],
63
+ ].map(([label, description]) => ` ${colors.bold(label.padEnd(21))}${description}`),
64
+ '',
65
+ ` ${colors.bold('Cloudflare response header rules')}`,
66
+ ` Rules ${colors.dim('›')} Transform Rules ${colors.dim('›')} Modify Response Header ${colors.dim('›')} Create rule`,
67
+ '',
68
+ ' Create the rules below in order.',
69
+ ` Select ${colors.bold('Custom filter expression')} ${colors.dim('›')} ${colors.bold('Edit expression')}.`,
70
+ ` Choose ${colors.green('Set static')} or ${colors.yellow('Remove')} as shown, then ${colors.bold('Deploy')}.`,
71
+ '',
57
72
  ];
58
73
  if (!hostname)
59
- lines.push('No site hostname configured in client.seo.url: scope these expressions to your deployment hostname if needed.');
74
+ lines.push(` ${colors.yellow('No site hostname configured')} in client.seo.url.`, ' Scope these expressions to your deployment hostname if needed.', '');
60
75
  for (const [index, rule] of rules.entries()) {
61
- lines.push('', `${index + 1}. ${rule.pattern}`);
76
+ lines.push(separator, '', ` ${colors.bold(colors.cyan(`${index + 1}. ${rule.pattern}`))}`, '');
62
77
  const filter = expression(rule.pattern, hostname);
63
78
  if (filter === null ||
64
79
  rule.headers.some((header) => /:[A-Za-z_]\w*/.test(header.value ?? ''))) {
65
- lines.push('This rule uses a pattern or placeholder requiring configuration at the origin or in a Worker.', rule.pattern, ...rule.headers.map((header) => header.value === null
66
- ? ` ! ${header.name}`
67
- : ` ${header.name}: ${header.value}`));
80
+ lines.push(` ${colors.yellow('Configure at the origin or in a Worker')}`, ' This rule uses a pattern or placeholder requiring configuration there.', '', ` ${rule.pattern}`, ...rule.headers.map((header) => header.value === null
81
+ ? ` ! ${header.name}`
82
+ : ` ${header.name}: ${header.value}`), '');
68
83
  continue;
69
84
  }
70
- lines.push('Expression:', filter);
85
+ lines.push(` ${colors.dim('Expression')}`, ` ${filter}`, '');
71
86
  for (const header of rule.headers) {
72
87
  if (header.value === null)
73
- lines.push(`Remove: ${header.name}`);
88
+ lines.push(` ${colors.yellow('Remove')} ${colors.bold(header.name)}`, '');
74
89
  else
75
- lines.push(`Set static: ${header.name}`, 'Value:', header.value);
90
+ lines.push(` ${colors.green('Set static')} ${colors.bold(header.name)}`, ` ${colors.dim('Value')}`, ` ${header.value}`, '');
76
91
  }
77
92
  }
78
- lines.push('', 'Keep the generated HTML CSP meta policy, import map, and integrity attributes when deploying.', '');
93
+ lines.push(separator, '', ` ${colors.bold('When deploying')}`, ' Keep the generated HTML CSP meta policy, import map, and integrity attributes.', ' Expressions and header values above are complete; copy each as a single value.', '');
79
94
  return lines.join('\n');
80
95
  }
81
96
  export function logSecurityDeployment(cfg) {
@@ -1,3 +1,4 @@
1
+ import { request as httpRequest } from 'node:http';
1
2
  import { writeResponseHeader } from 'toiljs/backend';
2
3
  import { builtContentSecurityPolicy } from 'toiljs/shared';
3
4
  const SKIP_REQUEST_HEADERS = new Set([
@@ -20,28 +21,48 @@ const SKIP_RESPONSE_HEADERS = new Set([
20
21
  'transfer-encoding',
21
22
  ]);
22
23
  export async function proxyToVite(request, response, target) {
23
- const url = `http://${target.host}:${String(target.port)}${request.url}`;
24
- const headers = new Headers();
24
+ const headers = { 'accept-encoding': 'identity' };
25
25
  for (const [name, value] of Object.entries(request.headers)) {
26
26
  if (!SKIP_REQUEST_HEADERS.has(name.toLowerCase()))
27
- headers.set(name, value);
27
+ headers[name] = value;
28
28
  }
29
29
  const hasBody = request.method !== 'GET' && request.method !== 'HEAD';
30
30
  const body = hasBody ? await request.buffer() : undefined;
31
- const upstream = await fetch(url, {
32
- method: request.method === 'HEAD' ? 'GET' : request.method,
33
- headers,
34
- body: body && body.length > 0 ? new Uint8Array(body) : undefined,
35
- redirect: 'manual',
31
+ const upstream = await new Promise((resolve, reject) => {
32
+ const outgoing = httpRequest({
33
+ hostname: target.host,
34
+ port: target.port,
35
+ path: request.url,
36
+ method: request.method === 'HEAD' ? 'GET' : request.method,
37
+ headers,
38
+ }, (incoming) => {
39
+ const chunks = [];
40
+ incoming.on('data', (chunk) => chunks.push(chunk));
41
+ incoming.on('error', reject);
42
+ incoming.on('aborted', () => reject(new Error('Vite response aborted')));
43
+ incoming.on('end', () => resolve({
44
+ status: incoming.statusCode ?? 502,
45
+ headers: incoming.headers,
46
+ body: Buffer.concat(chunks),
47
+ }));
48
+ });
49
+ outgoing.on('error', reject);
50
+ outgoing.end(body);
36
51
  });
37
52
  response.status(upstream.status);
38
- upstream.headers.forEach((value, name) => {
39
- if (!SKIP_RESPONSE_HEADERS.has(name))
40
- writeResponseHeader(response, name, value);
41
- });
42
- const buf = Buffer.from(await upstream.arrayBuffer());
43
- if (upstream.headers.get('content-type')?.includes('text/html') &&
44
- !upstream.headers.has('content-security-policy')) {
53
+ for (const [name, value] of Object.entries(upstream.headers)) {
54
+ if (value === undefined || SKIP_RESPONSE_HEADERS.has(name))
55
+ continue;
56
+ if (name === 'set-cookie' && Array.isArray(value)) {
57
+ for (const cookie of value)
58
+ writeResponseHeader(response, name, cookie);
59
+ }
60
+ else
61
+ writeResponseHeader(response, name, Array.isArray(value) ? value.join(', ') : value);
62
+ }
63
+ const buf = upstream.body;
64
+ if (upstream.headers['content-type']?.includes('text/html') &&
65
+ !upstream.headers['content-security-policy']) {
45
66
  const policy = builtContentSecurityPolicy(buf.toString('utf8'));
46
67
  if (policy !== null)
47
68
  response.header('content-security-policy', policy, true);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "toiljs",
3
3
  "type": "module",
4
- "version": "0.0.129",
4
+ "version": "0.0.131",
5
5
  "author": "Dacely",
6
6
  "description": "The modern React framework: a file-based React frontend and a ToilScript-compiled WebAssembly backend.",
7
7
  "repository": {