Skip to content

Commit

Permalink
file.href (#1113)
Browse files Browse the repository at this point in the history
* file.href

* list name first
  • Loading branch information
mbostock committed Mar 21, 2024
1 parent 263fe66 commit 311cd58
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions docs/javascript/files.md
Expand Up @@ -99,10 +99,10 @@ For missing files, `file.lastModified` is undefined. The `file.mimeType` is dete

The contents often dictate the appropriate method — for example, an Apache Arrow file is almost always read with `file.arrow`. When multiple methods are valid, choose based on your needs. For example, you can load a CSV file using `file.text` to implement parsing yourself instead of using D3.

In addition to the above, you can get the resolved absolute URL of the file using `file.url`. This returns a [promise](./promises) to a string:
In addition to the above, you can get the resolved absolute URL of the file using `file.href`:

```js echo
FileAttachment("volcano.json").url()
FileAttachment("volcano.json").href
```

See [file-based routing](../routing#files) for additional details.
Expand Down
28 changes: 14 additions & 14 deletions src/client/stdlib/fileAttachment.js
@@ -1,22 +1,22 @@
const files = new Map();

export function registerFile(name, file) {
const url = new URL(name, location).href;
if (file == null) files.delete(url);
else files.set(url, file);
const href = new URL(name, location).href;
if (file == null) files.delete(href);
else files.set(href, file);
}

export function FileAttachment(name, base = location.href) {
export function FileAttachment(name, base = location) {
if (new.target !== undefined) throw new TypeError("FileAttachment is not a constructor");
const url = new URL(name, base).href;
const file = files.get(url);
const href = new URL(name, base).href;
const file = files.get(href);
if (!file) throw new Error(`File not found: ${name}`);
const {path, mimeType, lastModified} = file;
return new FileAttachmentImpl(new URL(path, location).href, name.split("/").pop(), mimeType, lastModified);
}

async function remote_fetch(file) {
const response = await fetch(await file.url());
const response = await fetch(file.href);
if (!response.ok) throw new Error(`Unable to load file: ${file.name}`);
return response;
}
Expand All @@ -29,8 +29,8 @@ async function dsv(file, delimiter, {array = false, typed = false} = {}) {

export class AbstractFile {
constructor(name, mimeType = "application/octet-stream", lastModified) {
Object.defineProperty(this, "mimeType", {value: `${mimeType}`, enumerable: true});
Object.defineProperty(this, "name", {value: `${name}`, enumerable: true});
Object.defineProperty(this, "mimeType", {value: `${mimeType}`, enumerable: true});
if (lastModified !== undefined) Object.defineProperty(this, "lastModified", {value: Number(lastModified), enumerable: true}); // prettier-ignore
}
async blob() {
Expand All @@ -57,14 +57,13 @@ export class AbstractFile {
return dsv(this, "\t", options);
}
async image(props) {
const url = await this.url();
return new Promise((resolve, reject) => {
const i = new Image();
if (new URL(url, document.baseURI).origin !== new URL(location).origin) i.crossOrigin = "anonymous";
if (new URL(this.href, document.baseURI).origin !== new URL(location).origin) i.crossOrigin = "anonymous";
Object.assign(i, props);
i.onload = () => resolve(i);
i.onerror = () => reject(new Error(`Unable to load file: ${this.name}`));
i.src = url;
i.src = this.href;
});
}
async arrow() {
Expand Down Expand Up @@ -96,12 +95,13 @@ export class AbstractFile {
}

class FileAttachmentImpl extends AbstractFile {
constructor(url, name, mimeType, lastModified) {
constructor(href, name, mimeType, lastModified) {
super(name, mimeType, lastModified);
Object.defineProperty(this, "_url", {value: url});
Object.defineProperty(this, "href", {value: href});
}
/** @deprecated Use this.href instead. */
async url() {
return `${await this._url}`;
return this.href;
}
}

Expand Down

0 comments on commit 311cd58

Please sign in to comment.