Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

feat: support apiEndpoint override #230

Merged
merged 1 commit into from
Jun 17, 2019
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand All @@ -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": [
Expand Down Expand Up @@ -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",
Expand All @@ -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"
}
Expand Down
20 changes: 18 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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: {},
Expand Down
13 changes: 9 additions & 4 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -93,6 +95,7 @@ describe('gcs-resumable-upload', () => {
predefinedAcl: PREDEFINED_ACL,
userProject: USER_PROJECT,
authConfig: {keyFile},
apiEndpoint: API_ENDPOINT,
});
});

Expand Down Expand Up @@ -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});
Expand Down Expand Up @@ -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,
Expand Down