Skip to content

Commit 3c2c5dd

Browse files
committed
Move URL value coercion to the .url() function, support asynchronously resolved URLs.
1 parent ea5adf0 commit 3c2c5dd

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/fileAttachment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ async function remote_fetch(file) {
77
class FileAttachment {
88
constructor(url, name) {
99
Object.defineProperties(this, {
10-
_url: {value: url + ""},
10+
_url: {value: url},
1111
name: {value: name, enumerable: true}
1212
});
1313
}
1414
async url() {
15-
return this._url;
15+
return (await this._url) + "";
1616
}
1717
async blob() {
1818
return (await remote_fetch(this)).blob();

test/fileAttachments-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@ test("FileAttachment ensures that URLs are strings", async t => {
1616
t.equal(await file.url(), "https://example.com/filename.js");
1717
t.end();
1818
});
19+
20+
test("FileAttachment works with Promises that resolve to URLs", async t => {
21+
const fileAttachments = FileAttachments((name) =>
22+
new Promise((resolve) => resolve(new URL(`https://example.com/${name}.js`)))
23+
);
24+
const file = fileAttachments("otherfile");
25+
t.equal(file.constructor.name, "FileAttachment");
26+
t.equal(await file.url(), "https://example.com/otherfile.js");
27+
t.end();
28+
});

0 commit comments

Comments
 (0)