1 |
|
2 |
|
3 | import { GaxiosOptions, GaxiosPromise, GaxiosResponse } from 'gaxios';
|
4 | import { GoogleAuthOptions } from 'google-auth-library';
|
5 | import { Writable, WritableOptions } from 'stream';
|
6 | import { RetryOptions, PreconditionOptions } from './storage';
|
7 | export declare const PROTOCOL_REGEX: RegExp;
|
8 | export interface ErrorWithCode extends Error {
|
9 | code: number;
|
10 | }
|
11 | export type CreateUriCallback = (err: Error | null, uri?: string) => void;
|
12 | export interface Encryption {
|
13 | key: {};
|
14 | hash: {};
|
15 | }
|
16 | export type PredefinedAcl = 'authenticatedRead' | 'bucketOwnerFullControl' | 'bucketOwnerRead' | 'private' | 'projectPrivate' | 'publicRead';
|
17 | export interface QueryParameters extends PreconditionOptions {
|
18 | contentEncoding?: string;
|
19 | kmsKeyName?: string;
|
20 | predefinedAcl?: PredefinedAcl;
|
21 | projection?: 'full' | 'noAcl';
|
22 | userProject?: string;
|
23 | }
|
24 | export interface UploadConfig extends Pick<WritableOptions, 'highWaterMark'> {
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | apiEndpoint?: string;
|
33 | |
34 |
|
35 |
|
36 | bucket: string;
|
37 | |
38 |
|
39 |
|
40 | file: string;
|
41 | |
42 |
|
43 |
|
44 | authConfig?: GoogleAuthOptions;
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | authClient?: {
|
52 | request: <T>(opts: GaxiosOptions) => Promise<GaxiosResponse<T>> | GaxiosPromise<T>;
|
53 | };
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | chunkSize?: number;
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 | customRequestOptions?: GaxiosOptions;
|
69 | |
70 |
|
71 |
|
72 |
|
73 | generation?: number;
|
74 | |
75 |
|
76 |
|
77 |
|
78 | key?: string | Buffer;
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | kmsKeyName?: string;
|
86 | |
87 |
|
88 |
|
89 | metadata?: ConfigMetadata;
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 | offset?: number;
|
96 | |
97 |
|
98 |
|
99 | origin?: string;
|
100 | |
101 |
|
102 |
|
103 |
|
104 | params?: QueryParameters;
|
105 | |
106 |
|
107 |
|
108 | predefinedAcl?: PredefinedAcl;
|
109 | |
110 |
|
111 |
|
112 |
|
113 | private?: boolean;
|
114 | |
115 |
|
116 |
|
117 |
|
118 | public?: boolean;
|
119 | |
120 |
|
121 |
|
122 |
|
123 | uri?: string;
|
124 | |
125 |
|
126 |
|
127 |
|
128 | userProject?: string;
|
129 | |
130 |
|
131 |
|
132 | retryOptions: RetryOptions;
|
133 | }
|
134 | export interface ConfigMetadata {
|
135 | [key: string]: any;
|
136 | |
137 |
|
138 |
|
139 | contentLength?: number;
|
140 | |
141 |
|
142 |
|
143 | contentType?: string;
|
144 | }
|
145 | export interface GoogleInnerError {
|
146 | reason?: string;
|
147 | }
|
148 | export interface ApiError extends Error {
|
149 | code?: number;
|
150 | errors?: GoogleInnerError[];
|
151 | }
|
152 | export declare class Upload extends Writable {
|
153 | #private;
|
154 | bucket: string;
|
155 | file: string;
|
156 | apiEndpoint: string;
|
157 | baseURI: string;
|
158 | authConfig?: {
|
159 | scopes?: string[];
|
160 | };
|
161 | authClient: {
|
162 | request: <T>(opts: GaxiosOptions) => Promise<GaxiosResponse<T>> | GaxiosPromise<T>;
|
163 | };
|
164 | cacheKey: string;
|
165 | chunkSize?: number;
|
166 | customRequestOptions: GaxiosOptions;
|
167 | generation?: number;
|
168 | key?: string | Buffer;
|
169 | kmsKeyName?: string;
|
170 | metadata: ConfigMetadata;
|
171 | offset?: number;
|
172 | origin?: string;
|
173 | params: QueryParameters;
|
174 | predefinedAcl?: PredefinedAcl;
|
175 | private?: boolean;
|
176 | public?: boolean;
|
177 | uri?: string;
|
178 | userProject?: string;
|
179 | encryption?: Encryption;
|
180 | uriProvidedManually: boolean;
|
181 | numBytesWritten: number;
|
182 | numRetries: number;
|
183 | contentLength: number | '*';
|
184 | retryOptions: RetryOptions;
|
185 | timeOfFirstRequest: number;
|
186 | private currentInvocationId;
|
187 | |
188 |
|
189 |
|
190 | private writeBuffers;
|
191 | private numChunksReadInRequest;
|
192 | |
193 |
|
194 |
|
195 |
|
196 |
|
197 | private localWriteCache;
|
198 | private localWriteCacheByteLength;
|
199 | private upstreamEnded;
|
200 | constructor(cfg: UploadConfig);
|
201 | /**
|
202 | * Prevent 'finish' event until the upload has succeeded.
|
203 | *
|
204 | * @param fireFinishEvent The finish callback
|
205 | */
|
206 | _final(fireFinishEvent?: () => void): void;
|
207 | /**
|
208 | * Handles incoming data from upstream
|
209 | *
|
210 | * @param chunk The chunk to append to the buffer
|
211 | * @param encoding The encoding of the chunk
|
212 | * @param readCallback A callback for when the buffer has been read downstream
|
213 | */
|
214 | _write(chunk: Buffer | string, encoding: BufferEncoding, readCallback?: () => void): void;
|
215 | /**
|
216 | * Prepends the local buffer to write buffer and resets it.
|
217 | *
|
218 | * @param keepLastBytes number of bytes to keep from the end of the local buffer.
|
219 | */
|
220 | private prependLocalBufferToUpstream;
|
221 | /**
|
222 | * Retrieves data from upstream's buffer.
|
223 | *
|
224 | * @param limit The maximum amount to return from the buffer.
|
225 | * @returns The data requested.
|
226 | */
|
227 | private pullFromChunkBuffer;
|
228 | /**
|
229 | * A handler for determining if data is ready to be read from upstream.
|
230 | *
|
231 | * @returns If there will be more chunks to read in the future
|
232 | */
|
233 | private waitForNextChunk;
|
234 | /**
|
235 | * Reads data from upstream up to the provided `limit`.
|
236 | * Ends when the limit has reached or no data is expected to be pushed from upstream.
|
237 | *
|
238 | * @param limit The most amount of data this iterator should return. `Infinity` by default.
|
239 | */
|
240 | private upstreamIterator;
|
241 | createURI(): Promise<string>;
|
242 | createURI(callback: CreateUriCallback): void;
|
243 | protected createURIAsync(): Promise<string>;
|
244 | private continueUploading;
|
245 | startUploading(): Promise<void>;
|
246 | private responseHandler;
|
247 | private getAndSetOffset;
|
248 | private makeRequest;
|
249 | private makeRequestStream;
|
250 | /**
|
251 | * @return {bool} is the request good?
|
252 | */
|
253 | private onResponse;
|
254 | |
255 |
|
256 |
|
257 | private attemptDelayedRetry;
|
258 | |
259 |
|
260 |
|
261 | private getRetryDelay;
|
262 | private sanitizeEndpoint;
|
263 | |
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | isSuccessfulResponse(status: number): boolean;
|
270 | }
|
271 | export declare function upload(cfg: UploadConfig): Upload;
|
272 | export declare function createURI(cfg: UploadConfig): Promise<string>;
|
273 | export declare function createURI(cfg: UploadConfig, callback: CreateUriCallback): void;
|