UNPKG

5.98 kBTypeScriptView Raw
1/**
2 * This interface provides methods to monitor the asynchronous writing of blobs to disk using progress events [PROGRESS-EVENTS] and event handler attributes.
3 * This interface is specified to be used within the context of the global object (Window [HTML5]) and within Web Workers (WorkerUtils [WEBWORKERS-ED]).
4 */
5interface FileSaver extends EventTarget {
6 /**
7 * When the abort method is called, user agents must run the steps below:
8 * <ol>
9 * <li> If readyState == DONE or readyState == INIT, terminate this overall series of steps without doing anything else. </li>
10 * <li> Set readyState to DONE. </li>
11 * <li> If there are any tasks from the object's FileSaver task source in one of the task queues, then remove those tasks. </li>
12 * <li> Terminate the write algorithm being processed. </li>
13 * <li> Set the error attribute to a DOMError object of type "AbortError". </li>
14 * <li> Fire a progress event called abort </li>
15 * <li> Fire a progress event called writeend </li>
16 * <li> Terminate this algorithm. </li>
17 * </ol>
18 */
19 abort(): void;
20
21 /**
22 * The blob is being written.
23 */
24 INIT: number;
25
26 /**
27 * The object has been constructed, but there is no pending write.
28 */
29 WRITING: number;
30
31 /**
32 * The entire Blob has been written to the file, an error occurred during the write, or the write was aborted using abort(). The FileSaver is no longer writing the blob.
33 */
34 DONE: number;
35
36 /**
37 * The FileSaver object can be in one of 3 states. The readyState attribute, on getting, must return the current state, which must be one of the following values:
38 * <ul>
39 * <li>INIT</li>
40 * <li>WRITING</li>
41 * <li>DONE</li>
42 * <ul>
43 */
44 readyState: number;
45
46 /**
47 * The last error that occurred on the FileSaver.
48 */
49 error: Error;
50
51 /**
52 * Handler for writestart events
53 */
54 onwritestart: (event: ProgressEvent) => void;
55
56 /**
57 * Handler for progress events.
58 */
59 onprogress: (event: ProgressEvent) => void;
60
61 /**
62 * Handler for write events.
63 */
64 onwrite: (event: ProgressEvent) => void;
65
66 /**
67 * Handler for abort events.
68 */
69 onabort: (event: ProgressEvent) => void;
70
71 /**
72 * Handler for error events.
73 */
74 onerror: (event: ProgressEvent) => void;
75
76 /**
77 * Handler for writeend events.
78 */
79 onwriteend: (event: ProgressEvent) => void;
80}
81
82declare var FileSaver: {
83 /**
84 * When the FileSaver constructor is called, the user agent must return a new FileSaver object with readyState set to INIT.
85 * This constructor must be visible when the script's global object is either a Window object or an object implementing the WorkerUtils interface.
86 */
87 new(data: Blob): FileSaver;
88};
89
90/**
91 * This interface expands on the FileSaver interface to allow for multiple write actions, rather than just saving a single Blob.
92 */
93interface FileWriter extends FileSaver {
94 /**
95 * The byte offset at which the next write to the file will occur. This must be no greater than length.
96 * A newly-created FileWriter must have position set to 0.
97 */
98 position: number;
99
100 /**
101 * The length of the file. If the user does not have read access to the file, this must be the highest byte offset at which the user has written.
102 */
103 length: number;
104
105 /**
106 * Write the supplied data to the file at position.
107 * @param data The blob to write.
108 */
109 write(data: Blob): void;
110
111 /**
112 * Seek sets the file position at which the next write will occur.
113 * @param offset If nonnegative, an absolute byte offset into the file. If negative, an offset back from the end of the file.
114 */
115 seek(offset: number): void;
116
117 /**
118 * Changes the length of the file to that specified. If shortening the file, data beyond the new length must be discarded. If extending the file, the existing data must be zero-padded up to the new length.
119 * @param size The size to which the length of the file is to be adjusted, measured in bytes.
120 */
121 truncate(size: number): void;
122}
123
124/**
125 * This interface lets users write, truncate, and append to files using simple synchronous calls.
126 * This interface is specified to be used only within Web Workers (WorkerUtils [WEBWORKERS]).
127 */
128interface FileWriterSync {
129 /**
130 * The byte offset at which the next write to the file will occur. This must be no greater than length.
131 */
132 position: number;
133
134 /**
135 * The length of the file. If the user does not have read access to the file, this must be the highest byte offset at which the user has written.
136 */
137 length: number;
138
139 /**
140 * Write the supplied data to the file at position. Upon completion, position will increase by data.size.
141 * @param data The blob to write.
142 */
143 write(data: Blob): void;
144
145 /**
146 * Seek sets the file position at which the next write will occur.
147 * @param offset An absolute byte offset into the file. If offset is greater than length, length is used instead. If offset is less than zero, length is added to it, so that it is treated as an offset back from the end of the file. If it is still less than zero, zero is used.
148 */
149 seek(offset: number): void;
150
151 /**
152 * Changes the length of the file to that specified. If shortening the file, data beyond the new length must be discarded. If extending the file, the existing data must be zero-padded up to the new length.
153 * Upon successful completion:
154 * <ul>
155 * <li>length must be equal to size.</li>
156 * <li>position must be the lesser of
157 * <ul>
158 * <li>its pre-truncate value,</li>
159 * <li>size.</li>
160 * </ul>
161 * </li>
162 * </ul>
163 * @param size The size to which the length of the file is to be adjusted, measured in bytes.
164 */
165 truncate(size: number): void;
166}