mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-02 04:20:26 +08:00
* feat: preview remote HTML snapshots with peer file transport Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(files): 网页预览准备使用中性加载提示 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(files): 使用 SHA-256 生成远程缓存标识 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(remote-files): guard preview requests and owner-scoped cache completion Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(remote-files): guard preview media reference ingestion Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(files): 统一两端音视频流式读取策略 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(mobile): 区分文件预览与下载的读取意图 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(files): 避免手机预览持有短期 peer 文件地址 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(security): 为正式包文件传输页面注入 CSP Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(mobile): 统一处理文件 WebView 进程终止 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(mobile): 为 HTML 快照拒绝设备权限 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(files): enforce snapshot budgets before OSS uploads Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(files): 按需预览远程 HTML 并支持 2 GiB 文件传输 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * test(files): 复用真实的跨平台 HTML 路径转换 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(mobile): HTML 入口加载失败进入重试态 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(mobile): 取消 HTML 预览连接超时任务 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * chore: regenerate design inventory after main merge Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(mobile): keep on-demand document errors retryable Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(files): cancel shared remote reads when unused Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(mobile): keep async preview reads alive Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(files): serialize desktop HTML preview resource copies Reuse the existing file-read queue so one preview cannot overlap 2 GiB materializations. Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * test(desktop): 对齐主干 bootstrap 接线格式 GitHub CI 合入最新 main 后 bootstrap 换行变化,源码契约测试仍按旧单行断言。 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(files): allow SSH HTML preview and reuse on-demand paths SSH on-demand HTML was rejected as 415. Repeated cache-busted requests now reuse the preview-owned file instead of fetching again. Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(files): keep on-demand preview files until close Native on-demand responses no longer delete the materialized file when the first socket closes, and desktop reuses the preview-owned copy for the same path. Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> --------- Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>
112 lines
6.5 KiB
JavaScript
112 lines
6.5 KiB
JavaScript
// macOS native integration: compiles the production Network listener, drives real TCP/HTTP,
|
|
// and deletes every fixture/process. Does not require an installed mobile app or account.
|
|
import assert from 'node:assert/strict';
|
|
import { spawn, execFileSync } from 'node:child_process';
|
|
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import net from 'node:net';
|
|
|
|
if (process.platform !== 'darwin') throw new Error('This test requires Apple Network.framework on macOS');
|
|
const dir = await mkdtemp(join(tmpdir(), 'cindy-html-native-test-'));
|
|
let child;
|
|
const demand = process.argv.includes('--on-demand');
|
|
try {
|
|
await writeFile(join(dir, '0'), '<!doctype html><meta charset="utf-8"><h1>中文</h1>');
|
|
await writeFile(join(dir, '1'), 'body { color: green; }');
|
|
await writeFile(join(dir, '2'), '<h1>Second page</h1>');
|
|
await writeFile(join(dir, 'main.swift'), `import Foundation
|
|
import Network
|
|
let queue = DispatchQueue(label: "test")
|
|
let files = [["index.html", "0", "text/html"], ["dist/main.css", "1", "text/css"], ["second.html", "2", "text/html"]]
|
|
var server: HtmlSnapshotServer!
|
|
var sequence = 100
|
|
server = try HtmlSnapshotServer(root: CommandLine.arguments[1], entry: "index.html", token: String(repeating: "a", count: 48), csp: "default-src 'none'; style-src 'self'", files: ${demand ? '[]' : 'files'}, queue: queue${demand ? `, onRequest: { id, path in
|
|
guard let row = files.first(where: { $0[0] == path }) else {
|
|
_ = server.resolve(id, filename: "", mime: "", status: 404)
|
|
return
|
|
}
|
|
let filename = String(sequence)
|
|
sequence += 1
|
|
let root = URL(fileURLWithPath: CommandLine.arguments[1])
|
|
try! FileManager.default.copyItem(at: root.appendingPathComponent(row[1]), to: root.appendingPathComponent(filename))
|
|
queue.asyncAfter(deadline: .now() + 0.01) {
|
|
assert(server.resolve(id, filename: filename, mime: row[2], status: 200))
|
|
assert(!server.resolve(id, filename: filename, mime: row[2], status: 200))
|
|
}
|
|
}` : ''})
|
|
server.start { result in
|
|
switch result {
|
|
case .success(let url): print(url); fflush(stdout)
|
|
case .failure(let error): print(error); exit(1)
|
|
}
|
|
}
|
|
signal(SIGTERM, SIG_IGN)
|
|
let termination = DispatchSource.makeSignalSource(signal: SIGTERM, queue: queue)
|
|
termination.setEventHandler { server.stop(); exit(0) }
|
|
termination.resume()
|
|
dispatchMain()
|
|
`);
|
|
execFileSync('xcrun', ['swiftc', fileURLToPath(new URL('../modules/cindy-html-preview/ios/HtmlSnapshotServer.swift', import.meta.url)), join(dir, 'main.swift'), '-o', join(dir, 'server')], { timeout: 60_000 });
|
|
child = spawn(join(dir, 'server'), [dir], { stdio: ['ignore', 'pipe', 'inherit'] });
|
|
const url = await new Promise((resolve, reject) => {
|
|
const timer = setTimeout(() => reject(new Error('Native listener did not start')), 10_000);
|
|
child.once('exit', (code) => { clearTimeout(timer); reject(new Error(`Listener exited ${code}`)); });
|
|
child.stdout.once('data', (data) => { clearTimeout(timer); resolve(data.toString().trim()); });
|
|
});
|
|
const initial = new URL(url);
|
|
const origin = initial.origin;
|
|
const assertPermissions = (response) => assert.equal(
|
|
response.headers.get('permissions-policy'),
|
|
'camera=(), microphone=(), geolocation=()',
|
|
);
|
|
const bootstrap = await fetch(url, { redirect: 'manual' });
|
|
assertPermissions(bootstrap);
|
|
assert.equal(bootstrap.status, 302);
|
|
assert.equal(bootstrap.headers.get('location'), '/index.html');
|
|
const cookie = bootstrap.headers.get('set-cookie').split(';')[0];
|
|
assert.match(bootstrap.headers.get('set-cookie'), /HttpOnly; SameSite=Strict/);
|
|
assert.equal((await fetch(origin + '/index.html')).status, 403);
|
|
const page = await fetch(origin + '/index.html', { headers: { cookie } });
|
|
assertPermissions(page);
|
|
assert.match(await page.text(), /中文/);
|
|
assert.match(page.headers.get('content-type'), /charset=utf-8/);
|
|
assert.equal(page.headers.get('cache-control'), 'no-store');
|
|
const css = await fetch(origin + '/dist/main.css', { headers: { cookie } });
|
|
assert.equal(css.headers.get('content-type'), 'text/css');
|
|
assert.match(await css.text(), /green/);
|
|
assert.equal((await fetch(origin + '/second.html', { headers: { cookie } })).status, 200);
|
|
assert.equal((await fetch(origin + '/', { headers: { cookie } })).status, 200);
|
|
const head = await fetch(origin + '/index.html', { method: 'HEAD', headers: { cookie } });
|
|
assertPermissions(head);
|
|
assertPermissions(await fetch(origin + '/index.html'));
|
|
assertPermissions(await fetch(origin + '/missing.html', { headers: { cookie } }));
|
|
assert.equal(head.status, 200);
|
|
assert.equal(await head.text(), '');
|
|
assert.ok(Number(head.headers.get('content-length')) > 0);
|
|
assert.equal((await fetch(origin + '/0', { headers: { cookie } })).status, 404);
|
|
assert.equal((await fetch(origin + '/index.html', { headers: { cookie, Origin: 'https://example.org' } })).status, 403);
|
|
assert.equal((await fetch(origin + '/index.html', { method: 'POST', headers: { cookie } })).status, 400);
|
|
const raw = (parts) => new Promise((resolve, reject) => {
|
|
const client = net.connect(Number(initial.port), '127.0.0.1');
|
|
let response = '';
|
|
client.setTimeout(3000, () => client.destroy(new Error('TCP timeout')));
|
|
client.on('error', reject);
|
|
client.on('data', (chunk) => { response += chunk.toString(); });
|
|
client.on('end', () => resolve(response));
|
|
client.once('connect', () => { client.write(parts[0]); setTimeout(() => client.write(parts.slice(1).join('')), 20); });
|
|
});
|
|
assert.match(await raw(['GET /index.html HTTP/1.1\r\nHo', `st: ${initial.host}\r\nCookie: ${cookie}\r\n\r\n`]), /^HTTP\/1.1 200/);
|
|
assert.match(await raw([`GET /index.html HTTP/1.1\r\nHost: ${initial.host}\r\n`, `Host: evil\r\nCookie: ${cookie}\r\n\r\n`]), /^HTTP\/1.1 400/);
|
|
assert.match(await raw([`GET /%2e%2e/index.html HTTP/1.1\r\nHost: ${initial.host}\r\n`, `Cookie: ${cookie}\r\n\r\n`]), /^HTTP\/1.1 403/);
|
|
assert.equal((await fetch(origin + '/missing.js', { headers: { cookie } })).status, 404);
|
|
assert.equal((await fetch(origin + '/index.html', { headers: { cookie } })).status, 200);
|
|
await new Promise((resolve) => { child.once('exit', resolve); child.kill('SIGTERM'); });
|
|
await assert.rejects(fetch(origin + '/index.html', { headers: { cookie } }));
|
|
console.log((demand ? 'ON-DEMAND ' : 'LEGACY ') + 'PASS: native loopback listener, bootstrap/cookie, HTML/CSS/navigation, HEAD, manifest isolation, foreign origin, malformed/split headers, traversal and shutdown');
|
|
} finally {
|
|
if (child && child.exitCode === null) child.kill('SIGKILL');
|
|
await rm(dir, { recursive: true, force: true });
|
|
}
|