Skip to content

Commit 2119310

Browse files
Prefix list results with correct bucket (#2905)
1 parent 92aa385 commit 2119310

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

packages/storage/src/implementation/list.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2019 Google Inc.
3+
* Copyright 2019 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.
@@ -23,7 +23,6 @@ import { Location } from './location';
2323
import * as json from './json';
2424
import * as type from './type';
2525
import { ListResult } from '../list';
26-
import * as errors from './error';
2726

2827
/**
2928
* Represents the simplified object metadata returned by List API.
@@ -52,17 +51,14 @@ const ITEMS_KEY = 'items';
5251

5352
function fromBackendResponse(
5453
authWrapper: AuthWrapper,
54+
bucket: string,
5555
resource: ListResultResponse
5656
): ListResult {
5757
const listResult: ListResult = {
5858
prefixes: [],
5959
items: [],
6060
nextPageToken: resource['nextPageToken']
6161
};
62-
const bucket = authWrapper.bucket();
63-
if (bucket === null) {
64-
throw errors.noDefaultBucket();
65-
}
6662
if (resource[PREFIXES_KEY]) {
6763
for (const path of resource[PREFIXES_KEY]) {
6864
const pathWithoutTrailingSlash = path.replace(/\/$/, '');
@@ -86,14 +82,15 @@ function fromBackendResponse(
8682

8783
export function fromResponseString(
8884
authWrapper: AuthWrapper,
85+
bucket: string,
8986
resourceString: string
9087
): ListResult | null {
9188
const obj = json.jsonObjectOrNull(resourceString);
9289
if (obj === null) {
9390
return null;
9491
}
9592
const resource = (obj as unknown) as ListResultResponse;
96-
return fromBackendResponse(authWrapper, resource);
93+
return fromBackendResponse(authWrapper, bucket, resource);
9794
}
9895

9996
export function listOptionsValidator(p: unknown): void {

packages/storage/src/implementation/requests.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 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.
@@ -67,10 +67,15 @@ export function metadataHandler(
6767
}
6868

6969
export function listHandler(
70-
authWrapper: AuthWrapper
70+
authWrapper: AuthWrapper,
71+
bucket: string
7172
): (p1: XhrIo, p2: string) => ListResult {
7273
function handler(xhr: XhrIo, text: string): ListResult {
73-
const listResult = ListResultUtils.fromResponseString(authWrapper, text);
74+
const listResult = ListResultUtils.fromResponseString(
75+
authWrapper,
76+
bucket,
77+
text
78+
);
7479
handlerCheck(listResult !== null);
7580
return listResult as ListResult;
7681
}
@@ -190,7 +195,7 @@ export function list(
190195
const requestInfo = new RequestInfo(
191196
url,
192197
method,
193-
listHandler(authWrapper),
198+
listHandler(authWrapper, location.bucket),
194199
timeout
195200
);
196201
requestInfo.urlParams = urlParams;

packages/storage/test/requests.test.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 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.
@@ -41,9 +41,11 @@ import { FirebaseApp } from '@firebase/app-types';
4141

4242
describe('Firebase Storage > Requests', () => {
4343
const normalBucket = 'b';
44+
const differentBucket = 'c';
4445
const locationRoot = new Location(normalBucket, '');
4546
const locationNormal = new Location(normalBucket, 'o');
4647
const locationNormalUrl = '/b/' + normalBucket + '/o/o';
48+
const locationDifferentBucket = new Location(differentBucket, '');
4749
const locationNormalNoObjUrl = '/b/' + normalBucket + '/o';
4850
const locationEscapes = new Location('b/', 'o?');
4951
const locationEscapesUrl = '/b/b%2F/o/o%3F';
@@ -269,11 +271,11 @@ describe('Firebase Storage > Requests', () => {
269271
items: [
270272
{
271273
name: 'a/a',
272-
bucket: 'fredzqm-staging'
274+
bucket: normalBucket
273275
},
274276
{
275277
name: 'a/b',
276-
bucket: 'fredzqm-staging'
278+
bucket: normalBucket
277279
}
278280
],
279281
nextPageToken: pageToken
@@ -286,6 +288,23 @@ describe('Firebase Storage > Requests', () => {
286288
assert.equal(listResult.nextPageToken, pageToken);
287289
});
288290

291+
it('list handler with custom bucket', () => {
292+
const requestInfo = requests.list(authWrapper, locationDifferentBucket);
293+
const pageToken = 'YS9mLw==';
294+
const listResponse = {
295+
items: [
296+
{
297+
name: 'a/a',
298+
bucket: differentBucket
299+
}
300+
],
301+
nextPageToken: pageToken
302+
};
303+
const listResponseString = JSON.stringify(listResponse);
304+
const listResult = requestInfo.handler(fakeXhrIo({}), listResponseString);
305+
assert.equal(listResult.items[0].bucket, differentBucket);
306+
});
307+
289308
it('getDownloadUrl request info', () => {
290309
const maps: Array<[Location, string]> = [
291310
[locationNormal, locationNormalUrl],

0 commit comments

Comments
 (0)