Skip to content

Commit 9c7e6b9

Browse files
Add getBytes()/getBlob()/getStream()
1 parent e6aa3e0 commit 9c7e6b9

25 files changed

+861
-288
lines changed

common/api-review/storage.api.md

+21-94
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ export function connectStorageEmulator(storage: FirebaseStorage, host: string, p
2121
mockUserToken?: EmulatorMockTokenOptions | string;
2222
}): void;
2323

24-
// Warning: (ae-forgotten-export) The symbol "StringData" needs to be exported by the entry point index.d.ts
25-
//
26-
// @internal (undocumented)
27-
export function _dataFromString(format: StringFormat, stringData: string): StringData;
28-
2924
// @public
3025
export function deleteObject(ref: StorageReference): Promise<void>;
3126

@@ -53,51 +48,6 @@ export interface FirebaseStorage extends _FirebaseService {
5348
maxUploadRetryTime: number;
5449
}
5550

56-
// @internal
57-
export class _FirebaseStorageImpl implements FirebaseStorage {
58-
constructor(
59-
app: FirebaseApp, _authProvider: Provider<FirebaseAuthInternalName>,
60-
_appCheckProvider: Provider<AppCheckInternalComponentName>,
61-
_url?: string | undefined, _firebaseVersion?: string | undefined);
62-
readonly app: FirebaseApp;
63-
// (undocumented)
64-
readonly _appCheckProvider: Provider<AppCheckInternalComponentName>;
65-
// (undocumented)
66-
protected readonly _appId: string | null;
67-
// (undocumented)
68-
readonly _authProvider: Provider<FirebaseAuthInternalName>;
69-
// (undocumented)
70-
_bucket: _Location | null;
71-
_delete(): Promise<void>;
72-
// (undocumented)
73-
readonly _firebaseVersion?: string | undefined;
74-
// (undocumented)
75-
_getAppCheckToken(): Promise<string | null>;
76-
// (undocumented)
77-
_getAuthToken(): Promise<string | null>;
78-
get host(): string;
79-
set host(host: string);
80-
// Warning: (ae-forgotten-export) The symbol "RequestInfo" needs to be exported by the entry point index.d.ts
81-
// Warning: (ae-forgotten-export) The symbol "Connection" needs to be exported by the entry point index.d.ts
82-
// Warning: (ae-forgotten-export) The symbol "Request" needs to be exported by the entry point index.d.ts
83-
//
84-
// (undocumented)
85-
_makeRequest<T>(requestInfo: RequestInfo_2<T>, requestFactory: () => Connection, authToken: string | null, appCheckToken: string | null): Request_2<T>;
86-
// (undocumented)
87-
makeRequestWithTokens<T>(requestInfo: RequestInfo_2<T>, requestFactory: () => Connection): Promise<Request_2<T>>;
88-
_makeStorageReference(loc: _Location): _Reference;
89-
get maxOperationRetryTime(): number;
90-
set maxOperationRetryTime(time: number);
91-
get maxUploadRetryTime(): number;
92-
set maxUploadRetryTime(time: number);
93-
// (undocumented)
94-
_overrideAuthToken?: string;
95-
// (undocumented)
96-
_protocol: string;
97-
// (undocumented)
98-
readonly _url?: string | undefined;
99-
}
100-
10151
// @public
10252
export interface FullMetadata extends UploadMetadata {
10353
bucket: string;
@@ -112,6 +62,12 @@ export interface FullMetadata extends UploadMetadata {
11262
updated: string;
11363
}
11464

65+
// @public
66+
export function getBlob(ref: StorageReference, maxDownloadSizeBytes?: number): Promise<Blob>;
67+
68+
// @public
69+
export function getBytes(ref: StorageReference, maxDownloadSizeBytes?: number): Promise<ArrayBuffer>;
70+
11571
// @internal (undocumented)
11672
export function _getChild(ref: StorageReference, childPath: string): _Reference;
11773

@@ -124,14 +80,6 @@ export function getMetadata(ref: StorageReference): Promise<FullMetadata>;
12480
// @public
12581
export function getStorage(app?: FirebaseApp, bucketUrl?: string): FirebaseStorage;
12682

127-
// Warning: (ae-forgotten-export) The symbol "StorageError" needs to be exported by the entry point index.d.ts
128-
//
129-
// @internal (undocumented)
130-
export function _invalidArgument(message: string): StorageError_2;
131-
132-
// @internal (undocumented)
133-
export function _invalidRootOperation(name: string): StorageError_2;
134-
13583
// @public
13684
export function list(ref: StorageReference, options?: ListOptions): Promise<ListResult>;
13785

@@ -176,19 +124,25 @@ export function ref(storage: FirebaseStorage, url?: string): StorageReference;
176124
// @public
177125
export function ref(storageOrRef: FirebaseStorage | StorageReference, path?: string): StorageReference;
178126

179-
// @internal
127+
// @public (undocumented)
180128
export class _Reference {
181-
constructor(_service: _FirebaseStorageImpl, location: string | _Location);
129+
// Warning: (ae-forgotten-export) The symbol "FirebaseStorageImpl" needs to be exported by the entry point index.d.ts
130+
// Warning: (ae-incompatible-release-tags) The symbol "__constructor" is marked as @public, but its signature references "Location" which is marked as @internal
131+
constructor(_service: FirebaseStorageImpl, location: string | _Location);
182132
get bucket(): string;
183133
get fullPath(): string;
134+
// Warning: (ae-incompatible-release-tags) The symbol "_location" is marked as @public, but its signature references "Location" which is marked as @internal
135+
//
184136
// (undocumented)
185137
_location: _Location;
186138
get name(): string;
139+
// Warning: (ae-incompatible-release-tags) The symbol "_newRef" is marked as @public, but its signature references "Location" which is marked as @internal
140+
//
187141
// (undocumented)
188-
protected _newRef(service: _FirebaseStorageImpl, location: _Location): _Reference;
142+
protected _newRef(service: FirebaseStorageImpl, location: _Location): _Reference;
189143
get parent(): _Reference | null;
190144
get root(): _Reference;
191-
get storage(): _FirebaseStorageImpl;
145+
get storage(): FirebaseStorageImpl;
192146
_throwIfRoot(name: string): void;
193147
// @override
194148
toString(): string;
@@ -232,43 +186,12 @@ export interface StorageReference {
232186
toString(): string;
233187
}
234188

235-
// @public
236-
export type StringFormat = typeof StringFormat[keyof typeof StringFormat];
237-
238-
// @public
239-
export const StringFormat: {
240-
readonly RAW: "raw";
241-
readonly BASE64: "base64";
242-
readonly BASE64URL: "base64url";
243-
readonly DATA_URL: "data_url";
244-
};
245-
246189
// @public
247190
export type TaskEvent = 'state_changed';
248191

249-
// @internal
250-
export type _TaskEvent = string;
251-
252-
// @internal
253-
export const _TaskEvent: {
254-
STATE_CHANGED: string;
255-
};
256-
257192
// @public
258193
export type TaskState = 'running' | 'paused' | 'success' | 'canceled' | 'error';
259194

260-
// @internal
261-
export type _TaskState = typeof _TaskState[keyof typeof _TaskState];
262-
263-
// @internal
264-
export const _TaskState: {
265-
readonly RUNNING: "running";
266-
readonly PAUSED: "paused";
267-
readonly SUCCESS: "success";
268-
readonly CANCELED: "canceled";
269-
readonly ERROR: "error";
270-
};
271-
272195
// @public
273196
export function updateMetadata(ref: StorageReference, metadata: SettableMetadata): Promise<FullMetadata>;
274197

@@ -289,6 +212,8 @@ export interface UploadResult {
289212
readonly ref: StorageReference;
290213
}
291214

215+
// Warning: (ae-forgotten-export) The symbol "StringFormat" needs to be exported by the entry point index.d.ts
216+
//
292217
// @public
293218
export function uploadString(ref: StorageReference, value: string, format?: StringFormat, metadata?: UploadMetadata): Promise<UploadResult>;
294219

@@ -311,9 +236,11 @@ export class _UploadTask {
311236
catch<T>(onRejected: (p1: StorageError_2) => T | Promise<T>): Promise<T>;
312237
// Warning: (ae-forgotten-export) The symbol "Metadata" needs to be exported by the entry point index.d.ts
313238
_metadata: Metadata | null;
239+
// Warning: (ae-forgotten-export) The symbol "TaskEvent" needs to be exported by the entry point index.d.ts
240+
// Warning: (ae-forgotten-export) The symbol "StorageError" needs to be exported by the entry point index.d.ts
314241
// Warning: (ae-forgotten-export) The symbol "Unsubscribe" needs to be exported by the entry point index.d.ts
315242
// Warning: (ae-forgotten-export) The symbol "Subscribe" needs to be exported by the entry point index.d.ts
316-
on(type: _TaskEvent, nextOrObserver?: StorageObserver<UploadTaskSnapshot> | null | ((snapshot: UploadTaskSnapshot) => unknown), error?: ((a: StorageError_2) => unknown) | null, completed?: Unsubscribe_2 | null): Unsubscribe_2 | Subscribe_2<UploadTaskSnapshot>;
243+
on(type: TaskEvent_2, nextOrObserver?: StorageObserver<UploadTaskSnapshot> | null | ((snapshot: UploadTaskSnapshot) => unknown), error?: ((a: StorageError_2) => unknown) | null, completed?: Unsubscribe_2 | null): Unsubscribe_2 | Subscribe_2<UploadTaskSnapshot>;
317244
pause(): boolean;
318245
resume(): boolean;
319246
get snapshot(): UploadTaskSnapshot;

packages/storage/.run/All Tests.run.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<env name="TS_NODE_CACHE" value="NO" />
1212
</envs>
1313
<ui>bdd</ui>
14-
<extra-mocha-options>--require ts-node/register/type-check --require index.node.ts</extra-mocha-options>
14+
<extra-mocha-options>--require ts-node/register/type-check --require src/index.node.ts</extra-mocha-options>
1515
<test-kind>PATTERN</test-kind>
1616
<test-pattern>test/{,!(browser)/**/}*.test.ts</test-pattern>
1717
<method v="2" />

packages/storage/src/api.browser.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2021 Google LLC
3+
* Copyright 2020 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -15,6 +15,30 @@
1515
* limitations under the License.
1616
*/
1717

18-
// TODO(mrschmidt): Add getBlob()
18+
import { StorageReference } from './public-types';
19+
import { Reference, getBlobInternal } from '../src/reference';
20+
import { getModularInstance } from '@firebase/util';
1921

20-
export {};
22+
/**
23+
* Downloads the data at the object's location. Returns an error if the object
24+
* is not found.
25+
*
26+
* To use this functionality, you have to whitelist your app's origin in your
27+
* Cloud Storage bucket. See also
28+
* https://cloud.google.com/storage/docs/configuring-cors
29+
*
30+
* This API is not available in Node.
31+
*
32+
* @public
33+
* @param ref - StorageReference where data should be download.
34+
* @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
35+
* retrieve.
36+
* @returns A Promise that resolves with a Blob containing the object's bytes
37+
*/
38+
export function getBlob(
39+
ref: StorageReference,
40+
maxDownloadSizeBytes?: number
41+
): Promise<Blob> {
42+
ref = getModularInstance(ref);
43+
return getBlobInternal(ref as Reference, maxDownloadSizeBytes);
44+
}

packages/storage/src/api.node.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2021 Google LLC
3+
* Copyright 2020 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -15,6 +15,26 @@
1515
* limitations under the License.
1616
*/
1717

18-
// TODO(mrschmidt): Add getStream()
18+
import { StorageReference } from './public-types';
19+
import { Reference, getStreamInternal } from '../src/reference';
20+
import { getModularInstance } from '@firebase/util';
1921

20-
export {};
22+
/**
23+
* Downloads the data at the object's location. Raises an error event if the
24+
* object is not found.
25+
*
26+
* This API is only available in Node.
27+
*
28+
* @public
29+
* @param ref - StorageReference where data should be download.
30+
* @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
31+
* retrieve.
32+
* @returns A stream with the object's data as bytes
33+
*/
34+
export function getStream(
35+
ref: StorageReference,
36+
maxDownloadSizeBytes?: number
37+
): NodeJS.ReadableStream {
38+
ref = getModularInstance(ref);
39+
return getStreamInternal(ref as Reference, maxDownloadSizeBytes);
40+
}

packages/storage/src/api.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ import {
4747
getDownloadURL as getDownloadURLInternal,
4848
deleteObject as deleteObjectInternal,
4949
Reference,
50-
_getChild as _getChildInternal
50+
_getChild as _getChildInternal,
51+
getBytesInternal
5152
} from './reference';
5253
import { STORAGE_TYPE } from './constants';
5354
import { EmulatorMockTokenOptions, getModularInstance } from '@firebase/util';
@@ -76,6 +77,28 @@ export {
7677
} from './implementation/taskenums';
7778
export { StringFormat };
7879

80+
/**
81+
* Downloads the data at the object's location. Returns an error if the object
82+
* is not found.
83+
*
84+
* To use this functionality, you have to whitelist your app's origin in your
85+
* Cloud Storage bucket. See also
86+
* https://cloud.google.com/storage/docs/configuring-cors
87+
*
88+
* @public
89+
* @param ref - StorageReference where data should be download.
90+
* @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
91+
* retrieve.
92+
* @returns A Promise containing the object's bytes
93+
*/
94+
export function getBytes(
95+
ref: StorageReference,
96+
maxDownloadSizeBytes?: number
97+
): Promise<ArrayBuffer> {
98+
ref = getModularInstance(ref);
99+
return getBytesInternal(ref as Reference, maxDownloadSizeBytes);
100+
}
101+
79102
/**
80103
* Uploads data to this object's location.
81104
* The upload is not resumable.

packages/storage/src/implementation/connection.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ export type Headers = Record<string, string>;
2121
/**
2222
* A lightweight wrapper around XMLHttpRequest with a
2323
* goog.net.XhrIo-like interface.
24+
*
25+
* ResponseType is generally either `string`, `ArrayBuffer` or `ReadableSteam`.
26+
* You can create a new connection by invoking `newTextConnection()`,
27+
* `newBytesConnection()` or `newStreamConnection()`.
2428
*/
25-
export interface Connection {
29+
export interface Connection<ResponseType> {
2630
send(
2731
url: string,
2832
method: string,
@@ -34,7 +38,9 @@ export interface Connection {
3438

3539
getStatus(): number;
3640

37-
getResponseText(): string;
41+
getResponse(): ResponseType;
42+
43+
getErrorText(): string;
3844

3945
/**
4046
* Abort the request.

0 commit comments

Comments
 (0)