UNPKG

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