Skip to content

Prefix list results with correct bucket #2905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions packages/storage/src/implementation/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2019 Google Inc.
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,6 @@ import { Location } from './location';
import * as json from './json';
import * as type from './type';
import { ListResult } from '../list';
import * as errors from './error';

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

function fromBackendResponse(
authWrapper: AuthWrapper,
bucket: string,
resource: ListResultResponse
): ListResult {
const listResult: ListResult = {
prefixes: [],
items: [],
nextPageToken: resource['nextPageToken']
};
const bucket = authWrapper.bucket();
if (bucket === null) {
throw errors.noDefaultBucket();
}
if (resource[PREFIXES_KEY]) {
for (const path of resource[PREFIXES_KEY]) {
const pathWithoutTrailingSlash = path.replace(/\/$/, '');
Expand All @@ -86,14 +82,15 @@ function fromBackendResponse(

export function fromResponseString(
authWrapper: AuthWrapper,
bucket: string,
resourceString: string
): ListResult | null {
const obj = json.jsonObjectOrNull(resourceString);
if (obj === null) {
return null;
}
const resource = (obj as unknown) as ListResultResponse;
return fromBackendResponse(authWrapper, resource);
return fromBackendResponse(authWrapper, bucket, resource);
}

export function listOptionsValidator(p: unknown): void {
Expand Down
13 changes: 9 additions & 4 deletions packages/storage/src/implementation/requests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -67,10 +67,15 @@ export function metadataHandler(
}

export function listHandler(
authWrapper: AuthWrapper
authWrapper: AuthWrapper,
bucket: string
): (p1: XhrIo, p2: string) => ListResult {
function handler(xhr: XhrIo, text: string): ListResult {
const listResult = ListResultUtils.fromResponseString(authWrapper, text);
const listResult = ListResultUtils.fromResponseString(
authWrapper,
bucket,
text
);
handlerCheck(listResult !== null);
return listResult as ListResult;
}
Expand Down Expand Up @@ -190,7 +195,7 @@ export function list(
const requestInfo = new RequestInfo(
url,
method,
listHandler(authWrapper),
listHandler(authWrapper, location.bucket),
timeout
);
requestInfo.urlParams = urlParams;
Expand Down
25 changes: 22 additions & 3 deletions packages/storage/test/requests.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,9 +41,11 @@ import { FirebaseApp } from '@firebase/app-types';

describe('Firebase Storage > Requests', () => {
const normalBucket = 'b';
const differentBucket = 'c';
const locationRoot = new Location(normalBucket, '');
const locationNormal = new Location(normalBucket, 'o');
const locationNormalUrl = '/b/' + normalBucket + '/o/o';
const locationDifferentBucket = new Location(differentBucket, '');
const locationNormalNoObjUrl = '/b/' + normalBucket + '/o';
const locationEscapes = new Location('b/', 'o?');
const locationEscapesUrl = '/b/b%2F/o/o%3F';
Expand Down Expand Up @@ -269,11 +271,11 @@ describe('Firebase Storage > Requests', () => {
items: [
{
name: 'a/a',
bucket: 'fredzqm-staging'
bucket: normalBucket
},
{
name: 'a/b',
bucket: 'fredzqm-staging'
bucket: normalBucket
}
],
nextPageToken: pageToken
Expand All @@ -286,6 +288,23 @@ describe('Firebase Storage > Requests', () => {
assert.equal(listResult.nextPageToken, pageToken);
});

it('list handler with custom bucket', () => {
const requestInfo = requests.list(authWrapper, locationDifferentBucket);
const pageToken = 'YS9mLw==';
const listResponse = {
items: [
{
name: 'a/a',
bucket: differentBucket
}
],
nextPageToken: pageToken
};
const listResponseString = JSON.stringify(listResponse);
const listResult = requestInfo.handler(fakeXhrIo({}), listResponseString);
assert.equal(listResult.items[0].bucket, differentBucket);
});

it('getDownloadUrl request info', () => {
const maps: Array<[Location, string]> = [
[locationNormal, locationNormalUrl],
Expand Down