Skip to content

Commit 2194d03

Browse files
WIP
1 parent 27c330c commit 2194d03

File tree

8 files changed

+91
-68
lines changed

8 files changed

+91
-68
lines changed

packages/storage/compat/list.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
import * as types from '@firebase/storage-types';
20+
import {ListResult} from "../src/list";
21+
import {ReferenceCompat} from "./reference";
22+
23+
24+
export class ListResultCompat implements types.ListResult {
25+
constructor(private readonly delegate: ListResult) {
26+
}
27+
28+
prefixes = this.delegate.prefixes.map(v => ReferenceCompat.fromReference(v));
29+
items = this.delegate.items.map(v => ReferenceCompat.fromReference(v));
30+
nextPageToken = this.delegate.nextPageToken;
31+
32+
}

packages/storage/compat/reference.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,31 @@ import {
3838
} from '../src/implementation/args';
3939
import { Metadata } from '../src/metadata';
4040
import { StringFormat, formatValidator } from '../src/implementation/string';
41-
import { ListResult, ListOptions } from '../src/list';
41+
import { ListOptions } from '../src/list';
4242
import { UploadTaskCompat } from './task';
43+
import {ListResultCompat} from "./list";
44+
import {Location} from "../src/implementation/location";
45+
import {StorageServiceCompat} from "./service";
4346

