Skip to content

Commit

Permalink
refactor: Use Storage Context
Browse files Browse the repository at this point in the history
  • Loading branch information
d-goog committed Feb 5, 2024
1 parent 3654b0d commit c439bca
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
4 changes: 2 additions & 2 deletions conformance-test/v4SignedUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('v4 conformance test', () => {

storage = new Storage({
keyFilename: SERVICE_ACCOUNT,
apiEndpoint: testCase.hostname,
apiEndpoint: testCase.clientEndpoint,
universeDomain: testCase.universeDomain,
});

Expand All @@ -159,7 +159,7 @@ describe('v4 conformance test', () => {
extensionHeaders,
version,
expires,
cname: testCase.clientEndpoint || bucketBoundHostname,
cname: bucketBoundHostname,
virtualHostedStyle,
queryParams,
host,
Expand Down
2 changes: 1 addition & 1 deletion src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3150,7 +3150,7 @@ class Bucket extends ServiceObject<Bucket, BucketMetadata> {
this.storage.authClient,
this,
undefined,
this.storage.universeDomain
this.storage
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3034,7 +3034,7 @@ class File extends ServiceObject<File, FileMetadata> {
this.storage.authClient,
this.bucket,
this,
this.storage.universeDomain
this.storage
);
}

Expand Down
28 changes: 21 additions & 7 deletions src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import * as crypto from 'crypto';
import * as http from 'http';
import * as url from 'url';
import {ExceptionMessages} from './storage.js';
import {ExceptionMessages, Storage} from './storage.js';
import {encodeURI, qsStringify, objectEntries, formatAsUTCISO} from './util.js';
import {DEFAULT_UNIVERSE, GoogleAuth} from 'google-auth-library';

Check warning on line 20 in src/signer.ts

View workflow job for this annotation

GitHub Actions / lint

'DEFAULT_UNIVERSE' is defined but never used

Expand Down Expand Up @@ -54,6 +54,13 @@ export interface GetSignedUrlConfigInternal {
contentType?: string;
bucket: string;
file?: string;
/**
* The host for the generated signed URL
*
* @example
* 'https://localhost:8080/'
*/
host?: string | URL;
/**
* An endpoint for generating the signed URL
*
Expand Down Expand Up @@ -136,7 +143,16 @@ export class URLSigner {
private auth: AuthClient | GoogleAuthLike,
private bucket: BucketI,
private file?: FileI,
private universeDomain = DEFAULT_UNIVERSE
/**
* A {@link Storage} object.
*
* @privateRemarks
*
* Technically this is a required field, however it would be a breaking change to
* move it before optional properties. In the next major we should refactor the
* constructor of this class to only accept a config object.
*/
private storage: Storage = new Storage()
) {}

getSignedUrl(
Expand All @@ -159,7 +175,7 @@ export class URLSigner {
if (cfg.cname) {
customHost = cfg.cname;
} else if (isVirtualHostedStyle) {
customHost = `https://${this.bucket.name}.storage.${this.universeDomain}`;
customHost = `https://${this.bucket.name}.storage.${this.storage.universeDomain}`;
}

const secondsToMilliseconds = 1000;
Expand Down Expand Up @@ -192,9 +208,7 @@ export class URLSigner {
query = Object.assign(query, cfg.queryParams);

const signedUrl = new url.URL(
cfg.host?.toString() ||
config.cname ||
`https://storage.${this.universeDomain}`
cfg.host?.toString() || config.cname || this.storage.apiEndpoint
);

signedUrl.pathname = this.getResourcePath(
Expand Down Expand Up @@ -271,7 +285,7 @@ export class URLSigner {

const extensionHeaders = Object.assign({}, config.extensionHeaders);
const fqdn = new url.URL(
config.cname || `https://storage.${this.universeDomain}`
config.host?.toString() || config.cname || this.storage.apiEndpoint
);
extensionHeaders.host = fqdn.host;
if (config.contentMd5) {
Expand Down
9 changes: 6 additions & 3 deletions test/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
SignerExceptionMessages,
} from '../src/signer.js';
import {encodeURI, formatAsUTCISO, qsStringify} from '../src/util.js';
import {ExceptionMessages} from '../src/storage.js';
import {ExceptionMessages, Storage} from '../src/storage.js';
import {OutgoingHttpHeaders} from 'http';
import {GoogleAuth} from 'google-auth-library';

Expand Down Expand Up @@ -93,9 +93,12 @@ describe('signer', () => {

describe('getSignedUrl', () => {
let signer: URLSigner;
let storage: Storage;
let CONFIG: SignerGetSignedUrlConfig;

beforeEach(() => {
signer = new URLSigner(authClient, bucket, file);
storage = new Storage();
signer = new URLSigner(authClient, bucket, file, storage);

CONFIG = {
method: 'GET',
Expand Down Expand Up @@ -320,7 +323,7 @@ describe('signer', () => {
});

it('should use a universe domain with the virtual host', async () => {
signer['universeDomain'] = 'my-universe.com';
storage.universeDomain = 'my-universe.com';

CONFIG.virtualHostedStyle = true;
const expectedCname = `https://${bucket.name}.storage.my-universe.com`;
Expand Down

0 comments on commit c439bca

Please sign in to comment.