bitwrench 2.0.14 → 2.0.16
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 +57 -21
- package/dist/bitwrench-bccl.cjs.js +3746 -0
- package/dist/bitwrench-bccl.cjs.min.js +40 -0
- package/dist/bitwrench-bccl.esm.js +3741 -0
- package/dist/bitwrench-bccl.esm.min.js +40 -0
- package/dist/bitwrench-bccl.umd.js +3752 -0
- package/dist/bitwrench-bccl.umd.min.js +40 -0
- package/dist/bitwrench-code-edit.cjs.js +99 -49
- package/dist/bitwrench-code-edit.cjs.min.js +23 -0
- package/dist/bitwrench-code-edit.es5.js +79 -16
- package/dist/bitwrench-code-edit.es5.min.js +9 -2
- package/dist/bitwrench-code-edit.esm.js +99 -49
- package/dist/bitwrench-code-edit.esm.min.js +9 -2
- package/dist/bitwrench-code-edit.umd.js +99 -49
- package/dist/bitwrench-code-edit.umd.min.js +9 -2
- package/dist/bitwrench-lean.cjs.js +4923 -3248
- package/dist/bitwrench-lean.cjs.min.js +35 -6
- package/dist/bitwrench-lean.es5.js +6325 -4580
- package/dist/bitwrench-lean.es5.min.js +32 -3
- package/dist/bitwrench-lean.esm.js +4923 -3248
- package/dist/bitwrench-lean.esm.min.js +35 -6
- package/dist/bitwrench-lean.umd.js +4923 -3248
- package/dist/bitwrench-lean.umd.min.js +35 -6
- package/dist/bitwrench.cjs.js +5082 -3667
- package/dist/bitwrench.cjs.min.js +38 -8
- package/dist/bitwrench.css +2289 -6034
- package/dist/bitwrench.es5.js +6862 -5346
- package/dist/bitwrench.es5.min.js +34 -5
- package/dist/bitwrench.esm.js +5082 -3667
- package/dist/bitwrench.esm.min.js +38 -8
- package/dist/bitwrench.min.css +1 -0
- package/dist/bitwrench.umd.js +5082 -3667
- package/dist/bitwrench.umd.min.js +38 -8
- package/dist/builds.json +184 -74
- package/dist/bwserve.cjs.js +646 -0
- package/dist/bwserve.esm.js +638 -0
- package/dist/sri.json +36 -26
- package/package.json +23 -6
- package/readme.html +71 -32
- package/src/bitwrench-bccl-entry.js +72 -0
- package/src/{bitwrench-components-v2.js → bitwrench-bccl.js} +396 -647
- package/src/bitwrench-code-edit.js +98 -48
- package/src/bitwrench-color-utils.js +24 -18
- package/src/bitwrench-components-stub.js +4 -1
- package/src/bitwrench-file-ops.js +180 -0
- package/src/bitwrench-lean.js +2 -2
- package/src/bitwrench-styles.js +1287 -4029
- package/src/bitwrench-utils.js +458 -0
- package/src/bitwrench.js +2070 -1292
- package/src/bwserve/client.js +182 -0
- package/src/bwserve/index.js +352 -0
- package/src/bwserve/shell.js +103 -0
- package/src/cli/index.js +36 -15
- package/src/cli/layout-default.js +18 -18
- package/src/cli/serve.js +325 -0
- package/src/generate-css.js +73 -53
- package/src/version.js +3 -3
- package/src/bitwrench-component-base.js +0 -736
- package/src/bitwrench-components-inline.js +0 -374
- package/src/bitwrench-components.js +0 -610
- /package/bin/{bitwrench.js → bwcli.js} +0 -0
package/src/cli/index.js
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* bwcli — Main entry point for the bitwrench command-line tool
|
|
3
3
|
* Arg parsing with util.parseArgs(), help, version, dispatch
|
|
4
|
+
*
|
|
5
|
+
* Subcommands:
|
|
6
|
+
* bwcli <file> [options] Convert a file to styled HTML
|
|
7
|
+
* bwcli serve [dir] [options] Start bwserve dev server
|
|
4
8
|
*/
|
|
5
9
|
|
|
6
10
|
import { parseArgs } from 'node:util';
|
|
7
11
|
import { VERSION } from '../version.js';
|
|
8
12
|
import { convertFile } from './convert.js';
|
|
13
|
+
import { runServe } from './serve.js';
|
|
9
14
|
|
|
10
15
|
const USAGE = `
|
|
11
|
-
|
|
16
|
+
bwcli v${VERSION} — bitwrench command-line tool
|
|
12
17
|
|
|
13
18
|
Usage:
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
bwcli <file> [options] Convert a file to styled HTML
|
|
20
|
+
bwcli serve [dir] [options] Start bwserve development server
|
|
21
|
+
bwcli --version Print version
|
|
22
|
+
bwcli --help Print this help
|
|
17
23
|
|
|
18
|
-
|
|
24
|
+
Convert options:
|
|
19
25
|
-o, --output <file> Output file path (default: input with .html extension)
|
|
20
26
|
-c, --css <file> Include external CSS file
|
|
21
27
|
-t, --theme <name> Theme preset (ocean, sunset, forest, slate) or hex colors ("#pri,#sec")
|
|
@@ -26,16 +32,26 @@ Options:
|
|
|
26
32
|
-f, --favicon <path> Favicon path or URL
|
|
27
33
|
--highlight Include highlight.js for syntax highlighting
|
|
28
34
|
-v, --verbose Verbose output
|
|
35
|
+
|
|
36
|
+
Serve options:
|
|
37
|
+
-p, --port <number> Port to listen on (default: 7902)
|
|
38
|
+
-t, --theme <name> Theme preset or hex colors
|
|
39
|
+
--open Open browser on start
|
|
40
|
+
-v, --verbose Verbose output
|
|
41
|
+
|
|
42
|
+
General:
|
|
29
43
|
-h, --help Print this help
|
|
30
44
|
--version Print version
|
|
31
45
|
|
|
32
46
|
Examples:
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
47
|
+
bwcli README.md Convert README.md to README.html
|
|
48
|
+
bwcli README.md -o index.html Specify output file
|
|
49
|
+
bwcli README.md -o out.html --theme ocean Apply ocean theme
|
|
50
|
+
bwcli README.md -o out.html --standalone Self-contained offline HTML
|
|
51
|
+
bwcli README.md -o out.html --highlight With syntax highlighting
|
|
52
|
+
bwcli doc.md --theme "#336699,#cc6633" Custom theme colors
|
|
53
|
+
bwcli serve Serve current directory on port 7902
|
|
54
|
+
bwcli serve ./site --port 8080 Serve ./site on port 8080
|
|
39
55
|
`.trim();
|
|
40
56
|
|
|
41
57
|
/**
|
|
@@ -43,6 +59,11 @@ Examples:
|
|
|
43
59
|
* @param {string[]} argv - process.argv.slice(2)
|
|
44
60
|
*/
|
|
45
61
|
export function run(argv) {
|
|
62
|
+
// Check for subcommand before parseArgs (subcommands have different options)
|
|
63
|
+
if (argv.length > 0 && argv[0] === 'serve') {
|
|
64
|
+
return runServe(argv.slice(1));
|
|
65
|
+
}
|
|
66
|
+
|
|
46
67
|
let values, positionals;
|
|
47
68
|
|
|
48
69
|
try {
|
|
@@ -69,13 +90,13 @@ export function run(argv) {
|
|
|
69
90
|
positionals = result.positionals;
|
|
70
91
|
} catch (err) {
|
|
71
92
|
console.error(`Error: ${err.message}`);
|
|
72
|
-
console.error('Run "
|
|
93
|
+
console.error('Run "bwcli --help" for usage.');
|
|
73
94
|
process.exit(1);
|
|
74
95
|
}
|
|
75
96
|
|
|
76
97
|
// --version
|
|
77
98
|
if (values.version) {
|
|
78
|
-
console.log(`
|
|
99
|
+
console.log(`bwcli v${VERSION}`);
|
|
79
100
|
return;
|
|
80
101
|
}
|
|
81
102
|
|
|
@@ -88,7 +109,7 @@ export function run(argv) {
|
|
|
88
109
|
// No positional args → error
|
|
89
110
|
if (positionals.length === 0) {
|
|
90
111
|
console.error('Error: No input file specified.');
|
|
91
|
-
console.error('Run "
|
|
112
|
+
console.error('Run "bwcli --help" for usage.');
|
|
92
113
|
process.exit(1);
|
|
93
114
|
}
|
|
94
115
|
|
|
@@ -9,7 +9,7 @@ import bw from '../bitwrench.js';
|
|
|
9
9
|
* Base page CSS for the CLI-generated pages
|
|
10
10
|
*/
|
|
11
11
|
const BASE_PAGE_CSS = `
|
|
12
|
-
.
|
|
12
|
+
.bw_cli_page {
|
|
13
13
|
max-width: 48rem;
|
|
14
14
|
margin: 0 auto;
|
|
15
15
|
padding: 2rem 1.5rem;
|
|
@@ -18,61 +18,61 @@ const BASE_PAGE_CSS = `
|
|
|
18
18
|
line-height: 1.6;
|
|
19
19
|
color: #333;
|
|
20
20
|
}
|
|
21
|
-
.
|
|
21
|
+
.bw_cli_page pre {
|
|
22
22
|
overflow-x: auto;
|
|
23
23
|
padding: 1em;
|
|
24
24
|
background: #f5f5f5;
|
|
25
25
|
border-radius: 4px;
|
|
26
26
|
font-size: 0.875em;
|
|
27
27
|
}
|
|
28
|
-
.
|
|
28
|
+
.bw_cli_page code {
|
|
29
29
|
font-family: "SF Mono", "Fira Code", "Fira Mono", "Roboto Mono", Menlo, Courier, monospace;
|
|
30
30
|
}
|
|
31
|
-
.
|
|
31
|
+
.bw_cli_page p code {
|
|
32
32
|
background: #f0f0f0;
|
|
33
33
|
padding: 0.15em 0.3em;
|
|
34
34
|
border-radius: 3px;
|
|
35
35
|
font-size: 0.875em;
|
|
36
36
|
}
|
|
37
|
-
.
|
|
37
|
+
.bw_cli_page table {
|
|
38
38
|
border-collapse: collapse;
|
|
39
39
|
width: 100%;
|
|
40
40
|
margin: 1em 0;
|
|
41
41
|
overflow-x: auto;
|
|
42
42
|
display: block;
|
|
43
43
|
}
|
|
44
|
-
.
|
|
44
|
+
.bw_cli_page th, .bw_cli_page td {
|
|
45
45
|
border: 1px solid #ddd;
|
|
46
46
|
padding: 0.5em 0.75em;
|
|
47
47
|
text-align: left;
|
|
48
48
|
}
|
|
49
|
-
.
|
|
49
|
+
.bw_cli_page th {
|
|
50
50
|
background: #f5f5f5;
|
|
51
51
|
font-weight: 600;
|
|
52
52
|
}
|
|
53
|
-
.
|
|
53
|
+
.bw_cli_page blockquote {
|
|
54
54
|
border-left: 4px solid #ddd;
|
|
55
55
|
margin-left: 0;
|
|
56
56
|
padding-left: 1em;
|
|
57
57
|
color: #666;
|
|
58
58
|
}
|
|
59
|
-
.
|
|
59
|
+
.bw_cli_page img {
|
|
60
60
|
max-width: 100%;
|
|
61
61
|
height: auto;
|
|
62
62
|
}
|
|
63
|
-
.
|
|
64
|
-
.
|
|
63
|
+
.bw_cli_page h1, .bw_cli_page h2, .bw_cli_page h3,
|
|
64
|
+
.bw_cli_page h4, .bw_cli_page h5, .bw_cli_page h6 {
|
|
65
65
|
margin-top: 1.5em;
|
|
66
66
|
margin-bottom: 0.5em;
|
|
67
67
|
line-height: 1.25;
|
|
68
68
|
}
|
|
69
|
-
.
|
|
70
|
-
.
|
|
71
|
-
.
|
|
72
|
-
.
|
|
73
|
-
.
|
|
69
|
+
.bw_cli_page h1 { font-size: 2em; }
|
|
70
|
+
.bw_cli_page h2 { font-size: 1.5em; border-bottom: 1px solid #eee; padding-bottom: 0.3em; }
|
|
71
|
+
.bw_cli_page a { color: #0366d6; text-decoration: none; }
|
|
72
|
+
.bw_cli_page a:hover { text-decoration: underline; }
|
|
73
|
+
.bw_cli_page hr { border: none; border-top: 1px solid #eee; margin: 2em 0; }
|
|
74
74
|
@media (max-width: 600px) {
|
|
75
|
-
.
|
|
75
|
+
.bw_cli_page { padding: 1rem; }
|
|
76
76
|
}
|
|
77
77
|
`;
|
|
78
78
|
|
|
@@ -131,7 +131,7 @@ ${faviconTag}${headInjection}${highlightHead}
|
|
|
131
131
|
<style>${allCSS}</style>
|
|
132
132
|
</head>
|
|
133
133
|
<body>
|
|
134
|
-
<div class="
|
|
134
|
+
<div class="bw_cli_page">
|
|
135
135
|
${bodyHTML}
|
|
136
136
|
</div>
|
|
137
137
|
${bodyEndInjection}${highlightBodyEnd}
|
package/src/cli/serve.js
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bwcli serve — CLI subcommand for bwserve pipe server.
|
|
3
|
+
*
|
|
4
|
+
* Serves a directory of bitwrench pages and accepts protocol messages
|
|
5
|
+
* via an input HTTP port or stdin. Broadcasts messages to all connected
|
|
6
|
+
* browser tabs.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* bwcli serve [dir] [--port N] [--listen N] [--stdin] [--theme name]
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { parseArgs } from 'node:util';
|
|
13
|
+
import { createServer } from 'node:http';
|
|
14
|
+
import { createReadStream } from 'node:fs';
|
|
15
|
+
import { VERSION } from '../version.js';
|
|
16
|
+
|
|
17
|
+
var SERVE_USAGE = `
|
|
18
|
+
bwcli serve v${VERSION} — Pipe server for browser UI
|
|
19
|
+
|
|
20
|
+
Usage:
|
|
21
|
+
bwcli serve [dir] [options]
|
|
22
|
+
|
|
23
|
+
Arguments:
|
|
24
|
+
dir Directory to serve static files from (default: .)
|
|
25
|
+
|
|
26
|
+
Options:
|
|
27
|
+
-p, --port <number> Browser-facing web port (default: 8080)
|
|
28
|
+
-l, --listen <number> Input port for protocol messages (default: 9000)
|
|
29
|
+
--stdin Read protocol messages from stdin (newline-delimited JSON)
|
|
30
|
+
-t, --theme <name> Theme preset or hex colors ("#pri,#sec")
|
|
31
|
+
--title <string> Page title (default: "bwcli serve")
|
|
32
|
+
--open Open browser on start
|
|
33
|
+
-v, --verbose Verbose output
|
|
34
|
+
-h, --help Print this help
|
|
35
|
+
|
|
36
|
+
Examples:
|
|
37
|
+
bwcli serve Serve . on :8080, listen on :9000
|
|
38
|
+
bwcli serve ./public --port 3000 Serve ./public on :3000
|
|
39
|
+
bwcli serve --stdin Read from pipe instead of input port
|
|
40
|
+
sensor-reader | bwcli serve --stdin Pipe sensor data to browser
|
|
41
|
+
curl -X POST :9000 -d '{"type":"replace","target":"#app","node":{"t":"h1","c":"Hi"}}'
|
|
42
|
+
`.trim();
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Parse a message string — supports both strict JSON and r-prefixed relaxed JSON.
|
|
46
|
+
* @param {string} str
|
|
47
|
+
* @returns {Object|null} parsed message or null on error
|
|
48
|
+
*/
|
|
49
|
+
function parseMessage(str) {
|
|
50
|
+
str = str.trim();
|
|
51
|
+
if (!str) return null;
|
|
52
|
+
try {
|
|
53
|
+
if (str.charAt(0) === 'r') {
|
|
54
|
+
return parseRelaxedJSON(str.slice(1));
|
|
55
|
+
}
|
|
56
|
+
return JSON.parse(str);
|
|
57
|
+
} catch (e) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Parse relaxed JSON (single-quoted strings, trailing commas).
|
|
64
|
+
* State machine — walks char by char.
|
|
65
|
+
*/
|
|
66
|
+
function parseRelaxedJSON(str) {
|
|
67
|
+
var out = [];
|
|
68
|
+
var i = 0;
|
|
69
|
+
var len = str.length;
|
|
70
|
+
|
|
71
|
+
while (i < len) {
|
|
72
|
+
var ch = str[i];
|
|
73
|
+
|
|
74
|
+
if (ch === "'") {
|
|
75
|
+
out.push('"');
|
|
76
|
+
i++;
|
|
77
|
+
while (i < len) {
|
|
78
|
+
var c = str[i];
|
|
79
|
+
if (c === '\\' && i + 1 < len) {
|
|
80
|
+
var next = str[i + 1];
|
|
81
|
+
if (next === "'") {
|
|
82
|
+
out.push("'");
|
|
83
|
+
} else {
|
|
84
|
+
out.push('\\');
|
|
85
|
+
out.push(next);
|
|
86
|
+
}
|
|
87
|
+
i += 2;
|
|
88
|
+
} else if (c === '"') {
|
|
89
|
+
out.push('\\"');
|
|
90
|
+
i++;
|
|
91
|
+
} else if (c === "'") {
|
|
92
|
+
break;
|
|
93
|
+
} else {
|
|
94
|
+
out.push(c);
|
|
95
|
+
i++;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
out.push('"');
|
|
99
|
+
i++;
|
|
100
|
+
} else if (ch === '"') {
|
|
101
|
+
out.push(ch);
|
|
102
|
+
i++;
|
|
103
|
+
while (i < len) {
|
|
104
|
+
var c2 = str[i];
|
|
105
|
+
if (c2 === '\\' && i + 1 < len) {
|
|
106
|
+
out.push(c2);
|
|
107
|
+
out.push(str[i + 1]);
|
|
108
|
+
i += 2;
|
|
109
|
+
} else {
|
|
110
|
+
out.push(c2);
|
|
111
|
+
i++;
|
|
112
|
+
if (c2 === '"') break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} else if (ch === ',') {
|
|
116
|
+
var j = i + 1;
|
|
117
|
+
while (j < len && (str[j] === ' ' || str[j] === '\t' || str[j] === '\n' || str[j] === '\r')) j++;
|
|
118
|
+
if (j < len && (str[j] === '}' || str[j] === ']')) {
|
|
119
|
+
i++;
|
|
120
|
+
} else {
|
|
121
|
+
out.push(ch);
|
|
122
|
+
i++;
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
out.push(ch);
|
|
126
|
+
i++;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return JSON.parse(out.join(''));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Run the serve subcommand.
|
|
135
|
+
* @param {string[]} argv - arguments after "serve"
|
|
136
|
+
*/
|
|
137
|
+
export function runServe(argv) {
|
|
138
|
+
var values, positionals;
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
var result = parseArgs({
|
|
142
|
+
args: argv,
|
|
143
|
+
strict: true,
|
|
144
|
+
allowPositionals: true,
|
|
145
|
+
options: {
|
|
146
|
+
port: { type: 'string', short: 'p' },
|
|
147
|
+
listen: { type: 'string', short: 'l' },
|
|
148
|
+
stdin: { type: 'boolean' },
|
|
149
|
+
theme: { type: 'string', short: 't' },
|
|
150
|
+
title: { type: 'string' },
|
|
151
|
+
open: { type: 'boolean' },
|
|
152
|
+
verbose: { type: 'boolean', short: 'v' },
|
|
153
|
+
help: { type: 'boolean', short: 'h' }
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
values = result.values;
|
|
157
|
+
positionals = result.positionals;
|
|
158
|
+
} catch (err) {
|
|
159
|
+
console.error('Error: ' + err.message);
|
|
160
|
+
console.error('Run "bwcli serve --help" for usage.');
|
|
161
|
+
process.exit(1);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (values.help) {
|
|
165
|
+
console.log(SERVE_USAGE);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
var dir = positionals[0] || '.';
|
|
170
|
+
var webPort = values.port ? parseInt(values.port, 10) : 8080;
|
|
171
|
+
var listenPort = values.listen ? parseInt(values.listen, 10) : 9000;
|
|
172
|
+
var useStdin = !!values.stdin;
|
|
173
|
+
var theme = values.theme || null;
|
|
174
|
+
var title = values.title || 'bwcli serve';
|
|
175
|
+
var verbose = !!values.verbose;
|
|
176
|
+
|
|
177
|
+
if (isNaN(webPort) || webPort < 1 || webPort > 65535) {
|
|
178
|
+
console.error('Error: --port must be a number between 1 and 65535.');
|
|
179
|
+
process.exit(1);
|
|
180
|
+
}
|
|
181
|
+
if (!useStdin && (isNaN(listenPort) || listenPort < 1 || listenPort > 65535)) {
|
|
182
|
+
console.error('Error: --listen must be a number between 1 and 65535.');
|
|
183
|
+
process.exit(1);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Dynamic import of bwserve to avoid loading it at parse time
|
|
187
|
+
import('../../src/bwserve/index.js').then(function(bwserve) {
|
|
188
|
+
startServer(bwserve, {
|
|
189
|
+
dir: dir,
|
|
190
|
+
webPort: webPort,
|
|
191
|
+
listenPort: listenPort,
|
|
192
|
+
useStdin: useStdin,
|
|
193
|
+
theme: theme,
|
|
194
|
+
title: title,
|
|
195
|
+
verbose: verbose,
|
|
196
|
+
open: !!values.open
|
|
197
|
+
});
|
|
198
|
+
}).catch(function(err) {
|
|
199
|
+
console.error('Failed to load bwserve: ' + err.message);
|
|
200
|
+
process.exit(1);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Start the bwserve pipe server.
|
|
206
|
+
*/
|
|
207
|
+
function startServer(bwserve, opts) {
|
|
208
|
+
var app = bwserve.create({
|
|
209
|
+
port: opts.webPort,
|
|
210
|
+
title: opts.title,
|
|
211
|
+
static: opts.dir,
|
|
212
|
+
theme: opts.theme
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// Register a passthrough page handler — just keeps clients alive
|
|
216
|
+
app.page('/', function(client) {
|
|
217
|
+
if (opts.verbose) {
|
|
218
|
+
console.error('[bwcli serve] Client connected: ' + client.id);
|
|
219
|
+
}
|
|
220
|
+
client.on('_disconnect', function() {
|
|
221
|
+
if (opts.verbose) {
|
|
222
|
+
console.error('[bwcli serve] Client disconnected: ' + client.id);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// Start web server
|
|
228
|
+
app.listen(function() {
|
|
229
|
+
console.error('bwcli serve v' + VERSION);
|
|
230
|
+
console.error(' Web server: http://localhost:' + opts.webPort);
|
|
231
|
+
console.error(' Static dir: ' + opts.dir);
|
|
232
|
+
if (opts.theme) console.error(' Theme: ' + opts.theme);
|
|
233
|
+
|
|
234
|
+
if (opts.useStdin) {
|
|
235
|
+
console.error(' Input: stdin (newline-delimited JSON)');
|
|
236
|
+
startStdinReader(app, opts.verbose);
|
|
237
|
+
} else {
|
|
238
|
+
console.error(' Input port: http://localhost:' + opts.listenPort);
|
|
239
|
+
startInputServer(app, opts.listenPort, opts.verbose);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
console.error('');
|
|
243
|
+
console.error('Ready. Send protocol messages to push UI to browsers.');
|
|
244
|
+
|
|
245
|
+
if (opts.open) {
|
|
246
|
+
import('node:child_process').then(function(cp) {
|
|
247
|
+
var url = 'http://localhost:' + opts.webPort;
|
|
248
|
+
var cmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';
|
|
249
|
+
cp.exec(cmd + ' ' + url);
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Start the input HTTP server for receiving protocol messages.
|
|
257
|
+
*/
|
|
258
|
+
function startInputServer(app, listenPort, verbose) {
|
|
259
|
+
var inputServer = createServer(function(req, res) {
|
|
260
|
+
if (req.method !== 'POST') {
|
|
261
|
+
res.writeHead(405, { 'Content-Type': 'application/json' });
|
|
262
|
+
res.end(JSON.stringify({ error: 'Use POST' }));
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
var body = '';
|
|
267
|
+
req.on('data', function(chunk) { body += chunk; });
|
|
268
|
+
req.on('end', function() {
|
|
269
|
+
var msg = parseMessage(body);
|
|
270
|
+
if (!msg) {
|
|
271
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
272
|
+
res.end(JSON.stringify({ error: 'Invalid message' }));
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
var count = app.broadcast(msg);
|
|
277
|
+
if (verbose) {
|
|
278
|
+
console.error('[input] ' + msg.type + ' -> ' + count + ' client(s)');
|
|
279
|
+
}
|
|
280
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
281
|
+
res.end(JSON.stringify({ ok: true, clients: count }));
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
inputServer.listen(listenPort);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Read protocol messages from stdin (newline-delimited).
|
|
290
|
+
*/
|
|
291
|
+
function startStdinReader(app, verbose) {
|
|
292
|
+
var buffer = '';
|
|
293
|
+
|
|
294
|
+
process.stdin.setEncoding('utf8');
|
|
295
|
+
process.stdin.on('data', function(chunk) {
|
|
296
|
+
buffer += chunk;
|
|
297
|
+
var lines = buffer.split('\n');
|
|
298
|
+
buffer = lines.pop() || '';
|
|
299
|
+
|
|
300
|
+
for (var i = 0; i < lines.length; i++) {
|
|
301
|
+
var line = lines[i].trim();
|
|
302
|
+
if (!line) continue;
|
|
303
|
+
|
|
304
|
+
var msg = parseMessage(line);
|
|
305
|
+
if (!msg) {
|
|
306
|
+
if (verbose) console.error('[stdin] Parse error: ' + line.slice(0, 80));
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
var count = app.broadcast(msg);
|
|
311
|
+
if (verbose) {
|
|
312
|
+
console.error('[stdin] ' + msg.type + ' -> ' + count + ' client(s)');
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
process.stdin.on('end', function() {
|
|
318
|
+
// Flush remaining buffer
|
|
319
|
+
if (buffer.trim()) {
|
|
320
|
+
var msg = parseMessage(buffer.trim());
|
|
321
|
+
if (msg) app.broadcast(msg);
|
|
322
|
+
}
|
|
323
|
+
if (verbose) console.error('[stdin] Input stream closed. Server stays running.');
|
|
324
|
+
});
|
|
325
|
+
}
|