4447
export class ReferenceCompat extends Reference implements types.Reference {
4548
static fromReference(ref: Reference): types.Reference {
4649
return new ReferenceCompat(ref.service, ref.location);
4750
}
51+
52+
get root() : any {
53+
const location = new Location(this.location.bucket, '');
54+
return new ReferenceCompat(this.service, location);
55+
}
56+
57+
get storage(): any {
58+
return StorageServiceCompat.fromService(this.service);
59+
}
60+
4861
toString(): string {
4962
validate('toString', [], arguments);
5063
return super.toString();
5164
}
65+
5266
/**
5367
* @return A reference to the object obtained by
5468
* appending childPath, removing any duplicate, beginning, or trailing
@@ -82,7 +96,7 @@ export class ReferenceCompat extends Reference implements types.Reference {
8296
put(
8397
data: Blob | Uint8Array | ArrayBuffer,
8498
metadata?: Metadata
85-
): UploadTaskCompat {
99+
): any {
86100
validate('put', [uploadDataSpec(), metadataSpec(true)], arguments);
87101
this.throwIfRoot_('put');
88102
return uploadBytes(this, data, metadata) as UploadTaskCompat;
@@ -98,7 +112,7 @@ export class ReferenceCompat extends Reference implements types.Reference {
98112
value: string,
99113
format: StringFormat = StringFormat.RAW,
100114
metadata?: Metadata
101-
): UploadTaskCompat {
115+
): any {
102116
validate(
103117
'putString',
104118
[stringSpec(), stringSpec(formatValidator, true), metadataSpec(true)],
@@ -124,9 +138,9 @@ export class ReferenceCompat extends Reference implements types.Reference {
124138
* sub-directories and `items` contains references to objects in this
125139
* folder. `nextPageToken` is never returned.
126140
*/
127-
listAll(): Promise<ListResult> {
141+
listAll(): Promise<types.ListResult> {
128142
validate('listAll', [], arguments);
129-
return listAll(this);
143+
return listAll(this).then(r => new ListResultCompat(r));
130144
}
131145
/**
132146
* List items (files) and prefixes (folders) under this storage reference.
@@ -148,9 +162,9 @@ export class ReferenceCompat extends Reference implements types.Reference {
148162
* contains references to objects in this folder. `nextPageToken`
149163
* can be used to get the rest of the results.
150164
*/
151-
list(options?: ListOptions | null): Promise<ListResult> {
165+
list(options?: ListOptions | null): Promise<types.ListResult> {
152166
validate('list', [listOptionSpec(true)], arguments);
153-
return list(this, options);
167+
return list(this, options).then(r => new ListResultCompat(r));
154168
}
155169

156170
/**

packages/storage/compat/service.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import * as types from '@firebase/storage-types';
1819
import { StorageService, isUrl } from '../src/service';
1920
import { Location } from '../src/implementation/location';
2021
import { FirebaseApp } from '@firebase/app-types';
@@ -53,24 +54,29 @@ export function pathValidator(path: unknown): void {
5354
*
5455
* @struct
5556
*/
56-
export class StorageServiceCompat extends StorageService {
57+
export class StorageServiceCompat extends StorageService implements types.FirebaseStorage {
5758
private readonly internals_: ServiceInternals;
5859

5960
constructor(
60-
app: FirebaseApp | null,
61+
app: FirebaseApp,
6162
authProvider: Provider<FirebaseAuthInternalName>,
62-
pool: XhrIoPool,
63+
pool: XhrIoPool,
6364
url?: string
6465
) {
6566
super(app, authProvider, pool, url);
6667
this.internals_ = new ServiceInternals(this);
6768
}
6869

70+
71+
static fromService(service: StorageService) : StorageServiceCompat {
72+
return new StorageServiceCompat(service.app, service.authProvider_, service.pool_, service.url_);
73+
}
74+
6975
/**
7076
* Returns a firebaseStorage.Reference for the given path in the default
7177
* bucket.
7278
*/
73-
ref(path?: string): ReferenceCompat {
79+
ref(path?: string): types.Reference {
7480
args.validate('ref', [args.stringSpec(pathValidator, true)], arguments);
7581

7682
const reference = new ReferenceCompat(this, this.bucket_!);
@@ -85,7 +91,7 @@ export class StorageServiceCompat extends StorageService {
8591
* Returns a firebaseStorage.Reference object for the given absolute URL,
8692
* which must be a gs:// or http[s]:// URL.
8793
*/
88-
refFromURL(url: string): ReferenceCompat {
94+
refFromURL(url: string): types.Reference {
8995
args.validate(
9096
'refFromURL',
9197
[args.stringSpec(urlValidator, false)],
@@ -114,13 +120,10 @@ export class StorageServiceCompat extends StorageService {
114120
this.maxOperationRetryTime_ = time;
115121
}
116122

117-
get app(): FirebaseApp | null {
118-
return this.app_;
119-
}
120-
121123
get INTERNAL(): ServiceInternals {
122124
return this.internals_;
123125
}
126+
124127
}
125128

126129
/**

packages/storage/compat/task.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,19 @@ export class UploadTaskCompat extends UploadTask<UploadTaskSnapshotCompat>
3838
) {
3939
super(refCompat_, blob, metadata);
4040
}
41+
4142
get snapshot(): UploadTaskSnapshotCompat {
4243
const externalState = taskStateFromInternalTaskState(this.state_);
4344
return new UploadTaskSnapshotCompat(
4445
this.transferred_,
4546
this.blob_.size(),
4647
externalState,
47-
this.metadata_,
48+
this.metadata_!, // VERIFY
4849
this,
4950
this.refCompat_
5051
);
5152
}
53+
5254
on(
5355
type: TaskEvent,
5456
nextOrObserver?:

packages/storage/compat/tasksnapshot.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
18+
19+
import * as types from '@firebase/storage-types';
1720
import { TaskState } from '../src/implementation/taskenums';
1821
import { Metadata } from '../src/metadata';
1922
import { ReferenceCompat } from './reference';
2023
import { UploadTaskCompat } from './task';
2124

22-
export class UploadTaskSnapshotCompat {
25+
export class UploadTaskSnapshotCompat implements types.UploadTaskSnapshot{
2326
constructor(
2427
readonly bytesTransferred: number,
2528
readonly totalBytes: number,
2629
readonly state: TaskState,
27-
readonly metadata: Metadata | null,
30+
readonly metadata: Metadata,
2831
readonly task: UploadTaskCompat,
2932
readonly ref: ReferenceCompat
3033
) {}

packages/storage/src/list.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
18+
19+
import * as types from '@firebase/storage-types';
1720
import { Reference } from './reference';
1821

1922
/**
2023
* @fileoverview Documentation for ListOptions and ListResult format.
2124
*/
22-
export interface ListOptions {
23-
maxResults?: number | null;
24-
pageToken?: string | null;
25+
export interface ListOptions extends types.ListOptions {
2526
}
2627

27-
export interface ListResult {
28-
prefixes: Reference[];
28+
export interface ListResult {
29+
prefixes: Reference[];
2930
items: Reference[];
30-
nextPageToken?: string | null;
31+
nextPageToken: string | null;
3132
}

packages/storage/src/metadata.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,14 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
18+
import * as types from '@firebase/storage-types';
1719
import { Reference } from './reference';
1820

1921
/**
2022
* @fileoverview Documentation for the metadata format.
2123
*/
22-
interface Metadata {
23-
bucket: string | undefined;
24-
generation: string | undefined;
25-
metageneration: string | undefined;
26-
fullPath: string | undefined;
27-
name: string | undefined;
28-
size: number | undefined;
29-
type: string | undefined;
30-
timeCreated: string | undefined;
31-
updated: string | undefined;
32-
md5Hash: string | undefined;
33-
cacheControl: string | undefined;
34-
contentDisposition: string | undefined;
35-
contentEncoding: string | undefined;
36-
contentLanguage: string | undefined;
37-
contentType: string | undefined;
38-
downloadTokens: string[] | undefined;
39-
customMetadata: { [key: string]: string } | undefined;
40-
ref: Reference | undefined;
41-
42-
[prop: string]: unknown;
24+
interface Metadata extends types.FullMetadata {
4325
}
4426

4527
export { Metadata };

packages/storage/src/service.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,11 @@ export function ref(
137137
* @struct
138138
*/
139139
export class StorageService {
140-
/**
141-
* @internal
142-
*/
143-
app_: FirebaseApp | null;
144140
/**
145141
* @internal
146142
*/
147143
readonly bucket_: Location | null = null;
148-
private readonly authProvider_: Provider<FirebaseAuthInternalName>;
149-
private readonly appId_: string | null = null;
150-
private readonly pool_: XhrIoPool;
144+
protected readonly appId_: string | null = null;
151145
private readonly requests_: Set<Request<unknown>>;
152146
/**
153147
* @internal
@@ -157,21 +151,18 @@ export class StorageService {
157151
protected maxUploadRetryTime_: number;
158152

159153
constructor(
160-
app: FirebaseApp | null,
161-
authProvider: Provider<FirebaseAuthInternalName>,
162-
pool: XhrIoPool,
163-
url?: string
154+
readonly app: FirebaseApp,
155+
readonly authProvider_: Provider<FirebaseAuthInternalName>,
156+
readonly pool_: XhrIoPool,
157+
readonly url_?: string
164158
) {
165-
this.app_ = app;
166-
this.authProvider_ = authProvider;
167159
this.maxOperationRetryTime_ = constants.DEFAULT_MAX_OPERATION_RETRY_TIME;
168160
this.maxUploadRetryTime_ = constants.DEFAULT_MAX_UPLOAD_RETRY_TIME;
169161
this.requests_ = new Set();
170-
this.pool_ = pool;
171-
if (url != null) {
172-
this.bucket_ = Location.makeFromBucketSpec(url);
162+
if (url_ != null) {
163+
this.bucket_ = Location.makeFromBucketSpec(url_);
173164
} else {
174-
this.bucket_ = StorageService.extractBucket_(this.app_?.options);
165+
this.bucket_ = StorageService.extractBucket_(this.app.options);
175166
}
176167
}
177168

@@ -200,7 +191,6 @@ export class StorageService {
200191
*/
201192
deleteApp(): void {
202193
this.deleted_ = true;
203-
this.app_ = null;
204194
this.requests_.forEach(request => request.cancel());
205195
this.requests_.clear();
206196
}
@@ -259,8 +249,4 @@ export class StorageService {
259249
set maxOperationRetryTime(time: number) {
260250
this.maxOperationRetryTime_ = time;
261251
}
262-
263-
get app(): FirebaseApp | null {
264-
return this.app_;
265-
}
266252
}

0 commit comments

Comments
 (0)