Skip to content

Commit 9ca3b31

Browse files
authored
fix FileAttachment resolution in imports (#992)
* fix FileAttachment resolution in imports * FileAttachment tests * prettier
1 parent d34b73c commit 9ca3b31

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/client/stdlib/fileAttachment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function FileAttachment(name, base = location.href) {
1212
const file = files.get(url);
1313
if (!file) throw new Error(`File not found: ${name}`);
1414
const {path, mimeType} = file;
15-
return new FileAttachmentImpl(new URL(path, base).href, name.split("/").pop(), mimeType);
15+
return new FileAttachmentImpl(new URL(path, location).href, name.split("/").pop(), mimeType);
1616
}
1717

1818
async function remote_fetch(file) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import assert from "node:assert";
2+
import {FileAttachment, registerFile} from "../../../src/client/stdlib/fileAttachment.js";
3+
4+
describe("FileAttachment(name)", () => {
5+
before(() => {
6+
globalThis.location = new URL("http://localhost:3000/test") as any;
7+
registerFile("./test.csv", {"name":"./test.csv","mimeType":"text/csv","path":"./_file/test.csv?sha=edd06c0d902e9ce083a9a5d8d0e655732c72e8129d3c60bfe69d228265e892d6"}); // prettier-ignore
8+
});
9+
after(() => {
10+
delete (globalThis as any).location;
11+
});
12+
it("returns the file at the specified path", async () => {
13+
const f = FileAttachment("./test.csv");
14+
assert.strictEqual(f.name, "test.csv");
15+
assert.strictEqual(f.mimeType, "text/csv");
16+
assert.strictEqual(await f.url(), "http://localhost:3000/_file/test.csv?sha=edd06c0d902e9ce083a9a5d8d0e655732c72e8129d3c60bfe69d228265e892d6"); // prettier-ignore
17+
});
18+
it("normalizes the specified file path", async () => {
19+
const f = FileAttachment("test.csv");
20+
assert.strictEqual(f.name, "test.csv");
21+
assert.strictEqual(f.mimeType, "text/csv");
22+
assert.strictEqual(await f.url(), "http://localhost:3000/_file/test.csv?sha=edd06c0d902e9ce083a9a5d8d0e655732c72e8129d3c60bfe69d228265e892d6"); // prettier-ignore
23+
});
24+
it("resolves the specified path relative to the current location", async () => {
25+
const f = FileAttachment("/test.csv");
26+
assert.strictEqual(f.name, "test.csv");
27+
assert.strictEqual(f.mimeType, "text/csv");
28+
assert.strictEqual(await f.url(), "http://localhost:3000/_file/test.csv?sha=edd06c0d902e9ce083a9a5d8d0e655732c72e8129d3c60bfe69d228265e892d6"); // prettier-ignore
29+
});
30+
it("resolves the specified path relative to the specified location (1)", async () => {
31+
const f = FileAttachment("/test.csv", "http://localhost:3000/sub/path");
32+
assert.strictEqual(f.name, "test.csv");
33+
assert.strictEqual(f.mimeType, "text/csv");
34+
assert.strictEqual(await f.url(), "http://localhost:3000/_file/test.csv?sha=edd06c0d902e9ce083a9a5d8d0e655732c72e8129d3c60bfe69d228265e892d6"); // prettier-ignore
35+
});
36+
it("resolves the specified path relative to the specified location (2)", async () => {
37+
const f = FileAttachment("../test.csv", "http://localhost:3000/sub/path");
38+
assert.strictEqual(f.name, "test.csv");
39+
assert.strictEqual(f.mimeType, "text/csv");
40+
assert.strictEqual(await f.url(), "http://localhost:3000/_file/test.csv?sha=edd06c0d902e9ce083a9a5d8d0e655732c72e8129d3c60bfe69d228265e892d6"); // prettier-ignore
41+
});
42+
it("throws an error if the file does not exist", async () => {
43+
assert.throws(() => FileAttachment("does-not-exist.csv"), /file not found/i);
44+
assert.throws(() => FileAttachment("test.csv?found=not"), /file not found/i);
45+
assert.throws(() => FileAttachment("test.csv", "http://localhost:3000/sub/path"), /file not found/i);
46+
});
47+
it("throws an error if used as a constructor", async () => {
48+
assert.throws(() => new FileAttachment("test.csv"), /not a constructor/i);
49+
});
50+
});

0 commit comments

Comments
 (0)