Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file.href #1113

Merged
merged 2 commits into from Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
26 changes: 13 additions & 13 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 Down Expand Up @@ -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