1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | import * as ConfigStore from 'configstore';
|
10 | import { GaxiosOptions, GaxiosPromise, GaxiosResponse } from 'gaxios';
|
11 | import { GoogleAuthOptions } from 'google-auth-library';
|
12 | import * as Pumpify from 'pumpify';
|
13 | export declare const PROTOCOL_REGEX: RegExp;
|
14 | export interface ErrorWithCode extends Error {
|
15 | status: number;
|
16 | }
|
17 | export type CreateUriCallback = (err: Error | null, uri?: string) => void;
|
18 | export interface Encryption {
|
19 | key: {};
|
20 | hash: {};
|
21 | }
|
22 | export type PredefinedAcl = 'authenticatedRead' | 'bucketOwnerFullControl' | 'bucketOwnerRead' | 'private' | 'projectPrivate' | 'publicRead';
|
23 | export interface QueryParameters {
|
24 | contentEncoding?: string;
|
25 | ifGenerationMatch?: number;
|
26 | ifGenerationNotMatch?: number;
|
27 | ifMetagenerationMatch?: number;
|
28 | ifMetagenerationNotMatch?: number;
|
29 | kmsKeyName?: string;
|
30 | predefinedAcl?: PredefinedAcl;
|
31 | projection?: 'full' | 'noAcl';
|
32 | userProject?: string;
|
33 | }
|
34 | export interface UploadConfig {
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | apiEndpoint?: string;
|
43 | |
44 |
|
45 |
|
46 | bucket: string;
|
47 | |
48 |
|
49 |
|
50 | file: string;
|
51 | |
52 |
|
53 |
|
54 | authConfig?: GoogleAuthOptions;
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | authClient?: {
|
62 | request: <T>(opts: GaxiosOptions) => Promise<GaxiosResponse<T>> | GaxiosPromise<T>;
|
63 | };
|
64 | |
65 |
|
66 |
|
67 |
|
68 | configPath?: string;
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | chunkSize?: number;
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 | customRequestOptions?: GaxiosOptions;
|
84 | |
85 |
|
86 |
|
87 |
|
88 | generation?: number;
|
89 | |
90 |
|
91 |
|
92 |
|
93 | key?: string | Buffer;
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | kmsKeyName?: string;
|
101 | |
102 |
|
103 |
|
104 | metadata?: ConfigMetadata;
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 | offset?: number;
|
111 | |
112 |
|
113 |
|
114 | origin?: string;
|
115 | |
116 |
|
117 |
|
118 |
|
119 | params?: QueryParameters;
|
120 | |
121 |
|
122 |
|
123 | predefinedAcl?: PredefinedAcl;
|
124 | |
125 |
|
126 |
|
127 |
|
128 | private?: boolean;
|
129 | |
130 |
|
131 |
|
132 |
|
133 | public?: boolean;
|
134 | |
135 |
|
136 |
|
137 |
|
138 | uri?: string;
|
139 | |
140 |
|
141 |
|
142 |
|
143 | userProject?: string;
|
144 | |
145 |
|
146 |
|
147 | retryOptions?: RetryOptions;
|
148 | }
|
149 | export interface ConfigMetadata {
|
150 | [key: string]: any;
|
151 | |
152 |
|
153 |
|
154 | contentLength?: number;
|
155 | |
156 |
|
157 |
|
158 | contentType?: string;
|
159 | }
|
160 | export interface RetryOptions {
|
161 | retryDelayMultiplier?: number;
|
162 | totalTimeout?: number;
|
163 | maxRetryDelay?: number;
|
164 | autoRetry?: boolean;
|
165 | maxRetries?: number;
|
166 | retryableErrorFn?: (err: ApiError) => boolean;
|
167 | }
|
168 | export interface GoogleInnerError {
|
169 | reason?: string;
|
170 | }
|
171 | export interface ApiError extends Error {
|
172 | code?: number;
|
173 | errors?: GoogleInnerError[];
|
174 | }
|
175 | export declare class Upload extends Pumpify {
|
176 | bucket: string;
|
177 | file: string;
|
178 | apiEndpoint: string;
|
179 | baseURI: string;
|
180 | authConfig?: {
|
181 | scopes?: string[];
|
182 | };
|
183 | authClient: {
|
184 | request: <T>(opts: GaxiosOptions) => Promise<GaxiosResponse<T>> | GaxiosPromise<T>;
|
185 | };
|
186 | cacheKey: string;
|
187 | chunkSize?: number;
|
188 | customRequestOptions: GaxiosOptions;
|
189 | generation?: number;
|
190 | key?: string | Buffer;
|
191 | kmsKeyName?: string;
|
192 | metadata: ConfigMetadata;
|
193 | offset?: number;
|
194 | origin?: string;
|
195 | params: QueryParameters;
|
196 | predefinedAcl?: PredefinedAcl;
|
197 | private?: boolean;
|
198 | public?: boolean;
|
199 | uri?: string;
|
200 | userProject?: string;
|
201 | encryption?: Encryption;
|
202 | configStore: ConfigStore;
|
203 | uriProvidedManually: boolean;
|
204 | numBytesWritten: number;
|
205 | numRetries: number;
|
206 | contentLength: number | '*';
|
207 | retryLimit: number;
|
208 | maxRetryDelay: number;
|
209 | retryDelayMultiplier: number;
|
210 | maxRetryTotalTimeout: number;
|
211 | timeOfFirstRequest: number;
|
212 | retryableErrorFn?: (err: ApiError) => boolean;
|
213 | private upstreamChunkBuffer;
|
214 | private chunkBufferEncoding?;
|
215 | private numChunksReadInRequest;
|
216 | |
217 |
|
218 |
|
219 |
|
220 |
|
221 | private lastChunkSent;
|
222 | private upstreamEnded;
|
223 | constructor(cfg: UploadConfig);
|
224 | /** A stream representing the incoming data to upload */
|
225 | private readonly upstream;
|
226 | /**
|
227 | * A handler for `upstream` to write and buffer its data.
|
228 | *
|
229 | * @param chunk The chunk to append to the buffer
|
230 | * @param encoding The encoding of the chunk
|
231 | * @param readCallback A callback for when the buffer has been read downstream
|
232 | */
|
233 | private writeToChunkBuffer;
|
234 | /**
|
235 | * Prepends data back to the upstream chunk buffer.
|
236 | *
|
237 | * @param chunk The data to prepend
|
238 | */
|
239 | private unshiftChunkBuffer;
|
240 | /**
|
241 | * Retrieves data from upstream's buffer.
|
242 | *
|
243 | * @param limit The maximum amount to return from the buffer.
|
244 | * @returns The data requested.
|
245 | */
|
246 | private pullFromChunkBuffer;
|
247 | /**
|
248 | * A handler for determining if data is ready to be read from upstream.
|
249 | *
|
250 | * @returns If there will be more chunks to read in the future
|
251 | */
|
252 | private waitForNextChunk;
|
253 | /**
|
254 | * Reads data from upstream up to the provided `limit`.
|
255 | * Ends when the limit has reached or no data is expected to be pushed from upstream.
|
256 | *
|
257 | * @param limit The most amount of data this iterator should return. `Infinity` by default.
|
258 | * @param oneChunkMode Determines if one, exhaustive chunk is yielded for the iterator
|
259 | */
|
260 | private upstreamIterator;
|
261 | createURI(): Promise<string>;
|
262 | createURI(callback: CreateUriCallback): void;
|
263 | protected createURIAsync(): Promise<string>;
|
264 | private continueUploading;
|
265 | startUploading(): Promise<void>;
|
266 | private responseHandler;
|
267 | /**
|
268 | * Check if this is the same content uploaded previously. This caches a
|
269 | * slice of the first chunk, then compares it with the first byte of
|
270 | * incoming data.
|
271 | *
|
272 | * @returns if the request is ok to continue as-is
|
273 | */
|
274 | private ensureUploadingSameObject;
|
275 | private getAndSetOffset;
|
276 | private makeRequest;
|
277 | private makeRequestStream;
|
278 | private restart;
|
279 | private get;
|
280 | private set;
|
281 | deleteConfig(): void;
|
282 | /**
|
283 | * @return {bool} is the request good?
|
284 | */
|
285 | private onResponse;
|
286 | |
287 |
|
288 |
|
289 | private attemptDelayedRetry;
|
290 | |
291 |
|
292 |
|
293 | private getRetryDelay;
|
294 | private sanitizeEndpoint;
|
295 | |
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 | isSuccessfulResponse(status: number): boolean;
|
302 | |
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 | isServerErrorResponse(status: number): boolean;
|
309 | }
|
310 | export declare function upload(cfg: UploadConfig): Upload;
|
311 | export declare function createURI(cfg: UploadConfig): Promise<string>;
|
312 | export declare function createURI(cfg: UploadConfig, callback: CreateUriCallback): void;
|