Skip to content

Commit 90b7c97

Browse files
committed
Add default FileAttachment implementation.
It always throws not found! Also fix FileAttachments(resolve) to throw an error synchronously, rather than asynchronously, when the file does not exist.
1 parent 975ffb4 commit 90b7c97

File tree

5 files changed

+39
-27
lines changed

5 files changed

+39
-27
lines changed

src/fileAttachment.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ async function remote_fetch(file) {
55
}
66

77
class FileAttachment {
8-
constructor(resolve, name) {
8+
constructor(url, name) {
99
Object.defineProperties(this, {
10-
_resolve: {value: resolve},
10+
_url: {value: url},
1111
name: {value: name, enumerable: true}
1212
});
1313
}
1414
async url() {
15-
const url = await this._resolve(this.name);
16-
if (url == null) throw new Error(`Unknown file: ${this.name}`);
17-
return url;
15+
return this._url;
1816
}
1917
async blob() {
2018
return (await remote_fetch(this)).blob();
@@ -45,6 +43,14 @@ class FileAttachment {
4543
}
4644
}
4745

46+
export function NoFileAttachments(name) {
47+
throw new Error(`File not found: ${name}`);
48+
}
49+
4850
export default function FileAttachments(resolve) {
49-
return (name) => new FileAttachment(resolve, name);
51+
return name => {
52+
const url = resolve(name += ""); // Returns a Promise, or null.
53+
if (url == null) throw new Error(`File not found: ${name}`);
54+
return new FileAttachment(url, name);
55+
};
5056
}

src/library.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import constant from "./constant.js";
22
import DOM from "./dom/index.js";
33
import Files from "./files/index.js";
4+
import {NoFileAttachments} from "./fileAttachment.js";
45
import Generators from "./generators/index.js";
56
import html from "./html.js";
67
import md from "./md.js";
@@ -17,6 +18,7 @@ export default function Library(resolver) {
1718
const require = requirer(resolver);
1819
Object.defineProperties(this, {
1920
DOM: {value: DOM, writable: true, enumerable: true},
21+
FileAttachment: {value: constant(NoFileAttachments), writable: true, enumerable: true},
2022
Files: {value: Files, writable: true, enumerable: true},
2123
Generators: {value: Generators, writable: true, enumerable: true},
2224
html: {value: constant(html), writable: true, enumerable: true},

test/DOM/uid-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {test} from "tap";
2+
import UID from "../../src/dom/uid.js";
3+
4+
test("UID", t => {
5+
global.location = "https://test.com/";
6+
const hi = UID("hi");
7+
t.deepEqual(hi, {
8+
id: "O-hi-1",
9+
href: "https://test.com/#O-hi-1"
10+
});
11+
t.equal(hi.toString(), "url(https://test.com/#O-hi-1)");
12+
const anon = UID();
13+
t.equal(anon.toString(), "url(https://test.com/#O-2)");
14+
t.end();
15+
});

test/fileAttachments-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {test} from "tap";
2+
import {FileAttachments} from "../src/index.js";
3+
4+
test("FileAttachments is exported by stdlib", t => {
5+
t.equal(typeof FileAttachments, "function");
6+
t.equal(FileAttachments.name, "FileAttachments");
7+
t.end();
8+
});

test/index-test.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {test} from "tap";
2-
import {Library, FileAttachments} from "../src/index.js";
3-
import UID from "../src/dom/uid.js";
2+
import {Library} from "../src/index.js";
43

54
test("new Library returns a library with the expected keys", async t => {
65
t.deepEqual(Object.keys(new Library()).sort(), [
76
"DOM",
7+
"FileAttachment",
88
"Files",
99
"Generators",
1010
"Mutable",
@@ -20,22 +20,3 @@ test("new Library returns a library with the expected keys", async t => {
2020
]);
2121
t.end();
2222
});
23-
24-
test("FileAttachments is exported by stdlib/index", t => {
25-
t.equal(typeof FileAttachments, "function");
26-
t.equal(FileAttachments.name, "FileAttachments");
27-
t.end();
28-
});
29-
30-
test("UID", t => {
31-
global.location = "https://test.com/";
32-
const hi = UID("hi");
33-
t.deepEqual(hi, {
34-
id: "O-hi-1",
35-
href: "https://test.com/#O-hi-1"
36-
});
37-
t.equal(hi.toString(), "url(https://test.com/#O-hi-1)");
38-
const anon = UID();
39-
t.equal(anon.toString(), "url(https://test.com/#O-2)");
40-
t.end();
41-
});

0 commit comments

Comments
 (0)