UNPKG

8.91 kBTypeScriptView Raw
1/// <reference types="node" />
2/// <reference types="node" />
3import { GaxiosOptions, GaxiosPromise, GaxiosResponse } from 'gaxios';
4import { GoogleAuthOptions } from 'google-auth-library';
5import { Writable } from 'stream';
6import { RetryOptions, PreconditionOptions } from './storage';
7export declare const PROTOCOL_REGEX: RegExp;
8export interface ErrorWithCode extends Error {
9 code: number;
10}
11export type CreateUriCallback = (err: Error | null, uri?: string) => void;
12export interface Encryption {
13 key: {};
14 hash: {};
15}
16export type PredefinedAcl = 'authenticatedRead' | 'bucketOwnerFullControl' | 'bucketOwnerRead' | 'private' | 'projectPrivate' | 'publicRead';
17export interface QueryParameters extends PreconditionOptions {
18 contentEncoding?: string;
19 kmsKeyName?: string;
20 predefinedAcl?: PredefinedAcl;
21 projection?: 'full' | 'noAcl';
22 userProject?: string;
23}
24export interface UploadConfig {
25 /**
26 * The API endpoint used for the request.
27 * Defaults to `storage.googleapis.com`.
28 * **Warning**:
29 * If this value does not match the pattern *.googleapis.com,
30 * an emulator context will be assumed and authentication will be bypassed.
31 */
32 apiEndpoint?: string;
33 /**
34 * The name of the destination bucket.
35 */
36 bucket: string;
37 /**
38 * The name of the destination file.
39 */
40 file: string;
41 /**
42 * The GoogleAuthOptions passed to google-auth-library
43 */
44 authConfig?: GoogleAuthOptions;
45 /**
46 * If you want to re-use an auth client from google-auto-auth, pass an
47 * instance here.
48 * Defaults to GoogleAuth and gets automatically overridden if an
49 * emulator context is detected.
50 */
51 authClient?: {
52 request: <T>(opts: GaxiosOptions) => Promise<GaxiosResponse<T>> | GaxiosPromise<T>;
53 };
54 /**
55 * Create a separate request per chunk.
56 *
57 * This value is in bytes and should be a multiple of 256 KiB (2^18).
58 * We recommend using at least 8 MiB for the chunk size.
59 *
60 * @link https://cloud.google.com/storage/docs/performing-resumable-uploads#chunked-upload
61 */
62 chunkSize?: number;
63 /**
64 * For each API request we send, you may specify custom request options that
65 * we'll add onto the request. The request options follow the gaxios API:
66 * https://github.com/googleapis/gaxios#request-options.
67 */
68 customRequestOptions?: GaxiosOptions;
69 /**
70 * This will cause the upload to fail if the current generation of the remote
71 * object does not match the one provided here.
72 */
73 generation?: number;
74 /**
75 * A customer-supplied encryption key. See
76 * https://cloud.google.com/storage/docs/encryption#customer-supplied.
77 */
78 key?: string | Buffer;
79 /**
80 * Resource name of the Cloud KMS key, of the form
81 * `projects/my-project/locations/global/keyRings/my-kr/cryptoKeys/my-key`,
82 * that will be used to encrypt the object. Overrides the object metadata's
83 * `kms_key_name` value, if any.
84 */
85 kmsKeyName?: string;
86 /**
87 * Any metadata you wish to set on the object.
88 */
89 metadata?: ConfigMetadata;
90 /**
91 * The starting byte of the upload stream, for resuming an interrupted upload.
92 * See
93 * https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload#resume-upload.
94 */
95 offset?: number;
96 /**
97 * Set an Origin header when creating the resumable upload URI.
98 */
99 origin?: string;
100 /**
101 * Specify query parameters that go along with the initial upload request. See
102 * https://cloud.google.com/storage/docs/json_api/v1/objects/insert#parameters
103 */
104 params?: QueryParameters;
105 /**
106 * Apply a predefined set of access controls to the created file.
107 */
108 predefinedAcl?: PredefinedAcl;
109 /**
110 * Make the uploaded file private. (Alias for config.predefinedAcl =
111 * 'private')
112 */
113 private?: boolean;
114 /**
115 * Make the uploaded file public. (Alias for config.predefinedAcl =
116 * 'publicRead')
117 */
118 public?: boolean;
119 /**
120 * If you already have a resumable URI from a previously-created resumable
121 * upload, just pass it in here and we'll use that.
122 */
123 uri?: string;
124 /**
125 * If the bucket being accessed has requesterPays functionality enabled, this
126 * can be set to control which project is billed for the access of this file.
127 */
128 userProject?: string;
129 /**
130 * Configuration options for retrying retryable errors.
131 */
132 retryOptions: RetryOptions;
133}
134export interface ConfigMetadata {
135 [key: string]: any;
136 /**
137 * Set the length of the file being uploaded.
138 */
139 contentLength?: number;
140 /**
141 * Set the content type of the incoming data.
142 */
143 contentType?: string;
144}
145export interface GoogleInnerError {
146 reason?: string;
147}
148export interface ApiError extends Error {
149 code?: number;
150 errors?: GoogleInnerError[];
151}
152export declare class Upload extends Writable {
153 bucket: string;
154 file: string;
155 apiEndpoint: string;
156 baseURI: string;
157 authConfig?: {
158 scopes?: string[];
159 };
160 authClient: {
161 request: <T>(opts: GaxiosOptions) => Promise<GaxiosResponse<T>> | GaxiosPromise<T>;
162 };
163 cacheKey: string;
164 chunkSize?: number;
165 customRequestOptions: GaxiosOptions;
166 generation?: number;
167 key?: string | Buffer;
168 kmsKeyName?: string;
169 metadata: ConfigMetadata;
170 offset?: number;
171 origin?: string;
172 params: QueryParameters;
173 predefinedAcl?: PredefinedAcl;
174 private?: boolean;
175 public?: boolean;
176 uri?: string;
177 userProject?: string;
178 encryption?: Encryption;
179 uriProvidedManually: boolean;
180 numBytesWritten: number;
181 numRetries: number;
182 contentLength: number | '*';
183 retryOptions: RetryOptions;
184 timeOfFirstRequest: number;
185 private currentInvocationId;
186 private upstreamChunkBuffer;
187 private chunkBufferEncoding?;
188 private numChunksReadInRequest;
189 /**
190 * A chunk used for caching the most recent upload chunk.
191 * We should not assume that the server received all bytes sent in the request.
192 * - https://cloud.google.com/storage/docs/performing-resumable-uploads#chunked-upload
193 */
194 private lastChunkSent;
195 private upstreamEnded;
196 constructor(cfg: UploadConfig);
197 /**
198 * Prevent 'finish' event until the upload has succeeded.
199 *
200 * @param fireFinishEvent The finish callback
201 */
202 _final(fireFinishEvent?: () => void): void;
203 /**
204 * Handles incoming data from upstream
205 *
206 * @param chunk The chunk to append to the buffer
207 * @param encoding The encoding of the chunk
208 * @param readCallback A callback for when the buffer has been read downstream
209 */
210 _write(chunk: Buffer | string, encoding: BufferEncoding, readCallback?: () => void): void;
211 /**
212 * Prepends data back to the upstream chunk buffer.
213 *
214 * @param chunk The data to prepend
215 */
216 private unshiftChunkBuffer;
217 /**
218 * Retrieves data from upstream's buffer.
219 *
220 * @param limit The maximum amount to return from the buffer.
221 * @returns The data requested.
222 */
223 private pullFromChunkBuffer;
224 /**
225 * A handler for determining if data is ready to be read from upstream.
226 *
227 * @returns If there will be more chunks to read in the future
228 */
229 private waitForNextChunk;
230 /**
231 * Reads data from upstream up to the provided `limit`.
232 * Ends when the limit has reached or no data is expected to be pushed from upstream.
233 *
234 * @param limit The most amount of data this iterator should return. `Infinity` by default.
235 * @param oneChunkMode Determines if one, exhaustive chunk is yielded for the iterator
236 */
237 private upstreamIterator;
238 createURI(): Promise<string>;
239 createURI(callback: CreateUriCallback): void;
240 protected createURIAsync(): Promise<string>;
241 private continueUploading;
242 startUploading(): Promise<void>;
243 private responseHandler;
244 private getAndSetOffset;
245 private makeRequest;
246 private makeRequestStream;
247 /**
248 * @return {bool} is the request good?
249 */
250 private onResponse;
251 /**
252 * @param resp GaxiosResponse object from previous attempt
253 */
254 private attemptDelayedRetry;
255 /**
256 * @returns {number} the amount of time to wait before retrying the request
257 */
258 private getRetryDelay;
259 private sanitizeEndpoint;
260 /**
261 * Check if a given status code is 2xx
262 *
263 * @param status The status code to check
264 * @returns if the status is 2xx
265 */
266 isSuccessfulResponse(status: number): boolean;
267}
268export declare function upload(cfg: UploadConfig): Upload;
269export declare function createURI(cfg: UploadConfig): Promise<string>;
270export declare function createURI(cfg: UploadConfig, callback: CreateUriCallback): void;