Skip to content

Commit

Permalink
feat: Add Custom Endpoint Support for generateSignedPostPolicyV4
Browse files Browse the repository at this point in the history
  • Loading branch information
d-goog committed Feb 7, 2024
1 parent fedab76 commit 1df9734
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2786,7 +2786,9 @@ class File extends ServiceObject<File, FileMetadata> {

let url: string;

if (options.virtualHostedStyle) {
if (this.storage.customEndpoint) {
url = this.storage.apiEndpoint;
} else if (options.virtualHostedStyle) {
url = `https://${this.bucket.name}.storage.${universe}/`;
} else if (options.bucketBoundHostname) {
url = `${options.bucketBoundHostname}/`;
Expand Down
7 changes: 7 additions & 0 deletions src/nodejs-common/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export interface ServiceConfig {
* Reuse an existing `AuthClient` or `GoogleAuth` client instead of creating a new one.
*/
authClient?: AuthClient | GoogleAuth;

/**
* Set to true if the endpoint is a custom URL
*/
customEndpoint?: boolean;
}

export interface ServiceOptions extends Omit<GoogleAuthOptions, 'authClient'> {
Expand All @@ -92,6 +97,7 @@ export class Service {
apiEndpoint: string;
timeout?: number;
universeDomain: string;
customEndpoint: boolean;

/**
* Service is a base class, meant to be inherited from by a "service," like
Expand Down Expand Up @@ -121,6 +127,7 @@ export class Service {
this.projectIdRequired = config.projectIdRequired !== false;
this.providedUserAgent = options.userAgent;
this.universeDomain = options.universeDomain || DEFAULT_UNIVERSE;
this.customEndpoint = config.customEndpoint || false;

this.makeAuthenticatedRequest = util.makeAuthenticatedRequestFactory({
...config,
Expand Down
20 changes: 20 additions & 0 deletions test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ describe('File', () => {
},
idempotencyStrategy: IdempotencyStrategy.RetryConditional,
},
customEndpoint: false,
};

BUCKET = new Bucket(STORAGE, 'bucket-name');
Expand Down Expand Up @@ -3387,6 +3388,25 @@ describe('File', () => {
);
});

it('should prefer a customEndpoint > virtualHostedStyle, cname', done => {
const customEndpoint = 'https://my-custom-endpoint.com';

STORAGE.apiEndpoint = customEndpoint;
STORAGE.customEndpoint = true;

CONFIG.virtualHostedStyle = true;
CONFIG.bucketBoundHostname = 'http://domain.tld';

file.generateSignedPostPolicyV4(
CONFIG,
(err: Error, res: SignedPostPolicyV4Output) => {
assert.ifError(err);
assert(res.url, `https://${BUCKET.name}.storage.googleapis.com/`);
done();
}
);
});

describe('expires', () => {
it('should accept Date objects', done => {
const expires = new Date(Date.now() + 1000 * 60);
Expand Down
3 changes: 1 addition & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,7 @@ describe('Storage', () => {
projectId: PROJECT_ID,
});

const calledWith = storage.calledWith_[0];
assert.strictEqual(calledWith.customEndpoint, true);
assert.strictEqual(storage.customEndpoint, true);
});
});
});
Expand Down

0 comments on commit 1df9734

Please sign in to comment.