readingroom 0.1.0
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/LICENSE +21 -0
- package/README.md +59 -0
- package/bin/readingroom.js +90 -0
- package/dist/assets/bash-BiGZg9Om.js +1 -0
- package/dist/assets/c-DWGmR78Y.js +1 -0
- package/dist/assets/c-SSSlmE39.js +1 -0
- package/dist/assets/cpp-CJIxL8VH.js +1 -0
- package/dist/assets/cpp-Cts1PyIS.js +1 -0
- package/dist/assets/csharp-oqKa8noW.js +1 -0
- package/dist/assets/css-CdtkT07m.js +1 -0
- package/dist/assets/css-LbU1hoFO.js +1 -0
- package/dist/assets/diff-woXpYk--.js +1 -0
- package/dist/assets/docker-IyjqRm3v.js +1 -0
- package/dist/assets/go-rLFTqkRN.js +1 -0
- package/dist/assets/graphql-BeoSofCO.js +1 -0
- package/dist/assets/graphql-DaDNVHrz.js +1 -0
- package/dist/assets/html-BcACZWSP.js +1 -0
- package/dist/assets/html-DDNsd9lV.js +1 -0
- package/dist/assets/index-BUv67KbK.css +2 -0
- package/dist/assets/index-BpmvxNpC.js +190 -0
- package/dist/assets/ini-B5eOa1yu.js +1 -0
- package/dist/assets/java-BtIQNnN0.js +1 -0
- package/dist/assets/java-DxlWrXwe.js +1 -0
- package/dist/assets/javascript-BzbQS41l.js +1 -0
- package/dist/assets/javascript-Yelw-5ZN.js +1 -0
- package/dist/assets/json-B88Oqtu5.js +1 -0
- package/dist/assets/json-CaljABEy.js +1 -0
- package/dist/assets/kotlin-DhhofPvG.js +1 -0
- package/dist/assets/lua-BJObVtAx.js +1 -0
- package/dist/assets/lua-DjuecEsk.js +1 -0
- package/dist/assets/markdown-BYOwaDjH.js +1 -0
- package/dist/assets/php-CQT4wx5i.js +1 -0
- package/dist/assets/python-gzcpVVnB.js +1 -0
- package/dist/assets/ruby-CFZGJEm6.js +1 -0
- package/dist/assets/rust-Cfkwpbl8.js +1 -0
- package/dist/assets/shellscript-CqHdAlCp.js +1 -0
- package/dist/assets/sql-0Qcw9ht3.js +1 -0
- package/dist/assets/sql-CP0rg-5b.js +1 -0
- package/dist/assets/swift-CyEgAFGc.js +1 -0
- package/dist/assets/toml-CcmNWLt0.js +1 -0
- package/dist/assets/tsx-5f3ulCja.js +1 -0
- package/dist/assets/tsx-BOvsFOI6.js +1 -0
- package/dist/assets/typescript-Utq2Cl8c.js +1 -0
- package/dist/assets/typescript-j_1H8WHN.js +1 -0
- package/dist/assets/xml-CoDIzWF0.js +1 -0
- package/dist/assets/xml-WY0zc4PG.js +1 -0
- package/dist/assets/yaml-CxbubiML.js +1 -0
- package/dist/assets/yaml-DijMHRDA.js +1 -0
- package/dist/index.html +18 -0
- package/package.json +55 -0
- package/src/openBrowser.js +22 -0
- package/src/scanner.js +198 -0
- package/src/server.js +301 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=[Object.freeze(JSON.parse(`{"displayName":"YAML","fileTypes":["yaml","yml","rviz","reek","clang-format","yaml-tmlanguage","syntax","sublime-syntax"],"firstLineMatch":"^%YAML( ?1.\\\\d+)?","name":"yaml","patterns":[{"include":"#comment"},{"include":"#property"},{"include":"#directive"},{"match":"^---","name":"entity.other.document.begin.yaml"},{"match":"^\\\\.{3}","name":"entity.other.document.end.yaml"},{"include":"#node"}],"repository":{"block-collection":{"patterns":[{"include":"#block-sequence"},{"include":"#block-mapping"}]},"block-mapping":{"patterns":[{"include":"#block-pair"}]},"block-node":{"patterns":[{"include":"#prototype"},{"include":"#block-scalar"},{"include":"#block-collection"},{"include":"#flow-scalar-plain-out"},{"include":"#flow-node"}]},"block-pair":{"patterns":[{"begin":"\\\\?","beginCaptures":{"1":{"name":"punctuation.definition.key-value.begin.yaml"}},"end":"(?=\\\\?)|^ *(:)|(:)","endCaptures":{"1":{"name":"punctuation.separator.key-value.mapping.yaml"},"2":{"name":"invalid.illegal.expected-newline.yaml"}},"name":"meta.block-mapping.yaml","patterns":[{"include":"#block-node"}]},{"begin":"(?=(?:[^-\\\\]!\\"#%\\\\&'*,:>?@\\\\[\`{|}\\\\s]|[-:?]\\\\S)([^:\\\\s]|:\\\\S|\\\\s+(?![#\\\\s]))*\\\\s*:(\\\\s|$))","end":"(?=\\\\s*$|\\\\s+#|\\\\s*:(\\\\s|$))","patterns":[{"include":"#flow-scalar-plain-out-implicit-type"},{"begin":"[^-\\\\]!\\"#%\\\\&'*,:>?@\\\\[\`{|}\\\\s]|[-:?]\\\\S","beginCaptures":{"0":{"name":"entity.name.tag.yaml"}},"contentName":"entity.name.tag.yaml","end":"(?=\\\\s*$|\\\\s+#|\\\\s*:(\\\\s|$))","name":"string.unquoted.plain.out.yaml"}]},{"match":":(?=\\\\s|$)","name":"punctuation.separator.key-value.mapping.yaml"}]},"block-scalar":{"begin":"(?:(\\\\|)|(>))([1-9])?([-+])?(.*\\\\n?)","beginCaptures":{"1":{"name":"keyword.control.flow.block-scalar.literal.yaml"},"2":{"name":"keyword.control.flow.block-scalar.folded.yaml"},"3":{"name":"constant.numeric.indentation-indicator.yaml"},"4":{"name":"storage.modifier.chomping-indicator.yaml"},"5":{"patterns":[{"include":"#comment"},{"match":".+","name":"invalid.illegal.expected-comment-or-newline.yaml"}]}},"end":"^(?=\\\\S)|(?!\\\\G)","patterns":[{"begin":"^( +)(?! )","end":"^(?!\\\\1|\\\\s*$)","name":"string.unquoted.block.yaml"}]},"block-sequence":{"match":"(-)(?!\\\\S)","name":"punctuation.definition.block.sequence.item.yaml"},"comment":{"begin":"(?:^([\\\\t ]*)|[\\\\t ]+)(?=#\\\\p{print}*$)","beginCaptures":{"1":{"name":"punctuation.whitespace.comment.leading.yaml"}},"end":"(?!\\\\G)","patterns":[{"begin":"#","beginCaptures":{"0":{"name":"punctuation.definition.comment.yaml"}},"end":"\\\\n","name":"comment.line.number-sign.yaml"}]},"directive":{"begin":"^%","beginCaptures":{"0":{"name":"punctuation.definition.directive.begin.yaml"}},"end":"(?=$|[\\\\t ]+($|#))","name":"meta.directive.yaml","patterns":[{"captures":{"1":{"name":"keyword.other.directive.yaml.yaml"},"2":{"name":"constant.numeric.yaml-version.yaml"}},"match":"\\\\G(YAML)[\\\\t ]+(\\\\d+\\\\.\\\\d+)"},{"captures":{"1":{"name":"keyword.other.directive.tag.yaml"},"2":{"name":"storage.type.tag-handle.yaml"},"3":{"name":"support.type.tag-prefix.yaml"}},"match":"\\\\G(TAG)(?:[\\\\t ]+(!(?:[-0-9A-Za-z]*!)?)(?:[\\\\t ]+(!(?:%\\\\h{2}|[]!#$\\\\&-;=?-\\\\[_a-z~])*|(?![]!,\\\\[{}])(?:%\\\\h{2}|[]!#$\\\\&-;=?-\\\\[_a-z~])+))?)?"},{"captures":{"1":{"name":"support.other.directive.reserved.yaml"},"2":{"name":"string.unquoted.directive-name.yaml"},"3":{"name":"string.unquoted.directive-parameter.yaml"}},"match":"\\\\G(\\\\w+)(?:[\\\\t ]+(\\\\w+)(?:[\\\\t ]+(\\\\w+))?)?"},{"match":"\\\\S+","name":"invalid.illegal.unrecognized.yaml"}]},"flow-alias":{"captures":{"1":{"name":"keyword.control.flow.alias.yaml"},"2":{"name":"punctuation.definition.alias.yaml"},"3":{"name":"variable.other.alias.yaml"},"4":{"name":"invalid.illegal.character.anchor.yaml"}},"match":"((\\\\*))([^],/\\\\[{}\\\\s]+)([^],}\\\\s]\\\\S*)?"},"flow-collection":{"patterns":[{"include":"#flow-sequence"},{"include":"#flow-mapping"}]},"flow-mapping":{"begin":"\\\\{","beginCaptures":{"0":{"name":"punctuation.definition.mapping.begin.yaml"}},"end":"}","endCaptures":{"0":{"name":"punctuation.definition.mapping.end.yaml"}},"name":"meta.flow-mapping.yaml","patterns":[{"include":"#prototype"},{"match":",","name":"punctuation.separator.mapping.yaml"},{"include":"#flow-pair"}]},"flow-node":{"patterns":[{"include":"#prototype"},{"include":"#flow-alias"},{"include":"#flow-collection"},{"include":"#flow-scalar"}]},"flow-pair":{"patterns":[{"begin":"\\\\?","beginCaptures":{"0":{"name":"punctuation.definition.key-value.begin.yaml"}},"end":"(?=[],}])","name":"meta.flow-pair.explicit.yaml","patterns":[{"include":"#prototype"},{"include":"#flow-pair"},{"include":"#flow-node"},{"begin":":(?=\\\\s|$|[],\\\\[{}])","beginCaptures":{"0":{"name":"punctuation.separator.key-value.mapping.yaml"}},"end":"(?=[],}])","patterns":[{"include":"#flow-value"}]}]},{"begin":"(?=(?:[^-\\\\]!\\"#%\\\\&'*,:>?@\\\\[\`{|}\\\\s]|[-:?][^],\\\\[{}\\\\s])([^],:\\\\[{}\\\\s]|:[^],\\\\[{}\\\\s]|\\\\s+(?![#\\\\s]))*\\\\s*:(\\\\s|$))","end":"(?=\\\\s*$|\\\\s+#|\\\\s*:(\\\\s|$)|\\\\s*:[],\\\\[{}]|\\\\s*[],\\\\[{}])","name":"meta.flow-pair.key.yaml","patterns":[{"include":"#flow-scalar-plain-in-implicit-type"},{"begin":"[^-\\\\]!\\"#%\\\\&'*,:>?@\\\\[\`{|}\\\\s]|[-:?][^],\\\\[{}\\\\s]","beginCaptures":{"0":{"name":"entity.name.tag.yaml"}},"contentName":"entity.name.tag.yaml","end":"(?=\\\\s*$|\\\\s+#|\\\\s*:(\\\\s|$)|\\\\s*:[],\\\\[{}]|\\\\s*[],\\\\[{}])","name":"string.unquoted.plain.in.yaml"}]},{"include":"#flow-node"},{"begin":":(?=\\\\s|$|[],\\\\[{}])","captures":{"0":{"name":"punctuation.separator.key-value.mapping.yaml"}},"end":"(?=[],}])","name":"meta.flow-pair.yaml","patterns":[{"include":"#flow-value"}]}]},"flow-scalar":{"patterns":[{"include":"#flow-scalar-double-quoted"},{"include":"#flow-scalar-single-quoted"},{"include":"#flow-scalar-plain-in"}]},"flow-scalar-double-quoted":{"begin":"\\"","beginCaptures":{"0":{"name":"punctuation.definition.string.begin.yaml"}},"end":"\\"","endCaptures":{"0":{"name":"punctuation.definition.string.end.yaml"}},"name":"string.quoted.double.yaml","patterns":[{"match":"\\\\\\\\([ \\"/0LN\\\\\\\\_abefnprtv]|x\\\\d\\\\d|u\\\\d{4}|U\\\\d{8})","name":"constant.character.escape.yaml"},{"match":"\\\\\\\\\\\\n","name":"constant.character.escape.double-quoted.newline.yaml"}]},"flow-scalar-plain-in":{"patterns":[{"include":"#flow-scalar-plain-in-implicit-type"},{"begin":"[^-\\\\]!\\"#%\\\\&'*,:>?@\\\\[\`{|}\\\\s]|[-:?][^],\\\\[{}\\\\s]","end":"(?=\\\\s*$|\\\\s+#|\\\\s*:(\\\\s|$)|\\\\s*:[],\\\\[{}]|\\\\s*[],\\\\[{}])","name":"string.unquoted.plain.in.yaml"}]},"flow-scalar-plain-in-implicit-type":{"patterns":[{"captures":{"1":{"name":"constant.language.null.yaml"},"2":{"name":"constant.language.boolean.yaml"},"3":{"name":"constant.numeric.integer.yaml"},"4":{"name":"constant.numeric.float.yaml"},"5":{"name":"constant.other.timestamp.yaml"},"6":{"name":"constant.language.value.yaml"},"7":{"name":"constant.language.merge.yaml"}},"match":"(?:(null|Null|NULL|~)|([Yy]|yes|Yes|YES|[Nn]|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)|([-+]?0b[01_]+|[-+]?0[0-7_]+|[-+]?(?:0|[1-9][0-9_]*)|[-+]?0x[_\\\\h]+|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)|([-+]?(?:[0-9][0-9_]*)?\\\\.[.0-9]*(?:[Ee][-+][0-9]+)?|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\\\.[0-9_]*|[-+]?\\\\.(?:inf|Inf|INF)|\\\\.(?:nan|NaN|NAN))|(\\\\d{4}-\\\\d{2}-\\\\d{2}|\\\\d{4}-\\\\d{1,2}-\\\\d{1,2}(?:[Tt]|[\\\\t ]+)\\\\d{1,2}:\\\\d{2}:\\\\d{2}(?:\\\\.\\\\d*)?(?:[\\\\t ]*Z|[-+]\\\\d{1,2}(?::\\\\d{1,2})?)?)|(=)|(<<))(?=\\\\s*$|\\\\s+#|\\\\s*:(\\\\s|$)|\\\\s*:[],\\\\[{}]|\\\\s*[],\\\\[{}])"}]},"flow-scalar-plain-out":{"patterns":[{"include":"#flow-scalar-plain-out-implicit-type"},{"begin":"[^-\\\\]!\\"#%\\\\&'*,:>?@\\\\[\`{|}\\\\s]|[-:?]\\\\S","end":"(?=\\\\s*$|\\\\s+#|\\\\s*:(\\\\s|$))","name":"string.unquoted.plain.out.yaml"}]},"flow-scalar-plain-out-implicit-type":{"patterns":[{"captures":{"1":{"name":"constant.language.null.yaml"},"2":{"name":"constant.language.boolean.yaml"},"3":{"name":"constant.numeric.integer.yaml"},"4":{"name":"constant.numeric.float.yaml"},"5":{"name":"constant.other.timestamp.yaml"},"6":{"name":"constant.language.value.yaml"},"7":{"name":"constant.language.merge.yaml"}},"match":"(?:(null|Null|NULL|~)|([Yy]|yes|Yes|YES|[Nn]|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)|([-+]?0b[01_]+|[-+]?0[0-7_]+|[-+]?(?:0|[1-9][0-9_]*)|[-+]?0x[_\\\\h]+|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)|([-+]?(?:[0-9][0-9_]*)?\\\\.[.0-9]*(?:[Ee][-+][0-9]+)?|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\\\.[0-9_]*|[-+]?\\\\.(?:inf|Inf|INF)|\\\\.(?:nan|NaN|NAN))|(\\\\d{4}-\\\\d{2}-\\\\d{2}|\\\\d{4}-\\\\d{1,2}-\\\\d{1,2}(?:[Tt]|[\\\\t ]+)\\\\d{1,2}:\\\\d{2}:\\\\d{2}(?:\\\\.\\\\d*)?(?:[\\\\t ]*Z|[-+]\\\\d{1,2}(?::\\\\d{1,2})?)?)|(=)|(<<))(?=\\\\s*$|\\\\s+#|\\\\s*:(\\\\s|$))"}]},"flow-scalar-single-quoted":{"begin":"'","beginCaptures":{"0":{"name":"punctuation.definition.string.begin.yaml"}},"end":"'(?!')","endCaptures":{"0":{"name":"punctuation.definition.string.end.yaml"}},"name":"string.quoted.single.yaml","patterns":[{"match":"''","name":"constant.character.escape.single-quoted.yaml"}]},"flow-sequence":{"begin":"\\\\[","beginCaptures":{"0":{"name":"punctuation.definition.sequence.begin.yaml"}},"end":"]","endCaptures":{"0":{"name":"punctuation.definition.sequence.end.yaml"}},"name":"meta.flow-sequence.yaml","patterns":[{"include":"#prototype"},{"match":",","name":"punctuation.separator.sequence.yaml"},{"include":"#flow-pair"},{"include":"#flow-node"}]},"flow-value":{"patterns":[{"begin":"\\\\G(?![],}])","end":"(?=[],}])","name":"meta.flow-pair.value.yaml","patterns":[{"include":"#flow-node"}]}]},"node":{"patterns":[{"include":"#block-node"}]},"property":{"begin":"(?=[!\\\\&])","end":"(?!\\\\G)","name":"meta.property.yaml","patterns":[{"captures":{"1":{"name":"keyword.control.property.anchor.yaml"},"2":{"name":"punctuation.definition.anchor.yaml"},"3":{"name":"entity.name.type.anchor.yaml"},"4":{"name":"invalid.illegal.character.anchor.yaml"}},"match":"\\\\G((&))([^],/\\\\[{}\\\\s]+)(\\\\S+)?"},{"match":"\\\\G!(?:<(?:%\\\\h{2}|[]!#$\\\\&-;=?-\\\\[_a-z~])+>|(?:[-0-9A-Za-z]*!)?(?:%\\\\h{2}|[#$\\\\&-+\\\\--;=?-Z_a-z~])+|)(?=[\\\\t ]|$)","name":"storage.type.tag-handle.yaml"},{"match":"\\\\S+","name":"invalid.illegal.tag-handle.yaml"}]},"prototype":{"patterns":[{"include":"#comment"},{"include":"#property"}]}},"scopeName":"source.yaml","aliases":["yml"]}`))];export{e as t};
|
package/dist/index.html
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
+
<meta name="color-scheme" content="dark light" />
|
|
7
|
+
<title>readingroom</title>
|
|
8
|
+
<link
|
|
9
|
+
rel="icon"
|
|
10
|
+
href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Ctext y='.9em' font-size='90'%3E%F0%9F%97%82%EF%B8%8F%3C/text%3E%3C/svg%3E"
|
|
11
|
+
/>
|
|
12
|
+
<script type="module" crossorigin src="/assets/index-BpmvxNpC.js"></script>
|
|
13
|
+
<link rel="stylesheet" crossorigin href="/assets/index-BUv67KbK.css">
|
|
14
|
+
</head>
|
|
15
|
+
<body>
|
|
16
|
+
<div id="root"></div>
|
|
17
|
+
</body>
|
|
18
|
+
</html>
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "readingroom",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Zero-config doc browser for your codebase.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "saif71",
|
|
8
|
+
"homepage": "https://github.com/saif71/readingroom",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/saif71/readingroom.git"
|
|
12
|
+
},
|
|
13
|
+
"bin": {
|
|
14
|
+
"readingroom": "bin/readingroom.js"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"bin",
|
|
18
|
+
"src",
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
],
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=20"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"markdown",
|
|
28
|
+
"docs",
|
|
29
|
+
"documentation",
|
|
30
|
+
"viewer",
|
|
31
|
+
"browser",
|
|
32
|
+
"doc-browser",
|
|
33
|
+
"cli",
|
|
34
|
+
"txt"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "vite build --config web/vite.config.js",
|
|
38
|
+
"dev:server": "node bin/readingroom.js --no-open",
|
|
39
|
+
"dev:web": "vite dev --config web/vite.config.js",
|
|
40
|
+
"test": "node test/smoke.test.mjs",
|
|
41
|
+
"prepack": "npm run build"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@tailwindcss/typography": "^0.5.20",
|
|
45
|
+
"@tailwindcss/vite": "^4.3.3",
|
|
46
|
+
"@vitejs/plugin-react": "^6.0.5",
|
|
47
|
+
"react": "^19.2.8",
|
|
48
|
+
"react-dom": "^19.2.8",
|
|
49
|
+
"react-markdown": "^10.1.0",
|
|
50
|
+
"remark-gfm": "^4.0.1",
|
|
51
|
+
"shiki": "^4.4.3",
|
|
52
|
+
"tailwindcss": "^4.3.3",
|
|
53
|
+
"vite": "^8.2.1"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
|
|
3
|
+
export function openBrowser(url) {
|
|
4
|
+
let cmd;
|
|
5
|
+
let args;
|
|
6
|
+
if (process.platform === 'darwin') {
|
|
7
|
+
cmd = 'open';
|
|
8
|
+
args = [url];
|
|
9
|
+
} else if (process.platform === 'win32') {
|
|
10
|
+
cmd = 'cmd';
|
|
11
|
+
args = ['/c', 'start', '', url];
|
|
12
|
+
} else {
|
|
13
|
+
cmd = 'xdg-open';
|
|
14
|
+
args = [url];
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
const child = spawn(cmd, args, { stdio: 'ignore', detached: true });
|
|
18
|
+
child.unref();
|
|
19
|
+
} catch {
|
|
20
|
+
/* opening the browser is best-effort; the URL is printed regardless */
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/scanner.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
export const TEXT_EXTENSIONS = ['.md', '.txt'];
|
|
5
|
+
export const IMAGE_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.webp', '.gif', '.avif', '.svg'];
|
|
6
|
+
export const MAX_TEXT_SIZE = 2 * 1024 * 1024; // text files above this are excluded from the tree
|
|
7
|
+
export const MAX_IMAGE_SIZE = 50 * 1024 * 1024;
|
|
8
|
+
|
|
9
|
+
const KIND_BY_EXT = new Map([
|
|
10
|
+
...TEXT_EXTENSIONS.map((ext) => [ext, ext === '.txt' ? 'txt' : 'md']),
|
|
11
|
+
...IMAGE_EXTENSIONS.map((ext) => [ext, 'img']),
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
// Always ignored regardless of .gitignore. Dependency/build/cache directories
|
|
15
|
+
// would bury the docs this tool exists to surface.
|
|
16
|
+
export const DEFAULT_IGNORED_DIRS = new Set([
|
|
17
|
+
'node_modules',
|
|
18
|
+
'.git',
|
|
19
|
+
'dist',
|
|
20
|
+
'build',
|
|
21
|
+
'out',
|
|
22
|
+
'.next',
|
|
23
|
+
'.nuxt',
|
|
24
|
+
'.output',
|
|
25
|
+
'.cache',
|
|
26
|
+
'.turbo',
|
|
27
|
+
'.parcel-cache',
|
|
28
|
+
'target',
|
|
29
|
+
'vendor',
|
|
30
|
+
'coverage',
|
|
31
|
+
'.yarn',
|
|
32
|
+
'.pnpm-store',
|
|
33
|
+
'.venv',
|
|
34
|
+
'__pycache__',
|
|
35
|
+
'.idea',
|
|
36
|
+
'.vscode',
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
const MAX_DEPTH = 40;
|
|
40
|
+
|
|
41
|
+
export function fileKind(name) {
|
|
42
|
+
return KIND_BY_EXT.get(path.extname(name).toLowerCase()) ?? null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function escapeRe(ch) {
|
|
46
|
+
return ch.replace(/[.+^${}()|[\]\\]/g, '\\$&');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Translate a gitignore glob (subset: *, ?, **) into a regex source string.
|
|
50
|
+
function globToReSource(glob) {
|
|
51
|
+
let re = '';
|
|
52
|
+
let i = 0;
|
|
53
|
+
while (i < glob.length) {
|
|
54
|
+
const c = glob[i];
|
|
55
|
+
if (c === '*') {
|
|
56
|
+
if (glob[i + 1] === '*') {
|
|
57
|
+
re += '.*';
|
|
58
|
+
i += 2;
|
|
59
|
+
if (glob[i] === '/') i++;
|
|
60
|
+
} else {
|
|
61
|
+
re += '[^/]*';
|
|
62
|
+
i++;
|
|
63
|
+
}
|
|
64
|
+
} else if (c === '?') {
|
|
65
|
+
re += '[^/]';
|
|
66
|
+
i++;
|
|
67
|
+
} else {
|
|
68
|
+
re += escapeRe(c);
|
|
69
|
+
i++;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return re;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function parseGitignore(text) {
|
|
76
|
+
const rules = [];
|
|
77
|
+
for (let line of text.split(/\r?\n/)) {
|
|
78
|
+
line = line.replace(/\s+$/, '');
|
|
79
|
+
if (!line || line.startsWith('#')) continue;
|
|
80
|
+
let negate = false;
|
|
81
|
+
if (line.startsWith('!')) {
|
|
82
|
+
negate = true;
|
|
83
|
+
line = line.slice(1);
|
|
84
|
+
}
|
|
85
|
+
if (!line) continue;
|
|
86
|
+
const dirOnly = line.endsWith('/');
|
|
87
|
+
if (dirOnly) line = line.slice(0, -1);
|
|
88
|
+
const anchored = line.includes('/');
|
|
89
|
+
if (line.startsWith('/')) line = line.slice(1);
|
|
90
|
+
if (!line) continue;
|
|
91
|
+
const src = globToReSource(line);
|
|
92
|
+
rules.push({
|
|
93
|
+
negate,
|
|
94
|
+
dirOnly,
|
|
95
|
+
anchored,
|
|
96
|
+
reFull: new RegExp('^' + src + '$'),
|
|
97
|
+
reAny: new RegExp('(?:^|/)' + src + '$'),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return rules;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function ruleMatches(rule, rel, isDir) {
|
|
104
|
+
if (rule.dirOnly && !isDir) return false;
|
|
105
|
+
// dirOnly rules match the directory itself; unanchored rules match at any depth.
|
|
106
|
+
return rule.dirOnly || !rule.anchored ? rule.reFull.test(rel) || rule.reAny.test(rel) : rule.reFull.test(rel);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function isIgnored(rel, isDir, stack) {
|
|
110
|
+
let ignored = false;
|
|
111
|
+
for (const { baseRel, rules } of stack) {
|
|
112
|
+
const relFromBase = baseRel ? rel.slice(baseRel.length + 1) : rel;
|
|
113
|
+
if (relFromBase === '') continue;
|
|
114
|
+
for (const rule of rules) {
|
|
115
|
+
if (ruleMatches(rule, relFromBase, isDir)) ignored = !rule.negate;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return ignored;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function countFiles(nodes) {
|
|
122
|
+
let n = 0;
|
|
123
|
+
for (const node of nodes) n += node.type === 'file' ? 1 : node.count;
|
|
124
|
+
return n;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Scan a directory tree and return only the documentation files:
|
|
129
|
+
* { type, name, path, count?, size?, mtime?, children? }
|
|
130
|
+
* `path` is a repo-relative POSIX path ('' for the root).
|
|
131
|
+
*/
|
|
132
|
+
export function scanTree(rootDir, opts = {}) {
|
|
133
|
+
const maxTextSize = opts.maxTextSize ?? MAX_TEXT_SIZE;
|
|
134
|
+
const maxImageSize = opts.maxImageSize ?? MAX_IMAGE_SIZE;
|
|
135
|
+
const rootAbs = path.resolve(rootDir);
|
|
136
|
+
const rootName = path.basename(rootAbs) || rootAbs;
|
|
137
|
+
const stack = [];
|
|
138
|
+
|
|
139
|
+
function walk(dirAbs, dirRel, depth) {
|
|
140
|
+
let entries;
|
|
141
|
+
try {
|
|
142
|
+
entries = readdirSync(dirAbs, { withFileTypes: true });
|
|
143
|
+
} catch {
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const hasGitignore = entries.some((e) => e.name === '.gitignore' && e.isFile());
|
|
148
|
+
let pushedRule = false;
|
|
149
|
+
if (hasGitignore) {
|
|
150
|
+
try {
|
|
151
|
+
stack.push({ baseRel: dirRel, rules: parseGitignore(readFileSync(path.join(dirAbs, '.gitignore'), 'utf8')) });
|
|
152
|
+
pushedRule = true;
|
|
153
|
+
} catch {
|
|
154
|
+
/* unreadable .gitignore — treat as absent */
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const dirNodes = [];
|
|
159
|
+
const fileNodes = [];
|
|
160
|
+
for (const entry of entries) {
|
|
161
|
+
if (entry.isSymbolicLink()) continue; // loop safety
|
|
162
|
+
const rel = dirRel ? `${dirRel}/${entry.name}` : entry.name;
|
|
163
|
+
|
|
164
|
+
if (entry.isDirectory()) {
|
|
165
|
+
if (DEFAULT_IGNORED_DIRS.has(entry.name)) continue;
|
|
166
|
+
if (isIgnored(rel, true, stack)) continue;
|
|
167
|
+
if (depth >= MAX_DEPTH) continue;
|
|
168
|
+
const children = walk(path.join(dirAbs, entry.name), rel, depth + 1);
|
|
169
|
+
if (children.length > 0) {
|
|
170
|
+
dirNodes.push({ type: 'dir', name: entry.name, path: rel, count: countFiles(children), children });
|
|
171
|
+
}
|
|
172
|
+
} else if (entry.isFile()) {
|
|
173
|
+
const kind = fileKind(entry.name);
|
|
174
|
+
if (!kind) continue;
|
|
175
|
+
if (isIgnored(rel, false, stack)) continue;
|
|
176
|
+
let st;
|
|
177
|
+
try {
|
|
178
|
+
st = statSync(path.join(dirAbs, entry.name));
|
|
179
|
+
} catch {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (st.size > (kind === 'img' ? maxImageSize : maxTextSize)) continue;
|
|
183
|
+
fileNodes.push({ type: 'file', name: entry.name, path: rel, kind, size: st.size, mtime: st.mtimeMs });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
|
|
188
|
+
const byName = (a, b) => collator.compare(a.name, b.name);
|
|
189
|
+
dirNodes.sort(byName);
|
|
190
|
+
fileNodes.sort(byName);
|
|
191
|
+
|
|
192
|
+
if (pushedRule) stack.pop();
|
|
193
|
+
return [...dirNodes, ...fileNodes];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const children = walk(rootAbs, '', 0);
|
|
197
|
+
return { type: 'dir', name: rootName, path: '', count: countFiles(children), children };
|
|
198
|
+
}
|
package/src/server.js
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import { createReadStream, existsSync, readFileSync, statSync, watch } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { scanTree, fileKind, MAX_TEXT_SIZE, DEFAULT_IGNORED_DIRS } from './scanner.js';
|
|
5
|
+
|
|
6
|
+
const MIME = {
|
|
7
|
+
'.md': 'text/markdown; charset=utf-8',
|
|
8
|
+
'.markdown': 'text/markdown; charset=utf-8',
|
|
9
|
+
'.txt': 'text/plain; charset=utf-8',
|
|
10
|
+
'.html': 'text/html; charset=utf-8',
|
|
11
|
+
'.htm': 'text/html; charset=utf-8',
|
|
12
|
+
'.css': 'text/css; charset=utf-8',
|
|
13
|
+
'.js': 'text/javascript; charset=utf-8',
|
|
14
|
+
'.mjs': 'text/javascript; charset=utf-8',
|
|
15
|
+
'.json': 'application/json; charset=utf-8',
|
|
16
|
+
'.png': 'image/png',
|
|
17
|
+
'.jpg': 'image/jpeg',
|
|
18
|
+
'.jpeg': 'image/jpeg',
|
|
19
|
+
'.gif': 'image/gif',
|
|
20
|
+
'.webp': 'image/webp',
|
|
21
|
+
'.avif': 'image/avif',
|
|
22
|
+
'.svg': 'image/svg+xml',
|
|
23
|
+
'.ico': 'image/x-icon',
|
|
24
|
+
'.pdf': 'application/pdf',
|
|
25
|
+
'.woff': 'font/woff',
|
|
26
|
+
'.woff2': 'font/woff2',
|
|
27
|
+
'.map': 'application/json',
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function mimeType(file) {
|
|
31
|
+
return MIME[path.extname(file).toLowerCase()] || 'application/octet-stream';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function sendJson(res, status, body) {
|
|
35
|
+
const data = JSON.stringify(body);
|
|
36
|
+
res.writeHead(status, {
|
|
37
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
38
|
+
'Cache-Control': 'no-store',
|
|
39
|
+
});
|
|
40
|
+
res.end(data);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function hostAllowed(header) {
|
|
44
|
+
if (!header) return false;
|
|
45
|
+
try {
|
|
46
|
+
const hostname = new URL('http://' + header).hostname;
|
|
47
|
+
return hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1' || hostname === '[::1]';
|
|
48
|
+
} catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function listen(server, port, limit) {
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
let tries = 0;
|
|
56
|
+
const attempt = (p) => {
|
|
57
|
+
server.once('error', (err) => {
|
|
58
|
+
if (err.code === 'EADDRINUSE' && port !== 0 && tries < limit) {
|
|
59
|
+
tries++;
|
|
60
|
+
attempt(p + 1);
|
|
61
|
+
} else {
|
|
62
|
+
reject(err);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
server.listen(p, '127.0.0.1', () => resolve(server.address().port));
|
|
66
|
+
};
|
|
67
|
+
attempt(port);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const NOT_BUILT_PAGE = `<!doctype html>
|
|
72
|
+
<html><head><meta charset="utf-8"><title>readingroom</title></head>
|
|
73
|
+
<body style="font-family: ui-monospace, monospace; background:#0a0a0a; color:#e5e5e5; padding:3rem; line-height:1.6">
|
|
74
|
+
<h1 style="font-size:1.2rem">readingroom: frontend is not built</h1>
|
|
75
|
+
<p>The API is running, but <code>dist/</code> is missing from the package.</p>
|
|
76
|
+
<p>If you are hacking on readingroom itself, run <code>npm run build</code> (or use <code>npm run dev:web</code>) and reload.</p>
|
|
77
|
+
</body></html>`;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Start the readingroom server.
|
|
81
|
+
*
|
|
82
|
+
* Returns { server, port, url, root, tree(), close() }.
|
|
83
|
+
*/
|
|
84
|
+
export async function startServer({ root, port = 9345, distDir, autoIncrementLimit = 100 }) {
|
|
85
|
+
const rootAbs = path.resolve(root);
|
|
86
|
+
let cachedTree = scanTree(rootAbs);
|
|
87
|
+
let lastTreeJson = JSON.stringify(cachedTree);
|
|
88
|
+
const sseClients = new Set();
|
|
89
|
+
|
|
90
|
+
// Only serve files that resolve inside the scanned root.
|
|
91
|
+
function safeResolve(rel) {
|
|
92
|
+
if (typeof rel !== 'string' || rel.includes('\0')) return null;
|
|
93
|
+
const abs = path.resolve(rootAbs, rel.replace(/\\/g, '/'));
|
|
94
|
+
if (abs !== rootAbs && !abs.startsWith(rootAbs + path.sep)) return null;
|
|
95
|
+
return abs;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function broadcastTree() {
|
|
99
|
+
const payload = `event: tree\ndata: ${lastTreeJson}\n\n`;
|
|
100
|
+
for (const res of sseClients) res.write(payload);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let rescanTimer = null;
|
|
104
|
+
function scheduleRescan() {
|
|
105
|
+
clearTimeout(rescanTimer);
|
|
106
|
+
rescanTimer = setTimeout(() => {
|
|
107
|
+
try {
|
|
108
|
+
const next = scanTree(rootAbs);
|
|
109
|
+
const json = JSON.stringify(next);
|
|
110
|
+
if (json !== lastTreeJson) {
|
|
111
|
+
cachedTree = next;
|
|
112
|
+
lastTreeJson = json;
|
|
113
|
+
broadcastTree();
|
|
114
|
+
}
|
|
115
|
+
} catch {
|
|
116
|
+
/* transient scan failure — keep serving the previous tree */
|
|
117
|
+
}
|
|
118
|
+
}, 250);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let watcher = null;
|
|
122
|
+
try {
|
|
123
|
+
watcher = watch(rootAbs, { recursive: true }, (_event, filename) => {
|
|
124
|
+
const f = filename ? String(filename).replace(/\\/g, '/') : '';
|
|
125
|
+
// Skip churn inside ignored dirs (npm installs etc.) to avoid rescanning.
|
|
126
|
+
if (f && DEFAULT_IGNORED_DIRS.has(f.split('/')[0])) return;
|
|
127
|
+
scheduleRescan();
|
|
128
|
+
});
|
|
129
|
+
} catch (err) {
|
|
130
|
+
console.warn(`readingroom: live reload disabled (${err.message})`);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const heartbeat = setInterval(() => {
|
|
134
|
+
for (const res of sseClients) res.write(': ping\n\n');
|
|
135
|
+
}, 25000);
|
|
136
|
+
|
|
137
|
+
function serveStatic(req, res, pathname) {
|
|
138
|
+
if (!distDir || !existsSync(path.join(distDir, 'index.html'))) {
|
|
139
|
+
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store' });
|
|
140
|
+
res.end(NOT_BUILT_PAGE);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
let decoded;
|
|
144
|
+
try {
|
|
145
|
+
decoded = decodeURIComponent(pathname);
|
|
146
|
+
} catch {
|
|
147
|
+
sendJson(res, 400, { error: 'bad path' });
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const rel = decoded.replace(/^\/+/, '');
|
|
151
|
+
const abs = path.resolve(distDir, rel);
|
|
152
|
+
if (abs.startsWith(distDir + path.sep) && rel !== '' && existsSync(abs) && statSync(abs).isFile()) {
|
|
153
|
+
const immutable = rel.startsWith('assets/');
|
|
154
|
+
res.writeHead(200, {
|
|
155
|
+
'Content-Type': mimeType(abs),
|
|
156
|
+
'Content-Length': statSync(abs).size,
|
|
157
|
+
'Cache-Control': immutable ? 'public, max-age=31536000, immutable' : 'no-store',
|
|
158
|
+
});
|
|
159
|
+
createReadStream(abs).pipe(res);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
// SPA fallback: /view/<path> and any unknown route get the app shell.
|
|
163
|
+
const indexPath = path.join(distDir, 'index.html');
|
|
164
|
+
res.writeHead(200, {
|
|
165
|
+
'Content-Type': 'text/html; charset=utf-8',
|
|
166
|
+
'Cache-Control': 'no-store',
|
|
167
|
+
'Content-Length': statSync(indexPath).size,
|
|
168
|
+
});
|
|
169
|
+
createReadStream(indexPath).pipe(res);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const server = http.createServer(async (req, res) => {
|
|
173
|
+
try {
|
|
174
|
+
if (!hostAllowed(req.headers.host)) {
|
|
175
|
+
sendJson(res, 403, { error: 'forbidden host' });
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const url = new URL(req.url, 'http://127.0.0.1');
|
|
179
|
+
const pathname = url.pathname;
|
|
180
|
+
|
|
181
|
+
if (pathname === '/api/tree') {
|
|
182
|
+
sendJson(res, 200, cachedTree);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (pathname === '/api/file') {
|
|
187
|
+
const rel = url.searchParams.get('p');
|
|
188
|
+
const abs = safeResolve(rel);
|
|
189
|
+
if (!abs) {
|
|
190
|
+
sendJson(res, 403, { error: 'path outside root' });
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const kind = fileKind(path.basename(abs));
|
|
194
|
+
if (kind !== 'md' && kind !== 'txt') {
|
|
195
|
+
sendJson(res, 400, { error: 'only .md and .txt files can be opened as text' });
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
let st;
|
|
199
|
+
try {
|
|
200
|
+
st = statSync(abs);
|
|
201
|
+
} catch {
|
|
202
|
+
sendJson(res, 404, { error: 'not found' });
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (!st.isFile()) {
|
|
206
|
+
sendJson(res, 400, { error: 'not a file' });
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (st.size > MAX_TEXT_SIZE) {
|
|
210
|
+
sendJson(res, 413, { error: 'file is larger than 2MB and is not shown' });
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const content = readFileSync(abs, 'utf8');
|
|
214
|
+
sendJson(res, 200, {
|
|
215
|
+
path: rel.replace(/\\/g, '/'),
|
|
216
|
+
name: path.basename(abs),
|
|
217
|
+
ext: path.extname(abs).toLowerCase(),
|
|
218
|
+
size: st.size,
|
|
219
|
+
mtime: st.mtimeMs,
|
|
220
|
+
kind,
|
|
221
|
+
content,
|
|
222
|
+
});
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (pathname === '/api/raw') {
|
|
227
|
+
const abs = safeResolve(url.searchParams.get('p'));
|
|
228
|
+
if (!abs) {
|
|
229
|
+
sendJson(res, 403, { error: 'path outside root' });
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
let st;
|
|
233
|
+
try {
|
|
234
|
+
st = statSync(abs);
|
|
235
|
+
} catch {
|
|
236
|
+
sendJson(res, 404, { error: 'not found' });
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (!st.isFile()) {
|
|
240
|
+
sendJson(res, 404, { error: 'not found' });
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
res.writeHead(200, {
|
|
244
|
+
'Content-Type': mimeType(abs),
|
|
245
|
+
'Content-Length': st.size,
|
|
246
|
+
'Cache-Control': 'no-store',
|
|
247
|
+
'X-Content-Type-Options': 'nosniff',
|
|
248
|
+
// Repo files are data, not trusted same-origin documents.
|
|
249
|
+
'Content-Security-Policy': 'sandbox',
|
|
250
|
+
});
|
|
251
|
+
createReadStream(abs).pipe(res);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (pathname === '/api/events') {
|
|
256
|
+
res.writeHead(200, {
|
|
257
|
+
'Content-Type': 'text/event-stream',
|
|
258
|
+
'Cache-Control': 'no-store',
|
|
259
|
+
Connection: 'keep-alive',
|
|
260
|
+
});
|
|
261
|
+
res.write(': connected\n\n');
|
|
262
|
+
sseClients.add(res);
|
|
263
|
+
res.on('close', () => sseClients.delete(res));
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (pathname.startsWith('/api/')) {
|
|
268
|
+
sendJson(res, 404, { error: 'unknown endpoint' });
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
273
|
+
sendJson(res, 405, { error: 'method not allowed' });
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
serveStatic(req, res, pathname);
|
|
278
|
+
} catch (err) {
|
|
279
|
+
if (!res.headersSent) sendJson(res, 500, { error: 'internal error' });
|
|
280
|
+
else res.end();
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const actualPort = await listen(server, port, autoIncrementLimit);
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
server,
|
|
288
|
+
port: actualPort,
|
|
289
|
+
url: `http://127.0.0.1:${actualPort}`,
|
|
290
|
+
root: rootAbs,
|
|
291
|
+
tree: () => cachedTree,
|
|
292
|
+
liveReload: watcher !== null,
|
|
293
|
+
close() {
|
|
294
|
+
clearTimeout(rescanTimer);
|
|
295
|
+
clearInterval(heartbeat);
|
|
296
|
+
watcher?.close();
|
|
297
|
+
for (const res of sseClients) res.end();
|
|
298
|
+
server.close();
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
}
|