mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 20:34:53 +00:00
Opening a file in Workspace showed a bare text editor, and every image was reported as "Binary file; online preview/editing is not supported". The sheet now opens on a Preview tab, with the editor one click away: - Markdown is rendered with the existing Markdown component (no raw HTML). - JSON and HTML/XML get line numbers and the JSON/markup highlighting already used by HttpCodeBlock; other text gets line numbers. - PNG, JPEG, GIF, WebP, BMP, ICO and AVIF are shown inline. They come from the existing /api/workspace/download route through apiFetch, so the route's auth and path checks apply unchanged. The Blob takes its type from the extension allow-list, and the read stops at 10 MiB (by Content-Length, or by counting bytes when there is none). - SVG is shown as source, never rendered, since it can carry script. - Text over 200,000 characters or 5,000 lines is shown as one plain block, so a big log cannot stall the page. Binary files and text over the server's 2 MiB read limit keep the download prompt. The sheet's sm:max-w-2xl lost to the Sheet's own data-[side=right]:sm:max-w-sm, holding it at 384 px; it now uses the data-[side=right] prefix like the other wide sheets. No backend change.
150 lines
6.0 KiB
JavaScript
150 lines
6.0 KiB
JavaScript
import ts from "typescript";
|
|
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { test } from "node:test";
|
|
import { fileURLToPath } from "node:url";
|
|
import vm from "node:vm";
|
|
|
|
const web = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
const transpile = (file) =>
|
|
ts.transpileModule(fs.readFileSync(path.join(web, file), "utf8"), {
|
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
|
|
}).outputText;
|
|
const output = transpile("src/lib/api.ts");
|
|
// api.ts sends its requests through these two modules; they run for real.
|
|
const helpers = { "@/lib/api-fetch": transpile("src/lib/api-fetch.ts"), "@/lib/auth": transpile("src/lib/auth.ts") };
|
|
const MiB = 1024 * 1024;
|
|
|
|
function setup(mock = false, respond = () => ({ ok: true, json: async () => ({ attachments: [] }) })) {
|
|
const requests = [];
|
|
const forms = [];
|
|
const exports = {};
|
|
const loaded = {};
|
|
const sandbox = (moduleExports) => ({
|
|
exports: moduleExports,
|
|
require(name) {
|
|
if (name === "@/lib/mock/enabled") return { MOCK: mock };
|
|
if (name === "@/lib/mock/handler") return { mockHandle: () => assert.fail("unexpected mock request") };
|
|
if (name === "@/lib/sse") return { SSEStream: class {} };
|
|
// English passthrough: assertions below match the source (English) messages.
|
|
if (name === "@/i18n")
|
|
return { t: (key, vars) => key.replace(/\{(\w+)\}/g, (m, k) => (vars && k in vars ? String(vars[k]) : m)) };
|
|
if (name in helpers) {
|
|
if (!loaded[name]) {
|
|
loaded[name] = {};
|
|
vm.runInNewContext(helpers[name], sandbox(loaded[name]));
|
|
}
|
|
return loaded[name];
|
|
}
|
|
throw new Error("Unexpected import: " + name);
|
|
},
|
|
FormData: class {
|
|
fields = [];
|
|
constructor() {
|
|
forms.push(this);
|
|
}
|
|
append(name, value) {
|
|
this.fields.push([name, value]);
|
|
}
|
|
},
|
|
fetch: async (url, init) => {
|
|
requests.push({ url, init });
|
|
return respond(url, init);
|
|
},
|
|
Blob,
|
|
});
|
|
vm.runInNewContext(output, sandbox(exports));
|
|
return { api: exports.api, requests, forms };
|
|
}
|
|
|
|
for (const scope of ["task", "session", "staging"]) {
|
|
test(scope + ": rejects oversized single and combined files before FormData or fetch", async () => {
|
|
const s = setup();
|
|
for (const sizes of [[512 * MiB + 1], [256 * MiB, 256 * MiB + 1]]) {
|
|
const files = sizes.map((size, i) => ({ name: i + ".apk", size }));
|
|
await assert.rejects(s.api.chatUpload(scope, "42", files), /512 MiB.*combined size/);
|
|
}
|
|
assert.equal(s.forms.length, 0);
|
|
assert.equal(s.requests.length, 0);
|
|
});
|
|
|
|
test(scope + ": accepts ordinary files and exactly 512 MiB, including combined files", async () => {
|
|
for (const sizes of [[129 * MiB], [512 * MiB], [256 * MiB, 256 * MiB]]) {
|
|
const s = setup();
|
|
const files = sizes.map((size, i) => ({ name: i + ".apk", size }));
|
|
const result = await s.api.chatUpload(scope, "42", files);
|
|
assert.equal(result.attachments.length, 0);
|
|
assert.equal(s.requests.length, 1);
|
|
assert.equal(s.requests[0].url, "/api/chat/upload?scope=" + scope + "&id=42");
|
|
assert.equal(s.requests[0].init.method, "POST");
|
|
assert.equal(s.requests[0].init.body, s.forms[0]);
|
|
assert.deepEqual(
|
|
s.forms[0].fields,
|
|
files.map((file) => ["file", file]),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
test("mock uploads enforce the same combined limit", async () => {
|
|
const s = setup(true);
|
|
await assert.rejects(s.api.chatUpload("staging", "42", [{ name: "app.apk", size: 512 * MiB + 1 }]), /512 MiB/);
|
|
const result = await s.api.chatUpload("staging", "42", [{ name: "app.apk", size: 512 * MiB }]);
|
|
assert.equal(result.attachments[0].size, 512 * MiB);
|
|
assert.equal(s.requests.length, 0);
|
|
});
|
|
|
|
// A download response whose body is served chunk by chunk and records how much was read.
|
|
function download(chunks, headers = {}) {
|
|
const body = { pulled: 0, cancelled: false };
|
|
const queue = chunks.map((n) => new Uint8Array(n).fill(7));
|
|
body.stream = new ReadableStream(
|
|
{
|
|
pull(controller) {
|
|
const next = queue.shift();
|
|
if (!next) return controller.close();
|
|
body.pulled += next.byteLength;
|
|
controller.enqueue(next);
|
|
},
|
|
cancel() {
|
|
body.cancelled = true;
|
|
},
|
|
},
|
|
{ highWaterMark: 0 },
|
|
); // pull only when the reader asks, so `pulled` counts what api.ts read
|
|
const response = { ok: true, status: 200, headers: new Headers(headers), body: body.stream };
|
|
return { body, response };
|
|
}
|
|
|
|
test("workspaceImage loads the file through the download route and types the Blob itself", async () => {
|
|
const { response } = download([2, 1], { "Content-Length": "3", "Content-Type": "text/html" });
|
|
const s = setup(false, () => response);
|
|
const blob = await s.api.workspaceImage("tasks/3/shot.png", "image/png", 3);
|
|
assert.equal(s.requests[0].url, "/api/workspace/download?path=tasks%2F3%2Fshot.png");
|
|
assert.equal(blob.type, "image/png");
|
|
assert.equal(blob.size, 3);
|
|
});
|
|
|
|
test("workspaceImage refuses a declared length over the limit without reading the body", async () => {
|
|
const { body, response } = download([4], { "Content-Length": String(11 * MiB) });
|
|
const s = setup(false, () => response);
|
|
assert.equal(await s.api.workspaceImage("big.png", "image/png", 10 * MiB), null);
|
|
assert.equal(body.pulled, 0);
|
|
assert.equal(body.cancelled, true);
|
|
});
|
|
|
|
test("workspaceImage stops reading a body without a length once it passes the limit", async () => {
|
|
const { body, response } = download([2, 2, 2, 2]);
|
|
const s = setup(false, () => response);
|
|
assert.equal(await s.api.workspaceImage("grown.png", "image/png", 3), null);
|
|
assert.equal(body.cancelled, true);
|
|
assert.equal(body.pulled, 4);
|
|
});
|
|
|
|
test("workspaceImage passes on the server's error", async () => {
|
|
const s = setup(false, () => ({ ok: false, status: 400, json: async () => ({ error: "invalid path" }) }));
|
|
await assert.rejects(s.api.workspaceImage("../x.png", "image/png", 10 * MiB), /invalid path/);
|
|
});
|