Skip to content

Commit

Permalink
fix: allow leading slashes in filename.
Browse files Browse the repository at this point in the history
allow interop with other languages and current storage files with leading slash
  • Loading branch information
brianzinn committed Aug 21, 2019
1 parent 0eb689f commit b9ef7c1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -106,7 +106,7 @@
"jsdoc-fresh": "^1.0.1",
"linkinator": "^1.5.0",
"mocha": "^6.0.0",
"nock": "^11.0.0",
"nock": "~11.0.0",
"node-fetch": "^2.2.0",
"normalize-newline": "^3.0.0",
"nyc": "^14.0.0",
Expand Down
5 changes: 4 additions & 1 deletion src/file.ts
Expand Up @@ -291,6 +291,7 @@ export interface FileOptions {
generation?: number | string;
kmsKeyName?: string;
userProject?: string;
keepLeadingSlashes?: boolean;
}

export interface CopyOptions {
Expand Down Expand Up @@ -497,7 +498,9 @@ class File extends ServiceObject<File> {
* const file = myBucket.file('my-file');
*/
constructor(bucket: Bucket, name: string, options: FileOptions = {}) {
name = name.replace(/^\/+/, '');
if (options.keepLeadingSlashes !== true) {
name = name.replace(/^\/+/, '');
}

const requestQueryObject: {generation?: number; userProject?: string} = {};

Expand Down
11 changes: 11 additions & 0 deletions test/file.ts
Expand Up @@ -244,6 +244,17 @@ describe('File', () => {
assert.strictEqual(file.name, 'name');
});

it('should strip two leading slashes', () => {
const file = new File(BUCKET, '//name');
assert.strictEqual(file.name, 'name');
});

it('should keep leading slashes when specified in options', () => {
const keepLeadingSlashes = true;
const file = new File(BUCKET, '/name', {keepLeadingSlashes});
assert.strictEqual(file.name, '/name');
})

it('should assign KMS key name', () => {
const kmsKeyName = 'kms-key-name';
const file = new File(BUCKET, '/name', {kmsKeyName});
Expand Down

0 comments on commit b9ef7c1

Please sign in to comment.