From 05406d3534935174a8ab0c7194185bd29beeca01 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Thu, 8 Feb 2024 16:03:01 -0400 Subject: [PATCH 1/3] feat: add support for TPC Universes --- samples/package.json | 2 +- src/bigquery.ts | 25 ++++++++++++++++++++++++- test/bigquery.ts | 10 ++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/samples/package.json b/samples/package.json index 71b15b52..4fc023e5 100644 --- a/samples/package.json +++ b/samples/package.json @@ -19,7 +19,7 @@ "dependencies": { "@google-cloud/bigquery": "^7.3.0", "@google-cloud/storage": "^7.0.0", - "google-auth-library": "^9.0.0", + "google-auth-library": "^9.6.0", "readline-promise": "^1.0.4", "yargs": "^17.0.0" }, diff --git a/src/bigquery.ts b/src/bigquery.ts index c2a2983d..25137d1e 100644 --- a/src/bigquery.ts +++ b/src/bigquery.ts @@ -227,6 +227,12 @@ export interface BigQueryOptions extends GoogleAuthOptions { * Defaults to `bigquery.googleapis.com`. */ apiEndpoint?: string; + + /** + * The Trusted Cloud Domain (TPC) DNS of the service used to make requests. + * Defaults to `googleapis.com`. + */ + universeDomain?: string; } export interface IntegerTypeCastOptions { @@ -311,6 +317,7 @@ export const PROTOCOL_REGEX = /^(\w*):\/\//; */ export class BigQuery extends Service { location?: string; + private _universeDomain: string; createQueryStream(options?: Query | string): ResourceStream { // placeholder body, overwritten in constructor @@ -328,10 +335,17 @@ export class BigQuery extends Service { } constructor(options: BigQueryOptions = {}) { - let apiEndpoint = 'https://bigquery.googleapis.com'; + let universeDomain = "googleapis.com"; + const servicePath = "bigquery"; + + if (options.universeDomain) { + universeDomain = BigQuery.sanitizeDomain(options.universeDomain); + } const EMULATOR_HOST = process.env.BIGQUERY_EMULATOR_HOST; + let apiEndpoint = `https://${servicePath}.${universeDomain}`; + if (typeof EMULATOR_HOST === 'string') { apiEndpoint = BigQuery.sanitizeEndpoint(EMULATOR_HOST); } @@ -361,6 +375,7 @@ export class BigQuery extends Service { super(config, options); + this._universeDomain = universeDomain; this.location = options.location; /** * Run a query scoped to your project as a readable object stream. @@ -473,10 +488,18 @@ export class BigQuery extends Service { }); } + get universeDomain(){ + return this._universeDomain; + } + private static sanitizeEndpoint(url: string) { if (!PROTOCOL_REGEX.test(url)) { url = `https://${url}`; } + return this.sanitizeDomain(url); + } + + private static sanitizeDomain(url: string) { return url.replace(/\/+$/, ''); // Remove trailing slashes } diff --git a/test/bigquery.ts b/test/bigquery.ts index 1a6f51b9..7db13e18 100644 --- a/test/bigquery.ts +++ b/test/bigquery.ts @@ -263,6 +263,16 @@ describe('BigQuery', () => { assert.strictEqual(calledWith.apiEndpoint, 'https://some.fake.endpoint'); }); + it('should allow overriding TPC universe', () => { + const universeDomain = 'apis-tpclp.goog/'; + bq = new BigQuery({ + universeDomain: universeDomain, + }); + const calledWith = bq.calledWith_[0]; + assert.strictEqual(calledWith.baseUrl, `https://bigquery.apis-tpclp.goog/bigquery/v2`); + assert.strictEqual(calledWith.apiEndpoint, `https://bigquery.apis-tpclp.goog`); + }) + it('should capture any user specified location', () => { const bq = new BigQuery({ projectId: PROJECT_ID, From 0d885645f5d23000697beadfcc201b5fca5067a7 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Thu, 8 Feb 2024 16:06:14 -0400 Subject: [PATCH 2/3] fix: lint issues --- src/bigquery.ts | 12 ++++++------ test/bigquery.ts | 12 +++++++++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/bigquery.ts b/src/bigquery.ts index 25137d1e..f3e14113 100644 --- a/src/bigquery.ts +++ b/src/bigquery.ts @@ -335,8 +335,8 @@ export class BigQuery extends Service { } constructor(options: BigQueryOptions = {}) { - let universeDomain = "googleapis.com"; - const servicePath = "bigquery"; + let universeDomain = 'googleapis.com'; + const servicePath = 'bigquery'; if (options.universeDomain) { universeDomain = BigQuery.sanitizeDomain(options.universeDomain); @@ -344,8 +344,8 @@ export class BigQuery extends Service { const EMULATOR_HOST = process.env.BIGQUERY_EMULATOR_HOST; - let apiEndpoint = `https://${servicePath}.${universeDomain}`; - + let apiEndpoint = `https://${servicePath}.${universeDomain}`; + if (typeof EMULATOR_HOST === 'string') { apiEndpoint = BigQuery.sanitizeEndpoint(EMULATOR_HOST); } @@ -488,7 +488,7 @@ export class BigQuery extends Service { }); } - get universeDomain(){ + get universeDomain() { return this._universeDomain; } @@ -499,7 +499,7 @@ export class BigQuery extends Service { return this.sanitizeDomain(url); } - private static sanitizeDomain(url: string) { + private static sanitizeDomain(url: string) { return url.replace(/\/+$/, ''); // Remove trailing slashes } diff --git a/test/bigquery.ts b/test/bigquery.ts index 7db13e18..9cbcb417 100644 --- a/test/bigquery.ts +++ b/test/bigquery.ts @@ -269,9 +269,15 @@ describe('BigQuery', () => { universeDomain: universeDomain, }); const calledWith = bq.calledWith_[0]; - assert.strictEqual(calledWith.baseUrl, `https://bigquery.apis-tpclp.goog/bigquery/v2`); - assert.strictEqual(calledWith.apiEndpoint, `https://bigquery.apis-tpclp.goog`); - }) + assert.strictEqual( + calledWith.baseUrl, + 'https://bigquery.apis-tpclp.goog/bigquery/v2' + ); + assert.strictEqual( + calledWith.apiEndpoint, + 'https://bigquery.apis-tpclp.goog' + ); + }); it('should capture any user specified location', () => { const bq = new BigQuery({ From 4c7aa35c40f8dc399f6e4d1adb494a966c562435 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Fri, 9 Feb 2024 16:47:03 -0400 Subject: [PATCH 3/3] fix: remove tpc domain --- test/bigquery.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/bigquery.ts b/test/bigquery.ts index 9cbcb417..34669d1b 100644 --- a/test/bigquery.ts +++ b/test/bigquery.ts @@ -264,18 +264,18 @@ describe('BigQuery', () => { }); it('should allow overriding TPC universe', () => { - const universeDomain = 'apis-tpclp.goog/'; + const universeDomain = 'fake-tpc-env.example.com/'; bq = new BigQuery({ universeDomain: universeDomain, }); const calledWith = bq.calledWith_[0]; assert.strictEqual( calledWith.baseUrl, - 'https://bigquery.apis-tpclp.goog/bigquery/v2' + 'https://bigquery.fake-tpc-env.example.com/bigquery/v2' ); assert.strictEqual( calledWith.apiEndpoint, - 'https://bigquery.apis-tpclp.goog' + 'https://bigquery.fake-tpc-env.example.com' ); });