diff --git a/package.json b/package.json index 8ecbbac..946a9a8 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "gcs-upload": "build/src/cli.js" }, "scripts": { - "test": "nyc mocha build/test", + "test": "c8 mocha build/test", "lint": "gts check", "clean": "gts clean", "compile": "tsc -p .", @@ -21,7 +21,7 @@ "samples-test": "echo no samples 🤷♂️", "presystem-test": "npm run compile", "docs": "compodoc src/", - "docs-test": "linkinator docs -r --skip www.googleapis.com", + "docs-test": "linkinator docs -r", "predocs-test": "npm run docs" }, "keywords": [ @@ -58,6 +58,7 @@ "@types/node": "^10.3.0", "@types/pumpify": "^1.4.1", "assert-rejects": "^1.0.0", + "c8": "^5.0.1", "codecov": "^3.0.4", "gts": "^1.0.0", "intelli-espower-loader": "^1.0.1", @@ -66,7 +67,6 @@ "mocha": "^6.1.4", "mockery": "^2.1.0", "nock": "^10.0.0", - "nyc": "^14.0.0", "source-map-support": "^0.5.6", "typescript": "~3.5.0" } diff --git a/src/index.ts b/src/index.ts index 9ac4cdd..d25b166 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,6 @@ import * as Pumpify from 'pumpify'; import {PassThrough, Transform} from 'stream'; import * as streamEvents from 'stream-events'; -const BASE_URI = 'https://www.googleapis.com/upload/storage/v1/b'; const TERMINATED_UPLOAD_STATUS_CODE = 410; const RESUMABLE_INCOMPLETE_STATUS_CODE = 308; const RETRY_LIMIT = 5; @@ -31,6 +30,12 @@ export interface Encryption { } export interface UploadConfig { + /** + * The API endpoint used for the request. + * Defaults to `storage.googleapis.com`. + */ + apiEndpoint?: string; + /** * The name of the destination bucket. */ @@ -40,7 +45,12 @@ export interface UploadConfig { * The name of the destination file. */ file: string; + + /** + * The GoogleAuthOptions passed to google-auth-library + */ authConfig?: GoogleAuthOptions; + /** * If you want to re-use an auth client from google-auto-auth, pass an * instance here. @@ -141,6 +151,7 @@ export interface ConfigMetadata { export class Upload extends Pumpify { bucket: string; file: string; + apiEndpoint: string; authConfig?: {scopes?: string[]}; authClient: GoogleAuth; generation?: number; @@ -169,6 +180,10 @@ export class Upload extends Pumpify { private bufferStream?: PassThrough; private offsetStream?: PassThrough; + private get baseURI() { + return `https://${this.apiEndpoint}/upload/storage/v1/b`; + } + constructor(cfg: UploadConfig) { super(); streamEvents(this); @@ -185,6 +200,7 @@ export class Upload extends Pumpify { ]; this.authClient = cfg.authClient || new GoogleAuth(cfg.authConfig); + this.apiEndpoint = cfg.apiEndpoint || 'storage.googleapis.com'; this.bucket = cfg.bucket; this.file = cfg.file; this.generation = cfg.generation; @@ -254,7 +270,7 @@ export class Upload extends Pumpify { const reqOpts: GaxiosOptions = { method: 'POST', - url: [BASE_URI, this.bucket, 'o'].join('/'), + url: [this.baseURI, this.bucket, 'o'].join('/'), params: {name: this.file, uploadType: 'resumable'}, data: metadata, headers: {}, diff --git a/test/test.ts b/test/test.ts index 6cc5541..94a6e11 100644 --- a/test/test.ts +++ b/test/test.ts @@ -71,6 +71,8 @@ describe('gcs-resumable-upload', () => { const ORIGIN = '*'; const PREDEFINED_ACL = 'authenticatedRead'; const USER_PROJECT = 'user-project-id'; + const API_ENDPOINT = 'fake.googleapis.com'; + const BASE_URI = `https://${API_ENDPOINT}/upload/storage/v1/b`; let REQ_OPTS: GaxiosOptions; const keyFile = path.join(__dirname, '../../test/fixtures/keys.json'); @@ -93,6 +95,7 @@ describe('gcs-resumable-upload', () => { predefinedAcl: PREDEFINED_ACL, userProject: USER_PROJECT, authConfig: {keyFile}, + apiEndpoint: API_ENDPOINT, }); }); @@ -121,6 +124,11 @@ describe('gcs-resumable-upload', () => { assert.strictEqual(up.generation, GENERATION); }); + it('should localize the apiEndpoint', () => { + assert.strictEqual(up.apiEndpoint, API_ENDPOINT); + assert.strictEqual(up.baseURI, BASE_URI); + }); + it('should localize the KMS key name', () => { const kmsKeyName = 'kms-key-name'; const up = upload({bucket: 'BUCKET', file: FILE, kmsKeyName}); @@ -257,10 +265,7 @@ describe('gcs-resumable-upload', () => { it('should make the correct request', done => { up.makeRequest = async (reqOpts: GaxiosOptions) => { assert.strictEqual(reqOpts.method, 'POST'); - assert.strictEqual( - reqOpts.url, - `https://www.googleapis.com/upload/storage/v1/b/${BUCKET}/o` - ); + assert.strictEqual(reqOpts.url, `${BASE_URI}/${BUCKET}/o`); assert.deepStrictEqual(reqOpts.params, { predefinedAcl: up.predefinedAcl, name: FILE,