From 6ba4f4d9834fcdf6a7990f807ea45dd990f2c121 Mon Sep 17 00:00:00 2001 From: Brian Zinn Date: Tue, 20 Aug 2019 21:31:06 -0700 Subject: [PATCH 1/3] fix: allow leading slashes in filename allow interop with other languages and current storage files with leading slash --- package.json | 2 +- src/file.ts | 5 ++++- test/file.ts | 11 +++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e3fea28ce..e2e23f5b8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/file.ts b/src/file.ts index 3de579d22..7067fb48c 100644 --- a/src/file.ts +++ b/src/file.ts @@ -291,6 +291,7 @@ export interface FileOptions { generation?: number | string; kmsKeyName?: string; userProject?: string; + keepLeadingSlashes?: boolean; } export interface CopyOptions { @@ -497,7 +498,9 @@ class File extends ServiceObject { * 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} = {}; diff --git a/test/file.ts b/test/file.ts index fb6535e5b..c6f2e02d8 100644 --- a/test/file.ts +++ b/test/file.ts @@ -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}); From 27b75328147690990e4b31209db8d3d12b6bbeb3 Mon Sep 17 00:00:00 2001 From: Brian Zinn Date: Thu, 22 Aug 2019 23:04:24 -0700 Subject: [PATCH 2/3] Match expected behavior for file name. Leading slashes are maintained without options. This is a breaking change. --- src/file.ts | 5 ----- test/file.ts | 30 ++++++++++++++---------------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/file.ts b/src/file.ts index 19d534775..cba69f88f 100644 --- a/src/file.ts +++ b/src/file.ts @@ -291,7 +291,6 @@ export interface FileOptions { generation?: number | string; kmsKeyName?: string; userProject?: string; - keepLeadingSlashes?: boolean; } export interface CopyOptions { @@ -498,10 +497,6 @@ class File extends ServiceObject { * const file = myBucket.file('my-file'); */ constructor(bucket: Bucket, name: string, options: FileOptions = {}) { - if (options.keepLeadingSlashes !== true) { - name = name.replace(/^\/+/, ''); - } - const requestQueryObject: {generation?: number; userProject?: string} = {}; let generation: number; diff --git a/test/file.ts b/test/file.ts index 0e9bf06ce..add5d3bdd 100644 --- a/test/file.ts +++ b/test/file.ts @@ -239,21 +239,10 @@ describe('File', () => { assert.strictEqual(file.storage, BUCKET.storage); }); - it('should strip a single leading slash', () => { + it('should not strip leading slashes', () => { const file = new File(BUCKET, '/name'); - 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'; @@ -313,11 +302,11 @@ describe('File', () => { }); }); - it('should use stripped leading slash name in ServiceObject', () => { + it('should not strip leading slash name in ServiceObject', () => { const file = new File(BUCKET, '/name'); const calledWith = file.calledWith_[0]; - assert.strictEqual(calledWith.id, 'name'); + assert.strictEqual(calledWith.id, encodeURIComponent('/name')); }); it('should set a custom encryption key', done => { @@ -549,13 +538,22 @@ describe('File', () => { } it('should allow a string', done => { - const newFileName = '/new-file-name.png'; + const newFileName = 'new-file-name.png'; const newFile = new File(BUCKET, newFileName); const expectedPath = `/rewriteTo/b/${file.bucket.name}/o/${newFile.name}`; assertPathEquals(file, expectedPath, done); file.copy(newFileName); }); + it('should allow a string with leading slash.', done => { + const newFileName = '/new-file-name.png'; + const newFile = new File(BUCKET, newFileName); + // File uri encodes file name when calling this.request during copy + const expectedPath = `/rewriteTo/b/${file.bucket.name}/o/${encodeURIComponent(newFile.name)}`; + assertPathEquals(file, expectedPath, done); + file.copy(newFileName); + }); + it('should allow a "gs://..." string', done => { const newFileName = 'gs://other-bucket/new-file-name.png'; const expectedPath = `/rewriteTo/b/other-bucket/o/new-file-name.png`; From 10c2f0743d9de233a2392dedcacaca61df7b8cc7 Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 14 Oct 2019 11:26:11 -0400 Subject: [PATCH 3/3] lint --- test/file.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/file.ts b/test/file.ts index eea5b24f3..94f91f7ad 100644 --- a/test/file.ts +++ b/test/file.ts @@ -625,7 +625,9 @@ describe('File', () => { const newFileName = '/new-file-name.png'; const newFile = new File(BUCKET, newFileName); // File uri encodes file name when calling this.request during copy - const expectedPath = `/rewriteTo/b/${file.bucket.name}/o/${encodeURIComponent(newFile.name)}`; + const expectedPath = `/rewriteTo/b/${ + file.bucket.name + }/o/${encodeURIComponent(newFile.name)}`; assertPathEquals(file, expectedPath, done); file.copy(newFileName); });