2
2
const files = new Map < string , FileAttachmentImpl > ( ) ;
3
3
4
4
export type DsvOptions = { delimiter ?: string ; array ?: boolean ; typed ?: boolean } ;
5
- export type DsvResult = any [ ] & { columns : string [ ] } ;
5
+ export type DsvResult = ( Record < string , any > [ ] | any [ ] [ ] ) & { columns : string [ ] } ;
6
6
7
7
export interface FileAttachment {
8
+ /** The URL of the file. */
9
+ href : string ;
10
+ /** The name of the file (not including the path), such as "test.csv". */
8
11
name : string ;
12
+ /** The MIME type, such as "text/csv". */
9
13
mimeType : string ;
10
- href : string ;
11
- lastModified : number | undefined ;
12
- size : number | undefined ;
14
+ /** The time this file was most-recently modified, as milliseconds since epoch, if known. */
15
+ lastModified ?: number ;
16
+ /** The size of this file in bytes, if known. */
17
+ size ?: number ;
13
18
/** @deprecated use FileAttachment.href instead */
14
19
url ( ) : Promise < string > ;
20
+ /** Returns the contents of this file as a Blob. */
15
21
blob ( ) : Promise < Blob > ;
22
+ /** Returns the contents of this file as an ArrayBuffer. */
16
23
arrayBuffer ( ) : Promise < ArrayBuffer > ;
24
+ /** Returns the contents of this file as a string with the given encoding. */
17
25
text ( encoding ?: string ) : Promise < string > ;
26
+ /** Returns the contents of this file as JSON. */
18
27
json ( ) : Promise < any > ;
28
+ /** Returns a byte stream to the contents of this file. */
19
29
stream ( ) : Promise < ReadableStream < Uint8Array < ArrayBufferLike > > > ;
30
+ /** Returns the contents of this file as delimiter-separated values. */
20
31
dsv ( options ?: DsvOptions ) : Promise < DsvResult > ;
32
+ /** Returns the contents of this file as comma-separated values. */
21
33
csv ( options ?: Omit < DsvOptions , "delimiter" > ) : Promise < DsvResult > ;
34
+ /** Returns the contents of this file as tab-separated values. */
22
35
tsv ( options ?: Omit < DsvOptions , "delimiter" > ) : Promise < DsvResult > ;
36
+ /** Returns the contents of this file as an image. */
23
37
image ( props ?: Partial < HTMLImageElement > ) : Promise < HTMLImageElement > ;
38
+ /** Returns the contents of this Arrow IPC file as an Apache Arrow table. */
24
39
arrow ( ) : Promise < any > ;
40
+ /** Returns the contents of this file as an Arquero table. */
25
41
arquero ( options ?: any ) : Promise < any > ;
42
+ /** Returns the contents of this Parquet file as an Apache Arrow table. */
26
43
parquet ( ) : Promise < any > ;
44
+ /** Returns the contents of this file as an XML document. */
27
45
xml ( mimeType ?: DOMParserSupportedType ) : Promise < Document > ;
46
+ /** Returns the contents of this file as an HTML document. */
28
47
html ( ) : Promise < Document > ;
29
48
}
30
49
31
- export function FileAttachment ( name : string , base = document . baseURI ) : FileAttachment {
32
- if ( new . target !== undefined ) throw new TypeError ( "FileAttachment is not a constructor" ) ;
50
+ export const FileAttachment = ( name : string , base = document . baseURI ) : FileAttachment => {
33
51
const href = new URL ( name , base ) . href ;
34
52
let file = files . get ( href ) ;
35
53
if ( ! file ) {
36
54
file = new FileAttachmentImpl ( href , name . split ( "/" ) . pop ( ) ! ) ;
37
55
files . set ( href , file ) ;
38
56
}
39
57
return file ;
40
- }
58
+ } ;
41
59
42
- async function remote_fetch ( file : FileAttachment ) {
60
+ async function fetchFile ( file : FileAttachment ) : Promise < Response > {
43
61
const response = await fetch ( file . href ) ;
44
62
if ( ! response . ok ) throw new Error ( `Unable to load file: ${ file . name } ` ) ;
45
63
return response ;
@@ -51,12 +69,7 @@ export abstract class AbstractFile implements FileAttachment {
51
69
lastModified ! : number | undefined ;
52
70
size ! : number | undefined ;
53
71
abstract href : string ;
54
- constructor (
55
- name : string ,
56
- mimeType = guessMimeType ( name ) ,
57
- lastModified ?: number ,
58
- size ?: number
59
- ) {
72
+ constructor ( name : string , mimeType = guessMimeType ( name ) , lastModified ?: number , size ?: number ) {
60
73
Object . defineProperties ( this , {
61
74
name : { value : `${ name } ` , enumerable : true } ,
62
75
mimeType : { value : `${ mimeType } ` , enumerable : true } ,
@@ -68,21 +81,21 @@ export abstract class AbstractFile implements FileAttachment {
68
81
return this . href ;
69
82
}
70
83
async blob ( ) : Promise < Blob > {
71
- return ( await remote_fetch ( this ) ) . blob ( ) ;
84
+ return ( await fetchFile ( this ) ) . blob ( ) ;
72
85
}
73
86
async arrayBuffer ( ) : Promise < ArrayBuffer > {
74
- return ( await remote_fetch ( this ) ) . arrayBuffer ( ) ;
87
+ return ( await fetchFile ( this ) ) . arrayBuffer ( ) ;
75
88
}
76
89
async text ( encoding ?: string ) : Promise < string > {
77
90
return encoding === undefined
78
- ? ( await remote_fetch ( this ) ) . text ( )
91
+ ? ( await fetchFile ( this ) ) . text ( )
79
92
: new TextDecoder ( encoding ) . decode ( await this . arrayBuffer ( ) ) ;
80
93
}
81
94
async json ( ) : Promise < any > {
82
- return ( await remote_fetch ( this ) ) . json ( ) ;
95
+ return ( await fetchFile ( this ) ) . json ( ) ;
83
96
}
84
97
async stream ( ) : Promise < ReadableStream < Uint8Array < ArrayBufferLike > > > {
85
- return ( await remote_fetch ( this ) ) . body ! ;
98
+ return ( await fetchFile ( this ) ) . body ! ;
86
99
}
87
100
async dsv ( { delimiter = "," , array = false , typed = false } = { } ) : Promise < DsvResult > {
88
101
const [ text , d3 ] = await Promise . all ( [ this . text ( ) , import ( "npm:d3-dsv" ) ] ) ;
@@ -108,7 +121,7 @@ export abstract class AbstractFile implements FileAttachment {
108
121
} ) ;
109
122
}
110
123
async arrow ( ) : Promise < any > {
111
- const [ Arrow , response ] = await Promise . all ( [ import ( "npm:apache-arrow" ) , remote_fetch ( this ) ] ) ;
124
+ const [ Arrow , response ] = await Promise . all ( [ import ( "npm:apache-arrow" ) , fetchFile ( this ) ] ) ;
112
125
return Arrow . tableFromIPC ( response ) ;
113
126
}
114
127
async arquero ( options ?: any ) : Promise < any > {
0 commit comments