@lzwme/sserver 0.0.11 → 1.0.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/README.MD +71 -108
- package/dist/assets/assets/localsend/style.css +2 -0
- package/dist/assets/assets/localsend/vendor/qr-code-styling.js +2 -0
- package/dist/assets/localsend/style.css +2 -0
- package/dist/assets/localsend/vendor/qr-code-styling.js +2 -0
- package/dist/cli.js +9 -6
- package/dist/config.d.ts +38 -1
- package/dist/config.js +54 -9
- package/dist/lib/get-cert.js +1 -1
- package/dist/lib/merge-config.d.ts +9 -0
- package/dist/lib/merge-config.js +66 -0
- package/dist/lib/utils.d.ts +27 -1
- package/dist/lib/utils.js +94 -14
- package/dist/localsend/audit.d.ts +36 -0
- package/dist/localsend/audit.js +104 -0
- package/dist/localsend/auth.d.ts +94 -0
- package/dist/localsend/auth.js +227 -0
- package/dist/localsend/cache.d.ts +34 -0
- package/dist/localsend/cache.js +269 -0
- package/dist/localsend/chunk.d.ts +114 -0
- package/dist/localsend/chunk.js +475 -0
- package/dist/localsend/cleaner.d.ts +19 -0
- package/dist/localsend/cleaner.js +80 -0
- package/dist/localsend/client/app.js +4131 -0
- package/dist/localsend/client/crypto.js +233 -0
- package/dist/localsend/client/favicon.png +0 -0
- package/dist/localsend/client/icons/icon-192.png +0 -0
- package/dist/localsend/client/icons/icon-512.png +0 -0
- package/dist/localsend/client/index.html +451 -0
- package/dist/localsend/client/manifest.webmanifest +33 -0
- package/dist/localsend/client/sw.js +128 -0
- package/dist/localsend/guard.d.ts +58 -0
- package/dist/localsend/guard.js +210 -0
- package/dist/localsend/index.d.ts +52 -0
- package/dist/localsend/index.js +1771 -0
- package/dist/localsend/manage.d.ts +96 -0
- package/dist/localsend/manage.js +304 -0
- package/dist/localsend/metrics.d.ts +26 -0
- package/dist/localsend/metrics.js +58 -0
- package/dist/localsend/paste.d.ts +72 -0
- package/dist/localsend/paste.js +125 -0
- package/dist/localsend/path.d.ts +33 -0
- package/dist/localsend/path.js +151 -0
- package/dist/localsend/query.d.ts +78 -0
- package/dist/localsend/query.js +147 -0
- package/dist/localsend/secret.d.ts +31 -0
- package/dist/localsend/secret.js +96 -0
- package/dist/localsend/settings.d.ts +53 -0
- package/dist/localsend/settings.js +285 -0
- package/dist/localsend/share.d.ts +35 -0
- package/dist/localsend/share.js +117 -0
- package/dist/localsend/temp.d.ts +10 -0
- package/dist/localsend/temp.js +47 -0
- package/dist/localsend/ticket.d.ts +40 -0
- package/dist/localsend/ticket.js +98 -0
- package/dist/localsend/trash.d.ts +39 -0
- package/dist/localsend/trash.js +261 -0
- package/dist/localsend/webdav.d.ts +50 -0
- package/dist/localsend/webdav.js +530 -0
- package/dist/localsend/zip.d.ts +20 -0
- package/dist/localsend/zip.js +186 -0
- package/dist/proxy/php-proxy.js +31 -20
- package/dist/static-server.js +10 -3
- package/package.json +13 -5
- package/dist/localsend/index.html +0 -531
- package/dist/localsend.d.ts +0 -19
- package/dist/localsend.js +0 -217
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* 极简 ZIP 打包(store 模式,不压缩)
|
|
4
|
+
*
|
|
5
|
+
* 批量下载时把多个文件打成一个 zip,由服务端边读边写,不占用额外磁盘。
|
|
6
|
+
* 文件本身多为图片/视频/压缩包,压缩收益有限,因此使用 store 模式,
|
|
7
|
+
* 既省 CPU 也便于流式输出。
|
|
8
|
+
*
|
|
9
|
+
* 结构:local file header → 文件数据 → ... → central directory → end of central directory
|
|
10
|
+
* CRC 与长度在写文件前就算出,因此不使用 data descriptor(general purpose flag bit 3)。
|
|
11
|
+
*
|
|
12
|
+
* 文件名统一以 UTF-8 编码并置 EFS 标志位(bit 11),中文名在各平台解压后不会乱码。
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.crc32Update = crc32Update;
|
|
16
|
+
exports.createZipChunks = createZipChunks;
|
|
17
|
+
/** 生成 CRC32 查找表 */
|
|
18
|
+
function makeCrcTable() {
|
|
19
|
+
const table = new Uint32Array(256);
|
|
20
|
+
for (let i = 0; i < 256; i++) {
|
|
21
|
+
let c = i;
|
|
22
|
+
for (let k = 0; k < 8; k++)
|
|
23
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
24
|
+
table[i] = c >>> 0;
|
|
25
|
+
}
|
|
26
|
+
return table;
|
|
27
|
+
}
|
|
28
|
+
const CRC_TABLE = makeCrcTable();
|
|
29
|
+
/** 增量计算 CRC32(可与标准 zlib.crc32 结果比对) */
|
|
30
|
+
function crc32Update(crc, buf) {
|
|
31
|
+
let c = (crc ^ -1) >>> 0;
|
|
32
|
+
for (let i = 0; i < buf.length; i++)
|
|
33
|
+
c = (CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8)) >>> 0;
|
|
34
|
+
return (c ^ -1) >>> 0;
|
|
35
|
+
}
|
|
36
|
+
/** 把时间转换为 DOS 时间/日期 */
|
|
37
|
+
function dosDateTime(date) {
|
|
38
|
+
const year = Math.max(1980, date.getFullYear());
|
|
39
|
+
return {
|
|
40
|
+
time: (date.getHours() << 11) | (date.getMinutes() << 5) | (Math.floor(date.getSeconds() / 2) & 0x1f),
|
|
41
|
+
date: ((year - 1980) << 9) | ((date.getMonth() + 1) << 5) | date.getDate(),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const LOCAL_SIG = 0x04034b50;
|
|
45
|
+
const CENTRAL_SIG = 0x02014b50;
|
|
46
|
+
const END_SIG = 0x06054b50;
|
|
47
|
+
/**
|
|
48
|
+
* 通用标志位 bit 11(EFS):文件名与注释以 UTF-8 编码。
|
|
49
|
+
*
|
|
50
|
+
* 这是中文文件名不乱码的关键:不设置该位时,解压工具会按 CP437
|
|
51
|
+
* (或本地代码页)解释文件名里的字节,UTF-8 的中文就会显示成乱码。
|
|
52
|
+
* local file header 与 central directory 两处都要设置——
|
|
53
|
+
* 多数工具(含 Windows 资源管理器)读的是中央目录里的那一份。
|
|
54
|
+
*/
|
|
55
|
+
const FLAG_UTF8 = 0x0800;
|
|
56
|
+
/** Unicode Path Extra Field 的 header id(Info-ZIP 扩展) */
|
|
57
|
+
const EXTRA_UNICODE_PATH = 0x7075;
|
|
58
|
+
/**
|
|
59
|
+
* 生成 Unicode Path Extra Field。
|
|
60
|
+
*
|
|
61
|
+
* 较老的解压工具(部分 7-Zip / WinRAR / 国产解压软件)不认 EFS 标志,
|
|
62
|
+
* 但会优先读取这个扩展里的 UTF-8 文件名,可以作为兜底避免中文乱码。
|
|
63
|
+
*
|
|
64
|
+
* 结构:header id(2) + data size(2) + version(1) + nameCRC32(4) + UTF-8 文件名
|
|
65
|
+
* 其中 nameCRC32 是对本地头/中央目录中实际存储的文件名字节计算的 CRC32,
|
|
66
|
+
* 用于让工具确认两者一致。
|
|
67
|
+
*/
|
|
68
|
+
function buildUnicodePathExtra(nameBuf) {
|
|
69
|
+
const dataSize = 1 + 4 + nameBuf.length;
|
|
70
|
+
const extra = Buffer.alloc(4 + dataSize);
|
|
71
|
+
extra.writeUInt16LE(EXTRA_UNICODE_PATH, 0);
|
|
72
|
+
extra.writeUInt16LE(dataSize, 2);
|
|
73
|
+
extra.writeUInt8(1, 4); // version
|
|
74
|
+
extra.writeUInt32LE(crc32Update(0, nameBuf), 5);
|
|
75
|
+
nameBuf.copy(extra, 9);
|
|
76
|
+
return extra;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 生成条目名的扩展字段。
|
|
80
|
+
*
|
|
81
|
+
* 纯 ASCII 名不需要额外字段(任何工具都能正确解析),保持体积不变;
|
|
82
|
+
* 含非 ASCII 字符时补上 Unicode Path Extra Field。
|
|
83
|
+
*/
|
|
84
|
+
function buildNameExtra(nameBuf) {
|
|
85
|
+
// eslint-disable-next-line no-control-regex
|
|
86
|
+
const isAscii = /^[\x20-\x7e]*$/.test(nameBuf.toString('latin1'));
|
|
87
|
+
return isAscii ? Buffer.alloc(0) : buildUnicodePathExtra(nameBuf);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 生成 ZIP 内容的异步迭代器,可直接 pipe 到响应流。
|
|
91
|
+
*
|
|
92
|
+
* 为了兼容 Windows 资源管理器 / PowerShell Expand-Archive 等工具,
|
|
93
|
+
* 这里不使用 data descriptor(部分实现无法正确解析它),
|
|
94
|
+
* 而是先流式扫描一遍算出 CRC32 与大小,写入 local file header,
|
|
95
|
+
* 再扫描一遍输出文件内容。代价是本地文件读两遍,
|
|
96
|
+
* 相比网络传输耗时通常可以忽略。
|
|
97
|
+
*/
|
|
98
|
+
async function* createZipChunks(entries, readFile) {
|
|
99
|
+
const central = [];
|
|
100
|
+
let offset = 0;
|
|
101
|
+
// 第一遍:流式计算每个文件的 CRC32 与大小
|
|
102
|
+
const metas = [];
|
|
103
|
+
for (const entry of entries) {
|
|
104
|
+
let crc = 0;
|
|
105
|
+
let size = 0;
|
|
106
|
+
for await (const chunk of readFile(entry.path)) {
|
|
107
|
+
crc = crc32Update(crc, chunk);
|
|
108
|
+
size += chunk.length;
|
|
109
|
+
}
|
|
110
|
+
metas.push({ crc, size });
|
|
111
|
+
}
|
|
112
|
+
// 第二遍:输出 local header 与文件内容
|
|
113
|
+
for (const [index, entry] of entries.entries()) {
|
|
114
|
+
const meta = metas[index];
|
|
115
|
+
const nameBuf = Buffer.from(entry.name, 'utf8');
|
|
116
|
+
const extraBuf = buildNameExtra(nameBuf);
|
|
117
|
+
const { time, date } = dosDateTime(entry.mtime);
|
|
118
|
+
const local = Buffer.alloc(30);
|
|
119
|
+
local.writeUInt32LE(LOCAL_SIG, 0);
|
|
120
|
+
local.writeUInt16LE(20, 4); // version needed
|
|
121
|
+
local.writeUInt16LE(FLAG_UTF8, 6); // flags:文件名以 UTF-8 编码
|
|
122
|
+
local.writeUInt16LE(0, 8); // method: store
|
|
123
|
+
local.writeUInt16LE(time, 10);
|
|
124
|
+
local.writeUInt16LE(date, 12);
|
|
125
|
+
local.writeUInt32LE(meta.crc, 14);
|
|
126
|
+
local.writeUInt32LE(meta.size, 18); // compressed size
|
|
127
|
+
local.writeUInt32LE(meta.size, 22); // uncompressed size
|
|
128
|
+
local.writeUInt16LE(nameBuf.length, 26);
|
|
129
|
+
local.writeUInt16LE(extraBuf.length, 28); // extra length
|
|
130
|
+
yield local;
|
|
131
|
+
yield nameBuf;
|
|
132
|
+
if (extraBuf.length)
|
|
133
|
+
yield extraBuf;
|
|
134
|
+
for await (const chunk of readFile(entry.path)) {
|
|
135
|
+
yield chunk;
|
|
136
|
+
}
|
|
137
|
+
central.push({
|
|
138
|
+
nameBuf,
|
|
139
|
+
extraBuf,
|
|
140
|
+
crc: meta.crc,
|
|
141
|
+
size: meta.size,
|
|
142
|
+
offset,
|
|
143
|
+
time,
|
|
144
|
+
date,
|
|
145
|
+
});
|
|
146
|
+
offset += local.length + nameBuf.length + extraBuf.length + meta.size;
|
|
147
|
+
}
|
|
148
|
+
// central directory
|
|
149
|
+
const cdStart = offset;
|
|
150
|
+
for (const record of central) {
|
|
151
|
+
const header = Buffer.alloc(46);
|
|
152
|
+
header.writeUInt32LE(CENTRAL_SIG, 0);
|
|
153
|
+
header.writeUInt16LE(20, 4); // version made by
|
|
154
|
+
header.writeUInt16LE(20, 6); // version needed
|
|
155
|
+
header.writeUInt16LE(FLAG_UTF8, 8); // flags:文件名以 UTF-8 编码
|
|
156
|
+
header.writeUInt16LE(0, 10); // method
|
|
157
|
+
header.writeUInt16LE(record.time, 12);
|
|
158
|
+
header.writeUInt16LE(record.date, 14);
|
|
159
|
+
header.writeUInt32LE(record.crc, 16);
|
|
160
|
+
header.writeUInt32LE(record.size, 20); // compressed size
|
|
161
|
+
header.writeUInt32LE(record.size, 24); // uncompressed size
|
|
162
|
+
header.writeUInt16LE(record.nameBuf.length, 28);
|
|
163
|
+
header.writeUInt16LE(record.extraBuf.length, 30); // extra
|
|
164
|
+
header.writeUInt16LE(0, 32); // comment
|
|
165
|
+
header.writeUInt16LE(0, 34); // disk
|
|
166
|
+
header.writeUInt16LE(0, 36); // internal attrs
|
|
167
|
+
header.writeUInt32LE(0, 38); // external attrs
|
|
168
|
+
header.writeUInt32LE(record.offset, 42);
|
|
169
|
+
yield header;
|
|
170
|
+
yield record.nameBuf;
|
|
171
|
+
if (record.extraBuf.length)
|
|
172
|
+
yield record.extraBuf;
|
|
173
|
+
offset += header.length + record.nameBuf.length + record.extraBuf.length;
|
|
174
|
+
}
|
|
175
|
+
const cdSize = offset - cdStart;
|
|
176
|
+
const end = Buffer.alloc(22);
|
|
177
|
+
end.writeUInt32LE(END_SIG, 0);
|
|
178
|
+
end.writeUInt16LE(0, 4); // disk number
|
|
179
|
+
end.writeUInt16LE(0, 6); // disk with cd
|
|
180
|
+
end.writeUInt16LE(central.length, 8);
|
|
181
|
+
end.writeUInt16LE(central.length, 10);
|
|
182
|
+
end.writeUInt32LE(cdSize, 12);
|
|
183
|
+
end.writeUInt32LE(cdStart, 16);
|
|
184
|
+
end.writeUInt16LE(0, 20); // comment length
|
|
185
|
+
yield end;
|
|
186
|
+
}
|
package/dist/proxy/php-proxy.js
CHANGED
|
@@ -20,8 +20,12 @@ function getPhpCgi() {
|
|
|
20
20
|
return PHP_CGI;
|
|
21
21
|
}
|
|
22
22
|
async function findPhpFile(pathname, rootDir) {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
const base = (0, node_path_1.resolve)(rootDir);
|
|
24
|
+
// 去掉开头的斜杠,保持 join(rootDir, pathname) 的原有语义(pathname 以 / 开头时仍落在 rootDir 内)
|
|
25
|
+
let filepath = (0, node_path_1.resolve)(base, String(pathname).replace(/^[\\/]+/, ''));
|
|
26
|
+
// 严格的目录包含校验:%2e%2e 经 decodeURIComponent 后会变成 ..,不能只靠字符串 includes(rootDir) 判断
|
|
27
|
+
const rel = (0, node_path_1.relative)(base, filepath);
|
|
28
|
+
if (rel.startsWith('..') || (0, node_path_1.isAbsolute)(rel) || !(0, node_fs_1.existsSync)(filepath))
|
|
25
29
|
return false;
|
|
26
30
|
const stat = await node_fs_1.promises.stat(filepath);
|
|
27
31
|
if (stat.isDirectory()) {
|
|
@@ -161,27 +165,34 @@ function runPhpCgi(req, response, url, filepath, phpConfig, rootDir) {
|
|
|
161
165
|
});
|
|
162
166
|
childProc.on('exit', () => {
|
|
163
167
|
childProc.stdin.end();
|
|
164
|
-
|
|
168
|
+
// CGI 输出可能用 CRLF 或 LF 分隔,且未必带空行结束头,按行解析避免越界崩溃
|
|
169
|
+
const lines = res ? res.split(/\r?\n/) : [];
|
|
165
170
|
let line = 0;
|
|
166
171
|
let body = '';
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
172
|
+
for (; line < lines.length; line++) {
|
|
173
|
+
const current = lines[line];
|
|
174
|
+
if (!current)
|
|
175
|
+
break; // 空行表示响应头结束
|
|
176
|
+
const colon = current.indexOf(':');
|
|
177
|
+
if (colon < 0)
|
|
178
|
+
continue; // 忽略非法头行
|
|
179
|
+
const name = current.slice(0, colon).trim();
|
|
180
|
+
const value = current.slice(colon + 1).trim();
|
|
181
|
+
if (!name)
|
|
182
|
+
continue;
|
|
183
|
+
if (name.toLowerCase() === 'status') {
|
|
184
|
+
const code = parseInt(value, 10);
|
|
185
|
+
if (Number.isFinite(code))
|
|
186
|
+
response.statusCode = code;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
response.setHeader(name, value);
|
|
190
|
+
}
|
|
184
191
|
}
|
|
192
|
+
// 仅当确实遇到空行时才把后续内容当作响应体
|
|
193
|
+
body = line < lines.length ? lines.slice(line + 1).join('\n') : '';
|
|
194
|
+
if (!res)
|
|
195
|
+
body = err;
|
|
185
196
|
get_logger_1.logger.debug('[php]STATUS: ', response.statusCode);
|
|
186
197
|
response.status(response.statusCode).end(body);
|
|
187
198
|
});
|
package/dist/static-server.js
CHANGED
|
@@ -62,8 +62,10 @@ async function initServer(c) {
|
|
|
62
62
|
const url = `http${config.https ? 's' : ''}://${host}:${port}`;
|
|
63
63
|
const ipUrl = `http://${(0, utils_js_1.getIp)()}:${port}`;
|
|
64
64
|
app.use((req, res, next) => {
|
|
65
|
-
|
|
65
|
+
// 反射 Origin 时不能同时允许携带凭据,否则任意站点都能发起带凭据的跨域读取
|
|
66
|
+
if (config.cors && !config.headers['Access-Control-Allow-Credentials']) {
|
|
66
67
|
config.headers['Access-Control-Allow-Origin'] = req.header('origin') || '*';
|
|
68
|
+
}
|
|
67
69
|
Object.entries(config.headers).forEach(([key, value]) => res.header(key, value));
|
|
68
70
|
next();
|
|
69
71
|
});
|
|
@@ -109,8 +111,13 @@ async function initServer(c) {
|
|
|
109
111
|
}
|
|
110
112
|
get_logger_1.logger.debug(config);
|
|
111
113
|
if (config.open) {
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
+
const opener = process.platform === 'win32' ? 'start' : process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
115
|
+
try {
|
|
116
|
+
(0, node_child_process_1.execSync)(`${opener} ${url}`, { stdio: 'ignore' });
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
get_logger_1.logger.warn('自动打开首页失败,请手动访问:', url, err);
|
|
120
|
+
}
|
|
114
121
|
}
|
|
115
122
|
return app;
|
|
116
123
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lzwme/sserver",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "一个基于 Node.js 的简易静态服务器,用于辅助开发调试随启随用",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"private": false,
|
|
19
19
|
"engines": {
|
|
20
|
-
"node": "
|
|
20
|
+
"node": ">=20"
|
|
21
21
|
},
|
|
22
22
|
"keywords": [
|
|
23
23
|
"server",
|
|
@@ -40,8 +40,14 @@
|
|
|
40
40
|
"watch": "tsc -w",
|
|
41
41
|
"lint": "biome check",
|
|
42
42
|
"fix": "biome check --fix",
|
|
43
|
-
"dev": "
|
|
44
|
-
"
|
|
43
|
+
"dev": "npm run build:assets && bun run src/cli.ts",
|
|
44
|
+
"dev:watch": "npm run build:assets && bun --watch src/cli.ts",
|
|
45
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
46
|
+
"test": "npm run build && node --test --test-timeout=60000 \"test/*.test.js\"",
|
|
47
|
+
"build": "npm run clean && npm run build:assets && tsc && node scripts/copy-assets.js",
|
|
48
|
+
"build:assets": "npm run build:css && npm run build:vendor",
|
|
49
|
+
"build:css": "tailwindcss -i src/localsend/styles/input.css -o assets/localsend/style.css --minify",
|
|
50
|
+
"build:vendor": "node scripts/copy-vendor.js",
|
|
45
51
|
"clean": "flh rm -f dist",
|
|
46
52
|
"version": "standard-version",
|
|
47
53
|
"release": "npm run build"
|
|
@@ -59,13 +65,15 @@
|
|
|
59
65
|
"devDependencies": {
|
|
60
66
|
"@biomejs/biome": "^2.6.8",
|
|
61
67
|
"@lzwme/fed-lint-helper": "^2.6.6",
|
|
68
|
+
"@tailwindcss/cli": "^4.3.5",
|
|
62
69
|
"@types/express": "^5.0.6",
|
|
63
70
|
"@types/multer": "^2.3.0",
|
|
64
71
|
"@types/node": "^26.6.6",
|
|
65
72
|
"@types/node-forge": "^1.3.14",
|
|
66
73
|
"husky": "^9.1.7",
|
|
74
|
+
"qr-code-styling": "^1.9.2",
|
|
67
75
|
"standard-version": "^9.5.0",
|
|
68
|
-
"
|
|
76
|
+
"tailwindcss": "^4.3.5",
|
|
69
77
|
"typescript": "^7.0.2"
|
|
70
78
|
}
|
|
71
79
|
}
|